| 1 | /* Linker command language support. |
| 2 | Copyright 1991-2013 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of the GNU Binutils. |
| 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 3 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., 51 Franklin Street - Fifth Floor, Boston, |
| 19 | MA 02110-1301, USA. */ |
| 20 | |
| 21 | #include "sysdep.h" |
| 22 | #include "bfd.h" |
| 23 | #include "libiberty.h" |
| 24 | #include "filenames.h" |
| 25 | #include "safe-ctype.h" |
| 26 | #include "obstack.h" |
| 27 | #include "bfdlink.h" |
| 28 | |
| 29 | #include "ld.h" |
| 30 | #include "ldmain.h" |
| 31 | #include "ldexp.h" |
| 32 | #include "ldlang.h" |
| 33 | #include <ldgram.h> |
| 34 | #include "ldlex.h" |
| 35 | #include "ldmisc.h" |
| 36 | #include "ldctor.h" |
| 37 | #include "ldfile.h" |
| 38 | #include "ldemul.h" |
| 39 | #include "fnmatch.h" |
| 40 | #include "demangle.h" |
| 41 | #include "hashtab.h" |
| 42 | #include "libbfd.h" |
| 43 | #ifdef ENABLE_PLUGINS |
| 44 | #include "plugin.h" |
| 45 | #endif /* ENABLE_PLUGINS */ |
| 46 | |
| 47 | #ifndef offsetof |
| 48 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
| 49 | #endif |
| 50 | |
| 51 | /* Locals variables. */ |
| 52 | static struct obstack stat_obstack; |
| 53 | static struct obstack map_obstack; |
| 54 | |
| 55 | #define obstack_chunk_alloc xmalloc |
| 56 | #define obstack_chunk_free free |
| 57 | static const char *entry_symbol_default = "start"; |
| 58 | static bfd_boolean placed_commons = FALSE; |
| 59 | static bfd_boolean stripped_excluded_sections = FALSE; |
| 60 | static lang_output_section_statement_type *default_common_section; |
| 61 | static bfd_boolean map_option_f; |
| 62 | static bfd_vma print_dot; |
| 63 | static lang_input_statement_type *first_file; |
| 64 | static const char *current_target; |
| 65 | static lang_statement_list_type statement_list; |
| 66 | static struct bfd_hash_table lang_definedness_table; |
| 67 | static lang_statement_list_type *stat_save[10]; |
| 68 | static lang_statement_list_type **stat_save_ptr = &stat_save[0]; |
| 69 | static struct unique_sections *unique_section_list; |
| 70 | |
| 71 | /* Forward declarations. */ |
| 72 | static void exp_init_os (etree_type *); |
| 73 | static lang_input_statement_type *lookup_name (const char *); |
| 74 | static struct bfd_hash_entry *lang_definedness_newfunc |
| 75 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); |
| 76 | static void insert_undefined (const char *); |
| 77 | static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *); |
| 78 | static void print_statement (lang_statement_union_type *, |
| 79 | lang_output_section_statement_type *); |
| 80 | static void print_statement_list (lang_statement_union_type *, |
| 81 | lang_output_section_statement_type *); |
| 82 | static void print_statements (void); |
| 83 | static void print_input_section (asection *, bfd_boolean); |
| 84 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); |
| 85 | static void lang_record_phdrs (void); |
| 86 | static void lang_do_version_exports_section (void); |
| 87 | static void lang_finalize_version_expr_head |
| 88 | (struct bfd_elf_version_expr_head *); |
| 89 | |
| 90 | /* Exported variables. */ |
| 91 | const char *output_target; |
| 92 | lang_output_section_statement_type *abs_output_section; |
| 93 | lang_statement_list_type lang_output_section_statement; |
| 94 | lang_statement_list_type *stat_ptr = &statement_list; |
| 95 | lang_statement_list_type file_chain = { NULL, NULL }; |
| 96 | lang_statement_list_type input_file_chain; |
| 97 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
| 98 | const char *entry_section = ".text"; |
| 99 | struct lang_input_statement_flags input_flags; |
| 100 | bfd_boolean entry_from_cmdline; |
| 101 | bfd_boolean undef_from_cmdline; |
| 102 | bfd_boolean lang_has_input_file = FALSE; |
| 103 | bfd_boolean had_output_filename = FALSE; |
| 104 | bfd_boolean lang_float_flag = FALSE; |
| 105 | bfd_boolean delete_output_file_on_failure = FALSE; |
| 106 | struct lang_phdr *lang_phdr_list; |
| 107 | struct lang_nocrossrefs *nocrossref_list; |
| 108 | |
| 109 | /* Functions that traverse the linker script and might evaluate |
| 110 | DEFINED() need to increment this. */ |
| 111 | int lang_statement_iteration = 0; |
| 112 | |
| 113 | etree_type *base; /* Relocation base - or null */ |
| 114 | |
| 115 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
| 116 | Although backslashes are treated specially if a pattern contains |
| 117 | wildcards, we do not consider the mere presence of a backslash to |
| 118 | be enough to cause the pattern to be treated as a wildcard. |
| 119 | That lets us handle DOS filenames more naturally. */ |
| 120 | #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) |
| 121 | |
| 122 | #define new_stat(x, y) \ |
| 123 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
| 124 | |
| 125 | #define outside_section_address(q) \ |
| 126 | ((q)->output_offset + (q)->output_section->vma) |
| 127 | |
| 128 | #define outside_symbol_address(q) \ |
| 129 | ((q)->value + outside_section_address (q->section)) |
| 130 | |
| 131 | #define SECTION_NAME_MAP_LENGTH (16) |
| 132 | |
| 133 | void * |
| 134 | stat_alloc (size_t size) |
| 135 | { |
| 136 | return obstack_alloc (&stat_obstack, size); |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | name_match (const char *pattern, const char *name) |
| 141 | { |
| 142 | if (wildcardp (pattern)) |
| 143 | return fnmatch (pattern, name, 0); |
| 144 | return strcmp (pattern, name); |
| 145 | } |
| 146 | |
| 147 | /* If PATTERN is of the form archive:file, return a pointer to the |
| 148 | separator. If not, return NULL. */ |
| 149 | |
| 150 | static char * |
| 151 | archive_path (const char *pattern) |
| 152 | { |
| 153 | char *p = NULL; |
| 154 | |
| 155 | if (link_info.path_separator == 0) |
| 156 | return p; |
| 157 | |
| 158 | p = strchr (pattern, link_info.path_separator); |
| 159 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
| 160 | if (p == NULL || link_info.path_separator != ':') |
| 161 | return p; |
| 162 | |
| 163 | /* Assume a match on the second char is part of drive specifier, |
| 164 | as in "c:\silly.dos". */ |
| 165 | if (p == pattern + 1 && ISALPHA (*pattern)) |
| 166 | p = strchr (p + 1, link_info.path_separator); |
| 167 | #endif |
| 168 | return p; |
| 169 | } |
| 170 | |
| 171 | /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, |
| 172 | return whether F matches FILE_SPEC. */ |
| 173 | |
| 174 | static bfd_boolean |
| 175 | input_statement_is_archive_path (const char *file_spec, char *sep, |
| 176 | lang_input_statement_type *f) |
| 177 | { |
| 178 | bfd_boolean match = FALSE; |
| 179 | |
| 180 | if ((*(sep + 1) == 0 |
| 181 | || name_match (sep + 1, f->filename) == 0) |
| 182 | && ((sep != file_spec) |
| 183 | == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) |
| 184 | { |
| 185 | match = TRUE; |
| 186 | |
| 187 | if (sep != file_spec) |
| 188 | { |
| 189 | const char *aname = f->the_bfd->my_archive->filename; |
| 190 | *sep = 0; |
| 191 | match = name_match (file_spec, aname) == 0; |
| 192 | *sep = link_info.path_separator; |
| 193 | } |
| 194 | } |
| 195 | return match; |
| 196 | } |
| 197 | |
| 198 | static bfd_boolean |
| 199 | unique_section_p (const asection *sec, |
| 200 | const lang_output_section_statement_type *os) |
| 201 | { |
| 202 | struct unique_sections *unam; |
| 203 | const char *secnam; |
| 204 | |
| 205 | if (link_info.relocatable |
| 206 | && sec->owner != NULL |
| 207 | && bfd_is_group_section (sec->owner, sec)) |
| 208 | return !(os != NULL |
| 209 | && strcmp (os->name, DISCARD_SECTION_NAME) == 0); |
| 210 | |
| 211 | secnam = sec->name; |
| 212 | for (unam = unique_section_list; unam; unam = unam->next) |
| 213 | if (name_match (unam->name, secnam) == 0) |
| 214 | return TRUE; |
| 215 | |
| 216 | return FALSE; |
| 217 | } |
| 218 | |
| 219 | /* Generic traversal routines for finding matching sections. */ |
| 220 | |
| 221 | /* Try processing a section against a wildcard. This just calls |
| 222 | the callback unless the filename exclusion list is present |
| 223 | and excludes the file. It's hardly ever present so this |
| 224 | function is very fast. */ |
| 225 | |
| 226 | static void |
| 227 | walk_wild_consider_section (lang_wild_statement_type *ptr, |
| 228 | lang_input_statement_type *file, |
| 229 | asection *s, |
| 230 | struct wildcard_list *sec, |
| 231 | callback_t callback, |
| 232 | void *data) |
| 233 | { |
| 234 | struct name_list *list_tmp; |
| 235 | |
| 236 | /* Don't process sections from files which were excluded. */ |
| 237 | for (list_tmp = sec->spec.exclude_name_list; |
| 238 | list_tmp; |
| 239 | list_tmp = list_tmp->next) |
| 240 | { |
| 241 | char *p = archive_path (list_tmp->name); |
| 242 | |
| 243 | if (p != NULL) |
| 244 | { |
| 245 | if (input_statement_is_archive_path (list_tmp->name, p, file)) |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | else if (name_match (list_tmp->name, file->filename) == 0) |
| 250 | return; |
| 251 | |
| 252 | /* FIXME: Perhaps remove the following at some stage? Matching |
| 253 | unadorned archives like this was never documented and has |
| 254 | been superceded by the archive:path syntax. */ |
| 255 | else if (file->the_bfd != NULL |
| 256 | && file->the_bfd->my_archive != NULL |
| 257 | && name_match (list_tmp->name, |
| 258 | file->the_bfd->my_archive->filename) == 0) |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
| 263 | } |
| 264 | |
| 265 | /* Lowest common denominator routine that can handle everything correctly, |
| 266 | but slowly. */ |
| 267 | |
| 268 | static void |
| 269 | walk_wild_section_general (lang_wild_statement_type *ptr, |
| 270 | lang_input_statement_type *file, |
| 271 | callback_t callback, |
| 272 | void *data) |
| 273 | { |
| 274 | asection *s; |
| 275 | struct wildcard_list *sec; |
| 276 | |
| 277 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 278 | { |
| 279 | sec = ptr->section_list; |
| 280 | if (sec == NULL) |
| 281 | (*callback) (ptr, sec, s, ptr->section_flag_list, file, data); |
| 282 | |
| 283 | while (sec != NULL) |
| 284 | { |
| 285 | bfd_boolean skip = FALSE; |
| 286 | |
| 287 | if (sec->spec.name != NULL) |
| 288 | { |
| 289 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 290 | |
| 291 | skip = name_match (sec->spec.name, sname) != 0; |
| 292 | } |
| 293 | |
| 294 | if (!skip) |
| 295 | walk_wild_consider_section (ptr, file, s, sec, callback, data); |
| 296 | |
| 297 | sec = sec->next; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Routines to find a single section given its name. If there's more |
| 303 | than one section with that name, we report that. */ |
| 304 | |
| 305 | typedef struct |
| 306 | { |
| 307 | asection *found_section; |
| 308 | bfd_boolean multiple_sections_found; |
| 309 | } section_iterator_callback_data; |
| 310 | |
| 311 | static bfd_boolean |
| 312 | section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data) |
| 313 | { |
| 314 | section_iterator_callback_data *d = (section_iterator_callback_data *) data; |
| 315 | |
| 316 | if (d->found_section != NULL) |
| 317 | { |
| 318 | d->multiple_sections_found = TRUE; |
| 319 | return TRUE; |
| 320 | } |
| 321 | |
| 322 | d->found_section = s; |
| 323 | return FALSE; |
| 324 | } |
| 325 | |
| 326 | static asection * |
| 327 | find_section (lang_input_statement_type *file, |
| 328 | struct wildcard_list *sec, |
| 329 | bfd_boolean *multiple_sections_found) |
| 330 | { |
| 331 | section_iterator_callback_data cb_data = { NULL, FALSE }; |
| 332 | |
| 333 | bfd_get_section_by_name_if (file->the_bfd, sec->spec.name, |
| 334 | section_iterator_callback, &cb_data); |
| 335 | *multiple_sections_found = cb_data.multiple_sections_found; |
| 336 | return cb_data.found_section; |
| 337 | } |
| 338 | |
| 339 | /* Code for handling simple wildcards without going through fnmatch, |
| 340 | which can be expensive because of charset translations etc. */ |
| 341 | |
| 342 | /* A simple wild is a literal string followed by a single '*', |
| 343 | where the literal part is at least 4 characters long. */ |
| 344 | |
| 345 | static bfd_boolean |
| 346 | is_simple_wild (const char *name) |
| 347 | { |
| 348 | size_t len = strcspn (name, "*?["); |
| 349 | return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; |
| 350 | } |
| 351 | |
| 352 | static bfd_boolean |
| 353 | match_simple_wild (const char *pattern, const char *name) |
| 354 | { |
| 355 | /* The first four characters of the pattern are guaranteed valid |
| 356 | non-wildcard characters. So we can go faster. */ |
| 357 | if (pattern[0] != name[0] || pattern[1] != name[1] |
| 358 | || pattern[2] != name[2] || pattern[3] != name[3]) |
| 359 | return FALSE; |
| 360 | |
| 361 | pattern += 4; |
| 362 | name += 4; |
| 363 | while (*pattern != '*') |
| 364 | if (*name++ != *pattern++) |
| 365 | return FALSE; |
| 366 | |
| 367 | return TRUE; |
| 368 | } |
| 369 | |
| 370 | /* Return the numerical value of the init_priority attribute from |
| 371 | section name NAME. */ |
| 372 | |
| 373 | static unsigned long |
| 374 | get_init_priority (const char *name) |
| 375 | { |
| 376 | char *end; |
| 377 | unsigned long init_priority; |
| 378 | |
| 379 | /* GCC uses the following section names for the init_priority |
| 380 | attribute with numerical values 101 and 65535 inclusive. A |
| 381 | lower value means a higher priority. |
| 382 | |
| 383 | 1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the |
| 384 | decimal numerical value of the init_priority attribute. |
| 385 | The order of execution in .init_array is forward and |
| 386 | .fini_array is backward. |
| 387 | 2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the |
| 388 | decimal numerical value of the init_priority attribute. |
| 389 | The order of execution in .ctors is backward and .dtors |
| 390 | is forward. |
| 391 | */ |
| 392 | if (strncmp (name, ".init_array.", 12) == 0 |
| 393 | || strncmp (name, ".fini_array.", 12) == 0) |
| 394 | { |
| 395 | init_priority = strtoul (name + 12, &end, 10); |
| 396 | return *end ? 0 : init_priority; |
| 397 | } |
| 398 | else if (strncmp (name, ".ctors.", 7) == 0 |
| 399 | || strncmp (name, ".dtors.", 7) == 0) |
| 400 | { |
| 401 | init_priority = strtoul (name + 7, &end, 10); |
| 402 | return *end ? 0 : 65535 - init_priority; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* Compare sections ASEC and BSEC according to SORT. */ |
| 409 | |
| 410 | static int |
| 411 | compare_section (sort_type sort, asection *asec, asection *bsec) |
| 412 | { |
| 413 | int ret; |
| 414 | unsigned long ainit_priority, binit_priority; |
| 415 | |
| 416 | switch (sort) |
| 417 | { |
| 418 | default: |
| 419 | abort (); |
| 420 | |
| 421 | case by_init_priority: |
| 422 | ainit_priority |
| 423 | = get_init_priority (bfd_get_section_name (asec->owner, asec)); |
| 424 | binit_priority |
| 425 | = get_init_priority (bfd_get_section_name (bsec->owner, bsec)); |
| 426 | if (ainit_priority == 0 || binit_priority == 0) |
| 427 | goto sort_by_name; |
| 428 | ret = ainit_priority - binit_priority; |
| 429 | if (ret) |
| 430 | break; |
| 431 | else |
| 432 | goto sort_by_name; |
| 433 | |
| 434 | case by_alignment_name: |
| 435 | ret = (bfd_section_alignment (bsec->owner, bsec) |
| 436 | - bfd_section_alignment (asec->owner, asec)); |
| 437 | if (ret) |
| 438 | break; |
| 439 | /* Fall through. */ |
| 440 | |
| 441 | case by_name: |
| 442 | sort_by_name: |
| 443 | ret = strcmp (bfd_get_section_name (asec->owner, asec), |
| 444 | bfd_get_section_name (bsec->owner, bsec)); |
| 445 | break; |
| 446 | |
| 447 | case by_name_alignment: |
| 448 | ret = strcmp (bfd_get_section_name (asec->owner, asec), |
| 449 | bfd_get_section_name (bsec->owner, bsec)); |
| 450 | if (ret) |
| 451 | break; |
| 452 | /* Fall through. */ |
| 453 | |
| 454 | case by_alignment: |
| 455 | ret = (bfd_section_alignment (bsec->owner, bsec) |
| 456 | - bfd_section_alignment (asec->owner, asec)); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | /* Build a Binary Search Tree to sort sections, unlike insertion sort |
| 464 | used in wild_sort(). BST is considerably faster if the number of |
| 465 | of sections are large. */ |
| 466 | |
| 467 | static lang_section_bst_type ** |
| 468 | wild_sort_fast (lang_wild_statement_type *wild, |
| 469 | struct wildcard_list *sec, |
| 470 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
| 471 | asection *section) |
| 472 | { |
| 473 | lang_section_bst_type **tree; |
| 474 | |
| 475 | tree = &wild->tree; |
| 476 | if (!wild->filenames_sorted |
| 477 | && (sec == NULL || sec->spec.sorted == none)) |
| 478 | { |
| 479 | /* Append at the right end of tree. */ |
| 480 | while (*tree) |
| 481 | tree = &((*tree)->right); |
| 482 | return tree; |
| 483 | } |
| 484 | |
| 485 | while (*tree) |
| 486 | { |
| 487 | /* Find the correct node to append this section. */ |
| 488 | if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0) |
| 489 | tree = &((*tree)->left); |
| 490 | else |
| 491 | tree = &((*tree)->right); |
| 492 | } |
| 493 | |
| 494 | return tree; |
| 495 | } |
| 496 | |
| 497 | /* Use wild_sort_fast to build a BST to sort sections. */ |
| 498 | |
| 499 | static void |
| 500 | output_section_callback_fast (lang_wild_statement_type *ptr, |
| 501 | struct wildcard_list *sec, |
| 502 | asection *section, |
| 503 | struct flag_info *sflag_list ATTRIBUTE_UNUSED, |
| 504 | lang_input_statement_type *file, |
| 505 | void *output) |
| 506 | { |
| 507 | lang_section_bst_type *node; |
| 508 | lang_section_bst_type **tree; |
| 509 | lang_output_section_statement_type *os; |
| 510 | |
| 511 | os = (lang_output_section_statement_type *) output; |
| 512 | |
| 513 | if (unique_section_p (section, os)) |
| 514 | return; |
| 515 | |
| 516 | node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type)); |
| 517 | node->left = 0; |
| 518 | node->right = 0; |
| 519 | node->section = section; |
| 520 | |
| 521 | tree = wild_sort_fast (ptr, sec, file, section); |
| 522 | if (tree != NULL) |
| 523 | *tree = node; |
| 524 | } |
| 525 | |
| 526 | /* Convert a sorted sections' BST back to list form. */ |
| 527 | |
| 528 | static void |
| 529 | output_section_callback_tree_to_list (lang_wild_statement_type *ptr, |
| 530 | lang_section_bst_type *tree, |
| 531 | void *output) |
| 532 | { |
| 533 | if (tree->left) |
| 534 | output_section_callback_tree_to_list (ptr, tree->left, output); |
| 535 | |
| 536 | lang_add_section (&ptr->children, tree->section, NULL, |
| 537 | (lang_output_section_statement_type *) output); |
| 538 | |
| 539 | if (tree->right) |
| 540 | output_section_callback_tree_to_list (ptr, tree->right, output); |
| 541 | |
| 542 | free (tree); |
| 543 | } |
| 544 | |
| 545 | /* Specialized, optimized routines for handling different kinds of |
| 546 | wildcards */ |
| 547 | |
| 548 | static void |
| 549 | walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr, |
| 550 | lang_input_statement_type *file, |
| 551 | callback_t callback, |
| 552 | void *data) |
| 553 | { |
| 554 | /* We can just do a hash lookup for the section with the right name. |
| 555 | But if that lookup discovers more than one section with the name |
| 556 | (should be rare), we fall back to the general algorithm because |
| 557 | we would otherwise have to sort the sections to make sure they |
| 558 | get processed in the bfd's order. */ |
| 559 | bfd_boolean multiple_sections_found; |
| 560 | struct wildcard_list *sec0 = ptr->handler_data[0]; |
| 561 | asection *s0 = find_section (file, sec0, &multiple_sections_found); |
| 562 | |
| 563 | if (multiple_sections_found) |
| 564 | walk_wild_section_general (ptr, file, callback, data); |
| 565 | else if (s0) |
| 566 | walk_wild_consider_section (ptr, file, s0, sec0, callback, data); |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr, |
| 571 | lang_input_statement_type *file, |
| 572 | callback_t callback, |
| 573 | void *data) |
| 574 | { |
| 575 | asection *s; |
| 576 | struct wildcard_list *wildsec0 = ptr->handler_data[0]; |
| 577 | |
| 578 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 579 | { |
| 580 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 581 | bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname); |
| 582 | |
| 583 | if (!skip) |
| 584 | walk_wild_consider_section (ptr, file, s, wildsec0, callback, data); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr, |
| 590 | lang_input_statement_type *file, |
| 591 | callback_t callback, |
| 592 | void *data) |
| 593 | { |
| 594 | asection *s; |
| 595 | struct wildcard_list *sec0 = ptr->handler_data[0]; |
| 596 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; |
| 597 | bfd_boolean multiple_sections_found; |
| 598 | asection *s0 = find_section (file, sec0, &multiple_sections_found); |
| 599 | |
| 600 | if (multiple_sections_found) |
| 601 | { |
| 602 | walk_wild_section_general (ptr, file, callback, data); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | /* Note that if the section was not found, s0 is NULL and |
| 607 | we'll simply never succeed the s == s0 test below. */ |
| 608 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 609 | { |
| 610 | /* Recall that in this code path, a section cannot satisfy more |
| 611 | than one spec, so if s == s0 then it cannot match |
| 612 | wildspec1. */ |
| 613 | if (s == s0) |
| 614 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); |
| 615 | else |
| 616 | { |
| 617 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 618 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
| 619 | |
| 620 | if (!skip) |
| 621 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, |
| 622 | data); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | static void |
| 628 | walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr, |
| 629 | lang_input_statement_type *file, |
| 630 | callback_t callback, |
| 631 | void *data) |
| 632 | { |
| 633 | asection *s; |
| 634 | struct wildcard_list *sec0 = ptr->handler_data[0]; |
| 635 | struct wildcard_list *wildsec1 = ptr->handler_data[1]; |
| 636 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; |
| 637 | bfd_boolean multiple_sections_found; |
| 638 | asection *s0 = find_section (file, sec0, &multiple_sections_found); |
| 639 | |
| 640 | if (multiple_sections_found) |
| 641 | { |
| 642 | walk_wild_section_general (ptr, file, callback, data); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 647 | { |
| 648 | if (s == s0) |
| 649 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); |
| 650 | else |
| 651 | { |
| 652 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 653 | bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname); |
| 654 | |
| 655 | if (!skip) |
| 656 | walk_wild_consider_section (ptr, file, s, wildsec1, callback, data); |
| 657 | else |
| 658 | { |
| 659 | skip = !match_simple_wild (wildsec2->spec.name, sname); |
| 660 | if (!skip) |
| 661 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, |
| 662 | data); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void |
| 669 | walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr, |
| 670 | lang_input_statement_type *file, |
| 671 | callback_t callback, |
| 672 | void *data) |
| 673 | { |
| 674 | asection *s; |
| 675 | struct wildcard_list *sec0 = ptr->handler_data[0]; |
| 676 | struct wildcard_list *sec1 = ptr->handler_data[1]; |
| 677 | struct wildcard_list *wildsec2 = ptr->handler_data[2]; |
| 678 | struct wildcard_list *wildsec3 = ptr->handler_data[3]; |
| 679 | bfd_boolean multiple_sections_found; |
| 680 | asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1; |
| 681 | |
| 682 | if (multiple_sections_found) |
| 683 | { |
| 684 | walk_wild_section_general (ptr, file, callback, data); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | s1 = find_section (file, sec1, &multiple_sections_found); |
| 689 | if (multiple_sections_found) |
| 690 | { |
| 691 | walk_wild_section_general (ptr, file, callback, data); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 696 | { |
| 697 | if (s == s0) |
| 698 | walk_wild_consider_section (ptr, file, s, sec0, callback, data); |
| 699 | else |
| 700 | if (s == s1) |
| 701 | walk_wild_consider_section (ptr, file, s, sec1, callback, data); |
| 702 | else |
| 703 | { |
| 704 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 705 | bfd_boolean skip = !match_simple_wild (wildsec2->spec.name, |
| 706 | sname); |
| 707 | |
| 708 | if (!skip) |
| 709 | walk_wild_consider_section (ptr, file, s, wildsec2, callback, |
| 710 | data); |
| 711 | else |
| 712 | { |
| 713 | skip = !match_simple_wild (wildsec3->spec.name, sname); |
| 714 | if (!skip) |
| 715 | walk_wild_consider_section (ptr, file, s, wildsec3, |
| 716 | callback, data); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | static void |
| 723 | walk_wild_section (lang_wild_statement_type *ptr, |
| 724 | lang_input_statement_type *file, |
| 725 | callback_t callback, |
| 726 | void *data) |
| 727 | { |
| 728 | if (file->flags.just_syms) |
| 729 | return; |
| 730 | |
| 731 | (*ptr->walk_wild_section_handler) (ptr, file, callback, data); |
| 732 | } |
| 733 | |
| 734 | /* Returns TRUE when name1 is a wildcard spec that might match |
| 735 | something name2 can match. We're conservative: we return FALSE |
| 736 | only if the prefixes of name1 and name2 are different up to the |
| 737 | first wildcard character. */ |
| 738 | |
| 739 | static bfd_boolean |
| 740 | wild_spec_can_overlap (const char *name1, const char *name2) |
| 741 | { |
| 742 | size_t prefix1_len = strcspn (name1, "?*["); |
| 743 | size_t prefix2_len = strcspn (name2, "?*["); |
| 744 | size_t min_prefix_len; |
| 745 | |
| 746 | /* Note that if there is no wildcard character, then we treat the |
| 747 | terminating 0 as part of the prefix. Thus ".text" won't match |
| 748 | ".text." or ".text.*", for example. */ |
| 749 | if (name1[prefix1_len] == '\0') |
| 750 | prefix1_len++; |
| 751 | if (name2[prefix2_len] == '\0') |
| 752 | prefix2_len++; |
| 753 | |
| 754 | min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len; |
| 755 | |
| 756 | return memcmp (name1, name2, min_prefix_len) == 0; |
| 757 | } |
| 758 | |
| 759 | /* Select specialized code to handle various kinds of wildcard |
| 760 | statements. */ |
| 761 | |
| 762 | static void |
| 763 | analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) |
| 764 | { |
| 765 | int sec_count = 0; |
| 766 | int wild_name_count = 0; |
| 767 | struct wildcard_list *sec; |
| 768 | int signature; |
| 769 | int data_counter; |
| 770 | |
| 771 | ptr->walk_wild_section_handler = walk_wild_section_general; |
| 772 | ptr->handler_data[0] = NULL; |
| 773 | ptr->handler_data[1] = NULL; |
| 774 | ptr->handler_data[2] = NULL; |
| 775 | ptr->handler_data[3] = NULL; |
| 776 | ptr->tree = NULL; |
| 777 | |
| 778 | /* Count how many wildcard_specs there are, and how many of those |
| 779 | actually use wildcards in the name. Also, bail out if any of the |
| 780 | wildcard names are NULL. (Can this actually happen? |
| 781 | walk_wild_section used to test for it.) And bail out if any |
| 782 | of the wildcards are more complex than a simple string |
| 783 | ending in a single '*'. */ |
| 784 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
| 785 | { |
| 786 | ++sec_count; |
| 787 | if (sec->spec.name == NULL) |
| 788 | return; |
| 789 | if (wildcardp (sec->spec.name)) |
| 790 | { |
| 791 | ++wild_name_count; |
| 792 | if (!is_simple_wild (sec->spec.name)) |
| 793 | return; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /* The zero-spec case would be easy to optimize but it doesn't |
| 798 | happen in practice. Likewise, more than 4 specs doesn't |
| 799 | happen in practice. */ |
| 800 | if (sec_count == 0 || sec_count > 4) |
| 801 | return; |
| 802 | |
| 803 | /* Check that no two specs can match the same section. */ |
| 804 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
| 805 | { |
| 806 | struct wildcard_list *sec2; |
| 807 | for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next) |
| 808 | { |
| 809 | if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name)) |
| 810 | return; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | signature = (sec_count << 8) + wild_name_count; |
| 815 | switch (signature) |
| 816 | { |
| 817 | case 0x0100: |
| 818 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0; |
| 819 | break; |
| 820 | case 0x0101: |
| 821 | ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1; |
| 822 | break; |
| 823 | case 0x0201: |
| 824 | ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1; |
| 825 | break; |
| 826 | case 0x0302: |
| 827 | ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2; |
| 828 | break; |
| 829 | case 0x0402: |
| 830 | ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2; |
| 831 | break; |
| 832 | default: |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | /* Now fill the data array with pointers to the specs, first the |
| 837 | specs with non-wildcard names, then the specs with wildcard |
| 838 | names. It's OK to process the specs in different order from the |
| 839 | given order, because we've already determined that no section |
| 840 | will match more than one spec. */ |
| 841 | data_counter = 0; |
| 842 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
| 843 | if (!wildcardp (sec->spec.name)) |
| 844 | ptr->handler_data[data_counter++] = sec; |
| 845 | for (sec = ptr->section_list; sec != NULL; sec = sec->next) |
| 846 | if (wildcardp (sec->spec.name)) |
| 847 | ptr->handler_data[data_counter++] = sec; |
| 848 | } |
| 849 | |
| 850 | /* Handle a wild statement for a single file F. */ |
| 851 | |
| 852 | static void |
| 853 | walk_wild_file (lang_wild_statement_type *s, |
| 854 | lang_input_statement_type *f, |
| 855 | callback_t callback, |
| 856 | void *data) |
| 857 | { |
| 858 | if (f->the_bfd == NULL |
| 859 | || ! bfd_check_format (f->the_bfd, bfd_archive)) |
| 860 | walk_wild_section (s, f, callback, data); |
| 861 | else |
| 862 | { |
| 863 | bfd *member; |
| 864 | |
| 865 | /* This is an archive file. We must map each member of the |
| 866 | archive separately. */ |
| 867 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
| 868 | while (member != NULL) |
| 869 | { |
| 870 | /* When lookup_name is called, it will call the add_symbols |
| 871 | entry point for the archive. For each element of the |
| 872 | archive which is included, BFD will call ldlang_add_file, |
| 873 | which will set the usrdata field of the member to the |
| 874 | lang_input_statement. */ |
| 875 | if (member->usrdata != NULL) |
| 876 | { |
| 877 | walk_wild_section (s, |
| 878 | (lang_input_statement_type *) member->usrdata, |
| 879 | callback, data); |
| 880 | } |
| 881 | |
| 882 | member = bfd_openr_next_archived_file (f->the_bfd, member); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | static void |
| 888 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
| 889 | { |
| 890 | const char *file_spec = s->filename; |
| 891 | char *p; |
| 892 | |
| 893 | if (file_spec == NULL) |
| 894 | { |
| 895 | /* Perform the iteration over all files in the list. */ |
| 896 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 897 | { |
| 898 | walk_wild_file (s, f, callback, data); |
| 899 | } |
| 900 | } |
| 901 | else if ((p = archive_path (file_spec)) != NULL) |
| 902 | { |
| 903 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 904 | { |
| 905 | if (input_statement_is_archive_path (file_spec, p, f)) |
| 906 | walk_wild_file (s, f, callback, data); |
| 907 | } |
| 908 | } |
| 909 | else if (wildcardp (file_spec)) |
| 910 | { |
| 911 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 912 | { |
| 913 | if (fnmatch (file_spec, f->filename, 0) == 0) |
| 914 | walk_wild_file (s, f, callback, data); |
| 915 | } |
| 916 | } |
| 917 | else |
| 918 | { |
| 919 | lang_input_statement_type *f; |
| 920 | |
| 921 | /* Perform the iteration over a single file. */ |
| 922 | f = lookup_name (file_spec); |
| 923 | if (f) |
| 924 | walk_wild_file (s, f, callback, data); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* lang_for_each_statement walks the parse tree and calls the provided |
| 929 | function for each node, except those inside output section statements |
| 930 | with constraint set to -1. */ |
| 931 | |
| 932 | void |
| 933 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
| 934 | lang_statement_union_type *s) |
| 935 | { |
| 936 | for (; s != NULL; s = s->header.next) |
| 937 | { |
| 938 | func (s); |
| 939 | |
| 940 | switch (s->header.type) |
| 941 | { |
| 942 | case lang_constructors_statement_enum: |
| 943 | lang_for_each_statement_worker (func, constructor_list.head); |
| 944 | break; |
| 945 | case lang_output_section_statement_enum: |
| 946 | if (s->output_section_statement.constraint != -1) |
| 947 | lang_for_each_statement_worker |
| 948 | (func, s->output_section_statement.children.head); |
| 949 | break; |
| 950 | case lang_wild_statement_enum: |
| 951 | lang_for_each_statement_worker (func, |
| 952 | s->wild_statement.children.head); |
| 953 | break; |
| 954 | case lang_group_statement_enum: |
| 955 | lang_for_each_statement_worker (func, |
| 956 | s->group_statement.children.head); |
| 957 | break; |
| 958 | case lang_data_statement_enum: |
| 959 | case lang_reloc_statement_enum: |
| 960 | case lang_object_symbols_statement_enum: |
| 961 | case lang_output_statement_enum: |
| 962 | case lang_target_statement_enum: |
| 963 | case lang_input_section_enum: |
| 964 | case lang_input_statement_enum: |
| 965 | case lang_assignment_statement_enum: |
| 966 | case lang_padding_statement_enum: |
| 967 | case lang_address_statement_enum: |
| 968 | case lang_fill_statement_enum: |
| 969 | case lang_insert_statement_enum: |
| 970 | break; |
| 971 | default: |
| 972 | FAIL (); |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | void |
| 979 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
| 980 | { |
| 981 | lang_for_each_statement_worker (func, statement_list.head); |
| 982 | } |
| 983 | |
| 984 | /*----------------------------------------------------------------------*/ |
| 985 | |
| 986 | void |
| 987 | lang_list_init (lang_statement_list_type *list) |
| 988 | { |
| 989 | list->head = NULL; |
| 990 | list->tail = &list->head; |
| 991 | } |
| 992 | |
| 993 | void |
| 994 | push_stat_ptr (lang_statement_list_type *new_ptr) |
| 995 | { |
| 996 | if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) |
| 997 | abort (); |
| 998 | *stat_save_ptr++ = stat_ptr; |
| 999 | stat_ptr = new_ptr; |
| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | pop_stat_ptr (void) |
| 1004 | { |
| 1005 | if (stat_save_ptr <= stat_save) |
| 1006 | abort (); |
| 1007 | stat_ptr = *--stat_save_ptr; |
| 1008 | } |
| 1009 | |
| 1010 | /* Build a new statement node for the parse tree. */ |
| 1011 | |
| 1012 | static lang_statement_union_type * |
| 1013 | new_statement (enum statement_enum type, |
| 1014 | size_t size, |
| 1015 | lang_statement_list_type *list) |
| 1016 | { |
| 1017 | lang_statement_union_type *new_stmt; |
| 1018 | |
| 1019 | new_stmt = (lang_statement_union_type *) stat_alloc (size); |
| 1020 | new_stmt->header.type = type; |
| 1021 | new_stmt->header.next = NULL; |
| 1022 | lang_statement_append (list, new_stmt, &new_stmt->header.next); |
| 1023 | return new_stmt; |
| 1024 | } |
| 1025 | |
| 1026 | /* Build a new input file node for the language. There are several |
| 1027 | ways in which we treat an input file, eg, we only look at symbols, |
| 1028 | or prefix it with a -l etc. |
| 1029 | |
| 1030 | We can be supplied with requests for input files more than once; |
| 1031 | they may, for example be split over several lines like foo.o(.text) |
| 1032 | foo.o(.data) etc, so when asked for a file we check that we haven't |
| 1033 | got it already so we don't duplicate the bfd. */ |
| 1034 | |
| 1035 | static lang_input_statement_type * |
| 1036 | new_afile (const char *name, |
| 1037 | lang_input_file_enum_type file_type, |
| 1038 | const char *target, |
| 1039 | bfd_boolean add_to_list) |
| 1040 | { |
| 1041 | lang_input_statement_type *p; |
| 1042 | |
| 1043 | lang_has_input_file = TRUE; |
| 1044 | |
| 1045 | if (add_to_list) |
| 1046 | p = (lang_input_statement_type *) new_stat (lang_input_statement, stat_ptr); |
| 1047 | else |
| 1048 | { |
| 1049 | p = (lang_input_statement_type *) |
| 1050 | stat_alloc (sizeof (lang_input_statement_type)); |
| 1051 | p->header.type = lang_input_statement_enum; |
| 1052 | p->header.next = NULL; |
| 1053 | } |
| 1054 | |
| 1055 | memset (&p->the_bfd, 0, |
| 1056 | sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); |
| 1057 | p->target = target; |
| 1058 | p->flags.dynamic = input_flags.dynamic; |
| 1059 | p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; |
| 1060 | p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; |
| 1061 | p->flags.whole_archive = input_flags.whole_archive; |
| 1062 | p->flags.sysrooted = input_flags.sysrooted; |
| 1063 | |
| 1064 | if (file_type == lang_input_file_is_l_enum |
| 1065 | && name[0] == ':' && name[1] != '\0') |
| 1066 | { |
| 1067 | file_type = lang_input_file_is_search_file_enum; |
| 1068 | name = name + 1; |
| 1069 | } |
| 1070 | |
| 1071 | switch (file_type) |
| 1072 | { |
| 1073 | case lang_input_file_is_symbols_only_enum: |
| 1074 | p->filename = name; |
| 1075 | p->local_sym_name = name; |
| 1076 | p->flags.real = TRUE; |
| 1077 | p->flags.just_syms = TRUE; |
| 1078 | break; |
| 1079 | case lang_input_file_is_fake_enum: |
| 1080 | p->filename = name; |
| 1081 | p->local_sym_name = name; |
| 1082 | break; |
| 1083 | case lang_input_file_is_l_enum: |
| 1084 | p->filename = name; |
| 1085 | p->local_sym_name = concat ("-l", name, (const char *) NULL); |
| 1086 | p->flags.maybe_archive = TRUE; |
| 1087 | p->flags.real = TRUE; |
| 1088 | p->flags.search_dirs = TRUE; |
| 1089 | break; |
| 1090 | case lang_input_file_is_marker_enum: |
| 1091 | p->filename = name; |
| 1092 | p->local_sym_name = name; |
| 1093 | p->flags.search_dirs = TRUE; |
| 1094 | break; |
| 1095 | case lang_input_file_is_search_file_enum: |
| 1096 | p->filename = name; |
| 1097 | p->local_sym_name = name; |
| 1098 | p->flags.real = TRUE; |
| 1099 | p->flags.search_dirs = TRUE; |
| 1100 | break; |
| 1101 | case lang_input_file_is_file_enum: |
| 1102 | p->filename = name; |
| 1103 | p->local_sym_name = name; |
| 1104 | p->flags.real = TRUE; |
| 1105 | break; |
| 1106 | default: |
| 1107 | FAIL (); |
| 1108 | } |
| 1109 | |
| 1110 | lang_statement_append (&input_file_chain, |
| 1111 | (lang_statement_union_type *) p, |
| 1112 | &p->next_real_file); |
| 1113 | return p; |
| 1114 | } |
| 1115 | |
| 1116 | lang_input_statement_type * |
| 1117 | lang_add_input_file (const char *name, |
| 1118 | lang_input_file_enum_type file_type, |
| 1119 | const char *target) |
| 1120 | { |
| 1121 | return new_afile (name, file_type, target, TRUE); |
| 1122 | } |
| 1123 | |
| 1124 | struct out_section_hash_entry |
| 1125 | { |
| 1126 | struct bfd_hash_entry root; |
| 1127 | lang_statement_union_type s; |
| 1128 | }; |
| 1129 | |
| 1130 | /* The hash table. */ |
| 1131 | |
| 1132 | static struct bfd_hash_table output_section_statement_table; |
| 1133 | |
| 1134 | /* Support routines for the hash table used by lang_output_section_find, |
| 1135 | initialize the table, fill in an entry and remove the table. */ |
| 1136 | |
| 1137 | static struct bfd_hash_entry * |
| 1138 | output_section_statement_newfunc (struct bfd_hash_entry *entry, |
| 1139 | struct bfd_hash_table *table, |
| 1140 | const char *string) |
| 1141 | { |
| 1142 | lang_output_section_statement_type **nextp; |
| 1143 | struct out_section_hash_entry *ret; |
| 1144 | |
| 1145 | if (entry == NULL) |
| 1146 | { |
| 1147 | entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, |
| 1148 | sizeof (*ret)); |
| 1149 | if (entry == NULL) |
| 1150 | return entry; |
| 1151 | } |
| 1152 | |
| 1153 | entry = bfd_hash_newfunc (entry, table, string); |
| 1154 | if (entry == NULL) |
| 1155 | return entry; |
| 1156 | |
| 1157 | ret = (struct out_section_hash_entry *) entry; |
| 1158 | memset (&ret->s, 0, sizeof (ret->s)); |
| 1159 | ret->s.header.type = lang_output_section_statement_enum; |
| 1160 | ret->s.output_section_statement.subsection_alignment = -1; |
| 1161 | ret->s.output_section_statement.section_alignment = -1; |
| 1162 | ret->s.output_section_statement.block_value = 1; |
| 1163 | lang_list_init (&ret->s.output_section_statement.children); |
| 1164 | lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); |
| 1165 | |
| 1166 | /* For every output section statement added to the list, except the |
| 1167 | first one, lang_output_section_statement.tail points to the "next" |
| 1168 | field of the last element of the list. */ |
| 1169 | if (lang_output_section_statement.head != NULL) |
| 1170 | ret->s.output_section_statement.prev |
| 1171 | = ((lang_output_section_statement_type *) |
| 1172 | ((char *) lang_output_section_statement.tail |
| 1173 | - offsetof (lang_output_section_statement_type, next))); |
| 1174 | |
| 1175 | /* GCC's strict aliasing rules prevent us from just casting the |
| 1176 | address, so we store the pointer in a variable and cast that |
| 1177 | instead. */ |
| 1178 | nextp = &ret->s.output_section_statement.next; |
| 1179 | lang_statement_append (&lang_output_section_statement, |
| 1180 | &ret->s, |
| 1181 | (lang_statement_union_type **) nextp); |
| 1182 | return &ret->root; |
| 1183 | } |
| 1184 | |
| 1185 | static void |
| 1186 | output_section_statement_table_init (void) |
| 1187 | { |
| 1188 | if (!bfd_hash_table_init_n (&output_section_statement_table, |
| 1189 | output_section_statement_newfunc, |
| 1190 | sizeof (struct out_section_hash_entry), |
| 1191 | 61)) |
| 1192 | einfo (_("%P%F: can not create hash table: %E\n")); |
| 1193 | } |
| 1194 | |
| 1195 | static void |
| 1196 | output_section_statement_table_free (void) |
| 1197 | { |
| 1198 | bfd_hash_table_free (&output_section_statement_table); |
| 1199 | } |
| 1200 | |
| 1201 | /* Build enough state so that the parser can build its tree. */ |
| 1202 | |
| 1203 | void |
| 1204 | lang_init (void) |
| 1205 | { |
| 1206 | obstack_begin (&stat_obstack, 1000); |
| 1207 | |
| 1208 | stat_ptr = &statement_list; |
| 1209 | |
| 1210 | output_section_statement_table_init (); |
| 1211 | |
| 1212 | lang_list_init (stat_ptr); |
| 1213 | |
| 1214 | lang_list_init (&input_file_chain); |
| 1215 | lang_list_init (&lang_output_section_statement); |
| 1216 | lang_list_init (&file_chain); |
| 1217 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
| 1218 | NULL); |
| 1219 | abs_output_section = |
| 1220 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE); |
| 1221 | |
| 1222 | abs_output_section->bfd_section = bfd_abs_section_ptr; |
| 1223 | |
| 1224 | /* The value "3" is ad-hoc, somewhat related to the expected number of |
| 1225 | DEFINED expressions in a linker script. For most default linker |
| 1226 | scripts, there are none. Why a hash table then? Well, it's somewhat |
| 1227 | simpler to re-use working machinery than using a linked list in terms |
| 1228 | of code-complexity here in ld, besides the initialization which just |
| 1229 | looks like other code here. */ |
| 1230 | if (!bfd_hash_table_init_n (&lang_definedness_table, |
| 1231 | lang_definedness_newfunc, |
| 1232 | sizeof (struct lang_definedness_hash_entry), |
| 1233 | 3)) |
| 1234 | einfo (_("%P%F: can not create hash table: %E\n")); |
| 1235 | } |
| 1236 | |
| 1237 | void |
| 1238 | lang_finish (void) |
| 1239 | { |
| 1240 | bfd_link_hash_table_free (link_info.output_bfd, link_info.hash); |
| 1241 | bfd_hash_table_free (&lang_definedness_table); |
| 1242 | output_section_statement_table_free (); |
| 1243 | } |
| 1244 | |
| 1245 | /*---------------------------------------------------------------------- |
| 1246 | A region is an area of memory declared with the |
| 1247 | MEMORY { name:org=exp, len=exp ... } |
| 1248 | syntax. |
| 1249 | |
| 1250 | We maintain a list of all the regions here. |
| 1251 | |
| 1252 | If no regions are specified in the script, then the default is used |
| 1253 | which is created when looked up to be the entire data space. |
| 1254 | |
| 1255 | If create is true we are creating a region inside a MEMORY block. |
| 1256 | In this case it is probably an error to create a region that has |
| 1257 | already been created. If we are not inside a MEMORY block it is |
| 1258 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) |
| 1259 | and so we issue a warning. |
| 1260 | |
| 1261 | Each region has at least one name. The first name is either |
| 1262 | DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add |
| 1263 | alias names to an existing region within a script with |
| 1264 | REGION_ALIAS (alias, region_name). Each name corresponds to at most one |
| 1265 | region. */ |
| 1266 | |
| 1267 | static lang_memory_region_type *lang_memory_region_list; |
| 1268 | static lang_memory_region_type **lang_memory_region_list_tail |
| 1269 | = &lang_memory_region_list; |
| 1270 | |
| 1271 | lang_memory_region_type * |
| 1272 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
| 1273 | { |
| 1274 | lang_memory_region_name *n; |
| 1275 | lang_memory_region_type *r; |
| 1276 | lang_memory_region_type *new_region; |
| 1277 | |
| 1278 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
| 1279 | if (name == NULL) |
| 1280 | return NULL; |
| 1281 | |
| 1282 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
| 1283 | for (n = &r->name_list; n != NULL; n = n->next) |
| 1284 | if (strcmp (n->name, name) == 0) |
| 1285 | { |
| 1286 | if (create) |
| 1287 | einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"), |
| 1288 | NULL, name); |
| 1289 | return r; |
| 1290 | } |
| 1291 | |
| 1292 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
| 1293 | einfo (_("%P:%S: warning: memory region `%s' not declared\n"), |
| 1294 | NULL, name); |
| 1295 | |
| 1296 | new_region = (lang_memory_region_type *) |
| 1297 | stat_alloc (sizeof (lang_memory_region_type)); |
| 1298 | |
| 1299 | new_region->name_list.name = xstrdup (name); |
| 1300 | new_region->name_list.next = NULL; |
| 1301 | new_region->next = NULL; |
| 1302 | new_region->origin = 0; |
| 1303 | new_region->length = ~(bfd_size_type) 0; |
| 1304 | new_region->current = 0; |
| 1305 | new_region->last_os = NULL; |
| 1306 | new_region->flags = 0; |
| 1307 | new_region->not_flags = 0; |
| 1308 | new_region->had_full_message = FALSE; |
| 1309 | |
| 1310 | *lang_memory_region_list_tail = new_region; |
| 1311 | lang_memory_region_list_tail = &new_region->next; |
| 1312 | |
| 1313 | return new_region; |
| 1314 | } |
| 1315 | |
| 1316 | void |
| 1317 | lang_memory_region_alias (const char * alias, const char * region_name) |
| 1318 | { |
| 1319 | lang_memory_region_name * n; |
| 1320 | lang_memory_region_type * r; |
| 1321 | lang_memory_region_type * region; |
| 1322 | |
| 1323 | /* The default region must be unique. This ensures that it is not necessary |
| 1324 | to iterate through the name list if someone wants the check if a region is |
| 1325 | the default memory region. */ |
| 1326 | if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 |
| 1327 | || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) |
| 1328 | einfo (_("%F%P:%S: error: alias for default memory region\n"), NULL); |
| 1329 | |
| 1330 | /* Look for the target region and check if the alias is not already |
| 1331 | in use. */ |
| 1332 | region = NULL; |
| 1333 | for (r = lang_memory_region_list; r != NULL; r = r->next) |
| 1334 | for (n = &r->name_list; n != NULL; n = n->next) |
| 1335 | { |
| 1336 | if (region == NULL && strcmp (n->name, region_name) == 0) |
| 1337 | region = r; |
| 1338 | if (strcmp (n->name, alias) == 0) |
| 1339 | einfo (_("%F%P:%S: error: redefinition of memory region " |
| 1340 | "alias `%s'\n"), |
| 1341 | NULL, alias); |
| 1342 | } |
| 1343 | |
| 1344 | /* Check if the target region exists. */ |
| 1345 | if (region == NULL) |
| 1346 | einfo (_("%F%P:%S: error: memory region `%s' " |
| 1347 | "for alias `%s' does not exist\n"), |
| 1348 | NULL, region_name, alias); |
| 1349 | |
| 1350 | /* Add alias to region name list. */ |
| 1351 | n = (lang_memory_region_name *) stat_alloc (sizeof (lang_memory_region_name)); |
| 1352 | n->name = xstrdup (alias); |
| 1353 | n->next = region->name_list.next; |
| 1354 | region->name_list.next = n; |
| 1355 | } |
| 1356 | |
| 1357 | static lang_memory_region_type * |
| 1358 | lang_memory_default (asection * section) |
| 1359 | { |
| 1360 | lang_memory_region_type *p; |
| 1361 | |
| 1362 | flagword sec_flags = section->flags; |
| 1363 | |
| 1364 | /* Override SEC_DATA to mean a writable section. */ |
| 1365 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) |
| 1366 | sec_flags |= SEC_DATA; |
| 1367 | |
| 1368 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
| 1369 | { |
| 1370 | if ((p->flags & sec_flags) != 0 |
| 1371 | && (p->not_flags & sec_flags) == 0) |
| 1372 | { |
| 1373 | return p; |
| 1374 | } |
| 1375 | } |
| 1376 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
| 1377 | } |
| 1378 | |
| 1379 | /* Find or create an output_section_statement with the given NAME. |
| 1380 | If CONSTRAINT is non-zero match one with that constraint, otherwise |
| 1381 | match any non-negative constraint. If CREATE, always make a |
| 1382 | new output_section_statement for SPECIAL CONSTRAINT. */ |
| 1383 | |
| 1384 | lang_output_section_statement_type * |
| 1385 | lang_output_section_statement_lookup (const char *name, |
| 1386 | int constraint, |
| 1387 | bfd_boolean create) |
| 1388 | { |
| 1389 | struct out_section_hash_entry *entry; |
| 1390 | |
| 1391 | entry = ((struct out_section_hash_entry *) |
| 1392 | bfd_hash_lookup (&output_section_statement_table, name, |
| 1393 | create, FALSE)); |
| 1394 | if (entry == NULL) |
| 1395 | { |
| 1396 | if (create) |
| 1397 | einfo (_("%P%F: failed creating section `%s': %E\n"), name); |
| 1398 | return NULL; |
| 1399 | } |
| 1400 | |
| 1401 | if (entry->s.output_section_statement.name != NULL) |
| 1402 | { |
| 1403 | /* We have a section of this name, but it might not have the correct |
| 1404 | constraint. */ |
| 1405 | struct out_section_hash_entry *last_ent; |
| 1406 | |
| 1407 | name = entry->s.output_section_statement.name; |
| 1408 | if (create && constraint == SPECIAL) |
| 1409 | /* Not traversing to the end reverses the order of the second |
| 1410 | and subsequent SPECIAL sections in the hash table chain, |
| 1411 | but that shouldn't matter. */ |
| 1412 | last_ent = entry; |
| 1413 | else |
| 1414 | do |
| 1415 | { |
| 1416 | if (constraint == entry->s.output_section_statement.constraint |
| 1417 | || (constraint == 0 |
| 1418 | && entry->s.output_section_statement.constraint >= 0)) |
| 1419 | return &entry->s.output_section_statement; |
| 1420 | last_ent = entry; |
| 1421 | entry = (struct out_section_hash_entry *) entry->root.next; |
| 1422 | } |
| 1423 | while (entry != NULL |
| 1424 | && name == entry->s.output_section_statement.name); |
| 1425 | |
| 1426 | if (!create) |
| 1427 | return NULL; |
| 1428 | |
| 1429 | entry |
| 1430 | = ((struct out_section_hash_entry *) |
| 1431 | output_section_statement_newfunc (NULL, |
| 1432 | &output_section_statement_table, |
| 1433 | name)); |
| 1434 | if (entry == NULL) |
| 1435 | { |
| 1436 | einfo (_("%P%F: failed creating section `%s': %E\n"), name); |
| 1437 | return NULL; |
| 1438 | } |
| 1439 | entry->root = last_ent->root; |
| 1440 | last_ent->root.next = &entry->root; |
| 1441 | } |
| 1442 | |
| 1443 | entry->s.output_section_statement.name = name; |
| 1444 | entry->s.output_section_statement.constraint = constraint; |
| 1445 | return &entry->s.output_section_statement; |
| 1446 | } |
| 1447 | |
| 1448 | /* Find the next output_section_statement with the same name as OS. |
| 1449 | If CONSTRAINT is non-zero, find one with that constraint otherwise |
| 1450 | match any non-negative constraint. */ |
| 1451 | |
| 1452 | lang_output_section_statement_type * |
| 1453 | next_matching_output_section_statement (lang_output_section_statement_type *os, |
| 1454 | int constraint) |
| 1455 | { |
| 1456 | /* All output_section_statements are actually part of a |
| 1457 | struct out_section_hash_entry. */ |
| 1458 | struct out_section_hash_entry *entry = (struct out_section_hash_entry *) |
| 1459 | ((char *) os |
| 1460 | - offsetof (struct out_section_hash_entry, s.output_section_statement)); |
| 1461 | const char *name = os->name; |
| 1462 | |
| 1463 | ASSERT (name == entry->root.string); |
| 1464 | do |
| 1465 | { |
| 1466 | entry = (struct out_section_hash_entry *) entry->root.next; |
| 1467 | if (entry == NULL |
| 1468 | || name != entry->s.output_section_statement.name) |
| 1469 | return NULL; |
| 1470 | } |
| 1471 | while (constraint != entry->s.output_section_statement.constraint |
| 1472 | && (constraint != 0 |
| 1473 | || entry->s.output_section_statement.constraint < 0)); |
| 1474 | |
| 1475 | return &entry->s.output_section_statement; |
| 1476 | } |
| 1477 | |
| 1478 | /* A variant of lang_output_section_find used by place_orphan. |
| 1479 | Returns the output statement that should precede a new output |
| 1480 | statement for SEC. If an exact match is found on certain flags, |
| 1481 | sets *EXACT too. */ |
| 1482 | |
| 1483 | lang_output_section_statement_type * |
| 1484 | lang_output_section_find_by_flags (const asection *sec, |
| 1485 | lang_output_section_statement_type **exact, |
| 1486 | lang_match_sec_type_func match_type) |
| 1487 | { |
| 1488 | lang_output_section_statement_type *first, *look, *found; |
| 1489 | flagword flags; |
| 1490 | |
| 1491 | /* We know the first statement on this list is *ABS*. May as well |
| 1492 | skip it. */ |
| 1493 | first = &lang_output_section_statement.head->output_section_statement; |
| 1494 | first = first->next; |
| 1495 | |
| 1496 | /* First try for an exact match. */ |
| 1497 | found = NULL; |
| 1498 | for (look = first; look; look = look->next) |
| 1499 | { |
| 1500 | flags = look->flags; |
| 1501 | if (look->bfd_section != NULL) |
| 1502 | { |
| 1503 | flags = look->bfd_section->flags; |
| 1504 | if (match_type && !match_type (link_info.output_bfd, |
| 1505 | look->bfd_section, |
| 1506 | sec->owner, sec)) |
| 1507 | continue; |
| 1508 | } |
| 1509 | flags ^= sec->flags; |
| 1510 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY |
| 1511 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) |
| 1512 | found = look; |
| 1513 | } |
| 1514 | if (found != NULL) |
| 1515 | { |
| 1516 | if (exact != NULL) |
| 1517 | *exact = found; |
| 1518 | return found; |
| 1519 | } |
| 1520 | |
| 1521 | if ((sec->flags & SEC_CODE) != 0 |
| 1522 | && (sec->flags & SEC_ALLOC) != 0) |
| 1523 | { |
| 1524 | /* Try for a rw code section. */ |
| 1525 | for (look = first; look; look = look->next) |
| 1526 | { |
| 1527 | flags = look->flags; |
| 1528 | if (look->bfd_section != NULL) |
| 1529 | { |
| 1530 | flags = look->bfd_section->flags; |
| 1531 | if (match_type && !match_type (link_info.output_bfd, |
| 1532 | look->bfd_section, |
| 1533 | sec->owner, sec)) |
| 1534 | continue; |
| 1535 | } |
| 1536 | flags ^= sec->flags; |
| 1537 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD |
| 1538 | | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) |
| 1539 | found = look; |
| 1540 | } |
| 1541 | } |
| 1542 | else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0 |
| 1543 | && (sec->flags & SEC_ALLOC) != 0) |
| 1544 | { |
| 1545 | /* .rodata can go after .text, .sdata2 after .rodata. */ |
| 1546 | for (look = first; look; look = look->next) |
| 1547 | { |
| 1548 | flags = look->flags; |
| 1549 | if (look->bfd_section != NULL) |
| 1550 | { |
| 1551 | flags = look->bfd_section->flags; |
| 1552 | if (match_type && !match_type (link_info.output_bfd, |
| 1553 | look->bfd_section, |
| 1554 | sec->owner, sec)) |
| 1555 | continue; |
| 1556 | } |
| 1557 | flags ^= sec->flags; |
| 1558 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD |
| 1559 | | SEC_READONLY | SEC_SMALL_DATA)) |
| 1560 | || (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD |
| 1561 | | SEC_READONLY)) |
| 1562 | && !(look->flags & SEC_SMALL_DATA)) |
| 1563 | || (!(flags & (SEC_THREAD_LOCAL | SEC_ALLOC)) |
| 1564 | && (look->flags & SEC_THREAD_LOCAL) |
| 1565 | && (!(flags & SEC_LOAD) |
| 1566 | || (look->flags & SEC_LOAD)))) |
| 1567 | found = look; |
| 1568 | } |
| 1569 | } |
| 1570 | else if ((sec->flags & SEC_SMALL_DATA) != 0 |
| 1571 | && (sec->flags & SEC_ALLOC) != 0) |
| 1572 | { |
| 1573 | /* .sdata goes after .data, .sbss after .sdata. */ |
| 1574 | for (look = first; look; look = look->next) |
| 1575 | { |
| 1576 | flags = look->flags; |
| 1577 | if (look->bfd_section != NULL) |
| 1578 | { |
| 1579 | flags = look->bfd_section->flags; |
| 1580 | if (match_type && !match_type (link_info.output_bfd, |
| 1581 | look->bfd_section, |
| 1582 | sec->owner, sec)) |
| 1583 | continue; |
| 1584 | } |
| 1585 | flags ^= sec->flags; |
| 1586 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD |
| 1587 | | SEC_THREAD_LOCAL)) |
| 1588 | || ((look->flags & SEC_SMALL_DATA) |
| 1589 | && !(sec->flags & SEC_HAS_CONTENTS))) |
| 1590 | found = look; |
| 1591 | } |
| 1592 | } |
| 1593 | else if ((sec->flags & SEC_HAS_CONTENTS) != 0 |
| 1594 | && (sec->flags & SEC_ALLOC) != 0) |
| 1595 | { |
| 1596 | /* .data goes after .rodata. */ |
| 1597 | for (look = first; look; look = look->next) |
| 1598 | { |
| 1599 | flags = look->flags; |
| 1600 | if (look->bfd_section != NULL) |
| 1601 | { |
| 1602 | flags = look->bfd_section->flags; |
| 1603 | if (match_type && !match_type (link_info.output_bfd, |
| 1604 | look->bfd_section, |
| 1605 | sec->owner, sec)) |
| 1606 | continue; |
| 1607 | } |
| 1608 | flags ^= sec->flags; |
| 1609 | if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD |
| 1610 | | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) |
| 1611 | found = look; |
| 1612 | } |
| 1613 | } |
| 1614 | else if ((sec->flags & SEC_ALLOC) != 0) |
| 1615 | { |
| 1616 | /* .bss goes after any other alloc section. */ |
| 1617 | for (look = first; look; look = look->next) |
| 1618 | { |
| 1619 | flags = look->flags; |
| 1620 | if (look->bfd_section != NULL) |
| 1621 | { |
| 1622 | flags = look->bfd_section->flags; |
| 1623 | if (match_type && !match_type (link_info.output_bfd, |
| 1624 | look->bfd_section, |
| 1625 | sec->owner, sec)) |
| 1626 | continue; |
| 1627 | } |
| 1628 | flags ^= sec->flags; |
| 1629 | if (!(flags & SEC_ALLOC)) |
| 1630 | found = look; |
| 1631 | } |
| 1632 | } |
| 1633 | else |
| 1634 | { |
| 1635 | /* non-alloc go last. */ |
| 1636 | for (look = first; look; look = look->next) |
| 1637 | { |
| 1638 | flags = look->flags; |
| 1639 | if (look->bfd_section != NULL) |
| 1640 | flags = look->bfd_section->flags; |
| 1641 | flags ^= sec->flags; |
| 1642 | if (!(flags & SEC_DEBUGGING)) |
| 1643 | found = look; |
| 1644 | } |
| 1645 | return found; |
| 1646 | } |
| 1647 | |
| 1648 | if (found || !match_type) |
| 1649 | return found; |
| 1650 | |
| 1651 | return lang_output_section_find_by_flags (sec, NULL, NULL); |
| 1652 | } |
| 1653 | |
| 1654 | /* Find the last output section before given output statement. |
| 1655 | Used by place_orphan. */ |
| 1656 | |
| 1657 | static asection * |
| 1658 | output_prev_sec_find (lang_output_section_statement_type *os) |
| 1659 | { |
| 1660 | lang_output_section_statement_type *lookup; |
| 1661 | |
| 1662 | for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) |
| 1663 | { |
| 1664 | if (lookup->constraint < 0) |
| 1665 | continue; |
| 1666 | |
| 1667 | if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL) |
| 1668 | return lookup->bfd_section; |
| 1669 | } |
| 1670 | |
| 1671 | return NULL; |
| 1672 | } |
| 1673 | |
| 1674 | /* Look for a suitable place for a new output section statement. The |
| 1675 | idea is to skip over anything that might be inside a SECTIONS {} |
| 1676 | statement in a script, before we find another output section |
| 1677 | statement. Assignments to "dot" before an output section statement |
| 1678 | are assumed to belong to it, except in two cases; The first |
| 1679 | assignment to dot, and assignments before non-alloc sections. |
| 1680 | Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or |
| 1681 | similar assignments that set the initial address, or we might |
| 1682 | insert non-alloc note sections among assignments setting end of |
| 1683 | image symbols. */ |
| 1684 | |
| 1685 | static lang_statement_union_type ** |
| 1686 | insert_os_after (lang_output_section_statement_type *after) |
| 1687 | { |
| 1688 | lang_statement_union_type **where; |
| 1689 | lang_statement_union_type **assign = NULL; |
| 1690 | bfd_boolean ignore_first; |
| 1691 | |
| 1692 | ignore_first |
| 1693 | = after == &lang_output_section_statement.head->output_section_statement; |
| 1694 | |
| 1695 | for (where = &after->header.next; |
| 1696 | *where != NULL; |
| 1697 | where = &(*where)->header.next) |
| 1698 | { |
| 1699 | switch ((*where)->header.type) |
| 1700 | { |
| 1701 | case lang_assignment_statement_enum: |
| 1702 | if (assign == NULL) |
| 1703 | { |
| 1704 | lang_assignment_statement_type *ass; |
| 1705 | |
| 1706 | ass = &(*where)->assignment_statement; |
| 1707 | if (ass->exp->type.node_class != etree_assert |
| 1708 | && ass->exp->assign.dst[0] == '.' |
| 1709 | && ass->exp->assign.dst[1] == 0 |
| 1710 | && !ignore_first) |
| 1711 | assign = where; |
| 1712 | } |
| 1713 | ignore_first = FALSE; |
| 1714 | continue; |
| 1715 | case lang_wild_statement_enum: |
| 1716 | case lang_input_section_enum: |
| 1717 | case lang_object_symbols_statement_enum: |
| 1718 | case lang_fill_statement_enum: |
| 1719 | case lang_data_statement_enum: |
| 1720 | case lang_reloc_statement_enum: |
| 1721 | case lang_padding_statement_enum: |
| 1722 | case lang_constructors_statement_enum: |
| 1723 | assign = NULL; |
| 1724 | continue; |
| 1725 | case lang_output_section_statement_enum: |
| 1726 | if (assign != NULL) |
| 1727 | { |
| 1728 | asection *s = (*where)->output_section_statement.bfd_section; |
| 1729 | |
| 1730 | if (s == NULL |
| 1731 | || s->map_head.s == NULL |
| 1732 | || (s->flags & SEC_ALLOC) != 0) |
| 1733 | where = assign; |
| 1734 | } |
| 1735 | break; |
| 1736 | case lang_input_statement_enum: |
| 1737 | case lang_address_statement_enum: |
| 1738 | case lang_target_statement_enum: |
| 1739 | case lang_output_statement_enum: |
| 1740 | case lang_group_statement_enum: |
| 1741 | case lang_insert_statement_enum: |
| 1742 | continue; |
| 1743 | } |
| 1744 | break; |
| 1745 | } |
| 1746 | |
| 1747 | return where; |
| 1748 | } |
| 1749 | |
| 1750 | lang_output_section_statement_type * |
| 1751 | lang_insert_orphan (asection *s, |
| 1752 | const char *secname, |
| 1753 | int constraint, |
| 1754 | lang_output_section_statement_type *after, |
| 1755 | struct orphan_save *place, |
| 1756 | etree_type *address, |
| 1757 | lang_statement_list_type *add_child) |
| 1758 | { |
| 1759 | lang_statement_list_type add; |
| 1760 | const char *ps; |
| 1761 | lang_output_section_statement_type *os; |
| 1762 | lang_output_section_statement_type **os_tail; |
| 1763 | |
| 1764 | /* If we have found an appropriate place for the output section |
| 1765 | statements for this orphan, add them to our own private list, |
| 1766 | inserting them later into the global statement list. */ |
| 1767 | if (after != NULL) |
| 1768 | { |
| 1769 | lang_list_init (&add); |
| 1770 | push_stat_ptr (&add); |
| 1771 | } |
| 1772 | |
| 1773 | if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) |
| 1774 | address = exp_intop (0); |
| 1775 | |
| 1776 | os_tail = ((lang_output_section_statement_type **) |
| 1777 | lang_output_section_statement.tail); |
| 1778 | os = lang_enter_output_section_statement (secname, address, normal_section, |
| 1779 | NULL, NULL, NULL, constraint, 0); |
| 1780 | |
| 1781 | ps = NULL; |
| 1782 | if (config.build_constructors && *os_tail == os) |
| 1783 | { |
| 1784 | /* If the name of the section is representable in C, then create |
| 1785 | symbols to mark the start and the end of the section. */ |
| 1786 | for (ps = secname; *ps != '\0'; ps++) |
| 1787 | if (! ISALNUM ((unsigned char) *ps) && *ps != '_') |
| 1788 | break; |
| 1789 | if (*ps == '\0') |
| 1790 | { |
| 1791 | char *symname; |
| 1792 | |
| 1793 | symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1); |
| 1794 | symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd); |
| 1795 | sprintf (symname + (symname[0] != 0), "__start_%s", secname); |
| 1796 | lang_add_assignment (exp_provide (symname, |
| 1797 | exp_nameop (NAME, "."), |
| 1798 | FALSE)); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | if (add_child == NULL) |
| 1803 | add_child = &os->children; |
| 1804 | lang_add_section (add_child, s, NULL, os); |
| 1805 | |
| 1806 | if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) |
| 1807 | { |
| 1808 | const char *region = (after->region |
| 1809 | ? after->region->name_list.name |
| 1810 | : DEFAULT_MEMORY_REGION); |
| 1811 | const char *lma_region = (after->lma_region |
| 1812 | ? after->lma_region->name_list.name |
| 1813 | : NULL); |
| 1814 | lang_leave_output_section_statement (NULL, region, after->phdrs, |
| 1815 | lma_region); |
| 1816 | } |
| 1817 | else |
| 1818 | lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, |
| 1819 | NULL); |
| 1820 | |
| 1821 | if (ps != NULL && *ps == '\0') |
| 1822 | { |
| 1823 | char *symname; |
| 1824 | |
| 1825 | symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1); |
| 1826 | symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd); |
| 1827 | sprintf (symname + (symname[0] != 0), "__stop_%s", secname); |
| 1828 | lang_add_assignment (exp_provide (symname, |
| 1829 | exp_nameop (NAME, "."), |
| 1830 | FALSE)); |
| 1831 | } |
| 1832 | |
| 1833 | /* Restore the global list pointer. */ |
| 1834 | if (after != NULL) |
| 1835 | pop_stat_ptr (); |
| 1836 | |
| 1837 | if (after != NULL && os->bfd_section != NULL) |
| 1838 | { |
| 1839 | asection *snew, *as; |
| 1840 | |
| 1841 | snew = os->bfd_section; |
| 1842 | |
| 1843 | /* Shuffle the bfd section list to make the output file look |
| 1844 | neater. This is really only cosmetic. */ |
| 1845 | if (place->section == NULL |
| 1846 | && after != (&lang_output_section_statement.head |
| 1847 | ->output_section_statement)) |
| 1848 | { |
| 1849 | asection *bfd_section = after->bfd_section; |
| 1850 | |
| 1851 | /* If the output statement hasn't been used to place any input |
| 1852 | sections (and thus doesn't have an output bfd_section), |
| 1853 | look for the closest prior output statement having an |
| 1854 | output section. */ |
| 1855 | if (bfd_section == NULL) |
| 1856 | bfd_section = output_prev_sec_find (after); |
| 1857 | |
| 1858 | if (bfd_section != NULL && bfd_section != snew) |
| 1859 | place->section = &bfd_section->next; |
| 1860 | } |
| 1861 | |
| 1862 | if (place->section == NULL) |
| 1863 | place->section = &link_info.output_bfd->sections; |
| 1864 | |
| 1865 | as = *place->section; |
| 1866 | |
| 1867 | if (!as) |
| 1868 | { |
| 1869 | /* Put the section at the end of the list. */ |
| 1870 | |
| 1871 | /* Unlink the section. */ |
| 1872 | bfd_section_list_remove (link_info.output_bfd, snew); |
| 1873 | |
| 1874 | /* Now tack it back on in the right place. */ |
| 1875 | bfd_section_list_append (link_info.output_bfd, snew); |
| 1876 | } |
| 1877 | else if (as != snew && as->prev != snew) |
| 1878 | { |
| 1879 | /* Unlink the section. */ |
| 1880 | bfd_section_list_remove (link_info.output_bfd, snew); |
| 1881 | |
| 1882 | /* Now tack it back on in the right place. */ |
| 1883 | bfd_section_list_insert_before (link_info.output_bfd, as, snew); |
| 1884 | } |
| 1885 | |
| 1886 | /* Save the end of this list. Further ophans of this type will |
| 1887 | follow the one we've just added. */ |
| 1888 | place->section = &snew->next; |
| 1889 | |
| 1890 | /* The following is non-cosmetic. We try to put the output |
| 1891 | statements in some sort of reasonable order here, because they |
| 1892 | determine the final load addresses of the orphan sections. |
| 1893 | In addition, placing output statements in the wrong order may |
| 1894 | require extra segments. For instance, given a typical |
| 1895 | situation of all read-only sections placed in one segment and |
| 1896 | following that a segment containing all the read-write |
| 1897 | sections, we wouldn't want to place an orphan read/write |
| 1898 | section before or amongst the read-only ones. */ |
| 1899 | if (add.head != NULL) |
| 1900 | { |
| 1901 | lang_output_section_statement_type *newly_added_os; |
| 1902 | |
| 1903 | if (place->stmt == NULL) |
| 1904 | { |
| 1905 | lang_statement_union_type **where = insert_os_after (after); |
| 1906 | |
| 1907 | *add.tail = *where; |
| 1908 | *where = add.head; |
| 1909 | |
| 1910 | place->os_tail = &after->next; |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | /* Put it after the last orphan statement we added. */ |
| 1915 | *add.tail = *place->stmt; |
| 1916 | *place->stmt = add.head; |
| 1917 | } |
| 1918 | |
| 1919 | /* Fix the global list pointer if we happened to tack our |
| 1920 | new list at the tail. */ |
| 1921 | if (*stat_ptr->tail == add.head) |
| 1922 | stat_ptr->tail = add.tail; |
| 1923 | |
| 1924 | /* Save the end of this list. */ |
| 1925 | place->stmt = add.tail; |
| 1926 | |
| 1927 | /* Do the same for the list of output section statements. */ |
| 1928 | newly_added_os = *os_tail; |
| 1929 | *os_tail = NULL; |
| 1930 | newly_added_os->prev = (lang_output_section_statement_type *) |
| 1931 | ((char *) place->os_tail |
| 1932 | - offsetof (lang_output_section_statement_type, next)); |
| 1933 | newly_added_os->next = *place->os_tail; |
| 1934 | if (newly_added_os->next != NULL) |
| 1935 | newly_added_os->next->prev = newly_added_os; |
| 1936 | *place->os_tail = newly_added_os; |
| 1937 | place->os_tail = &newly_added_os->next; |
| 1938 | |
| 1939 | /* Fixing the global list pointer here is a little different. |
| 1940 | We added to the list in lang_enter_output_section_statement, |
| 1941 | trimmed off the new output_section_statment above when |
| 1942 | assigning *os_tail = NULL, but possibly added it back in |
| 1943 | the same place when assigning *place->os_tail. */ |
| 1944 | if (*os_tail == NULL) |
| 1945 | lang_output_section_statement.tail |
| 1946 | = (lang_statement_union_type **) os_tail; |
| 1947 | } |
| 1948 | } |
| 1949 | return os; |
| 1950 | } |
| 1951 | |
| 1952 | static void |
| 1953 | lang_map_flags (flagword flag) |
| 1954 | { |
| 1955 | if (flag & SEC_ALLOC) |
| 1956 | minfo ("a"); |
| 1957 | |
| 1958 | if (flag & SEC_CODE) |
| 1959 | minfo ("x"); |
| 1960 | |
| 1961 | if (flag & SEC_READONLY) |
| 1962 | minfo ("r"); |
| 1963 | |
| 1964 | if (flag & SEC_DATA) |
| 1965 | minfo ("w"); |
| 1966 | |
| 1967 | if (flag & SEC_LOAD) |
| 1968 | minfo ("l"); |
| 1969 | } |
| 1970 | |
| 1971 | void |
| 1972 | lang_map (void) |
| 1973 | { |
| 1974 | lang_memory_region_type *m; |
| 1975 | bfd_boolean dis_header_printed = FALSE; |
| 1976 | |
| 1977 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
| 1978 | { |
| 1979 | asection *s; |
| 1980 | |
| 1981 | if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 |
| 1982 | || file->flags.just_syms) |
| 1983 | continue; |
| 1984 | |
| 1985 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 1986 | if ((s->output_section == NULL |
| 1987 | || s->output_section->owner != link_info.output_bfd) |
| 1988 | && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) |
| 1989 | { |
| 1990 | if (! dis_header_printed) |
| 1991 | { |
| 1992 | fprintf (config.map_file, _("\nDiscarded input sections\n\n")); |
| 1993 | dis_header_printed = TRUE; |
| 1994 | } |
| 1995 | |
| 1996 | print_input_section (s, TRUE); |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | minfo (_("\nMemory Configuration\n\n")); |
| 2001 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", |
| 2002 | _("Name"), _("Origin"), _("Length"), _("Attributes")); |
| 2003 | |
| 2004 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
| 2005 | { |
| 2006 | char buf[100]; |
| 2007 | int len; |
| 2008 | |
| 2009 | fprintf (config.map_file, "%-16s ", m->name_list.name); |
| 2010 | |
| 2011 | sprintf_vma (buf, m->origin); |
| 2012 | minfo ("0x%s ", buf); |
| 2013 | len = strlen (buf); |
| 2014 | while (len < 16) |
| 2015 | { |
| 2016 | print_space (); |
| 2017 | ++len; |
| 2018 | } |
| 2019 | |
| 2020 | minfo ("0x%V", m->length); |
| 2021 | if (m->flags || m->not_flags) |
| 2022 | { |
| 2023 | #ifndef BFD64 |
| 2024 | minfo (" "); |
| 2025 | #endif |
| 2026 | if (m->flags) |
| 2027 | { |
| 2028 | print_space (); |
| 2029 | lang_map_flags (m->flags); |
| 2030 | } |
| 2031 | |
| 2032 | if (m->not_flags) |
| 2033 | { |
| 2034 | minfo (" !"); |
| 2035 | lang_map_flags (m->not_flags); |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | print_nl (); |
| 2040 | } |
| 2041 | |
| 2042 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); |
| 2043 | |
| 2044 | if (! link_info.reduce_memory_overheads) |
| 2045 | { |
| 2046 | obstack_begin (&map_obstack, 1000); |
| 2047 | bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); |
| 2048 | } |
| 2049 | lang_statement_iteration ++; |
| 2050 | print_statements (); |
| 2051 | } |
| 2052 | |
| 2053 | static bfd_boolean |
| 2054 | sort_def_symbol (struct bfd_link_hash_entry *hash_entry, |
| 2055 | void *info ATTRIBUTE_UNUSED) |
| 2056 | { |
| 2057 | if ((hash_entry->type == bfd_link_hash_defined |
| 2058 | || hash_entry->type == bfd_link_hash_defweak) |
| 2059 | && hash_entry->u.def.section->owner != link_info.output_bfd |
| 2060 | && hash_entry->u.def.section->owner != NULL) |
| 2061 | { |
| 2062 | input_section_userdata_type *ud; |
| 2063 | struct map_symbol_def *def; |
| 2064 | |
| 2065 | ud = ((input_section_userdata_type *) |
| 2066 | get_userdata (hash_entry->u.def.section)); |
| 2067 | if (!ud) |
| 2068 | { |
| 2069 | ud = (input_section_userdata_type *) stat_alloc (sizeof (*ud)); |
| 2070 | get_userdata (hash_entry->u.def.section) = ud; |
| 2071 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
| 2072 | ud->map_symbol_def_count = 0; |
| 2073 | } |
| 2074 | else if (!ud->map_symbol_def_tail) |
| 2075 | ud->map_symbol_def_tail = &ud->map_symbol_def_head; |
| 2076 | |
| 2077 | def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); |
| 2078 | def->entry = hash_entry; |
| 2079 | *(ud->map_symbol_def_tail) = def; |
| 2080 | ud->map_symbol_def_tail = &def->next; |
| 2081 | ud->map_symbol_def_count++; |
| 2082 | } |
| 2083 | return TRUE; |
| 2084 | } |
| 2085 | |
| 2086 | /* Initialize an output section. */ |
| 2087 | |
| 2088 | static void |
| 2089 | init_os (lang_output_section_statement_type *s, flagword flags) |
| 2090 | { |
| 2091 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
| 2092 | einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
| 2093 | |
| 2094 | if (s->constraint != SPECIAL) |
| 2095 | s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); |
| 2096 | if (s->bfd_section == NULL) |
| 2097 | s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, |
| 2098 | s->name, flags); |
| 2099 | if (s->bfd_section == NULL) |
| 2100 | { |
| 2101 | einfo (_("%P%F: output format %s cannot represent section called %s\n"), |
| 2102 | link_info.output_bfd->xvec->name, s->name); |
| 2103 | } |
| 2104 | s->bfd_section->output_section = s->bfd_section; |
| 2105 | s->bfd_section->output_offset = 0; |
| 2106 | |
| 2107 | /* If there is a base address, make sure that any sections it might |
| 2108 | mention are initialized. */ |
| 2109 | if (s->addr_tree != NULL) |
| 2110 | exp_init_os (s->addr_tree); |
| 2111 | |
| 2112 | if (s->load_base != NULL) |
| 2113 | exp_init_os (s->load_base); |
| 2114 | |
| 2115 | /* If supplied an alignment, set it. */ |
| 2116 | if (s->section_alignment != -1) |
| 2117 | s->bfd_section->alignment_power = s->section_alignment; |
| 2118 | } |
| 2119 | |
| 2120 | /* Make sure that all output sections mentioned in an expression are |
| 2121 | initialized. */ |
| 2122 | |
| 2123 | static void |
| 2124 | exp_init_os (etree_type *exp) |
| 2125 | { |
| 2126 | switch (exp->type.node_class) |
| 2127 | { |
| 2128 | case etree_assign: |
| 2129 | case etree_provide: |
| 2130 | exp_init_os (exp->assign.src); |
| 2131 | break; |
| 2132 | |
| 2133 | case etree_binary: |
| 2134 | exp_init_os (exp->binary.lhs); |
| 2135 | exp_init_os (exp->binary.rhs); |
| 2136 | break; |
| 2137 | |
| 2138 | case etree_trinary: |
| 2139 | exp_init_os (exp->trinary.cond); |
| 2140 | exp_init_os (exp->trinary.lhs); |
| 2141 | exp_init_os (exp->trinary.rhs); |
| 2142 | break; |
| 2143 | |
| 2144 | case etree_assert: |
| 2145 | exp_init_os (exp->assert_s.child); |
| 2146 | break; |
| 2147 | |
| 2148 | case etree_unary: |
| 2149 | exp_init_os (exp->unary.child); |
| 2150 | break; |
| 2151 | |
| 2152 | case etree_name: |
| 2153 | switch (exp->type.node_code) |
| 2154 | { |
| 2155 | case ADDR: |
| 2156 | case LOADADDR: |
| 2157 | case SIZEOF: |
| 2158 | { |
| 2159 | lang_output_section_statement_type *os; |
| 2160 | |
| 2161 | os = lang_output_section_find (exp->name.name); |
| 2162 | if (os != NULL && os->bfd_section == NULL) |
| 2163 | init_os (os, 0); |
| 2164 | } |
| 2165 | } |
| 2166 | break; |
| 2167 | |
| 2168 | default: |
| 2169 | break; |
| 2170 | } |
| 2171 | } |
| 2172 | \f |
| 2173 | static void |
| 2174 | section_already_linked (bfd *abfd, asection *sec, void *data) |
| 2175 | { |
| 2176 | lang_input_statement_type *entry = (lang_input_statement_type *) data; |
| 2177 | |
| 2178 | /* If we are only reading symbols from this object, then we want to |
| 2179 | discard all sections. */ |
| 2180 | if (entry->flags.just_syms) |
| 2181 | { |
| 2182 | bfd_link_just_syms (abfd, sec, &link_info); |
| 2183 | return; |
| 2184 | } |
| 2185 | |
| 2186 | if (!(abfd->flags & DYNAMIC)) |
| 2187 | bfd_section_already_linked (abfd, sec, &link_info); |
| 2188 | } |
| 2189 | \f |
| 2190 | /* The wild routines. |
| 2191 | |
| 2192 | These expand statements like *(.text) and foo.o to a list of |
| 2193 | explicit actions, like foo.o(.text), bar.o(.text) and |
| 2194 | foo.o(.text, .data). */ |
| 2195 | |
| 2196 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
| 2197 | lang_input_section statement which is placed at PTR. */ |
| 2198 | |
| 2199 | void |
| 2200 | lang_add_section (lang_statement_list_type *ptr, |
| 2201 | asection *section, |
| 2202 | struct flag_info *sflag_info, |
| 2203 | lang_output_section_statement_type *output) |
| 2204 | { |
| 2205 | flagword flags = section->flags; |
| 2206 | |
| 2207 | bfd_boolean discard; |
| 2208 | lang_input_section_type *new_section; |
| 2209 | bfd *abfd = link_info.output_bfd; |
| 2210 | |
| 2211 | /* Discard sections marked with SEC_EXCLUDE. */ |
| 2212 | discard = (flags & SEC_EXCLUDE) != 0; |
| 2213 | |
| 2214 | /* Discard input sections which are assigned to a section named |
| 2215 | DISCARD_SECTION_NAME. */ |
| 2216 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) |
| 2217 | discard = TRUE; |
| 2218 | |
| 2219 | /* Discard debugging sections if we are stripping debugging |
| 2220 | information. */ |
| 2221 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) |
| 2222 | && (flags & SEC_DEBUGGING) != 0) |
| 2223 | discard = TRUE; |
| 2224 | |
| 2225 | if (discard) |
| 2226 | { |
| 2227 | if (section->output_section == NULL) |
| 2228 | { |
| 2229 | /* This prevents future calls from assigning this section. */ |
| 2230 | section->output_section = bfd_abs_section_ptr; |
| 2231 | } |
| 2232 | return; |
| 2233 | } |
| 2234 | |
| 2235 | if (sflag_info) |
| 2236 | { |
| 2237 | bfd_boolean keep; |
| 2238 | |
| 2239 | keep = bfd_lookup_section_flags (&link_info, sflag_info, section); |
| 2240 | if (!keep) |
| 2241 | return; |
| 2242 | } |
| 2243 | |
| 2244 | if (section->output_section != NULL) |
| 2245 | return; |
| 2246 | |
| 2247 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
| 2248 | to an output section, because we want to be able to include a |
| 2249 | SEC_NEVER_LOAD section in the middle of an otherwise loaded |
| 2250 | section (I don't know why we want to do this, but we do). |
| 2251 | build_link_order in ldwrite.c handles this case by turning |
| 2252 | the embedded SEC_NEVER_LOAD section into a fill. */ |
| 2253 | flags &= ~ SEC_NEVER_LOAD; |
| 2254 | |
| 2255 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
| 2256 | already been processed. One reason to do this is that on pe |
| 2257 | format targets, .text$foo sections go into .text and it's odd |
| 2258 | to see .text with SEC_LINK_ONCE set. */ |
| 2259 | |
| 2260 | if (!link_info.relocatable) |
| 2261 | flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); |
| 2262 | |
| 2263 | switch (output->sectype) |
| 2264 | { |
| 2265 | case normal_section: |
| 2266 | case overlay_section: |
| 2267 | break; |
| 2268 | case noalloc_section: |
| 2269 | flags &= ~SEC_ALLOC; |
| 2270 | break; |
| 2271 | case noload_section: |
| 2272 | flags &= ~SEC_LOAD; |
| 2273 | flags |= SEC_NEVER_LOAD; |
| 2274 | /* Unfortunately GNU ld has managed to evolve two different |
| 2275 | meanings to NOLOAD in scripts. ELF gets a .bss style noload, |
| 2276 | alloc, no contents section. All others get a noload, noalloc |
| 2277 | section. */ |
| 2278 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) |
| 2279 | flags &= ~SEC_HAS_CONTENTS; |
| 2280 | else |
| 2281 | flags &= ~SEC_ALLOC; |
| 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | if (output->bfd_section == NULL) |
| 2286 | init_os (output, flags); |
| 2287 | |
| 2288 | /* If SEC_READONLY is not set in the input section, then clear |
| 2289 | it from the output section. */ |
| 2290 | output->bfd_section->flags &= flags | ~SEC_READONLY; |
| 2291 | |
| 2292 | if (output->bfd_section->linker_has_input) |
| 2293 | { |
| 2294 | /* Only set SEC_READONLY flag on the first input section. */ |
| 2295 | flags &= ~ SEC_READONLY; |
| 2296 | |
| 2297 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
| 2298 | if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) |
| 2299 | != (flags & (SEC_MERGE | SEC_STRINGS)) |
| 2300 | || ((flags & SEC_MERGE) != 0 |
| 2301 | && output->bfd_section->entsize != section->entsize)) |
| 2302 | { |
| 2303 | output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
| 2304 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
| 2305 | } |
| 2306 | } |
| 2307 | output->bfd_section->flags |= flags; |
| 2308 | |
| 2309 | if (!output->bfd_section->linker_has_input) |
| 2310 | { |
| 2311 | output->bfd_section->linker_has_input = 1; |
| 2312 | /* This must happen after flags have been updated. The output |
| 2313 | section may have been created before we saw its first input |
| 2314 | section, eg. for a data statement. */ |
| 2315 | bfd_init_private_section_data (section->owner, section, |
| 2316 | link_info.output_bfd, |
| 2317 | output->bfd_section, |
| 2318 | &link_info); |
| 2319 | if ((flags & SEC_MERGE) != 0) |
| 2320 | output->bfd_section->entsize = section->entsize; |
| 2321 | } |
| 2322 | |
| 2323 | if ((flags & SEC_TIC54X_BLOCK) != 0 |
| 2324 | && bfd_get_arch (section->owner) == bfd_arch_tic54x) |
| 2325 | { |
| 2326 | /* FIXME: This value should really be obtained from the bfd... */ |
| 2327 | output->block_value = 128; |
| 2328 | } |
| 2329 | |
| 2330 | if (section->alignment_power > output->bfd_section->alignment_power) |
| 2331 | output->bfd_section->alignment_power = section->alignment_power; |
| 2332 | |
| 2333 | section->output_section = output->bfd_section; |
| 2334 | |
| 2335 | if (!link_info.relocatable |
| 2336 | && !stripped_excluded_sections) |
| 2337 | { |
| 2338 | asection *s = output->bfd_section->map_tail.s; |
| 2339 | output->bfd_section->map_tail.s = section; |
| 2340 | section->map_head.s = NULL; |
| 2341 | section->map_tail.s = s; |
| 2342 | if (s != NULL) |
| 2343 | s->map_head.s = section; |
| 2344 | else |
| 2345 | output->bfd_section->map_head.s = section; |
| 2346 | } |
| 2347 | |
| 2348 | /* Add a section reference to the list. */ |
| 2349 | new_section = new_stat (lang_input_section, ptr); |
| 2350 | new_section->section = section; |
| 2351 | } |
| 2352 | |
| 2353 | /* Handle wildcard sorting. This returns the lang_input_section which |
| 2354 | should follow the one we are going to create for SECTION and FILE, |
| 2355 | based on the sorting requirements of WILD. It returns NULL if the |
| 2356 | new section should just go at the end of the current list. */ |
| 2357 | |
| 2358 | static lang_statement_union_type * |
| 2359 | wild_sort (lang_wild_statement_type *wild, |
| 2360 | struct wildcard_list *sec, |
| 2361 | lang_input_statement_type *file, |
| 2362 | asection *section) |
| 2363 | { |
| 2364 | lang_statement_union_type *l; |
| 2365 | |
| 2366 | if (!wild->filenames_sorted |
| 2367 | && (sec == NULL || sec->spec.sorted == none)) |
| 2368 | return NULL; |
| 2369 | |
| 2370 | for (l = wild->children.head; l != NULL; l = l->header.next) |
| 2371 | { |
| 2372 | lang_input_section_type *ls; |
| 2373 | |
| 2374 | if (l->header.type != lang_input_section_enum) |
| 2375 | continue; |
| 2376 | ls = &l->input_section; |
| 2377 | |
| 2378 | /* Sorting by filename takes precedence over sorting by section |
| 2379 | name. */ |
| 2380 | |
| 2381 | if (wild->filenames_sorted) |
| 2382 | { |
| 2383 | const char *fn, *ln; |
| 2384 | bfd_boolean fa, la; |
| 2385 | int i; |
| 2386 | |
| 2387 | /* The PE support for the .idata section as generated by |
| 2388 | dlltool assumes that files will be sorted by the name of |
| 2389 | the archive and then the name of the file within the |
| 2390 | archive. */ |
| 2391 | |
| 2392 | if (file->the_bfd != NULL |
| 2393 | && bfd_my_archive (file->the_bfd) != NULL) |
| 2394 | { |
| 2395 | fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); |
| 2396 | fa = TRUE; |
| 2397 | } |
| 2398 | else |
| 2399 | { |
| 2400 | fn = file->filename; |
| 2401 | fa = FALSE; |
| 2402 | } |
| 2403 | |
| 2404 | if (bfd_my_archive (ls->section->owner) != NULL) |
| 2405 | { |
| 2406 | ln = bfd_get_filename (bfd_my_archive (ls->section->owner)); |
| 2407 | la = TRUE; |
| 2408 | } |
| 2409 | else |
| 2410 | { |
| 2411 | ln = ls->section->owner->filename; |
| 2412 | la = FALSE; |
| 2413 | } |
| 2414 | |
| 2415 | i = filename_cmp (fn, ln); |
| 2416 | if (i > 0) |
| 2417 | continue; |
| 2418 | else if (i < 0) |
| 2419 | break; |
| 2420 | |
| 2421 | if (fa || la) |
| 2422 | { |
| 2423 | if (fa) |
| 2424 | fn = file->filename; |
| 2425 | if (la) |
| 2426 | ln = ls->section->owner->filename; |
| 2427 | |
| 2428 | i = filename_cmp (fn, ln); |
| 2429 | if (i > 0) |
| 2430 | continue; |
| 2431 | else if (i < 0) |
| 2432 | break; |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | /* Here either the files are not sorted by name, or we are |
| 2437 | looking at the sections for this file. */ |
| 2438 | |
| 2439 | if (sec != NULL |
| 2440 | && sec->spec.sorted != none |
| 2441 | && sec->spec.sorted != by_none) |
| 2442 | if (compare_section (sec->spec.sorted, section, ls->section) < 0) |
| 2443 | break; |
| 2444 | } |
| 2445 | |
| 2446 | return l; |
| 2447 | } |
| 2448 | |
| 2449 | /* Expand a wild statement for a particular FILE. SECTION may be |
| 2450 | NULL, in which case it is a wild card. */ |
| 2451 | |
| 2452 | static void |
| 2453 | output_section_callback (lang_wild_statement_type *ptr, |
| 2454 | struct wildcard_list *sec, |
| 2455 | asection *section, |
| 2456 | struct flag_info *sflag_info, |
| 2457 | lang_input_statement_type *file, |
| 2458 | void *output) |
| 2459 | { |
| 2460 | lang_statement_union_type *before; |
| 2461 | lang_output_section_statement_type *os; |
| 2462 | |
| 2463 | os = (lang_output_section_statement_type *) output; |
| 2464 | |
| 2465 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
| 2466 | if (unique_section_p (section, os)) |
| 2467 | return; |
| 2468 | |
| 2469 | before = wild_sort (ptr, sec, file, section); |
| 2470 | |
| 2471 | /* Here BEFORE points to the lang_input_section which |
| 2472 | should follow the one we are about to add. If BEFORE |
| 2473 | is NULL, then the section should just go at the end |
| 2474 | of the current list. */ |
| 2475 | |
| 2476 | if (before == NULL) |
| 2477 | lang_add_section (&ptr->children, section, sflag_info, os); |
| 2478 | else |
| 2479 | { |
| 2480 | lang_statement_list_type list; |
| 2481 | lang_statement_union_type **pp; |
| 2482 | |
| 2483 | lang_list_init (&list); |
| 2484 | lang_add_section (&list, section, sflag_info, os); |
| 2485 | |
| 2486 | /* If we are discarding the section, LIST.HEAD will |
| 2487 | be NULL. */ |
| 2488 | if (list.head != NULL) |
| 2489 | { |
| 2490 | ASSERT (list.head->header.next == NULL); |
| 2491 | |
| 2492 | for (pp = &ptr->children.head; |
| 2493 | *pp != before; |
| 2494 | pp = &(*pp)->header.next) |
| 2495 | ASSERT (*pp != NULL); |
| 2496 | |
| 2497 | list.head->header.next = *pp; |
| 2498 | *pp = list.head; |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /* Check if all sections in a wild statement for a particular FILE |
| 2504 | are readonly. */ |
| 2505 | |
| 2506 | static void |
| 2507 | check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, |
| 2508 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
| 2509 | asection *section, |
| 2510 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
| 2511 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
| 2512 | void *output) |
| 2513 | { |
| 2514 | lang_output_section_statement_type *os; |
| 2515 | |
| 2516 | os = (lang_output_section_statement_type *) output; |
| 2517 | |
| 2518 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
| 2519 | if (unique_section_p (section, os)) |
| 2520 | return; |
| 2521 | |
| 2522 | if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) |
| 2523 | os->all_input_readonly = FALSE; |
| 2524 | } |
| 2525 | |
| 2526 | /* This is passed a file name which must have been seen already and |
| 2527 | added to the statement tree. We will see if it has been opened |
| 2528 | already and had its symbols read. If not then we'll read it. */ |
| 2529 | |
| 2530 | static lang_input_statement_type * |
| 2531 | lookup_name (const char *name) |
| 2532 | { |
| 2533 | lang_input_statement_type *search; |
| 2534 | |
| 2535 | for (search = (lang_input_statement_type *) input_file_chain.head; |
| 2536 | search != NULL; |
| 2537 | search = (lang_input_statement_type *) search->next_real_file) |
| 2538 | { |
| 2539 | /* Use the local_sym_name as the name of the file that has |
| 2540 | already been loaded as filename might have been transformed |
| 2541 | via the search directory lookup mechanism. */ |
| 2542 | const char *filename = search->local_sym_name; |
| 2543 | |
| 2544 | if (filename != NULL |
| 2545 | && filename_cmp (filename, name) == 0) |
| 2546 | break; |
| 2547 | } |
| 2548 | |
| 2549 | if (search == NULL) |
| 2550 | search = new_afile (name, lang_input_file_is_search_file_enum, |
| 2551 | default_target, FALSE); |
| 2552 | |
| 2553 | /* If we have already added this file, or this file is not real |
| 2554 | don't add this file. */ |
| 2555 | if (search->flags.loaded || !search->flags.real) |
| 2556 | return search; |
| 2557 | |
| 2558 | if (! load_symbols (search, NULL)) |
| 2559 | return NULL; |
| 2560 | |
| 2561 | return search; |
| 2562 | } |
| 2563 | |
| 2564 | /* Save LIST as a list of libraries whose symbols should not be exported. */ |
| 2565 | |
| 2566 | struct excluded_lib |
| 2567 | { |
| 2568 | char *name; |
| 2569 | struct excluded_lib *next; |
| 2570 | }; |
| 2571 | static struct excluded_lib *excluded_libs; |
| 2572 | |
| 2573 | void |
| 2574 | add_excluded_libs (const char *list) |
| 2575 | { |
| 2576 | const char *p = list, *end; |
| 2577 | |
| 2578 | while (*p != '\0') |
| 2579 | { |
| 2580 | struct excluded_lib *entry; |
| 2581 | end = strpbrk (p, ",:"); |
| 2582 | if (end == NULL) |
| 2583 | end = p + strlen (p); |
| 2584 | entry = (struct excluded_lib *) xmalloc (sizeof (*entry)); |
| 2585 | entry->next = excluded_libs; |
| 2586 | entry->name = (char *) xmalloc (end - p + 1); |
| 2587 | memcpy (entry->name, p, end - p); |
| 2588 | entry->name[end - p] = '\0'; |
| 2589 | excluded_libs = entry; |
| 2590 | if (*end == '\0') |
| 2591 | break; |
| 2592 | p = end + 1; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | static void |
| 2597 | check_excluded_libs (bfd *abfd) |
| 2598 | { |
| 2599 | struct excluded_lib *lib = excluded_libs; |
| 2600 | |
| 2601 | while (lib) |
| 2602 | { |
| 2603 | int len = strlen (lib->name); |
| 2604 | const char *filename = lbasename (abfd->filename); |
| 2605 | |
| 2606 | if (strcmp (lib->name, "ALL") == 0) |
| 2607 | { |
| 2608 | abfd->no_export = TRUE; |
| 2609 | return; |
| 2610 | } |
| 2611 | |
| 2612 | if (filename_ncmp (lib->name, filename, len) == 0 |
| 2613 | && (filename[len] == '\0' |
| 2614 | || (filename[len] == '.' && filename[len + 1] == 'a' |
| 2615 | && filename[len + 2] == '\0'))) |
| 2616 | { |
| 2617 | abfd->no_export = TRUE; |
| 2618 | return; |
| 2619 | } |
| 2620 | |
| 2621 | lib = lib->next; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | /* Get the symbols for an input file. */ |
| 2626 | |
| 2627 | bfd_boolean |
| 2628 | load_symbols (lang_input_statement_type *entry, |
| 2629 | lang_statement_list_type *place) |
| 2630 | { |
| 2631 | char **matching; |
| 2632 | |
| 2633 | if (entry->flags.loaded) |
| 2634 | return TRUE; |
| 2635 | |
| 2636 | ldfile_open_file (entry); |
| 2637 | |
| 2638 | /* Do not process further if the file was missing. */ |
| 2639 | if (entry->flags.missing_file) |
| 2640 | return TRUE; |
| 2641 | |
| 2642 | if (! bfd_check_format (entry->the_bfd, bfd_archive) |
| 2643 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) |
| 2644 | { |
| 2645 | bfd_error_type err; |
| 2646 | struct lang_input_statement_flags save_flags; |
| 2647 | extern FILE *yyin; |
| 2648 | |
| 2649 | err = bfd_get_error (); |
| 2650 | |
| 2651 | /* See if the emulation has some special knowledge. */ |
| 2652 | if (ldemul_unrecognized_file (entry)) |
| 2653 | return TRUE; |
| 2654 | |
| 2655 | if (err == bfd_error_file_ambiguously_recognized) |
| 2656 | { |
| 2657 | char **p; |
| 2658 | |
| 2659 | einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); |
| 2660 | einfo (_("%B: matching formats:"), entry->the_bfd); |
| 2661 | for (p = matching; *p != NULL; p++) |
| 2662 | einfo (" %s", *p); |
| 2663 | einfo ("%F\n"); |
| 2664 | } |
| 2665 | else if (err != bfd_error_file_not_recognized |
| 2666 | || place == NULL) |
| 2667 | einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); |
| 2668 | |
| 2669 | bfd_close (entry->the_bfd); |
| 2670 | entry->the_bfd = NULL; |
| 2671 | |
| 2672 | /* Try to interpret the file as a linker script. */ |
| 2673 | save_flags = input_flags; |
| 2674 | ldfile_open_command_file (entry->filename); |
| 2675 | |
| 2676 | push_stat_ptr (place); |
| 2677 | input_flags.add_DT_NEEDED_for_regular |
| 2678 | = entry->flags.add_DT_NEEDED_for_regular; |
| 2679 | input_flags.add_DT_NEEDED_for_dynamic |
| 2680 | = entry->flags.add_DT_NEEDED_for_dynamic; |
| 2681 | input_flags.whole_archive = entry->flags.whole_archive; |
| 2682 | input_flags.dynamic = entry->flags.dynamic; |
| 2683 | |
| 2684 | ldfile_assumed_script = TRUE; |
| 2685 | parser_input = input_script; |
| 2686 | yyparse (); |
| 2687 | ldfile_assumed_script = FALSE; |
| 2688 | |
| 2689 | /* missing_file is sticky. sysrooted will already have been |
| 2690 | restored when seeing EOF in yyparse, but no harm to restore |
| 2691 | again. */ |
| 2692 | save_flags.missing_file |= input_flags.missing_file; |
| 2693 | input_flags = save_flags; |
| 2694 | pop_stat_ptr (); |
| 2695 | fclose (yyin); |
| 2696 | yyin = NULL; |
| 2697 | entry->flags.loaded = TRUE; |
| 2698 | |
| 2699 | return TRUE; |
| 2700 | } |
| 2701 | |
| 2702 | if (ldemul_recognized_file (entry)) |
| 2703 | return TRUE; |
| 2704 | |
| 2705 | /* We don't call ldlang_add_file for an archive. Instead, the |
| 2706 | add_symbols entry point will call ldlang_add_file, via the |
| 2707 | add_archive_element callback, for each element of the archive |
| 2708 | which is used. */ |
| 2709 | switch (bfd_get_format (entry->the_bfd)) |
| 2710 | { |
| 2711 | default: |
| 2712 | break; |
| 2713 | |
| 2714 | case bfd_object: |
| 2715 | #ifdef ENABLE_PLUGINS |
| 2716 | if (!entry->flags.reload) |
| 2717 | #endif |
| 2718 | ldlang_add_file (entry); |
| 2719 | if (trace_files || verbose) |
| 2720 | info_msg ("%I\n", entry); |
| 2721 | break; |
| 2722 | |
| 2723 | case bfd_archive: |
| 2724 | check_excluded_libs (entry->the_bfd); |
| 2725 | |
| 2726 | if (entry->flags.whole_archive) |
| 2727 | { |
| 2728 | bfd *member = NULL; |
| 2729 | bfd_boolean loaded = TRUE; |
| 2730 | |
| 2731 | for (;;) |
| 2732 | { |
| 2733 | bfd *subsbfd; |
| 2734 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
| 2735 | |
| 2736 | if (member == NULL) |
| 2737 | break; |
| 2738 | |
| 2739 | if (! bfd_check_format (member, bfd_object)) |
| 2740 | { |
| 2741 | einfo (_("%F%B: member %B in archive is not an object\n"), |
| 2742 | entry->the_bfd, member); |
| 2743 | loaded = FALSE; |
| 2744 | } |
| 2745 | |
| 2746 | subsbfd = member; |
| 2747 | if (!(*link_info.callbacks |
| 2748 | ->add_archive_element) (&link_info, member, |
| 2749 | "--whole-archive", &subsbfd)) |
| 2750 | abort (); |
| 2751 | |
| 2752 | /* Potentially, the add_archive_element hook may have set a |
| 2753 | substitute BFD for us. */ |
| 2754 | if (!bfd_link_add_symbols (subsbfd, &link_info)) |
| 2755 | { |
| 2756 | einfo (_("%F%B: error adding symbols: %E\n"), member); |
| 2757 | loaded = FALSE; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | entry->flags.loaded = loaded; |
| 2762 | return loaded; |
| 2763 | } |
| 2764 | break; |
| 2765 | } |
| 2766 | |
| 2767 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
| 2768 | entry->flags.loaded = TRUE; |
| 2769 | else |
| 2770 | einfo (_("%F%B: error adding symbols: %E\n"), entry->the_bfd); |
| 2771 | |
| 2772 | return entry->flags.loaded; |
| 2773 | } |
| 2774 | |
| 2775 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
| 2776 | may be NULL, indicating that it is a wildcard. Separate |
| 2777 | lang_input_section statements are created for each part of the |
| 2778 | expansion; they are added after the wild statement S. OUTPUT is |
| 2779 | the output section. */ |
| 2780 | |
| 2781 | static void |
| 2782 | wild (lang_wild_statement_type *s, |
| 2783 | const char *target ATTRIBUTE_UNUSED, |
| 2784 | lang_output_section_statement_type *output) |
| 2785 | { |
| 2786 | struct wildcard_list *sec; |
| 2787 | |
| 2788 | if (s->handler_data[0] |
| 2789 | && s->handler_data[0]->spec.sorted == by_name |
| 2790 | && !s->filenames_sorted) |
| 2791 | { |
| 2792 | lang_section_bst_type *tree; |
| 2793 | |
| 2794 | walk_wild (s, output_section_callback_fast, output); |
| 2795 | |
| 2796 | tree = s->tree; |
| 2797 | if (tree) |
| 2798 | { |
| 2799 | output_section_callback_tree_to_list (s, tree, output); |
| 2800 | s->tree = NULL; |
| 2801 | } |
| 2802 | } |
| 2803 | else |
| 2804 | walk_wild (s, output_section_callback, output); |
| 2805 | |
| 2806 | if (default_common_section == NULL) |
| 2807 | for (sec = s->section_list; sec != NULL; sec = sec->next) |
| 2808 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
| 2809 | { |
| 2810 | /* Remember the section that common is going to in case we |
| 2811 | later get something which doesn't know where to put it. */ |
| 2812 | default_common_section = output; |
| 2813 | break; |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /* Return TRUE iff target is the sought target. */ |
| 2818 | |
| 2819 | static int |
| 2820 | get_target (const bfd_target *target, void *data) |
| 2821 | { |
| 2822 | const char *sought = (const char *) data; |
| 2823 | |
| 2824 | return strcmp (target->name, sought) == 0; |
| 2825 | } |
| 2826 | |
| 2827 | /* Like strcpy() but convert to lower case as well. */ |
| 2828 | |
| 2829 | static void |
| 2830 | stricpy (char *dest, char *src) |
| 2831 | { |
| 2832 | char c; |
| 2833 | |
| 2834 | while ((c = *src++) != 0) |
| 2835 | *dest++ = TOLOWER (c); |
| 2836 | |
| 2837 | *dest = 0; |
| 2838 | } |
| 2839 | |
| 2840 | /* Remove the first occurrence of needle (if any) in haystack |
| 2841 | from haystack. */ |
| 2842 | |
| 2843 | static void |
| 2844 | strcut (char *haystack, char *needle) |
| 2845 | { |
| 2846 | haystack = strstr (haystack, needle); |
| 2847 | |
| 2848 | if (haystack) |
| 2849 | { |
| 2850 | char *src; |
| 2851 | |
| 2852 | for (src = haystack + strlen (needle); *src;) |
| 2853 | *haystack++ = *src++; |
| 2854 | |
| 2855 | *haystack = 0; |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | /* Compare two target format name strings. |
| 2860 | Return a value indicating how "similar" they are. */ |
| 2861 | |
| 2862 | static int |
| 2863 | name_compare (char *first, char *second) |
| 2864 | { |
| 2865 | char *copy1; |
| 2866 | char *copy2; |
| 2867 | int result; |
| 2868 | |
| 2869 | copy1 = (char *) xmalloc (strlen (first) + 1); |
| 2870 | copy2 = (char *) xmalloc (strlen (second) + 1); |
| 2871 | |
| 2872 | /* Convert the names to lower case. */ |
| 2873 | stricpy (copy1, first); |
| 2874 | stricpy (copy2, second); |
| 2875 | |
| 2876 | /* Remove size and endian strings from the name. */ |
| 2877 | strcut (copy1, "big"); |
| 2878 | strcut (copy1, "little"); |
| 2879 | strcut (copy2, "big"); |
| 2880 | strcut (copy2, "little"); |
| 2881 | |
| 2882 | /* Return a value based on how many characters match, |
| 2883 | starting from the beginning. If both strings are |
| 2884 | the same then return 10 * their length. */ |
| 2885 | for (result = 0; copy1[result] == copy2[result]; result++) |
| 2886 | if (copy1[result] == 0) |
| 2887 | { |
| 2888 | result *= 10; |
| 2889 | break; |
| 2890 | } |
| 2891 | |
| 2892 | free (copy1); |
| 2893 | free (copy2); |
| 2894 | |
| 2895 | return result; |
| 2896 | } |
| 2897 | |
| 2898 | /* Set by closest_target_match() below. */ |
| 2899 | static const bfd_target *winner; |
| 2900 | |
| 2901 | /* Scan all the valid bfd targets looking for one that has the endianness |
| 2902 | requirement that was specified on the command line, and is the nearest |
| 2903 | match to the original output target. */ |
| 2904 | |
| 2905 | static int |
| 2906 | closest_target_match (const bfd_target *target, void *data) |
| 2907 | { |
| 2908 | const bfd_target *original = (const bfd_target *) data; |
| 2909 | |
| 2910 | if (command_line.endian == ENDIAN_BIG |
| 2911 | && target->byteorder != BFD_ENDIAN_BIG) |
| 2912 | return 0; |
| 2913 | |
| 2914 | if (command_line.endian == ENDIAN_LITTLE |
| 2915 | && target->byteorder != BFD_ENDIAN_LITTLE) |
| 2916 | return 0; |
| 2917 | |
| 2918 | /* Must be the same flavour. */ |
| 2919 | if (target->flavour != original->flavour) |
| 2920 | return 0; |
| 2921 | |
| 2922 | /* Ignore generic big and little endian elf vectors. */ |
| 2923 | if (strcmp (target->name, "elf32-big") == 0 |
| 2924 | || strcmp (target->name, "elf64-big") == 0 |
| 2925 | || strcmp (target->name, "elf32-little") == 0 |
| 2926 | || strcmp (target->name, "elf64-little") == 0) |
| 2927 | return 0; |
| 2928 | |
| 2929 | /* If we have not found a potential winner yet, then record this one. */ |
| 2930 | if (winner == NULL) |
| 2931 | { |
| 2932 | winner = target; |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | /* Oh dear, we now have two potential candidates for a successful match. |
| 2937 | Compare their names and choose the better one. */ |
| 2938 | if (name_compare (target->name, original->name) |
| 2939 | > name_compare (winner->name, original->name)) |
| 2940 | winner = target; |
| 2941 | |
| 2942 | /* Keep on searching until wqe have checked them all. */ |
| 2943 | return 0; |
| 2944 | } |
| 2945 | |
| 2946 | /* Return the BFD target format of the first input file. */ |
| 2947 | |
| 2948 | static char * |
| 2949 | get_first_input_target (void) |
| 2950 | { |
| 2951 | char *target = NULL; |
| 2952 | |
| 2953 | LANG_FOR_EACH_INPUT_STATEMENT (s) |
| 2954 | { |
| 2955 | if (s->header.type == lang_input_statement_enum |
| 2956 | && s->flags.real) |
| 2957 | { |
| 2958 | ldfile_open_file (s); |
| 2959 | |
| 2960 | if (s->the_bfd != NULL |
| 2961 | && bfd_check_format (s->the_bfd, bfd_object)) |
| 2962 | { |
| 2963 | target = bfd_get_target (s->the_bfd); |
| 2964 | |
| 2965 | if (target != NULL) |
| 2966 | break; |
| 2967 | } |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | return target; |
| 2972 | } |
| 2973 | |
| 2974 | const char * |
| 2975 | lang_get_output_target (void) |
| 2976 | { |
| 2977 | const char *target; |
| 2978 | |
| 2979 | /* Has the user told us which output format to use? */ |
| 2980 | if (output_target != NULL) |
| 2981 | return output_target; |
| 2982 | |
| 2983 | /* No - has the current target been set to something other than |
| 2984 | the default? */ |
| 2985 | if (current_target != default_target && current_target != NULL) |
| 2986 | return current_target; |
| 2987 | |
| 2988 | /* No - can we determine the format of the first input file? */ |
| 2989 | target = get_first_input_target (); |
| 2990 | if (target != NULL) |
| 2991 | return target; |
| 2992 | |
| 2993 | /* Failed - use the default output target. */ |
| 2994 | return default_target; |
| 2995 | } |
| 2996 | |
| 2997 | /* Open the output file. */ |
| 2998 | |
| 2999 | static void |
| 3000 | open_output (const char *name) |
| 3001 | { |
| 3002 | output_target = lang_get_output_target (); |
| 3003 | |
| 3004 | /* Has the user requested a particular endianness on the command |
| 3005 | line? */ |
| 3006 | if (command_line.endian != ENDIAN_UNSET) |
| 3007 | { |
| 3008 | const bfd_target *target; |
| 3009 | enum bfd_endian desired_endian; |
| 3010 | |
| 3011 | /* Get the chosen target. */ |
| 3012 | target = bfd_search_for_target (get_target, (void *) output_target); |
| 3013 | |
| 3014 | /* If the target is not supported, we cannot do anything. */ |
| 3015 | if (target != NULL) |
| 3016 | { |
| 3017 | if (command_line.endian == ENDIAN_BIG) |
| 3018 | desired_endian = BFD_ENDIAN_BIG; |
| 3019 | else |
| 3020 | desired_endian = BFD_ENDIAN_LITTLE; |
| 3021 | |
| 3022 | /* See if the target has the wrong endianness. This should |
| 3023 | not happen if the linker script has provided big and |
| 3024 | little endian alternatives, but some scrips don't do |
| 3025 | this. */ |
| 3026 | if (target->byteorder != desired_endian) |
| 3027 | { |
| 3028 | /* If it does, then see if the target provides |
| 3029 | an alternative with the correct endianness. */ |
| 3030 | if (target->alternative_target != NULL |
| 3031 | && (target->alternative_target->byteorder == desired_endian)) |
| 3032 | output_target = target->alternative_target->name; |
| 3033 | else |
| 3034 | { |
| 3035 | /* Try to find a target as similar as possible to |
| 3036 | the default target, but which has the desired |
| 3037 | endian characteristic. */ |
| 3038 | bfd_search_for_target (closest_target_match, |
| 3039 | (void *) target); |
| 3040 | |
| 3041 | /* Oh dear - we could not find any targets that |
| 3042 | satisfy our requirements. */ |
| 3043 | if (winner == NULL) |
| 3044 | einfo (_("%P: warning: could not find any targets" |
| 3045 | " that match endianness requirement\n")); |
| 3046 | else |
| 3047 | output_target = winner->name; |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | link_info.output_bfd = bfd_openw (name, output_target); |
| 3054 | |
| 3055 | if (link_info.output_bfd == NULL) |
| 3056 | { |
| 3057 | if (bfd_get_error () == bfd_error_invalid_target) |
| 3058 | einfo (_("%P%F: target %s not found\n"), output_target); |
| 3059 | |
| 3060 | einfo (_("%P%F: cannot open output file %s: %E\n"), name); |
| 3061 | } |
| 3062 | |
| 3063 | delete_output_file_on_failure = TRUE; |
| 3064 | |
| 3065 | if (! bfd_set_format (link_info.output_bfd, bfd_object)) |
| 3066 | einfo (_("%P%F:%s: can not make object file: %E\n"), name); |
| 3067 | if (! bfd_set_arch_mach (link_info.output_bfd, |
| 3068 | ldfile_output_architecture, |
| 3069 | ldfile_output_machine)) |
| 3070 | einfo (_("%P%F:%s: can not set architecture: %E\n"), name); |
| 3071 | |
| 3072 | link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); |
| 3073 | if (link_info.hash == NULL) |
| 3074 | einfo (_("%P%F: can not create hash table: %E\n")); |
| 3075 | |
| 3076 | bfd_set_gp_size (link_info.output_bfd, g_switch_value); |
| 3077 | } |
| 3078 | |
| 3079 | static void |
| 3080 | ldlang_open_output (lang_statement_union_type *statement) |
| 3081 | { |
| 3082 | switch (statement->header.type) |
| 3083 | { |
| 3084 | case lang_output_statement_enum: |
| 3085 | ASSERT (link_info.output_bfd == NULL); |
| 3086 | open_output (statement->output_statement.name); |
| 3087 | ldemul_set_output_arch (); |
| 3088 | if (config.magic_demand_paged && !link_info.relocatable) |
| 3089 | link_info.output_bfd->flags |= D_PAGED; |
| 3090 | else |
| 3091 | link_info.output_bfd->flags &= ~D_PAGED; |
| 3092 | if (config.text_read_only) |
| 3093 | link_info.output_bfd->flags |= WP_TEXT; |
| 3094 | else |
| 3095 | link_info.output_bfd->flags &= ~WP_TEXT; |
| 3096 | if (link_info.traditional_format) |
| 3097 | link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
| 3098 | else |
| 3099 | link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
| 3100 | break; |
| 3101 | |
| 3102 | case lang_target_statement_enum: |
| 3103 | current_target = statement->target_statement.target; |
| 3104 | break; |
| 3105 | default: |
| 3106 | break; |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | /* Convert between addresses in bytes and sizes in octets. |
| 3111 | For currently supported targets, octets_per_byte is always a power |
| 3112 | of two, so we can use shifts. */ |
| 3113 | #define TO_ADDR(X) ((X) >> opb_shift) |
| 3114 | #define TO_SIZE(X) ((X) << opb_shift) |
| 3115 | |
| 3116 | /* Support the above. */ |
| 3117 | static unsigned int opb_shift = 0; |
| 3118 | |
| 3119 | static void |
| 3120 | init_opb (void) |
| 3121 | { |
| 3122 | unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 3123 | ldfile_output_machine); |
| 3124 | opb_shift = 0; |
| 3125 | if (x > 1) |
| 3126 | while ((x & 1) == 0) |
| 3127 | { |
| 3128 | x >>= 1; |
| 3129 | ++opb_shift; |
| 3130 | } |
| 3131 | ASSERT (x == 1); |
| 3132 | } |
| 3133 | |
| 3134 | /* Open all the input files. */ |
| 3135 | |
| 3136 | enum open_bfd_mode |
| 3137 | { |
| 3138 | OPEN_BFD_NORMAL = 0, |
| 3139 | OPEN_BFD_FORCE = 1, |
| 3140 | OPEN_BFD_RESCAN = 2 |
| 3141 | }; |
| 3142 | #ifdef ENABLE_PLUGINS |
| 3143 | static lang_input_statement_type *plugin_insert = NULL; |
| 3144 | #endif |
| 3145 | |
| 3146 | static void |
| 3147 | open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode) |
| 3148 | { |
| 3149 | for (; s != NULL; s = s->header.next) |
| 3150 | { |
| 3151 | switch (s->header.type) |
| 3152 | { |
| 3153 | case lang_constructors_statement_enum: |
| 3154 | open_input_bfds (constructor_list.head, mode); |
| 3155 | break; |
| 3156 | case lang_output_section_statement_enum: |
| 3157 | open_input_bfds (s->output_section_statement.children.head, mode); |
| 3158 | break; |
| 3159 | case lang_wild_statement_enum: |
| 3160 | /* Maybe we should load the file's symbols. */ |
| 3161 | if ((mode & OPEN_BFD_RESCAN) == 0 |
| 3162 | && s->wild_statement.filename |
| 3163 | && !wildcardp (s->wild_statement.filename) |
| 3164 | && !archive_path (s->wild_statement.filename)) |
| 3165 | lookup_name (s->wild_statement.filename); |
| 3166 | open_input_bfds (s->wild_statement.children.head, mode); |
| 3167 | break; |
| 3168 | case lang_group_statement_enum: |
| 3169 | { |
| 3170 | struct bfd_link_hash_entry *undefs; |
| 3171 | |
| 3172 | /* We must continually search the entries in the group |
| 3173 | until no new symbols are added to the list of undefined |
| 3174 | symbols. */ |
| 3175 | |
| 3176 | do |
| 3177 | { |
| 3178 | undefs = link_info.hash->undefs_tail; |
| 3179 | open_input_bfds (s->group_statement.children.head, |
| 3180 | mode | OPEN_BFD_FORCE); |
| 3181 | } |
| 3182 | while (undefs != link_info.hash->undefs_tail); |
| 3183 | } |
| 3184 | break; |
| 3185 | case lang_target_statement_enum: |
| 3186 | current_target = s->target_statement.target; |
| 3187 | break; |
| 3188 | case lang_input_statement_enum: |
| 3189 | if (s->input_statement.flags.real) |
| 3190 | { |
| 3191 | lang_statement_union_type **os_tail; |
| 3192 | lang_statement_list_type add; |
| 3193 | |
| 3194 | s->input_statement.target = current_target; |
| 3195 | |
| 3196 | /* If we are being called from within a group, and this |
| 3197 | is an archive which has already been searched, then |
| 3198 | force it to be researched unless the whole archive |
| 3199 | has been loaded already. Do the same for a rescan. */ |
| 3200 | if (mode != OPEN_BFD_NORMAL |
| 3201 | #ifdef ENABLE_PLUGINS |
| 3202 | && ((mode & OPEN_BFD_RESCAN) == 0 |
| 3203 | || plugin_insert == NULL) |
| 3204 | #endif |
| 3205 | && !s->input_statement.flags.whole_archive |
| 3206 | && s->input_statement.flags.loaded |
| 3207 | && s->input_statement.the_bfd != NULL |
| 3208 | && bfd_check_format (s->input_statement.the_bfd, |
| 3209 | bfd_archive)) |
| 3210 | s->input_statement.flags.loaded = FALSE; |
| 3211 | #ifdef ENABLE_PLUGINS |
| 3212 | /* When rescanning, reload --as-needed shared libs. */ |
| 3213 | else if ((mode & OPEN_BFD_RESCAN) != 0 |
| 3214 | && plugin_insert == NULL |
| 3215 | && s->input_statement.flags.loaded |
| 3216 | && s->input_statement.flags.add_DT_NEEDED_for_regular |
| 3217 | && s->input_statement.the_bfd != NULL |
| 3218 | && ((s->input_statement.the_bfd->flags) & DYNAMIC) != 0 |
| 3219 | && plugin_should_reload (s->input_statement.the_bfd)) |
| 3220 | { |
| 3221 | s->input_statement.flags.loaded = FALSE; |
| 3222 | s->input_statement.flags.reload = TRUE; |
| 3223 | } |
| 3224 | #endif |
| 3225 | |
| 3226 | os_tail = lang_output_section_statement.tail; |
| 3227 | lang_list_init (&add); |
| 3228 | |
| 3229 | if (! load_symbols (&s->input_statement, &add)) |
| 3230 | config.make_executable = FALSE; |
| 3231 | |
| 3232 | if (add.head != NULL) |
| 3233 | { |
| 3234 | /* If this was a script with output sections then |
| 3235 | tack any added statements on to the end of the |
| 3236 | list. This avoids having to reorder the output |
| 3237 | section statement list. Very likely the user |
| 3238 | forgot -T, and whatever we do here will not meet |
| 3239 | naive user expectations. */ |
| 3240 | if (os_tail != lang_output_section_statement.tail) |
| 3241 | { |
| 3242 | einfo (_("%P: warning: %s contains output sections;" |
| 3243 | " did you forget -T?\n"), |
| 3244 | s->input_statement.filename); |
| 3245 | *stat_ptr->tail = add.head; |
| 3246 | stat_ptr->tail = add.tail; |
| 3247 | } |
| 3248 | else |
| 3249 | { |
| 3250 | *add.tail = s->header.next; |
| 3251 | s->header.next = add.head; |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | #ifdef ENABLE_PLUGINS |
| 3256 | /* If we have found the point at which a plugin added new |
| 3257 | files, clear plugin_insert to enable archive rescan. */ |
| 3258 | if (&s->input_statement == plugin_insert) |
| 3259 | plugin_insert = NULL; |
| 3260 | #endif |
| 3261 | break; |
| 3262 | case lang_assignment_statement_enum: |
| 3263 | if (s->assignment_statement.exp->assign.defsym) |
| 3264 | /* This is from a --defsym on the command line. */ |
| 3265 | exp_fold_tree_no_dot (s->assignment_statement.exp); |
| 3266 | break; |
| 3267 | default: |
| 3268 | break; |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | /* Exit if any of the files were missing. */ |
| 3273 | if (input_flags.missing_file) |
| 3274 | einfo ("%F"); |
| 3275 | } |
| 3276 | |
| 3277 | /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */ |
| 3278 | |
| 3279 | void |
| 3280 | lang_track_definedness (const char *name) |
| 3281 | { |
| 3282 | if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL) |
| 3283 | einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name); |
| 3284 | } |
| 3285 | |
| 3286 | /* New-function for the definedness hash table. */ |
| 3287 | |
| 3288 | static struct bfd_hash_entry * |
| 3289 | lang_definedness_newfunc (struct bfd_hash_entry *entry, |
| 3290 | struct bfd_hash_table *table ATTRIBUTE_UNUSED, |
| 3291 | const char *name ATTRIBUTE_UNUSED) |
| 3292 | { |
| 3293 | struct lang_definedness_hash_entry *ret |
| 3294 | = (struct lang_definedness_hash_entry *) entry; |
| 3295 | |
| 3296 | if (ret == NULL) |
| 3297 | ret = (struct lang_definedness_hash_entry *) |
| 3298 | bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry)); |
| 3299 | |
| 3300 | if (ret == NULL) |
| 3301 | einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name); |
| 3302 | |
| 3303 | ret->iteration = -1; |
| 3304 | return &ret->root; |
| 3305 | } |
| 3306 | |
| 3307 | /* Return the iteration when the definition of NAME was last updated. A |
| 3308 | value of -1 means that the symbol is not defined in the linker script |
| 3309 | or the command line, but may be defined in the linker symbol table. */ |
| 3310 | |
| 3311 | int |
| 3312 | lang_symbol_definition_iteration (const char *name) |
| 3313 | { |
| 3314 | struct lang_definedness_hash_entry *defentry |
| 3315 | = (struct lang_definedness_hash_entry *) |
| 3316 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); |
| 3317 | |
| 3318 | /* We've already created this one on the presence of DEFINED in the |
| 3319 | script, so it can't be NULL unless something is borked elsewhere in |
| 3320 | the code. */ |
| 3321 | if (defentry == NULL) |
| 3322 | FAIL (); |
| 3323 | |
| 3324 | return defentry->iteration; |
| 3325 | } |
| 3326 | |
| 3327 | /* Update the definedness state of NAME. */ |
| 3328 | |
| 3329 | void |
| 3330 | lang_update_definedness (const char *name, struct bfd_link_hash_entry *h) |
| 3331 | { |
| 3332 | struct lang_definedness_hash_entry *defentry |
| 3333 | = (struct lang_definedness_hash_entry *) |
| 3334 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); |
| 3335 | |
| 3336 | /* We don't keep track of symbols not tested with DEFINED. */ |
| 3337 | if (defentry == NULL) |
| 3338 | return; |
| 3339 | |
| 3340 | /* If the symbol was already defined, and not from an earlier statement |
| 3341 | iteration, don't update the definedness iteration, because that'd |
| 3342 | make the symbol seem defined in the linker script at this point, and |
| 3343 | it wasn't; it was defined in some object. If we do anyway, DEFINED |
| 3344 | would start to yield false before this point and the construct "sym = |
| 3345 | DEFINED (sym) ? sym : X;" would change sym to X despite being defined |
| 3346 | in an object. */ |
| 3347 | if (h->type != bfd_link_hash_undefined |
| 3348 | && h->type != bfd_link_hash_common |
| 3349 | && h->type != bfd_link_hash_new |
| 3350 | && defentry->iteration == -1) |
| 3351 | return; |
| 3352 | |
| 3353 | defentry->iteration = lang_statement_iteration; |
| 3354 | } |
| 3355 | |
| 3356 | /* Add the supplied name to the symbol table as an undefined reference. |
| 3357 | This is a two step process as the symbol table doesn't even exist at |
| 3358 | the time the ld command line is processed. First we put the name |
| 3359 | on a list, then, once the output file has been opened, transfer the |
| 3360 | name to the symbol table. */ |
| 3361 | |
| 3362 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
| 3363 | |
| 3364 | #define ldlang_undef_chain_list_head entry_symbol.next |
| 3365 | |
| 3366 | void |
| 3367 | ldlang_add_undef (const char *const name, bfd_boolean cmdline) |
| 3368 | { |
| 3369 | ldlang_undef_chain_list_type *new_undef; |
| 3370 | |
| 3371 | undef_from_cmdline = undef_from_cmdline || cmdline; |
| 3372 | new_undef = (ldlang_undef_chain_list_type *) stat_alloc (sizeof (*new_undef)); |
| 3373 | new_undef->next = ldlang_undef_chain_list_head; |
| 3374 | ldlang_undef_chain_list_head = new_undef; |
| 3375 | |
| 3376 | new_undef->name = xstrdup (name); |
| 3377 | |
| 3378 | if (link_info.output_bfd != NULL) |
| 3379 | insert_undefined (new_undef->name); |
| 3380 | } |
| 3381 | |
| 3382 | /* Insert NAME as undefined in the symbol table. */ |
| 3383 | |
| 3384 | static void |
| 3385 | insert_undefined (const char *name) |
| 3386 | { |
| 3387 | struct bfd_link_hash_entry *h; |
| 3388 | |
| 3389 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
| 3390 | if (h == NULL) |
| 3391 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
| 3392 | if (h->type == bfd_link_hash_new) |
| 3393 | { |
| 3394 | h->type = bfd_link_hash_undefined; |
| 3395 | h->u.undef.abfd = NULL; |
| 3396 | bfd_link_add_undef (link_info.hash, h); |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | /* Run through the list of undefineds created above and place them |
| 3401 | into the linker hash table as undefined symbols belonging to the |
| 3402 | script file. */ |
| 3403 | |
| 3404 | static void |
| 3405 | lang_place_undefineds (void) |
| 3406 | { |
| 3407 | ldlang_undef_chain_list_type *ptr; |
| 3408 | |
| 3409 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
| 3410 | insert_undefined (ptr->name); |
| 3411 | } |
| 3412 | |
| 3413 | /* Check for all readonly or some readwrite sections. */ |
| 3414 | |
| 3415 | static void |
| 3416 | check_input_sections |
| 3417 | (lang_statement_union_type *s, |
| 3418 | lang_output_section_statement_type *output_section_statement) |
| 3419 | { |
| 3420 | for (; s != (lang_statement_union_type *) NULL; s = s->header.next) |
| 3421 | { |
| 3422 | switch (s->header.type) |
| 3423 | { |
| 3424 | case lang_wild_statement_enum: |
| 3425 | walk_wild (&s->wild_statement, check_section_callback, |
| 3426 | output_section_statement); |
| 3427 | if (! output_section_statement->all_input_readonly) |
| 3428 | return; |
| 3429 | break; |
| 3430 | case lang_constructors_statement_enum: |
| 3431 | check_input_sections (constructor_list.head, |
| 3432 | output_section_statement); |
| 3433 | if (! output_section_statement->all_input_readonly) |
| 3434 | return; |
| 3435 | break; |
| 3436 | case lang_group_statement_enum: |
| 3437 | check_input_sections (s->group_statement.children.head, |
| 3438 | output_section_statement); |
| 3439 | if (! output_section_statement->all_input_readonly) |
| 3440 | return; |
| 3441 | break; |
| 3442 | default: |
| 3443 | break; |
| 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | /* Update wildcard statements if needed. */ |
| 3449 | |
| 3450 | static void |
| 3451 | update_wild_statements (lang_statement_union_type *s) |
| 3452 | { |
| 3453 | struct wildcard_list *sec; |
| 3454 | |
| 3455 | switch (sort_section) |
| 3456 | { |
| 3457 | default: |
| 3458 | FAIL (); |
| 3459 | |
| 3460 | case none: |
| 3461 | break; |
| 3462 | |
| 3463 | case by_name: |
| 3464 | case by_alignment: |
| 3465 | for (; s != NULL; s = s->header.next) |
| 3466 | { |
| 3467 | switch (s->header.type) |
| 3468 | { |
| 3469 | default: |
| 3470 | break; |
| 3471 | |
| 3472 | case lang_wild_statement_enum: |
| 3473 | for (sec = s->wild_statement.section_list; sec != NULL; |
| 3474 | sec = sec->next) |
| 3475 | { |
| 3476 | switch (sec->spec.sorted) |
| 3477 | { |
| 3478 | case none: |
| 3479 | sec->spec.sorted = sort_section; |
| 3480 | break; |
| 3481 | case by_name: |
| 3482 | if (sort_section == by_alignment) |
| 3483 | sec->spec.sorted = by_name_alignment; |
| 3484 | break; |
| 3485 | case by_alignment: |
| 3486 | if (sort_section == by_name) |
| 3487 | sec->spec.sorted = by_alignment_name; |
| 3488 | break; |
| 3489 | default: |
| 3490 | break; |
| 3491 | } |
| 3492 | } |
| 3493 | break; |
| 3494 | |
| 3495 | case lang_constructors_statement_enum: |
| 3496 | update_wild_statements (constructor_list.head); |
| 3497 | break; |
| 3498 | |
| 3499 | case lang_output_section_statement_enum: |
| 3500 | /* Don't sort .init/.fini sections. */ |
| 3501 | if (strcmp (s->output_section_statement.name, ".init") != 0 |
| 3502 | && strcmp (s->output_section_statement.name, ".fini") != 0) |
| 3503 | update_wild_statements |
| 3504 | (s->output_section_statement.children.head); |
| 3505 | break; |
| 3506 | |
| 3507 | case lang_group_statement_enum: |
| 3508 | update_wild_statements (s->group_statement.children.head); |
| 3509 | break; |
| 3510 | } |
| 3511 | } |
| 3512 | break; |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | /* Open input files and attach to output sections. */ |
| 3517 | |
| 3518 | static void |
| 3519 | map_input_to_output_sections |
| 3520 | (lang_statement_union_type *s, const char *target, |
| 3521 | lang_output_section_statement_type *os) |
| 3522 | { |
| 3523 | for (; s != NULL; s = s->header.next) |
| 3524 | { |
| 3525 | lang_output_section_statement_type *tos; |
| 3526 | flagword flags; |
| 3527 | |
| 3528 | switch (s->header.type) |
| 3529 | { |
| 3530 | case lang_wild_statement_enum: |
| 3531 | wild (&s->wild_statement, target, os); |
| 3532 | break; |
| 3533 | case lang_constructors_statement_enum: |
| 3534 | map_input_to_output_sections (constructor_list.head, |
| 3535 | target, |
| 3536 | os); |
| 3537 | break; |
| 3538 | case lang_output_section_statement_enum: |
| 3539 | tos = &s->output_section_statement; |
| 3540 | if (tos->constraint != 0) |
| 3541 | { |
| 3542 | if (tos->constraint != ONLY_IF_RW |
| 3543 | && tos->constraint != ONLY_IF_RO) |
| 3544 | break; |
| 3545 | tos->all_input_readonly = TRUE; |
| 3546 | check_input_sections (tos->children.head, tos); |
| 3547 | if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) |
| 3548 | { |
| 3549 | tos->constraint = -1; |
| 3550 | break; |
| 3551 | } |
| 3552 | } |
| 3553 | map_input_to_output_sections (tos->children.head, |
| 3554 | target, |
| 3555 | tos); |
| 3556 | break; |
| 3557 | case lang_output_statement_enum: |
| 3558 | break; |
| 3559 | case lang_target_statement_enum: |
| 3560 | target = s->target_statement.target; |
| 3561 | break; |
| 3562 | case lang_group_statement_enum: |
| 3563 | map_input_to_output_sections (s->group_statement.children.head, |
| 3564 | target, |
| 3565 | os); |
| 3566 | break; |
| 3567 | case lang_data_statement_enum: |
| 3568 | /* Make sure that any sections mentioned in the expression |
| 3569 | are initialized. */ |
| 3570 | exp_init_os (s->data_statement.exp); |
| 3571 | /* The output section gets CONTENTS, ALLOC and LOAD, but |
| 3572 | these may be overridden by the script. */ |
| 3573 | flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; |
| 3574 | switch (os->sectype) |
| 3575 | { |
| 3576 | case normal_section: |
| 3577 | case overlay_section: |
| 3578 | break; |
| 3579 | case noalloc_section: |
| 3580 | flags = SEC_HAS_CONTENTS; |
| 3581 | break; |
| 3582 | case noload_section: |
| 3583 | if (bfd_get_flavour (link_info.output_bfd) |
| 3584 | == bfd_target_elf_flavour) |
| 3585 | flags = SEC_NEVER_LOAD | SEC_ALLOC; |
| 3586 | else |
| 3587 | flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; |
| 3588 | break; |
| 3589 | } |
| 3590 | if (os->bfd_section == NULL) |
| 3591 | init_os (os, flags); |
| 3592 | else |
| 3593 | os->bfd_section->flags |= flags; |
| 3594 | break; |
| 3595 | case lang_input_section_enum: |
| 3596 | break; |
| 3597 | case lang_fill_statement_enum: |
| 3598 | case lang_object_symbols_statement_enum: |
| 3599 | case lang_reloc_statement_enum: |
| 3600 | case lang_padding_statement_enum: |
| 3601 | case lang_input_statement_enum: |
| 3602 | if (os != NULL && os->bfd_section == NULL) |
| 3603 | init_os (os, 0); |
| 3604 | break; |
| 3605 | case lang_assignment_statement_enum: |
| 3606 | if (os != NULL && os->bfd_section == NULL) |
| 3607 | init_os (os, 0); |
| 3608 | |
| 3609 | /* Make sure that any sections mentioned in the assignment |
| 3610 | are initialized. */ |
| 3611 | exp_init_os (s->assignment_statement.exp); |
| 3612 | break; |
| 3613 | case lang_address_statement_enum: |
| 3614 | /* Mark the specified section with the supplied address. |
| 3615 | If this section was actually a segment marker, then the |
| 3616 | directive is ignored if the linker script explicitly |
| 3617 | processed the segment marker. Originally, the linker |
| 3618 | treated segment directives (like -Ttext on the |
| 3619 | command-line) as section directives. We honor the |
| 3620 | section directive semantics for backwards compatibilty; |
| 3621 | linker scripts that do not specifically check for |
| 3622 | SEGMENT_START automatically get the old semantics. */ |
| 3623 | if (!s->address_statement.segment |
| 3624 | || !s->address_statement.segment->used) |
| 3625 | { |
| 3626 | const char *name = s->address_statement.section_name; |
| 3627 | |
| 3628 | /* Create the output section statement here so that |
| 3629 | orphans with a set address will be placed after other |
| 3630 | script sections. If we let the orphan placement code |
| 3631 | place them in amongst other sections then the address |
| 3632 | will affect following script sections, which is |
| 3633 | likely to surprise naive users. */ |
| 3634 | tos = lang_output_section_statement_lookup (name, 0, TRUE); |
| 3635 | tos->addr_tree = s->address_statement.address; |
| 3636 | if (tos->bfd_section == NULL) |
| 3637 | init_os (tos, 0); |
| 3638 | } |
| 3639 | break; |
| 3640 | case lang_insert_statement_enum: |
| 3641 | break; |
| 3642 | } |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | /* An insert statement snips out all the linker statements from the |
| 3647 | start of the list and places them after the output section |
| 3648 | statement specified by the insert. This operation is complicated |
| 3649 | by the fact that we keep a doubly linked list of output section |
| 3650 | statements as well as the singly linked list of all statements. */ |
| 3651 | |
| 3652 | static void |
| 3653 | process_insert_statements (void) |
| 3654 | { |
| 3655 | lang_statement_union_type **s; |
| 3656 | lang_output_section_statement_type *first_os = NULL; |
| 3657 | lang_output_section_statement_type *last_os = NULL; |
| 3658 | lang_output_section_statement_type *os; |
| 3659 | |
| 3660 | /* "start of list" is actually the statement immediately after |
| 3661 | the special abs_section output statement, so that it isn't |
| 3662 | reordered. */ |
| 3663 | s = &lang_output_section_statement.head; |
| 3664 | while (*(s = &(*s)->header.next) != NULL) |
| 3665 | { |
| 3666 | if ((*s)->header.type == lang_output_section_statement_enum) |
| 3667 | { |
| 3668 | /* Keep pointers to the first and last output section |
| 3669 | statement in the sequence we may be about to move. */ |
| 3670 | os = &(*s)->output_section_statement; |
| 3671 | |
| 3672 | ASSERT (last_os == NULL || last_os->next == os); |
| 3673 | last_os = os; |
| 3674 | |
| 3675 | /* Set constraint negative so that lang_output_section_find |
| 3676 | won't match this output section statement. At this |
| 3677 | stage in linking constraint has values in the range |
| 3678 | [-1, ONLY_IN_RW]. */ |
| 3679 | last_os->constraint = -2 - last_os->constraint; |
| 3680 | if (first_os == NULL) |
| 3681 | first_os = last_os; |
| 3682 | } |
| 3683 | else if ((*s)->header.type == lang_insert_statement_enum) |
| 3684 | { |
| 3685 | lang_insert_statement_type *i = &(*s)->insert_statement; |
| 3686 | lang_output_section_statement_type *where; |
| 3687 | lang_statement_union_type **ptr; |
| 3688 | lang_statement_union_type *first; |
| 3689 | |
| 3690 | where = lang_output_section_find (i->where); |
| 3691 | if (where != NULL && i->is_before) |
| 3692 | { |
| 3693 | do |
| 3694 | where = where->prev; |
| 3695 | while (where != NULL && where->constraint < 0); |
| 3696 | } |
| 3697 | if (where == NULL) |
| 3698 | { |
| 3699 | einfo (_("%F%P: %s not found for insert\n"), i->where); |
| 3700 | return; |
| 3701 | } |
| 3702 | |
| 3703 | /* Deal with reordering the output section statement list. */ |
| 3704 | if (last_os != NULL) |
| 3705 | { |
| 3706 | asection *first_sec, *last_sec; |
| 3707 | struct lang_output_section_statement_struct **next; |
| 3708 | |
| 3709 | /* Snip out the output sections we are moving. */ |
| 3710 | first_os->prev->next = last_os->next; |
| 3711 | if (last_os->next == NULL) |
| 3712 | { |
| 3713 | next = &first_os->prev->next; |
| 3714 | lang_output_section_statement.tail |
| 3715 | = (lang_statement_union_type **) next; |
| 3716 | } |
| 3717 | else |
| 3718 | last_os->next->prev = first_os->prev; |
| 3719 | /* Add them in at the new position. */ |
| 3720 | last_os->next = where->next; |
| 3721 | if (where->next == NULL) |
| 3722 | { |
| 3723 | next = &last_os->next; |
| 3724 | lang_output_section_statement.tail |
| 3725 | = (lang_statement_union_type **) next; |
| 3726 | } |
| 3727 | else |
| 3728 | where->next->prev = last_os; |
| 3729 | first_os->prev = where; |
| 3730 | where->next = first_os; |
| 3731 | |
| 3732 | /* Move the bfd sections in the same way. */ |
| 3733 | first_sec = NULL; |
| 3734 | last_sec = NULL; |
| 3735 | for (os = first_os; os != NULL; os = os->next) |
| 3736 | { |
| 3737 | os->constraint = -2 - os->constraint; |
| 3738 | if (os->bfd_section != NULL |
| 3739 | && os->bfd_section->owner != NULL) |
| 3740 | { |
| 3741 | last_sec = os->bfd_section; |
| 3742 | if (first_sec == NULL) |
| 3743 | first_sec = last_sec; |
| 3744 | } |
| 3745 | if (os == last_os) |
| 3746 | break; |
| 3747 | } |
| 3748 | if (last_sec != NULL) |
| 3749 | { |
| 3750 | asection *sec = where->bfd_section; |
| 3751 | if (sec == NULL) |
| 3752 | sec = output_prev_sec_find (where); |
| 3753 | |
| 3754 | /* The place we want to insert must come after the |
| 3755 | sections we are moving. So if we find no |
| 3756 | section or if the section is the same as our |
| 3757 | last section, then no move is needed. */ |
| 3758 | if (sec != NULL && sec != last_sec) |
| 3759 | { |
| 3760 | /* Trim them off. */ |
| 3761 | if (first_sec->prev != NULL) |
| 3762 | first_sec->prev->next = last_sec->next; |
| 3763 | else |
| 3764 | link_info.output_bfd->sections = last_sec->next; |
| 3765 | if (last_sec->next != NULL) |
| 3766 | last_sec->next->prev = first_sec->prev; |
| 3767 | else |
| 3768 | link_info.output_bfd->section_last = first_sec->prev; |
| 3769 | /* Add back. */ |
| 3770 | last_sec->next = sec->next; |
| 3771 | if (sec->next != NULL) |
| 3772 | sec->next->prev = last_sec; |
| 3773 | else |
| 3774 | link_info.output_bfd->section_last = last_sec; |
| 3775 | first_sec->prev = sec; |
| 3776 | sec->next = first_sec; |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | first_os = NULL; |
| 3781 | last_os = NULL; |
| 3782 | } |
| 3783 | |
| 3784 | ptr = insert_os_after (where); |
| 3785 | /* Snip everything after the abs_section output statement we |
| 3786 | know is at the start of the list, up to and including |
| 3787 | the insert statement we are currently processing. */ |
| 3788 | first = lang_output_section_statement.head->header.next; |
| 3789 | lang_output_section_statement.head->header.next = (*s)->header.next; |
| 3790 | /* Add them back where they belong. */ |
| 3791 | *s = *ptr; |
| 3792 | if (*s == NULL) |
| 3793 | statement_list.tail = s; |
| 3794 | *ptr = first; |
| 3795 | s = &lang_output_section_statement.head; |
| 3796 | } |
| 3797 | } |
| 3798 | |
| 3799 | /* Undo constraint twiddling. */ |
| 3800 | for (os = first_os; os != NULL; os = os->next) |
| 3801 | { |
| 3802 | os->constraint = -2 - os->constraint; |
| 3803 | if (os == last_os) |
| 3804 | break; |
| 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | /* An output section might have been removed after its statement was |
| 3809 | added. For example, ldemul_before_allocation can remove dynamic |
| 3810 | sections if they turn out to be not needed. Clean them up here. */ |
| 3811 | |
| 3812 | void |
| 3813 | strip_excluded_output_sections (void) |
| 3814 | { |
| 3815 | lang_output_section_statement_type *os; |
| 3816 | |
| 3817 | /* Run lang_size_sections (if not already done). */ |
| 3818 | if (expld.phase != lang_mark_phase_enum) |
| 3819 | { |
| 3820 | expld.phase = lang_mark_phase_enum; |
| 3821 | expld.dataseg.phase = exp_dataseg_none; |
| 3822 | one_lang_size_sections_pass (NULL, FALSE); |
| 3823 | lang_reset_memory_regions (); |
| 3824 | } |
| 3825 | |
| 3826 | for (os = &lang_output_section_statement.head->output_section_statement; |
| 3827 | os != NULL; |
| 3828 | os = os->next) |
| 3829 | { |
| 3830 | asection *output_section; |
| 3831 | bfd_boolean exclude; |
| 3832 | |
| 3833 | if (os->constraint < 0) |
| 3834 | continue; |
| 3835 | |
| 3836 | output_section = os->bfd_section; |
| 3837 | if (output_section == NULL) |
| 3838 | continue; |
| 3839 | |
| 3840 | exclude = (output_section->rawsize == 0 |
| 3841 | && (output_section->flags & SEC_KEEP) == 0 |
| 3842 | && !bfd_section_removed_from_list (link_info.output_bfd, |
| 3843 | output_section)); |
| 3844 | |
| 3845 | /* Some sections have not yet been sized, notably .gnu.version, |
| 3846 | .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED |
| 3847 | input sections, so don't drop output sections that have such |
| 3848 | input sections unless they are also marked SEC_EXCLUDE. */ |
| 3849 | if (exclude && output_section->map_head.s != NULL) |
| 3850 | { |
| 3851 | asection *s; |
| 3852 | |
| 3853 | for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) |
| 3854 | if ((s->flags & SEC_EXCLUDE) == 0 |
| 3855 | && ((s->flags & SEC_LINKER_CREATED) != 0 |
| 3856 | || link_info.emitrelocations)) |
| 3857 | { |
| 3858 | exclude = FALSE; |
| 3859 | break; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | /* TODO: Don't just junk map_head.s, turn them into link_orders. */ |
| 3864 | output_section->map_head.link_order = NULL; |
| 3865 | output_section->map_tail.link_order = NULL; |
| 3866 | |
| 3867 | if (exclude) |
| 3868 | { |
| 3869 | /* We don't set bfd_section to NULL since bfd_section of the |
| 3870 | removed output section statement may still be used. */ |
| 3871 | if (!os->update_dot) |
| 3872 | os->ignored = TRUE; |
| 3873 | output_section->flags |= SEC_EXCLUDE; |
| 3874 | bfd_section_list_remove (link_info.output_bfd, output_section); |
| 3875 | link_info.output_bfd->section_count--; |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | /* Stop future calls to lang_add_section from messing with map_head |
| 3880 | and map_tail link_order fields. */ |
| 3881 | stripped_excluded_sections = TRUE; |
| 3882 | } |
| 3883 | |
| 3884 | static void |
| 3885 | print_output_section_statement |
| 3886 | (lang_output_section_statement_type *output_section_statement) |
| 3887 | { |
| 3888 | asection *section = output_section_statement->bfd_section; |
| 3889 | int len; |
| 3890 | |
| 3891 | if (output_section_statement != abs_output_section) |
| 3892 | { |
| 3893 | minfo ("\n%s", output_section_statement->name); |
| 3894 | |
| 3895 | if (section != NULL) |
| 3896 | { |
| 3897 | print_dot = section->vma; |
| 3898 | |
| 3899 | len = strlen (output_section_statement->name); |
| 3900 | if (len >= SECTION_NAME_MAP_LENGTH - 1) |
| 3901 | { |
| 3902 | print_nl (); |
| 3903 | len = 0; |
| 3904 | } |
| 3905 | while (len < SECTION_NAME_MAP_LENGTH) |
| 3906 | { |
| 3907 | print_space (); |
| 3908 | ++len; |
| 3909 | } |
| 3910 | |
| 3911 | minfo ("0x%V %W", section->vma, section->size); |
| 3912 | |
| 3913 | if (section->vma != section->lma) |
| 3914 | minfo (_(" load address 0x%V"), section->lma); |
| 3915 | |
| 3916 | if (output_section_statement->update_dot_tree != NULL) |
| 3917 | exp_fold_tree (output_section_statement->update_dot_tree, |
| 3918 | bfd_abs_section_ptr, &print_dot); |
| 3919 | } |
| 3920 | |
| 3921 | print_nl (); |
| 3922 | } |
| 3923 | |
| 3924 | print_statement_list (output_section_statement->children.head, |
| 3925 | output_section_statement); |
| 3926 | } |
| 3927 | |
| 3928 | static void |
| 3929 | print_assignment (lang_assignment_statement_type *assignment, |
| 3930 | lang_output_section_statement_type *output_section) |
| 3931 | { |
| 3932 | unsigned int i; |
| 3933 | bfd_boolean is_dot; |
| 3934 | etree_type *tree; |
| 3935 | asection *osec; |
| 3936 | |
| 3937 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 3938 | print_space (); |
| 3939 | |
| 3940 | if (assignment->exp->type.node_class == etree_assert) |
| 3941 | { |
| 3942 | is_dot = FALSE; |
| 3943 | tree = assignment->exp->assert_s.child; |
| 3944 | } |
| 3945 | else |
| 3946 | { |
| 3947 | const char *dst = assignment->exp->assign.dst; |
| 3948 | |
| 3949 | is_dot = (dst[0] == '.' && dst[1] == 0); |
| 3950 | expld.assign_name = dst; |
| 3951 | tree = assignment->exp->assign.src; |
| 3952 | } |
| 3953 | |
| 3954 | osec = output_section->bfd_section; |
| 3955 | if (osec == NULL) |
| 3956 | osec = bfd_abs_section_ptr; |
| 3957 | exp_fold_tree (tree, osec, &print_dot); |
| 3958 | if (expld.result.valid_p) |
| 3959 | { |
| 3960 | bfd_vma value; |
| 3961 | |
| 3962 | if (assignment->exp->type.node_class == etree_assert |
| 3963 | || is_dot |
| 3964 | || expld.assign_name != NULL) |
| 3965 | { |
| 3966 | value = expld.result.value; |
| 3967 | |
| 3968 | if (expld.result.section != NULL) |
| 3969 | value += expld.result.section->vma; |
| 3970 | |
| 3971 | minfo ("0x%V", value); |
| 3972 | if (is_dot) |
| 3973 | print_dot = value; |
| 3974 | } |
| 3975 | else |
| 3976 | { |
| 3977 | struct bfd_link_hash_entry *h; |
| 3978 | |
| 3979 | h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, |
| 3980 | FALSE, FALSE, TRUE); |
| 3981 | if (h) |
| 3982 | { |
| 3983 | value = h->u.def.value; |
| 3984 | value += h->u.def.section->output_section->vma; |
| 3985 | value += h->u.def.section->output_offset; |
| 3986 | |
| 3987 | minfo ("[0x%V]", value); |
| 3988 | } |
| 3989 | else |
| 3990 | minfo ("[unresolved]"); |
| 3991 | } |
| 3992 | } |
| 3993 | else |
| 3994 | { |
| 3995 | minfo ("*undef* "); |
| 3996 | #ifdef BFD64 |
| 3997 | minfo (" "); |
| 3998 | #endif |
| 3999 | } |
| 4000 | expld.assign_name = NULL; |
| 4001 | |
| 4002 | minfo (" "); |
| 4003 | exp_print_tree (assignment->exp); |
| 4004 | print_nl (); |
| 4005 | } |
| 4006 | |
| 4007 | static void |
| 4008 | print_input_statement (lang_input_statement_type *statm) |
| 4009 | { |
| 4010 | if (statm->filename != NULL |
| 4011 | && (statm->the_bfd == NULL |
| 4012 | || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0)) |
| 4013 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
| 4014 | } |
| 4015 | |
| 4016 | /* Print all symbols defined in a particular section. This is called |
| 4017 | via bfd_link_hash_traverse, or by print_all_symbols. */ |
| 4018 | |
| 4019 | static bfd_boolean |
| 4020 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
| 4021 | { |
| 4022 | asection *sec = (asection *) ptr; |
| 4023 | |
| 4024 | if ((hash_entry->type == bfd_link_hash_defined |
| 4025 | || hash_entry->type == bfd_link_hash_defweak) |
| 4026 | && sec == hash_entry->u.def.section) |
| 4027 | { |
| 4028 | int i; |
| 4029 | |
| 4030 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 4031 | print_space (); |
| 4032 | minfo ("0x%V ", |
| 4033 | (hash_entry->u.def.value |
| 4034 | + hash_entry->u.def.section->output_offset |
| 4035 | + hash_entry->u.def.section->output_section->vma)); |
| 4036 | |
| 4037 | minfo (" %T\n", hash_entry->root.string); |
| 4038 | } |
| 4039 | |
| 4040 | return TRUE; |
| 4041 | } |
| 4042 | |
| 4043 | static int |
| 4044 | hash_entry_addr_cmp (const void *a, const void *b) |
| 4045 | { |
| 4046 | const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; |
| 4047 | const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; |
| 4048 | |
| 4049 | if (l->u.def.value < r->u.def.value) |
| 4050 | return -1; |
| 4051 | else if (l->u.def.value > r->u.def.value) |
| 4052 | return 1; |
| 4053 | else |
| 4054 | return 0; |
| 4055 | } |
| 4056 | |
| 4057 | static void |
| 4058 | print_all_symbols (asection *sec) |
| 4059 | { |
| 4060 | input_section_userdata_type *ud |
| 4061 | = (input_section_userdata_type *) get_userdata (sec); |
| 4062 | struct map_symbol_def *def; |
| 4063 | struct bfd_link_hash_entry **entries; |
| 4064 | unsigned int i; |
| 4065 | |
| 4066 | if (!ud) |
| 4067 | return; |
| 4068 | |
| 4069 | *ud->map_symbol_def_tail = 0; |
| 4070 | |
| 4071 | /* Sort the symbols by address. */ |
| 4072 | entries = (struct bfd_link_hash_entry **) |
| 4073 | obstack_alloc (&map_obstack, ud->map_symbol_def_count * sizeof (*entries)); |
| 4074 | |
| 4075 | for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) |
| 4076 | entries[i] = def->entry; |
| 4077 | |
| 4078 | qsort (entries, ud->map_symbol_def_count, sizeof (*entries), |
| 4079 | hash_entry_addr_cmp); |
| 4080 | |
| 4081 | /* Print the symbols. */ |
| 4082 | for (i = 0; i < ud->map_symbol_def_count; i++) |
| 4083 | print_one_symbol (entries[i], sec); |
| 4084 | |
| 4085 | obstack_free (&map_obstack, entries); |
| 4086 | } |
| 4087 | |
| 4088 | /* Print information about an input section to the map file. */ |
| 4089 | |
| 4090 | static void |
| 4091 | print_input_section (asection *i, bfd_boolean is_discarded) |
| 4092 | { |
| 4093 | bfd_size_type size = i->size; |
| 4094 | int len; |
| 4095 | bfd_vma addr; |
| 4096 | |
| 4097 | init_opb (); |
| 4098 | |
| 4099 | print_space (); |
| 4100 | minfo ("%s", i->name); |
| 4101 | |
| 4102 | len = 1 + strlen (i->name); |
| 4103 | if (len >= SECTION_NAME_MAP_LENGTH - 1) |
| 4104 | { |
| 4105 | print_nl (); |
| 4106 | len = 0; |
| 4107 | } |
| 4108 | while (len < SECTION_NAME_MAP_LENGTH) |
| 4109 | { |
| 4110 | print_space (); |
| 4111 | ++len; |
| 4112 | } |
| 4113 | |
| 4114 | if (i->output_section != NULL |
| 4115 | && i->output_section->owner == link_info.output_bfd) |
| 4116 | addr = i->output_section->vma + i->output_offset; |
| 4117 | else |
| 4118 | { |
| 4119 | addr = print_dot; |
| 4120 | if (!is_discarded) |
| 4121 | size = 0; |
| 4122 | } |
| 4123 | |
| 4124 | minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner); |
| 4125 | |
| 4126 | if (size != i->rawsize && i->rawsize != 0) |
| 4127 | { |
| 4128 | len = SECTION_NAME_MAP_LENGTH + 3; |
| 4129 | #ifdef BFD64 |
| 4130 | len += 16; |
| 4131 | #else |
| 4132 | len += 8; |
| 4133 | #endif |
| 4134 | while (len > 0) |
| 4135 | { |
| 4136 | print_space (); |
| 4137 | --len; |
| 4138 | } |
| 4139 | |
| 4140 | minfo (_("%W (size before relaxing)\n"), i->rawsize); |
| 4141 | } |
| 4142 | |
| 4143 | if (i->output_section != NULL |
| 4144 | && i->output_section->owner == link_info.output_bfd) |
| 4145 | { |
| 4146 | if (link_info.reduce_memory_overheads) |
| 4147 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); |
| 4148 | else |
| 4149 | print_all_symbols (i); |
| 4150 | |
| 4151 | /* Update print_dot, but make sure that we do not move it |
| 4152 | backwards - this could happen if we have overlays and a |
| 4153 | later overlay is shorter than an earier one. */ |
| 4154 | if (addr + TO_ADDR (size) > print_dot) |
| 4155 | print_dot = addr + TO_ADDR (size); |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | static void |
| 4160 | print_fill_statement (lang_fill_statement_type *fill) |
| 4161 | { |
| 4162 | size_t size; |
| 4163 | unsigned char *p; |
| 4164 | fputs (" FILL mask 0x", config.map_file); |
| 4165 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) |
| 4166 | fprintf (config.map_file, "%02x", *p); |
| 4167 | fputs ("\n", config.map_file); |
| 4168 | } |
| 4169 | |
| 4170 | static void |
| 4171 | print_data_statement (lang_data_statement_type *data) |
| 4172 | { |
| 4173 | int i; |
| 4174 | bfd_vma addr; |
| 4175 | bfd_size_type size; |
| 4176 | const char *name; |
| 4177 | |
| 4178 | init_opb (); |
| 4179 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 4180 | print_space (); |
| 4181 | |
| 4182 | addr = data->output_offset; |
| 4183 | if (data->output_section != NULL) |
| 4184 | addr += data->output_section->vma; |
| 4185 | |
| 4186 | switch (data->type) |
| 4187 | { |
| 4188 | default: |
| 4189 | abort (); |
| 4190 | case BYTE: |
| 4191 | size = BYTE_SIZE; |
| 4192 | name = "BYTE"; |
| 4193 | break; |
| 4194 | case SHORT: |
| 4195 | size = SHORT_SIZE; |
| 4196 | name = "SHORT"; |
| 4197 | break; |
| 4198 | case LONG: |
| 4199 | size = LONG_SIZE; |
| 4200 | name = "LONG"; |
| 4201 | break; |
| 4202 | case QUAD: |
| 4203 | size = QUAD_SIZE; |
| 4204 | name = "QUAD"; |
| 4205 | break; |
| 4206 | case SQUAD: |
| 4207 | size = QUAD_SIZE; |
| 4208 | name = "SQUAD"; |
| 4209 | break; |
| 4210 | } |
| 4211 | |
| 4212 | minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); |
| 4213 | |
| 4214 | if (data->exp->type.node_class != etree_value) |
| 4215 | { |
| 4216 | print_space (); |
| 4217 | exp_print_tree (data->exp); |
| 4218 | } |
| 4219 | |
| 4220 | print_nl (); |
| 4221 | |
| 4222 | print_dot = addr + TO_ADDR (size); |
| 4223 | } |
| 4224 | |
| 4225 | /* Print an address statement. These are generated by options like |
| 4226 | -Ttext. */ |
| 4227 | |
| 4228 | static void |
| 4229 | print_address_statement (lang_address_statement_type *address) |
| 4230 | { |
| 4231 | minfo (_("Address of section %s set to "), address->section_name); |
| 4232 | exp_print_tree (address->address); |
| 4233 | print_nl (); |
| 4234 | } |
| 4235 | |
| 4236 | /* Print a reloc statement. */ |
| 4237 | |
| 4238 | static void |
| 4239 | print_reloc_statement (lang_reloc_statement_type *reloc) |
| 4240 | { |
| 4241 | int i; |
| 4242 | bfd_vma addr; |
| 4243 | bfd_size_type size; |
| 4244 | |
| 4245 | init_opb (); |
| 4246 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 4247 | print_space (); |
| 4248 | |
| 4249 | addr = reloc->output_offset; |
| 4250 | if (reloc->output_section != NULL) |
| 4251 | addr += reloc->output_section->vma; |
| 4252 | |
| 4253 | size = bfd_get_reloc_size (reloc->howto); |
| 4254 | |
| 4255 | minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); |
| 4256 | |
| 4257 | if (reloc->name != NULL) |
| 4258 | minfo ("%s+", reloc->name); |
| 4259 | else |
| 4260 | minfo ("%s+", reloc->section->name); |
| 4261 | |
| 4262 | exp_print_tree (reloc->addend_exp); |
| 4263 | |
| 4264 | print_nl (); |
| 4265 | |
| 4266 | print_dot = addr + TO_ADDR (size); |
| 4267 | } |
| 4268 | |
| 4269 | static void |
| 4270 | print_padding_statement (lang_padding_statement_type *s) |
| 4271 | { |
| 4272 | int len; |
| 4273 | bfd_vma addr; |
| 4274 | |
| 4275 | init_opb (); |
| 4276 | minfo (" *fill*"); |
| 4277 | |
| 4278 | len = sizeof " *fill*" - 1; |
| 4279 | while (len < SECTION_NAME_MAP_LENGTH) |
| 4280 | { |
| 4281 | print_space (); |
| 4282 | ++len; |
| 4283 | } |
| 4284 | |
| 4285 | addr = s->output_offset; |
| 4286 | if (s->output_section != NULL) |
| 4287 | addr += s->output_section->vma; |
| 4288 | minfo ("0x%V %W ", addr, (bfd_vma) s->size); |
| 4289 | |
| 4290 | if (s->fill->size != 0) |
| 4291 | { |
| 4292 | size_t size; |
| 4293 | unsigned char *p; |
| 4294 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) |
| 4295 | fprintf (config.map_file, "%02x", *p); |
| 4296 | } |
| 4297 | |
| 4298 | print_nl (); |
| 4299 | |
| 4300 | print_dot = addr + TO_ADDR (s->size); |
| 4301 | } |
| 4302 | |
| 4303 | static void |
| 4304 | print_wild_statement (lang_wild_statement_type *w, |
| 4305 | lang_output_section_statement_type *os) |
| 4306 | { |
| 4307 | struct wildcard_list *sec; |
| 4308 | |
| 4309 | print_space (); |
| 4310 | |
| 4311 | if (w->filenames_sorted) |
| 4312 | minfo ("SORT("); |
| 4313 | if (w->filename != NULL) |
| 4314 | minfo ("%s", w->filename); |
| 4315 | else |
| 4316 | minfo ("*"); |
| 4317 | if (w->filenames_sorted) |
| 4318 | minfo (")"); |
| 4319 | |
| 4320 | minfo ("("); |
| 4321 | for (sec = w->section_list; sec; sec = sec->next) |
| 4322 | { |
| 4323 | if (sec->spec.sorted) |
| 4324 | minfo ("SORT("); |
| 4325 | if (sec->spec.exclude_name_list != NULL) |
| 4326 | { |
| 4327 | name_list *tmp; |
| 4328 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
| 4329 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
| 4330 | minfo (" %s", tmp->name); |
| 4331 | minfo (") "); |
| 4332 | } |
| 4333 | if (sec->spec.name != NULL) |
| 4334 | minfo ("%s", sec->spec.name); |
| 4335 | else |
| 4336 | minfo ("*"); |
| 4337 | if (sec->spec.sorted) |
| 4338 | minfo (")"); |
| 4339 | if (sec->next) |
| 4340 | minfo (" "); |
| 4341 | } |
| 4342 | minfo (")"); |
| 4343 | |
| 4344 | print_nl (); |
| 4345 | |
| 4346 | print_statement_list (w->children.head, os); |
| 4347 | } |
| 4348 | |
| 4349 | /* Print a group statement. */ |
| 4350 | |
| 4351 | static void |
| 4352 | print_group (lang_group_statement_type *s, |
| 4353 | lang_output_section_statement_type *os) |
| 4354 | { |
| 4355 | fprintf (config.map_file, "START GROUP\n"); |
| 4356 | print_statement_list (s->children.head, os); |
| 4357 | fprintf (config.map_file, "END GROUP\n"); |
| 4358 | } |
| 4359 | |
| 4360 | /* Print the list of statements in S. |
| 4361 | This can be called for any statement type. */ |
| 4362 | |
| 4363 | static void |
| 4364 | print_statement_list (lang_statement_union_type *s, |
| 4365 | lang_output_section_statement_type *os) |
| 4366 | { |
| 4367 | while (s != NULL) |
| 4368 | { |
| 4369 | print_statement (s, os); |
| 4370 | s = s->header.next; |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | /* Print the first statement in statement list S. |
| 4375 | This can be called for any statement type. */ |
| 4376 | |
| 4377 | static void |
| 4378 | print_statement (lang_statement_union_type *s, |
| 4379 | lang_output_section_statement_type *os) |
| 4380 | { |
| 4381 | switch (s->header.type) |
| 4382 | { |
| 4383 | default: |
| 4384 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); |
| 4385 | FAIL (); |
| 4386 | break; |
| 4387 | case lang_constructors_statement_enum: |
| 4388 | if (constructor_list.head != NULL) |
| 4389 | { |
| 4390 | if (constructors_sorted) |
| 4391 | minfo (" SORT (CONSTRUCTORS)\n"); |
| 4392 | else |
| 4393 | minfo (" CONSTRUCTORS\n"); |
| 4394 | print_statement_list (constructor_list.head, os); |
| 4395 | } |
| 4396 | break; |
| 4397 | case lang_wild_statement_enum: |
| 4398 | print_wild_statement (&s->wild_statement, os); |
| 4399 | break; |
| 4400 | case lang_address_statement_enum: |
| 4401 | print_address_statement (&s->address_statement); |
| 4402 | break; |
| 4403 | case lang_object_symbols_statement_enum: |
| 4404 | minfo (" CREATE_OBJECT_SYMBOLS\n"); |
| 4405 | break; |
| 4406 | case lang_fill_statement_enum: |
| 4407 | print_fill_statement (&s->fill_statement); |
| 4408 | break; |
| 4409 | case lang_data_statement_enum: |
| 4410 | print_data_statement (&s->data_statement); |
| 4411 | break; |
| 4412 | case lang_reloc_statement_enum: |
| 4413 | print_reloc_statement (&s->reloc_statement); |
| 4414 | break; |
| 4415 | case lang_input_section_enum: |
| 4416 | print_input_section (s->input_section.section, FALSE); |
| 4417 | break; |
| 4418 | case lang_padding_statement_enum: |
| 4419 | print_padding_statement (&s->padding_statement); |
| 4420 | break; |
| 4421 | case lang_output_section_statement_enum: |
| 4422 | print_output_section_statement (&s->output_section_statement); |
| 4423 | break; |
| 4424 | case lang_assignment_statement_enum: |
| 4425 | print_assignment (&s->assignment_statement, os); |
| 4426 | break; |
| 4427 | case lang_target_statement_enum: |
| 4428 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); |
| 4429 | break; |
| 4430 | case lang_output_statement_enum: |
| 4431 | minfo ("OUTPUT(%s", s->output_statement.name); |
| 4432 | if (output_target != NULL) |
| 4433 | minfo (" %s", output_target); |
| 4434 | minfo (")\n"); |
| 4435 | break; |
| 4436 | case lang_input_statement_enum: |
| 4437 | print_input_statement (&s->input_statement); |
| 4438 | break; |
| 4439 | case lang_group_statement_enum: |
| 4440 | print_group (&s->group_statement, os); |
| 4441 | break; |
| 4442 | case lang_insert_statement_enum: |
| 4443 | minfo ("INSERT %s %s\n", |
| 4444 | s->insert_statement.is_before ? "BEFORE" : "AFTER", |
| 4445 | s->insert_statement.where); |
| 4446 | break; |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | static void |
| 4451 | print_statements (void) |
| 4452 | { |
| 4453 | print_statement_list (statement_list.head, abs_output_section); |
| 4454 | } |
| 4455 | |
| 4456 | /* Print the first N statements in statement list S to STDERR. |
| 4457 | If N == 0, nothing is printed. |
| 4458 | If N < 0, the entire list is printed. |
| 4459 | Intended to be called from GDB. */ |
| 4460 | |
| 4461 | void |
| 4462 | dprint_statement (lang_statement_union_type *s, int n) |
| 4463 | { |
| 4464 | FILE *map_save = config.map_file; |
| 4465 | |
| 4466 | config.map_file = stderr; |
| 4467 | |
| 4468 | if (n < 0) |
| 4469 | print_statement_list (s, abs_output_section); |
| 4470 | else |
| 4471 | { |
| 4472 | while (s && --n >= 0) |
| 4473 | { |
| 4474 | print_statement (s, abs_output_section); |
| 4475 | s = s->header.next; |
| 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | config.map_file = map_save; |
| 4480 | } |
| 4481 | |
| 4482 | static void |
| 4483 | insert_pad (lang_statement_union_type **ptr, |
| 4484 | fill_type *fill, |
| 4485 | bfd_size_type alignment_needed, |
| 4486 | asection *output_section, |
| 4487 | bfd_vma dot) |
| 4488 | { |
| 4489 | static fill_type zero_fill; |
| 4490 | lang_statement_union_type *pad = NULL; |
| 4491 | |
| 4492 | if (ptr != &statement_list.head) |
| 4493 | pad = ((lang_statement_union_type *) |
| 4494 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); |
| 4495 | if (pad != NULL |
| 4496 | && pad->header.type == lang_padding_statement_enum |
| 4497 | && pad->padding_statement.output_section == output_section) |
| 4498 | { |
| 4499 | /* Use the existing pad statement. */ |
| 4500 | } |
| 4501 | else if ((pad = *ptr) != NULL |
| 4502 | && pad->header.type == lang_padding_statement_enum |
| 4503 | && pad->padding_statement.output_section == output_section) |
| 4504 | { |
| 4505 | /* Use the existing pad statement. */ |
| 4506 | } |
| 4507 | else |
| 4508 | { |
| 4509 | /* Make a new padding statement, linked into existing chain. */ |
| 4510 | pad = (lang_statement_union_type *) |
| 4511 | stat_alloc (sizeof (lang_padding_statement_type)); |
| 4512 | pad->header.next = *ptr; |
| 4513 | *ptr = pad; |
| 4514 | pad->header.type = lang_padding_statement_enum; |
| 4515 | pad->padding_statement.output_section = output_section; |
| 4516 | if (fill == NULL) |
| 4517 | fill = &zero_fill; |
| 4518 | pad->padding_statement.fill = fill; |
| 4519 | } |
| 4520 | pad->padding_statement.output_offset = dot - output_section->vma; |
| 4521 | pad->padding_statement.size = alignment_needed; |
| 4522 | output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) |
| 4523 | - output_section->vma); |
| 4524 | } |
| 4525 | |
| 4526 | /* Work out how much this section will move the dot point. */ |
| 4527 | |
| 4528 | static bfd_vma |
| 4529 | size_input_section |
| 4530 | (lang_statement_union_type **this_ptr, |
| 4531 | lang_output_section_statement_type *output_section_statement, |
| 4532 | fill_type *fill, |
| 4533 | bfd_vma dot) |
| 4534 | { |
| 4535 | lang_input_section_type *is = &((*this_ptr)->input_section); |
| 4536 | asection *i = is->section; |
| 4537 | |
| 4538 | if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS |
| 4539 | && (i->flags & SEC_EXCLUDE) == 0) |
| 4540 | { |
| 4541 | bfd_size_type alignment_needed; |
| 4542 | asection *o; |
| 4543 | |
| 4544 | /* Align this section first to the input sections requirement, |
| 4545 | then to the output section's requirement. If this alignment |
| 4546 | is greater than any seen before, then record it too. Perform |
| 4547 | the alignment by inserting a magic 'padding' statement. */ |
| 4548 | |
| 4549 | if (output_section_statement->subsection_alignment != -1) |
| 4550 | i->alignment_power = output_section_statement->subsection_alignment; |
| 4551 | |
| 4552 | o = output_section_statement->bfd_section; |
| 4553 | if (o->alignment_power < i->alignment_power) |
| 4554 | o->alignment_power = i->alignment_power; |
| 4555 | |
| 4556 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
| 4557 | |
| 4558 | if (alignment_needed != 0) |
| 4559 | { |
| 4560 | insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); |
| 4561 | dot += alignment_needed; |
| 4562 | } |
| 4563 | |
| 4564 | /* Remember where in the output section this input section goes. */ |
| 4565 | |
| 4566 | i->output_offset = dot - o->vma; |
| 4567 | |
| 4568 | /* Mark how big the output section must be to contain this now. */ |
| 4569 | dot += TO_ADDR (i->size); |
| 4570 | o->size = TO_SIZE (dot - o->vma); |
| 4571 | } |
| 4572 | else |
| 4573 | { |
| 4574 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; |
| 4575 | } |
| 4576 | |
| 4577 | return dot; |
| 4578 | } |
| 4579 | |
| 4580 | static int |
| 4581 | sort_sections_by_lma (const void *arg1, const void *arg2) |
| 4582 | { |
| 4583 | const asection *sec1 = *(const asection **) arg1; |
| 4584 | const asection *sec2 = *(const asection **) arg2; |
| 4585 | |
| 4586 | if (bfd_section_lma (sec1->owner, sec1) |
| 4587 | < bfd_section_lma (sec2->owner, sec2)) |
| 4588 | return -1; |
| 4589 | else if (bfd_section_lma (sec1->owner, sec1) |
| 4590 | > bfd_section_lma (sec2->owner, sec2)) |
| 4591 | return 1; |
| 4592 | else if (sec1->id < sec2->id) |
| 4593 | return -1; |
| 4594 | else if (sec1->id > sec2->id) |
| 4595 | return 1; |
| 4596 | |
| 4597 | return 0; |
| 4598 | } |
| 4599 | |
| 4600 | #define IGNORE_SECTION(s) \ |
| 4601 | ((s->flags & SEC_ALLOC) == 0 \ |
| 4602 | || ((s->flags & SEC_THREAD_LOCAL) != 0 \ |
| 4603 | && (s->flags & SEC_LOAD) == 0)) |
| 4604 | |
| 4605 | /* Check to see if any allocated sections overlap with other allocated |
| 4606 | sections. This can happen if a linker script specifies the output |
| 4607 | section addresses of the two sections. Also check whether any memory |
| 4608 | region has overflowed. */ |
| 4609 | |
| 4610 | static void |
| 4611 | lang_check_section_addresses (void) |
| 4612 | { |
| 4613 | asection *s, *p; |
| 4614 | asection **sections, **spp; |
| 4615 | unsigned int count; |
| 4616 | bfd_vma s_start; |
| 4617 | bfd_vma s_end; |
| 4618 | bfd_vma p_start; |
| 4619 | bfd_vma p_end; |
| 4620 | bfd_size_type amt; |
| 4621 | lang_memory_region_type *m; |
| 4622 | |
| 4623 | if (bfd_count_sections (link_info.output_bfd) <= 1) |
| 4624 | return; |
| 4625 | |
| 4626 | amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *); |
| 4627 | sections = (asection **) xmalloc (amt); |
| 4628 | |
| 4629 | /* Scan all sections in the output list. */ |
| 4630 | count = 0; |
| 4631 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
| 4632 | { |
| 4633 | /* Only consider loadable sections with real contents. */ |
| 4634 | if (!(s->flags & SEC_LOAD) |
| 4635 | || !(s->flags & SEC_ALLOC) |
| 4636 | || s->size == 0) |
| 4637 | continue; |
| 4638 | |
| 4639 | sections[count] = s; |
| 4640 | count++; |
| 4641 | } |
| 4642 | |
| 4643 | if (count <= 1) |
| 4644 | return; |
| 4645 | |
| 4646 | qsort (sections, (size_t) count, sizeof (asection *), |
| 4647 | sort_sections_by_lma); |
| 4648 | |
| 4649 | spp = sections; |
| 4650 | s = *spp++; |
| 4651 | s_start = s->lma; |
| 4652 | s_end = s_start + TO_ADDR (s->size) - 1; |
| 4653 | for (count--; count; count--) |
| 4654 | { |
| 4655 | /* We must check the sections' LMA addresses not their VMA |
| 4656 | addresses because overlay sections can have overlapping VMAs |
| 4657 | but they must have distinct LMAs. */ |
| 4658 | p = s; |
| 4659 | p_start = s_start; |
| 4660 | p_end = s_end; |
| 4661 | s = *spp++; |
| 4662 | s_start = s->lma; |
| 4663 | s_end = s_start + TO_ADDR (s->size) - 1; |
| 4664 | |
| 4665 | /* Look for an overlap. We have sorted sections by lma, so we |
| 4666 | know that s_start >= p_start. Besides the obvious case of |
| 4667 | overlap when the current section starts before the previous |
| 4668 | one ends, we also must have overlap if the previous section |
| 4669 | wraps around the address space. */ |
| 4670 | if (s_start <= p_end |
| 4671 | || p_end < p_start) |
| 4672 | einfo (_("%X%P: section %s loaded at [%V,%V] overlaps section %s loaded at [%V,%V]\n"), |
| 4673 | s->name, s_start, s_end, p->name, p_start, p_end); |
| 4674 | } |
| 4675 | |
| 4676 | free (sections); |
| 4677 | |
| 4678 | /* If any memory region has overflowed, report by how much. |
| 4679 | We do not issue this diagnostic for regions that had sections |
| 4680 | explicitly placed outside their bounds; os_region_check's |
| 4681 | diagnostics are adequate for that case. |
| 4682 | |
| 4683 | FIXME: It is conceivable that m->current - (m->origin + m->length) |
| 4684 | might overflow a 32-bit integer. There is, alas, no way to print |
| 4685 | a bfd_vma quantity in decimal. */ |
| 4686 | for (m = lang_memory_region_list; m; m = m->next) |
| 4687 | if (m->had_full_message) |
| 4688 | einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"), |
| 4689 | m->name_list.name, (long)(m->current - (m->origin + m->length))); |
| 4690 | |
| 4691 | } |
| 4692 | |
| 4693 | /* Make sure the new address is within the region. We explicitly permit the |
| 4694 | current address to be at the exact end of the region when the address is |
| 4695 | non-zero, in case the region is at the end of addressable memory and the |
| 4696 | calculation wraps around. */ |
| 4697 | |
| 4698 | static void |
| 4699 | os_region_check (lang_output_section_statement_type *os, |
| 4700 | lang_memory_region_type *region, |
| 4701 | etree_type *tree, |
| 4702 | bfd_vma rbase) |
| 4703 | { |
| 4704 | if ((region->current < region->origin |
| 4705 | || (region->current - region->origin > region->length)) |
| 4706 | && ((region->current != region->origin + region->length) |
| 4707 | || rbase == 0)) |
| 4708 | { |
| 4709 | if (tree != NULL) |
| 4710 | { |
| 4711 | einfo (_("%X%P: address 0x%v of %B section `%s'" |
| 4712 | " is not within region `%s'\n"), |
| 4713 | region->current, |
| 4714 | os->bfd_section->owner, |
| 4715 | os->bfd_section->name, |
| 4716 | region->name_list.name); |
| 4717 | } |
| 4718 | else if (!region->had_full_message) |
| 4719 | { |
| 4720 | region->had_full_message = TRUE; |
| 4721 | |
| 4722 | einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"), |
| 4723 | os->bfd_section->owner, |
| 4724 | os->bfd_section->name, |
| 4725 | region->name_list.name); |
| 4726 | } |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | /* Set the sizes for all the output sections. */ |
| 4731 | |
| 4732 | static bfd_vma |
| 4733 | lang_size_sections_1 |
| 4734 | (lang_statement_union_type **prev, |
| 4735 | lang_output_section_statement_type *output_section_statement, |
| 4736 | fill_type *fill, |
| 4737 | bfd_vma dot, |
| 4738 | bfd_boolean *relax, |
| 4739 | bfd_boolean check_regions) |
| 4740 | { |
| 4741 | lang_statement_union_type *s; |
| 4742 | |
| 4743 | /* Size up the sections from their constituent parts. */ |
| 4744 | for (s = *prev; s != NULL; s = s->header.next) |
| 4745 | { |
| 4746 | switch (s->header.type) |
| 4747 | { |
| 4748 | case lang_output_section_statement_enum: |
| 4749 | { |
| 4750 | bfd_vma newdot, after; |
| 4751 | lang_output_section_statement_type *os; |
| 4752 | lang_memory_region_type *r; |
| 4753 | int section_alignment = 0; |
| 4754 | |
| 4755 | os = &s->output_section_statement; |
| 4756 | if (os->constraint == -1) |
| 4757 | break; |
| 4758 | |
| 4759 | /* FIXME: We shouldn't need to zero section vmas for ld -r |
| 4760 | here, in lang_insert_orphan, or in the default linker scripts. |
| 4761 | This is covering for coff backend linker bugs. See PR6945. */ |
| 4762 | if (os->addr_tree == NULL |
| 4763 | && link_info.relocatable |
| 4764 | && (bfd_get_flavour (link_info.output_bfd) |
| 4765 | == bfd_target_coff_flavour)) |
| 4766 | os->addr_tree = exp_intop (0); |
| 4767 | if (os->addr_tree != NULL) |
| 4768 | { |
| 4769 | os->processed_vma = FALSE; |
| 4770 | exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot); |
| 4771 | |
| 4772 | if (expld.result.valid_p) |
| 4773 | { |
| 4774 | dot = expld.result.value; |
| 4775 | if (expld.result.section != NULL) |
| 4776 | dot += expld.result.section->vma; |
| 4777 | } |
| 4778 | else if (expld.phase != lang_mark_phase_enum) |
| 4779 | einfo (_("%F%S: non constant or forward reference" |
| 4780 | " address expression for section %s\n"), |
| 4781 | os->addr_tree, os->name); |
| 4782 | } |
| 4783 | |
| 4784 | if (os->bfd_section == NULL) |
| 4785 | /* This section was removed or never actually created. */ |
| 4786 | break; |
| 4787 | |
| 4788 | /* If this is a COFF shared library section, use the size and |
| 4789 | address from the input section. FIXME: This is COFF |
| 4790 | specific; it would be cleaner if there were some other way |
| 4791 | to do this, but nothing simple comes to mind. */ |
| 4792 | if (((bfd_get_flavour (link_info.output_bfd) |
| 4793 | == bfd_target_ecoff_flavour) |
| 4794 | || (bfd_get_flavour (link_info.output_bfd) |
| 4795 | == bfd_target_coff_flavour)) |
| 4796 | && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
| 4797 | { |
| 4798 | asection *input; |
| 4799 | |
| 4800 | if (os->children.head == NULL |
| 4801 | || os->children.head->header.next != NULL |
| 4802 | || (os->children.head->header.type |
| 4803 | != lang_input_section_enum)) |
| 4804 | einfo (_("%P%X: Internal error on COFF shared library" |
| 4805 | " section %s\n"), os->name); |
| 4806 | |
| 4807 | input = os->children.head->input_section.section; |
| 4808 | bfd_set_section_vma (os->bfd_section->owner, |
| 4809 | os->bfd_section, |
| 4810 | bfd_section_vma (input->owner, input)); |
| 4811 | os->bfd_section->size = input->size; |
| 4812 | break; |
| 4813 | } |
| 4814 | |
| 4815 | newdot = dot; |
| 4816 | if (bfd_is_abs_section (os->bfd_section)) |
| 4817 | { |
| 4818 | /* No matter what happens, an abs section starts at zero. */ |
| 4819 | ASSERT (os->bfd_section->vma == 0); |
| 4820 | } |
| 4821 | else |
| 4822 | { |
| 4823 | if (os->addr_tree == NULL) |
| 4824 | { |
| 4825 | /* No address specified for this section, get one |
| 4826 | from the region specification. */ |
| 4827 | if (os->region == NULL |
| 4828 | || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) |
| 4829 | && os->region->name_list.name[0] == '*' |
| 4830 | && strcmp (os->region->name_list.name, |
| 4831 | DEFAULT_MEMORY_REGION) == 0)) |
| 4832 | { |
| 4833 | os->region = lang_memory_default (os->bfd_section); |
| 4834 | } |
| 4835 | |
| 4836 | /* If a loadable section is using the default memory |
| 4837 | region, and some non default memory regions were |
| 4838 | defined, issue an error message. */ |
| 4839 | if (!os->ignored |
| 4840 | && !IGNORE_SECTION (os->bfd_section) |
| 4841 | && ! link_info.relocatable |
| 4842 | && check_regions |
| 4843 | && strcmp (os->region->name_list.name, |
| 4844 | DEFAULT_MEMORY_REGION) == 0 |
| 4845 | && lang_memory_region_list != NULL |
| 4846 | && (strcmp (lang_memory_region_list->name_list.name, |
| 4847 | DEFAULT_MEMORY_REGION) != 0 |
| 4848 | || lang_memory_region_list->next != NULL) |
| 4849 | && expld.phase != lang_mark_phase_enum) |
| 4850 | { |
| 4851 | /* By default this is an error rather than just a |
| 4852 | warning because if we allocate the section to the |
| 4853 | default memory region we can end up creating an |
| 4854 | excessively large binary, or even seg faulting when |
| 4855 | attempting to perform a negative seek. See |
| 4856 | sources.redhat.com/ml/binutils/2003-04/msg00423.html |
| 4857 | for an example of this. This behaviour can be |
| 4858 | overridden by the using the --no-check-sections |
| 4859 | switch. */ |
| 4860 | if (command_line.check_section_addresses) |
| 4861 | einfo (_("%P%F: error: no memory region specified" |
| 4862 | " for loadable section `%s'\n"), |
| 4863 | bfd_get_section_name (link_info.output_bfd, |
| 4864 | os->bfd_section)); |
| 4865 | else |
| 4866 | einfo (_("%P: warning: no memory region specified" |
| 4867 | " for loadable section `%s'\n"), |
| 4868 | bfd_get_section_name (link_info.output_bfd, |
| 4869 | os->bfd_section)); |
| 4870 | } |
| 4871 | |
| 4872 | newdot = os->region->current; |
| 4873 | section_alignment = os->bfd_section->alignment_power; |
| 4874 | } |
| 4875 | else |
| 4876 | section_alignment = os->section_alignment; |
| 4877 | |
| 4878 | /* Align to what the section needs. */ |
| 4879 | if (section_alignment > 0) |
| 4880 | { |
| 4881 | bfd_vma savedot = newdot; |
| 4882 | newdot = align_power (newdot, section_alignment); |
| 4883 | |
| 4884 | if (newdot != savedot |
| 4885 | && (config.warn_section_align |
| 4886 | || os->addr_tree != NULL) |
| 4887 | && expld.phase != lang_mark_phase_enum) |
| 4888 | einfo (_("%P: warning: changing start of section" |
| 4889 | " %s by %lu bytes\n"), |
| 4890 | os->name, (unsigned long) (newdot - savedot)); |
| 4891 | } |
| 4892 | |
| 4893 | bfd_set_section_vma (0, os->bfd_section, newdot); |
| 4894 | |
| 4895 | os->bfd_section->output_offset = 0; |
| 4896 | } |
| 4897 | |
| 4898 | lang_size_sections_1 (&os->children.head, os, |
| 4899 | os->fill, newdot, relax, check_regions); |
| 4900 | |
| 4901 | os->processed_vma = TRUE; |
| 4902 | |
| 4903 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
| 4904 | /* Except for some special linker created sections, |
| 4905 | no output section should change from zero size |
| 4906 | after strip_excluded_output_sections. A non-zero |
| 4907 | size on an ignored section indicates that some |
| 4908 | input section was not sized early enough. */ |
| 4909 | ASSERT (os->bfd_section->size == 0); |
| 4910 | else |
| 4911 | { |
| 4912 | dot = os->bfd_section->vma; |
| 4913 | |
| 4914 | /* Put the section within the requested block size, or |
| 4915 | align at the block boundary. */ |
| 4916 | after = ((dot |
| 4917 | + TO_ADDR (os->bfd_section->size) |
| 4918 | + os->block_value - 1) |
| 4919 | & - (bfd_vma) os->block_value); |
| 4920 | |
| 4921 | os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma); |
| 4922 | } |
| 4923 | |
| 4924 | /* Set section lma. */ |
| 4925 | r = os->region; |
| 4926 | if (r == NULL) |
| 4927 | r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
| 4928 | |
| 4929 | if (os->load_base) |
| 4930 | { |
| 4931 | bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); |
| 4932 | os->bfd_section->lma = lma; |
| 4933 | } |
| 4934 | else if (os->lma_region != NULL) |
| 4935 | { |
| 4936 | bfd_vma lma = os->lma_region->current; |
| 4937 | |
| 4938 | /* When LMA_REGION is the same as REGION, align the LMA |
| 4939 | as we did for the VMA, possibly including alignment |
| 4940 | from the bfd section. If a different region, then |
| 4941 | only align according to the value in the output |
| 4942 | statement unless specified otherwise. */ |
| 4943 | if (os->lma_region != os->region && !os->align_lma_with_input) |
| 4944 | section_alignment = os->section_alignment; |
| 4945 | if (section_alignment > 0) |
| 4946 | lma = align_power (lma, section_alignment); |
| 4947 | os->bfd_section->lma = lma; |
| 4948 | } |
| 4949 | else if (r->last_os != NULL |
| 4950 | && (os->bfd_section->flags & SEC_ALLOC) != 0) |
| 4951 | { |
| 4952 | bfd_vma lma; |
| 4953 | asection *last; |
| 4954 | |
| 4955 | last = r->last_os->output_section_statement.bfd_section; |
| 4956 | |
| 4957 | /* A backwards move of dot should be accompanied by |
| 4958 | an explicit assignment to the section LMA (ie. |
| 4959 | os->load_base set) because backwards moves can |
| 4960 | create overlapping LMAs. */ |
| 4961 | if (dot < last->vma |
| 4962 | && os->bfd_section->size != 0 |
| 4963 | && dot + os->bfd_section->size <= last->vma) |
| 4964 | { |
| 4965 | /* If dot moved backwards then leave lma equal to |
| 4966 | vma. This is the old default lma, which might |
| 4967 | just happen to work when the backwards move is |
| 4968 | sufficiently large. Nag if this changes anything, |
| 4969 | so people can fix their linker scripts. */ |
| 4970 | |
| 4971 | if (last->vma != last->lma) |
| 4972 | einfo (_("%P: warning: dot moved backwards before `%s'\n"), |
| 4973 | os->name); |
| 4974 | } |
| 4975 | else |
| 4976 | { |
| 4977 | /* If this is an overlay, set the current lma to that |
| 4978 | at the end of the previous section. */ |
| 4979 | if (os->sectype == overlay_section) |
| 4980 | lma = last->lma + last->size; |
| 4981 | |
| 4982 | /* Otherwise, keep the same lma to vma relationship |
| 4983 | as the previous section. */ |
| 4984 | else |
| 4985 | lma = dot + last->lma - last->vma; |
| 4986 | |
| 4987 | if (section_alignment > 0) |
| 4988 | lma = align_power (lma, section_alignment); |
| 4989 | os->bfd_section->lma = lma; |
| 4990 | } |
| 4991 | } |
| 4992 | os->processed_lma = TRUE; |
| 4993 | |
| 4994 | if (bfd_is_abs_section (os->bfd_section) || os->ignored) |
| 4995 | break; |
| 4996 | |
| 4997 | /* Keep track of normal sections using the default |
| 4998 | lma region. We use this to set the lma for |
| 4999 | following sections. Overlays or other linker |
| 5000 | script assignment to lma might mean that the |
| 5001 | default lma == vma is incorrect. |
| 5002 | To avoid warnings about dot moving backwards when using |
| 5003 | -Ttext, don't start tracking sections until we find one |
| 5004 | of non-zero size or with lma set differently to vma. */ |
| 5005 | if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 |
| 5006 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0) |
| 5007 | && (os->bfd_section->flags & SEC_ALLOC) != 0 |
| 5008 | && (os->bfd_section->size != 0 |
| 5009 | || (r->last_os == NULL |
| 5010 | && os->bfd_section->vma != os->bfd_section->lma) |
| 5011 | || (r->last_os != NULL |
| 5012 | && dot >= (r->last_os->output_section_statement |
| 5013 | .bfd_section->vma))) |
| 5014 | && os->lma_region == NULL |
| 5015 | && !link_info.relocatable) |
| 5016 | r->last_os = s; |
| 5017 | |
| 5018 | /* .tbss sections effectively have zero size. */ |
| 5019 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 |
| 5020 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 |
| 5021 | || link_info.relocatable) |
| 5022 | dot += TO_ADDR (os->bfd_section->size); |
| 5023 | |
| 5024 | if (os->update_dot_tree != 0) |
| 5025 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
| 5026 | |
| 5027 | /* Update dot in the region ? |
| 5028 | We only do this if the section is going to be allocated, |
| 5029 | since unallocated sections do not contribute to the region's |
| 5030 | overall size in memory. */ |
| 5031 | if (os->region != NULL |
| 5032 | && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) |
| 5033 | { |
| 5034 | os->region->current = dot; |
| 5035 | |
| 5036 | if (check_regions) |
| 5037 | /* Make sure the new address is within the region. */ |
| 5038 | os_region_check (os, os->region, os->addr_tree, |
| 5039 | os->bfd_section->vma); |
| 5040 | |
| 5041 | if (os->lma_region != NULL && os->lma_region != os->region |
| 5042 | && (os->bfd_section->flags & SEC_LOAD)) |
| 5043 | { |
| 5044 | os->lma_region->current |
| 5045 | = os->bfd_section->lma + TO_ADDR (os->bfd_section->size); |
| 5046 | |
| 5047 | if (check_regions) |
| 5048 | os_region_check (os, os->lma_region, NULL, |
| 5049 | os->bfd_section->lma); |
| 5050 | } |
| 5051 | } |
| 5052 | } |
| 5053 | break; |
| 5054 | |
| 5055 | case lang_constructors_statement_enum: |
| 5056 | dot = lang_size_sections_1 (&constructor_list.head, |
| 5057 | output_section_statement, |
| 5058 | fill, dot, relax, check_regions); |
| 5059 | break; |
| 5060 | |
| 5061 | case lang_data_statement_enum: |
| 5062 | { |
| 5063 | unsigned int size = 0; |
| 5064 | |
| 5065 | s->data_statement.output_offset = |
| 5066 | dot - output_section_statement->bfd_section->vma; |
| 5067 | s->data_statement.output_section = |
| 5068 | output_section_statement->bfd_section; |
| 5069 | |
| 5070 | /* We might refer to provided symbols in the expression, and |
| 5071 | need to mark them as needed. */ |
| 5072 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
| 5073 | |
| 5074 | switch (s->data_statement.type) |
| 5075 | { |
| 5076 | default: |
| 5077 | abort (); |
| 5078 | case QUAD: |
| 5079 | case SQUAD: |
| 5080 | size = QUAD_SIZE; |
| 5081 | break; |
| 5082 | case LONG: |
| 5083 | size = LONG_SIZE; |
| 5084 | break; |
| 5085 | case SHORT: |
| 5086 | size = SHORT_SIZE; |
| 5087 | break; |
| 5088 | case BYTE: |
| 5089 | size = BYTE_SIZE; |
| 5090 | break; |
| 5091 | } |
| 5092 | if (size < TO_SIZE ((unsigned) 1)) |
| 5093 | size = TO_SIZE ((unsigned) 1); |
| 5094 | dot += TO_ADDR (size); |
| 5095 | output_section_statement->bfd_section->size |
| 5096 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); |
| 5097 | |
| 5098 | } |
| 5099 | break; |
| 5100 | |
| 5101 | case lang_reloc_statement_enum: |
| 5102 | { |
| 5103 | int size; |
| 5104 | |
| 5105 | s->reloc_statement.output_offset = |
| 5106 | dot - output_section_statement->bfd_section->vma; |
| 5107 | s->reloc_statement.output_section = |
| 5108 | output_section_statement->bfd_section; |
| 5109 | size = bfd_get_reloc_size (s->reloc_statement.howto); |
| 5110 | dot += TO_ADDR (size); |
| 5111 | output_section_statement->bfd_section->size |
| 5112 | = TO_SIZE (dot - output_section_statement->bfd_section->vma); |
| 5113 | } |
| 5114 | break; |
| 5115 | |
| 5116 | case lang_wild_statement_enum: |
| 5117 | dot = lang_size_sections_1 (&s->wild_statement.children.head, |
| 5118 | output_section_statement, |
| 5119 | fill, dot, relax, check_regions); |
| 5120 | break; |
| 5121 | |
| 5122 | case lang_object_symbols_statement_enum: |
| 5123 | link_info.create_object_symbols_section = |
| 5124 | output_section_statement->bfd_section; |
| 5125 | break; |
| 5126 | |
| 5127 | case lang_output_statement_enum: |
| 5128 | case lang_target_statement_enum: |
| 5129 | break; |
| 5130 | |
| 5131 | case lang_input_section_enum: |
| 5132 | { |
| 5133 | asection *i; |
| 5134 | |
| 5135 | i = s->input_section.section; |
| 5136 | if (relax) |
| 5137 | { |
| 5138 | bfd_boolean again; |
| 5139 | |
| 5140 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) |
| 5141 | einfo (_("%P%F: can't relax section: %E\n")); |
| 5142 | if (again) |
| 5143 | *relax = TRUE; |
| 5144 | } |
| 5145 | dot = size_input_section (prev, output_section_statement, |
| 5146 | output_section_statement->fill, dot); |
| 5147 | } |
| 5148 | break; |
| 5149 | |
| 5150 | case lang_input_statement_enum: |
| 5151 | break; |
| 5152 | |
| 5153 | case lang_fill_statement_enum: |
| 5154 | s->fill_statement.output_section = |
| 5155 | output_section_statement->bfd_section; |
| 5156 | |
| 5157 | fill = s->fill_statement.fill; |
| 5158 | break; |
| 5159 | |
| 5160 | case lang_assignment_statement_enum: |
| 5161 | { |
| 5162 | bfd_vma newdot = dot; |
| 5163 | etree_type *tree = s->assignment_statement.exp; |
| 5164 | |
| 5165 | expld.dataseg.relro = exp_dataseg_relro_none; |
| 5166 | |
| 5167 | exp_fold_tree (tree, |
| 5168 | output_section_statement->bfd_section, |
| 5169 | &newdot); |
| 5170 | |
| 5171 | if (expld.dataseg.relro == exp_dataseg_relro_start) |
| 5172 | { |
| 5173 | if (!expld.dataseg.relro_start_stat) |
| 5174 | expld.dataseg.relro_start_stat = s; |
| 5175 | else |
| 5176 | { |
| 5177 | ASSERT (expld.dataseg.relro_start_stat == s); |
| 5178 | } |
| 5179 | } |
| 5180 | else if (expld.dataseg.relro == exp_dataseg_relro_end) |
| 5181 | { |
| 5182 | if (!expld.dataseg.relro_end_stat) |
| 5183 | expld.dataseg.relro_end_stat = s; |
| 5184 | else |
| 5185 | { |
| 5186 | ASSERT (expld.dataseg.relro_end_stat == s); |
| 5187 | } |
| 5188 | } |
| 5189 | expld.dataseg.relro = exp_dataseg_relro_none; |
| 5190 | |
| 5191 | /* This symbol may be relative to this section. */ |
| 5192 | if ((tree->type.node_class == etree_provided |
| 5193 | || tree->type.node_class == etree_assign) |
| 5194 | && (tree->assign.dst [0] != '.' |
| 5195 | || tree->assign.dst [1] != '\0')) |
| 5196 | output_section_statement->update_dot = 1; |
| 5197 | |
| 5198 | if (!output_section_statement->ignored) |
| 5199 | { |
| 5200 | if (output_section_statement == abs_output_section) |
| 5201 | { |
| 5202 | /* If we don't have an output section, then just adjust |
| 5203 | the default memory address. */ |
| 5204 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, |
| 5205 | FALSE)->current = newdot; |
| 5206 | } |
| 5207 | else if (newdot != dot) |
| 5208 | { |
| 5209 | /* Insert a pad after this statement. We can't |
| 5210 | put the pad before when relaxing, in case the |
| 5211 | assignment references dot. */ |
| 5212 | insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), |
| 5213 | output_section_statement->bfd_section, dot); |
| 5214 | |
| 5215 | /* Don't neuter the pad below when relaxing. */ |
| 5216 | s = s->header.next; |
| 5217 | |
| 5218 | /* If dot is advanced, this implies that the section |
| 5219 | should have space allocated to it, unless the |
| 5220 | user has explicitly stated that the section |
| 5221 | should not be allocated. */ |
| 5222 | if (output_section_statement->sectype != noalloc_section |
| 5223 | && (output_section_statement->sectype != noload_section |
| 5224 | || (bfd_get_flavour (link_info.output_bfd) |
| 5225 | == bfd_target_elf_flavour))) |
| 5226 | output_section_statement->bfd_section->flags |= SEC_ALLOC; |
| 5227 | } |
| 5228 | dot = newdot; |
| 5229 | } |
| 5230 | } |
| 5231 | break; |
| 5232 | |
| 5233 | case lang_padding_statement_enum: |
| 5234 | /* If this is the first time lang_size_sections is called, |
| 5235 | we won't have any padding statements. If this is the |
| 5236 | second or later passes when relaxing, we should allow |
| 5237 | padding to shrink. If padding is needed on this pass, it |
| 5238 | will be added back in. */ |
| 5239 | s->padding_statement.size = 0; |
| 5240 | |
| 5241 | /* Make sure output_offset is valid. If relaxation shrinks |
| 5242 | the section and this pad isn't needed, it's possible to |
| 5243 | have output_offset larger than the final size of the |
| 5244 | section. bfd_set_section_contents will complain even for |
| 5245 | a pad size of zero. */ |
| 5246 | s->padding_statement.output_offset |
| 5247 | = dot - output_section_statement->bfd_section->vma; |
| 5248 | break; |
| 5249 | |
| 5250 | case lang_group_statement_enum: |
| 5251 | dot = lang_size_sections_1 (&s->group_statement.children.head, |
| 5252 | output_section_statement, |
| 5253 | fill, dot, relax, check_regions); |
| 5254 | break; |
| 5255 | |
| 5256 | case lang_insert_statement_enum: |
| 5257 | break; |
| 5258 | |
| 5259 | /* We can only get here when relaxing is turned on. */ |
| 5260 | case lang_address_statement_enum: |
| 5261 | break; |
| 5262 | |
| 5263 | default: |
| 5264 | FAIL (); |
| 5265 | break; |
| 5266 | } |
| 5267 | prev = &s->header.next; |
| 5268 | } |
| 5269 | return dot; |
| 5270 | } |
| 5271 | |
| 5272 | /* Callback routine that is used in _bfd_elf_map_sections_to_segments. |
| 5273 | The BFD library has set NEW_SEGMENT to TRUE iff it thinks that |
| 5274 | CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different |
| 5275 | segments. We are allowed an opportunity to override this decision. */ |
| 5276 | |
| 5277 | bfd_boolean |
| 5278 | ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED, |
| 5279 | bfd * abfd ATTRIBUTE_UNUSED, |
| 5280 | asection * current_section, |
| 5281 | asection * previous_section, |
| 5282 | bfd_boolean new_segment) |
| 5283 | { |
| 5284 | lang_output_section_statement_type * cur; |
| 5285 | lang_output_section_statement_type * prev; |
| 5286 | |
| 5287 | /* The checks below are only necessary when the BFD library has decided |
| 5288 | that the two sections ought to be placed into the same segment. */ |
| 5289 | if (new_segment) |
| 5290 | return TRUE; |
| 5291 | |
| 5292 | /* Paranoia checks. */ |
| 5293 | if (current_section == NULL || previous_section == NULL) |
| 5294 | return new_segment; |
| 5295 | |
| 5296 | /* If this flag is set, the target never wants code and non-code |
| 5297 | sections comingled in the same segment. */ |
| 5298 | if (config.separate_code |
| 5299 | && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) |
| 5300 | return TRUE; |
| 5301 | |
| 5302 | /* Find the memory regions associated with the two sections. |
| 5303 | We call lang_output_section_find() here rather than scanning the list |
| 5304 | of output sections looking for a matching section pointer because if |
| 5305 | we have a large number of sections then a hash lookup is faster. */ |
| 5306 | cur = lang_output_section_find (current_section->name); |
| 5307 | prev = lang_output_section_find (previous_section->name); |
| 5308 | |
| 5309 | /* More paranoia. */ |
| 5310 | if (cur == NULL || prev == NULL) |
| 5311 | return new_segment; |
| 5312 | |
| 5313 | /* If the regions are different then force the sections to live in |
| 5314 | different segments. See the email thread starting at the following |
| 5315 | URL for the reasons why this is necessary: |
| 5316 | http://sourceware.org/ml/binutils/2007-02/msg00216.html */ |
| 5317 | return cur->region != prev->region; |
| 5318 | } |
| 5319 | |
| 5320 | void |
| 5321 | one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions) |
| 5322 | { |
| 5323 | lang_statement_iteration++; |
| 5324 | lang_size_sections_1 (&statement_list.head, abs_output_section, |
| 5325 | 0, 0, relax, check_regions); |
| 5326 | } |
| 5327 | |
| 5328 | void |
| 5329 | lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions) |
| 5330 | { |
| 5331 | expld.phase = lang_allocating_phase_enum; |
| 5332 | expld.dataseg.phase = exp_dataseg_none; |
| 5333 | |
| 5334 | one_lang_size_sections_pass (relax, check_regions); |
| 5335 | if (expld.dataseg.phase == exp_dataseg_end_seen |
| 5336 | && link_info.relro && expld.dataseg.relro_end) |
| 5337 | { |
| 5338 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try |
| 5339 | to put expld.dataseg.relro_end on a (common) page boundary. */ |
| 5340 | bfd_vma min_base, relro_end, maxpage; |
| 5341 | |
| 5342 | expld.dataseg.phase = exp_dataseg_relro_adjust; |
| 5343 | maxpage = expld.dataseg.maxpagesize; |
| 5344 | /* MIN_BASE is the absolute minimum address we are allowed to start the |
| 5345 | read-write segment (byte before will be mapped read-only). */ |
| 5346 | min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1); |
| 5347 | expld.dataseg.base += (-expld.dataseg.relro_end |
| 5348 | & (expld.dataseg.pagesize - 1)); |
| 5349 | /* Compute the expected PT_GNU_RELRO segment end. */ |
| 5350 | relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1) |
| 5351 | & ~(expld.dataseg.pagesize - 1)); |
| 5352 | if (min_base + maxpage < expld.dataseg.base) |
| 5353 | { |
| 5354 | expld.dataseg.base -= maxpage; |
| 5355 | relro_end -= maxpage; |
| 5356 | } |
| 5357 | lang_reset_memory_regions (); |
| 5358 | one_lang_size_sections_pass (relax, check_regions); |
| 5359 | if (expld.dataseg.relro_end > relro_end) |
| 5360 | { |
| 5361 | /* The alignment of sections between DATA_SEGMENT_ALIGN |
| 5362 | and DATA_SEGMENT_RELRO_END can cause excessive padding to |
| 5363 | be inserted at DATA_SEGMENT_RELRO_END. Try to start a |
| 5364 | bit lower so that the section alignments will fit in. */ |
| 5365 | asection *sec; |
| 5366 | unsigned int max_alignment_power = 0; |
| 5367 | |
| 5368 | /* Find maximum alignment power of sections between |
| 5369 | DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */ |
| 5370 | for (sec = link_info.output_bfd->sections; sec; sec = sec->next) |
| 5371 | if (sec->vma >= expld.dataseg.base |
| 5372 | && sec->vma < expld.dataseg.relro_end |
| 5373 | && sec->alignment_power > max_alignment_power) |
| 5374 | max_alignment_power = sec->alignment_power; |
| 5375 | |
| 5376 | if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize) |
| 5377 | { |
| 5378 | /* Aligning the adjusted base guarantees the padding |
| 5379 | between sections won't change. This is better than |
| 5380 | simply subtracting 1 << max_alignment_power which is |
| 5381 | what we used to do here. */ |
| 5382 | expld.dataseg.base &= ~((1 << max_alignment_power) - 1); |
| 5383 | lang_reset_memory_regions (); |
| 5384 | one_lang_size_sections_pass (relax, check_regions); |
| 5385 | } |
| 5386 | } |
| 5387 | link_info.relro_start = expld.dataseg.base; |
| 5388 | link_info.relro_end = expld.dataseg.relro_end; |
| 5389 | } |
| 5390 | else if (expld.dataseg.phase == exp_dataseg_end_seen) |
| 5391 | { |
| 5392 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether |
| 5393 | a page could be saved in the data segment. */ |
| 5394 | bfd_vma first, last; |
| 5395 | |
| 5396 | first = -expld.dataseg.base & (expld.dataseg.pagesize - 1); |
| 5397 | last = expld.dataseg.end & (expld.dataseg.pagesize - 1); |
| 5398 | if (first && last |
| 5399 | && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1)) |
| 5400 | != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1))) |
| 5401 | && first + last <= expld.dataseg.pagesize) |
| 5402 | { |
| 5403 | expld.dataseg.phase = exp_dataseg_adjust; |
| 5404 | lang_reset_memory_regions (); |
| 5405 | one_lang_size_sections_pass (relax, check_regions); |
| 5406 | } |
| 5407 | else |
| 5408 | expld.dataseg.phase = exp_dataseg_done; |
| 5409 | } |
| 5410 | else |
| 5411 | expld.dataseg.phase = exp_dataseg_done; |
| 5412 | } |
| 5413 | |
| 5414 | static lang_output_section_statement_type *current_section; |
| 5415 | static lang_assignment_statement_type *current_assign; |
| 5416 | static bfd_boolean prefer_next_section; |
| 5417 | |
| 5418 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
| 5419 | |
| 5420 | static bfd_vma |
| 5421 | lang_do_assignments_1 (lang_statement_union_type *s, |
| 5422 | lang_output_section_statement_type *current_os, |
| 5423 | fill_type *fill, |
| 5424 | bfd_vma dot, |
| 5425 | bfd_boolean *found_end) |
| 5426 | { |
| 5427 | for (; s != NULL; s = s->header.next) |
| 5428 | { |
| 5429 | switch (s->header.type) |
| 5430 | { |
| 5431 | case lang_constructors_statement_enum: |
| 5432 | dot = lang_do_assignments_1 (constructor_list.head, |
| 5433 | current_os, fill, dot, found_end); |
| 5434 | break; |
| 5435 | |
| 5436 | case lang_output_section_statement_enum: |
| 5437 | { |
| 5438 | lang_output_section_statement_type *os; |
| 5439 | |
| 5440 | os = &(s->output_section_statement); |
| 5441 | os->after_end = *found_end; |
| 5442 | if (os->bfd_section != NULL && !os->ignored) |
| 5443 | { |
| 5444 | if ((os->bfd_section->flags & SEC_ALLOC) != 0) |
| 5445 | { |
| 5446 | current_section = os; |
| 5447 | prefer_next_section = FALSE; |
| 5448 | } |
| 5449 | dot = os->bfd_section->vma; |
| 5450 | |
| 5451 | lang_do_assignments_1 (os->children.head, |
| 5452 | os, os->fill, dot, found_end); |
| 5453 | |
| 5454 | /* .tbss sections effectively have zero size. */ |
| 5455 | if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0 |
| 5456 | || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0 |
| 5457 | || link_info.relocatable) |
| 5458 | dot += TO_ADDR (os->bfd_section->size); |
| 5459 | |
| 5460 | if (os->update_dot_tree != NULL) |
| 5461 | exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot); |
| 5462 | } |
| 5463 | } |
| 5464 | break; |
| 5465 | |
| 5466 | case lang_wild_statement_enum: |
| 5467 | |
| 5468 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
| 5469 | current_os, fill, dot, found_end); |
| 5470 | break; |
| 5471 | |
| 5472 | case lang_object_symbols_statement_enum: |
| 5473 | case lang_output_statement_enum: |
| 5474 | case lang_target_statement_enum: |
| 5475 | break; |
| 5476 | |
| 5477 | case lang_data_statement_enum: |
| 5478 | exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot); |
| 5479 | if (expld.result.valid_p) |
| 5480 | { |
| 5481 | s->data_statement.value = expld.result.value; |
| 5482 | if (expld.result.section != NULL) |
| 5483 | s->data_statement.value += expld.result.section->vma; |
| 5484 | } |
| 5485 | else |
| 5486 | einfo (_("%F%P: invalid data statement\n")); |
| 5487 | { |
| 5488 | unsigned int size; |
| 5489 | switch (s->data_statement.type) |
| 5490 | { |
| 5491 | default: |
| 5492 | abort (); |
| 5493 | case QUAD: |
| 5494 | case SQUAD: |
| 5495 | size = QUAD_SIZE; |
| 5496 | break; |
| 5497 | case LONG: |
| 5498 | size = LONG_SIZE; |
| 5499 | break; |
| 5500 | case SHORT: |
| 5501 | size = SHORT_SIZE; |
| 5502 | break; |
| 5503 | case BYTE: |
| 5504 | size = BYTE_SIZE; |
| 5505 | break; |
| 5506 | } |
| 5507 | if (size < TO_SIZE ((unsigned) 1)) |
| 5508 | size = TO_SIZE ((unsigned) 1); |
| 5509 | dot += TO_ADDR (size); |
| 5510 | } |
| 5511 | break; |
| 5512 | |
| 5513 | case lang_reloc_statement_enum: |
| 5514 | exp_fold_tree (s->reloc_statement.addend_exp, |
| 5515 | bfd_abs_section_ptr, &dot); |
| 5516 | if (expld.result.valid_p) |
| 5517 | s->reloc_statement.addend_value = expld.result.value; |
| 5518 | else |
| 5519 | einfo (_("%F%P: invalid reloc statement\n")); |
| 5520 | dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); |
| 5521 | break; |
| 5522 | |
| 5523 | case lang_input_section_enum: |
| 5524 | { |
| 5525 | asection *in = s->input_section.section; |
| 5526 | |
| 5527 | if ((in->flags & SEC_EXCLUDE) == 0) |
| 5528 | dot += TO_ADDR (in->size); |
| 5529 | } |
| 5530 | break; |
| 5531 | |
| 5532 | case lang_input_statement_enum: |
| 5533 | break; |
| 5534 | |
| 5535 | case lang_fill_statement_enum: |
| 5536 | fill = s->fill_statement.fill; |
| 5537 | break; |
| 5538 | |
| 5539 | case lang_assignment_statement_enum: |
| 5540 | current_assign = &s->assignment_statement; |
| 5541 | if (current_assign->exp->type.node_class != etree_assert) |
| 5542 | { |
| 5543 | const char *p = current_assign->exp->assign.dst; |
| 5544 | |
| 5545 | if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) |
| 5546 | prefer_next_section = TRUE; |
| 5547 | |
| 5548 | while (*p == '_') |
| 5549 | ++p; |
| 5550 | if (strcmp (p, "end") == 0) |
| 5551 | *found_end = TRUE; |
| 5552 | } |
| 5553 | exp_fold_tree (s->assignment_statement.exp, |
| 5554 | current_os->bfd_section, |
| 5555 | &dot); |
| 5556 | break; |
| 5557 | |
| 5558 | case lang_padding_statement_enum: |
| 5559 | dot += TO_ADDR (s->padding_statement.size); |
| 5560 | break; |
| 5561 | |
| 5562 | case lang_group_statement_enum: |
| 5563 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
| 5564 | current_os, fill, dot, found_end); |
| 5565 | break; |
| 5566 | |
| 5567 | case lang_insert_statement_enum: |
| 5568 | break; |
| 5569 | |
| 5570 | case lang_address_statement_enum: |
| 5571 | break; |
| 5572 | |
| 5573 | default: |
| 5574 | FAIL (); |
| 5575 | break; |
| 5576 | } |
| 5577 | } |
| 5578 | return dot; |
| 5579 | } |
| 5580 | |
| 5581 | void |
| 5582 | lang_do_assignments (lang_phase_type phase) |
| 5583 | { |
| 5584 | bfd_boolean found_end = FALSE; |
| 5585 | |
| 5586 | current_section = NULL; |
| 5587 | prefer_next_section = FALSE; |
| 5588 | expld.phase = phase; |
| 5589 | lang_statement_iteration++; |
| 5590 | lang_do_assignments_1 (statement_list.head, |
| 5591 | abs_output_section, NULL, 0, &found_end); |
| 5592 | } |
| 5593 | |
| 5594 | /* For an assignment statement outside of an output section statement, |
| 5595 | choose the best of neighbouring output sections to use for values |
| 5596 | of "dot". */ |
| 5597 | |
| 5598 | asection * |
| 5599 | section_for_dot (void) |
| 5600 | { |
| 5601 | asection *s; |
| 5602 | |
| 5603 | /* Assignments belong to the previous output section, unless there |
| 5604 | has been an assignment to "dot", in which case following |
| 5605 | assignments belong to the next output section. (The assumption |
| 5606 | is that an assignment to "dot" is setting up the address for the |
| 5607 | next output section.) Except that past the assignment to "_end" |
| 5608 | we always associate with the previous section. This exception is |
| 5609 | for targets like SH that define an alloc .stack or other |
| 5610 | weirdness after non-alloc sections. */ |
| 5611 | if (current_section == NULL || prefer_next_section) |
| 5612 | { |
| 5613 | lang_statement_union_type *stmt; |
| 5614 | lang_output_section_statement_type *os; |
| 5615 | |
| 5616 | for (stmt = (lang_statement_union_type *) current_assign; |
| 5617 | stmt != NULL; |
| 5618 | stmt = stmt->header.next) |
| 5619 | if (stmt->header.type == lang_output_section_statement_enum) |
| 5620 | break; |
| 5621 | |
| 5622 | os = &stmt->output_section_statement; |
| 5623 | while (os != NULL |
| 5624 | && !os->after_end |
| 5625 | && (os->bfd_section == NULL |
| 5626 | || (os->bfd_section->flags & SEC_EXCLUDE) != 0 |
| 5627 | || bfd_section_removed_from_list (link_info.output_bfd, |
| 5628 | os->bfd_section))) |
| 5629 | os = os->next; |
| 5630 | |
| 5631 | if (current_section == NULL || os == NULL || !os->after_end) |
| 5632 | { |
| 5633 | if (os != NULL) |
| 5634 | s = os->bfd_section; |
| 5635 | else |
| 5636 | s = link_info.output_bfd->section_last; |
| 5637 | while (s != NULL |
| 5638 | && ((s->flags & SEC_ALLOC) == 0 |
| 5639 | || (s->flags & SEC_THREAD_LOCAL) != 0)) |
| 5640 | s = s->prev; |
| 5641 | if (s != NULL) |
| 5642 | return s; |
| 5643 | |
| 5644 | return bfd_abs_section_ptr; |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | s = current_section->bfd_section; |
| 5649 | |
| 5650 | /* The section may have been stripped. */ |
| 5651 | while (s != NULL |
| 5652 | && ((s->flags & SEC_EXCLUDE) != 0 |
| 5653 | || (s->flags & SEC_ALLOC) == 0 |
| 5654 | || (s->flags & SEC_THREAD_LOCAL) != 0 |
| 5655 | || bfd_section_removed_from_list (link_info.output_bfd, s))) |
| 5656 | s = s->prev; |
| 5657 | if (s == NULL) |
| 5658 | s = link_info.output_bfd->sections; |
| 5659 | while (s != NULL |
| 5660 | && ((s->flags & SEC_ALLOC) == 0 |
| 5661 | || (s->flags & SEC_THREAD_LOCAL) != 0)) |
| 5662 | s = s->next; |
| 5663 | if (s != NULL) |
| 5664 | return s; |
| 5665 | |
| 5666 | return bfd_abs_section_ptr; |
| 5667 | } |
| 5668 | |
| 5669 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
| 5670 | operator .startof. (section_name), it produces an undefined symbol |
| 5671 | .startof.section_name. Similarly, when it sees |
| 5672 | .sizeof. (section_name), it produces an undefined symbol |
| 5673 | .sizeof.section_name. For all the output sections, we look for |
| 5674 | such symbols, and set them to the correct value. */ |
| 5675 | |
| 5676 | static void |
| 5677 | lang_set_startof (void) |
| 5678 | { |
| 5679 | asection *s; |
| 5680 | |
| 5681 | if (link_info.relocatable) |
| 5682 | return; |
| 5683 | |
| 5684 | for (s = link_info.output_bfd->sections; s != NULL; s = s->next) |
| 5685 | { |
| 5686 | const char *secname; |
| 5687 | char *buf; |
| 5688 | struct bfd_link_hash_entry *h; |
| 5689 | |
| 5690 | secname = bfd_get_section_name (link_info.output_bfd, s); |
| 5691 | buf = (char *) xmalloc (10 + strlen (secname)); |
| 5692 | |
| 5693 | sprintf (buf, ".startof.%s", secname); |
| 5694 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
| 5695 | if (h != NULL && h->type == bfd_link_hash_undefined) |
| 5696 | { |
| 5697 | h->type = bfd_link_hash_defined; |
| 5698 | h->u.def.value = 0; |
| 5699 | h->u.def.section = s; |
| 5700 | } |
| 5701 | |
| 5702 | sprintf (buf, ".sizeof.%s", secname); |
| 5703 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
| 5704 | if (h != NULL && h->type == bfd_link_hash_undefined) |
| 5705 | { |
| 5706 | h->type = bfd_link_hash_defined; |
| 5707 | h->u.def.value = TO_ADDR (s->size); |
| 5708 | h->u.def.section = bfd_abs_section_ptr; |
| 5709 | } |
| 5710 | |
| 5711 | free (buf); |
| 5712 | } |
| 5713 | } |
| 5714 | |
| 5715 | static void |
| 5716 | lang_end (void) |
| 5717 | { |
| 5718 | struct bfd_link_hash_entry *h; |
| 5719 | bfd_boolean warn; |
| 5720 | |
| 5721 | if ((link_info.relocatable && !link_info.gc_sections) |
| 5722 | || (link_info.shared && !link_info.executable)) |
| 5723 | warn = entry_from_cmdline; |
| 5724 | else |
| 5725 | warn = TRUE; |
| 5726 | |
| 5727 | /* Force the user to specify a root when generating a relocatable with |
| 5728 | --gc-sections. */ |
| 5729 | if (link_info.gc_sections && link_info.relocatable |
| 5730 | && !(entry_from_cmdline || undef_from_cmdline)) |
| 5731 | einfo (_("%P%F: gc-sections requires either an entry or " |
| 5732 | "an undefined symbol\n")); |
| 5733 | |
| 5734 | if (entry_symbol.name == NULL) |
| 5735 | { |
| 5736 | /* No entry has been specified. Look for the default entry, but |
| 5737 | don't warn if we don't find it. */ |
| 5738 | entry_symbol.name = entry_symbol_default; |
| 5739 | warn = FALSE; |
| 5740 | } |
| 5741 | |
| 5742 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
| 5743 | FALSE, FALSE, TRUE); |
| 5744 | if (h != NULL |
| 5745 | && (h->type == bfd_link_hash_defined |
| 5746 | || h->type == bfd_link_hash_defweak) |
| 5747 | && h->u.def.section->output_section != NULL) |
| 5748 | { |
| 5749 | bfd_vma val; |
| 5750 | |
| 5751 | val = (h->u.def.value |
| 5752 | + bfd_get_section_vma (link_info.output_bfd, |
| 5753 | h->u.def.section->output_section) |
| 5754 | + h->u.def.section->output_offset); |
| 5755 | if (! bfd_set_start_address (link_info.output_bfd, val)) |
| 5756 | einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name); |
| 5757 | } |
| 5758 | else |
| 5759 | { |
| 5760 | bfd_vma val; |
| 5761 | const char *send; |
| 5762 | |
| 5763 | /* We couldn't find the entry symbol. Try parsing it as a |
| 5764 | number. */ |
| 5765 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
| 5766 | if (*send == '\0') |
| 5767 | { |
| 5768 | if (! bfd_set_start_address (link_info.output_bfd, val)) |
| 5769 | einfo (_("%P%F: can't set start address\n")); |
| 5770 | } |
| 5771 | else |
| 5772 | { |
| 5773 | asection *ts; |
| 5774 | |
| 5775 | /* Can't find the entry symbol, and it's not a number. Use |
| 5776 | the first address in the text section. */ |
| 5777 | ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); |
| 5778 | if (ts != NULL) |
| 5779 | { |
| 5780 | if (warn) |
| 5781 | einfo (_("%P: warning: cannot find entry symbol %s;" |
| 5782 | " defaulting to %V\n"), |
| 5783 | entry_symbol.name, |
| 5784 | bfd_get_section_vma (link_info.output_bfd, ts)); |
| 5785 | if (!(bfd_set_start_address |
| 5786 | (link_info.output_bfd, |
| 5787 | bfd_get_section_vma (link_info.output_bfd, ts)))) |
| 5788 | einfo (_("%P%F: can't set start address\n")); |
| 5789 | } |
| 5790 | else |
| 5791 | { |
| 5792 | if (warn) |
| 5793 | einfo (_("%P: warning: cannot find entry symbol %s;" |
| 5794 | " not setting start address\n"), |
| 5795 | entry_symbol.name); |
| 5796 | } |
| 5797 | } |
| 5798 | } |
| 5799 | } |
| 5800 | |
| 5801 | /* This is a small function used when we want to ignore errors from |
| 5802 | BFD. */ |
| 5803 | |
| 5804 | static void |
| 5805 | ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) |
| 5806 | { |
| 5807 | /* Don't do anything. */ |
| 5808 | } |
| 5809 | |
| 5810 | /* Check that the architecture of all the input files is compatible |
| 5811 | with the output file. Also call the backend to let it do any |
| 5812 | other checking that is needed. */ |
| 5813 | |
| 5814 | static void |
| 5815 | lang_check (void) |
| 5816 | { |
| 5817 | lang_statement_union_type *file; |
| 5818 | bfd *input_bfd; |
| 5819 | const bfd_arch_info_type *compatible; |
| 5820 | |
| 5821 | for (file = file_chain.head; file != NULL; file = file->input_statement.next) |
| 5822 | { |
| 5823 | #ifdef ENABLE_PLUGINS |
| 5824 | /* Don't check format of files claimed by plugin. */ |
| 5825 | if (file->input_statement.flags.claimed) |
| 5826 | continue; |
| 5827 | #endif /* ENABLE_PLUGINS */ |
| 5828 | input_bfd = file->input_statement.the_bfd; |
| 5829 | compatible |
| 5830 | = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, |
| 5831 | command_line.accept_unknown_input_arch); |
| 5832 | |
| 5833 | /* In general it is not possible to perform a relocatable |
| 5834 | link between differing object formats when the input |
| 5835 | file has relocations, because the relocations in the |
| 5836 | input format may not have equivalent representations in |
| 5837 | the output format (and besides BFD does not translate |
| 5838 | relocs for other link purposes than a final link). */ |
| 5839 | if ((link_info.relocatable || link_info.emitrelocations) |
| 5840 | && (compatible == NULL |
| 5841 | || (bfd_get_flavour (input_bfd) |
| 5842 | != bfd_get_flavour (link_info.output_bfd))) |
| 5843 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
| 5844 | { |
| 5845 | einfo (_("%P%F: Relocatable linking with relocations from" |
| 5846 | " format %s (%B) to format %s (%B) is not supported\n"), |
| 5847 | bfd_get_target (input_bfd), input_bfd, |
| 5848 | bfd_get_target (link_info.output_bfd), link_info.output_bfd); |
| 5849 | /* einfo with %F exits. */ |
| 5850 | } |
| 5851 | |
| 5852 | if (compatible == NULL) |
| 5853 | { |
| 5854 | if (command_line.warn_mismatch) |
| 5855 | einfo (_("%P%X: %s architecture of input file `%B'" |
| 5856 | " is incompatible with %s output\n"), |
| 5857 | bfd_printable_name (input_bfd), input_bfd, |
| 5858 | bfd_printable_name (link_info.output_bfd)); |
| 5859 | } |
| 5860 | else if (bfd_count_sections (input_bfd)) |
| 5861 | { |
| 5862 | /* If the input bfd has no contents, it shouldn't set the |
| 5863 | private data of the output bfd. */ |
| 5864 | |
| 5865 | bfd_error_handler_type pfn = NULL; |
| 5866 | |
| 5867 | /* If we aren't supposed to warn about mismatched input |
| 5868 | files, temporarily set the BFD error handler to a |
| 5869 | function which will do nothing. We still want to call |
| 5870 | bfd_merge_private_bfd_data, since it may set up |
| 5871 | information which is needed in the output file. */ |
| 5872 | if (! command_line.warn_mismatch) |
| 5873 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
| 5874 | if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd)) |
| 5875 | { |
| 5876 | if (command_line.warn_mismatch) |
| 5877 | einfo (_("%P%X: failed to merge target specific data" |
| 5878 | " of file %B\n"), input_bfd); |
| 5879 | } |
| 5880 | if (! command_line.warn_mismatch) |
| 5881 | bfd_set_error_handler (pfn); |
| 5882 | } |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | /* Look through all the global common symbols and attach them to the |
| 5887 | correct section. The -sort-common command line switch may be used |
| 5888 | to roughly sort the entries by alignment. */ |
| 5889 | |
| 5890 | static void |
| 5891 | lang_common (void) |
| 5892 | { |
| 5893 | if (command_line.inhibit_common_definition) |
| 5894 | return; |
| 5895 | if (link_info.relocatable |
| 5896 | && ! command_line.force_common_definition) |
| 5897 | return; |
| 5898 | |
| 5899 | if (! config.sort_common) |
| 5900 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
| 5901 | else |
| 5902 | { |
| 5903 | unsigned int power; |
| 5904 | |
| 5905 | if (config.sort_common == sort_descending) |
| 5906 | { |
| 5907 | for (power = 4; power > 0; power--) |
| 5908 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
| 5909 | |
| 5910 | power = 0; |
| 5911 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
| 5912 | } |
| 5913 | else |
| 5914 | { |
| 5915 | for (power = 0; power <= 4; power++) |
| 5916 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
| 5917 | |
| 5918 | power = UINT_MAX; |
| 5919 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
| 5920 | } |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | /* Place one common symbol in the correct section. */ |
| 5925 | |
| 5926 | static bfd_boolean |
| 5927 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
| 5928 | { |
| 5929 | unsigned int power_of_two; |
| 5930 | bfd_vma size; |
| 5931 | asection *section; |
| 5932 | |
| 5933 | if (h->type != bfd_link_hash_common) |
| 5934 | return TRUE; |
| 5935 | |
| 5936 | size = h->u.c.size; |
| 5937 | power_of_two = h->u.c.p->alignment_power; |
| 5938 | |
| 5939 | if (config.sort_common == sort_descending |
| 5940 | && power_of_two < *(unsigned int *) info) |
| 5941 | return TRUE; |
| 5942 | else if (config.sort_common == sort_ascending |
| 5943 | && power_of_two > *(unsigned int *) info) |
| 5944 | return TRUE; |
| 5945 | |
| 5946 | section = h->u.c.p->section; |
| 5947 | if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) |
| 5948 | einfo (_("%P%F: Could not define common symbol `%T': %E\n"), |
| 5949 | h->root.string); |
| 5950 | |
| 5951 | if (config.map_file != NULL) |
| 5952 | { |
| 5953 | static bfd_boolean header_printed; |
| 5954 | int len; |
| 5955 | char *name; |
| 5956 | char buf[50]; |
| 5957 | |
| 5958 | if (! header_printed) |
| 5959 | { |
| 5960 | minfo (_("\nAllocating common symbols\n")); |
| 5961 | minfo (_("Common symbol size file\n\n")); |
| 5962 | header_printed = TRUE; |
| 5963 | } |
| 5964 | |
| 5965 | name = bfd_demangle (link_info.output_bfd, h->root.string, |
| 5966 | DMGL_ANSI | DMGL_PARAMS); |
| 5967 | if (name == NULL) |
| 5968 | { |
| 5969 | minfo ("%s", h->root.string); |
| 5970 | len = strlen (h->root.string); |
| 5971 | } |
| 5972 | else |
| 5973 | { |
| 5974 | minfo ("%s", name); |
| 5975 | len = strlen (name); |
| 5976 | free (name); |
| 5977 | } |
| 5978 | |
| 5979 | if (len >= 19) |
| 5980 | { |
| 5981 | print_nl (); |
| 5982 | len = 0; |
| 5983 | } |
| 5984 | while (len < 20) |
| 5985 | { |
| 5986 | print_space (); |
| 5987 | ++len; |
| 5988 | } |
| 5989 | |
| 5990 | minfo ("0x"); |
| 5991 | if (size <= 0xffffffff) |
| 5992 | sprintf (buf, "%lx", (unsigned long) size); |
| 5993 | else |
| 5994 | sprintf_vma (buf, size); |
| 5995 | minfo ("%s", buf); |
| 5996 | len = strlen (buf); |
| 5997 | |
| 5998 | while (len < 16) |
| 5999 | { |
| 6000 | print_space (); |
| 6001 | ++len; |
| 6002 | } |
| 6003 | |
| 6004 | minfo ("%B\n", section->owner); |
| 6005 | } |
| 6006 | |
| 6007 | return TRUE; |
| 6008 | } |
| 6009 | |
| 6010 | /* Run through the input files and ensure that every input section has |
| 6011 | somewhere to go. If one is found without a destination then create |
| 6012 | an input request and place it into the statement tree. */ |
| 6013 | |
| 6014 | static void |
| 6015 | lang_place_orphans (void) |
| 6016 | { |
| 6017 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
| 6018 | { |
| 6019 | asection *s; |
| 6020 | |
| 6021 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 6022 | { |
| 6023 | if (s->output_section == NULL) |
| 6024 | { |
| 6025 | /* This section of the file is not attached, root |
| 6026 | around for a sensible place for it to go. */ |
| 6027 | |
| 6028 | if (file->flags.just_syms) |
| 6029 | bfd_link_just_syms (file->the_bfd, s, &link_info); |
| 6030 | else if ((s->flags & SEC_EXCLUDE) != 0) |
| 6031 | s->output_section = bfd_abs_section_ptr; |
| 6032 | else if (strcmp (s->name, "COMMON") == 0) |
| 6033 | { |
| 6034 | /* This is a lonely common section which must have |
| 6035 | come from an archive. We attach to the section |
| 6036 | with the wildcard. */ |
| 6037 | if (! link_info.relocatable |
| 6038 | || command_line.force_common_definition) |
| 6039 | { |
| 6040 | if (default_common_section == NULL) |
| 6041 | default_common_section |
| 6042 | = lang_output_section_statement_lookup (".bss", 0, |
| 6043 | TRUE); |
| 6044 | lang_add_section (&default_common_section->children, s, |
| 6045 | NULL, default_common_section); |
| 6046 | } |
| 6047 | } |
| 6048 | else |
| 6049 | { |
| 6050 | const char *name = s->name; |
| 6051 | int constraint = 0; |
| 6052 | |
| 6053 | if (config.unique_orphan_sections |
| 6054 | || unique_section_p (s, NULL)) |
| 6055 | constraint = SPECIAL; |
| 6056 | |
| 6057 | if (!ldemul_place_orphan (s, name, constraint)) |
| 6058 | { |
| 6059 | lang_output_section_statement_type *os; |
| 6060 | os = lang_output_section_statement_lookup (name, |
| 6061 | constraint, |
| 6062 | TRUE); |
| 6063 | if (os->addr_tree == NULL |
| 6064 | && (link_info.relocatable |
| 6065 | || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) |
| 6066 | os->addr_tree = exp_intop (0); |
| 6067 | lang_add_section (&os->children, s, NULL, os); |
| 6068 | } |
| 6069 | } |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | void |
| 6076 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
| 6077 | { |
| 6078 | flagword *ptr_flags; |
| 6079 | |
| 6080 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
| 6081 | while (*flags) |
| 6082 | { |
| 6083 | switch (*flags) |
| 6084 | { |
| 6085 | case 'A': case 'a': |
| 6086 | *ptr_flags |= SEC_ALLOC; |
| 6087 | break; |
| 6088 | |
| 6089 | case 'R': case 'r': |
| 6090 | *ptr_flags |= SEC_READONLY; |
| 6091 | break; |
| 6092 | |
| 6093 | case 'W': case 'w': |
| 6094 | *ptr_flags |= SEC_DATA; |
| 6095 | break; |
| 6096 | |
| 6097 | case 'X': case 'x': |
| 6098 | *ptr_flags |= SEC_CODE; |
| 6099 | break; |
| 6100 | |
| 6101 | case 'L': case 'l': |
| 6102 | case 'I': case 'i': |
| 6103 | *ptr_flags |= SEC_LOAD; |
| 6104 | break; |
| 6105 | |
| 6106 | default: |
| 6107 | einfo (_("%P%F: invalid syntax in flags\n")); |
| 6108 | break; |
| 6109 | } |
| 6110 | flags++; |
| 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | /* Call a function on each input file. This function will be called |
| 6115 | on an archive, but not on the elements. */ |
| 6116 | |
| 6117 | void |
| 6118 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
| 6119 | { |
| 6120 | lang_input_statement_type *f; |
| 6121 | |
| 6122 | for (f = (lang_input_statement_type *) input_file_chain.head; |
| 6123 | f != NULL; |
| 6124 | f = (lang_input_statement_type *) f->next_real_file) |
| 6125 | func (f); |
| 6126 | } |
| 6127 | |
| 6128 | /* Call a function on each file. The function will be called on all |
| 6129 | the elements of an archive which are included in the link, but will |
| 6130 | not be called on the archive file itself. */ |
| 6131 | |
| 6132 | void |
| 6133 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
| 6134 | { |
| 6135 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 6136 | { |
| 6137 | func (f); |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | void |
| 6142 | ldlang_add_file (lang_input_statement_type *entry) |
| 6143 | { |
| 6144 | lang_statement_append (&file_chain, |
| 6145 | (lang_statement_union_type *) entry, |
| 6146 | &entry->next); |
| 6147 | |
| 6148 | /* The BFD linker needs to have a list of all input BFDs involved in |
| 6149 | a link. */ |
| 6150 | ASSERT (entry->the_bfd->link_next == NULL); |
| 6151 | ASSERT (entry->the_bfd != link_info.output_bfd); |
| 6152 | |
| 6153 | *link_info.input_bfds_tail = entry->the_bfd; |
| 6154 | link_info.input_bfds_tail = &entry->the_bfd->link_next; |
| 6155 | entry->the_bfd->usrdata = entry; |
| 6156 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
| 6157 | |
| 6158 | /* Look through the sections and check for any which should not be |
| 6159 | included in the link. We need to do this now, so that we can |
| 6160 | notice when the backend linker tries to report multiple |
| 6161 | definition errors for symbols which are in sections we aren't |
| 6162 | going to link. FIXME: It might be better to entirely ignore |
| 6163 | symbols which are defined in sections which are going to be |
| 6164 | discarded. This would require modifying the backend linker for |
| 6165 | each backend which might set the SEC_LINK_ONCE flag. If we do |
| 6166 | this, we should probably handle SEC_EXCLUDE in the same way. */ |
| 6167 | |
| 6168 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
| 6169 | } |
| 6170 | |
| 6171 | void |
| 6172 | lang_add_output (const char *name, int from_script) |
| 6173 | { |
| 6174 | /* Make -o on command line override OUTPUT in script. */ |
| 6175 | if (!had_output_filename || !from_script) |
| 6176 | { |
| 6177 | output_filename = name; |
| 6178 | had_output_filename = TRUE; |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | static int |
| 6183 | topower (int x) |
| 6184 | { |
| 6185 | unsigned int i = 1; |
| 6186 | int l; |
| 6187 | |
| 6188 | if (x < 0) |
| 6189 | return -1; |
| 6190 | |
| 6191 | for (l = 0; l < 32; l++) |
| 6192 | { |
| 6193 | if (i >= (unsigned int) x) |
| 6194 | return l; |
| 6195 | i <<= 1; |
| 6196 | } |
| 6197 | |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
| 6201 | lang_output_section_statement_type * |
| 6202 | lang_enter_output_section_statement (const char *output_section_statement_name, |
| 6203 | etree_type *address_exp, |
| 6204 | enum section_type sectype, |
| 6205 | etree_type *align, |
| 6206 | etree_type *subalign, |
| 6207 | etree_type *ebase, |
| 6208 | int constraint, |
| 6209 | int align_with_input) |
| 6210 | { |
| 6211 | lang_output_section_statement_type *os; |
| 6212 | |
| 6213 | os = lang_output_section_statement_lookup (output_section_statement_name, |
| 6214 | constraint, TRUE); |
| 6215 | current_section = os; |
| 6216 | |
| 6217 | if (os->addr_tree == NULL) |
| 6218 | { |
| 6219 | os->addr_tree = address_exp; |
| 6220 | } |
| 6221 | os->sectype = sectype; |
| 6222 | if (sectype != noload_section) |
| 6223 | os->flags = SEC_NO_FLAGS; |
| 6224 | else |
| 6225 | os->flags = SEC_NEVER_LOAD; |
| 6226 | os->block_value = 1; |
| 6227 | |
| 6228 | /* Make next things chain into subchain of this. */ |
| 6229 | push_stat_ptr (&os->children); |
| 6230 | |
| 6231 | os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; |
| 6232 | if (os->align_lma_with_input && align != NULL) |
| 6233 | einfo (_("%F%P:%S: error: align with input and explicit align specified\n"), NULL); |
| 6234 | |
| 6235 | os->subsection_alignment = |
| 6236 | topower (exp_get_value_int (subalign, -1, "subsection alignment")); |
| 6237 | os->section_alignment = |
| 6238 | topower (exp_get_value_int (align, -1, "section alignment")); |
| 6239 | |
| 6240 | os->load_base = ebase; |
| 6241 | return os; |
| 6242 | } |
| 6243 | |
| 6244 | void |
| 6245 | lang_final (void) |
| 6246 | { |
| 6247 | lang_output_statement_type *new_stmt; |
| 6248 | |
| 6249 | new_stmt = new_stat (lang_output_statement, stat_ptr); |
| 6250 | new_stmt->name = output_filename; |
| 6251 | |
| 6252 | } |
| 6253 | |
| 6254 | /* Reset the current counters in the regions. */ |
| 6255 | |
| 6256 | void |
| 6257 | lang_reset_memory_regions (void) |
| 6258 | { |
| 6259 | lang_memory_region_type *p = lang_memory_region_list; |
| 6260 | asection *o; |
| 6261 | lang_output_section_statement_type *os; |
| 6262 | |
| 6263 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
| 6264 | { |
| 6265 | p->current = p->origin; |
| 6266 | p->last_os = NULL; |
| 6267 | } |
| 6268 | |
| 6269 | for (os = &lang_output_section_statement.head->output_section_statement; |
| 6270 | os != NULL; |
| 6271 | os = os->next) |
| 6272 | { |
| 6273 | os->processed_vma = FALSE; |
| 6274 | os->processed_lma = FALSE; |
| 6275 | } |
| 6276 | |
| 6277 | for (o = link_info.output_bfd->sections; o != NULL; o = o->next) |
| 6278 | { |
| 6279 | /* Save the last size for possible use by bfd_relax_section. */ |
| 6280 | o->rawsize = o->size; |
| 6281 | o->size = 0; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | /* Worker for lang_gc_sections_1. */ |
| 6286 | |
| 6287 | static void |
| 6288 | gc_section_callback (lang_wild_statement_type *ptr, |
| 6289 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
| 6290 | asection *section, |
| 6291 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
| 6292 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
| 6293 | void *data ATTRIBUTE_UNUSED) |
| 6294 | { |
| 6295 | /* If the wild pattern was marked KEEP, the member sections |
| 6296 | should be as well. */ |
| 6297 | if (ptr->keep_sections) |
| 6298 | section->flags |= SEC_KEEP; |
| 6299 | } |
| 6300 | |
| 6301 | /* Iterate over sections marking them against GC. */ |
| 6302 | |
| 6303 | static void |
| 6304 | lang_gc_sections_1 (lang_statement_union_type *s) |
| 6305 | { |
| 6306 | for (; s != NULL; s = s->header.next) |
| 6307 | { |
| 6308 | switch (s->header.type) |
| 6309 | { |
| 6310 | case lang_wild_statement_enum: |
| 6311 | walk_wild (&s->wild_statement, gc_section_callback, NULL); |
| 6312 | break; |
| 6313 | case lang_constructors_statement_enum: |
| 6314 | lang_gc_sections_1 (constructor_list.head); |
| 6315 | break; |
| 6316 | case lang_output_section_statement_enum: |
| 6317 | lang_gc_sections_1 (s->output_section_statement.children.head); |
| 6318 | break; |
| 6319 | case lang_group_statement_enum: |
| 6320 | lang_gc_sections_1 (s->group_statement.children.head); |
| 6321 | break; |
| 6322 | default: |
| 6323 | break; |
| 6324 | } |
| 6325 | } |
| 6326 | } |
| 6327 | |
| 6328 | static void |
| 6329 | lang_gc_sections (void) |
| 6330 | { |
| 6331 | /* Keep all sections so marked in the link script. */ |
| 6332 | |
| 6333 | lang_gc_sections_1 (statement_list.head); |
| 6334 | |
| 6335 | /* SEC_EXCLUDE is ignored when doing a relocatable link, except in |
| 6336 | the special case of debug info. (See bfd/stabs.c) |
| 6337 | Twiddle the flag here, to simplify later linker code. */ |
| 6338 | if (link_info.relocatable) |
| 6339 | { |
| 6340 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 6341 | { |
| 6342 | asection *sec; |
| 6343 | #ifdef ENABLE_PLUGINS |
| 6344 | if (f->flags.claimed) |
| 6345 | continue; |
| 6346 | #endif |
| 6347 | for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) |
| 6348 | if ((sec->flags & SEC_DEBUGGING) == 0) |
| 6349 | sec->flags &= ~SEC_EXCLUDE; |
| 6350 | } |
| 6351 | } |
| 6352 | |
| 6353 | if (link_info.gc_sections) |
| 6354 | bfd_gc_sections (link_info.output_bfd, &link_info); |
| 6355 | } |
| 6356 | |
| 6357 | /* Worker for lang_find_relro_sections_1. */ |
| 6358 | |
| 6359 | static void |
| 6360 | find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, |
| 6361 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
| 6362 | asection *section, |
| 6363 | struct flag_info *sflag_info ATTRIBUTE_UNUSED, |
| 6364 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
| 6365 | void *data) |
| 6366 | { |
| 6367 | /* Discarded, excluded and ignored sections effectively have zero |
| 6368 | size. */ |
| 6369 | if (section->output_section != NULL |
| 6370 | && section->output_section->owner == link_info.output_bfd |
| 6371 | && (section->output_section->flags & SEC_EXCLUDE) == 0 |
| 6372 | && !IGNORE_SECTION (section) |
| 6373 | && section->size != 0) |
| 6374 | { |
| 6375 | bfd_boolean *has_relro_section = (bfd_boolean *) data; |
| 6376 | *has_relro_section = TRUE; |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | /* Iterate over sections for relro sections. */ |
| 6381 | |
| 6382 | static void |
| 6383 | lang_find_relro_sections_1 (lang_statement_union_type *s, |
| 6384 | bfd_boolean *has_relro_section) |
| 6385 | { |
| 6386 | if (*has_relro_section) |
| 6387 | return; |
| 6388 | |
| 6389 | for (; s != NULL; s = s->header.next) |
| 6390 | { |
| 6391 | if (s == expld.dataseg.relro_end_stat) |
| 6392 | break; |
| 6393 | |
| 6394 | switch (s->header.type) |
| 6395 | { |
| 6396 | case lang_wild_statement_enum: |
| 6397 | walk_wild (&s->wild_statement, |
| 6398 | find_relro_section_callback, |
| 6399 | has_relro_section); |
| 6400 | break; |
| 6401 | case lang_constructors_statement_enum: |
| 6402 | lang_find_relro_sections_1 (constructor_list.head, |
| 6403 | has_relro_section); |
| 6404 | break; |
| 6405 | case lang_output_section_statement_enum: |
| 6406 | lang_find_relro_sections_1 (s->output_section_statement.children.head, |
| 6407 | has_relro_section); |
| 6408 | break; |
| 6409 | case lang_group_statement_enum: |
| 6410 | lang_find_relro_sections_1 (s->group_statement.children.head, |
| 6411 | has_relro_section); |
| 6412 | break; |
| 6413 | default: |
| 6414 | break; |
| 6415 | } |
| 6416 | } |
| 6417 | } |
| 6418 | |
| 6419 | static void |
| 6420 | lang_find_relro_sections (void) |
| 6421 | { |
| 6422 | bfd_boolean has_relro_section = FALSE; |
| 6423 | |
| 6424 | /* Check all sections in the link script. */ |
| 6425 | |
| 6426 | lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, |
| 6427 | &has_relro_section); |
| 6428 | |
| 6429 | if (!has_relro_section) |
| 6430 | link_info.relro = FALSE; |
| 6431 | } |
| 6432 | |
| 6433 | /* Relax all sections until bfd_relax_section gives up. */ |
| 6434 | |
| 6435 | void |
| 6436 | lang_relax_sections (bfd_boolean need_layout) |
| 6437 | { |
| 6438 | if (RELAXATION_ENABLED) |
| 6439 | { |
| 6440 | /* We may need more than one relaxation pass. */ |
| 6441 | int i = link_info.relax_pass; |
| 6442 | |
| 6443 | /* The backend can use it to determine the current pass. */ |
| 6444 | link_info.relax_pass = 0; |
| 6445 | |
| 6446 | while (i--) |
| 6447 | { |
| 6448 | /* Keep relaxing until bfd_relax_section gives up. */ |
| 6449 | bfd_boolean relax_again; |
| 6450 | |
| 6451 | link_info.relax_trip = -1; |
| 6452 | do |
| 6453 | { |
| 6454 | link_info.relax_trip++; |
| 6455 | |
| 6456 | /* Note: pe-dll.c does something like this also. If you find |
| 6457 | you need to change this code, you probably need to change |
| 6458 | pe-dll.c also. DJ */ |
| 6459 | |
| 6460 | /* Do all the assignments with our current guesses as to |
| 6461 | section sizes. */ |
| 6462 | lang_do_assignments (lang_assigning_phase_enum); |
| 6463 | |
| 6464 | /* We must do this after lang_do_assignments, because it uses |
| 6465 | size. */ |
| 6466 | lang_reset_memory_regions (); |
| 6467 | |
| 6468 | /* Perform another relax pass - this time we know where the |
| 6469 | globals are, so can make a better guess. */ |
| 6470 | relax_again = FALSE; |
| 6471 | lang_size_sections (&relax_again, FALSE); |
| 6472 | } |
| 6473 | while (relax_again); |
| 6474 | |
| 6475 | link_info.relax_pass++; |
| 6476 | } |
| 6477 | need_layout = TRUE; |
| 6478 | } |
| 6479 | |
| 6480 | if (need_layout) |
| 6481 | { |
| 6482 | /* Final extra sizing to report errors. */ |
| 6483 | lang_do_assignments (lang_assigning_phase_enum); |
| 6484 | lang_reset_memory_regions (); |
| 6485 | lang_size_sections (NULL, TRUE); |
| 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | #ifdef ENABLE_PLUGINS |
| 6490 | /* Find the insert point for the plugin's replacement files. We |
| 6491 | place them after the first claimed real object file, or if the |
| 6492 | first claimed object is an archive member, after the last real |
| 6493 | object file immediately preceding the archive. In the event |
| 6494 | no objects have been claimed at all, we return the first dummy |
| 6495 | object file on the list as the insert point; that works, but |
| 6496 | the callee must be careful when relinking the file_chain as it |
| 6497 | is not actually on that chain, only the statement_list and the |
| 6498 | input_file list; in that case, the replacement files must be |
| 6499 | inserted at the head of the file_chain. */ |
| 6500 | |
| 6501 | static lang_input_statement_type * |
| 6502 | find_replacements_insert_point (void) |
| 6503 | { |
| 6504 | lang_input_statement_type *claim1, *lastobject; |
| 6505 | lastobject = &input_file_chain.head->input_statement; |
| 6506 | for (claim1 = &file_chain.head->input_statement; |
| 6507 | claim1 != NULL; |
| 6508 | claim1 = &claim1->next->input_statement) |
| 6509 | { |
| 6510 | if (claim1->flags.claimed) |
| 6511 | return claim1->flags.claim_archive ? lastobject : claim1; |
| 6512 | /* Update lastobject if this is a real object file. */ |
| 6513 | if (claim1->the_bfd && (claim1->the_bfd->my_archive == NULL)) |
| 6514 | lastobject = claim1; |
| 6515 | } |
| 6516 | /* No files were claimed by the plugin. Choose the last object |
| 6517 | file found on the list (maybe the first, dummy entry) as the |
| 6518 | insert point. */ |
| 6519 | return lastobject; |
| 6520 | } |
| 6521 | |
| 6522 | /* Insert SRCLIST into DESTLIST after given element by chaining |
| 6523 | on FIELD as the next-pointer. (Counterintuitively does not need |
| 6524 | a pointer to the actual after-node itself, just its chain field.) */ |
| 6525 | |
| 6526 | static void |
| 6527 | lang_list_insert_after (lang_statement_list_type *destlist, |
| 6528 | lang_statement_list_type *srclist, |
| 6529 | lang_statement_union_type **field) |
| 6530 | { |
| 6531 | *(srclist->tail) = *field; |
| 6532 | *field = srclist->head; |
| 6533 | if (destlist->tail == field) |
| 6534 | destlist->tail = srclist->tail; |
| 6535 | } |
| 6536 | |
| 6537 | /* Detach new nodes added to DESTLIST since the time ORIGLIST |
| 6538 | was taken as a copy of it and leave them in ORIGLIST. */ |
| 6539 | |
| 6540 | static void |
| 6541 | lang_list_remove_tail (lang_statement_list_type *destlist, |
| 6542 | lang_statement_list_type *origlist) |
| 6543 | { |
| 6544 | union lang_statement_union **savetail; |
| 6545 | /* Check that ORIGLIST really is an earlier state of DESTLIST. */ |
| 6546 | ASSERT (origlist->head == destlist->head); |
| 6547 | savetail = origlist->tail; |
| 6548 | origlist->head = *(savetail); |
| 6549 | origlist->tail = destlist->tail; |
| 6550 | destlist->tail = savetail; |
| 6551 | *savetail = NULL; |
| 6552 | } |
| 6553 | #endif /* ENABLE_PLUGINS */ |
| 6554 | |
| 6555 | void |
| 6556 | lang_process (void) |
| 6557 | { |
| 6558 | /* Finalize dynamic list. */ |
| 6559 | if (link_info.dynamic_list) |
| 6560 | lang_finalize_version_expr_head (&link_info.dynamic_list->head); |
| 6561 | |
| 6562 | current_target = default_target; |
| 6563 | |
| 6564 | /* Open the output file. */ |
| 6565 | lang_for_each_statement (ldlang_open_output); |
| 6566 | init_opb (); |
| 6567 | |
| 6568 | ldemul_create_output_section_statements (); |
| 6569 | |
| 6570 | /* Add to the hash table all undefineds on the command line. */ |
| 6571 | lang_place_undefineds (); |
| 6572 | |
| 6573 | if (!bfd_section_already_linked_table_init ()) |
| 6574 | einfo (_("%P%F: Failed to create hash table\n")); |
| 6575 | |
| 6576 | /* Create a bfd for each input file. */ |
| 6577 | current_target = default_target; |
| 6578 | open_input_bfds (statement_list.head, OPEN_BFD_NORMAL); |
| 6579 | |
| 6580 | #ifdef ENABLE_PLUGINS |
| 6581 | if (plugin_active_plugins_p ()) |
| 6582 | { |
| 6583 | lang_statement_list_type added; |
| 6584 | lang_statement_list_type files, inputfiles; |
| 6585 | |
| 6586 | /* Now all files are read, let the plugin(s) decide if there |
| 6587 | are any more to be added to the link before we call the |
| 6588 | emulation's after_open hook. We create a private list of |
| 6589 | input statements for this purpose, which we will eventually |
| 6590 | insert into the global statment list after the first claimed |
| 6591 | file. */ |
| 6592 | added = *stat_ptr; |
| 6593 | /* We need to manipulate all three chains in synchrony. */ |
| 6594 | files = file_chain; |
| 6595 | inputfiles = input_file_chain; |
| 6596 | if (plugin_call_all_symbols_read ()) |
| 6597 | einfo (_("%P%F: %s: plugin reported error after all symbols read\n"), |
| 6598 | plugin_error_plugin ()); |
| 6599 | /* Open any newly added files, updating the file chains. */ |
| 6600 | link_info.loading_lto_outputs = TRUE; |
| 6601 | open_input_bfds (*added.tail, OPEN_BFD_NORMAL); |
| 6602 | /* Restore the global list pointer now they have all been added. */ |
| 6603 | lang_list_remove_tail (stat_ptr, &added); |
| 6604 | /* And detach the fresh ends of the file lists. */ |
| 6605 | lang_list_remove_tail (&file_chain, &files); |
| 6606 | lang_list_remove_tail (&input_file_chain, &inputfiles); |
| 6607 | /* Were any new files added? */ |
| 6608 | if (added.head != NULL) |
| 6609 | { |
| 6610 | /* If so, we will insert them into the statement list immediately |
| 6611 | after the first input file that was claimed by the plugin. */ |
| 6612 | plugin_insert = find_replacements_insert_point (); |
| 6613 | /* If a plugin adds input files without having claimed any, we |
| 6614 | don't really have a good idea where to place them. Just putting |
| 6615 | them at the start or end of the list is liable to leave them |
| 6616 | outside the crtbegin...crtend range. */ |
| 6617 | ASSERT (plugin_insert != NULL); |
| 6618 | /* Splice the new statement list into the old one. */ |
| 6619 | lang_list_insert_after (stat_ptr, &added, |
| 6620 | &plugin_insert->header.next); |
| 6621 | /* Likewise for the file chains. */ |
| 6622 | lang_list_insert_after (&input_file_chain, &inputfiles, |
| 6623 | &plugin_insert->next_real_file); |
| 6624 | /* We must be careful when relinking file_chain; we may need to |
| 6625 | insert the new files at the head of the list if the insert |
| 6626 | point chosen is the dummy first input file. */ |
| 6627 | if (plugin_insert->filename) |
| 6628 | lang_list_insert_after (&file_chain, &files, &plugin_insert->next); |
| 6629 | else |
| 6630 | lang_list_insert_after (&file_chain, &files, &file_chain.head); |
| 6631 | |
| 6632 | /* Rescan archives in case new undefined symbols have appeared. */ |
| 6633 | open_input_bfds (statement_list.head, OPEN_BFD_RESCAN); |
| 6634 | } |
| 6635 | } |
| 6636 | #endif /* ENABLE_PLUGINS */ |
| 6637 | |
| 6638 | link_info.gc_sym_list = &entry_symbol; |
| 6639 | if (entry_symbol.name == NULL) |
| 6640 | link_info.gc_sym_list = ldlang_undef_chain_list_head; |
| 6641 | |
| 6642 | ldemul_after_open (); |
| 6643 | |
| 6644 | bfd_section_already_linked_table_free (); |
| 6645 | |
| 6646 | /* Make sure that we're not mixing architectures. We call this |
| 6647 | after all the input files have been opened, but before we do any |
| 6648 | other processing, so that any operations merge_private_bfd_data |
| 6649 | does on the output file will be known during the rest of the |
| 6650 | link. */ |
| 6651 | lang_check (); |
| 6652 | |
| 6653 | /* Handle .exports instead of a version script if we're told to do so. */ |
| 6654 | if (command_line.version_exports_section) |
| 6655 | lang_do_version_exports_section (); |
| 6656 | |
| 6657 | /* Build all sets based on the information gathered from the input |
| 6658 | files. */ |
| 6659 | ldctor_build_sets (); |
| 6660 | |
| 6661 | /* PR 13683: We must rerun the assignments prior to running garbage |
| 6662 | collection in order to make sure that all symbol aliases are resolved. */ |
| 6663 | lang_do_assignments (lang_mark_phase_enum); |
| 6664 | expld.phase = lang_first_phase_enum; |
| 6665 | |
| 6666 | /* Remove unreferenced sections if asked to. */ |
| 6667 | lang_gc_sections (); |
| 6668 | |
| 6669 | /* Size up the common data. */ |
| 6670 | lang_common (); |
| 6671 | |
| 6672 | /* Update wild statements. */ |
| 6673 | update_wild_statements (statement_list.head); |
| 6674 | |
| 6675 | /* Run through the contours of the script and attach input sections |
| 6676 | to the correct output sections. */ |
| 6677 | lang_statement_iteration++; |
| 6678 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
| 6679 | |
| 6680 | process_insert_statements (); |
| 6681 | |
| 6682 | /* Find any sections not attached explicitly and handle them. */ |
| 6683 | lang_place_orphans (); |
| 6684 | |
| 6685 | if (! link_info.relocatable) |
| 6686 | { |
| 6687 | asection *found; |
| 6688 | |
| 6689 | /* Merge SEC_MERGE sections. This has to be done after GC of |
| 6690 | sections, so that GCed sections are not merged, but before |
| 6691 | assigning dynamic symbols, since removing whole input sections |
| 6692 | is hard then. */ |
| 6693 | bfd_merge_sections (link_info.output_bfd, &link_info); |
| 6694 | |
| 6695 | /* Look for a text section and set the readonly attribute in it. */ |
| 6696 | found = bfd_get_section_by_name (link_info.output_bfd, ".text"); |
| 6697 | |
| 6698 | if (found != NULL) |
| 6699 | { |
| 6700 | if (config.text_read_only) |
| 6701 | found->flags |= SEC_READONLY; |
| 6702 | else |
| 6703 | found->flags &= ~SEC_READONLY; |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | /* Do anything special before sizing sections. This is where ELF |
| 6708 | and other back-ends size dynamic sections. */ |
| 6709 | ldemul_before_allocation (); |
| 6710 | |
| 6711 | /* We must record the program headers before we try to fix the |
| 6712 | section positions, since they will affect SIZEOF_HEADERS. */ |
| 6713 | lang_record_phdrs (); |
| 6714 | |
| 6715 | /* Check relro sections. */ |
| 6716 | if (link_info.relro && ! link_info.relocatable) |
| 6717 | lang_find_relro_sections (); |
| 6718 | |
| 6719 | /* Size up the sections. */ |
| 6720 | lang_size_sections (NULL, ! RELAXATION_ENABLED); |
| 6721 | |
| 6722 | /* See if anything special should be done now we know how big |
| 6723 | everything is. This is where relaxation is done. */ |
| 6724 | ldemul_after_allocation (); |
| 6725 | |
| 6726 | /* Fix any .startof. or .sizeof. symbols. */ |
| 6727 | lang_set_startof (); |
| 6728 | |
| 6729 | /* Do all the assignments, now that we know the final resting places |
| 6730 | of all the symbols. */ |
| 6731 | lang_do_assignments (lang_final_phase_enum); |
| 6732 | |
| 6733 | ldemul_finish (); |
| 6734 | |
| 6735 | /* Make sure that the section addresses make sense. */ |
| 6736 | if (command_line.check_section_addresses) |
| 6737 | lang_check_section_addresses (); |
| 6738 | |
| 6739 | lang_end (); |
| 6740 | } |
| 6741 | |
| 6742 | /* EXPORTED TO YACC */ |
| 6743 | |
| 6744 | void |
| 6745 | lang_add_wild (struct wildcard_spec *filespec, |
| 6746 | struct wildcard_list *section_list, |
| 6747 | bfd_boolean keep_sections) |
| 6748 | { |
| 6749 | struct wildcard_list *curr, *next; |
| 6750 | lang_wild_statement_type *new_stmt; |
| 6751 | |
| 6752 | /* Reverse the list as the parser puts it back to front. */ |
| 6753 | for (curr = section_list, section_list = NULL; |
| 6754 | curr != NULL; |
| 6755 | section_list = curr, curr = next) |
| 6756 | { |
| 6757 | if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0) |
| 6758 | placed_commons = TRUE; |
| 6759 | |
| 6760 | next = curr->next; |
| 6761 | curr->next = section_list; |
| 6762 | } |
| 6763 | |
| 6764 | if (filespec != NULL && filespec->name != NULL) |
| 6765 | { |
| 6766 | if (strcmp (filespec->name, "*") == 0) |
| 6767 | filespec->name = NULL; |
| 6768 | else if (! wildcardp (filespec->name)) |
| 6769 | lang_has_input_file = TRUE; |
| 6770 | } |
| 6771 | |
| 6772 | new_stmt = new_stat (lang_wild_statement, stat_ptr); |
| 6773 | new_stmt->filename = NULL; |
| 6774 | new_stmt->filenames_sorted = FALSE; |
| 6775 | new_stmt->section_flag_list = NULL; |
| 6776 | if (filespec != NULL) |
| 6777 | { |
| 6778 | new_stmt->filename = filespec->name; |
| 6779 | new_stmt->filenames_sorted = filespec->sorted == by_name; |
| 6780 | new_stmt->section_flag_list = filespec->section_flag_list; |
| 6781 | } |
| 6782 | new_stmt->section_list = section_list; |
| 6783 | new_stmt->keep_sections = keep_sections; |
| 6784 | lang_list_init (&new_stmt->children); |
| 6785 | analyze_walk_wild_section_handler (new_stmt); |
| 6786 | } |
| 6787 | |
| 6788 | void |
| 6789 | lang_section_start (const char *name, etree_type *address, |
| 6790 | const segment_type *segment) |
| 6791 | { |
| 6792 | lang_address_statement_type *ad; |
| 6793 | |
| 6794 | ad = new_stat (lang_address_statement, stat_ptr); |
| 6795 | ad->section_name = name; |
| 6796 | ad->address = address; |
| 6797 | ad->segment = segment; |
| 6798 | } |
| 6799 | |
| 6800 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called |
| 6801 | because of a -e argument on the command line, or zero if this is |
| 6802 | called by ENTRY in a linker script. Command line arguments take |
| 6803 | precedence. */ |
| 6804 | |
| 6805 | void |
| 6806 | lang_add_entry (const char *name, bfd_boolean cmdline) |
| 6807 | { |
| 6808 | if (entry_symbol.name == NULL |
| 6809 | || cmdline |
| 6810 | || ! entry_from_cmdline) |
| 6811 | { |
| 6812 | entry_symbol.name = name; |
| 6813 | entry_from_cmdline = cmdline; |
| 6814 | } |
| 6815 | } |
| 6816 | |
| 6817 | /* Set the default start symbol to NAME. .em files should use this, |
| 6818 | not lang_add_entry, to override the use of "start" if neither the |
| 6819 | linker script nor the command line specifies an entry point. NAME |
| 6820 | must be permanently allocated. */ |
| 6821 | void |
| 6822 | lang_default_entry (const char *name) |
| 6823 | { |
| 6824 | entry_symbol_default = name; |
| 6825 | } |
| 6826 | |
| 6827 | void |
| 6828 | lang_add_target (const char *name) |
| 6829 | { |
| 6830 | lang_target_statement_type *new_stmt; |
| 6831 | |
| 6832 | new_stmt = new_stat (lang_target_statement, stat_ptr); |
| 6833 | new_stmt->target = name; |
| 6834 | } |
| 6835 | |
| 6836 | void |
| 6837 | lang_add_map (const char *name) |
| 6838 | { |
| 6839 | while (*name) |
| 6840 | { |
| 6841 | switch (*name) |
| 6842 | { |
| 6843 | case 'F': |
| 6844 | map_option_f = TRUE; |
| 6845 | break; |
| 6846 | } |
| 6847 | name++; |
| 6848 | } |
| 6849 | } |
| 6850 | |
| 6851 | void |
| 6852 | lang_add_fill (fill_type *fill) |
| 6853 | { |
| 6854 | lang_fill_statement_type *new_stmt; |
| 6855 | |
| 6856 | new_stmt = new_stat (lang_fill_statement, stat_ptr); |
| 6857 | new_stmt->fill = fill; |
| 6858 | } |
| 6859 | |
| 6860 | void |
| 6861 | lang_add_data (int type, union etree_union *exp) |
| 6862 | { |
| 6863 | lang_data_statement_type *new_stmt; |
| 6864 | |
| 6865 | new_stmt = new_stat (lang_data_statement, stat_ptr); |
| 6866 | new_stmt->exp = exp; |
| 6867 | new_stmt->type = type; |
| 6868 | } |
| 6869 | |
| 6870 | /* Create a new reloc statement. RELOC is the BFD relocation type to |
| 6871 | generate. HOWTO is the corresponding howto structure (we could |
| 6872 | look this up, but the caller has already done so). SECTION is the |
| 6873 | section to generate a reloc against, or NAME is the name of the |
| 6874 | symbol to generate a reloc against. Exactly one of SECTION and |
| 6875 | NAME must be NULL. ADDEND is an expression for the addend. */ |
| 6876 | |
| 6877 | void |
| 6878 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
| 6879 | reloc_howto_type *howto, |
| 6880 | asection *section, |
| 6881 | const char *name, |
| 6882 | union etree_union *addend) |
| 6883 | { |
| 6884 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); |
| 6885 | |
| 6886 | p->reloc = reloc; |
| 6887 | p->howto = howto; |
| 6888 | p->section = section; |
| 6889 | p->name = name; |
| 6890 | p->addend_exp = addend; |
| 6891 | |
| 6892 | p->addend_value = 0; |
| 6893 | p->output_section = NULL; |
| 6894 | p->output_offset = 0; |
| 6895 | } |
| 6896 | |
| 6897 | lang_assignment_statement_type * |
| 6898 | lang_add_assignment (etree_type *exp) |
| 6899 | { |
| 6900 | lang_assignment_statement_type *new_stmt; |
| 6901 | |
| 6902 | new_stmt = new_stat (lang_assignment_statement, stat_ptr); |
| 6903 | new_stmt->exp = exp; |
| 6904 | return new_stmt; |
| 6905 | } |
| 6906 | |
| 6907 | void |
| 6908 | lang_add_attribute (enum statement_enum attribute) |
| 6909 | { |
| 6910 | new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); |
| 6911 | } |
| 6912 | |
| 6913 | void |
| 6914 | lang_startup (const char *name) |
| 6915 | { |
| 6916 | if (first_file->filename != NULL) |
| 6917 | { |
| 6918 | einfo (_("%P%F: multiple STARTUP files\n")); |
| 6919 | } |
| 6920 | first_file->filename = name; |
| 6921 | first_file->local_sym_name = name; |
| 6922 | first_file->flags.real = TRUE; |
| 6923 | } |
| 6924 | |
| 6925 | void |
| 6926 | lang_float (bfd_boolean maybe) |
| 6927 | { |
| 6928 | lang_float_flag = maybe; |
| 6929 | } |
| 6930 | |
| 6931 | |
| 6932 | /* Work out the load- and run-time regions from a script statement, and |
| 6933 | store them in *LMA_REGION and *REGION respectively. |
| 6934 | |
| 6935 | MEMSPEC is the name of the run-time region, or the value of |
| 6936 | DEFAULT_MEMORY_REGION if the statement didn't specify one. |
| 6937 | LMA_MEMSPEC is the name of the load-time region, or null if the |
| 6938 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement |
| 6939 | had an explicit load address. |
| 6940 | |
| 6941 | It is an error to specify both a load region and a load address. */ |
| 6942 | |
| 6943 | static void |
| 6944 | lang_get_regions (lang_memory_region_type **region, |
| 6945 | lang_memory_region_type **lma_region, |
| 6946 | const char *memspec, |
| 6947 | const char *lma_memspec, |
| 6948 | bfd_boolean have_lma, |
| 6949 | bfd_boolean have_vma) |
| 6950 | { |
| 6951 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
| 6952 | |
| 6953 | /* If no runtime region or VMA has been specified, but the load region |
| 6954 | has been specified, then use the load region for the runtime region |
| 6955 | as well. */ |
| 6956 | if (lma_memspec != NULL |
| 6957 | && ! have_vma |
| 6958 | && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
| 6959 | *region = *lma_region; |
| 6960 | else |
| 6961 | *region = lang_memory_region_lookup (memspec, FALSE); |
| 6962 | |
| 6963 | if (have_lma && lma_memspec != 0) |
| 6964 | einfo (_("%X%P:%S: section has both a load address and a load region\n"), |
| 6965 | NULL); |
| 6966 | } |
| 6967 | |
| 6968 | void |
| 6969 | lang_leave_output_section_statement (fill_type *fill, const char *memspec, |
| 6970 | lang_output_section_phdr_list *phdrs, |
| 6971 | const char *lma_memspec) |
| 6972 | { |
| 6973 | lang_get_regions (¤t_section->region, |
| 6974 | ¤t_section->lma_region, |
| 6975 | memspec, lma_memspec, |
| 6976 | current_section->load_base != NULL, |
| 6977 | current_section->addr_tree != NULL); |
| 6978 | |
| 6979 | /* If this section has no load region or base, but uses the same |
| 6980 | region as the previous section, then propagate the previous |
| 6981 | section's load region. */ |
| 6982 | |
| 6983 | if (current_section->lma_region == NULL |
| 6984 | && current_section->load_base == NULL |
| 6985 | && current_section->addr_tree == NULL |
| 6986 | && current_section->region == current_section->prev->region) |
| 6987 | current_section->lma_region = current_section->prev->lma_region; |
| 6988 | |
| 6989 | current_section->fill = fill; |
| 6990 | current_section->phdrs = phdrs; |
| 6991 | pop_stat_ptr (); |
| 6992 | } |
| 6993 | |
| 6994 | void |
| 6995 | lang_statement_append (lang_statement_list_type *list, |
| 6996 | lang_statement_union_type *element, |
| 6997 | lang_statement_union_type **field) |
| 6998 | { |
| 6999 | *(list->tail) = element; |
| 7000 | list->tail = field; |
| 7001 | } |
| 7002 | |
| 7003 | /* Set the output format type. -oformat overrides scripts. */ |
| 7004 | |
| 7005 | void |
| 7006 | lang_add_output_format (const char *format, |
| 7007 | const char *big, |
| 7008 | const char *little, |
| 7009 | int from_script) |
| 7010 | { |
| 7011 | if (output_target == NULL || !from_script) |
| 7012 | { |
| 7013 | if (command_line.endian == ENDIAN_BIG |
| 7014 | && big != NULL) |
| 7015 | format = big; |
| 7016 | else if (command_line.endian == ENDIAN_LITTLE |
| 7017 | && little != NULL) |
| 7018 | format = little; |
| 7019 | |
| 7020 | output_target = format; |
| 7021 | } |
| 7022 | } |
| 7023 | |
| 7024 | void |
| 7025 | lang_add_insert (const char *where, int is_before) |
| 7026 | { |
| 7027 | lang_insert_statement_type *new_stmt; |
| 7028 | |
| 7029 | new_stmt = new_stat (lang_insert_statement, stat_ptr); |
| 7030 | new_stmt->where = where; |
| 7031 | new_stmt->is_before = is_before; |
| 7032 | saved_script_handle = previous_script_handle; |
| 7033 | } |
| 7034 | |
| 7035 | /* Enter a group. This creates a new lang_group_statement, and sets |
| 7036 | stat_ptr to build new statements within the group. */ |
| 7037 | |
| 7038 | void |
| 7039 | lang_enter_group (void) |
| 7040 | { |
| 7041 | lang_group_statement_type *g; |
| 7042 | |
| 7043 | g = new_stat (lang_group_statement, stat_ptr); |
| 7044 | lang_list_init (&g->children); |
| 7045 | push_stat_ptr (&g->children); |
| 7046 | } |
| 7047 | |
| 7048 | /* Leave a group. This just resets stat_ptr to start writing to the |
| 7049 | regular list of statements again. Note that this will not work if |
| 7050 | groups can occur inside anything else which can adjust stat_ptr, |
| 7051 | but currently they can't. */ |
| 7052 | |
| 7053 | void |
| 7054 | lang_leave_group (void) |
| 7055 | { |
| 7056 | pop_stat_ptr (); |
| 7057 | } |
| 7058 | |
| 7059 | /* Add a new program header. This is called for each entry in a PHDRS |
| 7060 | command in a linker script. */ |
| 7061 | |
| 7062 | void |
| 7063 | lang_new_phdr (const char *name, |
| 7064 | etree_type *type, |
| 7065 | bfd_boolean filehdr, |
| 7066 | bfd_boolean phdrs, |
| 7067 | etree_type *at, |
| 7068 | etree_type *flags) |
| 7069 | { |
| 7070 | struct lang_phdr *n, **pp; |
| 7071 | bfd_boolean hdrs; |
| 7072 | |
| 7073 | n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr)); |
| 7074 | n->next = NULL; |
| 7075 | n->name = name; |
| 7076 | n->type = exp_get_value_int (type, 0, "program header type"); |
| 7077 | n->filehdr = filehdr; |
| 7078 | n->phdrs = phdrs; |
| 7079 | n->at = at; |
| 7080 | n->flags = flags; |
| 7081 | |
| 7082 | hdrs = n->type == 1 && (phdrs || filehdr); |
| 7083 | |
| 7084 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) |
| 7085 | if (hdrs |
| 7086 | && (*pp)->type == 1 |
| 7087 | && !((*pp)->filehdr || (*pp)->phdrs)) |
| 7088 | { |
| 7089 | einfo (_("%X%P:%S: PHDRS and FILEHDR are not supported" |
| 7090 | " when prior PT_LOAD headers lack them\n"), NULL); |
| 7091 | hdrs = FALSE; |
| 7092 | } |
| 7093 | |
| 7094 | *pp = n; |
| 7095 | } |
| 7096 | |
| 7097 | /* Record the program header information in the output BFD. FIXME: We |
| 7098 | should not be calling an ELF specific function here. */ |
| 7099 | |
| 7100 | static void |
| 7101 | lang_record_phdrs (void) |
| 7102 | { |
| 7103 | unsigned int alc; |
| 7104 | asection **secs; |
| 7105 | lang_output_section_phdr_list *last; |
| 7106 | struct lang_phdr *l; |
| 7107 | lang_output_section_statement_type *os; |
| 7108 | |
| 7109 | alc = 10; |
| 7110 | secs = (asection **) xmalloc (alc * sizeof (asection *)); |
| 7111 | last = NULL; |
| 7112 | |
| 7113 | for (l = lang_phdr_list; l != NULL; l = l->next) |
| 7114 | { |
| 7115 | unsigned int c; |
| 7116 | flagword flags; |
| 7117 | bfd_vma at; |
| 7118 | |
| 7119 | c = 0; |
| 7120 | for (os = &lang_output_section_statement.head->output_section_statement; |
| 7121 | os != NULL; |
| 7122 | os = os->next) |
| 7123 | { |
| 7124 | lang_output_section_phdr_list *pl; |
| 7125 | |
| 7126 | if (os->constraint < 0) |
| 7127 | continue; |
| 7128 | |
| 7129 | pl = os->phdrs; |
| 7130 | if (pl != NULL) |
| 7131 | last = pl; |
| 7132 | else |
| 7133 | { |
| 7134 | if (os->sectype == noload_section |
| 7135 | || os->bfd_section == NULL |
| 7136 | || (os->bfd_section->flags & SEC_ALLOC) == 0) |
| 7137 | continue; |
| 7138 | |
| 7139 | /* Don't add orphans to PT_INTERP header. */ |
| 7140 | if (l->type == 3) |
| 7141 | continue; |
| 7142 | |
| 7143 | if (last == NULL) |
| 7144 | { |
| 7145 | lang_output_section_statement_type * tmp_os; |
| 7146 | |
| 7147 | /* If we have not run across a section with a program |
| 7148 | header assigned to it yet, then scan forwards to find |
| 7149 | one. This prevents inconsistencies in the linker's |
| 7150 | behaviour when a script has specified just a single |
| 7151 | header and there are sections in that script which are |
| 7152 | not assigned to it, and which occur before the first |
| 7153 | use of that header. See here for more details: |
| 7154 | http://sourceware.org/ml/binutils/2007-02/msg00291.html */ |
| 7155 | for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) |
| 7156 | if (tmp_os->phdrs) |
| 7157 | { |
| 7158 | last = tmp_os->phdrs; |
| 7159 | break; |
| 7160 | } |
| 7161 | if (last == NULL) |
| 7162 | einfo (_("%F%P: no sections assigned to phdrs\n")); |
| 7163 | } |
| 7164 | pl = last; |
| 7165 | } |
| 7166 | |
| 7167 | if (os->bfd_section == NULL) |
| 7168 | continue; |
| 7169 | |
| 7170 | for (; pl != NULL; pl = pl->next) |
| 7171 | { |
| 7172 | if (strcmp (pl->name, l->name) == 0) |
| 7173 | { |
| 7174 | if (c >= alc) |
| 7175 | { |
| 7176 | alc *= 2; |
| 7177 | secs = (asection **) xrealloc (secs, |
| 7178 | alc * sizeof (asection *)); |
| 7179 | } |
| 7180 | secs[c] = os->bfd_section; |
| 7181 | ++c; |
| 7182 | pl->used = TRUE; |
| 7183 | } |
| 7184 | } |
| 7185 | } |
| 7186 | |
| 7187 | if (l->flags == NULL) |
| 7188 | flags = 0; |
| 7189 | else |
| 7190 | flags = exp_get_vma (l->flags, 0, "phdr flags"); |
| 7191 | |
| 7192 | if (l->at == NULL) |
| 7193 | at = 0; |
| 7194 | else |
| 7195 | at = exp_get_vma (l->at, 0, "phdr load address"); |
| 7196 | |
| 7197 | if (! bfd_record_phdr (link_info.output_bfd, l->type, |
| 7198 | l->flags != NULL, flags, l->at != NULL, |
| 7199 | at, l->filehdr, l->phdrs, c, secs)) |
| 7200 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
| 7201 | } |
| 7202 | |
| 7203 | free (secs); |
| 7204 | |
| 7205 | /* Make sure all the phdr assignments succeeded. */ |
| 7206 | for (os = &lang_output_section_statement.head->output_section_statement; |
| 7207 | os != NULL; |
| 7208 | os = os->next) |
| 7209 | { |
| 7210 | lang_output_section_phdr_list *pl; |
| 7211 | |
| 7212 | if (os->constraint < 0 |
| 7213 | || os->bfd_section == NULL) |
| 7214 | continue; |
| 7215 | |
| 7216 | for (pl = os->phdrs; |
| 7217 | pl != NULL; |
| 7218 | pl = pl->next) |
| 7219 | if (! pl->used && strcmp (pl->name, "NONE") != 0) |
| 7220 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
| 7221 | os->name, pl->name); |
| 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | /* Record a list of sections which may not be cross referenced. */ |
| 7226 | |
| 7227 | void |
| 7228 | lang_add_nocrossref (lang_nocrossref_type *l) |
| 7229 | { |
| 7230 | struct lang_nocrossrefs *n; |
| 7231 | |
| 7232 | n = (struct lang_nocrossrefs *) xmalloc (sizeof *n); |
| 7233 | n->next = nocrossref_list; |
| 7234 | n->list = l; |
| 7235 | nocrossref_list = n; |
| 7236 | |
| 7237 | /* Set notice_all so that we get informed about all symbols. */ |
| 7238 | link_info.notice_all = TRUE; |
| 7239 | } |
| 7240 | \f |
| 7241 | /* Overlay handling. We handle overlays with some static variables. */ |
| 7242 | |
| 7243 | /* The overlay virtual address. */ |
| 7244 | static etree_type *overlay_vma; |
| 7245 | /* And subsection alignment. */ |
| 7246 | static etree_type *overlay_subalign; |
| 7247 | |
| 7248 | /* An expression for the maximum section size seen so far. */ |
| 7249 | static etree_type *overlay_max; |
| 7250 | |
| 7251 | /* A list of all the sections in this overlay. */ |
| 7252 | |
| 7253 | struct overlay_list { |
| 7254 | struct overlay_list *next; |
| 7255 | lang_output_section_statement_type *os; |
| 7256 | }; |
| 7257 | |
| 7258 | static struct overlay_list *overlay_list; |
| 7259 | |
| 7260 | /* Start handling an overlay. */ |
| 7261 | |
| 7262 | void |
| 7263 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
| 7264 | { |
| 7265 | /* The grammar should prevent nested overlays from occurring. */ |
| 7266 | ASSERT (overlay_vma == NULL |
| 7267 | && overlay_subalign == NULL |
| 7268 | && overlay_max == NULL); |
| 7269 | |
| 7270 | overlay_vma = vma_expr; |
| 7271 | overlay_subalign = subalign; |
| 7272 | } |
| 7273 | |
| 7274 | /* Start a section in an overlay. We handle this by calling |
| 7275 | lang_enter_output_section_statement with the correct VMA. |
| 7276 | lang_leave_overlay sets up the LMA and memory regions. */ |
| 7277 | |
| 7278 | void |
| 7279 | lang_enter_overlay_section (const char *name) |
| 7280 | { |
| 7281 | struct overlay_list *n; |
| 7282 | etree_type *size; |
| 7283 | |
| 7284 | lang_enter_output_section_statement (name, overlay_vma, overlay_section, |
| 7285 | 0, overlay_subalign, 0, 0, 0); |
| 7286 | |
| 7287 | /* If this is the first section, then base the VMA of future |
| 7288 | sections on this one. This will work correctly even if `.' is |
| 7289 | used in the addresses. */ |
| 7290 | if (overlay_list == NULL) |
| 7291 | overlay_vma = exp_nameop (ADDR, name); |
| 7292 | |
| 7293 | /* Remember the section. */ |
| 7294 | n = (struct overlay_list *) xmalloc (sizeof *n); |
| 7295 | n->os = current_section; |
| 7296 | n->next = overlay_list; |
| 7297 | overlay_list = n; |
| 7298 | |
| 7299 | size = exp_nameop (SIZEOF, name); |
| 7300 | |
| 7301 | /* Arrange to work out the maximum section end address. */ |
| 7302 | if (overlay_max == NULL) |
| 7303 | overlay_max = size; |
| 7304 | else |
| 7305 | overlay_max = exp_binop (MAX_K, overlay_max, size); |
| 7306 | } |
| 7307 | |
| 7308 | /* Finish a section in an overlay. There isn't any special to do |
| 7309 | here. */ |
| 7310 | |
| 7311 | void |
| 7312 | lang_leave_overlay_section (fill_type *fill, |
| 7313 | lang_output_section_phdr_list *phdrs) |
| 7314 | { |
| 7315 | const char *name; |
| 7316 | char *clean, *s2; |
| 7317 | const char *s1; |
| 7318 | char *buf; |
| 7319 | |
| 7320 | name = current_section->name; |
| 7321 | |
| 7322 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
| 7323 | region and that no load-time region has been specified. It doesn't |
| 7324 | really matter what we say here, since lang_leave_overlay will |
| 7325 | override it. */ |
| 7326 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); |
| 7327 | |
| 7328 | /* Define the magic symbols. */ |
| 7329 | |
| 7330 | clean = (char *) xmalloc (strlen (name) + 1); |
| 7331 | s2 = clean; |
| 7332 | for (s1 = name; *s1 != '\0'; s1++) |
| 7333 | if (ISALNUM (*s1) || *s1 == '_') |
| 7334 | *s2++ = *s1; |
| 7335 | *s2 = '\0'; |
| 7336 | |
| 7337 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_"); |
| 7338 | sprintf (buf, "__load_start_%s", clean); |
| 7339 | lang_add_assignment (exp_provide (buf, |
| 7340 | exp_nameop (LOADADDR, name), |
| 7341 | FALSE)); |
| 7342 | |
| 7343 | buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_"); |
| 7344 | sprintf (buf, "__load_stop_%s", clean); |
| 7345 | lang_add_assignment (exp_provide (buf, |
| 7346 | exp_binop ('+', |
| 7347 | exp_nameop (LOADADDR, name), |
| 7348 | exp_nameop (SIZEOF, name)), |
| 7349 | FALSE)); |
| 7350 | |
| 7351 | free (clean); |
| 7352 | } |
| 7353 | |
| 7354 | /* Finish an overlay. If there are any overlay wide settings, this |
| 7355 | looks through all the sections in the overlay and sets them. */ |
| 7356 | |
| 7357 | void |
| 7358 | lang_leave_overlay (etree_type *lma_expr, |
| 7359 | int nocrossrefs, |
| 7360 | fill_type *fill, |
| 7361 | const char *memspec, |
| 7362 | lang_output_section_phdr_list *phdrs, |
| 7363 | const char *lma_memspec) |
| 7364 | { |
| 7365 | lang_memory_region_type *region; |
| 7366 | lang_memory_region_type *lma_region; |
| 7367 | struct overlay_list *l; |
| 7368 | lang_nocrossref_type *nocrossref; |
| 7369 | |
| 7370 | lang_get_regions (®ion, &lma_region, |
| 7371 | memspec, lma_memspec, |
| 7372 | lma_expr != NULL, FALSE); |
| 7373 | |
| 7374 | nocrossref = NULL; |
| 7375 | |
| 7376 | /* After setting the size of the last section, set '.' to end of the |
| 7377 | overlay region. */ |
| 7378 | if (overlay_list != NULL) |
| 7379 | { |
| 7380 | overlay_list->os->update_dot = 1; |
| 7381 | overlay_list->os->update_dot_tree |
| 7382 | = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), FALSE); |
| 7383 | } |
| 7384 | |
| 7385 | l = overlay_list; |
| 7386 | while (l != NULL) |
| 7387 | { |
| 7388 | struct overlay_list *next; |
| 7389 | |
| 7390 | if (fill != NULL && l->os->fill == NULL) |
| 7391 | l->os->fill = fill; |
| 7392 | |
| 7393 | l->os->region = region; |
| 7394 | l->os->lma_region = lma_region; |
| 7395 | |
| 7396 | /* The first section has the load address specified in the |
| 7397 | OVERLAY statement. The rest are worked out from that. |
| 7398 | The base address is not needed (and should be null) if |
| 7399 | an LMA region was specified. */ |
| 7400 | if (l->next == 0) |
| 7401 | { |
| 7402 | l->os->load_base = lma_expr; |
| 7403 | l->os->sectype = normal_section; |
| 7404 | } |
| 7405 | if (phdrs != NULL && l->os->phdrs == NULL) |
| 7406 | l->os->phdrs = phdrs; |
| 7407 | |
| 7408 | if (nocrossrefs) |
| 7409 | { |
| 7410 | lang_nocrossref_type *nc; |
| 7411 | |
| 7412 | nc = (lang_nocrossref_type *) xmalloc (sizeof *nc); |
| 7413 | nc->name = l->os->name; |
| 7414 | nc->next = nocrossref; |
| 7415 | nocrossref = nc; |
| 7416 | } |
| 7417 | |
| 7418 | next = l->next; |
| 7419 | free (l); |
| 7420 | l = next; |
| 7421 | } |
| 7422 | |
| 7423 | if (nocrossref != NULL) |
| 7424 | lang_add_nocrossref (nocrossref); |
| 7425 | |
| 7426 | overlay_vma = NULL; |
| 7427 | overlay_list = NULL; |
| 7428 | overlay_max = NULL; |
| 7429 | } |
| 7430 | \f |
| 7431 | /* Version handling. This is only useful for ELF. */ |
| 7432 | |
| 7433 | /* If PREV is NULL, return first version pattern matching particular symbol. |
| 7434 | If PREV is non-NULL, return first version pattern matching particular |
| 7435 | symbol after PREV (previously returned by lang_vers_match). */ |
| 7436 | |
| 7437 | static struct bfd_elf_version_expr * |
| 7438 | lang_vers_match (struct bfd_elf_version_expr_head *head, |
| 7439 | struct bfd_elf_version_expr *prev, |
| 7440 | const char *sym) |
| 7441 | { |
| 7442 | const char *c_sym; |
| 7443 | const char *cxx_sym = sym; |
| 7444 | const char *java_sym = sym; |
| 7445 | struct bfd_elf_version_expr *expr = NULL; |
| 7446 | enum demangling_styles curr_style; |
| 7447 | |
| 7448 | curr_style = CURRENT_DEMANGLING_STYLE; |
| 7449 | cplus_demangle_set_style (no_demangling); |
| 7450 | c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); |
| 7451 | if (!c_sym) |
| 7452 | c_sym = sym; |
| 7453 | cplus_demangle_set_style (curr_style); |
| 7454 | |
| 7455 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
| 7456 | { |
| 7457 | cxx_sym = bfd_demangle (link_info.output_bfd, sym, |
| 7458 | DMGL_PARAMS | DMGL_ANSI); |
| 7459 | if (!cxx_sym) |
| 7460 | cxx_sym = sym; |
| 7461 | } |
| 7462 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
| 7463 | { |
| 7464 | java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); |
| 7465 | if (!java_sym) |
| 7466 | java_sym = sym; |
| 7467 | } |
| 7468 | |
| 7469 | if (head->htab && (prev == NULL || prev->literal)) |
| 7470 | { |
| 7471 | struct bfd_elf_version_expr e; |
| 7472 | |
| 7473 | switch (prev ? prev->mask : 0) |
| 7474 | { |
| 7475 | case 0: |
| 7476 | if (head->mask & BFD_ELF_VERSION_C_TYPE) |
| 7477 | { |
| 7478 | e.pattern = c_sym; |
| 7479 | expr = (struct bfd_elf_version_expr *) |
| 7480 | htab_find ((htab_t) head->htab, &e); |
| 7481 | while (expr && strcmp (expr->pattern, c_sym) == 0) |
| 7482 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
| 7483 | goto out_ret; |
| 7484 | else |
| 7485 | expr = expr->next; |
| 7486 | } |
| 7487 | /* Fallthrough */ |
| 7488 | case BFD_ELF_VERSION_C_TYPE: |
| 7489 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
| 7490 | { |
| 7491 | e.pattern = cxx_sym; |
| 7492 | expr = (struct bfd_elf_version_expr *) |
| 7493 | htab_find ((htab_t) head->htab, &e); |
| 7494 | while (expr && strcmp (expr->pattern, cxx_sym) == 0) |
| 7495 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
| 7496 | goto out_ret; |
| 7497 | else |
| 7498 | expr = expr->next; |
| 7499 | } |
| 7500 | /* Fallthrough */ |
| 7501 | case BFD_ELF_VERSION_CXX_TYPE: |
| 7502 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
| 7503 | { |
| 7504 | e.pattern = java_sym; |
| 7505 | expr = (struct bfd_elf_version_expr *) |
| 7506 | htab_find ((htab_t) head->htab, &e); |
| 7507 | while (expr && strcmp (expr->pattern, java_sym) == 0) |
| 7508 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
| 7509 | goto out_ret; |
| 7510 | else |
| 7511 | expr = expr->next; |
| 7512 | } |
| 7513 | /* Fallthrough */ |
| 7514 | default: |
| 7515 | break; |
| 7516 | } |
| 7517 | } |
| 7518 | |
| 7519 | /* Finally, try the wildcards. */ |
| 7520 | if (prev == NULL || prev->literal) |
| 7521 | expr = head->remaining; |
| 7522 | else |
| 7523 | expr = prev->next; |
| 7524 | for (; expr; expr = expr->next) |
| 7525 | { |
| 7526 | const char *s; |
| 7527 | |
| 7528 | if (!expr->pattern) |
| 7529 | continue; |
| 7530 | |
| 7531 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
| 7532 | break; |
| 7533 | |
| 7534 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
| 7535 | s = java_sym; |
| 7536 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
| 7537 | s = cxx_sym; |
| 7538 | else |
| 7539 | s = c_sym; |
| 7540 | if (fnmatch (expr->pattern, s, 0) == 0) |
| 7541 | break; |
| 7542 | } |
| 7543 | |
| 7544 | out_ret: |
| 7545 | if (c_sym != sym) |
| 7546 | free ((char *) c_sym); |
| 7547 | if (cxx_sym != sym) |
| 7548 | free ((char *) cxx_sym); |
| 7549 | if (java_sym != sym) |
| 7550 | free ((char *) java_sym); |
| 7551 | return expr; |
| 7552 | } |
| 7553 | |
| 7554 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
| 7555 | return a pointer to the symbol name with any backslash quotes removed. */ |
| 7556 | |
| 7557 | static const char * |
| 7558 | realsymbol (const char *pattern) |
| 7559 | { |
| 7560 | const char *p; |
| 7561 | bfd_boolean changed = FALSE, backslash = FALSE; |
| 7562 | char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); |
| 7563 | |
| 7564 | for (p = pattern, s = symbol; *p != '\0'; ++p) |
| 7565 | { |
| 7566 | /* It is a glob pattern only if there is no preceding |
| 7567 | backslash. */ |
| 7568 | if (backslash) |
| 7569 | { |
| 7570 | /* Remove the preceding backslash. */ |
| 7571 | *(s - 1) = *p; |
| 7572 | backslash = FALSE; |
| 7573 | changed = TRUE; |
| 7574 | } |
| 7575 | else |
| 7576 | { |
| 7577 | if (*p == '?' || *p == '*' || *p == '[') |
| 7578 | { |
| 7579 | free (symbol); |
| 7580 | return NULL; |
| 7581 | } |
| 7582 | |
| 7583 | *s++ = *p; |
| 7584 | backslash = *p == '\\'; |
| 7585 | } |
| 7586 | } |
| 7587 | |
| 7588 | if (changed) |
| 7589 | { |
| 7590 | *s = '\0'; |
| 7591 | return symbol; |
| 7592 | } |
| 7593 | else |
| 7594 | { |
| 7595 | free (symbol); |
| 7596 | return pattern; |
| 7597 | } |
| 7598 | } |
| 7599 | |
| 7600 | /* This is called for each variable name or match expression. NEW_NAME is |
| 7601 | the name of the symbol to match, or, if LITERAL_P is FALSE, a glob |
| 7602 | pattern to be matched against symbol names. */ |
| 7603 | |
| 7604 | struct bfd_elf_version_expr * |
| 7605 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
| 7606 | const char *new_name, |
| 7607 | const char *lang, |
| 7608 | bfd_boolean literal_p) |
| 7609 | { |
| 7610 | struct bfd_elf_version_expr *ret; |
| 7611 | |
| 7612 | ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret); |
| 7613 | ret->next = orig; |
| 7614 | ret->symver = 0; |
| 7615 | ret->script = 0; |
| 7616 | ret->literal = TRUE; |
| 7617 | ret->pattern = literal_p ? new_name : realsymbol (new_name); |
| 7618 | if (ret->pattern == NULL) |
| 7619 | { |
| 7620 | ret->pattern = new_name; |
| 7621 | ret->literal = FALSE; |
| 7622 | } |
| 7623 | |
| 7624 | if (lang == NULL || strcasecmp (lang, "C") == 0) |
| 7625 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
| 7626 | else if (strcasecmp (lang, "C++") == 0) |
| 7627 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
| 7628 | else if (strcasecmp (lang, "Java") == 0) |
| 7629 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
| 7630 | else |
| 7631 | { |
| 7632 | einfo (_("%X%P: unknown language `%s' in version information\n"), |
| 7633 | lang); |
| 7634 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
| 7635 | } |
| 7636 | |
| 7637 | return ldemul_new_vers_pattern (ret); |
| 7638 | } |
| 7639 | |
| 7640 | /* This is called for each set of variable names and match |
| 7641 | expressions. */ |
| 7642 | |
| 7643 | struct bfd_elf_version_tree * |
| 7644 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
| 7645 | struct bfd_elf_version_expr *locals) |
| 7646 | { |
| 7647 | struct bfd_elf_version_tree *ret; |
| 7648 | |
| 7649 | ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret); |
| 7650 | ret->globals.list = globals; |
| 7651 | ret->locals.list = locals; |
| 7652 | ret->match = lang_vers_match; |
| 7653 | ret->name_indx = (unsigned int) -1; |
| 7654 | return ret; |
| 7655 | } |
| 7656 | |
| 7657 | /* This static variable keeps track of version indices. */ |
| 7658 | |
| 7659 | static int version_index; |
| 7660 | |
| 7661 | static hashval_t |
| 7662 | version_expr_head_hash (const void *p) |
| 7663 | { |
| 7664 | const struct bfd_elf_version_expr *e = |
| 7665 | (const struct bfd_elf_version_expr *) p; |
| 7666 | |
| 7667 | return htab_hash_string (e->pattern); |
| 7668 | } |
| 7669 | |
| 7670 | static int |
| 7671 | version_expr_head_eq (const void *p1, const void *p2) |
| 7672 | { |
| 7673 | const struct bfd_elf_version_expr *e1 = |
| 7674 | (const struct bfd_elf_version_expr *) p1; |
| 7675 | const struct bfd_elf_version_expr *e2 = |
| 7676 | (const struct bfd_elf_version_expr *) p2; |
| 7677 | |
| 7678 | return strcmp (e1->pattern, e2->pattern) == 0; |
| 7679 | } |
| 7680 | |
| 7681 | static void |
| 7682 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) |
| 7683 | { |
| 7684 | size_t count = 0; |
| 7685 | struct bfd_elf_version_expr *e, *next; |
| 7686 | struct bfd_elf_version_expr **list_loc, **remaining_loc; |
| 7687 | |
| 7688 | for (e = head->list; e; e = e->next) |
| 7689 | { |
| 7690 | if (e->literal) |
| 7691 | count++; |
| 7692 | head->mask |= e->mask; |
| 7693 | } |
| 7694 | |
| 7695 | if (count) |
| 7696 | { |
| 7697 | head->htab = htab_create (count * 2, version_expr_head_hash, |
| 7698 | version_expr_head_eq, NULL); |
| 7699 | list_loc = &head->list; |
| 7700 | remaining_loc = &head->remaining; |
| 7701 | for (e = head->list; e; e = next) |
| 7702 | { |
| 7703 | next = e->next; |
| 7704 | if (!e->literal) |
| 7705 | { |
| 7706 | *remaining_loc = e; |
| 7707 | remaining_loc = &e->next; |
| 7708 | } |
| 7709 | else |
| 7710 | { |
| 7711 | void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); |
| 7712 | |
| 7713 | if (*loc) |
| 7714 | { |
| 7715 | struct bfd_elf_version_expr *e1, *last; |
| 7716 | |
| 7717 | e1 = (struct bfd_elf_version_expr *) *loc; |
| 7718 | last = NULL; |
| 7719 | do |
| 7720 | { |
| 7721 | if (e1->mask == e->mask) |
| 7722 | { |
| 7723 | last = NULL; |
| 7724 | break; |
| 7725 | } |
| 7726 | last = e1; |
| 7727 | e1 = e1->next; |
| 7728 | } |
| 7729 | while (e1 && strcmp (e1->pattern, e->pattern) == 0); |
| 7730 | |
| 7731 | if (last == NULL) |
| 7732 | { |
| 7733 | /* This is a duplicate. */ |
| 7734 | /* FIXME: Memory leak. Sometimes pattern is not |
| 7735 | xmalloced alone, but in larger chunk of memory. */ |
| 7736 | /* free (e->pattern); */ |
| 7737 | free (e); |
| 7738 | } |
| 7739 | else |
| 7740 | { |
| 7741 | e->next = last->next; |
| 7742 | last->next = e; |
| 7743 | } |
| 7744 | } |
| 7745 | else |
| 7746 | { |
| 7747 | *loc = e; |
| 7748 | *list_loc = e; |
| 7749 | list_loc = &e->next; |
| 7750 | } |
| 7751 | } |
| 7752 | } |
| 7753 | *remaining_loc = NULL; |
| 7754 | *list_loc = head->remaining; |
| 7755 | } |
| 7756 | else |
| 7757 | head->remaining = head->list; |
| 7758 | } |
| 7759 | |
| 7760 | /* This is called when we know the name and dependencies of the |
| 7761 | version. */ |
| 7762 | |
| 7763 | void |
| 7764 | lang_register_vers_node (const char *name, |
| 7765 | struct bfd_elf_version_tree *version, |
| 7766 | struct bfd_elf_version_deps *deps) |
| 7767 | { |
| 7768 | struct bfd_elf_version_tree *t, **pp; |
| 7769 | struct bfd_elf_version_expr *e1; |
| 7770 | |
| 7771 | if (name == NULL) |
| 7772 | name = ""; |
| 7773 | |
| 7774 | if (link_info.version_info != NULL |
| 7775 | && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) |
| 7776 | { |
| 7777 | einfo (_("%X%P: anonymous version tag cannot be combined" |
| 7778 | " with other version tags\n")); |
| 7779 | free (version); |
| 7780 | return; |
| 7781 | } |
| 7782 | |
| 7783 | /* Make sure this node has a unique name. */ |
| 7784 | for (t = link_info.version_info; t != NULL; t = t->next) |
| 7785 | if (strcmp (t->name, name) == 0) |
| 7786 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); |
| 7787 | |
| 7788 | lang_finalize_version_expr_head (&version->globals); |
| 7789 | lang_finalize_version_expr_head (&version->locals); |
| 7790 | |
| 7791 | /* Check the global and local match names, and make sure there |
| 7792 | aren't any duplicates. */ |
| 7793 | |
| 7794 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
| 7795 | { |
| 7796 | for (t = link_info.version_info; t != NULL; t = t->next) |
| 7797 | { |
| 7798 | struct bfd_elf_version_expr *e2; |
| 7799 | |
| 7800 | if (t->locals.htab && e1->literal) |
| 7801 | { |
| 7802 | e2 = (struct bfd_elf_version_expr *) |
| 7803 | htab_find ((htab_t) t->locals.htab, e1); |
| 7804 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
| 7805 | { |
| 7806 | if (e1->mask == e2->mask) |
| 7807 | einfo (_("%X%P: duplicate expression `%s'" |
| 7808 | " in version information\n"), e1->pattern); |
| 7809 | e2 = e2->next; |
| 7810 | } |
| 7811 | } |
| 7812 | else if (!e1->literal) |
| 7813 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
| 7814 | if (strcmp (e1->pattern, e2->pattern) == 0 |
| 7815 | && e1->mask == e2->mask) |
| 7816 | einfo (_("%X%P: duplicate expression `%s'" |
| 7817 | " in version information\n"), e1->pattern); |
| 7818 | } |
| 7819 | } |
| 7820 | |
| 7821 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
| 7822 | { |
| 7823 | for (t = link_info.version_info; t != NULL; t = t->next) |
| 7824 | { |
| 7825 | struct bfd_elf_version_expr *e2; |
| 7826 | |
| 7827 | if (t->globals.htab && e1->literal) |
| 7828 | { |
| 7829 | e2 = (struct bfd_elf_version_expr *) |
| 7830 | htab_find ((htab_t) t->globals.htab, e1); |
| 7831 | while (e2 && strcmp (e1->pattern, e2->pattern) == 0) |
| 7832 | { |
| 7833 | if (e1->mask == e2->mask) |
| 7834 | einfo (_("%X%P: duplicate expression `%s'" |
| 7835 | " in version information\n"), |
| 7836 | e1->pattern); |
| 7837 | e2 = e2->next; |
| 7838 | } |
| 7839 | } |
| 7840 | else if (!e1->literal) |
| 7841 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
| 7842 | if (strcmp (e1->pattern, e2->pattern) == 0 |
| 7843 | && e1->mask == e2->mask) |
| 7844 | einfo (_("%X%P: duplicate expression `%s'" |
| 7845 | " in version information\n"), e1->pattern); |
| 7846 | } |
| 7847 | } |
| 7848 | |
| 7849 | version->deps = deps; |
| 7850 | version->name = name; |
| 7851 | if (name[0] != '\0') |
| 7852 | { |
| 7853 | ++version_index; |
| 7854 | version->vernum = version_index; |
| 7855 | } |
| 7856 | else |
| 7857 | version->vernum = 0; |
| 7858 | |
| 7859 | for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) |
| 7860 | ; |
| 7861 | *pp = version; |
| 7862 | } |
| 7863 | |
| 7864 | /* This is called when we see a version dependency. */ |
| 7865 | |
| 7866 | struct bfd_elf_version_deps * |
| 7867 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
| 7868 | { |
| 7869 | struct bfd_elf_version_deps *ret; |
| 7870 | struct bfd_elf_version_tree *t; |
| 7871 | |
| 7872 | ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret); |
| 7873 | ret->next = list; |
| 7874 | |
| 7875 | for (t = link_info.version_info; t != NULL; t = t->next) |
| 7876 | { |
| 7877 | if (strcmp (t->name, name) == 0) |
| 7878 | { |
| 7879 | ret->version_needed = t; |
| 7880 | return ret; |
| 7881 | } |
| 7882 | } |
| 7883 | |
| 7884 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); |
| 7885 | |
| 7886 | ret->version_needed = NULL; |
| 7887 | return ret; |
| 7888 | } |
| 7889 | |
| 7890 | static void |
| 7891 | lang_do_version_exports_section (void) |
| 7892 | { |
| 7893 | struct bfd_elf_version_expr *greg = NULL, *lreg; |
| 7894 | |
| 7895 | LANG_FOR_EACH_INPUT_STATEMENT (is) |
| 7896 | { |
| 7897 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); |
| 7898 | char *contents, *p; |
| 7899 | bfd_size_type len; |
| 7900 | |
| 7901 | if (sec == NULL) |
| 7902 | continue; |
| 7903 | |
| 7904 | len = sec->size; |
| 7905 | contents = (char *) xmalloc (len); |
| 7906 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
| 7907 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
| 7908 | |
| 7909 | p = contents; |
| 7910 | while (p < contents + len) |
| 7911 | { |
| 7912 | greg = lang_new_vers_pattern (greg, p, NULL, FALSE); |
| 7913 | p = strchr (p, '\0') + 1; |
| 7914 | } |
| 7915 | |
| 7916 | /* Do not free the contents, as we used them creating the regex. */ |
| 7917 | |
| 7918 | /* Do not include this section in the link. */ |
| 7919 | sec->flags |= SEC_EXCLUDE | SEC_KEEP; |
| 7920 | } |
| 7921 | |
| 7922 | lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE); |
| 7923 | lang_register_vers_node (command_line.version_exports_section, |
| 7924 | lang_new_vers_node (greg, lreg), NULL); |
| 7925 | } |
| 7926 | |
| 7927 | void |
| 7928 | lang_add_unique (const char *name) |
| 7929 | { |
| 7930 | struct unique_sections *ent; |
| 7931 | |
| 7932 | for (ent = unique_section_list; ent; ent = ent->next) |
| 7933 | if (strcmp (ent->name, name) == 0) |
| 7934 | return; |
| 7935 | |
| 7936 | ent = (struct unique_sections *) xmalloc (sizeof *ent); |
| 7937 | ent->name = xstrdup (name); |
| 7938 | ent->next = unique_section_list; |
| 7939 | unique_section_list = ent; |
| 7940 | } |
| 7941 | |
| 7942 | /* Append the list of dynamic symbols to the existing one. */ |
| 7943 | |
| 7944 | void |
| 7945 | lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic) |
| 7946 | { |
| 7947 | if (link_info.dynamic_list) |
| 7948 | { |
| 7949 | struct bfd_elf_version_expr *tail; |
| 7950 | for (tail = dynamic; tail->next != NULL; tail = tail->next) |
| 7951 | ; |
| 7952 | tail->next = link_info.dynamic_list->head.list; |
| 7953 | link_info.dynamic_list->head.list = dynamic; |
| 7954 | } |
| 7955 | else |
| 7956 | { |
| 7957 | struct bfd_elf_dynamic_list *d; |
| 7958 | |
| 7959 | d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d); |
| 7960 | d->head.list = dynamic; |
| 7961 | d->match = lang_vers_match; |
| 7962 | link_info.dynamic_list = d; |
| 7963 | } |
| 7964 | } |
| 7965 | |
| 7966 | /* Append the list of C++ typeinfo dynamic symbols to the existing |
| 7967 | one. */ |
| 7968 | |
| 7969 | void |
| 7970 | lang_append_dynamic_list_cpp_typeinfo (void) |
| 7971 | { |
| 7972 | const char * symbols [] = |
| 7973 | { |
| 7974 | "typeinfo name for*", |
| 7975 | "typeinfo for*" |
| 7976 | }; |
| 7977 | struct bfd_elf_version_expr *dynamic = NULL; |
| 7978 | unsigned int i; |
| 7979 | |
| 7980 | for (i = 0; i < ARRAY_SIZE (symbols); i++) |
| 7981 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", |
| 7982 | FALSE); |
| 7983 | |
| 7984 | lang_append_dynamic_list (dynamic); |
| 7985 | } |
| 7986 | |
| 7987 | /* Append the list of C++ operator new and delete dynamic symbols to the |
| 7988 | existing one. */ |
| 7989 | |
| 7990 | void |
| 7991 | lang_append_dynamic_list_cpp_new (void) |
| 7992 | { |
| 7993 | const char * symbols [] = |
| 7994 | { |
| 7995 | "operator new*", |
| 7996 | "operator delete*" |
| 7997 | }; |
| 7998 | struct bfd_elf_version_expr *dynamic = NULL; |
| 7999 | unsigned int i; |
| 8000 | |
| 8001 | for (i = 0; i < ARRAY_SIZE (symbols); i++) |
| 8002 | dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", |
| 8003 | FALSE); |
| 8004 | |
| 8005 | lang_append_dynamic_list (dynamic); |
| 8006 | } |
| 8007 | |
| 8008 | /* Scan a space and/or comma separated string of features. */ |
| 8009 | |
| 8010 | void |
| 8011 | lang_ld_feature (char *str) |
| 8012 | { |
| 8013 | char *p, *q; |
| 8014 | |
| 8015 | p = str; |
| 8016 | while (*p) |
| 8017 | { |
| 8018 | char sep; |
| 8019 | while (*p == ',' || ISSPACE (*p)) |
| 8020 | ++p; |
| 8021 | if (!*p) |
| 8022 | break; |
| 8023 | q = p + 1; |
| 8024 | while (*q && *q != ',' && !ISSPACE (*q)) |
| 8025 | ++q; |
| 8026 | sep = *q; |
| 8027 | *q = 0; |
| 8028 | if (strcasecmp (p, "SANE_EXPR") == 0) |
| 8029 | config.sane_expr = TRUE; |
| 8030 | else |
| 8031 | einfo (_("%X%P: unknown feature `%s'\n"), p); |
| 8032 | *q = sep; |
| 8033 | p = q; |
| 8034 | } |
| 8035 | } |