| 1 | # This shell script emits a C file. -*- C -*- | 
|---|
| 2 | #   Copyright 2001, 2002 Free Software Foundation, Inc. | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of GLD, the Gnu Linker. | 
|---|
| 5 | # | 
|---|
| 6 | # This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | # it under the terms of the GNU General Public License as published by | 
|---|
| 8 | # the Free Software Foundation; either version 2 of the License, or | 
|---|
| 9 | # (at your option) any later version. | 
|---|
| 10 | # | 
|---|
| 11 | # This program is distributed in the hope that it will be useful, | 
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | # GNU General Public License for more details. | 
|---|
| 15 | # | 
|---|
| 16 | # You should have received a copy of the GNU General Public License | 
|---|
| 17 | # along with this program; if not, write to the Free Software | 
|---|
| 18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
|---|
| 19 | # | 
|---|
| 20 |  | 
|---|
| 21 | # This file is sourced from elf32.em and mmo.em, used to define | 
|---|
| 22 | # linker MMIX-specifics common to ELF and MMO. | 
|---|
| 23 |  | 
|---|
| 24 | cat >>e${EMULATION_NAME}.c <<EOF | 
|---|
| 25 | /* Need to have this define before mmix-elfnmmo, which includes | 
|---|
| 26 | needrelax.em which uses this name for the before_allocation function, | 
|---|
| 27 | normally defined in elf32.em.  */ | 
|---|
| 28 | #define gldmmo_before_allocation before_allocation_default | 
|---|
| 29 | EOF | 
|---|
| 30 |  | 
|---|
| 31 | . ${srcdir}/emultempl/mmix-elfnmmo.em | 
|---|
| 32 |  | 
|---|
| 33 | cat >>e${EMULATION_NAME}.c <<EOF | 
|---|
| 34 |  | 
|---|
| 35 | static bfd_boolean mmo_place_orphan | 
|---|
| 36 | PARAMS ((lang_input_statement_type *, asection *)); | 
|---|
| 37 | static asection *output_prev_sec_find | 
|---|
| 38 | PARAMS ((lang_output_section_statement_type *)); | 
|---|
| 39 | static void mmo_finish PARAMS ((void)); | 
|---|
| 40 | static void mmo_wipe_sec_reloc_flag PARAMS ((bfd *, asection *, PTR)); | 
|---|
| 41 | static void mmo_after_open PARAMS ((void)); | 
|---|
| 42 |  | 
|---|
| 43 | /* Find the last output section before given output statement. | 
|---|
| 44 | Used by place_orphan.  */ | 
|---|
| 45 |  | 
|---|
| 46 | static asection * | 
|---|
| 47 | output_prev_sec_find (os) | 
|---|
| 48 | lang_output_section_statement_type *os; | 
|---|
| 49 | { | 
|---|
| 50 | asection *s = (asection *) NULL; | 
|---|
| 51 | lang_statement_union_type *u; | 
|---|
| 52 | lang_output_section_statement_type *lookup; | 
|---|
| 53 |  | 
|---|
| 54 | for (u = lang_output_section_statement.head; | 
|---|
| 55 | u != (lang_statement_union_type *) NULL; | 
|---|
| 56 | u = lookup->next) | 
|---|
| 57 | { | 
|---|
| 58 | lookup = &u->output_section_statement; | 
|---|
| 59 | if (lookup == os) | 
|---|
| 60 | break; | 
|---|
| 61 | if (lookup->bfd_section != NULL | 
|---|
| 62 | && lookup->bfd_section != bfd_abs_section_ptr | 
|---|
| 63 | && lookup->bfd_section != bfd_com_section_ptr | 
|---|
| 64 | && lookup->bfd_section != bfd_und_section_ptr) | 
|---|
| 65 | s = lookup->bfd_section; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | if (u == NULL) | 
|---|
| 69 | return NULL; | 
|---|
| 70 |  | 
|---|
| 71 | return s; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | struct orphan_save { | 
|---|
| 75 | lang_output_section_statement_type *os; | 
|---|
| 76 | asection **section; | 
|---|
| 77 | lang_statement_union_type **stmt; | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | #define HAVE_SECTION(hold, name) \ | 
|---|
| 81 | (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL) | 
|---|
| 82 |  | 
|---|
| 83 | /* Place an orphan section.  We use this to put random SEC_CODE or | 
|---|
| 84 | SEC_READONLY sections right after MMO_TEXT_SECTION_NAME.  Much borrowed | 
|---|
| 85 | from elf32.em.  */ | 
|---|
| 86 |  | 
|---|
| 87 | static bfd_boolean | 
|---|
| 88 | mmo_place_orphan (file, s) | 
|---|
| 89 | lang_input_statement_type *file; | 
|---|
| 90 | asection *s; | 
|---|
| 91 | { | 
|---|
| 92 | static struct orphan_save hold_text; | 
|---|
| 93 | struct orphan_save *place; | 
|---|
| 94 | lang_output_section_statement_type *os; | 
|---|
| 95 | lang_statement_list_type *old; | 
|---|
| 96 | lang_statement_list_type add; | 
|---|
| 97 | asection *snew, **pps, *bfd_section; | 
|---|
| 98 |  | 
|---|
| 99 | /* We have nothing to say for anything other than a final link.  */ | 
|---|
| 100 | if (link_info.relocateable | 
|---|
| 101 | || (bfd_get_section_flags (s->owner, s) | 
|---|
| 102 | & (SEC_EXCLUDE | SEC_LOAD)) != SEC_LOAD) | 
|---|
| 103 | return FALSE; | 
|---|
| 104 |  | 
|---|
| 105 | /* Only care for sections we're going to load.  */ | 
|---|
| 106 | os = lang_output_section_find (bfd_get_section_name (s->owner, s)); | 
|---|
| 107 |  | 
|---|
| 108 | /* We have an output section by this name.  Place the section inside it | 
|---|
| 109 | (regardless of whether the linker script lists it as input).  */ | 
|---|
| 110 | if (os != NULL) | 
|---|
| 111 | { | 
|---|
| 112 | lang_add_section (&os->children, s, os, file); | 
|---|
| 113 | return TRUE; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /* If this section does not have .text-type section flags or there's no | 
|---|
| 117 | MMO_TEXT_SECTION_NAME, we don't have anything to say.  */ | 
|---|
| 118 | if ((bfd_get_section_flags (s->owner, s) & (SEC_CODE | SEC_READONLY)) == 0) | 
|---|
| 119 | return FALSE; | 
|---|
| 120 |  | 
|---|
| 121 | if (hold_text.os == NULL) | 
|---|
| 122 | hold_text.os = lang_output_section_find (MMO_TEXT_SECTION_NAME); | 
|---|
| 123 |  | 
|---|
| 124 | place = &hold_text; | 
|---|
| 125 |  | 
|---|
| 126 | /* If there's an output section by this name, we'll use it, regardless | 
|---|
| 127 | of section flags, in contrast to what's done in elf32.em.  */ | 
|---|
| 128 |  | 
|---|
| 129 | /* Start building a list of statements for this section. | 
|---|
| 130 | First save the current statement pointer.  */ | 
|---|
| 131 | old = stat_ptr; | 
|---|
| 132 |  | 
|---|
| 133 | /* Add the output section statements for this orphan to our own private | 
|---|
| 134 | list, inserting them later into the global statement list.  */ | 
|---|
| 135 | stat_ptr = &add; | 
|---|
| 136 | lang_list_init (stat_ptr); | 
|---|
| 137 |  | 
|---|
| 138 | os = lang_enter_output_section_statement (bfd_get_section_name (s->owner, | 
|---|
| 139 | s), | 
|---|
| 140 | NULL, 0, | 
|---|
| 141 | (bfd_vma) 0, | 
|---|
| 142 | (etree_type *) NULL, | 
|---|
| 143 | (etree_type *) NULL, | 
|---|
| 144 | (etree_type *) NULL); | 
|---|
| 145 |  | 
|---|
| 146 | lang_add_section (&os->children, s, os, file); | 
|---|
| 147 |  | 
|---|
| 148 | lang_leave_output_section_statement | 
|---|
| 149 | ((bfd_vma) 0, "*default*", | 
|---|
| 150 | (struct lang_output_section_phdr_list *) NULL, NULL); | 
|---|
| 151 |  | 
|---|
| 152 | /* Restore the global list pointer.  */ | 
|---|
| 153 | stat_ptr = old; | 
|---|
| 154 |  | 
|---|
| 155 | snew = os->bfd_section; | 
|---|
| 156 | if (snew == NULL) | 
|---|
| 157 | /* /DISCARD/ section.  */ | 
|---|
| 158 | return TRUE; | 
|---|
| 159 |  | 
|---|
| 160 | /* We need an output section for .text as a root, so if there was none | 
|---|
| 161 | (might happen with a peculiar linker script such as in "map | 
|---|
| 162 | addresses", map-address.exp), we grab the output section created | 
|---|
| 163 | above.  */ | 
|---|
| 164 | if (hold_text.os == NULL) | 
|---|
| 165 | { | 
|---|
| 166 | if (os == NULL) | 
|---|
| 167 | return FALSE; | 
|---|
| 168 | hold_text.os = os; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | bfd_section = place->os->bfd_section; | 
|---|
| 172 | if (place->section == NULL && bfd_section == NULL) | 
|---|
| 173 | bfd_section = output_prev_sec_find (place->os); | 
|---|
| 174 |  | 
|---|
| 175 | if (place->section != NULL | 
|---|
| 176 | || (bfd_section != NULL | 
|---|
| 177 | && bfd_section != snew)) | 
|---|
| 178 | { | 
|---|
| 179 | /* Shuffle the section to make the output file look neater.  This is | 
|---|
| 180 | really only cosmetic.  */ | 
|---|
| 181 | if (place->section == NULL) | 
|---|
| 182 | /* Put orphans after the first section on the list.  */ | 
|---|
| 183 | place->section = &bfd_section->next; | 
|---|
| 184 |  | 
|---|
| 185 | /* Unlink the section.  */ | 
|---|
| 186 | for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next) | 
|---|
| 187 | ; | 
|---|
| 188 | bfd_section_list_remove (output_bfd, pps); | 
|---|
| 189 |  | 
|---|
| 190 | /* Now tack it on to the "place->os" section list.  */ | 
|---|
| 191 | bfd_section_list_insert (output_bfd, place->section, snew); | 
|---|
| 192 | } | 
|---|
| 193 | place->section = &snew->next; /* Save the end of this list.  */ | 
|---|
| 194 |  | 
|---|
| 195 | if (add.head != NULL) | 
|---|
| 196 | { | 
|---|
| 197 | /* We try to put the output statements in some sort of reasonable | 
|---|
| 198 | order here, because they determine the final load addresses of | 
|---|
| 199 | the orphan sections.  */ | 
|---|
| 200 | if (place->stmt == NULL) | 
|---|
| 201 | { | 
|---|
| 202 | /* Put the new statement list right at the head.  */ | 
|---|
| 203 | *add.tail = place->os->header.next; | 
|---|
| 204 | place->os->header.next = add.head; | 
|---|
| 205 | } | 
|---|
| 206 | else | 
|---|
| 207 | { | 
|---|
| 208 | /* Put it after the last orphan statement we added.  */ | 
|---|
| 209 | *add.tail = *place->stmt; | 
|---|
| 210 | *place->stmt = add.head; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | /* Fix the global list pointer if we happened to tack our new list | 
|---|
| 214 | at the tail.  */ | 
|---|
| 215 | if (*old->tail == add.head) | 
|---|
| 216 | old->tail = add.tail; | 
|---|
| 217 |  | 
|---|
| 218 | /* Save the end of this list.  */ | 
|---|
| 219 | place->stmt = add.tail; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | return TRUE; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | /* Remove the spurious settings of SEC_RELOC that make it to the output at | 
|---|
| 226 | link time.  We are as confused as elflink.h:elf_bfd_final_link, and | 
|---|
| 227 | paper over the bug similarly.  */ | 
|---|
| 228 |  | 
|---|
| 229 | static void | 
|---|
| 230 | mmo_wipe_sec_reloc_flag (abfd, sec, ptr) | 
|---|
| 231 | bfd *abfd; | 
|---|
| 232 | asection *sec; | 
|---|
| 233 | PTR ptr ATTRIBUTE_UNUSED; | 
|---|
| 234 | { | 
|---|
| 235 | bfd_set_section_flags (abfd, sec, | 
|---|
| 236 | bfd_get_section_flags (abfd, sec) & ~SEC_RELOC); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | /* Iterate with bfd_map_over_sections over mmo_wipe_sec_reloc_flag... */ | 
|---|
| 240 |  | 
|---|
| 241 | static void | 
|---|
| 242 | mmo_finish () | 
|---|
| 243 | { | 
|---|
| 244 | bfd_map_over_sections (output_bfd, mmo_wipe_sec_reloc_flag, NULL); | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 |  | 
|---|
| 248 | /* To get on-demand global register allocation right, we need to parse the | 
|---|
| 249 | relocs, like what happens when linking to ELF.  It needs to be done | 
|---|
| 250 | before all input sections are supposed to be present.  When linking to | 
|---|
| 251 | ELF, it's done when reading symbols.  When linking to mmo, we do it | 
|---|
| 252 | when all input files are seen, which is equivalent.  */ | 
|---|
| 253 |  | 
|---|
| 254 | static void | 
|---|
| 255 | mmo_after_open () | 
|---|
| 256 | { | 
|---|
| 257 | /* When there's a mismatch between the output format and the emulation | 
|---|
| 258 | (using weird combinations like "-m mmo --oformat elf64-mmix" for | 
|---|
| 259 | example), we'd count relocs twice because they'd also be counted | 
|---|
| 260 | along the usual route for ELF-only linking, which would lead to an | 
|---|
| 261 | internal accounting error.  */ | 
|---|
| 262 | if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour) | 
|---|
| 263 | { | 
|---|
| 264 | LANG_FOR_EACH_INPUT_STATEMENT (is) | 
|---|
| 265 | { | 
|---|
| 266 | if (bfd_get_flavour (is->the_bfd) == bfd_target_elf_flavour | 
|---|
| 267 | && !_bfd_mmix_check_all_relocs (is->the_bfd, &link_info)) | 
|---|
| 268 | einfo ("%X%P: Internal problems scanning %B after opening it", | 
|---|
| 269 | is->the_bfd); | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 | EOF | 
|---|
| 274 |  | 
|---|
| 275 | LDEMUL_PLACE_ORPHAN=mmo_place_orphan | 
|---|
| 276 | LDEMUL_FINISH=mmo_finish | 
|---|
| 277 | LDEMUL_AFTER_OPEN=mmo_after_open | 
|---|