1 /* Linker command language support. 2 Copyright (C) 1991-2025 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 <limits.h> 23 #include "bfd.h" 24 #include "libiberty.h" 25 #include "filenames.h" 26 #include "safe-ctype.h" 27 #include "obstack.h" 28 #include "bfdlink.h" 29 #include "ctf-api.h" 30 #include "ld.h" 31 #include "ldmain.h" 32 #include "ldexp.h" 33 #include "ldlang.h" 34 #include <ldgram.h> 35 #include "ldlex.h" 36 #include "ldmisc.h" 37 #include "ldctor.h" 38 #include "ldfile.h" 39 #include "ldemul.h" 40 #include "ldwrite.h" 41 #include "fnmatch.h" 42 #include "demangle.h" 43 #include "hashtab.h" 44 #include "elf-bfd.h" 45 #include "bfdver.h" 46 #include <errno.h> 47 48 #if BFD_SUPPORTS_PLUGINS 49 #include "plugin.h" 50 #endif 51 52 /* FIXME: Put it here to avoid NAME conflict from ldgram.h. */ 53 #include "elf-bfd.h" 54 55 #ifndef offsetof 56 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) 57 #endif 58 59 /* Convert between addresses in bytes and sizes in octets. 60 For currently supported targets, octets_per_byte is always a power 61 of two, so we can use shifts. */ 62 #define TO_ADDR(X) ((X) >> opb_shift) 63 #define TO_SIZE(X) ((X) << opb_shift) 64 65 /* Local variables. */ 66 static struct obstack stat_obstack; 67 static struct obstack map_obstack; 68 static struct obstack pt_obstack; 69 70 #define obstack_chunk_alloc xmalloc 71 #define obstack_chunk_free free 72 static const char *entry_symbol_default = "start"; 73 static bool map_head_is_link_order = false; 74 static lang_output_section_statement_type *default_common_section; 75 static bool map_option_f; 76 static bfd_vma print_dot; 77 static lang_input_statement_type *first_file; 78 static const char *current_target; 79 static lang_statement_list_type *stat_save[10]; 80 static lang_statement_list_type **stat_save_ptr = &stat_save[0]; 81 static struct unique_sections *unique_section_list; 82 static struct asneeded_minfo *asneeded_list_head; 83 static unsigned int opb_shift = 0; 84 static cmdline_list_type cmdline_object_only_file_list; 85 static cmdline_list_type cmdline_object_only_archive_list; 86 static cmdline_list_type cmdline_temp_object_only_list; 87 88 /* Forward declarations. */ 89 static void exp_init_os (etree_type *); 90 static lang_input_statement_type *lookup_name (const char *); 91 static bool wont_add_section_p (asection *, 92 lang_output_section_statement_type *); 93 static void insert_undefined (const char *); 94 static bool sort_def_symbol (struct bfd_link_hash_entry *, void *); 95 static lang_statement_union_type *new_statement (enum statement_enum type, 96 size_t size, 97 lang_statement_list_type *list); 98 static void print_statement (lang_statement_union_type *, 99 lang_output_section_statement_type *); 100 static void print_statement_list (lang_statement_union_type *, 101 lang_output_section_statement_type *); 102 static void print_statements (void); 103 static void print_input_section (asection *, bool); 104 static bool lang_one_common (struct bfd_link_hash_entry *, void *); 105 static void lang_record_phdrs (void); 106 static void lang_do_version_exports_section (void); 107 static void lang_finalize_version_expr_head 108 (struct bfd_elf_version_expr_head *); 109 static void lang_do_memory_regions (bool); 110 static void cmdline_lists_init (void); 111 static void cmdline_get_object_only_input_files (void); 112 static void print_cmdline_list (cmdline_union_type *); 113 static bool cmdline_on_object_only_archive_list_p (bfd *); 114 115 /* Exported variables. */ 116 const char *output_target; 117 lang_output_section_statement_type *abs_output_section; 118 /* Header for list of statements corresponding to any files involved in the 119 link, either specified from the command-line or added implicitely (eg. 120 archive member used to resolved undefined symbol, wildcard statement from 121 linker script, etc.). Next pointer is in next field of a 122 lang_statement_header_type (reached via header field in a 123 lang_statement_union). */ 124 lang_statement_list_type statement_list; 125 lang_statement_list_type lang_os_list; 126 lang_statement_list_type *stat_ptr = &statement_list; 127 /* Header for list of statements corresponding to files used in the final 128 executable. This can be either object file specified on the command-line 129 or library member resolving an undefined reference. Next pointer is in next 130 field of a lang_input_statement_type (reached via input_statement field in a 131 lang_statement_union). */ 132 lang_statement_list_type file_chain = { NULL, NULL }; 133 /* Header for list of statements corresponding to files specified on the 134 command-line for linking. It thus contains real object files and archive 135 but not archive members. Next pointer is in next_real_file field of a 136 lang_input_statement_type statement (reached via input_statement field in a 137 lang_statement_union). */ 138 lang_statement_list_type input_file_chain; 139 static const char *current_input_file; 140 struct bfd_elf_dynamic_list **current_dynamic_list_p; 141 struct bfd_sym_chain entry_symbol = { NULL, NULL }; 142 const char *entry_section = ".text"; 143 struct lang_input_statement_flags input_flags; 144 bool entry_from_cmdline; 145 bool lang_has_input_file = false; 146 bool had_output_filename = false; 147 bool lang_float_flag = false; 148 bool delete_output_file_on_failure = false; 149 bool enable_linker_version = false; 150 struct lang_phdr *lang_phdr_list; 151 struct lang_nocrossrefs *nocrossref_list; 152 struct asneeded_minfo **asneeded_list_tail; 153 #ifdef ENABLE_LIBCTF 154 static ctf_dict_t *ctf_output; 155 #endif 156 157 /* Functions that traverse the linker script and might evaluate 158 DEFINED() need to increment this at the start of the traversal. */ 159 int lang_statement_iteration = 0; 160 161 /* Count times through one_lang_size_sections_pass after mark phase. */ 162 static int lang_sizing_iteration = 0; 163 164 /* Return TRUE if the PATTERN argument is a wildcard pattern. 165 Although backslashes are treated specially if a pattern contains 166 wildcards, we do not consider the mere presence of a backslash to 167 be enough to cause the pattern to be treated as a wildcard. 168 That lets us handle DOS filenames more naturally. */ 169 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL) 170 171 #define new_stat(x, y) \ 172 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) 173 174 #define outside_section_address(q) \ 175 ((q)->output_offset + (q)->output_section->vma) 176 177 #define outside_symbol_address(q) \ 178 ((q)->value + outside_section_address (q->section)) 179 180 /* CTF sections smaller than this are not compressed: compression of 181 dictionaries this small doesn't gain much, and this lets consumers mmap the 182 sections directly out of the ELF file and use them with no decompression 183 overhead if they want to. */ 184 #define CTF_COMPRESSION_THRESHOLD 4096 185 186 void * 187 stat_alloc (size_t size) 188 { 189 return obstack_alloc (&stat_obstack, size); 190 } 191 192 void 193 stat_free (void *str) 194 { 195 obstack_free (&stat_obstack, str); 196 } 197 198 void * 199 stat_memdup (const void *src, size_t copy_size, size_t alloc_size) 200 { 201 void *ret = obstack_alloc (&stat_obstack, alloc_size); 202 memcpy (ret, src, copy_size); 203 if (alloc_size > copy_size) 204 memset ((char *) ret + copy_size, 0, alloc_size - copy_size); 205 return ret; 206 } 207 208 char * 209 stat_strdup (const char *str) 210 { 211 size_t len = strlen (str) + 1; 212 return stat_memdup (str, len, len); 213 } 214 215 char * 216 stat_concat (const char *first, ...) 217 { 218 va_list args; 219 va_start (args, first); 220 221 size_t length = 0; 222 for (const char *arg = first; arg; arg = va_arg (args, const char *)) 223 length += strlen (arg); 224 va_end (args); 225 char *new_str = stat_alloc (length + 1); 226 227 va_start (args, first); 228 char *end = new_str; 229 for (const char *arg = first; arg; arg = va_arg (args, const char *)) 230 { 231 length = strlen (arg); 232 memcpy (end, arg, length); 233 end += length; 234 } 235 *end = 0; 236 va_end (args); 237 return new_str; 238 } 239 240 /* Code for handling simple wildcards without going through fnmatch, 241 which can be expensive because of charset translations etc. */ 242 243 /* A simple wild is a literal string followed by a single '*', 244 where the literal part is at least 4 characters long. */ 245 246 static bool 247 is_simple_wild (const char *name) 248 { 249 size_t len = strcspn (name, "*?["); 250 return len >= 4 && name[len] == '*' && name[len + 1] == '\0'; 251 } 252 253 static bool 254 match_simple_wild (const char *pattern, const char *name) 255 { 256 /* The first four characters of the pattern are guaranteed valid 257 non-wildcard characters. So we can go faster. */ 258 if (pattern[0] != name[0] || pattern[1] != name[1] 259 || pattern[2] != name[2] || pattern[3] != name[3]) 260 return false; 261 262 pattern += 4; 263 name += 4; 264 while (*pattern != '*') 265 if (*name++ != *pattern++) 266 return false; 267 268 return true; 269 } 270 271 static int 272 name_match (const char *pattern, const char *name) 273 { 274 if (is_simple_wild (pattern)) 275 return !match_simple_wild (pattern, name); 276 if (wildcardp (pattern)) 277 return fnmatch (pattern, name, 0); 278 return strcmp (pattern, name); 279 } 280 281 /* Given an analyzed wildcard_spec SPEC, match it against NAME, 282 returns zero on a match, non-zero if there's no match. */ 283 284 static int 285 spec_match (const struct wildcard_spec *spec, const char *name) 286 { 287 size_t nl = spec->namelen; 288 size_t pl = spec->prefixlen; 289 size_t sl = spec->suffixlen; 290 size_t inputlen = strlen (name); 291 int r; 292 293 if (pl) 294 { 295 if (inputlen < pl) 296 return 1; 297 298 r = memcmp (spec->name, name, pl); 299 if (r) 300 return r; 301 } 302 303 if (sl) 304 { 305 if (inputlen < sl) 306 return 1; 307 308 r = memcmp (spec->name + nl - sl, name + inputlen - sl, sl); 309 if (r) 310 return r; 311 } 312 313 if (nl == pl + sl + 1 && spec->name[pl] == '*') 314 return 0; 315 316 if (nl > pl) 317 return fnmatch (spec->name + pl, name + pl, 0); 318 319 if (inputlen >= nl) 320 return name[nl]; 321 322 return 0; 323 } 324 325 static char * 326 stat_ldirname (const char *name) 327 { 328 const char *base = lbasename (name); 329 330 while (base > name && IS_DIR_SEPARATOR (base[-1])) 331 --base; 332 size_t len = base - name; 333 if (len == 0) 334 return "."; 335 return stat_memdup (name, len, len + 1); 336 } 337 338 /* If PATTERN is of the form archive:file, return a pointer to the 339 separator. If not, return NULL. */ 340 341 static char * 342 archive_path (const char *pattern) 343 { 344 char *p = NULL; 345 346 if (link_info.path_separator == 0) 347 return p; 348 349 p = strchr (pattern, link_info.path_separator); 350 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 351 if (p == NULL || link_info.path_separator != ':') 352 return p; 353 354 /* Assume a match on the second char is part of drive specifier, 355 as in "c:\silly.dos". */ 356 if (p == pattern + 1 && ISALPHA (*pattern)) 357 p = strchr (p + 1, link_info.path_separator); 358 #endif 359 return p; 360 } 361 362 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path, 363 return whether F matches FILE_SPEC. */ 364 365 static bool 366 input_statement_is_archive_path (const char *file_spec, char *sep, 367 lang_input_statement_type *f) 368 { 369 bool match = false; 370 371 if ((*(sep + 1) == 0 372 || name_match (sep + 1, f->filename) == 0) 373 && ((sep != file_spec) 374 == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL))) 375 { 376 match = true; 377 378 if (sep != file_spec) 379 { 380 const char *aname = bfd_get_filename (f->the_bfd->my_archive); 381 *sep = 0; 382 match = name_match (file_spec, aname) == 0; 383 *sep = link_info.path_separator; 384 } 385 } 386 return match; 387 } 388 389 static bool 390 unique_section_p (const asection *sec, 391 const lang_output_section_statement_type *os) 392 { 393 struct unique_sections *unam; 394 const char *secnam; 395 396 if (!link_info.resolve_section_groups 397 && sec->owner != NULL 398 && bfd_is_group_section (sec->owner, sec)) 399 return !(os != NULL 400 && strcmp (os->name, DISCARD_SECTION_NAME) == 0); 401 402 secnam = sec->name; 403 for (unam = unique_section_list; unam; unam = unam->next) 404 if (name_match (unam->name, secnam) == 0) 405 return true; 406 407 return false; 408 } 409 410 /* Generic traversal routines for finding matching sections. */ 411 412 /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return 413 false. */ 414 415 static bool 416 walk_wild_file_in_exclude_list (struct name_list *exclude_list, 417 lang_input_statement_type *file) 418 { 419 struct name_list *list_tmp; 420 421 for (list_tmp = exclude_list; 422 list_tmp; 423 list_tmp = list_tmp->next) 424 { 425 char *p = archive_path (list_tmp->name); 426 427 if (p != NULL) 428 { 429 if (input_statement_is_archive_path (list_tmp->name, p, file)) 430 return true; 431 } 432 433 else if (name_match (list_tmp->name, file->filename) == 0) 434 return true; 435 436 /* FIXME: Perhaps remove the following at some stage? Matching 437 unadorned archives like this was never documented and has 438 been superceded by the archive:path syntax. */ 439 else if (file->the_bfd != NULL 440 && file->the_bfd->my_archive != NULL 441 && name_match (list_tmp->name, 442 bfd_get_filename (file->the_bfd->my_archive)) == 0) 443 return true; 444 } 445 446 return false; 447 } 448 449 /* Add SECTION (from input FILE) to the list of matching sections 450 within PTR (the matching wildcard is SEC). */ 451 452 static void 453 add_matching_section (lang_wild_statement_type *ptr, 454 struct wildcard_list *sec, 455 asection *section, 456 lang_input_statement_type *file) 457 { 458 lang_input_matcher_type *new_section; 459 /* Add a section reference to the list. */ 460 new_section = new_stat (lang_input_matcher, &ptr->matching_sections); 461 new_section->section = section; 462 new_section->pattern = sec; 463 new_section->input_stmt = file; 464 } 465 466 /* Process section S (from input file FILE) in relation to wildcard 467 statement PTR. We already know that a prefix of the name of S matches 468 some wildcard in PTR's wildcard list. Here we check if the filename 469 matches as well (if it's specified) and if any of the wildcards in fact 470 does match. */ 471 472 static void 473 walk_wild_section_match (lang_wild_statement_type *ptr, 474 lang_input_statement_type *file, 475 asection *s) 476 { 477 struct wildcard_list *sec; 478 const char *file_spec = ptr->filename; 479 char *p; 480 481 /* Check if filenames match. */ 482 if (file_spec == NULL) 483 ; 484 else if ((p = archive_path (file_spec)) != NULL) 485 { 486 if (!input_statement_is_archive_path (file_spec, p, file)) 487 return; 488 } 489 else if (wildcardp (file_spec)) 490 { 491 if (fnmatch (file_spec, file->filename, 0) != 0) 492 return; 493 } 494 else 495 { 496 /* XXX Matching against non-wildcard filename in wild statements 497 was done by going through lookup_name, which uses 498 ->local_sym_name to compare against, not ->filename. We retain 499 this behaviour even though the above code paths use filename. 500 It would be more logical to use it here as well, in which 501 case the above wildcard() arm could be folded into this by using 502 name_match. This would also solve the worry of what to do 503 about unset local_sym_name (in which case lookup_name simply adds 504 the input file again). */ 505 const char *filename = file->local_sym_name; 506 lang_input_statement_type *arch_is; 507 if (filename && filename_cmp (filename, file_spec) == 0) 508 ; 509 /* FIXME: see also walk_wild_file_in_exclude_list for why we 510 also check parents BFD (local_sym_)name to match input statements 511 with unadorned archive names. */ 512 else if (file->the_bfd 513 && file->the_bfd->my_archive 514 && (arch_is = bfd_usrdata (file->the_bfd->my_archive)) 515 && arch_is->local_sym_name 516 && filename_cmp (arch_is->local_sym_name, file_spec) == 0) 517 ; 518 else 519 return; 520 } 521 522 /* If filename is excluded we're done. */ 523 if (walk_wild_file_in_exclude_list (ptr->exclude_name_list, file)) 524 return; 525 526 /* Check section name against each wildcard spec. If there's no 527 wildcard all sections match. */ 528 sec = ptr->section_list; 529 if (sec == NULL) 530 add_matching_section (ptr, sec, s, file); 531 else 532 { 533 const char *sname = bfd_section_name (s); 534 for (; sec != NULL; sec = sec->next) 535 { 536 if (sec->spec.name != NULL 537 && spec_match (&sec->spec, sname) != 0) 538 continue; 539 540 /* Don't process sections from files which were excluded. */ 541 if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, 542 file)) 543 add_matching_section (ptr, sec, s, file); 544 } 545 } 546 } 547 548 /* Return the numerical value of the init_priority attribute from 549 section name NAME. */ 550 551 static int 552 get_init_priority (const asection *sec) 553 { 554 const char *name = bfd_section_name (sec); 555 const char *dot; 556 557 /* GCC uses the following section names for the init_priority 558 attribute with numerical values 101 to 65535 inclusive. A 559 lower value means a higher priority. 560 561 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the 562 decimal numerical value of the init_priority attribute. 563 The order of execution in .init_array is forward and 564 .fini_array is backward. 565 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the 566 decimal numerical value of the init_priority attribute. 567 The order of execution in .ctors is backward and .dtors 568 is forward. 569 570 .init_array.NNNNN sections would normally be placed in an output 571 .init_array section, .fini_array.NNNNN in .fini_array, 572 .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means 573 we should sort by increasing number (and could just use 574 SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are 575 being placed in .init_array (which may also contain 576 .init_array.NNNNN sections) or .dtors.NNNNN sections are being 577 placed in .fini_array then we need to extract the init_priority 578 attribute and sort on that. */ 579 dot = strrchr (name, '.'); 580 if (dot != NULL && ISDIGIT (dot[1])) 581 { 582 char *end; 583 unsigned long init_priority = strtoul (dot + 1, &end, 10); 584 if (*end == 0) 585 { 586 if (dot == name + 6 587 && (strncmp (name, ".ctors", 6) == 0 588 || strncmp (name, ".dtors", 6) == 0)) 589 init_priority = 65535 - init_priority; 590 if (init_priority <= INT_MAX) 591 return init_priority; 592 } 593 } 594 return -1; 595 } 596 597 /* Compare sections ASEC and BSEC according to SORT. */ 598 599 static int 600 compare_section (sort_type sort, asection *asec, asection *bsec, bool reversed) 601 { 602 int ret; 603 int a_priority, b_priority; 604 605 switch (sort) 606 { 607 default: 608 abort (); 609 610 case by_init_priority: 611 a_priority = get_init_priority (asec); 612 b_priority = get_init_priority (bsec); 613 if (a_priority < 0 || b_priority < 0) 614 goto sort_by_name; 615 if (reversed) 616 ret = b_priority - a_priority; 617 else 618 ret = a_priority - b_priority; 619 if (ret) 620 break; 621 else 622 goto sort_by_name; 623 624 case by_alignment_name: 625 ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); 626 if (ret) 627 break; 628 /* Fall through. */ 629 630 case by_name: 631 sort_by_name: 632 if (reversed) 633 ret = strcmp (bfd_section_name (bsec), bfd_section_name (asec)); 634 else 635 ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); 636 break; 637 638 case by_name_alignment: 639 if (reversed) 640 ret = strcmp (bfd_section_name (bsec), bfd_section_name (asec)); 641 else 642 ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec)); 643 if (ret) 644 break; 645 /* Fall through. */ 646 647 case by_alignment: 648 ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec); 649 break; 650 } 651 652 return ret; 653 } 654 655 /* PE puts the sort key in the input statement. */ 656 657 static const char * 658 sort_filename (bfd *abfd) 659 { 660 lang_input_statement_type *is = bfd_usrdata (abfd); 661 if (is->sort_key) 662 return is->sort_key; 663 return bfd_get_filename (abfd); 664 } 665 666 /* Handle wildcard sorting. This returns the place in a binary search tree 667 where this FILE:SECTION should be inserted for wild statement WILD where 668 the spec SEC was the matching one. The tree is later linearized. */ 669 670 static lang_section_bst_type ** 671 wild_sort (lang_wild_statement_type *wild, 672 struct wildcard_list *sec, 673 lang_input_statement_type *file, 674 asection *section) 675 { 676 lang_section_bst_type **tree; 677 678 if (!wild->filenames_sorted 679 && (sec == NULL || sec->spec.sorted == none 680 || sec->spec.sorted == by_none)) 681 { 682 /* We might be called even if _this_ spec doesn't need sorting, 683 in which case we simply append at the right end of tree. */ 684 return wild->rightmost; 685 } 686 687 tree = &wild->tree; 688 while (*tree) 689 { 690 /* Sorting by filename takes precedence over sorting by section 691 name. */ 692 693 if (wild->filenames_sorted) 694 { 695 const char *fn, *ln; 696 bool fa, la; 697 int i; 698 asection *lsec = (*tree)->section; 699 700 /* The PE support for the .idata section as generated by 701 dlltool assumes that files will be sorted by the name of 702 the archive and then the name of the file within the 703 archive. */ 704 705 fa = file->the_bfd->my_archive != NULL; 706 if (fa) 707 fn = sort_filename (file->the_bfd->my_archive); 708 else 709 fn = sort_filename (file->the_bfd); 710 711 la = lsec->owner->my_archive != NULL; 712 if (la) 713 ln = sort_filename (lsec->owner->my_archive); 714 else 715 ln = sort_filename (lsec->owner); 716 717 if (wild->filenames_reversed) 718 i = filename_cmp (ln, fn); 719 else 720 i = filename_cmp (fn, ln); 721 722 if (i > 0) 723 { tree = &((*tree)->right); continue; } 724 else if (i < 0) 725 { tree = &((*tree)->left); continue; } 726 727 if (fa || la) 728 { 729 if (fa) 730 fn = sort_filename (file->the_bfd); 731 if (la) 732 ln = sort_filename (lsec->owner); 733 734 if (wild->filenames_reversed) 735 i = filename_cmp (ln, fn); 736 else 737 i = filename_cmp (fn, ln); 738 739 if (i > 0) 740 { tree = &((*tree)->right); continue; } 741 else if (i < 0) 742 { tree = &((*tree)->left); continue; } 743 } 744 } 745 746 /* Here either the files are not sorted by name, or we are 747 looking at the sections for this file. */ 748 749 /* Find the correct node to append this section. */ 750 if (sec && sec->spec.sorted != none && sec->spec.sorted != by_none 751 && compare_section (sec->spec.sorted, section, (*tree)->section, sec->spec.reversed) < 0) 752 tree = &((*tree)->left); 753 else 754 tree = &((*tree)->right); 755 } 756 757 return tree; 758 } 759 760 /* Use wild_sort to build a BST to sort sections. */ 761 762 static void 763 output_section_callback_sort (lang_wild_statement_type *ptr, 764 struct wildcard_list *sec, 765 asection *section, 766 lang_input_statement_type *file, 767 void *output) 768 { 769 lang_section_bst_type *node; 770 lang_section_bst_type **tree; 771 lang_output_section_statement_type *os; 772 773 os = (lang_output_section_statement_type *) output; 774 775 if (unique_section_p (section, os)) 776 return; 777 778 /* Don't add sections to the tree when we already know that 779 lang_add_section won't do anything with it. */ 780 if (wont_add_section_p (section, os)) 781 return; 782 783 node = stat_alloc (sizeof (*node)); 784 node->left = 0; 785 node->right = 0; 786 node->section = section; 787 node->pattern = ptr->section_list; 788 789 tree = wild_sort (ptr, sec, file, section); 790 if (tree != NULL) 791 { 792 *tree = node; 793 if (tree == ptr->rightmost) 794 ptr->rightmost = &node->right; 795 } 796 } 797 798 /* Convert a sorted sections' BST back to list form. */ 799 800 static void 801 output_section_callback_tree_to_list (lang_wild_statement_type *ptr, 802 lang_section_bst_type *tree, 803 void *output) 804 { 805 if (tree->left) 806 output_section_callback_tree_to_list (ptr, tree->left, output); 807 808 lang_add_section (&ptr->children, tree->section, tree->pattern, 809 ptr->section_flag_list, 810 (lang_output_section_statement_type *) output); 811 812 if (tree->right) 813 output_section_callback_tree_to_list (ptr, tree->right, output); 814 } 815 816 817 /* Sections are matched against wildcard statements via a prefix tree. 819 The prefix tree holds prefixes of all matching patterns (up to the first 820 wildcard character), and the wild statement from which those patterns 821 came. When matching a section name against the tree we're walking through 822 the tree character by character. Each statement we hit is one that 823 potentially matches. This is checked by actually going through the 824 (glob) matching routines. 825 826 When the section name turns out to actually match we record that section 827 in the wild statements list of matching sections. */ 828 829 /* A prefix can be matched by multiple statement, so we need a list of them. */ 830 struct wild_stmt_list 831 { 832 lang_wild_statement_type *stmt; 833 struct wild_stmt_list *next; 834 }; 835 836 /* The prefix tree itself. */ 837 struct prefixtree 838 { 839 /* The list of all children (linked via .next). */ 840 struct prefixtree *child; 841 struct prefixtree *next; 842 /* This tree node is responsible for the prefix of parent plus 'c'. */ 843 char c; 844 /* The statements that potentially can match this prefix. */ 845 struct wild_stmt_list *stmt; 846 }; 847 848 /* We always have a root node in the prefix tree. It corresponds to the 849 empty prefix. E.g. a glob like "*" would sit in this root. */ 850 static struct prefixtree the_root, *ptroot = &the_root; 851 852 /* Given a prefix tree in *TREE, corresponding to prefix P, find or 853 INSERT the tree node corresponding to prefix P+C. */ 854 855 static struct prefixtree * 856 get_prefix_tree (struct prefixtree **tree, char c, bool insert) 857 { 858 struct prefixtree *t; 859 for (t = *tree; t; t = t->next) 860 if (t->c == c) 861 return t; 862 if (!insert) 863 return NULL; 864 t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); 865 t->child = NULL; 866 t->next = *tree; 867 t->c = c; 868 t->stmt = NULL; 869 *tree = t; 870 return t; 871 } 872 873 /* Add STMT to the set of statements that can be matched by the prefix 874 corresponding to prefix tree T. */ 875 876 static void 877 pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) 878 { 879 struct wild_stmt_list *sl, **psl; 880 sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); 881 sl->stmt = stmt; 882 sl->next = NULL; 883 psl = &t->stmt; 884 while (*psl) 885 psl = &(*psl)->next; 886 *psl = sl; 887 } 888 889 /* Insert STMT into the global prefix tree. */ 890 891 static void 892 insert_prefix_tree (lang_wild_statement_type *stmt) 893 { 894 struct wildcard_list *sec; 895 struct prefixtree *t; 896 897 if (!stmt->section_list) 898 { 899 /* If we have no section_list (no wildcards in the wild STMT), 900 then every section name will match, so add this to the root. */ 901 pt_add_stmt (ptroot, stmt); 902 return; 903 } 904 905 for (sec = stmt->section_list; sec; sec = sec->next) 906 { 907 const char *name = sec->spec.name ? sec->spec.name : "*"; 908 char c; 909 t = ptroot; 910 for (; (c = *name); name++) 911 { 912 if (c == '*' || c == '[' || c == '?') 913 break; 914 t = get_prefix_tree (&t->child, c, true); 915 } 916 /* If we hit a glob character, the matching prefix is what we saw 917 until now. If we hit the end of pattern (hence it's no glob) then 918 we can do better: we only need to record a match when a section name 919 completely matches, not merely a prefix, so record the trailing 0 920 as well. */ 921 if (!c) 922 t = get_prefix_tree (&t->child, 0, true); 923 pt_add_stmt (t, stmt); 924 } 925 } 926 927 /* Dump T indented by INDENT spaces. */ 928 929 static void 930 debug_prefix_tree_rec (struct prefixtree *t, int indent) 931 { 932 for (; t; t = t->next) 933 { 934 struct wild_stmt_list *sl; 935 printf ("%*s %c", indent, "", t->c); 936 for (sl = t->stmt; sl; sl = sl->next) 937 { 938 struct wildcard_list *curr; 939 printf (" %p ", sl->stmt); 940 for (curr = sl->stmt->section_list; curr; curr = curr->next) 941 printf ("%s ", curr->spec.name ? curr->spec.name : "*"); 942 } 943 printf ("\n"); 944 debug_prefix_tree_rec (t->child, indent + 2); 945 } 946 } 947 948 /* Dump the global prefix tree. */ 949 950 static void 951 debug_prefix_tree (void) 952 { 953 debug_prefix_tree_rec (ptroot, 2); 954 } 955 956 /* Like strcspn() but start to look from the end to beginning of 957 S. Returns the length of the suffix of S consisting entirely 958 of characters not in REJECT. */ 959 960 static size_t 961 rstrcspn (const char *s, const char *reject) 962 { 963 size_t len = strlen (s), sufflen = 0; 964 while (len--) 965 { 966 char c = s[len]; 967 if (strchr (reject, c) != 0) 968 break; 969 sufflen++; 970 } 971 return sufflen; 972 } 973 974 /* Analyze the wildcards in wild statement PTR to setup various 975 things for quick matching. */ 976 977 static void 978 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) 979 { 980 struct wildcard_list *sec; 981 982 ptr->tree = NULL; 983 ptr->rightmost = &ptr->tree; 984 985 for (sec = ptr->section_list; sec != NULL; sec = sec->next) 986 { 987 if (sec->spec.name) 988 { 989 sec->spec.namelen = strlen (sec->spec.name); 990 sec->spec.prefixlen = strcspn (sec->spec.name, "?*["); 991 sec->spec.suffixlen = rstrcspn (sec->spec.name + sec->spec.prefixlen, 992 "?*]"); 993 } 994 else 995 sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; 996 } 997 998 insert_prefix_tree (ptr); 999 } 1000 1001 /* Match all sections from FILE against the global prefix tree, 1002 and record them into each wild statement that has a match. */ 1003 1004 static void 1005 resolve_wild_sections (lang_input_statement_type *file) 1006 { 1007 asection *s; 1008 1009 if (file->flags.just_syms) 1010 return; 1011 1012 for (s = file->the_bfd->sections; s != NULL; s = s->next) 1013 { 1014 const char *sname = bfd_section_name (s); 1015 char c = 1; 1016 struct prefixtree *t = ptroot; 1017 //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename); 1018 do 1019 { 1020 if (t->stmt) 1021 { 1022 struct wild_stmt_list *sl; 1023 for (sl = t->stmt; sl; sl = sl->next) 1024 { 1025 walk_wild_section_match (sl->stmt, file, s); 1026 //printf (" ZZZ maybe place into %p\n", sl->stmt); 1027 } 1028 } 1029 if (!c) 1030 break; 1031 c = *sname++; 1032 t = get_prefix_tree (&t->child, c, false); 1033 } 1034 while (t); 1035 } 1036 } 1037 1038 /* Match all sections from all input files against the global prefix tree. */ 1039 1040 static void 1041 resolve_wilds (void) 1042 { 1043 LANG_FOR_EACH_INPUT_STATEMENT (f) 1044 { 1045 //printf("XXX %s\n", f->filename); 1046 if (f->the_bfd == NULL 1047 || !bfd_check_format (f->the_bfd, bfd_archive)) 1048 resolve_wild_sections (f); 1049 else 1050 { 1051 bfd *member; 1052 1053 /* This is an archive file. We must map each member of the 1054 archive separately. */ 1055 member = bfd_openr_next_archived_file (f->the_bfd, NULL); 1056 while (member != NULL) 1057 { 1058 /* When lookup_name is called, it will call the add_symbols 1059 entry point for the archive. For each element of the 1060 archive which is included, BFD will call ldlang_add_file, 1061 which will set the usrdata field of the member to the 1062 lang_input_statement. */ 1063 if (bfd_usrdata (member) != NULL) 1064 resolve_wild_sections (bfd_usrdata (member)); 1065 1066 member = bfd_openr_next_archived_file (f->the_bfd, member); 1067 } 1068 } 1069 } 1070 } 1071 1072 /* For each input section that matches wild statement S calls 1073 CALLBACK with DATA. */ 1074 1075 static void 1076 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) 1077 { 1078 lang_statement_union_type *l; 1079 1080 for (l = s->matching_sections.head; l; l = l->header.next) 1081 { 1082 (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, 1083 l->input_matcher.input_stmt, data); 1084 } 1085 } 1086 1087 /* lang_for_each_statement walks the parse tree and calls the provided 1088 function for each node, except those inside output section statements 1089 with constraint set to -1. */ 1090 1091 void 1092 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), 1093 lang_statement_union_type *s) 1094 { 1095 for (; s != NULL; s = s->header.next) 1096 { 1097 func (s); 1098 1099 switch (s->header.type) 1100 { 1101 case lang_constructors_statement_enum: 1102 lang_for_each_statement_worker (func, constructor_list.head); 1103 break; 1104 case lang_output_section_statement_enum: 1105 if (s->output_section_statement.constraint != -1) 1106 lang_for_each_statement_worker 1107 (func, s->output_section_statement.children.head); 1108 break; 1109 case lang_wild_statement_enum: 1110 lang_for_each_statement_worker (func, 1111 s->wild_statement.children.head); 1112 break; 1113 case lang_group_statement_enum: 1114 lang_for_each_statement_worker (func, 1115 s->group_statement.children.head); 1116 break; 1117 case lang_data_statement_enum: 1118 case lang_reloc_statement_enum: 1119 case lang_object_symbols_statement_enum: 1120 case lang_output_statement_enum: 1121 case lang_target_statement_enum: 1122 case lang_input_section_enum: 1123 case lang_input_statement_enum: 1124 case lang_assignment_statement_enum: 1125 case lang_padding_statement_enum: 1126 case lang_address_statement_enum: 1127 case lang_fill_statement_enum: 1128 case lang_insert_statement_enum: 1129 break; 1130 default: 1131 FAIL (); 1132 break; 1133 } 1134 } 1135 } 1136 1137 void 1138 lang_for_each_statement (void (*func) (lang_statement_union_type *)) 1139 { 1140 lang_for_each_statement_worker (func, statement_list.head); 1141 } 1142 1143 /*----------------------------------------------------------------------*/ 1144 1145 void 1146 lang_list_init (lang_statement_list_type *list) 1147 { 1148 list->head = NULL; 1149 list->tail = &list->head; 1150 } 1151 1152 static void 1153 lang_statement_append (lang_statement_list_type *list, 1154 void *element, 1155 void *field) 1156 { 1157 *(list->tail) = element; 1158 list->tail = field; 1159 } 1160 1161 void 1162 push_stat_ptr (lang_statement_list_type *new_ptr) 1163 { 1164 if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0])) 1165 abort (); 1166 *stat_save_ptr++ = stat_ptr; 1167 stat_ptr = new_ptr; 1168 } 1169 1170 void 1171 pop_stat_ptr (void) 1172 { 1173 if (stat_save_ptr <= stat_save) 1174 abort (); 1175 stat_ptr = *--stat_save_ptr; 1176 } 1177 1178 /* Build a new statement node for the parse tree. */ 1179 1180 static lang_statement_union_type * 1181 new_statement (enum statement_enum type, 1182 size_t size, 1183 lang_statement_list_type *list) 1184 { 1185 lang_statement_union_type *new_stmt; 1186 1187 new_stmt = stat_alloc (size); 1188 new_stmt->header.type = type; 1189 new_stmt->header.next = NULL; 1190 lang_statement_append (list, new_stmt, &new_stmt->header.next); 1191 return new_stmt; 1192 } 1193 1194 /* Build a new input file node for the language. There are several 1195 ways in which we treat an input file, eg, we only look at symbols, 1196 or prefix it with a -l etc. 1197 1198 We can be supplied with requests for input files more than once; 1199 they may, for example be split over several lines like foo.o(.text) 1200 foo.o(.data) etc, so when asked for a file we check that we haven't 1201 got it already so we don't duplicate the bfd. */ 1202 1203 static lang_input_statement_type * 1204 new_afile (const char *name, 1205 lang_input_file_enum_type file_type, 1206 const char *target, 1207 const char *from_filename) 1208 { 1209 lang_input_statement_type *p; 1210 1211 lang_has_input_file = true; 1212 1213 /* PR 30632: It is OK for name to be NULL. For example 1214 see the initialization of first_file in lang_init(). */ 1215 if (name != NULL) 1216 { 1217 name = ldfile_possibly_remap_input (name); 1218 /* But if a name is remapped to NULL, it should be ignored. */ 1219 if (name == NULL) 1220 return NULL; 1221 } 1222 1223 p = new_stat (lang_input_statement, stat_ptr); 1224 memset (&p->the_bfd, 0, 1225 sizeof (*p) - offsetof (lang_input_statement_type, the_bfd)); 1226 p->extra_search_path = NULL; 1227 p->target = target; 1228 p->flags.dynamic = input_flags.dynamic; 1229 p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic; 1230 p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular; 1231 p->flags.whole_archive = input_flags.whole_archive; 1232 p->flags.sysrooted = input_flags.sysrooted; 1233 p->sort_key = NULL; 1234 1235 switch (file_type) 1236 { 1237 case lang_input_file_is_symbols_only_enum: 1238 p->filename = name; 1239 p->local_sym_name = name; 1240 p->flags.real = true; 1241 p->flags.just_syms = true; 1242 break; 1243 case lang_input_file_is_fake_enum: 1244 p->filename = name; 1245 p->local_sym_name = name; 1246 break; 1247 case lang_input_file_is_l_enum: 1248 if (name[0] == ':' && name[1] != '\0') 1249 { 1250 p->filename = name + 1; 1251 p->flags.full_name_provided = true; 1252 } 1253 else 1254 p->filename = name; 1255 p->local_sym_name = concat ("-l", name, (const char *) NULL); 1256 p->flags.maybe_archive = true; 1257 p->flags.real = true; 1258 p->flags.search_dirs = true; 1259 break; 1260 case lang_input_file_is_marker_enum: 1261 p->filename = name; 1262 p->local_sym_name = name; 1263 p->flags.search_dirs = true; 1264 break; 1265 case lang_input_file_is_search_file_enum: 1266 p->filename = name; 1267 p->local_sym_name = name; 1268 /* If name is a relative path, search the directory of the current linker 1269 script first. */ 1270 if (from_filename && !IS_ABSOLUTE_PATH (name)) 1271 p->extra_search_path = stat_ldirname (from_filename); 1272 p->flags.real = true; 1273 p->flags.search_dirs = true; 1274 break; 1275 case lang_input_file_is_file_enum: 1276 p->filename = name; 1277 p->local_sym_name = name; 1278 p->flags.real = true; 1279 break; 1280 default: 1281 FAIL (); 1282 } 1283 1284 lang_statement_append (&input_file_chain, p, &p->next_real_file); 1285 return p; 1286 } 1287 1288 lang_input_statement_type * 1289 lang_add_input_file (const char *name, 1290 lang_input_file_enum_type file_type, 1291 const char *target) 1292 { 1293 if (name != NULL 1294 && (*name == '=' || startswith (name, "$SYSROOT"))) 1295 { 1296 lang_input_statement_type *ret; 1297 char *sysrooted_name 1298 = concat (ld_sysroot, 1299 name + (*name == '=' ? 1 : strlen ("$SYSROOT")), 1300 (const char *) NULL); 1301 1302 /* We've now forcibly prepended the sysroot, making the input 1303 file independent of the context. Therefore, temporarily 1304 force a non-sysrooted context for this statement, so it won't 1305 get the sysroot prepended again when opened. (N.B. if it's a 1306 script, any child nodes with input files starting with "/" 1307 will be handled as "sysrooted" as they'll be found to be 1308 within the sysroot subdirectory.) */ 1309 unsigned int outer_sysrooted = input_flags.sysrooted; 1310 input_flags.sysrooted = 0; 1311 ret = new_afile (sysrooted_name, file_type, target, NULL); 1312 input_flags.sysrooted = outer_sysrooted; 1313 return ret; 1314 } 1315 1316 return new_afile (name, file_type, target, current_input_file); 1317 } 1318 1319 struct out_section_hash_entry 1320 { 1321 struct bfd_hash_entry root; 1322 lang_statement_union_type s; 1323 struct out_section_hash_entry *tail; 1324 }; 1325 1326 /* The hash table. */ 1327 1328 static struct bfd_hash_table output_section_statement_table; 1329 1330 /* Support routines for the hash table used by lang_output_section_find, 1331 initialize the table, fill in an entry and remove the table. */ 1332 1333 static struct out_section_hash_entry * 1334 output_section_statement_newfunc_1 (struct bfd_hash_entry *entry, 1335 struct bfd_hash_table *table, 1336 const char *string) 1337 { 1338 lang_output_section_statement_type **nextp; 1339 struct out_section_hash_entry *ret; 1340 1341 if (entry == NULL) 1342 { 1343 entry = (struct bfd_hash_entry *) bfd_hash_allocate (table, 1344 sizeof (*ret)); 1345 if (entry == NULL) 1346 return NULL; 1347 } 1348 1349 entry = bfd_hash_newfunc (entry, table, string); 1350 if (entry == NULL) 1351 return NULL; 1352 1353 ret = (struct out_section_hash_entry *) entry; 1354 memset (&ret->s, 0, sizeof (ret->s)); 1355 ret->s.header.type = lang_output_section_statement_enum; 1356 ret->s.output_section_statement.subsection_alignment = NULL; 1357 ret->s.output_section_statement.section_alignment = NULL; 1358 ret->s.output_section_statement.block_value = 1; 1359 lang_list_init (&ret->s.output_section_statement.children); 1360 lang_list_init (&ret->s.output_section_statement.sort_children); 1361 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); 1362 1363 /* For every output section statement added to the list, except the 1364 first one, lang_os_list.tail points to the "next" 1365 field of the last element of the list. */ 1366 if (lang_os_list.head != NULL) 1367 ret->s.output_section_statement.prev 1368 = ((lang_output_section_statement_type *) 1369 ((char *) lang_os_list.tail 1370 - offsetof (lang_output_section_statement_type, next))); 1371 1372 /* GCC's strict aliasing rules prevent us from just casting the 1373 address, so we store the pointer in a variable and cast that 1374 instead. */ 1375 nextp = &ret->s.output_section_statement.next; 1376 lang_statement_append (&lang_os_list, &ret->s, nextp); 1377 return ret; 1378 } 1379 1380 static struct bfd_hash_entry * 1381 output_section_statement_newfunc (struct bfd_hash_entry *entry, 1382 struct bfd_hash_table *table, 1383 const char *string) 1384 { 1385 struct out_section_hash_entry *ret; 1386 1387 ret = output_section_statement_newfunc_1 (entry, table, string); 1388 if (ret == NULL) 1389 return NULL; 1390 ret->tail = ret; 1391 return &ret->root; 1392 } 1393 1394 static void 1395 output_section_statement_table_init (void) 1396 { 1397 if (!bfd_hash_table_init_n (&output_section_statement_table, 1398 output_section_statement_newfunc, 1399 sizeof (struct out_section_hash_entry), 1400 61)) 1401 fatal (_("%P: can not create hash table: %E\n")); 1402 } 1403 1404 static void 1405 output_section_statement_table_free (void) 1406 { 1407 bfd_hash_table_free (&output_section_statement_table); 1408 } 1409 1410 /* Build enough state so that the parser can build its tree. */ 1411 1412 void 1413 lang_init (bool object_only) 1414 { 1415 if (!object_only) 1416 { 1417 obstack_begin (&stat_obstack, 1000); 1418 obstack_init (&pt_obstack); 1419 } 1420 1421 stat_ptr = &statement_list; 1422 1423 output_section_statement_table_init (); 1424 1425 cmdline_lists_init (); 1426 1427 lang_list_init (stat_ptr); 1428 1429 lang_list_init (&input_file_chain); 1430 lang_list_init (&lang_os_list); 1431 lang_list_init (&file_chain); 1432 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, 1433 NULL); 1434 abs_output_section = 1435 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1); 1436 1437 abs_output_section->bfd_section = bfd_abs_section_ptr; 1438 1439 asneeded_list_head = NULL; 1440 asneeded_list_tail = &asneeded_list_head; 1441 } 1442 1443 void 1444 lang_finish (void) 1445 { 1446 output_section_statement_table_free (); 1447 ldfile_free (); 1448 } 1449 1450 /*---------------------------------------------------------------------- 1451 A region is an area of memory declared with the 1452 MEMORY { name:org=exp, len=exp ... } 1453 syntax. 1454 1455 We maintain a list of all the regions here. 1456 1457 If no regions are specified in the script, then the default is used 1458 which is created when looked up to be the entire data space. 1459 1460 If create is true we are creating a region inside a MEMORY block. 1461 In this case it is probably an error to create a region that has 1462 already been created. If we are not inside a MEMORY block it is 1463 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) 1464 and so we issue a warning. 1465 1466 Each region has at least one name. The first name is either 1467 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add 1468 alias names to an existing region within a script with 1469 REGION_ALIAS (alias, region_name). Each name corresponds to at most one 1470 region. */ 1471 1472 static lang_memory_region_type *lang_memory_region_list; 1473 static lang_memory_region_type **lang_memory_region_list_tail 1474 = &lang_memory_region_list; 1475 1476 lang_memory_region_type * 1477 lang_memory_region_lookup (const char *const name, bool create) 1478 { 1479 lang_memory_region_name *n; 1480 lang_memory_region_type *r; 1481 lang_memory_region_type *new_region; 1482 1483 /* NAME is NULL for LMA memspecs if no region was specified. */ 1484 if (name == NULL) 1485 return NULL; 1486 1487 for (r = lang_memory_region_list; r != NULL; r = r->next) 1488 for (n = &r->name_list; n != NULL; n = n->next) 1489 if (strcmp (n->name, name) == 0) 1490 { 1491 if (create) 1492 einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"), 1493 NULL, name); 1494 return r; 1495 } 1496 1497 if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) 1498 einfo (_("%P:%pS: warning: memory region `%s' not declared\n"), 1499 NULL, name); 1500 1501 new_region = stat_alloc (sizeof (lang_memory_region_type)); 1502 1503 new_region->name_list.name = stat_strdup (name); 1504 new_region->name_list.next = NULL; 1505 new_region->next = NULL; 1506 new_region->origin_exp = NULL; 1507 new_region->origin = 0; 1508 new_region->length_exp = NULL; 1509 new_region->length = ~(bfd_size_type) 0; 1510 new_region->current = 0; 1511 new_region->last_os = NULL; 1512 new_region->flags = 0; 1513 new_region->not_flags = 0; 1514 new_region->had_full_message = false; 1515 1516 *lang_memory_region_list_tail = new_region; 1517 lang_memory_region_list_tail = &new_region->next; 1518 1519 return new_region; 1520 } 1521 1522 void 1523 lang_memory_region_alias (const char *alias, const char *region_name) 1524 { 1525 lang_memory_region_name *n; 1526 lang_memory_region_type *r; 1527 lang_memory_region_type *region; 1528 1529 /* The default region must be unique. This ensures that it is not necessary 1530 to iterate through the name list if someone wants the check if a region is 1531 the default memory region. */ 1532 if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0 1533 || strcmp (alias, DEFAULT_MEMORY_REGION) == 0) 1534 fatal (_("%P:%pS: error: alias for default memory region\n"), NULL); 1535 1536 /* Look for the target region and check if the alias is not already 1537 in use. */ 1538 region = NULL; 1539 for (r = lang_memory_region_list; r != NULL; r = r->next) 1540 for (n = &r->name_list; n != NULL; n = n->next) 1541 { 1542 if (region == NULL && strcmp (n->name, region_name) == 0) 1543 region = r; 1544 if (strcmp (n->name, alias) == 0) 1545 fatal (_("%P:%pS: error: redefinition of memory region " 1546 "alias `%s'\n"), 1547 NULL, alias); 1548 } 1549 1550 /* Check if the target region exists. */ 1551 if (region == NULL) 1552 fatal (_("%P:%pS: error: memory region `%s' " 1553 "for alias `%s' does not exist\n"), 1554 NULL, region_name, alias); 1555 1556 /* Add alias to region name list. */ 1557 n = stat_alloc (sizeof (lang_memory_region_name)); 1558 n->name = stat_strdup (alias); 1559 n->next = region->name_list.next; 1560 region->name_list.next = n; 1561 } 1562 1563 static lang_memory_region_type * 1564 lang_memory_default (asection *section) 1565 { 1566 lang_memory_region_type *p; 1567 1568 flagword sec_flags = section->flags; 1569 1570 /* Override SEC_DATA to mean a writable section. */ 1571 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) 1572 sec_flags |= SEC_DATA; 1573 1574 for (p = lang_memory_region_list; p != NULL; p = p->next) 1575 { 1576 if ((p->flags & sec_flags) != 0 1577 && (p->not_flags & sec_flags) == 0) 1578 { 1579 return p; 1580 } 1581 } 1582 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); 1583 } 1584 1585 /* Get the output section statement directly from the userdata. */ 1586 1587 lang_output_section_statement_type * 1588 lang_output_section_get (const asection *output_section) 1589 { 1590 return bfd_section_userdata (output_section); 1591 } 1592 1593 /* Find or create an output_section_statement with the given NAME. 1594 If CONSTRAINT is non-zero match one with that constraint, otherwise 1595 match any non-negative constraint. If CREATE is 0 return NULL when 1596 no match exists. If CREATE is 1, create an output_section_statement 1597 when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2, 1598 always make a new output_section_statement. */ 1599 1600 lang_output_section_statement_type * 1601 lang_output_section_statement_lookup (const char *name, 1602 int constraint, 1603 int create) 1604 { 1605 struct out_section_hash_entry *entry; 1606 1607 entry = ((struct out_section_hash_entry *) 1608 bfd_hash_lookup (&output_section_statement_table, name, 1609 create != 0, false)); 1610 if (entry == NULL) 1611 { 1612 if (create) 1613 fatal (_("%P: failed creating section `%s': %E\n"), name); 1614 return NULL; 1615 } 1616 1617 if (entry->s.output_section_statement.name != NULL) 1618 { 1619 /* We have a section of this name, but it might not have the correct 1620 constraint. */ 1621 struct out_section_hash_entry *first_ent = entry; 1622 struct out_section_hash_entry *last_ent; 1623 1624 name = entry->s.output_section_statement.name; 1625 if (create != 2 1626 && !(create && constraint == SPECIAL)) 1627 { 1628 do 1629 { 1630 if (constraint == entry->s.output_section_statement.constraint 1631 || (constraint == 0 1632 && entry->s.output_section_statement.constraint >= 0)) 1633 return &entry->s.output_section_statement; 1634 last_ent = entry; 1635 entry = (struct out_section_hash_entry *) entry->root.next; 1636 } 1637 while (entry != NULL 1638 && name == entry->s.output_section_statement.name); 1639 } 1640 else 1641 last_ent = first_ent->tail; 1642 1643 if (!create) 1644 return NULL; 1645 1646 /* Only the first entry needs the tail pointer. */ 1647 entry = bfd_hash_allocate (&output_section_statement_table, 1648 offsetof (struct out_section_hash_entry, tail)); 1649 if (entry != NULL) 1650 entry 1651 = output_section_statement_newfunc_1 (&entry->root, 1652 &output_section_statement_table, 1653 name); 1654 if (entry == NULL) 1655 { 1656 fatal (_("%P: failed creating section `%s': %E\n"), name); 1657 return NULL; 1658 } 1659 entry->root = last_ent->root; 1660 last_ent->root.next = &entry->root; 1661 first_ent->tail = entry; 1662 } 1663 1664 entry->s.output_section_statement.name = name; 1665 entry->s.output_section_statement.constraint = constraint; 1666 entry->s.output_section_statement.dup_output = (create == 2 1667 || constraint == SPECIAL); 1668 return &entry->s.output_section_statement; 1669 } 1670 1671 /* Find the next output_section_statement with the same name as OS. 1672 If CONSTRAINT is non-zero, find one with that constraint otherwise 1673 match any non-negative constraint. */ 1674 1675 lang_output_section_statement_type * 1676 next_matching_output_section_statement (lang_output_section_statement_type *os, 1677 int constraint) 1678 { 1679 /* All output_section_statements are actually part of a 1680 struct out_section_hash_entry. */ 1681 struct out_section_hash_entry *entry = (struct out_section_hash_entry *) 1682 ((char *) os 1683 - offsetof (struct out_section_hash_entry, s.output_section_statement)); 1684 const char *name = os->name; 1685 1686 ASSERT (name == entry->root.string); 1687 do 1688 { 1689 entry = (struct out_section_hash_entry *) entry->root.next; 1690 if (entry == NULL 1691 || name != entry->s.output_section_statement.name) 1692 return NULL; 1693 } 1694 while (constraint != entry->s.output_section_statement.constraint 1695 && (constraint != 0 1696 || entry->s.output_section_statement.constraint < 0)); 1697 1698 return &entry->s.output_section_statement; 1699 } 1700 1701 /* A variant of lang_output_section_find used by place_orphan. 1702 Returns the output statement that should precede a new output 1703 statement for SEC. If an exact match is found on certain flags, 1704 sets *EXACT too. */ 1705 1706 lang_output_section_statement_type * 1707 lang_output_section_find_by_flags (const asection *sec, 1708 flagword sec_flags, 1709 lang_output_section_statement_type **exact, 1710 lang_match_sec_type_func match_type) 1711 { 1712 lang_output_section_statement_type *first, *look, *found; 1713 flagword look_flags, differ; 1714 1715 /* We know the first statement on this list is *ABS*. May as well 1716 skip it. */ 1717 first = (void *) lang_os_list.head; 1718 first = first->next; 1719 1720 /* First try for an exact match. */ 1721 found = NULL; 1722 for (look = first; look; look = look->next) 1723 { 1724 look_flags = look->flags; 1725 if (look->bfd_section != NULL) 1726 { 1727 look_flags = look->bfd_section->flags; 1728 if (match_type && !match_type (link_info.output_bfd, 1729 look->bfd_section, 1730 sec->owner, sec)) 1731 continue; 1732 } 1733 differ = look_flags ^ sec_flags; 1734 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY 1735 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) 1736 found = look; 1737 } 1738 if (found != NULL) 1739 { 1740 if (exact != NULL) 1741 *exact = found; 1742 return found; 1743 } 1744 1745 if ((sec_flags & SEC_CODE) != 0 1746 && (sec_flags & SEC_ALLOC) != 0) 1747 { 1748 /* Try for a rw code section. */ 1749 for (look = first; look; look = look->next) 1750 { 1751 look_flags = look->flags; 1752 if (look->bfd_section != NULL) 1753 { 1754 look_flags = look->bfd_section->flags; 1755 if (match_type && !match_type (link_info.output_bfd, 1756 look->bfd_section, 1757 sec->owner, sec)) 1758 continue; 1759 } 1760 differ = look_flags ^ sec_flags; 1761 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD 1762 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) 1763 found = look; 1764 } 1765 } 1766 else if ((sec_flags & SEC_READONLY) != 0 1767 && (sec_flags & SEC_ALLOC) != 0) 1768 { 1769 /* .rodata can go after .text, .sdata2 after .rodata. */ 1770 for (look = first; look; look = look->next) 1771 { 1772 look_flags = look->flags; 1773 if (look->bfd_section != NULL) 1774 { 1775 look_flags = look->bfd_section->flags; 1776 if (match_type && !match_type (link_info.output_bfd, 1777 look->bfd_section, 1778 sec->owner, sec)) 1779 continue; 1780 } 1781 differ = look_flags ^ sec_flags; 1782 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD 1783 | SEC_READONLY | SEC_SMALL_DATA)) 1784 || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD 1785 | SEC_READONLY)) 1786 && !(look_flags & SEC_SMALL_DATA))) 1787 found = look; 1788 } 1789 } 1790 else if ((sec_flags & SEC_THREAD_LOCAL) != 0 1791 && (sec_flags & SEC_ALLOC) != 0) 1792 { 1793 /* .tdata can go after .data, .tbss after .tdata. Treat .tbss 1794 as if it were a loaded section, and don't use match_type. */ 1795 bool seen_thread_local = false; 1796 1797 match_type = NULL; 1798 for (look = first; look; look = look->next) 1799 { 1800 look_flags = look->flags; 1801 if (look->bfd_section != NULL) 1802 look_flags = look->bfd_section->flags; 1803 1804 differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS); 1805 if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC))) 1806 { 1807 /* .tdata and .tbss must be adjacent and in that order. */ 1808 if (!(look_flags & SEC_LOAD) 1809 && (sec_flags & SEC_LOAD)) 1810 /* ..so if we're at a .tbss section and we're placing 1811 a .tdata section stop looking and return the 1812 previous section. */ 1813 break; 1814 found = look; 1815 seen_thread_local = true; 1816 } 1817 else if (seen_thread_local) 1818 break; 1819 else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD))) 1820 found = look; 1821 } 1822 } 1823 else if ((sec_flags & SEC_SMALL_DATA) != 0 1824 && (sec_flags & SEC_ALLOC) != 0) 1825 { 1826 /* .sdata goes after .data, .sbss after .sdata. */ 1827 for (look = first; look; look = look->next) 1828 { 1829 look_flags = look->flags; 1830 if (look->bfd_section != NULL) 1831 { 1832 look_flags = look->bfd_section->flags; 1833 if (match_type && !match_type (link_info.output_bfd, 1834 look->bfd_section, 1835 sec->owner, sec)) 1836 continue; 1837 } 1838 differ = look_flags ^ sec_flags; 1839 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD 1840 | SEC_THREAD_LOCAL)) 1841 || ((look_flags & SEC_SMALL_DATA) 1842 && !(sec_flags & SEC_HAS_CONTENTS))) 1843 found = look; 1844 } 1845 } 1846 else if ((sec_flags & SEC_HAS_CONTENTS) != 0 1847 && (sec_flags & SEC_ALLOC) != 0) 1848 { 1849 /* .data goes after .rodata. */ 1850 for (look = first; look; look = look->next) 1851 { 1852 look_flags = look->flags; 1853 if (look->bfd_section != NULL) 1854 { 1855 look_flags = look->bfd_section->flags; 1856 if (match_type && !match_type (link_info.output_bfd, 1857 look->bfd_section, 1858 sec->owner, sec)) 1859 continue; 1860 } 1861 differ = look_flags ^ sec_flags; 1862 if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD 1863 | SEC_SMALL_DATA | SEC_THREAD_LOCAL))) 1864 found = look; 1865 } 1866 } 1867 else if ((sec_flags & SEC_ALLOC) != 0) 1868 { 1869 /* .bss goes after any other alloc section. */ 1870 for (look = first; look; look = look->next) 1871 { 1872 look_flags = look->flags; 1873 if (look->bfd_section != NULL) 1874 { 1875 look_flags = look->bfd_section->flags; 1876 if (match_type && !match_type (link_info.output_bfd, 1877 look->bfd_section, 1878 sec->owner, sec)) 1879 continue; 1880 } 1881 differ = look_flags ^ sec_flags; 1882 if (!(differ & SEC_ALLOC)) 1883 found = look; 1884 } 1885 } 1886 else 1887 { 1888 /* non-alloc go last. */ 1889 for (look = first; look; look = look->next) 1890 { 1891 look_flags = look->flags; 1892 if (look->bfd_section != NULL) 1893 look_flags = look->bfd_section->flags; 1894 differ = look_flags ^ sec_flags; 1895 if (!(differ & SEC_DEBUGGING)) 1896 found = look; 1897 } 1898 return found; 1899 } 1900 1901 if (found || !match_type) 1902 return found; 1903 1904 return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL); 1905 } 1906 1907 /* Find the last output section before given output statement. 1908 Used by place_orphan. */ 1909 1910 static asection * 1911 output_prev_sec_find (lang_output_section_statement_type *os) 1912 { 1913 lang_output_section_statement_type *lookup; 1914 1915 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev) 1916 { 1917 if (lookup->constraint < 0) 1918 continue; 1919 1920 if (lookup->bfd_section != NULL) 1921 return lookup->bfd_section; 1922 } 1923 1924 return NULL; 1925 } 1926 1927 /* Look for a suitable place for a new output section statement. The 1928 idea is to skip over anything that might be inside a SECTIONS {} 1929 statement in a script, before we find another output section 1930 statement. Assignments to "dot" before an output section statement 1931 are assumed to belong to it, except in two cases; The first 1932 assignment to dot, and assignments before non-alloc sections. 1933 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or 1934 similar assignments that set the initial address, or we might 1935 insert non-alloc note sections among assignments setting end of 1936 image symbols. */ 1937 1938 static lang_statement_union_type ** 1939 insert_os_after (lang_statement_union_type *after) 1940 { 1941 lang_statement_union_type **where; 1942 lang_statement_union_type **assign = NULL; 1943 bool ignore_first; 1944 1945 ignore_first = after == lang_os_list.head; 1946 1947 for (where = &after->header.next; 1948 *where != NULL; 1949 where = &(*where)->header.next) 1950 { 1951 switch ((*where)->header.type) 1952 { 1953 case lang_assignment_statement_enum: 1954 if (assign == NULL) 1955 { 1956 lang_assignment_statement_type *ass; 1957 1958 ass = &(*where)->assignment_statement; 1959 if (ass->exp->type.node_class != etree_assert 1960 && ass->exp->assign.dst[0] == '.' 1961 && ass->exp->assign.dst[1] == 0) 1962 { 1963 if (!ignore_first) 1964 assign = where; 1965 ignore_first = false; 1966 } 1967 } 1968 continue; 1969 case lang_wild_statement_enum: 1970 case lang_input_section_enum: 1971 case lang_object_symbols_statement_enum: 1972 case lang_fill_statement_enum: 1973 case lang_data_statement_enum: 1974 case lang_reloc_statement_enum: 1975 case lang_padding_statement_enum: 1976 case lang_constructors_statement_enum: 1977 assign = NULL; 1978 ignore_first = false; 1979 continue; 1980 case lang_output_section_statement_enum: 1981 if (assign != NULL) 1982 { 1983 asection *s = (*where)->output_section_statement.bfd_section; 1984 1985 if (s == NULL 1986 || s->map_head.s == NULL 1987 || (s->flags & SEC_ALLOC) != 0) 1988 where = assign; 1989 } 1990 break; 1991 case lang_input_statement_enum: 1992 case lang_address_statement_enum: 1993 case lang_target_statement_enum: 1994 case lang_output_statement_enum: 1995 case lang_group_statement_enum: 1996 case lang_insert_statement_enum: 1997 continue; 1998 case lang_input_matcher_enum: 1999 FAIL (); 2000 } 2001 break; 2002 } 2003 2004 return where; 2005 } 2006 2007 lang_output_section_statement_type * 2008 lang_insert_orphan (asection *s, 2009 const char *secname, 2010 int constraint, 2011 lang_output_section_statement_type *after, 2012 struct orphan_save *place, 2013 etree_type *address, 2014 lang_statement_list_type *add_child) 2015 { 2016 lang_statement_list_type add; 2017 lang_output_section_statement_type *os; 2018 lang_output_section_statement_type **os_tail; 2019 2020 /* If we have found an appropriate place for the output section 2021 statements for this orphan, add them to our own private list, 2022 inserting them later into the global statement list. */ 2023 if (after != NULL) 2024 { 2025 lang_list_init (&add); 2026 push_stat_ptr (&add); 2027 } 2028 2029 if (bfd_link_relocatable (&link_info) 2030 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0) 2031 address = exp_intop (0); 2032 2033 os_tail = (lang_output_section_statement_type **) lang_os_list.tail; 2034 os = lang_enter_output_section_statement ( 2035 secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0); 2036 2037 if (add_child == NULL) 2038 add_child = &os->children; 2039 lang_add_section (add_child, s, NULL, NULL, os); 2040 2041 if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0) 2042 { 2043 const char *region = (after->region 2044 ? after->region->name_list.name 2045 : DEFAULT_MEMORY_REGION); 2046 const char *lma_region = (after->lma_region 2047 ? after->lma_region->name_list.name 2048 : NULL); 2049 lang_leave_output_section_statement (NULL, region, after->phdrs, 2050 lma_region); 2051 } 2052 else 2053 lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL, 2054 NULL); 2055 2056 /* Restore the global list pointer. */ 2057 if (after != NULL) 2058 pop_stat_ptr (); 2059 2060 if (after != NULL && os->bfd_section != NULL) 2061 { 2062 asection *snew, *as; 2063 bool place_after = place->stmt == NULL; 2064 bool insert_after = true; 2065 2066 snew = os->bfd_section; 2067 2068 /* Shuffle the bfd section list to make the output file look 2069 neater. This is really only cosmetic. */ 2070 if (place->section == NULL 2071 && after != (void *) lang_os_list.head) 2072 { 2073 asection *bfd_section = after->bfd_section; 2074 2075 /* If the output statement hasn't been used to place any input 2076 sections (and thus doesn't have an output bfd_section), 2077 look for the closest prior output statement having an 2078 output section. */ 2079 if (bfd_section == NULL) 2080 bfd_section = output_prev_sec_find (after); 2081 2082 if (bfd_section != NULL 2083 && bfd_section->owner != NULL 2084 && bfd_section != snew) 2085 place->section = &bfd_section->next; 2086 } 2087 2088 if (place->section == NULL) 2089 place->section = &link_info.output_bfd->sections; 2090 2091 as = *place->section; 2092 2093 if (!as) 2094 { 2095 /* Put the section at the end of the list. */ 2096 2097 /* Unlink the section. */ 2098 bfd_section_list_remove (link_info.output_bfd, snew); 2099 2100 /* Now tack it back on in the right place. */ 2101 bfd_section_list_append (link_info.output_bfd, snew); 2102 } 2103 else if ((bfd_get_flavour (link_info.output_bfd) 2104 == bfd_target_elf_flavour) 2105 && (bfd_get_flavour (s->owner) 2106 == bfd_target_elf_flavour) 2107 && ((elf_section_type (s) == SHT_NOTE 2108 && (s->flags & SEC_LOAD) != 0) 2109 || (elf_section_type (as) == SHT_NOTE 2110 && (as->flags & SEC_LOAD) != 0))) 2111 { 2112 /* Make sure that output note sections are grouped and sorted 2113 by alignments when inserting a note section or insert a 2114 section after a note section, */ 2115 asection *sec; 2116 /* A specific section after which the output note section 2117 should be placed. */ 2118 asection *after_sec; 2119 /* True if we need to insert the orphan section after a 2120 specific section to maintain output note section order. */ 2121 bool after_sec_note = false; 2122 2123 static asection *first_orphan_note = NULL; 2124 2125 /* Group and sort output note section by alignments in 2126 ascending order. */ 2127 after_sec = NULL; 2128 if (elf_section_type (s) == SHT_NOTE 2129 && (s->flags & SEC_LOAD) != 0) 2130 { 2131 /* Search from the beginning for the last output note 2132 section with equal or larger alignments. NB: Don't 2133 place orphan note section after non-note sections. */ 2134 2135 first_orphan_note = NULL; 2136 2137 /* NB: When --rosegment is used, the .note.gnu.build-id 2138 section is placed before text sections. Ignore the 2139 .note.gnu.build-id section if -z separate-code and 2140 --rosegment are used together to avoid putting any 2141 note sections between the .note.gnu.build-id section 2142 and text sections in the same PT_LOAD segment. */ 2143 bool ignore_build_id = (link_info.separate_code 2144 && link_info.one_rosegment); 2145 2146 for (sec = link_info.output_bfd->sections; 2147 (sec != NULL 2148 && !bfd_is_abs_section (sec)); 2149 sec = sec->next) 2150 if (sec != snew 2151 && elf_section_type (sec) == SHT_NOTE 2152 && (sec->flags & SEC_LOAD) != 0 2153 && (!ignore_build_id 2154 || strcmp (sec->name, ".note.gnu.build-id") != 0)) 2155 { 2156 if (!first_orphan_note) 2157 first_orphan_note = sec; 2158 if (sec->alignment_power >= s->alignment_power) 2159 after_sec = sec; 2160 } 2161 else if (first_orphan_note) 2162 { 2163 /* Stop if there is non-note section after the first 2164 orphan note section. */ 2165 break; 2166 } 2167 2168 /* If this will be the first orphan note section, it can 2169 be placed at the default location. */ 2170 after_sec_note = first_orphan_note != NULL; 2171 if (after_sec == NULL && after_sec_note) 2172 { 2173 /* If all output note sections have smaller 2174 alignments, place the section before all 2175 output orphan note sections. */ 2176 after_sec = first_orphan_note; 2177 insert_after = false; 2178 } 2179 } 2180 else if (first_orphan_note) 2181 { 2182 /* Don't place non-note sections in the middle of orphan 2183 note sections. */ 2184 after_sec_note = true; 2185 after_sec = as; 2186 for (sec = as->next; 2187 (sec != NULL 2188 && !bfd_is_abs_section (sec)); 2189 sec = sec->next) 2190 if (elf_section_type (sec) == SHT_NOTE 2191 && (sec->flags & SEC_LOAD) != 0) 2192 after_sec = sec; 2193 } 2194 2195 if (after_sec_note) 2196 { 2197 if (after_sec) 2198 { 2199 /* Search forward to insert OS after AFTER_SEC output 2200 statement. */ 2201 lang_output_section_statement_type *stmt, *next; 2202 bool found = false; 2203 for (stmt = after; stmt != NULL; stmt = next) 2204 { 2205 next = stmt->next; 2206 if (insert_after) 2207 { 2208 if (stmt->bfd_section == after_sec) 2209 { 2210 place_after = true; 2211 found = true; 2212 after = stmt; 2213 break; 2214 } 2215 } 2216 else 2217 { 2218 /* If INSERT_AFTER is FALSE, place OS before 2219 AFTER_SEC output statement. */ 2220 if (next && next->bfd_section == after_sec) 2221 { 2222 place_after = true; 2223 found = true; 2224 after = stmt; 2225 break; 2226 } 2227 } 2228 } 2229 2230 /* Search backward to insert OS after AFTER_SEC output 2231 statement. */ 2232 if (!found) 2233 for (stmt = after; stmt != NULL; stmt = stmt->prev) 2234 { 2235 if (insert_after) 2236 { 2237 if (stmt->bfd_section == after_sec) 2238 { 2239 place_after = true; 2240 after = stmt; 2241 break; 2242 } 2243 } 2244 else 2245 { 2246 /* If INSERT_AFTER is FALSE, place OS before 2247 AFTER_SEC output statement. */ 2248 if (stmt->next->bfd_section == after_sec) 2249 { 2250 place_after = true; 2251 after = stmt; 2252 break; 2253 } 2254 } 2255 } 2256 } 2257 2258 if (after_sec == NULL 2259 || (insert_after && after_sec->next != snew) 2260 || (!insert_after && after_sec->prev != snew)) 2261 { 2262 /* Unlink the section. */ 2263 bfd_section_list_remove (link_info.output_bfd, snew); 2264 2265 /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL, 2266 prepend SNEW. */ 2267 if (after_sec) 2268 { 2269 if (insert_after) 2270 bfd_section_list_insert_after (link_info.output_bfd, 2271 after_sec, snew); 2272 else 2273 bfd_section_list_insert_before (link_info.output_bfd, 2274 after_sec, snew); 2275 } 2276 else 2277 bfd_section_list_prepend (link_info.output_bfd, snew); 2278 } 2279 } 2280 else if (as != snew && as->prev != snew) 2281 { 2282 /* Unlink the section. */ 2283 bfd_section_list_remove (link_info.output_bfd, snew); 2284 2285 /* Now tack it back on in the right place. */ 2286 bfd_section_list_insert_before (link_info.output_bfd, 2287 as, snew); 2288 } 2289 } 2290 else if (as != snew && as->prev != snew) 2291 { 2292 /* Unlink the section. */ 2293 bfd_section_list_remove (link_info.output_bfd, snew); 2294 2295 /* Now tack it back on in the right place. */ 2296 bfd_section_list_insert_before (link_info.output_bfd, as, snew); 2297 } 2298 2299 /* Save the end of this list. Further ophans of this type will 2300 follow the one we've just added. */ 2301 place->section = &snew->next; 2302 2303 /* The following is non-cosmetic. We try to put the output 2304 statements in some sort of reasonable order here, because they 2305 determine the final load addresses of the orphan sections. 2306 In addition, placing output statements in the wrong order may 2307 require extra segments. For instance, given a typical 2308 situation of all read-only sections placed in one segment and 2309 following that a segment containing all the read-write 2310 sections, we wouldn't want to place an orphan read/write 2311 section before or amongst the read-only ones. */ 2312 if (add.head != NULL) 2313 { 2314 lang_output_section_statement_type *newly_added_os; 2315 2316 /* Place OS after AFTER if AFTER_NOTE is TRUE. */ 2317 if (place_after) 2318 { 2319 lang_statement_union_type **where; 2320 2321 where = insert_os_after ((lang_statement_union_type *) after); 2322 *add.tail = *where; 2323 *where = add.head; 2324 2325 place->os_tail = &after->next; 2326 } 2327 else 2328 { 2329 /* Put it after the last orphan statement we added. */ 2330 *add.tail = *place->stmt; 2331 *place->stmt = add.head; 2332 } 2333 2334 /* Fix the global list pointer if we happened to tack our 2335 new list at the tail. */ 2336 if (*stat_ptr->tail == add.head) 2337 stat_ptr->tail = add.tail; 2338 2339 /* Save the end of this list. */ 2340 place->stmt = add.tail; 2341 2342 /* Do the same for the list of output section statements. */ 2343 newly_added_os = *os_tail; 2344 *os_tail = NULL; 2345 newly_added_os->prev = (lang_output_section_statement_type *) 2346 ((char *) place->os_tail 2347 - offsetof (lang_output_section_statement_type, next)); 2348 newly_added_os->next = *place->os_tail; 2349 if (newly_added_os->next != NULL) 2350 newly_added_os->next->prev = newly_added_os; 2351 *place->os_tail = newly_added_os; 2352 place->os_tail = &newly_added_os->next; 2353 2354 /* Fixing the global list pointer here is a little different. 2355 We added to the list in lang_enter_output_section_statement, 2356 trimmed off the new output_section_statment above when 2357 assigning *os_tail = NULL, but possibly added it back in 2358 the same place when assigning *place->os_tail. */ 2359 if (*os_tail == NULL) 2360 lang_os_list.tail = (lang_statement_union_type **) os_tail; 2361 } 2362 } 2363 return os; 2364 } 2365 2366 static void 2367 lang_print_asneeded (void) 2368 { 2369 struct asneeded_minfo *m; 2370 2371 if (asneeded_list_head == NULL) 2372 return; 2373 2374 minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n")); 2375 2376 for (m = asneeded_list_head; m != NULL; m = m->next) 2377 { 2378 int len; 2379 2380 minfo ("%s", m->soname); 2381 len = strlen (m->soname); 2382 2383 if (len >= 29) 2384 { 2385 print_nl (); 2386 len = 0; 2387 } 2388 print_spaces (30 - len); 2389 2390 if (m->ref != NULL) 2391 minfo ("%pB ", m->ref); 2392 minfo ("(%pT)\n", m->name); 2393 } 2394 } 2395 2396 static void 2397 lang_map_flags (flagword flag) 2398 { 2399 if (flag & SEC_ALLOC) 2400 minfo ("a"); 2401 2402 if (flag & SEC_CODE) 2403 minfo ("x"); 2404 2405 if (flag & SEC_READONLY) 2406 minfo ("r"); 2407 2408 if (flag & SEC_DATA) 2409 minfo ("w"); 2410 2411 if (flag & SEC_LOAD) 2412 minfo ("l"); 2413 } 2414 2415 void 2416 lang_map (void) 2417 { 2418 lang_memory_region_type *m; 2419 bool dis_header_printed = false; 2420 2421 ldfile_print_input_remaps (); 2422 2423 LANG_FOR_EACH_INPUT_STATEMENT (file) 2424 { 2425 asection *s; 2426 2427 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 2428 || file->flags.just_syms) 2429 continue; 2430 2431 if (config.print_map_discarded) 2432 for (s = file->the_bfd->sections; s != NULL; s = s->next) 2433 if ((s->output_section == NULL 2434 || s->output_section->owner != link_info.output_bfd) 2435 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0) 2436 { 2437 if (! dis_header_printed) 2438 { 2439 minfo (_("\nDiscarded input sections\n\n")); 2440 dis_header_printed = true; 2441 } 2442 2443 print_input_section (s, true); 2444 } 2445 } 2446 if (config.print_map_discarded && ! dis_header_printed) 2447 minfo (_("\nThere are no discarded input sections\n")); 2448 2449 minfo (_("\nMemory Configuration\n\n")); 2450 fprintf (config.map_file, "%-16s %-18s %-18s %s\n", 2451 _("Name"), _("Origin"), _("Length"), _("Attributes")); 2452 2453 for (m = lang_memory_region_list; m != NULL; m = m->next) 2454 { 2455 fprintf (config.map_file, "%-16s", m->name_list.name); 2456 2457 char buf[32]; 2458 bfd_sprintf_vma (link_info.output_bfd, buf, m->origin); 2459 fprintf (config.map_file, " 0x%-16s", buf); 2460 bfd_sprintf_vma (link_info.output_bfd, buf, m->length); 2461 fprintf (config.map_file, 2462 " 0x%*s", m->flags || m->not_flags ? -17 : 0, buf); 2463 if (m->flags) 2464 lang_map_flags (m->flags); 2465 2466 if (m->not_flags) 2467 { 2468 minfo ("!"); 2469 lang_map_flags (m->not_flags); 2470 } 2471 2472 print_nl (); 2473 } 2474 2475 minfo (_("\nLinker script and memory map\n\n")); 2476 2477 if (!link_info.reduce_memory_overheads) 2478 { 2479 obstack_begin (&map_obstack, 1000); 2480 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0); 2481 } 2482 expld.phase = lang_fixed_phase_enum; 2483 lang_statement_iteration++; 2484 print_statements (); 2485 2486 ldemul_extra_map_file_text (link_info.output_bfd, &link_info, 2487 config.map_file); 2488 } 2489 2490 static bool 2491 is_defined (struct bfd_link_hash_entry *h) 2492 { 2493 return h != NULL 2494 && (h->type == bfd_link_hash_defined 2495 || h->type == bfd_link_hash_defweak); 2496 } 2497 2498 static bool 2499 sort_def_symbol (struct bfd_link_hash_entry *hash_entry, 2500 void *info ATTRIBUTE_UNUSED) 2501 { 2502 if (is_defined (hash_entry) 2503 && hash_entry->u.def.section->owner != link_info.output_bfd 2504 && hash_entry->u.def.section->owner != NULL) 2505 { 2506 input_section_userdata_type *ud; 2507 struct map_symbol_def *def; 2508 2509 ud = bfd_section_userdata (hash_entry->u.def.section); 2510 if (!ud) 2511 { 2512 ud = stat_alloc (sizeof (*ud)); 2513 bfd_set_section_userdata (hash_entry->u.def.section, ud); 2514 ud->map_symbol_def_tail = &ud->map_symbol_def_head; 2515 ud->map_symbol_def_count = 0; 2516 } 2517 else if (!ud->map_symbol_def_tail) 2518 ud->map_symbol_def_tail = &ud->map_symbol_def_head; 2519 2520 def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def); 2521 def->entry = hash_entry; 2522 *(ud->map_symbol_def_tail) = def; 2523 ud->map_symbol_def_tail = &def->next; 2524 ud->map_symbol_def_count++; 2525 } 2526 return true; 2527 } 2528 2529 /* Initialize an output section. */ 2530 2531 static void 2532 init_os (lang_output_section_statement_type *s, flagword flags) 2533 { 2534 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) 2535 fatal (_("%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME); 2536 2537 if (!s->dup_output) 2538 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name); 2539 if (s->bfd_section == NULL) 2540 s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd, 2541 s->name, flags); 2542 if (s->bfd_section == NULL) 2543 { 2544 fatal (_("%P: output format %s cannot represent section" 2545 " called %s: %E\n"), 2546 link_info.output_bfd->xvec->name, s->name); 2547 } 2548 s->bfd_section->output_section = s->bfd_section; 2549 s->bfd_section->output_offset = 0; 2550 2551 /* Set the userdata of the output section to the output section 2552 statement to avoid lookup. */ 2553 bfd_set_section_userdata (s->bfd_section, s); 2554 2555 /* If there is a base address, make sure that any sections it might 2556 mention are initialized. */ 2557 if (s->addr_tree != NULL) 2558 exp_init_os (s->addr_tree); 2559 2560 if (s->load_base != NULL) 2561 exp_init_os (s->load_base); 2562 2563 /* If supplied an alignment, set it. */ 2564 if (s->section_alignment != NULL) 2565 s->bfd_section->alignment_power = exp_get_power (s->section_alignment, s, 2566 "section alignment"); 2567 } 2568 2569 static flagword 2570 get_os_init_flag (lang_output_section_statement_type * os) 2571 { 2572 if (os != NULL) 2573 switch (os->sectype) 2574 { 2575 case readonly_section: return SEC_READONLY; 2576 case noload_section: return SEC_NEVER_LOAD; 2577 default: break; 2578 } 2579 2580 return 0; 2581 } 2582 2583 /* Make sure that all output sections mentioned in an expression are 2584 initialized. */ 2585 2586 static void 2587 exp_init_os (etree_type *exp) 2588 { 2589 switch (exp->type.node_class) 2590 { 2591 case etree_assign: 2592 case etree_provide: 2593 case etree_provided: 2594 exp_init_os (exp->assign.src); 2595 break; 2596 2597 case etree_binary: 2598 exp_init_os (exp->binary.lhs); 2599 exp_init_os (exp->binary.rhs); 2600 break; 2601 2602 case etree_trinary: 2603 exp_init_os (exp->trinary.cond); 2604 exp_init_os (exp->trinary.lhs); 2605 exp_init_os (exp->trinary.rhs); 2606 break; 2607 2608 case etree_assert: 2609 exp_init_os (exp->assert_s.child); 2610 break; 2611 2612 case etree_unary: 2613 exp_init_os (exp->unary.child); 2614 break; 2615 2616 case etree_name: 2617 switch (exp->type.node_code) 2618 { 2619 case ADDR: 2620 case LOADADDR: 2621 { 2622 lang_output_section_statement_type *os; 2623 2624 os = lang_output_section_find (exp->name.name); 2625 if (os != NULL && os->bfd_section == NULL) 2626 init_os (os, get_os_init_flag (os)); 2627 } 2628 } 2629 break; 2630 2631 default: 2632 break; 2633 } 2634 } 2635 2636 static void 2638 section_already_linked (bfd *abfd, asection *sec, void *data) 2639 { 2640 lang_input_statement_type *entry = (lang_input_statement_type *) data; 2641 2642 /* If we are only reading symbols from this object, then we want to 2643 discard all sections. */ 2644 if (entry->flags.just_syms) 2645 { 2646 bfd_link_just_syms (abfd, sec, &link_info); 2647 return; 2648 } 2649 2650 /* Deal with SHF_EXCLUDE ELF sections. */ 2651 if (!bfd_link_relocatable (&link_info) 2652 && (abfd->flags & BFD_PLUGIN) == 0 2653 && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE) 2654 sec->output_section = bfd_abs_section_ptr; 2655 2656 if (!(abfd->flags & DYNAMIC)) 2657 bfd_section_already_linked (abfd, sec, &link_info); 2658 } 2659 2660 2662 /* Returns true if SECTION is one we know will be discarded based on its 2663 section flags, otherwise returns false. */ 2664 2665 static bool 2666 lang_discard_section_p (asection *section) 2667 { 2668 bool discard; 2669 flagword flags = section->flags; 2670 2671 /* Discard sections marked with SEC_EXCLUDE. */ 2672 discard = (flags & SEC_EXCLUDE) != 0; 2673 2674 /* Discard the group descriptor sections when we're finally placing the 2675 sections from within the group. */ 2676 if ((flags & SEC_GROUP) != 0 2677 && link_info.resolve_section_groups) 2678 discard = true; 2679 2680 /* Discard debugging sections if we are stripping debugging 2681 information. */ 2682 if ((link_info.strip == strip_debugger || link_info.strip == strip_all) 2683 && (flags & SEC_DEBUGGING) != 0) 2684 discard = true; 2685 2686 /* Discard non-alloc sections if we are stripping section headers. */ 2687 else if (config.no_section_header && (flags & SEC_ALLOC) == 0) 2688 discard = true; 2689 2690 return discard; 2691 } 2692 2693 /* Return TRUE if SECTION is never going to be added to output statement 2694 OUTPUT. lang_add_section() definitely won't do anything with SECTION 2695 if this returns TRUE. It may do something (or not) if this returns FALSE. 2696 2697 Can be used as early-out to filter matches. This may set 2698 output_section of SECTION, if it was unset, to the abs section in case 2699 we discover SECTION to be always discarded. This may also give 2700 warning messages. */ 2701 2702 static bool 2703 wont_add_section_p (asection *section, 2704 lang_output_section_statement_type *output) 2705 { 2706 bool discard; 2707 2708 /* Is this section one we know should be discarded? */ 2709 discard = lang_discard_section_p (section); 2710 2711 /* Discard input sections which are assigned to a section named 2712 DISCARD_SECTION_NAME. */ 2713 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) 2714 discard = true; 2715 2716 if (discard) 2717 { 2718 if (section->output_section == NULL) 2719 { 2720 /* This prevents future calls from assigning this section or 2721 warning about it again. */ 2722 section->output_section = bfd_abs_section_ptr; 2723 } 2724 else if (bfd_is_abs_section (section->output_section)) 2725 ; 2726 else if (link_info.non_contiguous_regions_warnings) 2727 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes " 2728 "section `%pA' from `%pB' match /DISCARD/ clause.\n"), 2729 NULL, section, section->owner); 2730 2731 return true; 2732 } 2733 2734 if (section->output_section != NULL) 2735 { 2736 if (!link_info.non_contiguous_regions) 2737 return true; 2738 2739 /* SECTION has already been handled in a special way 2740 (eg. LINK_ONCE): skip it. */ 2741 if (bfd_is_abs_section (section->output_section)) 2742 return true; 2743 2744 /* Already assigned to the same output section, do not process 2745 it again, to avoid creating loops between duplicate sections 2746 later. */ 2747 if (section->output_section == output->bfd_section) 2748 return true; 2749 2750 if (link_info.non_contiguous_regions_warnings && output->bfd_section) 2751 einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may " 2752 "change behaviour for section `%pA' from `%pB' (assigned to " 2753 "%pA, but additional match: %pA)\n"), 2754 NULL, section, section->owner, section->output_section, 2755 output->bfd_section); 2756 2757 /* SECTION has already been assigned to an output section, but 2758 the user allows it to be mapped to another one in case it 2759 overflows. We'll later update the actual output section in 2760 size_input_section as appropriate. */ 2761 } 2762 2763 return false; 2764 } 2765 2766 /* The wild routines. 2767 2768 These expand statements like *(.text) and foo.o to a list of 2769 explicit actions, like foo.o(.text), bar.o(.text) and 2770 foo.o(.text, .data). */ 2771 2772 /* Add SECTION to the output section OUTPUT. Do this by creating a 2773 lang_input_section statement which is placed at PTR. */ 2774 2775 void 2776 lang_add_section (lang_statement_list_type *ptr, 2777 asection *section, 2778 struct wildcard_list *pattern, 2779 struct flag_info *sflag_info, 2780 lang_output_section_statement_type *output) 2781 { 2782 flagword flags = section->flags; 2783 2784 lang_input_section_type *new_section; 2785 bfd *abfd = link_info.output_bfd; 2786 2787 if (wont_add_section_p (section, output)) 2788 return; 2789 2790 if (sflag_info) 2791 { 2792 bool keep; 2793 2794 keep = bfd_lookup_section_flags (&link_info, sflag_info, section); 2795 if (!keep) 2796 return; 2797 } 2798 2799 /* We don't copy the SEC_NEVER_LOAD flag from an input section 2800 to an output section, because we want to be able to include a 2801 SEC_NEVER_LOAD section in the middle of an otherwise loaded 2802 section (I don't know why we want to do this, but we do). 2803 build_link_order in ldwrite.c handles this case by turning 2804 the embedded SEC_NEVER_LOAD section into a fill. */ 2805 flags &= ~ SEC_NEVER_LOAD; 2806 2807 /* If final link, don't copy the SEC_LINK_ONCE flags, they've 2808 already been processed. One reason to do this is that on pe 2809 format targets, .text$foo sections go into .text and it's odd 2810 to see .text with SEC_LINK_ONCE set. */ 2811 if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP)) 2812 { 2813 if (link_info.resolve_section_groups) 2814 flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); 2815 else 2816 flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC); 2817 } 2818 else if (!bfd_link_relocatable (&link_info)) 2819 flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC); 2820 2821 switch (output->sectype) 2822 { 2823 case normal_section: 2824 case overlay_section: 2825 case first_overlay_section: 2826 case type_section: 2827 break; 2828 case noalloc_section: 2829 flags &= ~SEC_ALLOC; 2830 break; 2831 case typed_readonly_section: 2832 case readonly_section: 2833 flags |= SEC_READONLY; 2834 break; 2835 case noload_section: 2836 flags &= ~SEC_LOAD; 2837 flags |= SEC_NEVER_LOAD; 2838 /* Unfortunately GNU ld has managed to evolve two different 2839 meanings to NOLOAD in scripts. ELF gets a .bss style noload, 2840 alloc, no contents section. All others get a noload, noalloc 2841 section. Unlike a .bss style section, if a note section is 2842 marked as NOLOAD, also clear SEC_ALLOC. */ 2843 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour 2844 && elf_section_type (section) != SHT_NOTE) 2845 flags &= ~SEC_HAS_CONTENTS; 2846 else 2847 flags &= ~SEC_ALLOC; 2848 break; 2849 } 2850 2851 if (output->bfd_section == NULL) 2852 init_os (output, flags); 2853 2854 /* If SEC_READONLY is not set in the input section, then clear 2855 it from the output section. */ 2856 output->bfd_section->flags &= flags | ~SEC_READONLY; 2857 2858 if (output->bfd_section->linker_has_input) 2859 { 2860 /* Only set SEC_READONLY flag on the first input section. */ 2861 flags &= ~ SEC_READONLY; 2862 2863 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ 2864 if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS)) 2865 != (flags & (SEC_MERGE | SEC_STRINGS)) 2866 || ((flags & SEC_MERGE) != 0 2867 && output->bfd_section->entsize != section->entsize)) 2868 { 2869 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); 2870 flags &= ~ (SEC_MERGE | SEC_STRINGS); 2871 } 2872 } 2873 output->bfd_section->flags |= flags; 2874 2875 if (!output->bfd_section->linker_has_input) 2876 { 2877 output->bfd_section->linker_has_input = 1; 2878 /* This must happen after flags have been updated. The output 2879 section may have been created before we saw its first input 2880 section, eg. for a data statement. */ 2881 bfd_copy_private_section_data (section->owner, section, 2882 link_info.output_bfd, 2883 output->bfd_section, 2884 &link_info); 2885 if ((flags & SEC_MERGE) != 0) 2886 output->bfd_section->entsize = section->entsize; 2887 } 2888 2889 if ((flags & SEC_TIC54X_BLOCK) != 0 2890 && bfd_get_arch (section->owner) == bfd_arch_tic54x) 2891 { 2892 /* FIXME: This value should really be obtained from the bfd... */ 2893 output->block_value = 128; 2894 } 2895 2896 /* When a .ctors section is placed in .init_array it must be copied 2897 in reverse order. Similarly for .dtors. Set that up. */ 2898 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour 2899 && ((startswith (section->name, ".ctors") 2900 && strcmp (output->bfd_section->name, ".init_array") == 0) 2901 || (startswith (section->name, ".dtors") 2902 && strcmp (output->bfd_section->name, ".fini_array") == 0)) 2903 && (section->name[6] == 0 || section->name[6] == '.')) 2904 section->flags |= SEC_ELF_REVERSE_COPY; 2905 2906 if (section->alignment_power > output->bfd_section->alignment_power) 2907 output->bfd_section->alignment_power = section->alignment_power; 2908 2909 section->output_section = output->bfd_section; 2910 2911 if (!map_head_is_link_order) 2912 { 2913 asection *s = output->bfd_section->map_tail.s; 2914 output->bfd_section->map_tail.s = section; 2915 section->map_head.s = NULL; 2916 section->map_tail.s = s; 2917 if (s != NULL) 2918 s->map_head.s = section; 2919 else 2920 output->bfd_section->map_head.s = section; 2921 } 2922 2923 /* Add a section reference to the list. */ 2924 new_section = new_stat (lang_input_section, ptr); 2925 new_section->section = section; 2926 new_section->pattern = pattern; 2927 } 2928 2929 /* Expand a wild statement for a particular FILE. SECTION may be 2930 NULL, in which case it is a wild card. This assumes that the 2931 wild statement doesn't need any sorting (of filenames or sections). */ 2932 2933 static void 2934 output_section_callback_nosort (lang_wild_statement_type *ptr, 2935 struct wildcard_list *sec ATTRIBUTE_UNUSED, 2936 asection *section, 2937 lang_input_statement_type *file ATTRIBUTE_UNUSED, 2938 void *output) 2939 { 2940 lang_output_section_statement_type *os; 2941 2942 os = (lang_output_section_statement_type *) output; 2943 2944 /* Exclude sections that match UNIQUE_SECTION_LIST. */ 2945 if (unique_section_p (section, os)) 2946 return; 2947 2948 lang_add_section (&ptr->children, section, ptr->section_list, 2949 ptr->section_flag_list, os); 2950 } 2951 2952 /* Check if all sections in a wild statement for a particular FILE 2953 are readonly. */ 2954 2955 static void 2956 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, 2957 struct wildcard_list *sec ATTRIBUTE_UNUSED, 2958 asection *section, 2959 lang_input_statement_type *file ATTRIBUTE_UNUSED, 2960 void *output) 2961 { 2962 lang_output_section_statement_type *os; 2963 2964 os = (lang_output_section_statement_type *) output; 2965 2966 /* Exclude sections that match UNIQUE_SECTION_LIST. */ 2967 if (unique_section_p (section, os)) 2968 return; 2969 2970 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0) 2971 os->all_input_readonly = false; 2972 } 2973 2974 /* This is passed a file name which must have been seen already and 2975 added to the statement tree. We will see if it has been opened 2976 already and had its symbols read. If not then we'll read it. */ 2977 2978 static lang_input_statement_type * 2979 lookup_name (const char *name) 2980 { 2981 lang_input_statement_type *search; 2982 2983 for (search = (void *) input_file_chain.head; 2984 search != NULL; 2985 search = search->next_real_file) 2986 { 2987 /* Use the local_sym_name as the name of the file that has 2988 already been loaded as filename might have been transformed 2989 via the search directory lookup mechanism. */ 2990 const char *filename = search->local_sym_name; 2991 2992 if (filename != NULL 2993 && filename_cmp (filename, name) == 0) 2994 break; 2995 } 2996 2997 if (search == NULL) 2998 { 2999 /* Arrange to splice the input statement added by new_afile into 3000 statement_list after the current input_file_chain tail. 3001 We know input_file_chain is not an empty list, and that 3002 lookup_name was called via open_input_bfds. Later calls to 3003 lookup_name should always match an existing input_statement. */ 3004 lang_statement_union_type **tail = stat_ptr->tail; 3005 lang_statement_union_type **after 3006 = (void *) ((char *) input_file_chain.tail 3007 - offsetof (lang_input_statement_type, next_real_file) 3008 + offsetof (lang_input_statement_type, header.next)); 3009 lang_statement_union_type *rest = *after; 3010 stat_ptr->tail = after; 3011 search = new_afile (name, lang_input_file_is_search_file_enum, 3012 default_target, NULL); 3013 *stat_ptr->tail = rest; 3014 if (*tail == NULL) 3015 stat_ptr->tail = tail; 3016 } 3017 3018 /* If we have already added this file, or this file is not real 3019 don't add this file. */ 3020 if (search->flags.loaded || !search->flags.real) 3021 return search; 3022 3023 if (!load_symbols (search, NULL)) 3024 return NULL; 3025 3026 return search; 3027 } 3028 3029 /* Save LIST as a list of libraries whose symbols should not be exported. */ 3030 3031 struct excluded_lib 3032 { 3033 char *name; 3034 struct excluded_lib *next; 3035 }; 3036 static struct excluded_lib *excluded_libs; 3037 3038 void 3039 add_excluded_libs (const char *list) 3040 { 3041 const char *p = list, *end; 3042 3043 while (*p != '\0') 3044 { 3045 struct excluded_lib *entry; 3046 end = strpbrk (p, ",:"); 3047 if (end == NULL) 3048 end = p + strlen (p); 3049 entry = stat_alloc (sizeof (*entry)); 3050 entry->next = excluded_libs; 3051 entry->name = stat_memdup (p, end - p, end - p + 1); 3052 excluded_libs = entry; 3053 if (*end == '\0') 3054 break; 3055 p = end + 1; 3056 } 3057 } 3058 3059 static void 3060 check_excluded_libs (bfd *abfd) 3061 { 3062 struct excluded_lib *lib = excluded_libs; 3063 3064 while (lib) 3065 { 3066 int len = strlen (lib->name); 3067 const char *filename = lbasename (bfd_get_filename (abfd)); 3068 3069 if (strcmp (lib->name, "ALL") == 0) 3070 { 3071 abfd->no_export = true; 3072 return; 3073 } 3074 3075 if (filename_ncmp (lib->name, filename, len) == 0 3076 && (filename[len] == '\0' 3077 || (filename[len] == '.' && filename[len + 1] == 'a' 3078 && filename[len + 2] == '\0'))) 3079 { 3080 abfd->no_export = true; 3081 return; 3082 } 3083 3084 lib = lib->next; 3085 } 3086 } 3087 3088 /* Get the symbols for an input file. */ 3089 3090 bool 3091 load_symbols (lang_input_statement_type *entry, 3092 lang_statement_list_type *place) 3093 { 3094 char **matching; 3095 3096 if (entry->flags.loaded) 3097 return true; 3098 3099 ldfile_open_file (entry); 3100 3101 /* Do not process further if the file was missing. */ 3102 if (entry->flags.missing_file) 3103 return true; 3104 3105 if (trace_files || verbose) 3106 info_msg ("%pI\n", entry); 3107 3108 if (!bfd_check_format (entry->the_bfd, bfd_archive) 3109 && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) 3110 { 3111 bfd_error_type err; 3112 struct lang_input_statement_flags save_flags; 3113 extern FILE *yyin; 3114 3115 err = bfd_get_error (); 3116 3117 /* See if the emulation has some special knowledge. */ 3118 if (ldemul_unrecognized_file (entry)) 3119 { 3120 if (err == bfd_error_file_ambiguously_recognized) 3121 free (matching); 3122 return true; 3123 } 3124 3125 if (err == bfd_error_file_ambiguously_recognized) 3126 { 3127 char **p; 3128 3129 einfo (_("%P: %pB: file not recognized: %E;" 3130 " matching formats:"), entry->the_bfd); 3131 for (p = matching; *p != NULL; p++) 3132 einfo (" %s", *p); 3133 free (matching); 3134 fatal ("\n"); 3135 } 3136 else if (err != bfd_error_file_not_recognized 3137 || place == NULL) 3138 fatal (_("%P: %pB: file not recognized: %E\n"), entry->the_bfd); 3139 3140 bfd_close (entry->the_bfd); 3141 entry->the_bfd = NULL; 3142 3143 /* Try to interpret the file as a linker script. */ 3144 save_flags = input_flags; 3145 ldfile_open_command_file (entry->filename); 3146 3147 push_stat_ptr (place); 3148 input_flags.add_DT_NEEDED_for_regular 3149 = entry->flags.add_DT_NEEDED_for_regular; 3150 input_flags.add_DT_NEEDED_for_dynamic 3151 = entry->flags.add_DT_NEEDED_for_dynamic; 3152 input_flags.whole_archive = entry->flags.whole_archive; 3153 input_flags.dynamic = entry->flags.dynamic; 3154 3155 ldfile_assumed_script = true; 3156 parser_input = input_script; 3157 current_input_file = entry->filename; 3158 yyparse (); 3159 current_input_file = NULL; 3160 ldfile_assumed_script = false; 3161 3162 /* missing_file is sticky. sysrooted will already have been 3163 restored when seeing EOF in yyparse, but no harm to restore 3164 again. */ 3165 save_flags.missing_file |= input_flags.missing_file; 3166 input_flags = save_flags; 3167 pop_stat_ptr (); 3168 fclose (yyin); 3169 yyin = NULL; 3170 entry->flags.loaded = true; 3171 3172 return true; 3173 } 3174 3175 if (ldemul_recognized_file (entry)) 3176 return true; 3177 3178 /* We don't call ldlang_add_file for an archive. Instead, the 3179 add_symbols entry point will call ldlang_add_file, via the 3180 add_archive_element callback, for each element of the archive 3181 which is used. */ 3182 switch (bfd_get_format (entry->the_bfd)) 3183 { 3184 default: 3185 break; 3186 3187 case bfd_object: 3188 if (!entry->flags.reload) 3189 ldlang_add_file (entry); 3190 break; 3191 3192 case bfd_archive: 3193 check_excluded_libs (entry->the_bfd); 3194 3195 bfd_set_usrdata (entry->the_bfd, entry); 3196 if (entry->flags.whole_archive) 3197 { 3198 bfd *member = NULL; 3199 bool loaded = true; 3200 3201 for (;;) 3202 { 3203 bfd *subsbfd; 3204 member = bfd_openr_next_archived_file (entry->the_bfd, member); 3205 3206 if (member == NULL) 3207 break; 3208 3209 if (!bfd_check_format (member, bfd_object)) 3210 { 3211 fatal (_("%P: %pB: member %pB in archive is not an object\n"), 3212 entry->the_bfd, member); 3213 loaded = false; 3214 } 3215 3216 if (config.emitting_gnu_object_only) 3217 { 3218 if (!cmdline_on_object_only_archive_list_p (member)) 3219 continue; 3220 } 3221 3222 subsbfd = member; 3223 if (!(*link_info.callbacks 3224 ->add_archive_element) (&link_info, member, 3225 "--whole-archive", &subsbfd)) 3226 abort (); 3227 3228 /* Potentially, the add_archive_element hook may have set a 3229 substitute BFD for us. */ 3230 if (!bfd_link_add_symbols (subsbfd, &link_info)) 3231 { 3232 fatal (_("%P: %pB: error adding symbols: %E\n"), member); 3233 loaded = false; 3234 } 3235 } 3236 3237 entry->flags.loaded = loaded; 3238 return loaded; 3239 } 3240 break; 3241 } 3242 3243 if (bfd_link_add_symbols (entry->the_bfd, &link_info)) 3244 entry->flags.loaded = true; 3245 else 3246 fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); 3247 3248 return entry->flags.loaded; 3249 } 3250 3251 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both 3252 may be NULL, indicating that it is a wildcard. Separate 3253 lang_input_section statements are created for each part of the 3254 expansion; they are added after the wild statement S. OUTPUT is 3255 the output section. */ 3256 3257 static void 3258 wild (lang_wild_statement_type *s, 3259 const char *target ATTRIBUTE_UNUSED, 3260 lang_output_section_statement_type *output) 3261 { 3262 struct wildcard_list *sec; 3263 3264 if (s->filenames_sorted || s->any_specs_sorted) 3265 { 3266 lang_section_bst_type *tree; 3267 3268 walk_wild (s, output_section_callback_sort, output); 3269 3270 tree = s->tree; 3271 if (tree) 3272 { 3273 output_section_callback_tree_to_list (s, tree, output); 3274 s->tree = NULL; 3275 s->rightmost = &s->tree; 3276 } 3277 } 3278 else 3279 walk_wild (s, output_section_callback_nosort, output); 3280 3281 if (default_common_section == NULL) 3282 for (sec = s->section_list; sec != NULL; sec = sec->next) 3283 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) 3284 { 3285 /* Remember the section that common is going to in case we 3286 later get something which doesn't know where to put it. */ 3287 default_common_section = output; 3288 break; 3289 } 3290 } 3291 3292 /* Return TRUE iff target is the sought target. */ 3293 3294 static int 3295 get_target (const bfd_target *target, void *data) 3296 { 3297 const char *sought = (const char *) data; 3298 3299 return strcmp (target->name, sought) == 0; 3300 } 3301 3302 /* Like strcpy() but convert to lower case as well. */ 3303 3304 static void 3305 stricpy (char *dest, const char *src) 3306 { 3307 char c; 3308 3309 while ((c = *src++) != 0) 3310 *dest++ = TOLOWER (c); 3311 3312 *dest = 0; 3313 } 3314 3315 /* Remove the first occurrence of needle (if any) in haystack 3316 from haystack. */ 3317 3318 static void 3319 strcut (char *haystack, const char *needle) 3320 { 3321 haystack = strstr (haystack, needle); 3322 3323 if (haystack) 3324 { 3325 char *src; 3326 3327 for (src = haystack + strlen (needle); *src;) 3328 *haystack++ = *src++; 3329 3330 *haystack = 0; 3331 } 3332 } 3333 3334 /* Compare two target format name strings. 3335 Return a value indicating how "similar" they are. */ 3336 3337 static int 3338 name_compare (const char *first, const char *second) 3339 { 3340 char *copy1; 3341 char *copy2; 3342 int result; 3343 3344 copy1 = (char *) xmalloc (strlen (first) + 1); 3345 copy2 = (char *) xmalloc (strlen (second) + 1); 3346 3347 /* Convert the names to lower case. */ 3348 stricpy (copy1, first); 3349 stricpy (copy2, second); 3350 3351 /* Remove size and endian strings from the name. */ 3352 strcut (copy1, "big"); 3353 strcut (copy1, "little"); 3354 strcut (copy2, "big"); 3355 strcut (copy2, "little"); 3356 3357 /* Return a value based on how many characters match, 3358 starting from the beginning. If both strings are 3359 the same then return 10 * their length. */ 3360 for (result = 0; copy1[result] == copy2[result]; result++) 3361 if (copy1[result] == 0) 3362 { 3363 result *= 10; 3364 break; 3365 } 3366 3367 free (copy1); 3368 free (copy2); 3369 3370 return result; 3371 } 3372 3373 /* Set by closest_target_match() below. */ 3374 static const bfd_target *winner; 3375 3376 /* Scan all the valid bfd targets looking for one that has the endianness 3377 requirement that was specified on the command line, and is the nearest 3378 match to the original output target. */ 3379 3380 static int 3381 closest_target_match (const bfd_target *target, void *data) 3382 { 3383 const bfd_target *original = (const bfd_target *) data; 3384 3385 if (command_line.endian == ENDIAN_BIG 3386 && target->byteorder != BFD_ENDIAN_BIG) 3387 return 0; 3388 3389 if (command_line.endian == ENDIAN_LITTLE 3390 && target->byteorder != BFD_ENDIAN_LITTLE) 3391 return 0; 3392 3393 /* Must be the same flavour. */ 3394 if (target->flavour != original->flavour) 3395 return 0; 3396 3397 /* Ignore generic big and little endian elf vectors. */ 3398 if (strcmp (target->name, "elf32-big") == 0 3399 || strcmp (target->name, "elf64-big") == 0 3400 || strcmp (target->name, "elf32-little") == 0 3401 || strcmp (target->name, "elf64-little") == 0) 3402 return 0; 3403 3404 /* If we have not found a potential winner yet, then record this one. */ 3405 if (winner == NULL) 3406 { 3407 winner = target; 3408 return 0; 3409 } 3410 3411 /* Oh dear, we now have two potential candidates for a successful match. 3412 Compare their names and choose the better one. */ 3413 if (name_compare (target->name, original->name) 3414 > name_compare (winner->name, original->name)) 3415 winner = target; 3416 3417 /* Keep on searching until wqe have checked them all. */ 3418 return 0; 3419 } 3420 3421 /* Return the BFD target format of the first input file. */ 3422 3423 static const char * 3424 get_first_input_target (void) 3425 { 3426 const char *target = NULL; 3427 3428 LANG_FOR_EACH_INPUT_STATEMENT (s) 3429 { 3430 if (s->header.type == lang_input_statement_enum 3431 && s->flags.real) 3432 { 3433 ldfile_open_file (s); 3434 3435 if (s->the_bfd != NULL 3436 && bfd_check_format (s->the_bfd, bfd_object)) 3437 { 3438 target = bfd_get_target (s->the_bfd); 3439 3440 if (target != NULL) 3441 break; 3442 } 3443 } 3444 } 3445 3446 return target; 3447 } 3448 3449 const char * 3450 lang_get_output_target (void) 3451 { 3452 const char *target; 3453 3454 /* Has the user told us which output format to use? */ 3455 if (output_target != NULL) 3456 return output_target; 3457 3458 /* No - has the current target been set to something other than 3459 the default? */ 3460 if (current_target != default_target && current_target != NULL) 3461 return current_target; 3462 3463 /* No - can we determine the format of the first input file? */ 3464 target = get_first_input_target (); 3465 if (target != NULL) 3466 return target; 3467 3468 /* Failed - use the default output target. */ 3469 return default_target; 3470 } 3471 3472 /* Open the output file. */ 3473 3474 static void 3475 open_output (const char *name) 3476 { 3477 lang_input_statement_type *f; 3478 char *out = lrealpath (name); 3479 3480 for (f = (void *) input_file_chain.head; 3481 f != NULL; 3482 f = f->next_real_file) 3483 if (f->flags.real) 3484 { 3485 char *in = lrealpath (f->local_sym_name); 3486 if (filename_cmp (in, out) == 0) 3487 fatal (_("%P: input file '%s' is the same as output file\n"), 3488 f->filename); 3489 free (in); 3490 } 3491 free (out); 3492 3493 output_target = lang_get_output_target (); 3494 3495 /* Has the user requested a particular endianness on the command 3496 line? */ 3497 if (command_line.endian != ENDIAN_UNSET) 3498 { 3499 /* Get the chosen target. */ 3500 const bfd_target *target 3501 = bfd_iterate_over_targets (get_target, (void *) output_target); 3502 3503 /* If the target is not supported, we cannot do anything. */ 3504 if (target != NULL) 3505 { 3506 enum bfd_endian desired_endian; 3507 3508 if (command_line.endian == ENDIAN_BIG) 3509 desired_endian = BFD_ENDIAN_BIG; 3510 else 3511 desired_endian = BFD_ENDIAN_LITTLE; 3512 3513 /* See if the target has the wrong endianness. This should 3514 not happen if the linker script has provided big and 3515 little endian alternatives, but some scrips don't do 3516 this. */ 3517 if (target->byteorder != desired_endian) 3518 { 3519 /* If it does, then see if the target provides 3520 an alternative with the correct endianness. */ 3521 if (target->alternative_target != NULL 3522 && (target->alternative_target->byteorder == desired_endian)) 3523 output_target = target->alternative_target->name; 3524 else 3525 { 3526 /* Try to find a target as similar as possible to 3527 the default target, but which has the desired 3528 endian characteristic. */ 3529 bfd_iterate_over_targets (closest_target_match, 3530 (void *) target); 3531 3532 /* Oh dear - we could not find any targets that 3533 satisfy our requirements. */ 3534 if (winner == NULL) 3535 einfo (_("%P: warning: could not find any targets" 3536 " that match endianness requirement\n")); 3537 else 3538 output_target = winner->name; 3539 } 3540 } 3541 } 3542 } 3543 3544 link_info.output_bfd = bfd_openw (name, output_target); 3545 3546 if (link_info.output_bfd == NULL) 3547 { 3548 if (bfd_get_error () == bfd_error_invalid_target) 3549 fatal (_("%P: target %s not found\n"), output_target); 3550 3551 fatal (_("%P: cannot open output file %s: %E\n"), name); 3552 } 3553 3554 delete_output_file_on_failure = true; 3555 3556 if (!bfd_set_format (link_info.output_bfd, bfd_object)) 3557 fatal (_("%P: %s: can not make object file: %E\n"), name); 3558 if (!bfd_set_arch_mach (link_info.output_bfd, 3559 ldfile_output_architecture, 3560 ldfile_output_machine)) 3561 fatal (_("%P: %s: can not set architecture: %E\n"), name); 3562 3563 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd); 3564 if (link_info.hash == NULL) 3565 fatal (_("%P: can not create hash table: %E\n")); 3566 3567 bfd_set_gp_size (link_info.output_bfd, g_switch_value); 3568 } 3569 3570 static void 3571 ldlang_open_output (lang_statement_union_type *statement) 3572 { 3573 switch (statement->header.type) 3574 { 3575 case lang_output_statement_enum: 3576 ASSERT (link_info.output_bfd == NULL); 3577 open_output (statement->output_statement.name); 3578 ldemul_set_output_arch (); 3579 if (config.magic_demand_paged 3580 && !bfd_link_relocatable (&link_info)) 3581 link_info.output_bfd->flags |= D_PAGED; 3582 else 3583 link_info.output_bfd->flags &= ~D_PAGED; 3584 if (config.text_read_only) 3585 link_info.output_bfd->flags |= WP_TEXT; 3586 else 3587 link_info.output_bfd->flags &= ~WP_TEXT; 3588 if (link_info.traditional_format) 3589 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT; 3590 else 3591 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; 3592 if (config.no_section_header) 3593 link_info.output_bfd->flags |= BFD_NO_SECTION_HEADER; 3594 else 3595 link_info.output_bfd->flags &= ~BFD_NO_SECTION_HEADER; 3596 break; 3597 3598 case lang_target_statement_enum: 3599 current_target = statement->target_statement.target; 3600 break; 3601 default: 3602 break; 3603 } 3604 } 3605 3606 static void 3607 init_opb (asection *s) 3608 { 3609 unsigned int x; 3610 3611 opb_shift = 0; 3612 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour 3613 && s != NULL 3614 && (s->flags & SEC_ELF_OCTETS) != 0) 3615 return; 3616 3617 x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, 3618 ldfile_output_machine); 3619 if (x > 1) 3620 while ((x & 1) == 0) 3621 { 3622 x >>= 1; 3623 ++opb_shift; 3624 } 3625 ASSERT (x == 1); 3626 } 3627 3628 /* Open all the input files. */ 3629 3630 enum open_bfd_mode 3631 { 3632 OPEN_BFD_NORMAL = 0, 3633 OPEN_BFD_FORCE = 1, 3634 OPEN_BFD_RESCAN = 2 3635 }; 3636 #if BFD_SUPPORTS_PLUGINS 3637 static lang_input_statement_type *plugin_insert = NULL; 3638 static struct bfd_link_hash_entry *plugin_undefs = NULL; 3639 #endif 3640 3641 static void 3642 open_input_bfds (lang_statement_union_type *s, 3643 lang_output_section_statement_type *os, 3644 enum open_bfd_mode mode) 3645 { 3646 for (; s != NULL; s = s->header.next) 3647 { 3648 switch (s->header.type) 3649 { 3650 case lang_constructors_statement_enum: 3651 open_input_bfds (constructor_list.head, os, mode); 3652 break; 3653 case lang_output_section_statement_enum: 3654 os = &s->output_section_statement; 3655 open_input_bfds (os->children.head, os, mode); 3656 break; 3657 case lang_wild_statement_enum: 3658 /* Maybe we should load the file's symbols. */ 3659 if ((mode & OPEN_BFD_RESCAN) == 0 3660 && s->wild_statement.filename 3661 && !wildcardp (s->wild_statement.filename) 3662 && !archive_path (s->wild_statement.filename)) 3663 lookup_name (s->wild_statement.filename); 3664 open_input_bfds (s->wild_statement.children.head, os, mode); 3665 break; 3666 case lang_group_statement_enum: 3667 { 3668 struct bfd_link_hash_entry *undefs; 3669 #if BFD_SUPPORTS_PLUGINS 3670 lang_input_statement_type *plugin_insert_save; 3671 #endif 3672 3673 /* We must continually search the entries in the group 3674 until no new symbols are added to the list of undefined 3675 symbols. */ 3676 3677 do 3678 { 3679 #if BFD_SUPPORTS_PLUGINS 3680 plugin_insert_save = plugin_insert; 3681 #endif 3682 undefs = link_info.hash->undefs_tail; 3683 open_input_bfds (s->group_statement.children.head, os, 3684 mode | OPEN_BFD_FORCE); 3685 } 3686 while (undefs != link_info.hash->undefs_tail 3687 #if BFD_SUPPORTS_PLUGINS 3688 /* Objects inserted by a plugin, which are loaded 3689 before we hit this loop, may have added new 3690 undefs. */ 3691 || (plugin_insert != plugin_insert_save && plugin_undefs) 3692 #endif 3693 ); 3694 } 3695 break; 3696 case lang_target_statement_enum: 3697 current_target = s->target_statement.target; 3698 break; 3699 case lang_input_statement_enum: 3700 if (s->input_statement.flags.real) 3701 { 3702 lang_statement_union_type **os_tail; 3703 lang_statement_list_type add; 3704 bfd *abfd; 3705 3706 s->input_statement.target = current_target; 3707 3708 /* If we are being called from within a group, and this 3709 is an archive which has already been searched, then 3710 force it to be researched unless the whole archive 3711 has been loaded already. Do the same for a rescan. 3712 Likewise reload --as-needed shared libs. */ 3713 if (mode != OPEN_BFD_NORMAL 3714 #if BFD_SUPPORTS_PLUGINS 3715 && ((mode & OPEN_BFD_RESCAN) == 0 3716 || plugin_insert == NULL) 3717 #endif 3718 && s->input_statement.flags.loaded 3719 && (abfd = s->input_statement.the_bfd) != NULL 3720 && ((bfd_get_format (abfd) == bfd_archive 3721 && !s->input_statement.flags.whole_archive) 3722 || (bfd_get_format (abfd) == bfd_object 3723 && ((abfd->flags) & DYNAMIC) != 0 3724 && s->input_statement.flags.add_DT_NEEDED_for_regular 3725 && bfd_get_flavour (abfd) == bfd_target_elf_flavour 3726 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0))) 3727 { 3728 s->input_statement.flags.loaded = false; 3729 s->input_statement.flags.reload = true; 3730 } 3731 3732 os_tail = lang_os_list.tail; 3733 lang_list_init (&add); 3734 3735 if (!load_symbols (&s->input_statement, &add)) 3736 config.make_executable = false; 3737 3738 if (add.head != NULL) 3739 { 3740 /* If this was a script with output sections then 3741 tack any added statements on to the end of the 3742 list. This avoids having to reorder the output 3743 section statement list. Very likely the user 3744 forgot -T, and whatever we do here will not meet 3745 naive user expectations. */ 3746 if (os_tail != lang_os_list.tail) 3747 { 3748 einfo (_("%P: warning: %s contains output sections;" 3749 " did you forget -T?\n"), 3750 s->input_statement.filename); 3751 *stat_ptr->tail = add.head; 3752 stat_ptr->tail = add.tail; 3753 } 3754 else 3755 { 3756 *add.tail = s->header.next; 3757 s->header.next = add.head; 3758 } 3759 } 3760 } 3761 #if BFD_SUPPORTS_PLUGINS 3762 /* If we have found the point at which a plugin added new 3763 files, clear plugin_insert to enable archive rescan. */ 3764 if (&s->input_statement == plugin_insert) 3765 plugin_insert = NULL; 3766 #endif 3767 break; 3768 case lang_assignment_statement_enum: 3769 if (s->assignment_statement.exp->type.node_class != etree_assert) 3770 exp_fold_tree_no_dot (s->assignment_statement.exp, os); 3771 break; 3772 default: 3773 break; 3774 } 3775 } 3776 3777 /* Exit if any of the files were missing. */ 3778 if (input_flags.missing_file) 3779 fatal (""); 3780 } 3781 3782 #ifdef ENABLE_LIBCTF 3783 /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings 3784 that happened specifically at CTF open time. */ 3785 static void 3786 lang_ctf_errs_warnings (ctf_dict_t *fp) 3787 { 3788 ctf_next_t *i = NULL; 3789 char *text; 3790 int is_warning; 3791 int err; 3792 3793 while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL) 3794 { 3795 einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"), 3796 text); 3797 free (text); 3798 } 3799 if (err != ECTF_NEXT_END) 3800 { 3801 einfo (_("CTF error: cannot get CTF errors: `%s'\n"), 3802 ctf_errmsg (err)); 3803 } 3804 3805 /* `err' returns errors from the error/warning iterator in particular. 3806 These never assert. But if we have an fp, that could have recorded 3807 an assertion failure: assert if it has done so. */ 3808 ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL); 3809 } 3810 3811 /* Open the CTF sections in the input files with libctf: if any were opened, 3812 create a fake input file that we'll write the merged CTF data to later 3813 on. */ 3814 3815 static void 3816 ldlang_open_ctf (void) 3817 { 3818 int any_ctf = 0; 3819 int err; 3820 3821 ld_start_phase (PHASE_CTF); 3822 3823 LANG_FOR_EACH_INPUT_STATEMENT (file) 3824 { 3825 asection *sect; 3826 3827 /* Incoming files from the compiler have a single ctf_dict_t in them 3828 (which is presented to us by the libctf API in a ctf_archive_t 3829 wrapper): files derived from a previous relocatable link have a CTF 3830 archive containing possibly many CTF files. */ 3831 3832 if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL) 3833 { 3834 if (err != ECTF_NOCTFDATA) 3835 { 3836 lang_ctf_errs_warnings (NULL); 3837 einfo (_("%P: warning: CTF section in %pB not loaded; " 3838 "its types will be discarded: %s\n"), file->the_bfd, 3839 ctf_errmsg (err)); 3840 } 3841 continue; 3842 } 3843 3844 /* Prevent the contents of this section from being written, while 3845 requiring the section itself to be duplicated in the output, but only 3846 once. */ 3847 /* This section must exist if ctf_bfdopen() succeeded. */ 3848 sect = bfd_get_section_by_name (file->the_bfd, ".ctf"); 3849 sect->size = 0; 3850 sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED; 3851 3852 if (any_ctf) 3853 sect->flags |= SEC_EXCLUDE; 3854 any_ctf = 1; 3855 } 3856 3857 if (!any_ctf) 3858 { 3859 ctf_output = NULL; 3860 ld_stop_phase (PHASE_CTF); 3861 return; 3862 } 3863 3864 if ((ctf_output = ctf_create (&err)) != NULL) 3865 { 3866 ld_stop_phase (PHASE_CTF); 3867 return; 3868 } 3869 3870 einfo (_("%P: warning: CTF output not created: `%s'\n"), 3871 ctf_errmsg (err)); 3872 3873 LANG_FOR_EACH_INPUT_STATEMENT (errfile) 3874 ctf_close (errfile->the_ctf); 3875 3876 ld_stop_phase (PHASE_CTF); 3877 } 3878 3879 /* Merge together CTF sections. After this, only the symtab-dependent 3880 function and data object sections need adjustment. */ 3881 3882 static void 3883 lang_merge_ctf (void) 3884 { 3885 asection *output_sect; 3886 int flags = 0; 3887 3888 if (!ctf_output) 3889 return; 3890 3891 ld_start_phase (PHASE_CTF); 3892 3893 output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); 3894 3895 /* If the section was discarded, don't waste time merging. */ 3896 if (output_sect == NULL) 3897 { 3898 ctf_dict_close (ctf_output); 3899 ctf_output = NULL; 3900 3901 LANG_FOR_EACH_INPUT_STATEMENT (file) 3902 { 3903 ctf_close (file->the_ctf); 3904 file->the_ctf = NULL; 3905 } 3906 3907 ld_stop_phase (PHASE_CTF); 3908 return; 3909 } 3910 3911 LANG_FOR_EACH_INPUT_STATEMENT (file) 3912 { 3913 if (!file->the_ctf) 3914 continue; 3915 3916 /* Takes ownership of file->the_ctf. */ 3917 if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0) 3918 { 3919 einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"), 3920 file->the_bfd, ctf_errmsg (ctf_errno (ctf_output))); 3921 ctf_close (file->the_ctf); 3922 file->the_ctf = NULL; 3923 continue; 3924 } 3925 } 3926 3927 if (!config.ctf_share_duplicated) 3928 flags = CTF_LINK_SHARE_UNCONFLICTED; 3929 else 3930 flags = CTF_LINK_SHARE_DUPLICATED; 3931 if (!config.ctf_variables) 3932 flags |= CTF_LINK_OMIT_VARIABLES_SECTION; 3933 if (bfd_link_relocatable (&link_info)) 3934 flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS; 3935 3936 if (ctf_link (ctf_output, flags) < 0) 3937 { 3938 lang_ctf_errs_warnings (ctf_output); 3939 einfo (_("%P: warning: CTF linking failed; " 3940 "output will have no CTF section: %s\n"), 3941 ctf_errmsg (ctf_errno (ctf_output))); 3942 if (output_sect) 3943 { 3944 output_sect->size = 0; 3945 output_sect->flags |= SEC_EXCLUDE; 3946 } 3947 } 3948 /* Output any lingering errors that didn't come from ctf_link. */ 3949 lang_ctf_errs_warnings (ctf_output); 3950 3951 ld_stop_phase (PHASE_CTF); 3952 } 3953 3954 /* Let the emulation acquire strings from the dynamic strtab to help it optimize 3955 the CTF, if supported. */ 3956 3957 void 3958 ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab) 3959 { 3960 ld_start_phase (PHASE_CTF); 3961 ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab); 3962 ld_stop_phase (PHASE_CTF); 3963 } 3964 3965 /* Inform the emulation about the addition of a new dynamic symbol, in BFD 3966 internal format. */ 3967 void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym) 3968 { 3969 ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym); 3970 } 3971 3972 /* Write out the CTF section. Called early, if the emulation isn't going to 3973 need to dedup against the strtab and symtab, then possibly called from the 3974 target linker code if the dedup has happened. */ 3975 static void 3976 lang_write_ctf (int late) 3977 { 3978 size_t output_size; 3979 asection *output_sect; 3980 3981 if (!ctf_output) 3982 return; 3983 3984 ld_start_phase (PHASE_CTF); 3985 3986 if (late) 3987 { 3988 /* Emit CTF late if this emulation says it can do so. */ 3989 if (ldemul_emit_ctf_early ()) 3990 { 3991 ld_stop_phase (PHASE_CTF); 3992 return; 3993 } 3994 } 3995 else 3996 { 3997 if (!ldemul_emit_ctf_early ()) 3998 { 3999 ld_stop_phase (PHASE_CTF); 4000 return; 4001 } 4002 } 4003 4004 /* Inform the emulation that all the symbols that will be received have 4005 been. */ 4006 4007 ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL); 4008 4009 /* Emit CTF. */ 4010 4011 output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf"); 4012 if (output_sect) 4013 { 4014 output_sect->contents = ctf_link_write (ctf_output, &output_size, 4015 CTF_COMPRESSION_THRESHOLD); 4016 output_sect->size = output_size; 4017 output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP; 4018 4019 lang_ctf_errs_warnings (ctf_output); 4020 if (!output_sect->contents) 4021 { 4022 einfo (_("%P: warning: CTF section emission failed; " 4023 "output will have no CTF section: %s\n"), 4024 ctf_errmsg (ctf_errno (ctf_output))); 4025 output_sect->size = 0; 4026 output_sect->flags |= SEC_EXCLUDE; 4027 } 4028 } 4029 4030 /* This also closes every CTF input file used in the link. */ 4031 ctf_dict_close (ctf_output); 4032 ctf_output = NULL; 4033 4034 LANG_FOR_EACH_INPUT_STATEMENT (file) 4035 file->the_ctf = NULL; 4036 4037 ld_stop_phase (PHASE_CTF); 4038 } 4039 4040 /* Write out the CTF section late, if the emulation needs that. */ 4041 4042 void 4043 ldlang_write_ctf_late (void) 4044 { 4045 /* Trigger a "late call", if the emulation needs one. */ 4046 4047 lang_write_ctf (1); 4048 } 4049 #else 4050 static void 4051 ldlang_open_ctf (void) 4052 { 4053 LANG_FOR_EACH_INPUT_STATEMENT (file) 4054 { 4055 asection *sect; 4056 4057 /* If built without CTF, warn and delete all CTF sections from the output. 4058 (The alternative would be to simply concatenate them, which does not 4059 yield a valid CTF section.) */ 4060 4061 if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL) 4062 { 4063 einfo (_("%P: warning: CTF section in %pB not linkable: " 4064 "%P was built without support for CTF\n"), file->the_bfd); 4065 sect->size = 0; 4066 sect->flags |= SEC_EXCLUDE; 4067 } 4068 } 4069 } 4070 4071 static void lang_merge_ctf (void) {} 4072 void 4073 ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab 4074 ATTRIBUTE_UNUSED) {} 4075 void 4076 ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED, 4077 struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {} 4078 static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {} 4079 void ldlang_write_ctf_late (void) {} 4080 #endif 4081 4082 /* Add the supplied name to the symbol table as an undefined reference. 4083 This is a two step process as the symbol table doesn't even exist at 4084 the time the ld command line is processed. First we put the name 4085 on a list, then, once the output file has been opened, transfer the 4086 name to the symbol table. */ 4087 4088 typedef struct bfd_sym_chain ldlang_undef_chain_list_type; 4089 4090 #define ldlang_undef_chain_list_head entry_symbol.next 4091 4092 void 4093 ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED) 4094 { 4095 ldlang_undef_chain_list_type *new_undef; 4096 4097 new_undef = stat_alloc (sizeof (*new_undef)); 4098 new_undef->next = ldlang_undef_chain_list_head; 4099 ldlang_undef_chain_list_head = new_undef; 4100 4101 new_undef->name = stat_strdup (name); 4102 4103 if (link_info.output_bfd != NULL) 4104 insert_undefined (new_undef->name); 4105 } 4106 4107 /* Insert NAME as undefined in the symbol table. */ 4108 4109 static void 4110 insert_undefined (const char *name) 4111 { 4112 struct bfd_link_hash_entry *h; 4113 4114 h = bfd_link_hash_lookup (link_info.hash, name, true, false, true); 4115 if (h == NULL) 4116 fatal (_("%P: bfd_link_hash_lookup failed: %E\n")); 4117 if (h->type == bfd_link_hash_new) 4118 { 4119 h->type = bfd_link_hash_undefined; 4120 h->u.undef.abfd = NULL; 4121 h->non_ir_ref_regular = true; 4122 bfd_link_add_undef (link_info.hash, h); 4123 } 4124 } 4125 4126 /* Run through the list of undefineds created above and place them 4127 into the linker hash table as undefined symbols belonging to the 4128 script file. */ 4129 4130 static void 4131 lang_place_undefineds (void) 4132 { 4133 ldlang_undef_chain_list_type *ptr; 4134 4135 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) 4136 insert_undefined (ptr->name); 4137 } 4138 4139 /* Mark -u symbols against garbage collection. */ 4140 4141 static void 4142 lang_mark_undefineds (void) 4143 { 4144 ldlang_undef_chain_list_type *ptr; 4145 4146 if (is_elf_hash_table (link_info.hash)) 4147 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) 4148 { 4149 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) 4150 bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true); 4151 if (h != NULL) 4152 h->mark = 1; 4153 } 4154 } 4155 4156 /* Structure used to build the list of symbols that the user has required 4157 be defined. */ 4158 4159 struct require_defined_symbol 4160 { 4161 const char *name; 4162 struct require_defined_symbol *next; 4163 }; 4164 4165 /* The list of symbols that the user has required be defined. */ 4166 4167 static struct require_defined_symbol *require_defined_symbol_list; 4168 4169 /* Add a new symbol NAME to the list of symbols that are required to be 4170 defined. */ 4171 4172 void 4173 ldlang_add_require_defined (const char *const name) 4174 { 4175 struct require_defined_symbol *ptr; 4176 4177 ldlang_add_undef (name, true); 4178 ptr = stat_alloc (sizeof (*ptr)); 4179 ptr->next = require_defined_symbol_list; 4180 ptr->name = stat_strdup (name); 4181 require_defined_symbol_list = ptr; 4182 } 4183 4184 /* Check that all symbols the user required to be defined, are defined, 4185 raise an error if we find a symbol that is not defined. */ 4186 4187 static void 4188 ldlang_check_require_defined_symbols (void) 4189 { 4190 struct require_defined_symbol *ptr; 4191 4192 for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next) 4193 { 4194 struct bfd_link_hash_entry *h; 4195 4196 h = bfd_link_hash_lookup (link_info.hash, ptr->name, 4197 false, false, true); 4198 if (! is_defined (h)) 4199 einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name); 4200 } 4201 } 4202 4203 /* Check for all readonly or some readwrite sections. */ 4204 4205 static void 4206 check_input_sections 4207 (lang_statement_union_type *s, 4208 lang_output_section_statement_type *output_section_statement) 4209 { 4210 for (; s != NULL; s = s->header.next) 4211 { 4212 switch (s->header.type) 4213 { 4214 case lang_wild_statement_enum: 4215 walk_wild (&s->wild_statement, check_section_callback, 4216 output_section_statement); 4217 if (!output_section_statement->all_input_readonly) 4218 return; 4219 break; 4220 case lang_constructors_statement_enum: 4221 check_input_sections (constructor_list.head, 4222 output_section_statement); 4223 if (!output_section_statement->all_input_readonly) 4224 return; 4225 break; 4226 case lang_group_statement_enum: 4227 check_input_sections (s->group_statement.children.head, 4228 output_section_statement); 4229 if (!output_section_statement->all_input_readonly) 4230 return; 4231 break; 4232 default: 4233 break; 4234 } 4235 } 4236 } 4237 4238 /* Update wildcard statements if needed. */ 4239 4240 static void 4241 update_wild_statements (lang_statement_union_type *s) 4242 { 4243 struct wildcard_list *sec; 4244 4245 switch (sort_section) 4246 { 4247 default: 4248 FAIL (); 4249 4250 case none: 4251 break; 4252 4253 case by_name: 4254 case by_alignment: 4255 for (; s != NULL; s = s->header.next) 4256 { 4257 switch (s->header.type) 4258 { 4259 default: 4260 break; 4261 4262 case lang_wild_statement_enum: 4263 for (sec = s->wild_statement.section_list; sec != NULL; 4264 sec = sec->next) 4265 /* Don't sort .init/.fini sections. */ 4266 if (strcmp (sec->spec.name, ".init") != 0 4267 && strcmp (sec->spec.name, ".fini") != 0) 4268 { 4269 switch (sec->spec.sorted) 4270 { 4271 case none: 4272 sec->spec.sorted = sort_section; 4273 break; 4274 case by_name: 4275 if (sort_section == by_alignment) 4276 sec->spec.sorted = by_name_alignment; 4277 break; 4278 case by_alignment: 4279 if (sort_section == by_name) 4280 sec->spec.sorted = by_alignment_name; 4281 break; 4282 default: 4283 break; 4284 } 4285 s->wild_statement.any_specs_sorted = true; 4286 } 4287 break; 4288 4289 case lang_constructors_statement_enum: 4290 update_wild_statements (constructor_list.head); 4291 break; 4292 4293 case lang_output_section_statement_enum: 4294 update_wild_statements 4295 (s->output_section_statement.children.head); 4296 break; 4297 4298 case lang_group_statement_enum: 4299 update_wild_statements (s->group_statement.children.head); 4300 break; 4301 } 4302 } 4303 break; 4304 } 4305 } 4306 4307 /* Open input files and attach to output sections. */ 4308 4309 static void 4310 map_input_to_output_sections 4311 (lang_statement_union_type *s, const char *target, 4312 lang_output_section_statement_type *os) 4313 { 4314 for (; s != NULL; s = s->header.next) 4315 { 4316 lang_output_section_statement_type *tos; 4317 flagword flags; 4318 unsigned int type = 0; 4319 4320 switch (s->header.type) 4321 { 4322 case lang_wild_statement_enum: 4323 wild (&s->wild_statement, target, os); 4324 break; 4325 case lang_constructors_statement_enum: 4326 map_input_to_output_sections (constructor_list.head, 4327 target, 4328 os); 4329 break; 4330 case lang_output_section_statement_enum: 4331 tos = &s->output_section_statement; 4332 if (tos->constraint == ONLY_IF_RW 4333 || tos->constraint == ONLY_IF_RO) 4334 { 4335 tos->all_input_readonly = true; 4336 check_input_sections (tos->children.head, tos); 4337 if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO)) 4338 tos->constraint = -1; 4339 } 4340 if (tos->constraint >= 0) 4341 map_input_to_output_sections (tos->children.head, 4342 target, 4343 tos); 4344 break; 4345 case lang_output_statement_enum: 4346 break; 4347 case lang_target_statement_enum: 4348 target = s->target_statement.target; 4349 break; 4350 case lang_group_statement_enum: 4351 map_input_to_output_sections (s->group_statement.children.head, 4352 target, 4353 os); 4354 break; 4355 case lang_data_statement_enum: 4356 if (os == NULL) 4357 /* This should never happen. */ 4358 FAIL (); 4359 /* Make sure that any sections mentioned in the expression 4360 are initialized. */ 4361 exp_init_os (s->data_statement.exp); 4362 /* The output section gets CONTENTS, ALLOC and LOAD, but 4363 these may be overridden by the script. */ 4364 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD; 4365 switch (os->sectype) 4366 { 4367 case normal_section: 4368 case overlay_section: 4369 case first_overlay_section: 4370 break; 4371 case noalloc_section: 4372 flags = SEC_HAS_CONTENTS; 4373 break; 4374 case readonly_section: 4375 flags |= SEC_READONLY; 4376 break; 4377 case typed_readonly_section: 4378 flags |= SEC_READONLY; 4379 /* Fall through. */ 4380 case type_section: 4381 if (os->sectype_value->type.node_class == etree_name 4382 && os->sectype_value->type.node_code == NAME) 4383 { 4384 const char *name = os->sectype_value->name.name; 4385 if (strcmp (name, "SHT_PROGBITS") == 0) 4386 type = SHT_PROGBITS; 4387 else if (strcmp (name, "SHT_STRTAB") == 0) 4388 type = SHT_STRTAB; 4389 else if (strcmp (name, "SHT_NOTE") == 0) 4390 type = SHT_NOTE; 4391 else if (strcmp (name, "SHT_NOBITS") == 0) 4392 type = SHT_NOBITS; 4393 else if (strcmp (name, "SHT_INIT_ARRAY") == 0) 4394 type = SHT_INIT_ARRAY; 4395 else if (strcmp (name, "SHT_FINI_ARRAY") == 0) 4396 type = SHT_FINI_ARRAY; 4397 else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0) 4398 type = SHT_PREINIT_ARRAY; 4399 else 4400 fatal (_ ("%P: invalid type for output section `%s'\n"), 4401 os->name); 4402 } 4403 else 4404 { 4405 exp_fold_tree_no_dot (os->sectype_value, os); 4406 if (expld.result.valid_p) 4407 type = expld.result.value; 4408 else 4409 fatal (_ ("%P: invalid type for output section `%s'\n"), 4410 os->name); 4411 } 4412 break; 4413 case noload_section: 4414 if (bfd_get_flavour (link_info.output_bfd) 4415 == bfd_target_elf_flavour) 4416 flags = SEC_NEVER_LOAD | SEC_ALLOC; 4417 else 4418 flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS; 4419 break; 4420 } 4421 if (os->bfd_section == NULL) 4422 init_os (os, flags | SEC_READONLY); 4423 else 4424 os->bfd_section->flags |= flags; 4425 os->bfd_section->type = type; 4426 break; 4427 case lang_input_section_enum: 4428 break; 4429 case lang_fill_statement_enum: 4430 case lang_object_symbols_statement_enum: 4431 case lang_reloc_statement_enum: 4432 case lang_padding_statement_enum: 4433 case lang_input_statement_enum: 4434 if (os != NULL && os->bfd_section == NULL) 4435 init_os (os, 0); 4436 break; 4437 4438 case lang_assignment_statement_enum: 4439 if (os != NULL && os->bfd_section == NULL) 4440 init_os (os, get_os_init_flag (os)); 4441 4442 /* Make sure that any sections mentioned in the assignment 4443 are initialized. */ 4444 exp_init_os (s->assignment_statement.exp); 4445 break; 4446 4447 case lang_address_statement_enum: 4448 /* Mark the specified section with the supplied address. 4449 If this section was actually a segment marker, then the 4450 directive is ignored if the linker script explicitly 4451 processed the segment marker. Originally, the linker 4452 treated segment directives (like -Ttext on the 4453 command-line) as section directives. We honor the 4454 section directive semantics for backwards compatibility; 4455 linker scripts that do not specifically check for 4456 SEGMENT_START automatically get the old semantics. */ 4457 if (!s->address_statement.segment 4458 || !s->address_statement.segment->used) 4459 { 4460 const char *name = s->address_statement.section_name; 4461 4462 /* Create the output section statement here so that 4463 orphans with a set address will be placed after other 4464 script sections. If we let the orphan placement code 4465 place them in amongst other sections then the address 4466 will affect following script sections, which is 4467 likely to surprise naive users. */ 4468 tos = lang_output_section_statement_lookup (name, 0, 1); 4469 tos->addr_tree = s->address_statement.address; 4470 if (tos->bfd_section == NULL) 4471 init_os (tos, 0); 4472 } 4473 break; 4474 case lang_insert_statement_enum: 4475 break; 4476 case lang_input_matcher_enum: 4477 FAIL (); 4478 } 4479 } 4480 } 4481 4482 /* An insert statement snips out all the linker statements from the 4483 start of the list and places them after the output section 4484 statement specified by the insert. This operation is complicated 4485 by the fact that we keep a doubly linked list of output section 4486 statements as well as the singly linked list of all statements. 4487 FIXME someday: Twiddling with the list not only moves statements 4488 from the user's script but also input and group statements that are 4489 built from command line object files and --start-group. We only 4490 get away with this because the list pointers used by file_chain 4491 and input_file_chain are not reordered, and processing via 4492 statement_list after this point mostly ignores input statements. 4493 One exception is the map file, where LOAD and START GROUP/END GROUP 4494 can end up looking odd. */ 4495 4496 static void 4497 process_insert_statements (lang_statement_union_type **start) 4498 { 4499 lang_statement_union_type **s; 4500 lang_output_section_statement_type *first_os = NULL; 4501 lang_output_section_statement_type *last_os = NULL; 4502 lang_output_section_statement_type *os; 4503 4504 s = start; 4505 while (*s != NULL) 4506 { 4507 if ((*s)->header.type == lang_output_section_statement_enum) 4508 { 4509 /* Keep pointers to the first and last output section 4510 statement in the sequence we may be about to move. */ 4511 os = &(*s)->output_section_statement; 4512 4513 ASSERT (last_os == NULL || last_os->next == os); 4514 last_os = os; 4515 4516 /* Set constraint negative so that lang_output_section_find 4517 won't match this output section statement. At this 4518 stage in linking constraint has values in the range 4519 [-1, ONLY_IN_RW]. */ 4520 last_os->constraint = -2 - last_os->constraint; 4521 if (first_os == NULL) 4522 first_os = last_os; 4523 } 4524 else if ((*s)->header.type == lang_group_statement_enum) 4525 { 4526 /* A user might put -T between --start-group and 4527 --end-group. One way this odd construct might arise is 4528 from a wrapper around ld to change library search 4529 behaviour. For example: 4530 #! /bin/sh 4531 exec real_ld --start-group "$@" --end-group 4532 This isn't completely unreasonable so go looking inside a 4533 group statement for insert statements. */ 4534 process_insert_statements (&(*s)->group_statement.children.head); 4535 } 4536 else if ((*s)->header.type == lang_insert_statement_enum) 4537 { 4538 lang_insert_statement_type *i = &(*s)->insert_statement; 4539 lang_output_section_statement_type *where; 4540 lang_statement_union_type **ptr; 4541 lang_statement_union_type *first; 4542 4543 if (link_info.non_contiguous_regions) 4544 { 4545 einfo (_("warning: INSERT statement in linker script is " 4546 "incompatible with --enable-non-contiguous-regions.\n")); 4547 } 4548 4549 where = lang_output_section_find (i->where); 4550 if (where != NULL && i->is_before) 4551 { 4552 do 4553 where = where->prev; 4554 while (where != NULL && where->constraint < 0); 4555 } 4556 if (where == NULL) 4557 { 4558 fatal (_("%P: %s not found for insert\n"), i->where); 4559 return; 4560 } 4561 4562 /* Deal with reordering the output section statement list. */ 4563 if (last_os != NULL) 4564 { 4565 asection *first_sec, *last_sec; 4566 struct lang_output_section_statement_struct **next; 4567 4568 /* Snip out the output sections we are moving. */ 4569 first_os->prev->next = last_os->next; 4570 if (last_os->next == NULL) 4571 { 4572 next = &first_os->prev->next; 4573 lang_os_list.tail = (lang_statement_union_type **) next; 4574 } 4575 else 4576 last_os->next->prev = first_os->prev; 4577 /* Add them in at the new position. */ 4578 last_os->next = where->next; 4579 if (where->next == NULL) 4580 { 4581 next = &last_os->next; 4582 lang_os_list.tail = (lang_statement_union_type **) next; 4583 } 4584 else 4585 where->next->prev = last_os; 4586 first_os->prev = where; 4587 where->next = first_os; 4588 4589 /* Move the bfd sections in the same way. */ 4590 first_sec = NULL; 4591 last_sec = NULL; 4592 for (os = first_os; os != NULL; os = os->next) 4593 { 4594 os->constraint = -2 - os->constraint; 4595 if (os->bfd_section != NULL 4596 && os->bfd_section->owner != NULL) 4597 { 4598 last_sec = os->bfd_section; 4599 if (first_sec == NULL) 4600 first_sec = last_sec; 4601 } 4602 if (os == last_os) 4603 break; 4604 } 4605 if (last_sec != NULL) 4606 { 4607 asection *sec = where->bfd_section; 4608 if (sec == NULL) 4609 sec = output_prev_sec_find (where); 4610 4611 /* The place we want to insert must come after the 4612 sections we are moving. So if we find no 4613 section or if the section is the same as our 4614 last section, then no move is needed. */ 4615 if (sec != NULL && sec != last_sec) 4616 { 4617 /* Trim them off. */ 4618 if (first_sec->prev != NULL) 4619 first_sec->prev->next = last_sec->next; 4620 else 4621 link_info.output_bfd->sections = last_sec->next; 4622 if (last_sec->next != NULL) 4623 last_sec->next->prev = first_sec->prev; 4624 else 4625 link_info.output_bfd->section_last = first_sec->prev; 4626 /* Add back. */ 4627 if (sec->owner == NULL) 4628 /* SEC is the absolute section, from the 4629 first dummy output section statement. Add 4630 back the sections we trimmed off to the 4631 start of the bfd sections. */ 4632 sec = NULL; 4633 if (sec != NULL) 4634 last_sec->next = sec->next; 4635 else 4636 last_sec->next = link_info.output_bfd->sections; 4637 if (last_sec->next != NULL) 4638 last_sec->next->prev = last_sec; 4639 else 4640 link_info.output_bfd->section_last = last_sec; 4641 first_sec->prev = sec; 4642 if (first_sec->prev != NULL) 4643 first_sec->prev->next = first_sec; 4644 else 4645 link_info.output_bfd->sections = first_sec; 4646 } 4647 } 4648 } 4649 4650 lang_statement_union_type *after = (void *) where; 4651 if (where == &lang_os_list.head->output_section_statement 4652 && where->next == first_os) 4653 { 4654 /* PR30155. Handle a corner case where the statement 4655 list is something like the following: 4656 . LOAD t.o 4657 . .data 0x0000000000000000 0x0 4658 . [0x0000000000000000] b = . 4659 . *(.data) 4660 . .data 0x0000000000000000 0x0 t.o 4661 . 0x0000000000000000 0x4 LONG 0x0 4662 . INSERT BEFORE .text.start 4663 . [0x0000000000000004] a = . 4664 . .text.start 0x0000000000000000 0x0 4665 . [0x0000000000000000] c = . 4666 . OUTPUT(a.out elf64-x86-64) 4667 Here we do not want to allow insert_os_after to 4668 choose a point inside the list we are moving. 4669 That would lose the list. Instead, let 4670 insert_os_after work from the INSERT, which in this 4671 particular example will result in inserting after 4672 the assignment "a = .". */ 4673 after = *s; 4674 } 4675 ptr = insert_os_after (after); 4676 /* Snip everything from the start of the list, up to and 4677 including the insert statement we are currently processing. */ 4678 first = *start; 4679 *start = (*s)->header.next; 4680 /* Add them back where they belong, minus the insert. */ 4681 *s = *ptr; 4682 if (*s == NULL) 4683 statement_list.tail = s; 4684 *ptr = first; 4685 s = start; 4686 first_os = NULL; 4687 last_os = NULL; 4688 continue; 4689 } 4690 s = &(*s)->header.next; 4691 } 4692 4693 /* Undo constraint twiddling. */ 4694 for (os = first_os; os != NULL; os = os->next) 4695 { 4696 os->constraint = -2 - os->constraint; 4697 if (os == last_os) 4698 break; 4699 } 4700 } 4701 4702 /* An output section might have been removed after its statement was 4703 added. For example, ldemul_before_allocation can remove dynamic 4704 sections if they turn out to be not needed. Clean them up here. */ 4705 4706 void 4707 strip_excluded_output_sections (void) 4708 { 4709 lang_output_section_statement_type *os; 4710 4711 /* Run lang_size_sections (if not already done). */ 4712 if (expld.phase != lang_mark_phase_enum) 4713 { 4714 expld.phase = lang_mark_phase_enum; 4715 expld.dataseg.phase = exp_seg_none; 4716 one_lang_size_sections_pass (NULL, false); 4717 lang_reset_memory_regions (); 4718 } 4719 4720 for (os = (void *) lang_os_list.head; 4721 os != NULL; 4722 os = os->next) 4723 { 4724 asection *output_section; 4725 bool exclude; 4726 4727 if (os->constraint < 0) 4728 continue; 4729 4730 output_section = os->bfd_section; 4731 if (output_section == NULL) 4732 continue; 4733 4734 exclude = (output_section->rawsize == 0 4735 && (output_section->flags & SEC_KEEP) == 0 4736 && !bfd_section_removed_from_list (link_info.output_bfd, 4737 output_section)); 4738 4739 /* Some sections have not yet been sized, notably .gnu.version, 4740 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED 4741 input sections, so don't drop output sections that have such 4742 input sections unless they are also marked SEC_EXCLUDE. */ 4743 if (exclude && output_section->map_head.s != NULL) 4744 { 4745 asection *s; 4746 4747 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s) 4748 if ((s->flags & SEC_EXCLUDE) == 0 4749 && ((s->flags & SEC_LINKER_CREATED) != 0 4750 || link_info.emitrelocations)) 4751 { 4752 exclude = false; 4753 break; 4754 } 4755 } 4756 4757 if (exclude) 4758 { 4759 /* We don't set bfd_section to NULL since bfd_section of the 4760 removed output section statement may still be used. */ 4761 if (!os->update_dot) 4762 os->ignored = true; 4763 output_section->flags |= SEC_EXCLUDE; 4764 bfd_section_list_remove (link_info.output_bfd, output_section); 4765 link_info.output_bfd->section_count--; 4766 } 4767 } 4768 } 4769 4770 /* Called from ldwrite to clear out asection.map_head and 4771 asection.map_tail for use as link_orders in ldwrite. */ 4772 4773 void 4774 lang_clear_os_map (void) 4775 { 4776 lang_output_section_statement_type *os; 4777 4778 if (map_head_is_link_order) 4779 return; 4780 4781 for (os = (void *) lang_os_list.head; 4782 os != NULL; 4783 os = os->next) 4784 { 4785 asection *output_section; 4786 4787 if (os->constraint < 0) 4788 continue; 4789 4790 output_section = os->bfd_section; 4791 if (output_section == NULL) 4792 continue; 4793 4794 /* TODO: Don't just junk map_head.s, turn them into link_orders. */ 4795 output_section->map_head.link_order = NULL; 4796 output_section->map_tail.link_order = NULL; 4797 } 4798 4799 /* Stop future calls to lang_add_section from messing with map_head 4800 and map_tail link_order fields. */ 4801 map_head_is_link_order = true; 4802 } 4803 4804 static void 4805 print_output_section_statement 4806 (lang_output_section_statement_type *output_section_statement) 4807 { 4808 asection *section = output_section_statement->bfd_section; 4809 int len; 4810 4811 if (output_section_statement != abs_output_section) 4812 { 4813 minfo ("\n%s", output_section_statement->name); 4814 4815 if (section != NULL) 4816 { 4817 print_dot = section->vma; 4818 4819 len = strlen (output_section_statement->name); 4820 if (len >= SECTION_NAME_MAP_LENGTH - 1) 4821 { 4822 print_nl (); 4823 len = 0; 4824 } 4825 print_spaces (SECTION_NAME_MAP_LENGTH - len); 4826 4827 minfo ("0x%V %W", section->vma, TO_ADDR (section->size)); 4828 4829 if (section->vma != section->lma) 4830 minfo (_(" load address 0x%V"), section->lma); 4831 4832 if (output_section_statement->update_dot_tree != NULL) 4833 exp_fold_tree (output_section_statement->update_dot_tree, 4834 output_section_statement, 4835 bfd_abs_section_ptr, &print_dot); 4836 } 4837 4838 print_nl (); 4839 } 4840 4841 print_statement_list (output_section_statement->children.head, 4842 output_section_statement); 4843 } 4844 4845 static void 4846 print_assignment (lang_assignment_statement_type *assignment, 4847 lang_output_section_statement_type *output_section) 4848 { 4849 bool is_dot; 4850 etree_type *tree; 4851 asection *osec; 4852 4853 print_spaces (SECTION_NAME_MAP_LENGTH); 4854 4855 if (assignment->exp->type.node_class == etree_assert) 4856 { 4857 is_dot = false; 4858 tree = assignment->exp->assert_s.child; 4859 } 4860 else 4861 { 4862 const char *dst = assignment->exp->assign.dst; 4863 4864 is_dot = (dst[0] == '.' && dst[1] == 0); 4865 tree = assignment->exp; 4866 } 4867 4868 osec = output_section->bfd_section; 4869 if (osec == NULL) 4870 osec = bfd_abs_section_ptr; 4871 4872 if (assignment->exp->type.node_class != etree_provide) 4873 exp_fold_tree (tree, output_section, osec, &print_dot); 4874 else 4875 expld.result.valid_p = false; 4876 4877 char buf[32]; 4878 const char *str = buf; 4879 if (expld.result.valid_p) 4880 { 4881 bfd_vma value; 4882 4883 if (assignment->exp->type.node_class == etree_assert 4884 || is_dot 4885 || expld.assign_name != NULL) 4886 { 4887 value = expld.result.value; 4888 4889 if (expld.result.section != NULL) 4890 value += expld.result.section->vma; 4891 4892 buf[0] = '0'; 4893 buf[1] = 'x'; 4894 bfd_sprintf_vma (link_info.output_bfd, buf + 2, value); 4895 if (is_dot) 4896 print_dot = value; 4897 } 4898 else 4899 { 4900 struct bfd_link_hash_entry *h; 4901 4902 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst, 4903 false, false, true); 4904 if (is_defined (h)) 4905 { 4906 value = h->u.def.value; 4907 value += h->u.def.section->output_section->vma; 4908 value += h->u.def.section->output_offset; 4909 4910 buf[0] = '['; 4911 buf[1] = '0'; 4912 buf[2] = 'x'; 4913 bfd_sprintf_vma (link_info.output_bfd, buf + 3, value); 4914 strcat (buf, "]"); 4915 } 4916 else 4917 str = "[unresolved]"; 4918 } 4919 } 4920 else 4921 { 4922 if (assignment->exp->type.node_class == etree_provide) 4923 str = "[!provide]"; 4924 else 4925 str = "*undef*"; 4926 } 4927 expld.assign_name = NULL; 4928 4929 fprintf (config.map_file, "%-34s", str); 4930 exp_print_tree (assignment->exp); 4931 print_nl (); 4932 } 4933 4934 static void 4935 print_input_statement (lang_input_statement_type *statm) 4936 { 4937 if (statm->filename != NULL) 4938 fprintf (config.map_file, "LOAD %s\n", statm->filename); 4939 } 4940 4941 /* Print all symbols defined in a particular section. This is called 4942 via bfd_link_hash_traverse, or by print_all_symbols. */ 4943 4944 bool 4945 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) 4946 { 4947 asection *sec = (asection *) ptr; 4948 4949 if (is_defined (hash_entry) 4950 && sec == hash_entry->u.def.section) 4951 { 4952 print_spaces (SECTION_NAME_MAP_LENGTH); 4953 minfo ("0x%V ", 4954 (hash_entry->u.def.value 4955 + hash_entry->u.def.section->output_offset 4956 + hash_entry->u.def.section->output_section->vma)); 4957 4958 minfo (" %pT\n", hash_entry->root.string); 4959 } 4960 4961 return true; 4962 } 4963 4964 static int 4965 hash_entry_addr_cmp (const void *a, const void *b) 4966 { 4967 const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a; 4968 const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b; 4969 4970 if (l->u.def.value < r->u.def.value) 4971 return -1; 4972 else if (l->u.def.value > r->u.def.value) 4973 return 1; 4974 else 4975 return 0; 4976 } 4977 4978 static void 4979 print_all_symbols (asection *sec) 4980 { 4981 input_section_userdata_type *ud = bfd_section_userdata (sec); 4982 struct map_symbol_def *def; 4983 struct bfd_link_hash_entry **entries; 4984 unsigned int i; 4985 4986 if (!ud) 4987 return; 4988 4989 *ud->map_symbol_def_tail = 0; 4990 4991 /* Sort the symbols by address. */ 4992 entries = (struct bfd_link_hash_entry **) 4993 obstack_alloc (&map_obstack, 4994 ud->map_symbol_def_count * sizeof (*entries)); 4995 4996 for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++) 4997 entries[i] = def->entry; 4998 4999 qsort (entries, ud->map_symbol_def_count, sizeof (*entries), 5000 hash_entry_addr_cmp); 5001 5002 /* Print the symbols. */ 5003 for (i = 0; i < ud->map_symbol_def_count; i++) 5004 ldemul_print_symbol (entries[i], sec); 5005 5006 obstack_free (&map_obstack, entries); 5007 } 5008 5009 /* Returns TRUE if SYM is a symbol suitable for printing 5010 in a linker map as a local symbol. */ 5011 5012 static bool 5013 ld_is_local_symbol (asymbol * sym) 5014 { 5015 const char * name = bfd_asymbol_name (sym); 5016 5017 if (name == NULL || *name == 0) 5018 return false; 5019 5020 /* Skip .Lxxx and such like. */ 5021 if (bfd_is_local_label (link_info.output_bfd, sym)) 5022 return false; 5023 5024 /* FIXME: This is intended to skip ARM mapping symbols, 5025 which for some reason are not excluded by bfd_is_local_label, 5026 but maybe it is wrong for other architectures. 5027 It would be better to fix bfd_is_local_label. */ 5028 if (*name == '$') 5029 return false; 5030 5031 /* Some local symbols, eg _GLOBAL_OFFSET_TABLE_, are present 5032 in the hash table, so do not print duplicates here. */ 5033 struct bfd_link_hash_entry * h; 5034 h = bfd_link_hash_lookup (link_info.hash, name, false /* create */, 5035 false /* copy */, true /* follow */); 5036 if (h == NULL) 5037 return true; 5038 5039 /* Symbols from the plugin owned BFD will not get their own 5040 iteration of this function, but can be on the link_info 5041 list. So include them here. */ 5042 if (h->u.def.section->owner != NULL 5043 && ((bfd_get_file_flags (h->u.def.section->owner) & (BFD_LINKER_CREATED | BFD_PLUGIN)) 5044 == (BFD_LINKER_CREATED | BFD_PLUGIN))) 5045 return true; 5046 5047 return false; 5048 } 5049 5050 /* Print information about an input section to the map file. */ 5051 5052 static void 5053 print_input_section (asection *i, bool is_discarded) 5054 { 5055 bfd_size_type size = i->size; 5056 int len; 5057 bfd_vma addr; 5058 5059 init_opb (i); 5060 5061 minfo (" %s", i->name); 5062 5063 len = 1 + strlen (i->name); 5064 if (len >= SECTION_NAME_MAP_LENGTH - 1) 5065 { 5066 print_nl (); 5067 len = 0; 5068 } 5069 print_spaces (SECTION_NAME_MAP_LENGTH - len); 5070 5071 if ((i->flags & SEC_EXCLUDE) == 0 5072 && i->output_section != NULL 5073 && i->output_section->owner == link_info.output_bfd) 5074 addr = i->output_section->vma + i->output_offset; 5075 else 5076 { 5077 addr = print_dot; 5078 if (!is_discarded) 5079 size = 0; 5080 } 5081 5082 char buf[32]; 5083 bfd_sprintf_vma (link_info.output_bfd, buf, addr); 5084 minfo ("0x%s %W %pB\n", buf, TO_ADDR (size), i->owner); 5085 5086 if (size != i->rawsize && i->rawsize != 0) 5087 { 5088 len = SECTION_NAME_MAP_LENGTH + 3 + strlen (buf); 5089 print_spaces (len); 5090 minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize)); 5091 } 5092 5093 if (i->output_section != NULL 5094 && i->output_section->owner == link_info.output_bfd) 5095 { 5096 if (link_info.reduce_memory_overheads) 5097 bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i); 5098 else 5099 print_all_symbols (i); 5100 5101 /* Update print_dot, but make sure that we do not move it 5102 backwards - this could happen if we have overlays and a 5103 later overlay is shorter than an earier one. */ 5104 if (addr + TO_ADDR (size) > print_dot) 5105 print_dot = addr + TO_ADDR (size); 5106 5107 if (config.print_map_locals) 5108 { 5109 long storage_needed; 5110 5111 /* FIXME: It would be better to cache this table, rather 5112 than recreating it for each output section. */ 5113 /* FIXME: This call is not working for non-ELF based targets. 5114 Find out why. */ 5115 storage_needed = bfd_get_symtab_upper_bound (link_info.output_bfd); 5116 if (storage_needed > 0) 5117 { 5118 asymbol ** symbol_table; 5119 long number_of_symbols; 5120 long j; 5121 5122 symbol_table = xmalloc (storage_needed); 5123 number_of_symbols = bfd_canonicalize_symtab (link_info.output_bfd, symbol_table); 5124 5125 for (j = 0; j < number_of_symbols; j++) 5126 { 5127 asymbol * sym = symbol_table[j]; 5128 bfd_vma sym_addr = sym->value + i->output_section->vma; 5129 5130 if (sym->section == i->output_section 5131 && (sym->flags & BSF_LOCAL) != 0 5132 && sym_addr >= addr 5133 && sym_addr < print_dot 5134 && ld_is_local_symbol (sym)) 5135 { 5136 print_spaces (SECTION_NAME_MAP_LENGTH); 5137 minfo ("0x%V (local) %s\n", sym_addr, bfd_asymbol_name (sym)); 5138 } 5139 } 5140 5141 free (symbol_table); 5142 } 5143 } 5144 } 5145 } 5146 5147 static void 5148 print_fill_statement (lang_fill_statement_type *fill) 5149 { 5150 size_t size; 5151 unsigned char *p; 5152 fputs (" FILL mask 0x", config.map_file); 5153 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) 5154 fprintf (config.map_file, "%02x", *p); 5155 fputs ("\n", config.map_file); 5156 } 5157 5158 static void 5159 print_data_statement (lang_data_statement_type *data) 5160 { 5161 bfd_vma addr; 5162 bfd_size_type size; 5163 const char *name; 5164 5165 init_opb (data->output_section); 5166 print_spaces (SECTION_NAME_MAP_LENGTH); 5167 5168 addr = data->output_offset; 5169 if (data->output_section != NULL) 5170 addr += data->output_section->vma; 5171 5172 switch (data->type) 5173 { 5174 default: 5175 abort (); 5176 case BYTE: 5177 size = BYTE_SIZE; 5178 name = "BYTE"; 5179 break; 5180 case SHORT: 5181 size = SHORT_SIZE; 5182 name = "SHORT"; 5183 break; 5184 case LONG: 5185 size = LONG_SIZE; 5186 name = "LONG"; 5187 break; 5188 case QUAD: 5189 size = QUAD_SIZE; 5190 name = "QUAD"; 5191 break; 5192 case SQUAD: 5193 size = QUAD_SIZE; 5194 name = "SQUAD"; 5195 break; 5196 } 5197 5198 if (size < TO_SIZE ((unsigned) 1)) 5199 size = TO_SIZE ((unsigned) 1); 5200 minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value); 5201 5202 if (data->exp->type.node_class != etree_value) 5203 { 5204 print_space (); 5205 exp_print_tree (data->exp); 5206 } 5207 5208 print_nl (); 5209 5210 print_dot = addr + TO_ADDR (size); 5211 } 5212 5213 /* Print an address statement. These are generated by options like 5214 -Ttext. */ 5215 5216 static void 5217 print_address_statement (lang_address_statement_type *address) 5218 { 5219 minfo (_("Address of section %s set to "), address->section_name); 5220 exp_print_tree (address->address); 5221 print_nl (); 5222 } 5223 5224 /* Print a reloc statement. */ 5225 5226 static void 5227 print_reloc_statement (lang_reloc_statement_type *reloc) 5228 { 5229 bfd_vma addr; 5230 bfd_size_type size; 5231 5232 init_opb (reloc->output_section); 5233 print_spaces (SECTION_NAME_MAP_LENGTH); 5234 5235 addr = reloc->output_offset; 5236 if (reloc->output_section != NULL) 5237 addr += reloc->output_section->vma; 5238 5239 size = bfd_get_reloc_size (reloc->howto); 5240 5241 minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name); 5242 5243 if (reloc->name != NULL) 5244 minfo ("%s+", reloc->name); 5245 else 5246 minfo ("%s+", reloc->section->name); 5247 5248 exp_print_tree (reloc->addend_exp); 5249 5250 print_nl (); 5251 5252 print_dot = addr + TO_ADDR (size); 5253 } 5254 5255 static void 5256 print_padding_statement (lang_padding_statement_type *s) 5257 { 5258 int len; 5259 bfd_vma addr; 5260 5261 init_opb (s->output_section); 5262 minfo (" *fill*"); 5263 5264 len = sizeof " *fill*" - 1; 5265 print_spaces (SECTION_NAME_MAP_LENGTH - len); 5266 5267 addr = s->output_offset; 5268 if (s->output_section != NULL) 5269 addr += s->output_section->vma; 5270 minfo ("0x%V %W ", addr, TO_ADDR (s->size)); 5271 5272 if (s->fill->size != 0) 5273 { 5274 size_t size; 5275 unsigned char *p; 5276 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) 5277 fprintf (config.map_file, "%02x", *p); 5278 } 5279 5280 print_nl (); 5281 5282 print_dot = addr + TO_ADDR (s->size); 5283 } 5284 5285 static void 5286 print_wild_statement (lang_wild_statement_type *w, 5287 lang_output_section_statement_type *os) 5288 { 5289 struct wildcard_list *sec; 5290 5291 print_space (); 5292 5293 if (w->exclude_name_list) 5294 { 5295 name_list *tmp; 5296 minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name); 5297 for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next) 5298 minfo (" %s", tmp->name); 5299 minfo (") "); 5300 } 5301 5302 if (w->filenames_sorted) 5303 minfo ("SORT_BY_NAME("); 5304 if (w->filenames_reversed) 5305 minfo ("REVERSE("); 5306 if (w->filename != NULL) 5307 minfo ("%s", w->filename); 5308 else 5309 minfo ("*"); 5310 if (w->filenames_reversed) 5311 minfo (")"); 5312 if (w->filenames_sorted) 5313 minfo (")"); 5314 5315 minfo ("("); 5316 for (sec = w->section_list; sec; sec = sec->next) 5317 { 5318 int closing_paren = 0; 5319 5320 switch (sec->spec.sorted) 5321 { 5322 case none: 5323 break; 5324 5325 case by_name: 5326 minfo ("SORT_BY_NAME("); 5327 closing_paren = 1; 5328 break; 5329 5330 case by_alignment: 5331 minfo ("SORT_BY_ALIGNMENT("); 5332 closing_paren = 1; 5333 break; 5334 5335 case by_name_alignment: 5336 minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT("); 5337 closing_paren = 2; 5338 break; 5339 5340 case by_alignment_name: 5341 minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME("); 5342 closing_paren = 2; 5343 break; 5344 5345 case by_none: 5346 minfo ("SORT_NONE("); 5347 closing_paren = 1; 5348 break; 5349 5350 case by_init_priority: 5351 minfo ("SORT_BY_INIT_PRIORITY("); 5352 closing_paren = 1; 5353 break; 5354 } 5355 5356 if (sec->spec.reversed) 5357 { 5358 minfo ("REVERSE("); 5359 closing_paren++; 5360 } 5361 5362 if (sec->spec.exclude_name_list != NULL) 5363 { 5364 name_list *tmp; 5365 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); 5366 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) 5367 minfo (" %s", tmp->name); 5368 minfo (") "); 5369 } 5370 if (sec->spec.name != NULL) 5371 minfo ("%s", sec->spec.name); 5372 else 5373 minfo ("*"); 5374 for (;closing_paren > 0; closing_paren--) 5375 minfo (")"); 5376 if (sec->next) 5377 minfo (" "); 5378 } 5379 minfo (")"); 5380 5381 print_nl (); 5382 5383 print_statement_list (w->children.head, os); 5384 } 5385 5386 /* Print a group statement. */ 5387 5388 static void 5389 print_group (lang_group_statement_type *s, 5390 lang_output_section_statement_type *os) 5391 { 5392 fprintf (config.map_file, "START GROUP\n"); 5393 print_statement_list (s->children.head, os); 5394 fprintf (config.map_file, "END GROUP\n"); 5395 } 5396 5397 /* Print the list of statements in S. 5398 This can be called for any statement type. */ 5399 5400 static void 5401 print_statement_list (lang_statement_union_type *s, 5402 lang_output_section_statement_type *os) 5403 { 5404 while (s != NULL) 5405 { 5406 print_statement (s, os); 5407 s = s->header.next; 5408 } 5409 } 5410 5411 /* Print the first statement in statement list S. 5412 This can be called for any statement type. */ 5413 5414 static void 5415 print_statement (lang_statement_union_type *s, 5416 lang_output_section_statement_type *os) 5417 { 5418 switch (s->header.type) 5419 { 5420 default: 5421 fprintf (config.map_file, _("Fail with %d\n"), s->header.type); 5422 FAIL (); 5423 break; 5424 case lang_constructors_statement_enum: 5425 if (constructor_list.head != NULL) 5426 { 5427 if (constructors_sorted) 5428 minfo (" SORT (CONSTRUCTORS)\n"); 5429 else 5430 minfo (" CONSTRUCTORS\n"); 5431 print_statement_list (constructor_list.head, os); 5432 } 5433 break; 5434 case lang_wild_statement_enum: 5435 print_wild_statement (&s->wild_statement, os); 5436 break; 5437 case lang_address_statement_enum: 5438 print_address_statement (&s->address_statement); 5439 break; 5440 case lang_object_symbols_statement_enum: 5441 minfo (" CREATE_OBJECT_SYMBOLS\n"); 5442 break; 5443 case lang_fill_statement_enum: 5444 print_fill_statement (&s->fill_statement); 5445 break; 5446 case lang_data_statement_enum: 5447 print_data_statement (&s->data_statement); 5448 break; 5449 case lang_reloc_statement_enum: 5450 print_reloc_statement (&s->reloc_statement); 5451 break; 5452 case lang_input_section_enum: 5453 print_input_section (s->input_section.section, false); 5454 break; 5455 case lang_padding_statement_enum: 5456 print_padding_statement (&s->padding_statement); 5457 break; 5458 case lang_output_section_statement_enum: 5459 print_output_section_statement (&s->output_section_statement); 5460 break; 5461 case lang_assignment_statement_enum: 5462 print_assignment (&s->assignment_statement, os); 5463 break; 5464 case lang_target_statement_enum: 5465 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); 5466 break; 5467 case lang_output_statement_enum: 5468 minfo ("OUTPUT(%s", s->output_statement.name); 5469 if (output_target != NULL) 5470 minfo (" %s", output_target); 5471 minfo (")\n"); 5472 break; 5473 case lang_input_statement_enum: 5474 print_input_statement (&s->input_statement); 5475 break; 5476 case lang_group_statement_enum: 5477 print_group (&s->group_statement, os); 5478 break; 5479 case lang_insert_statement_enum: 5480 minfo ("INSERT %s %s\n", 5481 s->insert_statement.is_before ? "BEFORE" : "AFTER", 5482 s->insert_statement.where); 5483 break; 5484 } 5485 } 5486 5487 static void 5488 print_statements (void) 5489 { 5490 print_statement_list (statement_list.head, abs_output_section); 5491 } 5492 5493 /* Print the first N statements in statement list S to STDERR. 5494 If N == 0, nothing is printed. 5495 If N < 0, the entire list is printed. 5496 Intended to be called from GDB. */ 5497 5498 void 5499 dprint_statement (lang_statement_union_type *s, int n) 5500 { 5501 FILE *map_save = config.map_file; 5502 5503 config.map_file = stderr; 5504 5505 if (n < 0) 5506 print_statement_list (s, abs_output_section); 5507 else 5508 { 5509 while (s && --n >= 0) 5510 { 5511 print_statement (s, abs_output_section); 5512 s = s->header.next; 5513 } 5514 } 5515 5516 config.map_file = map_save; 5517 } 5518 5519 static void 5520 insert_pad (lang_statement_union_type **ptr, 5521 fill_type *fill, 5522 bfd_size_type alignment_needed, 5523 asection *output_section, 5524 bfd_vma dot) 5525 { 5526 static fill_type zero_fill; 5527 lang_statement_union_type *pad = NULL; 5528 5529 if (ptr != &statement_list.head) 5530 pad = ((lang_statement_union_type *) 5531 ((char *) ptr - offsetof (lang_statement_union_type, header.next))); 5532 if (pad != NULL 5533 && pad->header.type == lang_padding_statement_enum 5534 && pad->padding_statement.output_section == output_section) 5535 { 5536 /* Use the existing pad statement. */ 5537 } 5538 else if ((pad = *ptr) != NULL 5539 && pad->header.type == lang_padding_statement_enum 5540 && pad->padding_statement.output_section == output_section) 5541 { 5542 /* Use the existing pad statement. */ 5543 } 5544 else 5545 { 5546 /* Make a new padding statement, linked into existing chain. */ 5547 pad = stat_alloc (sizeof (lang_padding_statement_type)); 5548 pad->header.next = *ptr; 5549 *ptr = pad; 5550 pad->header.type = lang_padding_statement_enum; 5551 pad->padding_statement.output_section = output_section; 5552 if (fill == NULL) 5553 fill = &zero_fill; 5554 pad->padding_statement.fill = fill; 5555 } 5556 pad->padding_statement.output_offset = dot - output_section->vma; 5557 pad->padding_statement.size = alignment_needed; 5558 if (!(output_section->flags & SEC_FIXED_SIZE)) 5559 output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed) 5560 - output_section->vma); 5561 } 5562 5563 /* Work out how much this section will move the dot point. */ 5564 5565 static bfd_vma 5566 size_input_section 5567 (lang_statement_union_type **this_ptr, 5568 lang_output_section_statement_type *output_section_statement, 5569 fill_type *fill, 5570 bool *removed, 5571 bfd_vma dot) 5572 { 5573 lang_input_section_type *is = &((*this_ptr)->input_section); 5574 asection *i = is->section; 5575 asection *o = output_section_statement->bfd_section; 5576 *removed = 0; 5577 5578 if (link_info.non_contiguous_regions) 5579 { 5580 /* If the input section I has already been successfully assigned 5581 to an output section other than O, don't bother with it and 5582 let the caller remove it from the list. Keep processing in 5583 case we have already handled O, because the repeated passes 5584 have reinitialized its size. */ 5585 if (i->already_assigned && i->already_assigned != o) 5586 { 5587 *removed = 1; 5588 return dot; 5589 } 5590 } 5591 5592 if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 5593 i->output_offset = i->vma - o->vma; 5594 else if (((i->flags & SEC_EXCLUDE) != 0) 5595 || output_section_statement->ignored) 5596 i->output_offset = dot - o->vma; 5597 else 5598 { 5599 bfd_size_type alignment_needed; 5600 5601 /* Align this section first to the input sections requirement, 5602 then to the output section's requirement. If this alignment 5603 is greater than any seen before, then record it too. Perform 5604 the alignment by inserting a magic 'padding' statement. 5605 We can force input section alignment within an output section 5606 by using SUBALIGN. The value specified overrides any alignment 5607 given by input sections, whether larger or smaller. */ 5608 5609 if (output_section_statement->subsection_alignment != NULL) 5610 o->alignment_power = i->alignment_power = 5611 exp_get_power (output_section_statement->subsection_alignment, 5612 output_section_statement, 5613 "subsection alignment"); 5614 5615 if (o->alignment_power < i->alignment_power) 5616 o->alignment_power = i->alignment_power; 5617 5618 alignment_needed = align_power (dot, i->alignment_power) - dot; 5619 5620 if (alignment_needed != 0) 5621 { 5622 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot); 5623 dot += alignment_needed; 5624 } 5625 5626 if (link_info.non_contiguous_regions) 5627 { 5628 /* If I would overflow O, let the caller remove I from the 5629 list. */ 5630 if (output_section_statement->region) 5631 { 5632 bfd_vma end = output_section_statement->region->origin 5633 + output_section_statement->region->length; 5634 5635 if (dot + TO_ADDR (i->size) > end) 5636 { 5637 if (i->flags & SEC_LINKER_CREATED) 5638 fatal (_("%P: Output section `%pA' not large enough for " 5639 "the linker-created stubs section `%pA'.\n"), 5640 i->output_section, i); 5641 5642 if (i->rawsize && i->rawsize != i->size) 5643 fatal (_("%P: Relaxation not supported with " 5644 "--enable-non-contiguous-regions (section `%pA' " 5645 "would overflow `%pA' after it changed size).\n"), 5646 i, i->output_section); 5647 5648 *removed = 1; 5649 dot = end; 5650 i->output_section = NULL; 5651 return dot; 5652 } 5653 } 5654 } 5655 5656 /* Remember where in the output section this input section goes. */ 5657 i->output_offset = dot - o->vma; 5658 5659 /* Mark how big the output section must be to contain this now. */ 5660 dot += TO_ADDR (i->size); 5661 if (!(o->flags & SEC_FIXED_SIZE)) 5662 o->size = TO_SIZE (dot - o->vma); 5663 5664 if (link_info.non_contiguous_regions) 5665 { 5666 /* Record that I was successfully assigned to O, and update 5667 its actual output section too. */ 5668 i->already_assigned = o; 5669 i->output_section = o; 5670 } 5671 } 5672 5673 return dot; 5674 } 5675 5676 struct check_sec 5677 { 5678 asection *sec; 5679 bool warned; 5680 }; 5681 5682 static int 5683 sort_sections_by_lma (const void *arg1, const void *arg2) 5684 { 5685 const asection *sec1 = ((const struct check_sec *) arg1)->sec; 5686 const asection *sec2 = ((const struct check_sec *) arg2)->sec; 5687 5688 if (sec1->lma < sec2->lma) 5689 return -1; 5690 else if (sec1->lma > sec2->lma) 5691 return 1; 5692 else if (sec1->id < sec2->id) 5693 return -1; 5694 else if (sec1->id > sec2->id) 5695 return 1; 5696 5697 return 0; 5698 } 5699 5700 static int 5701 sort_sections_by_vma (const void *arg1, const void *arg2) 5702 { 5703 const asection *sec1 = ((const struct check_sec *) arg1)->sec; 5704 const asection *sec2 = ((const struct check_sec *) arg2)->sec; 5705 5706 if (sec1->vma < sec2->vma) 5707 return -1; 5708 else if (sec1->vma > sec2->vma) 5709 return 1; 5710 else if (sec1->id < sec2->id) 5711 return -1; 5712 else if (sec1->id > sec2->id) 5713 return 1; 5714 5715 return 0; 5716 } 5717 5718 #define IS_TBSS(s) \ 5719 ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL) 5720 5721 #define IGNORE_SECTION(s) \ 5722 ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s)) 5723 5724 /* Check to see if any allocated sections overlap with other allocated 5725 sections. This can happen if a linker script specifies the output 5726 section addresses of the two sections. Also check whether any memory 5727 region has overflowed. */ 5728 5729 static void 5730 lang_check_section_addresses (void) 5731 { 5732 asection *s, *p; 5733 struct check_sec *sections; 5734 size_t i, count; 5735 bfd_vma addr_mask; 5736 bfd_vma s_start; 5737 bfd_vma s_end; 5738 bfd_vma p_start = 0; 5739 bfd_vma p_end = 0; 5740 lang_memory_region_type *m; 5741 bool overlays; 5742 5743 /* Detect address space overflow on allocated sections. */ 5744 addr_mask = ((bfd_vma) 1 << 5745 (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1; 5746 addr_mask = (addr_mask << 1) + 1; 5747 for (s = link_info.output_bfd->sections; s != NULL; s = s->next) 5748 if ((s->flags & SEC_ALLOC) != 0) 5749 { 5750 s_end = (s->vma + s->size) & addr_mask; 5751 if (s_end != 0 && s_end < (s->vma & addr_mask)) 5752 einfo (_("%X%P: section %s VMA wraps around address space\n"), 5753 s->name); 5754 else 5755 { 5756 s_end = (s->lma + s->size) & addr_mask; 5757 if (s_end != 0 && s_end < (s->lma & addr_mask)) 5758 einfo (_("%X%P: section %s LMA wraps around address space\n"), 5759 s->name); 5760 } 5761 } 5762 5763 if (bfd_count_sections (link_info.output_bfd) <= 1) 5764 return; 5765 5766 count = bfd_count_sections (link_info.output_bfd); 5767 sections = XNEWVEC (struct check_sec, count); 5768 5769 /* Scan all sections in the output list. */ 5770 count = 0; 5771 for (s = link_info.output_bfd->sections; s != NULL; s = s->next) 5772 { 5773 if (IGNORE_SECTION (s) 5774 || s->size == 0) 5775 continue; 5776 5777 sections[count].sec = s; 5778 sections[count].warned = false; 5779 count++; 5780 } 5781 5782 if (count <= 1) 5783 { 5784 free (sections); 5785 return; 5786 } 5787 5788 qsort (sections, count, sizeof (*sections), sort_sections_by_lma); 5789 5790 /* First check section LMAs. There should be no overlap of LMAs on 5791 loadable sections, even with overlays. */ 5792 for (p = NULL, i = 0; i < count; i++) 5793 { 5794 s = sections[i].sec; 5795 init_opb (s); 5796 if ((s->flags & SEC_LOAD) != 0) 5797 { 5798 s_start = s->lma; 5799 s_end = s_start + TO_ADDR (s->size) - 1; 5800 5801 /* Look for an overlap. We have sorted sections by lma, so 5802 we know that s_start >= p_start. Besides the obvious 5803 case of overlap when the current section starts before 5804 the previous one ends, we also must have overlap if the 5805 previous section wraps around the address space. */ 5806 if (p != NULL 5807 && (s_start <= p_end 5808 || p_end < p_start)) 5809 { 5810 einfo (_("%X%P: section %s LMA [%V,%V]" 5811 " overlaps section %s LMA [%V,%V]\n"), 5812 s->name, s_start, s_end, p->name, p_start, p_end); 5813 sections[i].warned = true; 5814 } 5815 p = s; 5816 p_start = s_start; 5817 p_end = s_end; 5818 } 5819 } 5820 5821 /* If any non-zero size allocated section (excluding tbss) starts at 5822 exactly the same VMA as another such section, then we have 5823 overlays. Overlays generated by the OVERLAY keyword will have 5824 this property. It is possible to intentionally generate overlays 5825 that fail this test, but it would be unusual. */ 5826 qsort (sections, count, sizeof (*sections), sort_sections_by_vma); 5827 overlays = false; 5828 p_start = sections[0].sec->vma; 5829 for (i = 1; i < count; i++) 5830 { 5831 s_start = sections[i].sec->vma; 5832 if (p_start == s_start) 5833 { 5834 overlays = true; 5835 break; 5836 } 5837 p_start = s_start; 5838 } 5839 5840 /* Now check section VMAs if no overlays were detected. */ 5841 if (!overlays) 5842 { 5843 for (p = NULL, i = 0; i < count; i++) 5844 { 5845 s = sections[i].sec; 5846 init_opb (s); 5847 s_start = s->vma; 5848 s_end = s_start + TO_ADDR (s->size) - 1; 5849 5850 if (p != NULL 5851 && !sections[i].warned 5852 && (s_start <= p_end 5853 || p_end < p_start)) 5854 einfo (_("%X%P: section %s VMA [%V,%V]" 5855 " overlaps section %s VMA [%V,%V]\n"), 5856 s->name, s_start, s_end, p->name, p_start, p_end); 5857 p = s; 5858 p_start = s_start; 5859 p_end = s_end; 5860 } 5861 } 5862 5863 free (sections); 5864 5865 /* If any memory region has overflowed, report by how much. 5866 We do not issue this diagnostic for regions that had sections 5867 explicitly placed outside their bounds; os_region_check's 5868 diagnostics are adequate for that case. 5869 5870 FIXME: It is conceivable that m->current - (m->origin + m->length) 5871 might overflow a 32-bit integer. There is, alas, no way to print 5872 a bfd_vma quantity in decimal. */ 5873 for (m = lang_memory_region_list; m; m = m->next) 5874 if (m->had_full_message) 5875 { 5876 unsigned long over = m->current - (m->origin + m->length); 5877 einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n", 5878 "%X%P: region `%s' overflowed by %lu bytes\n", 5879 over), 5880 m->name_list.name, over); 5881 } 5882 } 5883 5884 /* Make sure the new address is within the region. We explicitly permit the 5885 current address to be at the exact end of the region when the address is 5886 non-zero, in case the region is at the end of addressable memory and the 5887 calculation wraps around. */ 5888 5889 static void 5890 os_region_check (lang_output_section_statement_type *os, 5891 lang_memory_region_type *region, 5892 etree_type *tree, 5893 bfd_vma rbase) 5894 { 5895 if ((region->current < region->origin 5896 || (region->current - region->origin > region->length)) 5897 && ((region->current != region->origin + region->length) 5898 || rbase == 0)) 5899 { 5900 if (tree != NULL) 5901 { 5902 einfo (_("%X%P: address 0x%v of %pB section `%s'" 5903 " is not within region `%s'\n"), 5904 region->current, 5905 os->bfd_section->owner, 5906 os->bfd_section->name, 5907 region->name_list.name); 5908 } 5909 else if (!region->had_full_message) 5910 { 5911 region->had_full_message = true; 5912 5913 einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"), 5914 os->bfd_section->owner, 5915 os->bfd_section->name, 5916 region->name_list.name); 5917 } 5918 } 5919 } 5920 5921 static void 5922 ldlang_check_relro_region (lang_statement_union_type *s) 5923 { 5924 seg_align_type *seg = &expld.dataseg; 5925 5926 if (seg->relro == exp_seg_relro_start) 5927 { 5928 if (!seg->relro_start_stat) 5929 seg->relro_start_stat = s; 5930 else 5931 { 5932 ASSERT (seg->relro_start_stat == s); 5933 } 5934 } 5935 else if (seg->relro == exp_seg_relro_end) 5936 { 5937 if (!seg->relro_end_stat) 5938 seg->relro_end_stat = s; 5939 else 5940 { 5941 ASSERT (seg->relro_end_stat == s); 5942 } 5943 } 5944 } 5945 5946 /* Set the sizes for all the output sections. */ 5947 5948 static bfd_vma 5949 lang_size_sections_1 5950 (lang_statement_union_type **prev, 5951 lang_output_section_statement_type *current_os, 5952 fill_type *fill, 5953 bfd_vma dot, 5954 bool *relax, 5955 bool check_regions) 5956 { 5957 lang_statement_union_type *s; 5958 lang_statement_union_type *prev_s = NULL; 5959 bool removed_prev_s = false; 5960 lang_output_section_statement_type *os = current_os; 5961 5962 /* Size up the sections from their constituent parts. */ 5963 for (s = *prev; s != NULL; prev_s = s, s = s->header.next) 5964 { 5965 bool removed = false; 5966 5967 switch (s->header.type) 5968 { 5969 case lang_output_section_statement_enum: 5970 { 5971 bfd_vma newdot, after, dotdelta; 5972 lang_memory_region_type *r; 5973 int section_alignment = 0; 5974 5975 os = &s->output_section_statement; 5976 init_opb (os->bfd_section); 5977 if (os->constraint == -1) 5978 break; 5979 5980 /* FIXME: We shouldn't need to zero section vmas for ld -r 5981 here, in lang_insert_orphan, or in the default linker scripts. 5982 This is covering for coff backend linker bugs. See PR6945. */ 5983 if (os->addr_tree == NULL 5984 && bfd_link_relocatable (&link_info) 5985 && (bfd_get_flavour (link_info.output_bfd) 5986 == bfd_target_coff_flavour)) 5987 os->addr_tree = exp_intop (0); 5988 if (os->addr_tree != NULL) 5989 { 5990 exp_fold_tree (os->addr_tree, os, bfd_abs_section_ptr, &dot); 5991 5992 if (expld.result.valid_p) 5993 { 5994 dot = expld.result.value; 5995 if (expld.result.section != NULL) 5996 dot += expld.result.section->vma; 5997 } 5998 else if (expld.phase != lang_mark_phase_enum) 5999 fatal (_("%P:%pS: non constant or forward reference" 6000 " address expression for section %s\n"), 6001 os->addr_tree, os->name); 6002 } 6003 6004 if (os->bfd_section == NULL) 6005 /* This section was removed or never actually created. */ 6006 break; 6007 6008 /* If this is a COFF shared library section, use the size and 6009 address from the input section. FIXME: This is COFF 6010 specific; it would be cleaner if there were some other way 6011 to do this, but nothing simple comes to mind. */ 6012 if (((bfd_get_flavour (link_info.output_bfd) 6013 == bfd_target_ecoff_flavour) 6014 || (bfd_get_flavour (link_info.output_bfd) 6015 == bfd_target_coff_flavour)) 6016 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) 6017 { 6018 asection *input; 6019 6020 if (os->children.head == NULL 6021 || os->children.head->header.next != NULL 6022 || (os->children.head->header.type 6023 != lang_input_section_enum)) 6024 einfo (_("%X%P: internal error on COFF shared library" 6025 " section %s\n"), os->name); 6026 6027 input = os->children.head->input_section.section; 6028 bfd_set_section_vma (os->bfd_section, 6029 bfd_section_vma (input)); 6030 if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) 6031 os->bfd_section->size = input->size; 6032 break; 6033 } 6034 6035 newdot = dot; 6036 dotdelta = 0; 6037 if (bfd_is_abs_section (os->bfd_section)) 6038 { 6039 /* No matter what happens, an abs section starts at zero. */ 6040 ASSERT (os->bfd_section->vma == 0); 6041 } 6042 else 6043 { 6044 if (os->addr_tree == NULL) 6045 { 6046 /* No address specified for this section, get one 6047 from the region specification. */ 6048 if (os->region == NULL 6049 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)) 6050 && os->region->name_list.name[0] == '*' 6051 && strcmp (os->region->name_list.name, 6052 DEFAULT_MEMORY_REGION) == 0)) 6053 { 6054 os->region = lang_memory_default (os->bfd_section); 6055 } 6056 6057 /* If a loadable section is using the default memory 6058 region, and some non default memory regions were 6059 defined, issue an error message. */ 6060 if (!os->ignored 6061 && !IGNORE_SECTION (os->bfd_section) 6062 && !bfd_link_relocatable (&link_info) 6063 && check_regions 6064 && strcmp (os->region->name_list.name, 6065 DEFAULT_MEMORY_REGION) == 0 6066 && lang_memory_region_list != NULL 6067 && (strcmp (lang_memory_region_list->name_list.name, 6068 DEFAULT_MEMORY_REGION) != 0 6069 || lang_memory_region_list->next != NULL) 6070 && lang_sizing_iteration == 1) 6071 { 6072 /* By default this is an error rather than just a 6073 warning because if we allocate the section to the 6074 default memory region we can end up creating an 6075 excessively large binary, or even seg faulting when 6076 attempting to perform a negative seek. See 6077 sources.redhat.com/ml/binutils/2003-04/msg00423.html 6078 for an example of this. This behaviour can be 6079 overridden by the using the --no-check-sections 6080 switch. */ 6081 if (command_line.check_section_addresses) 6082 fatal (_("%P: error: no memory region specified" 6083 " for loadable section `%s'\n"), 6084 bfd_section_name (os->bfd_section)); 6085 else 6086 einfo (_("%P: warning: no memory region specified" 6087 " for loadable section `%s'\n"), 6088 bfd_section_name (os->bfd_section)); 6089 } 6090 6091 newdot = os->region->current; 6092 section_alignment = os->bfd_section->alignment_power; 6093 } 6094 else 6095 section_alignment = exp_get_power (os->section_alignment, os, 6096 "section alignment"); 6097 6098 /* Align to what the section needs. */ 6099 if (section_alignment > 0) 6100 { 6101 bfd_vma savedot = newdot; 6102 bfd_vma diff = 0; 6103 6104 newdot = align_power (newdot, section_alignment); 6105 dotdelta = newdot - savedot; 6106 6107 if (lang_sizing_iteration == 1) 6108 diff = dotdelta; 6109 else if (lang_sizing_iteration > 1) 6110 { 6111 /* Only report adjustments that would change 6112 alignment from what we have already reported. */ 6113 diff = newdot - os->bfd_section->vma; 6114 if (!(diff & (((bfd_vma) 1 << section_alignment) - 1))) 6115 diff = 0; 6116 } 6117 if (diff != 0 6118 && (config.warn_section_align 6119 || os->addr_tree != NULL)) 6120 einfo (_("%P: warning: " 6121 "start of section %s changed by %ld\n"), 6122 os->name, (long) diff); 6123 } 6124 6125 bfd_set_section_vma (os->bfd_section, newdot); 6126 6127 os->bfd_section->output_offset = 0; 6128 } 6129 6130 lang_size_sections_1 (&os->children.head, os, 6131 os->fill, newdot, relax, check_regions); 6132 6133 os->processed_vma = true; 6134 6135 if (bfd_is_abs_section (os->bfd_section) || os->ignored) 6136 /* Except for some special linker created sections, 6137 no output section should change from zero size 6138 after strip_excluded_output_sections. A non-zero 6139 size on an ignored section indicates that some 6140 input section was not sized early enough. */ 6141 ASSERT (os->bfd_section->size == 0); 6142 else 6143 { 6144 dot = os->bfd_section->vma; 6145 6146 /* Put the section within the requested block size, or 6147 align at the block boundary. */ 6148 after = ((dot 6149 + TO_ADDR (os->bfd_section->size) 6150 + os->block_value - 1) 6151 & - (bfd_vma) os->block_value); 6152 6153 if (!(os->bfd_section->flags & SEC_FIXED_SIZE)) 6154 os->bfd_section->size = TO_SIZE (after 6155 - os->bfd_section->vma); 6156 } 6157 6158 /* Set section lma. */ 6159 r = os->region; 6160 if (r == NULL) 6161 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false); 6162 6163 if (os->load_base) 6164 { 6165 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base"); 6166 os->bfd_section->lma = lma; 6167 } 6168 else if (os->lma_region != NULL) 6169 { 6170 bfd_vma lma = os->lma_region->current; 6171 6172 if (os->align_lma_with_input) 6173 lma += dotdelta; 6174 else 6175 { 6176 /* When LMA_REGION is the same as REGION, align the LMA 6177 as we did for the VMA, possibly including alignment 6178 from the bfd section. If a different region, then 6179 only align according to the value in the output 6180 statement. */ 6181 if (os->lma_region != os->region) 6182 section_alignment = exp_get_power (os->section_alignment, 6183 os, 6184 "section alignment"); 6185 if (section_alignment > 0) 6186 lma = align_power (lma, section_alignment); 6187 } 6188 os->bfd_section->lma = lma; 6189 } 6190 else if (r->last_os != NULL 6191 && (os->bfd_section->flags & SEC_ALLOC) != 0) 6192 { 6193 bfd_vma lma; 6194 asection *last; 6195 6196 last = r->last_os->output_section_statement.bfd_section; 6197 6198 /* A backwards move of dot should be accompanied by 6199 an explicit assignment to the section LMA (ie. 6200 os->load_base set) because backwards moves can 6201 create overlapping LMAs. */ 6202 if (dot < last->vma 6203 && os->bfd_section->size != 0 6204 && dot + TO_ADDR (os->bfd_section->size) <= last->vma) 6205 { 6206 /* If dot moved backwards then leave lma equal to 6207 vma. This is the old default lma, which might 6208 just happen to work when the backwards move is 6209 sufficiently large. Nag if this changes anything, 6210 so people can fix their linker scripts. */ 6211 6212 if (last->vma != last->lma) 6213 einfo (_("%P: warning: dot moved backwards " 6214 "before `%s'\n"), os->name); 6215 } 6216 else 6217 { 6218 /* If this is an overlay, set the current lma to that 6219 at the end of the previous section. */ 6220 if (os->sectype == overlay_section) 6221 lma = last->lma + TO_ADDR (last->size); 6222 6223 /* Otherwise, keep the same lma to vma relationship 6224 as the previous section. */ 6225 else 6226 lma = os->bfd_section->vma + last->lma - last->vma; 6227 6228 if (section_alignment > 0) 6229 lma = align_power (lma, section_alignment); 6230 os->bfd_section->lma = lma; 6231 } 6232 } 6233 os->processed_lma = true; 6234 6235 /* Keep track of normal sections using the default 6236 lma region. We use this to set the lma for 6237 following sections. Overlays or other linker 6238 script assignment to lma might mean that the 6239 default lma == vma is incorrect. 6240 To avoid warnings about dot moving backwards when using 6241 -Ttext, don't start tracking sections until we find one 6242 of non-zero size or with lma set differently to vma. 6243 Do this tracking before we short-cut the loop so that we 6244 track changes for the case where the section size is zero, 6245 but the lma is set differently to the vma. This is 6246 important, if an orphan section is placed after an 6247 otherwise empty output section that has an explicit lma 6248 set, we want that lma reflected in the orphans lma. */ 6249 if (((!IGNORE_SECTION (os->bfd_section) 6250 && (os->bfd_section->size != 0 6251 || (r->last_os == NULL 6252 && os->bfd_section->vma != os->bfd_section->lma) 6253 || (r->last_os != NULL 6254 && dot >= (r->last_os->output_section_statement 6255 .bfd_section->vma)))) 6256 || os->sectype == first_overlay_section) 6257 && os->lma_region == NULL 6258 && !bfd_link_relocatable (&link_info)) 6259 r->last_os = s; 6260 6261 if (bfd_is_abs_section (os->bfd_section) || os->ignored) 6262 break; 6263 6264 /* .tbss sections effectively have zero size. */ 6265 if (!IS_TBSS (os->bfd_section) 6266 || bfd_link_relocatable (&link_info)) 6267 dotdelta = TO_ADDR (os->bfd_section->size); 6268 else 6269 dotdelta = 0; 6270 dot += dotdelta; 6271 6272 if (os->update_dot_tree != 0) 6273 exp_fold_tree (os->update_dot_tree, os, bfd_abs_section_ptr, &dot); 6274 6275 /* Update dot in the region ? 6276 We only do this if the section is going to be allocated, 6277 since unallocated sections do not contribute to the region's 6278 overall size in memory. */ 6279 if (os->region != NULL 6280 && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))) 6281 { 6282 os->region->current = dot; 6283 6284 if (check_regions) 6285 /* Make sure the new address is within the region. */ 6286 os_region_check (os, os->region, os->addr_tree, 6287 os->bfd_section->vma); 6288 6289 if (os->lma_region != NULL && os->lma_region != os->region 6290 && ((os->bfd_section->flags & SEC_LOAD) 6291 || os->align_lma_with_input)) 6292 { 6293 os->lma_region->current = os->bfd_section->lma + dotdelta; 6294 6295 if (check_regions) 6296 os_region_check (os, os->lma_region, NULL, 6297 os->bfd_section->lma); 6298 } 6299 } 6300 } 6301 break; 6302 6303 case lang_constructors_statement_enum: 6304 dot = lang_size_sections_1 (&constructor_list.head, current_os, 6305 fill, dot, relax, check_regions); 6306 break; 6307 6308 case lang_data_statement_enum: 6309 { 6310 unsigned int size = 0; 6311 6312 s->data_statement.output_offset = dot - current_os->bfd_section->vma; 6313 s->data_statement.output_section = current_os->bfd_section; 6314 6315 /* We might refer to provided symbols in the expression, and 6316 need to mark them as needed. */ 6317 exp_fold_tree (s->data_statement.exp, os, 6318 bfd_abs_section_ptr, &dot); 6319 6320 switch (s->data_statement.type) 6321 { 6322 default: 6323 abort (); 6324 case QUAD: 6325 case SQUAD: 6326 size = QUAD_SIZE; 6327 break; 6328 case LONG: 6329 size = LONG_SIZE; 6330 break; 6331 case SHORT: 6332 size = SHORT_SIZE; 6333 break; 6334 case BYTE: 6335 size = BYTE_SIZE; 6336 break; 6337 } 6338 if (size < TO_SIZE ((unsigned) 1)) 6339 size = TO_SIZE ((unsigned) 1); 6340 dot += TO_ADDR (size); 6341 if (!(current_os->bfd_section->flags & SEC_FIXED_SIZE)) 6342 current_os->bfd_section->size 6343 = TO_SIZE (dot - current_os->bfd_section->vma); 6344 6345 } 6346 break; 6347 6348 case lang_reloc_statement_enum: 6349 { 6350 int size; 6351 6352 s->reloc_statement.output_offset 6353 = dot - current_os->bfd_section->vma; 6354 s->reloc_statement.output_section 6355 = current_os->bfd_section; 6356 size = bfd_get_reloc_size (s->reloc_statement.howto); 6357 dot += TO_ADDR (size); 6358 if (!(current_os->bfd_section->flags & SEC_FIXED_SIZE)) 6359 current_os->bfd_section->size 6360 = TO_SIZE (dot - current_os->bfd_section->vma); 6361 } 6362 break; 6363 6364 case lang_wild_statement_enum: 6365 dot = lang_size_sections_1 (&s->wild_statement.children.head, 6366 current_os, fill, dot, relax, 6367 check_regions); 6368 break; 6369 6370 case lang_object_symbols_statement_enum: 6371 link_info.create_object_symbols_section = current_os->bfd_section; 6372 current_os->bfd_section->flags |= SEC_KEEP; 6373 break; 6374 6375 case lang_output_statement_enum: 6376 case lang_target_statement_enum: 6377 break; 6378 6379 case lang_input_section_enum: 6380 { 6381 asection *i; 6382 6383 i = s->input_section.section; 6384 if (relax) 6385 { 6386 bool again; 6387 6388 if (!bfd_relax_section (i->owner, i, &link_info, &again)) 6389 fatal (_("%P: can't relax section: %E\n")); 6390 if (again) 6391 *relax = true; 6392 } 6393 dot = size_input_section (prev, current_os, fill, &removed, dot); 6394 } 6395 break; 6396 6397 case lang_input_statement_enum: 6398 break; 6399 6400 case lang_fill_statement_enum: 6401 s->fill_statement.output_section = current_os->bfd_section; 6402 6403 fill = s->fill_statement.fill; 6404 break; 6405 6406 case lang_assignment_statement_enum: 6407 { 6408 bfd_vma newdot = dot; 6409 etree_type *tree = s->assignment_statement.exp; 6410 6411 expld.dataseg.relro = exp_seg_relro_none; 6412 6413 exp_fold_tree (tree, os, current_os->bfd_section, &newdot); 6414 6415 ldlang_check_relro_region (s); 6416 6417 expld.dataseg.relro = exp_seg_relro_none; 6418 6419 /* This symbol may be relative to this section. */ 6420 if ((tree->type.node_class == etree_provided 6421 || tree->type.node_class == etree_assign) 6422 && (tree->assign.dst [0] != '.' 6423 || tree->assign.dst [1] != '\0')) 6424 current_os->update_dot = 1; 6425 6426 if (!current_os->ignored) 6427 { 6428 if (current_os == abs_output_section) 6429 { 6430 /* If we don't have an output section, then just adjust 6431 the default memory address. */ 6432 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, 6433 false)->current = newdot; 6434 } 6435 else if (newdot != dot) 6436 { 6437 /* Insert a pad after this statement. We can't 6438 put the pad before when relaxing, in case the 6439 assignment references dot. */ 6440 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot), 6441 current_os->bfd_section, dot); 6442 6443 /* Don't neuter the pad below when relaxing. */ 6444 s = s->header.next; 6445 6446 /* If dot is advanced, this implies that the section 6447 should have space allocated to it, unless the 6448 user has explicitly stated that the section 6449 should not be allocated. */ 6450 if (current_os->sectype != noalloc_section 6451 && (current_os->sectype != noload_section 6452 || (bfd_get_flavour (link_info.output_bfd) 6453 == bfd_target_elf_flavour))) 6454 current_os->bfd_section->flags |= SEC_ALLOC; 6455 } 6456 dot = newdot; 6457 } 6458 } 6459 break; 6460 6461 case lang_padding_statement_enum: 6462 /* If this is the first time lang_size_sections is called, 6463 we won't have any padding statements. If this is the 6464 second or later passes when relaxing, we should allow 6465 padding to shrink. If padding is needed on this pass, it 6466 will be added back in. */ 6467 s->padding_statement.size = 0; 6468 6469 /* Make sure output_offset is valid. If relaxation shrinks 6470 the section and this pad isn't needed, it's possible to 6471 have output_offset larger than the final size of the 6472 section. bfd_set_section_contents will complain even for 6473 a pad size of zero. */ 6474 s->padding_statement.output_offset 6475 = dot - current_os->bfd_section->vma; 6476 break; 6477 6478 case lang_group_statement_enum: 6479 dot = lang_size_sections_1 (&s->group_statement.children.head, 6480 current_os, fill, dot, relax, 6481 check_regions); 6482 break; 6483 6484 case lang_insert_statement_enum: 6485 break; 6486 6487 /* We can only get here when relaxing is turned on. */ 6488 case lang_address_statement_enum: 6489 break; 6490 6491 default: 6492 FAIL (); 6493 break; 6494 } 6495 6496 /* If an input section doesn't fit in the current output 6497 section, remove it from the list. Handle the case where we 6498 have to remove an input_section statement here: there is a 6499 special case to remove the first element of the list. */ 6500 if (link_info.non_contiguous_regions && removed) 6501 { 6502 /* If we removed the first element during the previous 6503 iteration, override the loop assignment of prev_s. */ 6504 if (removed_prev_s) 6505 prev_s = NULL; 6506 6507 if (prev_s) 6508 { 6509 /* If there was a real previous input section, just skip 6510 the current one. */ 6511 prev_s->header.next=s->header.next; 6512 s = prev_s; 6513 removed_prev_s = false; 6514 } 6515 else 6516 { 6517 /* Remove the first input section of the list. */ 6518 *prev = s->header.next; 6519 removed_prev_s = true; 6520 } 6521 6522 /* Move to next element, unless we removed the head of the 6523 list. */ 6524 if (!removed_prev_s) 6525 prev = &s->header.next; 6526 } 6527 else 6528 { 6529 prev = &s->header.next; 6530 removed_prev_s = false; 6531 } 6532 } 6533 return dot; 6534 } 6535 6536 /* Callback routine that is used in _bfd_elf_map_sections_to_segments. 6537 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that 6538 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different 6539 segments. We are allowed an opportunity to override this decision. */ 6540 6541 bool 6542 ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED, 6543 bfd *abfd ATTRIBUTE_UNUSED, 6544 asection *current_section, 6545 asection *previous_section, 6546 bool new_segment) 6547 { 6548 lang_output_section_statement_type *cur; 6549 lang_output_section_statement_type *prev; 6550 6551 /* The checks below are only necessary when the BFD library has decided 6552 that the two sections ought to be placed into the same segment. */ 6553 if (new_segment) 6554 return true; 6555 6556 /* Paranoia checks. */ 6557 if (current_section == NULL || previous_section == NULL) 6558 return new_segment; 6559 6560 /* If this flag is set, the target never wants code and non-code 6561 sections comingled in the same segment. */ 6562 if (config.separate_code 6563 && ((current_section->flags ^ previous_section->flags) & SEC_CODE)) 6564 return true; 6565 6566 /* Find the memory regions associated with the two sections. 6567 We call lang_output_section_find() here rather than scanning the list 6568 of output sections looking for a matching section pointer because if 6569 we have a large number of sections then a hash lookup is faster. */ 6570 cur = lang_output_section_find (current_section->name); 6571 prev = lang_output_section_find (previous_section->name); 6572 6573 /* More paranoia. */ 6574 if (cur == NULL || prev == NULL) 6575 return new_segment; 6576 6577 /* If the regions are different then force the sections to live in 6578 different segments. See the email thread starting at the following 6579 URL for the reasons why this is necessary: 6580 http://sourceware.org/ml/binutils/2007-02/msg00216.html */ 6581 return cur->region != prev->region; 6582 } 6583 6584 void 6585 one_lang_size_sections_pass (bool *relax, bool check_regions) 6586 { 6587 lang_statement_iteration++; 6588 if (expld.phase != lang_mark_phase_enum) 6589 lang_sizing_iteration++; 6590 lang_size_sections_1 (&statement_list.head, abs_output_section, 6591 0, 0, relax, check_regions); 6592 } 6593 6594 static bool 6595 lang_size_segment (void) 6596 { 6597 /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether 6598 a page could be saved in the data segment. */ 6599 seg_align_type *seg = &expld.dataseg; 6600 bfd_vma first, last; 6601 6602 first = -seg->base & (seg->commonpagesize - 1); 6603 last = seg->end & (seg->commonpagesize - 1); 6604 if (first && last 6605 && ((seg->base & ~(seg->commonpagesize - 1)) 6606 != (seg->end & ~(seg->commonpagesize - 1))) 6607 && first + last <= seg->commonpagesize) 6608 { 6609 seg->phase = exp_seg_adjust; 6610 return true; 6611 } 6612 6613 seg->phase = exp_seg_done; 6614 return false; 6615 } 6616 6617 static bfd_vma 6618 lang_size_relro_segment_1 (void) 6619 { 6620 seg_align_type *seg = &expld.dataseg; 6621 bfd_vma relro_end, desired_end; 6622 asection *sec; 6623 6624 /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */ 6625 relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize; 6626 6627 /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */ 6628 desired_end = relro_end - seg->relro_offset; 6629 6630 /* For sections in the relro segment.. */ 6631 for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev) 6632 if ((sec->flags & SEC_ALLOC) != 0 6633 && sec->vma >= seg->base 6634 && sec->vma < seg->relro_end - seg->relro_offset) 6635 { 6636 /* Where do we want to put this section so that it ends as 6637 desired? */ 6638 bfd_vma start, end, bump; 6639 6640 end = start = sec->vma; 6641 if (!IS_TBSS (sec)) 6642 end += TO_ADDR (sec->size); 6643 bump = desired_end - end; 6644 /* We'd like to increase START by BUMP, but we must heed 6645 alignment so the increase might be less than optimum. */ 6646 start += bump; 6647 start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1); 6648 /* This is now the desired end for the previous section. */ 6649 desired_end = start; 6650 } 6651 6652 seg->phase = exp_seg_relro_adjust; 6653 ASSERT (desired_end >= seg->base); 6654 seg->base = desired_end; 6655 return relro_end; 6656 } 6657 6658 static bool 6659 lang_size_relro_segment (bool *relax, bool check_regions) 6660 { 6661 bool do_reset = false; 6662 6663 if (link_info.relro && expld.dataseg.relro_end) 6664 { 6665 bfd_vma data_initial_base = expld.dataseg.base; 6666 bfd_vma data_relro_end = lang_size_relro_segment_1 (); 6667 6668 lang_reset_memory_regions (); 6669 one_lang_size_sections_pass (relax, check_regions); 6670 6671 /* Assignments to dot, or to output section address in a user 6672 script have increased padding over the original. Revert. */ 6673 if (expld.dataseg.relro_end > data_relro_end) 6674 { 6675 expld.dataseg.base = data_initial_base; 6676 do_reset = true; 6677 } 6678 } 6679 else if (lang_size_segment ()) 6680 do_reset = true; 6681 6682 return do_reset; 6683 } 6684 6685 void 6686 lang_size_sections (bool *relax, bool check_regions) 6687 { 6688 expld.phase = lang_allocating_phase_enum; 6689 expld.dataseg.phase = exp_seg_none; 6690 6691 one_lang_size_sections_pass (relax, check_regions); 6692 6693 if (expld.dataseg.phase != exp_seg_end_seen) 6694 expld.dataseg.phase = exp_seg_done; 6695 6696 if (expld.dataseg.phase == exp_seg_end_seen) 6697 { 6698 bool do_reset 6699 = lang_size_relro_segment (relax, check_regions); 6700 6701 if (do_reset) 6702 { 6703 lang_reset_memory_regions (); 6704 one_lang_size_sections_pass (relax, check_regions); 6705 } 6706 6707 if (link_info.relro && expld.dataseg.relro_end) 6708 { 6709 link_info.relro_start = expld.dataseg.base; 6710 link_info.relro_end = expld.dataseg.relro_end; 6711 } 6712 } 6713 } 6714 6715 static lang_output_section_statement_type *current_section; 6716 static lang_assignment_statement_type *current_assign; 6717 static bool prefer_next_section; 6718 6719 /* Worker function for lang_do_assignments. Recursiveness goes here. */ 6720 6721 static bfd_vma 6722 lang_do_assignments_1 (lang_statement_union_type *s, 6723 lang_output_section_statement_type *current_os, 6724 fill_type *fill, 6725 bfd_vma dot, 6726 bool *found_end) 6727 { 6728 lang_output_section_statement_type *os = current_os; 6729 6730 for (; s != NULL; s = s->header.next) 6731 { 6732 switch (s->header.type) 6733 { 6734 case lang_constructors_statement_enum: 6735 dot = lang_do_assignments_1 (constructor_list.head, 6736 current_os, fill, dot, found_end); 6737 break; 6738 6739 case lang_output_section_statement_enum: 6740 { 6741 bfd_vma newdot; 6742 6743 os = &s->output_section_statement; 6744 os->after_end = *found_end; 6745 init_opb (os->bfd_section); 6746 newdot = dot; 6747 if (os->bfd_section != NULL) 6748 { 6749 if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0) 6750 { 6751 current_section = os; 6752 prefer_next_section = false; 6753 } 6754 newdot = os->bfd_section->vma; 6755 } 6756 newdot = lang_do_assignments_1 (os->children.head, 6757 os, os->fill, newdot, found_end); 6758 if (!os->ignored) 6759 { 6760 if (os->bfd_section != NULL) 6761 { 6762 newdot = os->bfd_section->vma; 6763 6764 /* .tbss sections effectively have zero size. */ 6765 if (!IS_TBSS (os->bfd_section) 6766 || bfd_link_relocatable (&link_info)) 6767 newdot += TO_ADDR (os->bfd_section->size); 6768 6769 if (os->update_dot_tree != NULL) 6770 exp_fold_tree (os->update_dot_tree, os, 6771 bfd_abs_section_ptr, &newdot); 6772 } 6773 dot = newdot; 6774 } 6775 } 6776 break; 6777 6778 case lang_wild_statement_enum: 6779 6780 dot = lang_do_assignments_1 (s->wild_statement.children.head, 6781 current_os, fill, dot, found_end); 6782 break; 6783 6784 case lang_object_symbols_statement_enum: 6785 case lang_output_statement_enum: 6786 case lang_target_statement_enum: 6787 break; 6788 6789 case lang_data_statement_enum: 6790 exp_fold_tree (s->data_statement.exp, os, bfd_abs_section_ptr, &dot); 6791 if (expld.result.valid_p) 6792 { 6793 s->data_statement.value = expld.result.value; 6794 if (expld.result.section != NULL) 6795 s->data_statement.value += expld.result.section->vma; 6796 } 6797 else if (expld.phase == lang_final_phase_enum) 6798 fatal (_("%P: invalid data statement\n")); 6799 { 6800 unsigned int size; 6801 switch (s->data_statement.type) 6802 { 6803 default: 6804 abort (); 6805 case QUAD: 6806 case SQUAD: 6807 size = QUAD_SIZE; 6808 break; 6809 case LONG: 6810 size = LONG_SIZE; 6811 break; 6812 case SHORT: 6813 size = SHORT_SIZE; 6814 break; 6815 case BYTE: 6816 size = BYTE_SIZE; 6817 break; 6818 } 6819 if (size < TO_SIZE ((unsigned) 1)) 6820 size = TO_SIZE ((unsigned) 1); 6821 dot += TO_ADDR (size); 6822 } 6823 break; 6824 6825 case lang_reloc_statement_enum: 6826 exp_fold_tree (s->reloc_statement.addend_exp, os, 6827 bfd_abs_section_ptr, &dot); 6828 if (expld.result.valid_p) 6829 s->reloc_statement.addend_value = expld.result.value; 6830 else if (expld.phase == lang_final_phase_enum) 6831 fatal (_("%P: invalid reloc statement\n")); 6832 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto)); 6833 break; 6834 6835 case lang_input_section_enum: 6836 { 6837 asection *in = s->input_section.section; 6838 6839 if ((in->flags & SEC_EXCLUDE) == 0) 6840 dot += TO_ADDR (in->size); 6841 } 6842 break; 6843 6844 case lang_input_statement_enum: 6845 break; 6846 6847 case lang_fill_statement_enum: 6848 fill = s->fill_statement.fill; 6849 break; 6850 6851 case lang_assignment_statement_enum: 6852 current_assign = &s->assignment_statement; 6853 if (current_assign->exp->type.node_class != etree_assert) 6854 { 6855 const char *p = current_assign->exp->assign.dst; 6856 6857 if (current_os == abs_output_section && p[0] == '.' && p[1] == 0) 6858 prefer_next_section = true; 6859 6860 while (*p == '_') 6861 ++p; 6862 if (strcmp (p, "end") == 0) 6863 *found_end = true; 6864 } 6865 exp_fold_tree (s->assignment_statement.exp, os, 6866 (current_os->bfd_section != NULL 6867 ? current_os->bfd_section : bfd_und_section_ptr), 6868 &dot); 6869 break; 6870 6871 case lang_padding_statement_enum: 6872 dot += TO_ADDR (s->padding_statement.size); 6873 break; 6874 6875 case lang_group_statement_enum: 6876 dot = lang_do_assignments_1 (s->group_statement.children.head, 6877 current_os, fill, dot, found_end); 6878 break; 6879 6880 case lang_insert_statement_enum: 6881 break; 6882 6883 case lang_address_statement_enum: 6884 break; 6885 6886 default: 6887 FAIL (); 6888 break; 6889 } 6890 } 6891 return dot; 6892 } 6893 6894 void 6895 lang_do_assignments (lang_phase_type phase) 6896 { 6897 bool found_end = false; 6898 6899 current_section = NULL; 6900 prefer_next_section = false; 6901 expld.phase = phase; 6902 lang_statement_iteration++; 6903 lang_do_assignments_1 (statement_list.head, 6904 abs_output_section, NULL, 0, &found_end); 6905 } 6906 6907 /* For an assignment statement outside of an output section statement, 6908 choose the best of neighbouring output sections to use for values 6909 of "dot". */ 6910 6911 asection * 6912 section_for_dot (void) 6913 { 6914 asection *s; 6915 6916 /* Assignments belong to the previous output section, unless there 6917 has been an assignment to "dot", in which case following 6918 assignments belong to the next output section. (The assumption 6919 is that an assignment to "dot" is setting up the address for the 6920 next output section.) Except that past the assignment to "_end" 6921 we always associate with the previous section. This exception is 6922 for targets like SH that define an alloc .stack or other 6923 weirdness after non-alloc sections. */ 6924 if (current_section == NULL || prefer_next_section) 6925 { 6926 lang_statement_union_type *stmt; 6927 lang_output_section_statement_type *os; 6928 6929 for (stmt = (lang_statement_union_type *) current_assign; 6930 stmt != NULL; 6931 stmt = stmt->header.next) 6932 if (stmt->header.type == lang_output_section_statement_enum) 6933 break; 6934 6935 os = stmt ? &stmt->output_section_statement : NULL; 6936 while (os != NULL 6937 && !os->after_end 6938 && (os->bfd_section == NULL 6939 || (os->bfd_section->flags & SEC_EXCLUDE) != 0 6940 || bfd_section_removed_from_list (link_info.output_bfd, 6941 os->bfd_section))) 6942 os = os->next; 6943 6944 if (current_section == NULL || os == NULL || !os->after_end) 6945 { 6946 if (os != NULL) 6947 s = os->bfd_section; 6948 else 6949 s = link_info.output_bfd->section_last; 6950 while (s != NULL 6951 && ((s->flags & SEC_ALLOC) == 0 6952 || (s->flags & SEC_THREAD_LOCAL) != 0)) 6953 s = s->prev; 6954 if (s != NULL) 6955 return s; 6956 6957 return bfd_abs_section_ptr; 6958 } 6959 } 6960 6961 s = current_section->bfd_section; 6962 6963 /* The section may have been stripped. */ 6964 while (s != NULL 6965 && ((s->flags & SEC_EXCLUDE) != 0 6966 || (s->flags & SEC_ALLOC) == 0 6967 || (s->flags & SEC_THREAD_LOCAL) != 0 6968 || bfd_section_removed_from_list (link_info.output_bfd, s))) 6969 s = s->prev; 6970 if (s == NULL) 6971 s = link_info.output_bfd->sections; 6972 while (s != NULL 6973 && ((s->flags & SEC_ALLOC) == 0 6974 || (s->flags & SEC_THREAD_LOCAL) != 0)) 6975 s = s->next; 6976 if (s != NULL) 6977 return s; 6978 6979 return bfd_abs_section_ptr; 6980 } 6981 6982 /* Array of __start/__stop/.startof./.sizeof/ symbols. */ 6983 6984 static struct bfd_link_hash_entry **start_stop_syms; 6985 static size_t start_stop_count = 0; 6986 static size_t start_stop_alloc = 0; 6987 6988 /* Give start/stop SYMBOL for SEC a preliminary definition, and add it 6989 to start_stop_syms. */ 6990 6991 static void 6992 lang_define_start_stop (const char *symbol, asection *sec) 6993 { 6994 struct bfd_link_hash_entry *h; 6995 6996 h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec); 6997 if (h != NULL) 6998 { 6999 if (start_stop_count == start_stop_alloc) 7000 { 7001 start_stop_alloc = 2 * start_stop_alloc + 10; 7002 start_stop_syms 7003 = xrealloc (start_stop_syms, 7004 start_stop_alloc * sizeof (*start_stop_syms)); 7005 } 7006 start_stop_syms[start_stop_count++] = h; 7007 } 7008 } 7009 7010 /* Check for input sections whose names match references to 7011 __start_SECNAME or __stop_SECNAME symbols. Give the symbols 7012 preliminary definitions. */ 7013 7014 static void 7015 lang_init_start_stop (void) 7016 { 7017 bfd *abfd; 7018 asection *s; 7019 char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd); 7020 7021 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) 7022 for (s = abfd->sections; s != NULL; s = s->next) 7023 { 7024 const char *ps; 7025 const char *secname = s->name; 7026 7027 for (ps = secname; *ps != '\0'; ps++) 7028 if (!ISALNUM ((unsigned char) *ps) && *ps != '_') 7029 break; 7030 if (*ps == '\0') 7031 { 7032 char *symbol = (char *) xmalloc (10 + strlen (secname)); 7033 7034 symbol[0] = leading_char; 7035 sprintf (symbol + (leading_char != 0), "__start_%s", secname); 7036 lang_define_start_stop (symbol, s); 7037 7038 symbol[1] = leading_char; 7039 memcpy (symbol + 1 + (leading_char != 0), "__stop", 6); 7040 lang_define_start_stop (symbol + 1, s); 7041 7042 free (symbol); 7043 } 7044 } 7045 } 7046 7047 /* Iterate over start_stop_syms. */ 7048 7049 static void 7050 foreach_start_stop (void (*func) (struct bfd_link_hash_entry *)) 7051 { 7052 size_t i; 7053 7054 for (i = 0; i < start_stop_count; ++i) 7055 func (start_stop_syms[i]); 7056 } 7057 7058 /* __start and __stop symbols are only supposed to be defined by the 7059 linker for orphan sections, but we now extend that to sections that 7060 map to an output section of the same name. The symbols were 7061 defined early for --gc-sections, before we mapped input to output 7062 sections, so undo those that don't satisfy this rule. */ 7063 7064 static void 7065 undef_start_stop (struct bfd_link_hash_entry *h) 7066 { 7067 if (h->ldscript_def) 7068 return; 7069 7070 if (h->u.def.section->output_section == NULL 7071 || h->u.def.section->output_section->owner != link_info.output_bfd 7072 || strcmp (h->u.def.section->name, 7073 h->u.def.section->output_section->name) != 0) 7074 { 7075 asection *sec = bfd_get_section_by_name (link_info.output_bfd, 7076 h->u.def.section->name); 7077 if (sec != NULL) 7078 { 7079 /* When there are more than one input sections with the same 7080 section name, SECNAME, linker picks the first one to define 7081 __start_SECNAME and __stop_SECNAME symbols. When the first 7082 input section is removed by comdat group, we need to check 7083 if there is still an output section with section name 7084 SECNAME. */ 7085 asection *i; 7086 for (i = sec->map_head.s; i != NULL; i = i->map_head.s) 7087 if (strcmp (h->u.def.section->name, i->name) == 0) 7088 { 7089 h->u.def.section = i; 7090 return; 7091 } 7092 } 7093 h->type = bfd_link_hash_undefined; 7094 h->u.undef.abfd = NULL; 7095 if (is_elf_hash_table (link_info.hash)) 7096 { 7097 const struct elf_backend_data *bed; 7098 struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h; 7099 unsigned int was_forced = eh->forced_local; 7100 7101 bed = get_elf_backend_data (link_info.output_bfd); 7102 (*bed->elf_backend_hide_symbol) (&link_info, eh, true); 7103 if (!eh->ref_regular_nonweak) 7104 h->type = bfd_link_hash_undefweak; 7105 eh->def_regular = 0; 7106 eh->forced_local = was_forced; 7107 } 7108 } 7109 } 7110 7111 static void 7112 lang_undef_start_stop (void) 7113 { 7114 foreach_start_stop (undef_start_stop); 7115 } 7116 7117 /* Check for output sections whose names match references to 7118 .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols 7119 preliminary definitions. */ 7120 7121 static void 7122 lang_init_startof_sizeof (void) 7123 { 7124 asection *s; 7125 7126 for (s = link_info.output_bfd->sections; s != NULL; s = s->next) 7127 { 7128 const char *secname = s->name; 7129 char *symbol = (char *) xmalloc (10 + strlen (secname)); 7130 7131 sprintf (symbol, ".startof.%s", secname); 7132 lang_define_start_stop (symbol, s); 7133 7134 memcpy (symbol + 1, ".size", 5); 7135 lang_define_start_stop (symbol + 1, s); 7136 free (symbol); 7137 } 7138 } 7139 7140 /* Set .startof., .sizeof., __start and __stop symbols final values. */ 7141 7142 static void 7143 set_start_stop (struct bfd_link_hash_entry *h) 7144 { 7145 if (h->ldscript_def 7146 || h->type != bfd_link_hash_defined) 7147 return; 7148 7149 if (h->root.string[0] == '.') 7150 { 7151 /* .startof. or .sizeof. symbol. 7152 .startof. already has final value. */ 7153 if (h->root.string[2] == 'i') 7154 { 7155 /* .sizeof. */ 7156 h->u.def.value = TO_ADDR (h->u.def.section->size); 7157 h->u.def.section = bfd_abs_section_ptr; 7158 } 7159 } 7160 else 7161 { 7162 /* __start or __stop symbol. */ 7163 int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0; 7164 7165 h->u.def.section = h->u.def.section->output_section; 7166 if (h->root.string[4 + has_lead] == 'o') 7167 { 7168 /* __stop_ */ 7169 h->u.def.value = TO_ADDR (h->u.def.section->size); 7170 } 7171 } 7172 } 7173 7174 static void 7175 lang_finalize_start_stop (void) 7176 { 7177 foreach_start_stop (set_start_stop); 7178 } 7179 7180 static void 7181 lang_symbol_tweaks (void) 7182 { 7183 /* Give initial values for __start and __stop symbols, so that ELF 7184 gc_sections will keep sections referenced by these symbols. Must 7185 be done before lang_do_assignments. */ 7186 if (config.build_constructors) 7187 lang_init_start_stop (); 7188 7189 /* Make __ehdr_start hidden, and set def_regular even though it is 7190 likely undefined at this stage. For lang_check_relocs. */ 7191 if (is_elf_hash_table (link_info.hash) 7192 && !bfd_link_relocatable (&link_info)) 7193 { 7194 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) 7195 bfd_link_hash_lookup (link_info.hash, "__ehdr_start", 7196 false, false, true); 7197 7198 /* Only adjust the export class if the symbol was referenced 7199 and not defined, otherwise leave it alone. */ 7200 if (h != NULL 7201 && (h->root.type == bfd_link_hash_new 7202 || h->root.type == bfd_link_hash_undefined 7203 || h->root.type == bfd_link_hash_undefweak 7204 || h->root.type == bfd_link_hash_common)) 7205 { 7206 const struct elf_backend_data *bed; 7207 bed = get_elf_backend_data (link_info.output_bfd); 7208 (*bed->elf_backend_hide_symbol) (&link_info, h, true); 7209 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 7210 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 7211 h->def_regular = 1; 7212 h->root.linker_def = 1; 7213 h->root.rel_from_abs = 1; 7214 elf_hash_table (&link_info)->hehdr_start = h; 7215 } 7216 } 7217 } 7218 7219 static void 7220 lang_end (void) 7221 { 7222 struct bfd_link_hash_entry *h; 7223 bool warn; 7224 7225 if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections) 7226 || bfd_link_dll (&link_info)) 7227 warn = entry_from_cmdline; 7228 else 7229 warn = true; 7230 7231 /* Force the user to specify a root when generating a relocatable with 7232 --gc-sections, unless --gc-keep-exported was also given. */ 7233 if (bfd_link_relocatable (&link_info) 7234 && link_info.gc_sections 7235 && !link_info.gc_keep_exported) 7236 { 7237 struct bfd_sym_chain *sym; 7238 7239 for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next) 7240 { 7241 h = bfd_link_hash_lookup (link_info.hash, sym->name, 7242 false, false, false); 7243 if (is_defined (h) 7244 && !bfd_is_const_section (h->u.def.section)) 7245 break; 7246 } 7247 if (!sym) 7248 fatal (_("%P: --gc-sections requires a defined symbol root " 7249 "specified by -e or -u\n")); 7250 } 7251 7252 if (entry_symbol.name == NULL) 7253 { 7254 /* No entry has been specified. Look for the default entry, but 7255 don't warn if we don't find it. */ 7256 entry_symbol.name = entry_symbol_default; 7257 warn = false; 7258 } 7259 7260 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, 7261 false, false, true); 7262 7263 if (! is_defined (h) || h->u.def.section->output_section == NULL) 7264 h = ldemul_find_alt_start_symbol (&entry_symbol); 7265 7266 if (is_defined (h) 7267 && h->u.def.section->output_section != NULL) 7268 { 7269 bfd_vma val; 7270 7271 val = (h->u.def.value 7272 + bfd_section_vma (h->u.def.section->output_section) 7273 + h->u.def.section->output_offset); 7274 if (!bfd_set_start_address (link_info.output_bfd, val)) 7275 fatal (_("%P: %s: can't set start address\n"), entry_symbol.name); 7276 } 7277 else 7278 { 7279 bfd_vma val; 7280 const char *send; 7281 7282 /* We couldn't find the entry symbol. Try parsing it as a 7283 number. */ 7284 val = bfd_scan_vma (entry_symbol.name, &send, 0); 7285 if (*send == '\0') 7286 { 7287 if (!bfd_set_start_address (link_info.output_bfd, val)) 7288 fatal (_("%P: can't set start address\n")); 7289 } 7290 /* BZ 2004952: Only use the start of the entry section for executables. */ 7291 else if bfd_link_executable (&link_info) 7292 { 7293 asection *ts; 7294 7295 /* Can't find the entry symbol, and it's not a number. Use 7296 the first address in the text section. */ 7297 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section); 7298 if (ts != NULL) 7299 { 7300 if (warn) 7301 einfo (_("%P: warning: cannot find entry symbol %s;" 7302 " defaulting to %V\n"), 7303 entry_symbol.name, 7304 bfd_section_vma (ts)); 7305 if (!bfd_set_start_address (link_info.output_bfd, 7306 bfd_section_vma (ts))) 7307 fatal (_("%P: can't set start address\n")); 7308 } 7309 else 7310 { 7311 if (warn) 7312 einfo (_("%P: warning: cannot find entry symbol %s;" 7313 " not setting start address\n"), 7314 entry_symbol.name); 7315 } 7316 } 7317 else 7318 { 7319 if (warn) 7320 einfo (_("%P: warning: cannot find entry symbol %s;" 7321 " not setting start address\n"), 7322 entry_symbol.name); 7323 } 7324 } 7325 } 7326 7327 /* This is a small function used when we want to ignore errors from 7328 BFD. */ 7329 7330 static void 7331 ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED, 7332 va_list ap ATTRIBUTE_UNUSED) 7333 { 7334 /* Don't do anything. */ 7335 } 7336 7337 /* Check that the architecture of all the input files is compatible 7338 with the output file. Also call the backend to let it do any 7339 other checking that is needed. */ 7340 7341 static void 7342 lang_check (void) 7343 { 7344 lang_input_statement_type *file; 7345 bfd *input_bfd; 7346 const bfd_arch_info_type *compatible; 7347 7348 for (file = (void *) file_chain.head; 7349 file != NULL; 7350 file = file->next) 7351 { 7352 #if BFD_SUPPORTS_PLUGINS 7353 /* Don't check format of files claimed by plugin. */ 7354 if (file->flags.claimed) 7355 continue; 7356 #endif /* BFD_SUPPORTS_PLUGINS */ 7357 input_bfd = file->the_bfd; 7358 compatible 7359 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd, 7360 command_line.accept_unknown_input_arch); 7361 7362 /* In general it is not possible to perform a relocatable 7363 link between differing object formats when the input 7364 file has relocations, because the relocations in the 7365 input format may not have equivalent representations in 7366 the output format (and besides BFD does not translate 7367 relocs for other link purposes than a final link). */ 7368 if (!file->flags.just_syms 7369 && (bfd_link_relocatable (&link_info) 7370 || link_info.emitrelocations) 7371 && (compatible == NULL 7372 || (bfd_get_flavour (input_bfd) 7373 != bfd_get_flavour (link_info.output_bfd))) 7374 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) 7375 { 7376 fatal (_("%P: relocatable linking with relocations from" 7377 " format %s (%pB) to format %s (%pB) is not supported\n"), 7378 bfd_get_target (input_bfd), input_bfd, 7379 bfd_get_target (link_info.output_bfd), link_info.output_bfd); 7380 } 7381 7382 if (compatible == NULL) 7383 { 7384 if (command_line.warn_mismatch) 7385 einfo (_("%X%P: %s architecture of input file `%pB'" 7386 " is incompatible with %s output\n"), 7387 bfd_printable_name (input_bfd), input_bfd, 7388 bfd_printable_name (link_info.output_bfd)); 7389 } 7390 7391 /* If the input bfd has no contents, it shouldn't set the 7392 private data of the output bfd. */ 7393 else if (!file->flags.just_syms 7394 && ((input_bfd->flags & DYNAMIC) != 0 7395 || bfd_count_sections (input_bfd) != 0)) 7396 { 7397 bfd_error_handler_type pfn = NULL; 7398 7399 /* If we aren't supposed to warn about mismatched input 7400 files, temporarily set the BFD error handler to a 7401 function which will do nothing. We still want to call 7402 bfd_merge_private_bfd_data, since it may set up 7403 information which is needed in the output file. */ 7404 if (!command_line.warn_mismatch) 7405 pfn = bfd_set_error_handler (ignore_bfd_errors); 7406 if (!bfd_merge_private_bfd_data (input_bfd, &link_info)) 7407 { 7408 if (command_line.warn_mismatch) 7409 einfo (_("%X%P: failed to merge target specific data" 7410 " of file %pB\n"), input_bfd); 7411 } 7412 if (!command_line.warn_mismatch) 7413 bfd_set_error_handler (pfn); 7414 } 7415 } 7416 } 7417 7418 /* Look through all the global common symbols and attach them to the 7419 correct section. The -sort-common command line switch may be used 7420 to roughly sort the entries by alignment. */ 7421 7422 static void 7423 lang_common (void) 7424 { 7425 if (link_info.inhibit_common_definition) 7426 return; 7427 if (bfd_link_relocatable (&link_info) 7428 && !command_line.force_common_definition) 7429 return; 7430 7431 if (!config.sort_common) 7432 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); 7433 else 7434 { 7435 unsigned int power; 7436 7437 if (config.sort_common == sort_descending) 7438 { 7439 for (power = 4; power > 0; power--) 7440 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); 7441 7442 power = 0; 7443 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); 7444 } 7445 else 7446 { 7447 for (power = 0; power <= 4; power++) 7448 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); 7449 7450 power = (unsigned int) -1; 7451 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); 7452 } 7453 } 7454 } 7455 7456 /* Place one common symbol in the correct section. */ 7457 7458 static bool 7459 lang_one_common (struct bfd_link_hash_entry *h, void *info) 7460 { 7461 unsigned int power_of_two; 7462 bfd_vma size; 7463 asection *section; 7464 7465 if (h->type != bfd_link_hash_common) 7466 return true; 7467 7468 size = h->u.c.size; 7469 power_of_two = h->u.c.p->alignment_power; 7470 7471 if (config.sort_common == sort_descending 7472 && power_of_two < *(unsigned int *) info) 7473 return true; 7474 else if (config.sort_common == sort_ascending 7475 && power_of_two > *(unsigned int *) info) 7476 return true; 7477 7478 section = h->u.c.p->section; 7479 if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h)) 7480 fatal (_("%P: could not define common symbol `%pT': %E\n"), 7481 h->root.string); 7482 7483 if (config.map_file != NULL) 7484 { 7485 static bool header_printed; 7486 int len; 7487 char *name; 7488 char buf[32]; 7489 7490 if (!header_printed) 7491 { 7492 minfo (_("\nAllocating common symbols\n")); 7493 minfo (_("Common symbol size file\n\n")); 7494 header_printed = true; 7495 } 7496 7497 name = bfd_demangle (link_info.output_bfd, h->root.string, 7498 DMGL_ANSI | DMGL_PARAMS); 7499 if (name == NULL) 7500 { 7501 minfo ("%s", h->root.string); 7502 len = strlen (h->root.string); 7503 } 7504 else 7505 { 7506 minfo ("%s", name); 7507 len = strlen (name); 7508 free (name); 7509 } 7510 7511 if (len >= 19) 7512 { 7513 print_nl (); 7514 len = 0; 7515 } 7516 7517 sprintf (buf, "%" PRIx64, (uint64_t) size); 7518 fprintf (config.map_file, "%*s0x%-16s", 20 - len, "", buf); 7519 7520 minfo ("%pB\n", section->owner); 7521 } 7522 7523 return true; 7524 } 7525 7526 /* Handle a single orphan section S, placing the orphan into an appropriate 7527 output section. The effects of the --orphan-handling command line 7528 option are handled here. */ 7529 7530 static void 7531 ldlang_place_orphan (asection *s) 7532 { 7533 if (config.orphan_handling == orphan_handling_discard) 7534 { 7535 lang_output_section_statement_type *os; 7536 os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1); 7537 if (os->addr_tree == NULL 7538 && (bfd_link_relocatable (&link_info) 7539 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) 7540 os->addr_tree = exp_intop (0); 7541 lang_add_section (&os->children, s, NULL, NULL, os); 7542 } 7543 else 7544 { 7545 lang_output_section_statement_type *os; 7546 const char *name = s->name; 7547 int constraint = 0; 7548 7549 if (config.orphan_handling == orphan_handling_error) 7550 einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"), 7551 s, s->owner); 7552 7553 if (config.unique_orphan_sections || unique_section_p (s, NULL)) 7554 constraint = SPECIAL; 7555 7556 os = ldemul_place_orphan (s, name, constraint); 7557 if (os == NULL) 7558 { 7559 os = lang_output_section_statement_lookup (name, constraint, 1); 7560 if (os->addr_tree == NULL 7561 && (bfd_link_relocatable (&link_info) 7562 || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)) 7563 os->addr_tree = exp_intop (0); 7564 lang_add_section (&os->children, s, NULL, NULL, os); 7565 } 7566 7567 if (config.orphan_handling == orphan_handling_warn) 7568 einfo (_("%P: warning: orphan section `%pA' from `%pB' being " 7569 "placed in section `%s'\n"), 7570 s, s->owner, os->name); 7571 } 7572 } 7573 7574 /* Run through the input files and ensure that every input section has 7575 somewhere to go. If one is found without a destination then create 7576 an input request and place it into the statement tree. */ 7577 7578 static void 7579 lang_place_orphans (void) 7580 { 7581 LANG_FOR_EACH_INPUT_STATEMENT (file) 7582 { 7583 asection *s; 7584 7585 for (s = file->the_bfd->sections; s != NULL; s = s->next) 7586 { 7587 if (s->output_section == NULL) 7588 { 7589 /* This section of the file is not attached, root 7590 around for a sensible place for it to go. */ 7591 7592 if (file->flags.just_syms) 7593 bfd_link_just_syms (file->the_bfd, s, &link_info); 7594 else if (lang_discard_section_p (s)) 7595 s->output_section = bfd_abs_section_ptr; 7596 else if (strcmp (s->name, "COMMON") == 0) 7597 { 7598 /* This is a lonely common section which must have 7599 come from an archive. We attach to the section 7600 with the wildcard. */ 7601 if (!bfd_link_relocatable (&link_info) 7602 || command_line.force_common_definition) 7603 { 7604 if (default_common_section == NULL) 7605 default_common_section 7606 = lang_output_section_statement_lookup (".bss", 0, 1); 7607 lang_add_section (&default_common_section->children, s, 7608 NULL, NULL, default_common_section); 7609 } 7610 } 7611 else 7612 ldlang_place_orphan (s); 7613 } 7614 } 7615 } 7616 } 7617 7618 void 7619 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) 7620 { 7621 flagword *ptr_flags; 7622 7623 ptr_flags = invert ? &ptr->not_flags : &ptr->flags; 7624 7625 while (*flags) 7626 { 7627 switch (*flags) 7628 { 7629 /* PR 17900: An exclamation mark in the attributes reverses 7630 the sense of any of the attributes that follow. */ 7631 case '!': 7632 invert = !invert; 7633 ptr_flags = invert ? &ptr->not_flags : &ptr->flags; 7634 break; 7635 7636 case 'A': case 'a': 7637 *ptr_flags |= SEC_ALLOC; 7638 break; 7639 7640 case 'R': case 'r': 7641 *ptr_flags |= SEC_READONLY; 7642 break; 7643 7644 case 'W': case 'w': 7645 *ptr_flags |= SEC_DATA; 7646 break; 7647 7648 case 'X': case 'x': 7649 *ptr_flags |= SEC_CODE; 7650 break; 7651 7652 case 'L': case 'l': 7653 case 'I': case 'i': 7654 *ptr_flags |= SEC_LOAD; 7655 break; 7656 7657 default: 7658 fatal (_("%P: invalid character %c (%d) in flags\n"), 7659 *flags, *flags); 7660 break; 7661 } 7662 flags++; 7663 } 7664 } 7665 7666 /* Call a function on each real input file. This function will be 7667 called on an archive, but not on the elements. */ 7668 7669 void 7670 lang_for_each_input_file (void (*func) (lang_input_statement_type *)) 7671 { 7672 lang_input_statement_type *f; 7673 7674 for (f = (void *) input_file_chain.head; 7675 f != NULL; 7676 f = f->next_real_file) 7677 if (f->flags.real) 7678 func (f); 7679 } 7680 7681 /* Call a function on each real file. The function will be called on 7682 all the elements of an archive which are included in the link, but 7683 will not be called on the archive file itself. */ 7684 7685 void 7686 lang_for_each_file (void (*func) (lang_input_statement_type *)) 7687 { 7688 LANG_FOR_EACH_INPUT_STATEMENT (f) 7689 { 7690 if (f->flags.real) 7691 func (f); 7692 } 7693 } 7694 7695 void 7696 ldlang_add_file (lang_input_statement_type *entry) 7697 { 7698 lang_statement_append (&file_chain, entry, &entry->next); 7699 7700 /* The BFD linker needs to have a list of all input BFDs involved in 7701 a link. */ 7702 ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next 7703 && entry->the_bfd->link.next == NULL); 7704 ASSERT (entry->the_bfd != link_info.output_bfd); 7705 7706 *link_info.input_bfds_tail = entry->the_bfd; 7707 link_info.input_bfds_tail = &entry->the_bfd->link.next; 7708 bfd_set_usrdata (entry->the_bfd, entry); 7709 bfd_set_gp_size (entry->the_bfd, g_switch_value); 7710 7711 /* Look through the sections and check for any which should not be 7712 included in the link. We need to do this now, so that we can 7713 notice when the backend linker tries to report multiple 7714 definition errors for symbols which are in sections we aren't 7715 going to link. FIXME: It might be better to entirely ignore 7716 symbols which are defined in sections which are going to be 7717 discarded. This would require modifying the backend linker for 7718 each backend which might set the SEC_LINK_ONCE flag. If we do 7719 this, we should probably handle SEC_EXCLUDE in the same way. */ 7720 7721 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); 7722 } 7723 7724 void 7725 lang_add_output (const char *name, int from_script) 7726 { 7727 /* Make -o on command line override OUTPUT in script. */ 7728 if (!had_output_filename || !from_script) 7729 { 7730 output_filename = name; 7731 had_output_filename = true; 7732 } 7733 } 7734 7735 lang_output_section_statement_type * 7736 lang_enter_output_section_statement (const char *output_section_statement_name, 7737 etree_type *address_exp, 7738 enum section_type sectype, 7739 etree_type *sectype_value, 7740 etree_type *align, 7741 etree_type *subalign, 7742 etree_type *ebase, 7743 int constraint, 7744 int align_with_input) 7745 { 7746 lang_output_section_statement_type *os; 7747 7748 os = lang_output_section_statement_lookup (output_section_statement_name, 7749 constraint, 7750 in_section_ordering ? 0 : 2); 7751 if (os == NULL) /* && in_section_ordering */ 7752 fatal (_("%P:%pS: error: output section '%s' must already exist\n"), 7753 NULL, output_section_statement_name); 7754 current_section = os; 7755 7756 /* Make next things chain into subchain of this. */ 7757 push_stat_ptr (in_section_ordering ? &os->sort_children : &os->children); 7758 7759 if (in_section_ordering) 7760 return os; 7761 7762 if (os->addr_tree == NULL) 7763 os->addr_tree = address_exp; 7764 7765 os->sectype = sectype; 7766 if (sectype == type_section || sectype == typed_readonly_section) 7767 os->sectype_value = sectype_value; 7768 else if (sectype == noload_section) 7769 os->flags = SEC_NEVER_LOAD; 7770 else 7771 os->flags = SEC_NO_FLAGS; 7772 os->block_value = 1; 7773 7774 os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; 7775 if (os->align_lma_with_input && align != NULL) 7776 fatal (_("%P:%pS: error: align with input and explicit align specified\n"), 7777 NULL); 7778 7779 os->subsection_alignment = subalign; 7780 os->section_alignment = align; 7781 7782 os->load_base = ebase; 7783 return os; 7784 } 7785 7786 void 7787 lang_final (void) 7788 { 7789 lang_output_statement_type *new_stmt; 7790 7791 new_stmt = new_stat (lang_output_statement, stat_ptr); 7792 new_stmt->name = output_filename; 7793 } 7794 7795 /* Reset the current counters in the regions. */ 7796 7797 void 7798 lang_reset_memory_regions (void) 7799 { 7800 lang_memory_region_type *p = lang_memory_region_list; 7801 asection *o; 7802 lang_output_section_statement_type *os; 7803 7804 for (p = lang_memory_region_list; p != NULL; p = p->next) 7805 { 7806 p->current = p->origin; 7807 p->last_os = NULL; 7808 } 7809 7810 for (os = (void *) lang_os_list.head; 7811 os != NULL; 7812 os = os->next) 7813 { 7814 os->processed_vma = false; 7815 os->processed_lma = false; 7816 } 7817 7818 for (o = link_info.output_bfd->sections; o != NULL; o = o->next) 7819 { 7820 /* Save the last size for possible use by bfd_relax_section. */ 7821 o->rawsize = o->size; 7822 if (!(o->flags & SEC_FIXED_SIZE)) 7823 o->size = 0; 7824 } 7825 } 7826 7827 /* Worker for lang_gc_sections_1. */ 7828 7829 static void 7830 gc_section_callback (lang_wild_statement_type *ptr, 7831 struct wildcard_list *sec ATTRIBUTE_UNUSED, 7832 asection *section, 7833 lang_input_statement_type *file ATTRIBUTE_UNUSED, 7834 void *data ATTRIBUTE_UNUSED) 7835 { 7836 /* If the wild pattern was marked KEEP, the member sections 7837 should be as well. */ 7838 if (ptr->keep_sections) 7839 section->flags |= SEC_KEEP; 7840 } 7841 7842 /* Iterate over sections marking them against GC. */ 7843 7844 static void 7845 lang_gc_sections_1 (lang_statement_union_type *s) 7846 { 7847 for (; s != NULL; s = s->header.next) 7848 { 7849 switch (s->header.type) 7850 { 7851 case lang_wild_statement_enum: 7852 walk_wild (&s->wild_statement, gc_section_callback, NULL); 7853 break; 7854 case lang_constructors_statement_enum: 7855 lang_gc_sections_1 (constructor_list.head); 7856 break; 7857 case lang_output_section_statement_enum: 7858 lang_gc_sections_1 (s->output_section_statement.children.head); 7859 break; 7860 case lang_group_statement_enum: 7861 lang_gc_sections_1 (s->group_statement.children.head); 7862 break; 7863 default: 7864 break; 7865 } 7866 } 7867 } 7868 7869 static void 7870 lang_gc_sections (void) 7871 { 7872 /* Keep all sections so marked in the link script. */ 7873 lang_gc_sections_1 (statement_list.head); 7874 7875 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in 7876 the special case of .stabstr debug info. (See bfd/stabs.c) 7877 Twiddle the flag here, to simplify later linker code. */ 7878 if (bfd_link_relocatable (&link_info)) 7879 { 7880 LANG_FOR_EACH_INPUT_STATEMENT (f) 7881 { 7882 asection *sec; 7883 #if BFD_SUPPORTS_PLUGINS 7884 if (f->flags.claimed) 7885 continue; 7886 #endif 7887 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next) 7888 if ((sec->flags & SEC_DEBUGGING) == 0 7889 || strcmp (sec->name, ".stabstr") != 0) 7890 sec->flags &= ~SEC_EXCLUDE; 7891 } 7892 } 7893 7894 if (link_info.gc_sections) 7895 bfd_gc_sections (link_info.output_bfd, &link_info); 7896 } 7897 7898 /* Worker for lang_find_relro_sections_1. */ 7899 7900 static void 7901 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED, 7902 struct wildcard_list *sec ATTRIBUTE_UNUSED, 7903 asection *section, 7904 lang_input_statement_type *file ATTRIBUTE_UNUSED, 7905 void *data) 7906 { 7907 /* Discarded, excluded and ignored sections effectively have zero 7908 size. */ 7909 if (section->output_section != NULL 7910 && section->output_section->owner == link_info.output_bfd 7911 && (section->output_section->flags & SEC_EXCLUDE) == 0 7912 && !IGNORE_SECTION (section) 7913 && section->size != 0) 7914 { 7915 bool *has_relro_section = (bool *) data; 7916 *has_relro_section = true; 7917 } 7918 } 7919 7920 /* Iterate over sections for relro sections. */ 7921 7922 static void 7923 lang_find_relro_sections_1 (lang_statement_union_type *s, 7924 bool *has_relro_section) 7925 { 7926 if (*has_relro_section) 7927 return; 7928 7929 for (; s != NULL; s = s->header.next) 7930 { 7931 if (s == expld.dataseg.relro_end_stat) 7932 break; 7933 7934 switch (s->header.type) 7935 { 7936 case lang_wild_statement_enum: 7937 walk_wild (&s->wild_statement, 7938 find_relro_section_callback, 7939 has_relro_section); 7940 break; 7941 case lang_constructors_statement_enum: 7942 lang_find_relro_sections_1 (constructor_list.head, 7943 has_relro_section); 7944 break; 7945 case lang_output_section_statement_enum: 7946 lang_find_relro_sections_1 (s->output_section_statement.children.head, 7947 has_relro_section); 7948 break; 7949 case lang_group_statement_enum: 7950 lang_find_relro_sections_1 (s->group_statement.children.head, 7951 has_relro_section); 7952 break; 7953 default: 7954 break; 7955 } 7956 } 7957 } 7958 7959 static void 7960 lang_find_relro_sections (void) 7961 { 7962 bool has_relro_section = false; 7963 7964 /* Check all sections in the link script. */ 7965 7966 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat, 7967 &has_relro_section); 7968 7969 if (!has_relro_section) 7970 link_info.relro = false; 7971 } 7972 7973 /* Relax all sections until bfd_relax_section gives up. */ 7974 7975 void 7976 lang_relax_sections (bool need_layout) 7977 { 7978 /* NB: Also enable relaxation to layout sections for DT_RELR. */ 7979 if (RELAXATION_ENABLED || link_info.enable_dt_relr) 7980 { 7981 /* We may need more than one relaxation pass. */ 7982 int i = link_info.relax_pass; 7983 7984 /* The backend can use it to determine the current pass. */ 7985 link_info.relax_pass = 0; 7986 7987 while (i--) 7988 { 7989 /* Keep relaxing until bfd_relax_section gives up. */ 7990 bool relax_again; 7991 7992 link_info.relax_trip = -1; 7993 do 7994 { 7995 link_info.relax_trip++; 7996 7997 /* Note: pe-dll.c does something like this also. If you find 7998 you need to change this code, you probably need to change 7999 pe-dll.c also. DJ */ 8000 8001 /* Do all the assignments with our current guesses as to 8002 section sizes. */ 8003 lang_do_assignments (lang_assigning_phase_enum); 8004 8005 /* We must do this after lang_do_assignments, because it uses 8006 size. */ 8007 lang_reset_memory_regions (); 8008 8009 /* Perform another relax pass - this time we know where the 8010 globals are, so can make a better guess. */ 8011 relax_again = false; 8012 lang_size_sections (&relax_again, false); 8013 } 8014 while (relax_again); 8015 8016 link_info.relax_pass++; 8017 } 8018 need_layout = true; 8019 } 8020 8021 if (need_layout) 8022 { 8023 /* Final extra sizing to report errors. */ 8024 lang_do_assignments (lang_assigning_phase_enum); 8025 lang_reset_memory_regions (); 8026 lang_size_sections (NULL, true); 8027 } 8028 } 8029 8030 #if BFD_SUPPORTS_PLUGINS 8031 /* Find the insert point for the plugin's replacement files. We 8032 place them after the first claimed real object file, or if the 8033 first claimed object is an archive member, after the last real 8034 object file immediately preceding the archive. In the event 8035 no objects have been claimed at all, we return the first dummy 8036 object file on the list as the insert point; that works, but 8037 the callee must be careful when relinking the file_chain as it 8038 is not actually on that chain, only the statement_list and the 8039 input_file list; in that case, the replacement files must be 8040 inserted at the head of the file_chain. */ 8041 8042 static lang_input_statement_type * 8043 find_replacements_insert_point (bool *before) 8044 { 8045 lang_input_statement_type *claim1, *lastobject; 8046 lastobject = (void *) input_file_chain.head; 8047 for (claim1 = (void *) file_chain.head; 8048 claim1 != NULL; 8049 claim1 = claim1->next) 8050 { 8051 if (claim1->flags.claimed) 8052 { 8053 *before = claim1->flags.claim_archive; 8054 return claim1->flags.claim_archive ? lastobject : claim1; 8055 } 8056 /* Update lastobject if this is a real object file. */ 8057 if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL) 8058 lastobject = claim1; 8059 } 8060 /* No files were claimed by the plugin. Choose the last object 8061 file found on the list (maybe the first, dummy entry) as the 8062 insert point. */ 8063 *before = false; 8064 return lastobject; 8065 } 8066 8067 /* Find where to insert ADD, an archive element or shared library 8068 added during a rescan. */ 8069 8070 static lang_input_statement_type ** 8071 find_rescan_insertion (lang_input_statement_type *add) 8072 { 8073 bfd *add_bfd = add->the_bfd; 8074 lang_input_statement_type *f; 8075 lang_input_statement_type *last_loaded = NULL; 8076 lang_input_statement_type *before = NULL; 8077 lang_input_statement_type **iter = NULL; 8078 8079 if (add_bfd->my_archive != NULL) 8080 add_bfd = add_bfd->my_archive; 8081 8082 /* First look through the input file chain, to find an object file 8083 before the one we've rescanned. Normal object files always 8084 appear on both the input file chain and the file chain, so this 8085 lets us get quickly to somewhere near the correct place on the 8086 file chain if it is full of archive elements. Archives don't 8087 appear on the file chain, but if an element has been extracted 8088 then their input_statement->next points at it. */ 8089 for (f = (void *) input_file_chain.head; 8090 f != NULL; 8091 f = f->next_real_file) 8092 { 8093 if (f->the_bfd == add_bfd) 8094 { 8095 before = last_loaded; 8096 if (f->next != NULL) 8097 return &f->next->next; 8098 } 8099 if (f->the_bfd != NULL && f->next != NULL) 8100 last_loaded = f; 8101 } 8102 8103 for (iter = before ? &before->next : &file_chain.head->input_statement.next; 8104 *iter != NULL; 8105 iter = &(*iter)->next) 8106 if (!(*iter)->flags.claim_archive 8107 && (*iter)->the_bfd->my_archive == NULL) 8108 break; 8109 8110 return iter; 8111 } 8112 8113 /* Detach new nodes added to DESTLIST since the time ORIGLIST 8114 was taken as a copy of it and leave them in ORIGLIST. */ 8115 8116 static void 8117 lang_list_remove_tail (lang_statement_list_type *destlist, 8118 lang_statement_list_type *origlist) 8119 { 8120 union lang_statement_union **savetail; 8121 /* Check that ORIGLIST really is an earlier state of DESTLIST. */ 8122 ASSERT (origlist->head == destlist->head); 8123 savetail = origlist->tail; 8124 origlist->head = *(savetail); 8125 origlist->tail = destlist->tail; 8126 destlist->tail = savetail; 8127 *savetail = NULL; 8128 } 8129 8130 static lang_statement_union_type ** 8131 find_next_input_statement (lang_statement_union_type **s) 8132 { 8133 for ( ; *s; s = &(*s)->header.next) 8134 { 8135 lang_statement_union_type **t; 8136 switch ((*s)->header.type) 8137 { 8138 case lang_input_statement_enum: 8139 return s; 8140 case lang_wild_statement_enum: 8141 t = &(*s)->wild_statement.children.head; 8142 break; 8143 case lang_group_statement_enum: 8144 t = &(*s)->group_statement.children.head; 8145 break; 8146 case lang_output_section_statement_enum: 8147 t = &(*s)->output_section_statement.children.head; 8148 break; 8149 default: 8150 continue; 8151 } 8152 t = find_next_input_statement (t); 8153 if (*t) 8154 return t; 8155 } 8156 return s; 8157 } 8158 #endif /* BFD_SUPPORTS_PLUGINS */ 8159 8160 /* Insert SRCLIST into DESTLIST after given element by chaining 8161 on FIELD as the next-pointer. (Counterintuitively does not need 8162 a pointer to the actual after-node itself, just its chain field.) */ 8163 8164 static void 8165 lang_list_insert_after (lang_statement_list_type *destlist, 8166 lang_statement_list_type *srclist, 8167 lang_statement_union_type **field) 8168 { 8169 *(srclist->tail) = *field; 8170 *field = srclist->head; 8171 if (destlist->tail == field) 8172 destlist->tail = srclist->tail; 8173 } 8174 8175 /* Add NAME to the list of garbage collection entry points. */ 8176 8177 void 8178 lang_add_gc_name (const char *name) 8179 { 8180 struct bfd_sym_chain *sym; 8181 8182 if (name == NULL) 8183 return; 8184 8185 sym = stat_alloc (sizeof (*sym)); 8186 8187 sym->next = link_info.gc_sym_list; 8188 sym->name = name; 8189 link_info.gc_sym_list = sym; 8190 } 8191 8192 /* Check relocations. */ 8193 8194 static void 8195 lang_check_relocs (void) 8196 { 8197 if (link_info.check_relocs_after_open_input) 8198 { 8199 bfd *abfd; 8200 8201 for (abfd = link_info.input_bfds; 8202 abfd != (bfd *) NULL; abfd = abfd->link.next) 8203 if (!bfd_link_check_relocs (abfd, &link_info)) 8204 { 8205 /* No object output, fail return. */ 8206 config.make_executable = false; 8207 /* Note: we do not abort the loop, but rather 8208 continue the scan in case there are other 8209 bad relocations to report. */ 8210 } 8211 } 8212 } 8213 8214 /* Look through all output sections looking for places where we can 8215 propagate forward the lma region. */ 8216 8217 static void 8218 lang_propagate_lma_regions (void) 8219 { 8220 lang_output_section_statement_type *os; 8221 8222 for (os = (void *) lang_os_list.head; 8223 os != NULL; 8224 os = os->next) 8225 { 8226 if (os->prev != NULL 8227 && os->lma_region == NULL 8228 && os->load_base == NULL 8229 && os->addr_tree == NULL 8230 && os->region == os->prev->region) 8231 os->lma_region = os->prev->lma_region; 8232 } 8233 } 8234 8235 static void 8236 warn_non_contiguous_discards (void) 8237 { 8238 LANG_FOR_EACH_INPUT_STATEMENT (file) 8239 { 8240 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0 8241 || file->flags.just_syms) 8242 continue; 8243 8244 for (asection *s = file->the_bfd->sections; s != NULL; s = s->next) 8245 if (s->output_section == NULL 8246 && (s->flags & SEC_LINKER_CREATED) == 0) 8247 einfo (_("%P: warning: --enable-non-contiguous-regions " 8248 "discards section `%pA' from `%pB'\n"), 8249 s, file->the_bfd); 8250 } 8251 } 8252 8253 static void 8254 reset_one_wild (lang_statement_union_type *statement) 8255 { 8256 if (statement->header.type == lang_wild_statement_enum) 8257 { 8258 lang_wild_statement_type *stmt = &statement->wild_statement; 8259 lang_list_init (&stmt->matching_sections); 8260 } 8261 } 8262 8263 static void 8264 reset_resolved_wilds (void) 8265 { 8266 lang_for_each_statement (reset_one_wild); 8267 } 8268 8269 /* For each output section statement, splice any entries on the 8270 sort_children list before the first wild statement on the children 8271 list. */ 8272 8273 static void 8274 lang_os_merge_sort_children (void) 8275 { 8276 lang_output_section_statement_type *os; 8277 for (os = (void *) lang_os_list.head; os != NULL; os = os->next) 8278 { 8279 if (os->sort_children.head != NULL) 8280 { 8281 lang_statement_union_type **where; 8282 for (where = &os->children.head; 8283 *where != NULL; 8284 where = &(*where)->header.next) 8285 if ((*where)->header.type == lang_wild_statement_enum) 8286 break; 8287 lang_list_insert_after (&os->children, &os->sort_children, where); 8288 } 8289 } 8290 } 8291 8292 void 8293 lang_process (void) 8294 { 8295 lang_os_merge_sort_children (); 8296 8297 /* Finalize dynamic list. */ 8298 if (link_info.dynamic_list) 8299 lang_finalize_version_expr_head (&link_info.dynamic_list->head); 8300 8301 current_target = default_target; 8302 8303 /* Open the output file. */ 8304 lang_for_each_statement (ldlang_open_output); 8305 init_opb (NULL); 8306 8307 ldemul_create_output_section_statements (); 8308 8309 /* Add to the hash table all undefineds on the command line. */ 8310 lang_place_undefineds (); 8311 8312 if (!bfd_section_already_linked_table_init ()) 8313 fatal (_("%P: can not create hash table: %E\n")); 8314 8315 /* A first pass through the memory regions ensures that if any region 8316 references a symbol for its origin or length then this symbol will be 8317 added to the symbol table. Having these symbols in the symbol table 8318 means that when we call open_input_bfds PROVIDE statements will 8319 trigger to provide any needed symbols. The regions origins and 8320 lengths are not assigned as a result of this call. */ 8321 lang_do_memory_regions (false); 8322 8323 /* Create a bfd for each input file. */ 8324 current_target = default_target; 8325 lang_statement_iteration++; 8326 open_input_bfds (statement_list.head, NULL, OPEN_BFD_NORMAL); 8327 8328 /* Now that open_input_bfds has processed assignments and provide 8329 statements we can give values to symbolic origin/length now. */ 8330 lang_do_memory_regions (true); 8331 8332 ldemul_before_plugin_all_symbols_read (); 8333 8334 #if BFD_SUPPORTS_PLUGINS 8335 if (link_info.lto_plugin_active) 8336 { 8337 lang_statement_list_type added; 8338 lang_statement_list_type files, inputfiles; 8339 8340 /* Now all files are read, let the plugin(s) decide if there 8341 are any more to be added to the link before we call the 8342 emulation's after_open hook. We create a private list of 8343 input statements for this purpose, which we will eventually 8344 insert into the global statement list after the first claimed 8345 file. */ 8346 added = *stat_ptr; 8347 /* We need to manipulate all three chains in synchrony. */ 8348 files = file_chain; 8349 inputfiles = input_file_chain; 8350 if (plugin_call_all_symbols_read ()) 8351 fatal (_("%P: %s: plugin reported error after all symbols read\n"), 8352 plugin_error_plugin ()); 8353 link_info.lto_all_symbols_read = true; 8354 /* Open any newly added files, updating the file chains. */ 8355 plugin_undefs = link_info.hash->undefs_tail; 8356 lang_output_section_statement_type *last_os = NULL; 8357 if (lang_os_list.head != NULL) 8358 last_os = ((lang_output_section_statement_type *) 8359 ((char *) lang_os_list.tail 8360 - offsetof (lang_output_section_statement_type, next))); 8361 open_input_bfds (*added.tail, last_os, OPEN_BFD_NORMAL); 8362 if (plugin_undefs == link_info.hash->undefs_tail) 8363 plugin_undefs = NULL; 8364 /* Restore the global list pointer now they have all been added. */ 8365 lang_list_remove_tail (stat_ptr, &added); 8366 /* And detach the fresh ends of the file lists. */ 8367 lang_list_remove_tail (&file_chain, &files); 8368 lang_list_remove_tail (&input_file_chain, &inputfiles); 8369 /* Were any new files added? */ 8370 if (added.head != NULL) 8371 { 8372 /* If so, we will insert them into the statement list immediately 8373 after the first input file that was claimed by the plugin, 8374 unless that file was an archive in which case it is inserted 8375 immediately before. */ 8376 bool before; 8377 lang_statement_union_type **prev; 8378 plugin_insert = find_replacements_insert_point (&before); 8379 /* If a plugin adds input files without having claimed any, we 8380 don't really have a good idea where to place them. Just putting 8381 them at the start or end of the list is liable to leave them 8382 outside the crtbegin...crtend range. */ 8383 ASSERT (plugin_insert != NULL); 8384 /* Splice the new statement list into the old one. */ 8385 prev = &plugin_insert->header.next; 8386 if (before) 8387 { 8388 prev = find_next_input_statement (prev); 8389 if (*prev != (void *) plugin_insert->next_real_file) 8390 { 8391 /* We didn't find the expected input statement. 8392 Fall back to adding after plugin_insert. */ 8393 prev = &plugin_insert->header.next; 8394 } 8395 } 8396 lang_list_insert_after (stat_ptr, &added, prev); 8397 /* Likewise for the file chains. */ 8398 lang_list_insert_after (&input_file_chain, &inputfiles, 8399 (void *) &plugin_insert->next_real_file); 8400 /* We must be careful when relinking file_chain; we may need to 8401 insert the new files at the head of the list if the insert 8402 point chosen is the dummy first input file. */ 8403 if (plugin_insert->filename) 8404 lang_list_insert_after (&file_chain, &files, 8405 (void *) &plugin_insert->next); 8406 else 8407 lang_list_insert_after (&file_chain, &files, &file_chain.head); 8408 8409 /* Rescan archives in case new undefined symbols have appeared. */ 8410 files = file_chain; 8411 lang_statement_iteration++; 8412 open_input_bfds (statement_list.head, NULL, OPEN_BFD_RESCAN); 8413 lang_list_remove_tail (&file_chain, &files); 8414 while (files.head != NULL) 8415 { 8416 lang_input_statement_type **insert; 8417 lang_input_statement_type **iter, *temp; 8418 bfd *my_arch; 8419 8420 insert = find_rescan_insertion (&files.head->input_statement); 8421 /* All elements from an archive can be added at once. */ 8422 iter = &files.head->input_statement.next; 8423 my_arch = files.head->input_statement.the_bfd->my_archive; 8424 if (my_arch != NULL) 8425 for (; *iter != NULL; iter = &(*iter)->next) 8426 if ((*iter)->the_bfd->my_archive != my_arch) 8427 break; 8428 temp = *insert; 8429 *insert = &files.head->input_statement; 8430 files.head = (lang_statement_union_type *) *iter; 8431 *iter = temp; 8432 if (file_chain.tail == (lang_statement_union_type **) insert) 8433 file_chain.tail = (lang_statement_union_type **) iter; 8434 if (my_arch != NULL) 8435 { 8436 lang_input_statement_type *parent = bfd_usrdata (my_arch); 8437 if (parent != NULL) 8438 parent->next = (lang_input_statement_type *) 8439 ((char *) iter 8440 - offsetof (lang_input_statement_type, next)); 8441 } 8442 } 8443 } 8444 } 8445 else 8446 #endif /* BFD_SUPPORTS_PLUGINS */ 8447 if (bfd_link_relocatable (&link_info)) 8448 { 8449 /* Check if .gnu_object_only section should be created. */ 8450 bfd *p; 8451 int object_type; 8452 8453 object_type = 0; 8454 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link.next) 8455 { 8456 enum bfd_lto_object_type lto_type = bfd_get_lto_type (p); 8457 /* NB: Treat fat IR object as IR object here. */ 8458 if (lto_type == lto_fat_ir_object) 8459 lto_type = lto_slim_ir_object; 8460 object_type |= 1 << lto_type; 8461 if ((object_type & (1 << lto_mixed_object)) != 0 8462 || ((object_type 8463 & (1 << lto_non_ir_object 8464 | 1 << lto_slim_ir_object)) 8465 == (1 << lto_non_ir_object | 1 << lto_slim_ir_object))) 8466 { 8467 config.emit_gnu_object_only = true; 8468 break; 8469 } 8470 } 8471 8472 if (verbose 8473 && (cmdline_object_only_file_list.head 8474 || cmdline_object_only_archive_list.head)) 8475 { 8476 info_msg (_("Object-only input files:\n ")); 8477 print_cmdline_list (cmdline_object_only_file_list.head); 8478 print_cmdline_list (cmdline_object_only_archive_list.head); 8479 } 8480 } 8481 8482 struct bfd_sym_chain **sym = &link_info.gc_sym_list; 8483 while (*sym) 8484 sym = &(*sym)->next; 8485 8486 *sym = &entry_symbol; 8487 8488 if (entry_symbol.name == NULL) 8489 { 8490 *sym = ldlang_undef_chain_list_head; 8491 8492 /* entry_symbol is normally initialised by an ENTRY definition in the 8493 linker script or the -e command line option. But if neither of 8494 these have been used, the target specific backend may still have 8495 provided an entry symbol via a call to lang_default_entry(). 8496 Unfortunately this value will not be processed until lang_end() 8497 is called, long after this function has finished. So detect this 8498 case here and add the target's entry symbol to the list of starting 8499 points for garbage collection resolution. */ 8500 lang_add_gc_name (entry_symbol_default); 8501 } 8502 8503 lang_add_gc_name (link_info.init_function); 8504 lang_add_gc_name (link_info.fini_function); 8505 8506 ldemul_after_open (); 8507 if (config.map_file != NULL) 8508 lang_print_asneeded (); 8509 8510 ldlang_open_ctf (); 8511 8512 bfd_section_already_linked_table_free (); 8513 8514 /* Make sure that we're not mixing architectures. We call this 8515 after all the input files have been opened, but before we do any 8516 other processing, so that any operations merge_private_bfd_data 8517 does on the output file will be known during the rest of the 8518 link. */ 8519 lang_check (); 8520 8521 /* Handle .exports instead of a version script if we're told to do so. */ 8522 if (command_line.version_exports_section) 8523 lang_do_version_exports_section (); 8524 8525 /* Build all sets based on the information gathered from the input 8526 files. */ 8527 ldctor_build_sets (); 8528 8529 lang_symbol_tweaks (); 8530 8531 /* PR 13683: We must rerun the assignments prior to running garbage 8532 collection in order to make sure that all symbol aliases are resolved. */ 8533 lang_do_assignments (lang_mark_phase_enum); 8534 expld.phase = lang_first_phase_enum; 8535 8536 /* Size up the common data. */ 8537 lang_common (); 8538 8539 if (0) 8540 debug_prefix_tree (); 8541 8542 resolve_wilds (); 8543 8544 /* Remove unreferenced sections if asked to. */ 8545 lang_gc_sections (); 8546 8547 lang_mark_undefineds (); 8548 8549 /* Check relocations. */ 8550 lang_check_relocs (); 8551 8552 ldemul_after_check_relocs (); 8553 8554 /* There might have been new sections created (e.g. as result of 8555 checking relocs to need a .got, or suchlike), so to properly order 8556 them into our lists of matching sections reset them here. */ 8557 reset_resolved_wilds (); 8558 resolve_wilds (); 8559 8560 /* Update wild statements in case the user gave --sort-section. 8561 Note how the option might have come after the linker script and 8562 so couldn't have been set when the wild statements were created. */ 8563 update_wild_statements (statement_list.head); 8564 8565 /* Run through the contours of the script and attach input sections 8566 to the correct output sections. */ 8567 lang_statement_iteration++; 8568 map_input_to_output_sections (statement_list.head, NULL, NULL); 8569 8570 /* Start at the statement immediately after the special abs_section 8571 output statement, so that it isn't reordered. */ 8572 process_insert_statements (&lang_os_list.head->header.next); 8573 8574 ldemul_before_place_orphans (); 8575 8576 /* Find any sections not attached explicitly and handle them. */ 8577 lang_place_orphans (); 8578 8579 if (!bfd_link_relocatable (&link_info)) 8580 { 8581 asection *found; 8582 8583 ld_start_phase (PHASE_MERGE); 8584 8585 /* Merge SEC_MERGE sections. This has to be done after GC of 8586 sections, so that GCed sections are not merged, but before 8587 assigning dynamic symbols, since removing whole input sections 8588 is hard then. */ 8589 if (!bfd_merge_sections (link_info.output_bfd, &link_info)) 8590 fatal (_("%P: bfd_merge_sections failed: %E\n")); 8591 8592 ld_stop_phase (PHASE_MERGE); 8593 8594 /* Look for a text section and set the readonly attribute in it. */ 8595 found = bfd_get_section_by_name (link_info.output_bfd, ".text"); 8596 8597 if (found != NULL) 8598 { 8599 if (config.text_read_only) 8600 found->flags |= SEC_READONLY; 8601 else 8602 found->flags &= ~SEC_READONLY; 8603 } 8604 } 8605 8606 /* Merge together CTF sections. After this, only the symtab-dependent 8607 function and data object sections need adjustment. */ 8608 lang_merge_ctf (); 8609 8610 /* Emit the CTF, iff the emulation doesn't need to do late emission after 8611 examining things laid out late, like the strtab. */ 8612 lang_write_ctf (0); 8613 8614 /* Copy forward lma regions for output sections in same lma region. */ 8615 lang_propagate_lma_regions (); 8616 8617 /* Defining __start/__stop symbols early for --gc-sections to work 8618 around a glibc build problem can result in these symbols being 8619 defined when they should not be. Fix them now. */ 8620 if (config.build_constructors) 8621 lang_undef_start_stop (); 8622 8623 /* Define .startof./.sizeof. symbols with preliminary values before 8624 dynamic symbols are created. */ 8625 if (!bfd_link_relocatable (&link_info)) 8626 lang_init_startof_sizeof (); 8627 8628 /* Do anything special before sizing sections. This is where ELF 8629 and other back-ends size dynamic sections. */ 8630 ldemul_before_allocation (); 8631 8632 /* We must record the program headers before we try to fix the 8633 section positions, since they will affect SIZEOF_HEADERS. */ 8634 lang_record_phdrs (); 8635 8636 /* Check relro sections. */ 8637 if (link_info.relro && !bfd_link_relocatable (&link_info)) 8638 lang_find_relro_sections (); 8639 8640 /* Size up the sections. */ 8641 lang_size_sections (NULL, !RELAXATION_ENABLED); 8642 8643 /* See if anything special should be done now we know how big 8644 everything is. This is where relaxation is done. */ 8645 ldemul_after_allocation (); 8646 8647 /* Fix any __start, __stop, .startof. or .sizeof. symbols. */ 8648 lang_finalize_start_stop (); 8649 8650 /* Do all the assignments again, to report errors. Assignment 8651 statements are processed multiple times, updating symbols; In 8652 open_input_bfds, lang_do_assignments, and lang_size_sections. 8653 Since lang_relax_sections calls lang_do_assignments, symbols are 8654 also updated in ldemul_after_allocation. */ 8655 lang_do_assignments (lang_final_phase_enum); 8656 8657 ldemul_finish (); 8658 8659 /* Convert absolute symbols to section relative. */ 8660 ldexp_finalize_syms (); 8661 8662 /* Make sure that the section addresses make sense. */ 8663 if (command_line.check_section_addresses) 8664 lang_check_section_addresses (); 8665 8666 if (link_info.non_contiguous_regions 8667 && link_info.non_contiguous_regions_warnings) 8668 warn_non_contiguous_discards (); 8669 8670 /* Check any required symbols are known. */ 8671 ldlang_check_require_defined_symbols (); 8672 8673 lang_end (); 8674 } 8675 8676 void 8677 lang_add_version_string (void) 8678 { 8679 if (! enable_linker_version) 8680 return; 8681 8682 const char * str = "GNU ld "; 8683 int len = strlen (str); 8684 int i; 8685 8686 for (i = 0 ; i < len ; i++) 8687 lang_add_data (BYTE, exp_intop (str[i])); 8688 8689 str = BFD_VERSION_STRING; 8690 len = strlen (str); 8691 8692 for (i = 0 ; i < len ; i++) 8693 lang_add_data (BYTE, exp_intop (str[i])); 8694 8695 lang_add_data (BYTE, exp_intop ('\0')); 8696 } 8697 8698 /* EXPORTED TO YACC */ 8699 8700 void 8701 lang_add_wild (struct wildcard_spec *filespec, 8702 struct wildcard_list *section_list, 8703 bool keep_sections) 8704 { 8705 struct wildcard_list *curr, *next; 8706 lang_wild_statement_type *new_stmt; 8707 bool any_specs_sorted = false; 8708 8709 /* Reverse the list as the parser puts it back to front. */ 8710 for (curr = section_list, section_list = NULL; 8711 curr != NULL; 8712 section_list = curr, curr = next) 8713 { 8714 if (curr->spec.sorted != none && curr->spec.sorted != by_none) 8715 any_specs_sorted = true; 8716 next = curr->next; 8717 curr->next = section_list; 8718 } 8719 8720 if (filespec != NULL && filespec->name != NULL) 8721 { 8722 if (strcmp (filespec->name, "*") == 0) 8723 filespec->name = NULL; 8724 else if (!wildcardp (filespec->name)) 8725 lang_has_input_file = true; 8726 } 8727 8728 new_stmt = new_stat (lang_wild_statement, stat_ptr); 8729 new_stmt->filename = NULL; 8730 new_stmt->filenames_sorted = false; 8731 new_stmt->filenames_reversed = false; 8732 new_stmt->any_specs_sorted = any_specs_sorted; 8733 new_stmt->section_flag_list = NULL; 8734 new_stmt->exclude_name_list = NULL; 8735 if (filespec != NULL) 8736 { 8737 new_stmt->filename = filespec->name; 8738 new_stmt->filenames_sorted = (filespec->sorted == by_name || filespec->reversed); 8739 new_stmt->filenames_reversed = filespec->reversed; 8740 new_stmt->section_flag_list = filespec->section_flag_list; 8741 new_stmt->exclude_name_list = filespec->exclude_name_list; 8742 } 8743 new_stmt->section_list = section_list; 8744 new_stmt->keep_sections = keep_sections; 8745 lang_list_init (&new_stmt->children); 8746 lang_list_init (&new_stmt->matching_sections); 8747 analyze_walk_wild_section_handler (new_stmt); 8748 if (0) 8749 { 8750 printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); 8751 for (curr = new_stmt->section_list; curr; curr = curr->next) 8752 printf ("%s ", curr->spec.name ? curr->spec.name : "*"); 8753 printf (")\n"); 8754 } 8755 } 8756 8757 void 8758 lang_section_start (const char *name, etree_type *address, 8759 const segment_type *segment) 8760 { 8761 lang_address_statement_type *ad; 8762 8763 ad = new_stat (lang_address_statement, stat_ptr); 8764 ad->section_name = name; 8765 ad->address = address; 8766 ad->segment = segment; 8767 } 8768 8769 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called 8770 because of a -e argument on the command line, or zero if this is 8771 called by ENTRY in a linker script. Command line arguments take 8772 precedence. */ 8773 8774 void 8775 lang_add_entry (const char *name, bool cmdline) 8776 { 8777 if (entry_symbol.name == NULL 8778 || cmdline 8779 || !entry_from_cmdline) 8780 { 8781 entry_symbol.name = name; 8782 entry_from_cmdline = cmdline; 8783 } 8784 } 8785 8786 /* Set the default start symbol to NAME. .em files should use this, 8787 not lang_add_entry, to override the use of "start" if neither the 8788 linker script nor the command line specifies an entry point. NAME 8789 must be permanently allocated. */ 8790 void 8791 lang_default_entry (const char *name) 8792 { 8793 entry_symbol_default = name; 8794 } 8795 8796 void 8797 lang_add_target (const char *name) 8798 { 8799 lang_target_statement_type *new_stmt; 8800 8801 new_stmt = new_stat (lang_target_statement, stat_ptr); 8802 new_stmt->target = name; 8803 } 8804 8805 void 8806 lang_add_map (const char *name) 8807 { 8808 while (*name) 8809 { 8810 switch (*name) 8811 { 8812 case 'F': 8813 map_option_f = true; 8814 break; 8815 } 8816 name++; 8817 } 8818 } 8819 8820 void 8821 lang_add_fill (fill_type *fill) 8822 { 8823 lang_fill_statement_type *new_stmt; 8824 8825 new_stmt = new_stat (lang_fill_statement, stat_ptr); 8826 new_stmt->fill = fill; 8827 } 8828 8829 void 8830 lang_add_data (int type, union etree_union *exp) 8831 { 8832 lang_data_statement_type *new_stmt; 8833 8834 new_stmt = new_stat (lang_data_statement, stat_ptr); 8835 new_stmt->exp = exp; 8836 new_stmt->type = type; 8837 } 8838 8839 void 8840 lang_add_string (const char *s) 8841 { 8842 bfd_vma len = strlen (s); 8843 bfd_vma i; 8844 bool escape = false; 8845 8846 /* Add byte expressions until end of string. */ 8847 for (i = 0 ; i < len; i++) 8848 { 8849 char c = *s++; 8850 8851 if (escape) 8852 { 8853 switch (c) 8854 { 8855 default: 8856 /* Ignore the escape. */ 8857 break; 8858 8859 case 'n': c = '\n'; break; 8860 case 'r': c = '\r'; break; 8861 case 't': c = '\t'; break; 8862 8863 case '0': 8864 case '1': 8865 case '2': 8866 case '3': 8867 case '4': 8868 case '5': 8869 case '6': 8870 case '7': 8871 /* We have an octal number. */ 8872 { 8873 unsigned int value = c - '0'; 8874 8875 c = *s; 8876 if ((c >= '0') && (c <= '7')) 8877 { 8878 value <<= 3; 8879 value += (c - '0'); 8880 i++; 8881 s++; 8882 8883 c = *s; 8884 if ((c >= '0') && (c <= '7')) 8885 { 8886 value <<= 3; 8887 value += (c - '0'); 8888 i++; 8889 s++; 8890 } 8891 } 8892 8893 if (value > 0xff) 8894 { 8895 /* octal: \777 is treated as '\077' + '7' */ 8896 value >>= 3; 8897 i--; 8898 s--; 8899 } 8900 8901 c = value; 8902 } 8903 break; 8904 } 8905 8906 lang_add_data (BYTE, exp_intop (c)); 8907 escape = false; 8908 } 8909 else 8910 { 8911 if (c == '\\') 8912 escape = true; 8913 else 8914 lang_add_data (BYTE, exp_intop (c)); 8915 } 8916 } 8917 8918 /* Remeber to terminate the string. */ 8919 lang_add_data (BYTE, exp_intop (0)); 8920 } 8921 8922 /* Create a new reloc statement. RELOC is the BFD relocation type to 8923 generate. HOWTO is the corresponding howto structure (we could 8924 look this up, but the caller has already done so). SECTION is the 8925 section to generate a reloc against, or NAME is the name of the 8926 symbol to generate a reloc against. Exactly one of SECTION and 8927 NAME must be NULL. ADDEND is an expression for the addend. */ 8928 8929 void 8930 lang_add_reloc (bfd_reloc_code_real_type reloc, 8931 reloc_howto_type *howto, 8932 asection *section, 8933 const char *name, 8934 union etree_union *addend) 8935 { 8936 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); 8937 8938 p->reloc = reloc; 8939 p->howto = howto; 8940 p->section = section; 8941 p->name = name; 8942 p->addend_exp = addend; 8943 8944 p->addend_value = 0; 8945 p->output_section = NULL; 8946 p->output_offset = 0; 8947 } 8948 8949 lang_assignment_statement_type * 8950 lang_add_assignment (etree_type *exp) 8951 { 8952 lang_assignment_statement_type *new_stmt; 8953 8954 new_stmt = new_stat (lang_assignment_statement, stat_ptr); 8955 new_stmt->exp = exp; 8956 return new_stmt; 8957 } 8958 8959 void 8960 lang_add_attribute (enum statement_enum attribute) 8961 { 8962 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr); 8963 } 8964 8965 void 8966 lang_startup (const char *name) 8967 { 8968 if (first_file->filename != NULL) 8969 fatal (_("%P: multiple STARTUP files\n")); 8970 first_file->filename = name; 8971 first_file->local_sym_name = name; 8972 first_file->flags.real = true; 8973 } 8974 8975 void 8976 lang_float (bool maybe) 8977 { 8978 lang_float_flag = maybe; 8979 } 8980 8981 8982 /* Work out the load- and run-time regions from a script statement, and 8983 store them in *LMA_REGION and *REGION respectively. 8984 8985 MEMSPEC is the name of the run-time region, or the value of 8986 DEFAULT_MEMORY_REGION if the statement didn't specify one. 8987 LMA_MEMSPEC is the name of the load-time region, or null if the 8988 statement didn't specify one.HAVE_LMA_P is TRUE if the statement 8989 had an explicit load address. 8990 8991 It is an error to specify both a load region and a load address. */ 8992 8993 static void 8994 lang_get_regions (lang_memory_region_type **region, 8995 lang_memory_region_type **lma_region, 8996 const char *memspec, 8997 const char *lma_memspec, 8998 bool have_lma, 8999 bool have_vma) 9000 { 9001 *lma_region = lang_memory_region_lookup (lma_memspec, false); 9002 9003 /* If no runtime region or VMA has been specified, but the load region 9004 has been specified, then use the load region for the runtime region 9005 as well. */ 9006 if (lma_memspec != NULL 9007 && !have_vma 9008 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) 9009 *region = *lma_region; 9010 else 9011 *region = lang_memory_region_lookup (memspec, false); 9012 9013 if (have_lma && lma_memspec != 0) 9014 einfo (_("%X%P:%pS: section has both a load address and a load region\n"), 9015 NULL); 9016 } 9017 9018 void 9019 lang_leave_output_section_statement (fill_type *fill, const char *memspec, 9020 lang_output_section_phdr_list *phdrs, 9021 const char *lma_memspec) 9022 { 9023 pop_stat_ptr (); 9024 if (in_section_ordering) 9025 return; 9026 9027 lang_get_regions (¤t_section->region, 9028 ¤t_section->lma_region, 9029 memspec, lma_memspec, 9030 current_section->load_base != NULL, 9031 current_section->addr_tree != NULL); 9032 9033 current_section->fill = fill; 9034 current_section->phdrs = phdrs; 9035 } 9036 9037 /* Set the output format type. -oformat overrides scripts. */ 9038 9039 void 9040 lang_add_output_format (const char *format, 9041 const char *big, 9042 const char *little, 9043 int from_script) 9044 { 9045 if (output_target == NULL || !from_script) 9046 { 9047 if (command_line.endian == ENDIAN_BIG 9048 && big != NULL) 9049 format = big; 9050 else if (command_line.endian == ENDIAN_LITTLE 9051 && little != NULL) 9052 format = little; 9053 9054 output_target = format; 9055 } 9056 } 9057 9058 void 9059 lang_add_insert (const char *where, int is_before) 9060 { 9061 lang_insert_statement_type *new_stmt; 9062 9063 new_stmt = new_stat (lang_insert_statement, stat_ptr); 9064 new_stmt->where = where; 9065 new_stmt->is_before = is_before; 9066 saved_script_handle = previous_script_handle; 9067 } 9068 9069 /* Enter a group. This creates a new lang_group_statement, and sets 9070 stat_ptr to build new statements within the group. */ 9071 9072 void 9073 lang_enter_group (void) 9074 { 9075 lang_group_statement_type *g; 9076 9077 g = new_stat (lang_group_statement, stat_ptr); 9078 lang_list_init (&g->children); 9079 push_stat_ptr (&g->children); 9080 } 9081 9082 /* Leave a group. This just resets stat_ptr to start writing to the 9083 regular list of statements again. Note that this will not work if 9084 groups can occur inside anything else which can adjust stat_ptr, 9085 but currently they can't. */ 9086 9087 void 9088 lang_leave_group (void) 9089 { 9090 pop_stat_ptr (); 9091 } 9092 9093 /* Add a new program header. This is called for each entry in a PHDRS 9094 command in a linker script. */ 9095 9096 void 9097 lang_new_phdr (const char *name, 9098 etree_type *type, 9099 bool filehdr, 9100 bool phdrs, 9101 etree_type *at, 9102 etree_type *flags) 9103 { 9104 struct lang_phdr *n, **pp; 9105 bool hdrs; 9106 9107 n = stat_alloc (sizeof (struct lang_phdr)); 9108 n->next = NULL; 9109 n->name = name; 9110 n->type = exp_get_vma (type, NULL, 0, "program header type"); 9111 n->filehdr = filehdr; 9112 n->phdrs = phdrs; 9113 n->at = at; 9114 n->flags = flags; 9115 9116 hdrs = n->type == 1 && (phdrs || filehdr); 9117 9118 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) 9119 if (hdrs 9120 && (*pp)->type == 1 9121 && !((*pp)->filehdr || (*pp)->phdrs)) 9122 { 9123 einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported" 9124 " when prior PT_LOAD headers lack them\n"), NULL); 9125 hdrs = false; 9126 } 9127 9128 *pp = n; 9129 } 9130 9131 /* Record the program header information in the output BFD. FIXME: We 9132 should not be calling an ELF specific function here. */ 9133 9134 static void 9135 lang_record_phdrs (void) 9136 { 9137 unsigned int alc; 9138 asection **secs; 9139 lang_output_section_phdr_list *last; 9140 struct lang_phdr *l; 9141 lang_output_section_statement_type *os; 9142 9143 alc = 10; 9144 secs = (asection **) xmalloc (alc * sizeof (asection *)); 9145 last = NULL; 9146 9147 for (l = lang_phdr_list; l != NULL; l = l->next) 9148 { 9149 unsigned int c; 9150 flagword flags; 9151 bfd_vma at; 9152 9153 c = 0; 9154 for (os = (void *) lang_os_list.head; 9155 os != NULL; 9156 os = os->next) 9157 { 9158 lang_output_section_phdr_list *pl; 9159 9160 if (os->constraint < 0) 9161 continue; 9162 9163 pl = os->phdrs; 9164 if (pl != NULL) 9165 last = pl; 9166 else 9167 { 9168 if (os->sectype == noload_section 9169 || os->bfd_section == NULL 9170 || (os->bfd_section->flags & SEC_ALLOC) == 0) 9171 continue; 9172 9173 /* Don't add orphans to PT_INTERP header. */ 9174 if (l->type == PT_INTERP) 9175 continue; 9176 9177 if (last == NULL) 9178 { 9179 lang_output_section_statement_type *tmp_os; 9180 9181 /* If we have not run across a section with a program 9182 header assigned to it yet, then scan forwards to find 9183 one. This prevents inconsistencies in the linker's 9184 behaviour when a script has specified just a single 9185 header and there are sections in that script which are 9186 not assigned to it, and which occur before the first 9187 use of that header. See here for more details: 9188 http://sourceware.org/ml/binutils/2007-02/msg00291.html */ 9189 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next) 9190 if (tmp_os->phdrs) 9191 { 9192 last = tmp_os->phdrs; 9193 break; 9194 } 9195 if (last == NULL) 9196 fatal (_("%P: no sections assigned to phdrs\n")); 9197 } 9198 pl = last; 9199 } 9200 9201 if (os->bfd_section == NULL) 9202 continue; 9203 9204 for (; pl != NULL; pl = pl->next) 9205 { 9206 if (strcmp (pl->name, l->name) == 0) 9207 { 9208 if (c >= alc) 9209 { 9210 alc *= 2; 9211 secs = (asection **) xrealloc (secs, 9212 alc * sizeof (asection *)); 9213 } 9214 secs[c] = os->bfd_section; 9215 ++c; 9216 pl->used = true; 9217 } 9218 } 9219 } 9220 9221 if (l->flags == NULL) 9222 flags = 0; 9223 else 9224 flags = exp_get_vma (l->flags, NULL, 0, "phdr flags"); 9225 9226 if (l->at == NULL) 9227 at = 0; 9228 else 9229 at = exp_get_vma (l->at, NULL, 0, "phdr load address"); 9230 9231 if (!bfd_record_phdr (link_info.output_bfd, l->type, 9232 l->flags != NULL, flags, l->at != NULL, 9233 at, l->filehdr, l->phdrs, c, secs)) 9234 fatal (_("%P: bfd_record_phdr failed: %E\n")); 9235 } 9236 9237 free (secs); 9238 9239 /* Make sure all the phdr assignments succeeded. */ 9240 for (os = (void *) lang_os_list.head; 9241 os != NULL; 9242 os = os->next) 9243 { 9244 lang_output_section_phdr_list *pl; 9245 9246 if (os->constraint < 0 9247 || os->bfd_section == NULL) 9248 continue; 9249 9250 for (pl = os->phdrs; 9251 pl != NULL; 9252 pl = pl->next) 9253 if (!pl->used && strcmp (pl->name, "NONE") != 0) 9254 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), 9255 os->name, pl->name); 9256 } 9257 } 9258 9259 /* Record a list of sections which may not be cross referenced. */ 9260 9261 void 9262 lang_add_nocrossref (lang_nocrossref_type *l) 9263 { 9264 struct lang_nocrossrefs *n; 9265 9266 n = stat_alloc (sizeof *n); 9267 n->next = nocrossref_list; 9268 n->list = l; 9269 n->onlyfirst = false; 9270 nocrossref_list = n; 9271 9272 /* Set notice_all so that we get informed about all symbols. */ 9273 link_info.notice_all = true; 9274 } 9275 9276 /* Record a section that cannot be referenced from a list of sections. */ 9277 9278 void 9279 lang_add_nocrossref_to (lang_nocrossref_type *l) 9280 { 9281 lang_add_nocrossref (l); 9282 nocrossref_list->onlyfirst = true; 9283 } 9284 9285 /* Overlay handling. We handle overlays with some static variables. */ 9287 9288 /* The overlay virtual address. */ 9289 static etree_type *overlay_vma; 9290 /* And subsection alignment. */ 9291 static etree_type *overlay_subalign; 9292 9293 /* An expression for the maximum section size seen so far. */ 9294 static etree_type *overlay_max; 9295 9296 /* A list of all the sections in this overlay. */ 9297 9298 struct overlay_list { 9299 struct overlay_list *next; 9300 lang_output_section_statement_type *os; 9301 }; 9302 9303 static struct overlay_list *overlay_list; 9304 9305 /* Start handling an overlay. */ 9306 9307 void 9308 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) 9309 { 9310 /* The grammar should prevent nested overlays from occurring. */ 9311 ASSERT (overlay_vma == NULL 9312 && overlay_subalign == NULL 9313 && overlay_max == NULL); 9314 9315 overlay_vma = vma_expr; 9316 overlay_subalign = subalign; 9317 } 9318 9319 /* Start a section in an overlay. We handle this by calling 9320 lang_enter_output_section_statement with the correct VMA. 9321 lang_leave_overlay sets up the LMA and memory regions. */ 9322 9323 void 9324 lang_enter_overlay_section (const char *name) 9325 { 9326 struct overlay_list *n; 9327 etree_type *size; 9328 9329 lang_enter_output_section_statement (name, overlay_vma, overlay_section, 9330 0, 0, overlay_subalign, 0, 0, 0); 9331 9332 /* If this is the first section, then base the VMA of future 9333 sections on this one. This will work correctly even if `.' is 9334 used in the addresses. */ 9335 if (overlay_list == NULL) 9336 overlay_vma = exp_nameop (ADDR, name); 9337 9338 /* Remember the section. */ 9339 n = (struct overlay_list *) xmalloc (sizeof *n); 9340 n->os = current_section; 9341 n->next = overlay_list; 9342 overlay_list = n; 9343 9344 size = exp_nameop (SIZEOF, name); 9345 9346 /* Arrange to work out the maximum section end address. */ 9347 if (overlay_max == NULL) 9348 overlay_max = size; 9349 else 9350 overlay_max = exp_binop (MAX_K, overlay_max, size); 9351 } 9352 9353 /* Finish a section in an overlay. There isn't any special to do 9354 here. */ 9355 9356 void 9357 lang_leave_overlay_section (fill_type *fill, 9358 lang_output_section_phdr_list *phdrs) 9359 { 9360 const char *name = current_section->name;; 9361 9362 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory 9363 region and that no load-time region has been specified. It doesn't 9364 really matter what we say here, since lang_leave_overlay will 9365 override it. */ 9366 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); 9367 9368 /* Define the magic symbols. */ 9369 9370 char *clean = xmalloc (strlen (name) + 1); 9371 char *s2 = clean; 9372 for (const char *s1 = name; *s1 != '\0'; s1++) 9373 if (ISALNUM (*s1) || *s1 == '_') 9374 *s2++ = *s1; 9375 *s2 = '\0'; 9376 9377 char *buf = xasprintf ("__load_start_%s", clean); 9378 lang_add_assignment (exp_provide (buf, 9379 exp_nameop (LOADADDR, name), 9380 false)); 9381 9382 buf = xasprintf ("__load_stop_%s", clean); 9383 lang_add_assignment (exp_provide (buf, 9384 exp_binop ('+', 9385 exp_nameop (LOADADDR, name), 9386 exp_nameop (SIZEOF, name)), 9387 false)); 9388 9389 free (clean); 9390 } 9391 9392 /* Finish an overlay. If there are any overlay wide settings, this 9393 looks through all the sections in the overlay and sets them. */ 9394 9395 void 9396 lang_leave_overlay (etree_type *lma_expr, 9397 int nocrossrefs, 9398 fill_type *fill, 9399 const char *memspec, 9400 lang_output_section_phdr_list *phdrs, 9401 const char *lma_memspec) 9402 { 9403 lang_memory_region_type *region; 9404 lang_memory_region_type *lma_region; 9405 struct overlay_list *l; 9406 lang_nocrossref_type *nocrossref; 9407 9408 lang_get_regions (®ion, &lma_region, 9409 memspec, lma_memspec, 9410 lma_expr != NULL, false); 9411 9412 nocrossref = NULL; 9413 9414 /* After setting the size of the last section, set '.' to end of the 9415 overlay region. */ 9416 if (overlay_list != NULL) 9417 { 9418 overlay_list->os->update_dot = 1; 9419 overlay_list->os->update_dot_tree 9420 = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false); 9421 } 9422 9423 l = overlay_list; 9424 while (l != NULL) 9425 { 9426 struct overlay_list *next; 9427 9428 if (fill != NULL && l->os->fill == NULL) 9429 l->os->fill = fill; 9430 9431 l->os->region = region; 9432 l->os->lma_region = lma_region; 9433 9434 /* The first section has the load address specified in the 9435 OVERLAY statement. The rest are worked out from that. 9436 The base address is not needed (and should be null) if 9437 an LMA region was specified. */ 9438 if (l->next == 0) 9439 { 9440 l->os->load_base = lma_expr; 9441 l->os->sectype = first_overlay_section; 9442 } 9443 if (phdrs != NULL && l->os->phdrs == NULL) 9444 l->os->phdrs = phdrs; 9445 9446 if (nocrossrefs) 9447 { 9448 lang_nocrossref_type *nc; 9449 9450 nc = stat_alloc (sizeof *nc); 9451 nc->name = l->os->name; 9452 nc->next = nocrossref; 9453 nocrossref = nc; 9454 } 9455 9456 next = l->next; 9457 free (l); 9458 l = next; 9459 } 9460 9461 if (nocrossref != NULL) 9462 lang_add_nocrossref (nocrossref); 9463 9464 overlay_vma = NULL; 9465 overlay_list = NULL; 9466 overlay_max = NULL; 9467 overlay_subalign = NULL; 9468 } 9469 9470 /* Version handling. This is only useful for ELF. */ 9472 9473 /* If PREV is NULL, return first version pattern matching particular symbol. 9474 If PREV is non-NULL, return first version pattern matching particular 9475 symbol after PREV (previously returned by lang_vers_match). */ 9476 9477 static struct bfd_elf_version_expr * 9478 lang_vers_match (struct bfd_elf_version_expr_head *head, 9479 struct bfd_elf_version_expr *prev, 9480 const char *sym) 9481 { 9482 const char *c_sym; 9483 const char *cxx_sym = sym; 9484 const char *java_sym = sym; 9485 struct bfd_elf_version_expr *expr = NULL; 9486 enum demangling_styles curr_style; 9487 9488 curr_style = CURRENT_DEMANGLING_STYLE; 9489 cplus_demangle_set_style (no_demangling); 9490 c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS); 9491 if (!c_sym) 9492 c_sym = sym; 9493 cplus_demangle_set_style (curr_style); 9494 9495 if (head->mask & BFD_ELF_VERSION_CXX_TYPE) 9496 { 9497 cxx_sym = bfd_demangle (link_info.output_bfd, sym, 9498 DMGL_PARAMS | DMGL_ANSI); 9499 if (!cxx_sym) 9500 cxx_sym = sym; 9501 } 9502 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) 9503 { 9504 java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA); 9505 if (!java_sym) 9506 java_sym = sym; 9507 } 9508 9509 if (head->htab && (prev == NULL || prev->literal)) 9510 { 9511 struct bfd_elf_version_expr e; 9512 9513 switch (prev ? prev->mask : 0) 9514 { 9515 case 0: 9516 if (head->mask & BFD_ELF_VERSION_C_TYPE) 9517 { 9518 e.pattern = c_sym; 9519 expr = (struct bfd_elf_version_expr *) 9520 htab_find ((htab_t) head->htab, &e); 9521 while (expr && strcmp (expr->pattern, c_sym) == 0) 9522 if (expr->mask == BFD_ELF_VERSION_C_TYPE) 9523 goto out_ret; 9524 else 9525 expr = expr->next; 9526 } 9527 /* Fallthrough */ 9528 case BFD_ELF_VERSION_C_TYPE: 9529 if (head->mask & BFD_ELF_VERSION_CXX_TYPE) 9530 { 9531 e.pattern = cxx_sym; 9532 expr = (struct bfd_elf_version_expr *) 9533 htab_find ((htab_t) head->htab, &e); 9534 while (expr && strcmp (expr->pattern, cxx_sym) == 0) 9535 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) 9536 goto out_ret; 9537 else 9538 expr = expr->next; 9539 } 9540 /* Fallthrough */ 9541 case BFD_ELF_VERSION_CXX_TYPE: 9542 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) 9543 { 9544 e.pattern = java_sym; 9545 expr = (struct bfd_elf_version_expr *) 9546 htab_find ((htab_t) head->htab, &e); 9547 while (expr && strcmp (expr->pattern, java_sym) == 0) 9548 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) 9549 goto out_ret; 9550 else 9551 expr = expr->next; 9552 } 9553 /* Fallthrough */ 9554 default: 9555 break; 9556 } 9557 } 9558 9559 /* Finally, try the wildcards. */ 9560 if (prev == NULL || prev->literal) 9561 expr = head->remaining; 9562 else 9563 expr = prev->next; 9564 for (; expr; expr = expr->next) 9565 { 9566 const char *s; 9567 9568 if (!expr->pattern) 9569 continue; 9570 9571 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') 9572 break; 9573 9574 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) 9575 s = java_sym; 9576 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) 9577 s = cxx_sym; 9578 else 9579 s = c_sym; 9580 if (fnmatch (expr->pattern, s, 0) == 0) 9581 break; 9582 } 9583 9584 out_ret: 9585 if (c_sym != sym) 9586 free ((char *) c_sym); 9587 if (cxx_sym != sym) 9588 free ((char *) cxx_sym); 9589 if (java_sym != sym) 9590 free ((char *) java_sym); 9591 return expr; 9592 } 9593 9594 /* Return NULL if the PATTERN argument is a glob pattern, otherwise, 9595 return a pointer to the symbol name with any backslash quotes removed. */ 9596 9597 static const char * 9598 realsymbol (const char *pattern) 9599 { 9600 const char *p; 9601 bool changed = false, backslash = false; 9602 char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1); 9603 9604 for (p = pattern, s = symbol; *p != '\0'; ++p) 9605 { 9606 /* It is a glob pattern only if there is no preceding 9607 backslash. */ 9608 if (backslash) 9609 { 9610 /* Remove the preceding backslash. */ 9611 *(s - 1) = *p; 9612 backslash = false; 9613 changed = true; 9614 } 9615 else 9616 { 9617 if (*p == '?' || *p == '*' || *p == '[') 9618 { 9619 free (symbol); 9620 return NULL; 9621 } 9622 9623 *s++ = *p; 9624 backslash = *p == '\\'; 9625 } 9626 } 9627 9628 if (changed) 9629 { 9630 *s = '\0'; 9631 pattern = stat_strdup (symbol); 9632 } 9633 free (symbol); 9634 return pattern; 9635 } 9636 9637 /* This is called for each variable name or match expression. NEW_NAME is 9638 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob 9639 pattern to be matched against symbol names. */ 9640 9641 struct bfd_elf_version_expr * 9642 lang_new_vers_pattern (struct bfd_elf_version_expr *orig, 9643 const char *new_name, 9644 const char *lang, 9645 bool literal_p) 9646 { 9647 struct bfd_elf_version_expr *ret; 9648 9649 ret = stat_alloc (sizeof *ret); 9650 ret->next = orig; 9651 ret->symver = 0; 9652 ret->script = 0; 9653 ret->literal = true; 9654 ret->pattern = literal_p ? new_name : realsymbol (new_name); 9655 if (ret->pattern == NULL) 9656 { 9657 ret->pattern = new_name; 9658 ret->literal = false; 9659 } 9660 9661 if (lang == NULL || strcasecmp (lang, "C") == 0) 9662 ret->mask = BFD_ELF_VERSION_C_TYPE; 9663 else if (strcasecmp (lang, "C++") == 0) 9664 ret->mask = BFD_ELF_VERSION_CXX_TYPE; 9665 else if (strcasecmp (lang, "Java") == 0) 9666 ret->mask = BFD_ELF_VERSION_JAVA_TYPE; 9667 else 9668 { 9669 einfo (_("%X%P: unknown language `%s' in version information\n"), 9670 lang); 9671 ret->mask = BFD_ELF_VERSION_C_TYPE; 9672 } 9673 9674 return ldemul_new_vers_pattern (ret); 9675 } 9676 9677 /* This is called for each set of variable names and match 9678 expressions. */ 9679 9680 struct bfd_elf_version_tree * 9681 lang_new_vers_node (struct bfd_elf_version_expr *globals, 9682 struct bfd_elf_version_expr *locals) 9683 { 9684 struct bfd_elf_version_tree *ret; 9685 9686 ret = stat_alloc (sizeof (*ret)); 9687 memset (ret, 0, sizeof (*ret)); 9688 ret->globals.list = globals; 9689 ret->locals.list = locals; 9690 ret->match = lang_vers_match; 9691 ret->name_indx = (unsigned int) -1; 9692 return ret; 9693 } 9694 9695 /* This static variable keeps track of version indices. */ 9696 9697 static int version_index; 9698 9699 static hashval_t 9700 version_expr_head_hash (const void *p) 9701 { 9702 const struct bfd_elf_version_expr *e = 9703 (const struct bfd_elf_version_expr *) p; 9704 9705 return htab_hash_string (e->pattern); 9706 } 9707 9708 static int 9709 version_expr_head_eq (const void *p1, const void *p2) 9710 { 9711 const struct bfd_elf_version_expr *e1 = 9712 (const struct bfd_elf_version_expr *) p1; 9713 const struct bfd_elf_version_expr *e2 = 9714 (const struct bfd_elf_version_expr *) p2; 9715 9716 return strcmp (e1->pattern, e2->pattern) == 0; 9717 } 9718 9719 static void 9720 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) 9721 { 9722 size_t count = 0; 9723 struct bfd_elf_version_expr *e, *next; 9724 struct bfd_elf_version_expr **list_loc, **remaining_loc; 9725 9726 for (e = head->list; e; e = e->next) 9727 { 9728 if (e->literal) 9729 count++; 9730 head->mask |= e->mask; 9731 } 9732 9733 if (count) 9734 { 9735 head->htab = htab_create (count * 2, version_expr_head_hash, 9736 version_expr_head_eq, NULL); 9737 list_loc = &head->list; 9738 remaining_loc = &head->remaining; 9739 for (e = head->list; e; e = next) 9740 { 9741 next = e->next; 9742 if (!e->literal) 9743 { 9744 *remaining_loc = e; 9745 remaining_loc = &e->next; 9746 } 9747 else 9748 { 9749 void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT); 9750 9751 if (*loc) 9752 { 9753 struct bfd_elf_version_expr *e1, *last; 9754 9755 e1 = (struct bfd_elf_version_expr *) *loc; 9756 last = NULL; 9757 do 9758 { 9759 if (e1->mask == e->mask) 9760 { 9761 last = NULL; 9762 break; 9763 } 9764 last = e1; 9765 e1 = e1->next; 9766 } 9767 while (e1 && strcmp (e1->pattern, e->pattern) == 0); 9768 9769 if (last != NULL) 9770 { 9771 e->next = last->next; 9772 last->next = e; 9773 } 9774 } 9775 else 9776 { 9777 *loc = e; 9778 *list_loc = e; 9779 list_loc = &e->next; 9780 } 9781 } 9782 } 9783 *remaining_loc = NULL; 9784 *list_loc = head->remaining; 9785 } 9786 else 9787 head->remaining = head->list; 9788 } 9789 9790 /* This is called when we know the name and dependencies of the 9791 version. */ 9792 9793 void 9794 lang_register_vers_node (const char *name, 9795 struct bfd_elf_version_tree *version, 9796 struct bfd_elf_version_deps *deps) 9797 { 9798 struct bfd_elf_version_tree *t, **pp; 9799 struct bfd_elf_version_expr *e1; 9800 9801 if (name == NULL) 9802 name = ""; 9803 9804 if (link_info.version_info != NULL 9805 && (name[0] == '\0' || link_info.version_info->name[0] == '\0')) 9806 { 9807 einfo (_("%X%P: anonymous version tag cannot be combined" 9808 " with other version tags\n")); 9809 return; 9810 } 9811 9812 /* Make sure this node has a unique name. */ 9813 for (t = link_info.version_info; t != NULL; t = t->next) 9814 if (strcmp (t->name, name) == 0) 9815 einfo (_("%X%P: duplicate version tag `%s'\n"), name); 9816 9817 lang_finalize_version_expr_head (&version->globals); 9818 lang_finalize_version_expr_head (&version->locals); 9819 9820 /* Check the global and local match names, and make sure there 9821 aren't any duplicates. */ 9822 9823 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) 9824 { 9825 for (t = link_info.version_info; t != NULL; t = t->next) 9826 { 9827 struct bfd_elf_version_expr *e2; 9828 9829 if (t->locals.htab && e1->literal) 9830 { 9831 e2 = (struct bfd_elf_version_expr *) 9832 htab_find ((htab_t) t->locals.htab, e1); 9833 while (e2 && strcmp (e1->pattern, e2->pattern) == 0) 9834 { 9835 if (e1->mask == e2->mask) 9836 einfo (_("%X%P: duplicate expression `%s'" 9837 " in version information\n"), e1->pattern); 9838 e2 = e2->next; 9839 } 9840 } 9841 else if (!e1->literal) 9842 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) 9843 if (strcmp (e1->pattern, e2->pattern) == 0 9844 && e1->mask == e2->mask) 9845 einfo (_("%X%P: duplicate expression `%s'" 9846 " in version information\n"), e1->pattern); 9847 } 9848 } 9849 9850 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) 9851 { 9852 for (t = link_info.version_info; t != NULL; t = t->next) 9853 { 9854 struct bfd_elf_version_expr *e2; 9855 9856 if (t->globals.htab && e1->literal) 9857 { 9858 e2 = (struct bfd_elf_version_expr *) 9859 htab_find ((htab_t) t->globals.htab, e1); 9860 while (e2 && strcmp (e1->pattern, e2->pattern) == 0) 9861 { 9862 if (e1->mask == e2->mask) 9863 einfo (_("%X%P: duplicate expression `%s'" 9864 " in version information\n"), 9865 e1->pattern); 9866 e2 = e2->next; 9867 } 9868 } 9869 else if (!e1->literal) 9870 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) 9871 if (strcmp (e1->pattern, e2->pattern) == 0 9872 && e1->mask == e2->mask) 9873 einfo (_("%X%P: duplicate expression `%s'" 9874 " in version information\n"), e1->pattern); 9875 } 9876 } 9877 9878 version->deps = deps; 9879 version->name = name; 9880 if (name[0] != '\0') 9881 { 9882 ++version_index; 9883 version->vernum = version_index; 9884 } 9885 else 9886 version->vernum = 0; 9887 9888 for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next) 9889 ; 9890 *pp = version; 9891 } 9892 9893 /* This is called when we see a version dependency. */ 9894 9895 struct bfd_elf_version_deps * 9896 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) 9897 { 9898 struct bfd_elf_version_deps *ret; 9899 struct bfd_elf_version_tree *t; 9900 9901 ret = stat_alloc (sizeof *ret); 9902 ret->next = list; 9903 9904 for (t = link_info.version_info; t != NULL; t = t->next) 9905 { 9906 if (strcmp (t->name, name) == 0) 9907 { 9908 ret->version_needed = t; 9909 return ret; 9910 } 9911 } 9912 9913 einfo (_("%X%P: unable to find version dependency `%s'\n"), name); 9914 9915 ret->version_needed = NULL; 9916 return ret; 9917 } 9918 9919 static void 9920 lang_do_version_exports_section (void) 9921 { 9922 struct bfd_elf_version_expr *greg = NULL, *lreg; 9923 9924 LANG_FOR_EACH_INPUT_STATEMENT (is) 9925 { 9926 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); 9927 char *contents, *p; 9928 bfd_size_type len; 9929 9930 if (sec == NULL) 9931 continue; 9932 9933 len = sec->size; 9934 contents = stat_alloc (len); 9935 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) 9936 einfo (_("%X%P: unable to read .exports section contents\n"), sec); 9937 9938 p = contents; 9939 while (p < contents + len) 9940 { 9941 greg = lang_new_vers_pattern (greg, p, NULL, false); 9942 p = strchr (p, '\0') + 1; 9943 } 9944 9945 /* Do not include this section in the link. */ 9946 sec->flags |= SEC_EXCLUDE | SEC_KEEP; 9947 } 9948 9949 lreg = lang_new_vers_pattern (NULL, "*", NULL, false); 9950 lang_register_vers_node (command_line.version_exports_section, 9951 lang_new_vers_node (greg, lreg), NULL); 9952 } 9953 9954 /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially 9955 called with UPDATE_REGIONS_P set to FALSE, in this case no errors are 9956 thrown, however, references to symbols in the origin and length fields 9957 will be pushed into the symbol table, this allows PROVIDE statements to 9958 then provide these symbols. This function is called a second time with 9959 UPDATE_REGIONS_P set to TRUE, this time the we update the actual region 9960 data structures, and throw errors if missing symbols are encountered. */ 9961 9962 static void 9963 lang_do_memory_regions (bool update_regions_p) 9964 { 9965 lang_memory_region_type *r = lang_memory_region_list; 9966 9967 for (; r != NULL; r = r->next) 9968 { 9969 if (r->origin_exp) 9970 { 9971 exp_fold_tree_no_dot (r->origin_exp, NULL); 9972 if (update_regions_p) 9973 { 9974 if (expld.result.valid_p) 9975 { 9976 r->origin = expld.result.value; 9977 r->current = r->origin; 9978 } 9979 else 9980 einfo (_("%P: invalid origin for memory region %s\n"), 9981 r->name_list.name); 9982 } 9983 } 9984 if (r->length_exp) 9985 { 9986 exp_fold_tree_no_dot (r->length_exp, NULL); 9987 if (update_regions_p) 9988 { 9989 if (expld.result.valid_p) 9990 r->length = expld.result.value; 9991 else 9992 einfo (_("%P: invalid length for memory region %s\n"), 9993 r->name_list.name); 9994 } 9995 } 9996 } 9997 } 9998 9999 void 10000 lang_add_unique (const char *name) 10001 { 10002 struct unique_sections *ent; 10003 10004 for (ent = unique_section_list; ent; ent = ent->next) 10005 if (strcmp (ent->name, name) == 0) 10006 return; 10007 10008 ent = stat_alloc (sizeof *ent); 10009 ent->name = stat_strdup (name); 10010 ent->next = unique_section_list; 10011 unique_section_list = ent; 10012 } 10013 10014 /* Append the list of dynamic symbols to the existing one. */ 10015 10016 void 10017 lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p, 10018 struct bfd_elf_version_expr *dynamic) 10019 { 10020 if (*list_p) 10021 { 10022 struct bfd_elf_version_expr *tail; 10023 for (tail = dynamic; tail->next != NULL; tail = tail->next) 10024 ; 10025 tail->next = (*list_p)->head.list; 10026 (*list_p)->head.list = dynamic; 10027 } 10028 else 10029 { 10030 struct bfd_elf_dynamic_list *d; 10031 10032 d = stat_alloc (sizeof (*d)); 10033 memset (d, 0, sizeof (*d)); 10034 d->head.list = dynamic; 10035 d->match = lang_vers_match; 10036 *list_p = d; 10037 } 10038 } 10039 10040 /* Append the list of C++ typeinfo dynamic symbols to the existing 10041 one. */ 10042 10043 void 10044 lang_append_dynamic_list_cpp_typeinfo (void) 10045 { 10046 const char *symbols[] = 10047 { 10048 "typeinfo name for*", 10049 "typeinfo for*" 10050 }; 10051 struct bfd_elf_version_expr *dynamic = NULL; 10052 unsigned int i; 10053 10054 for (i = 0; i < ARRAY_SIZE (symbols); i++) 10055 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", 10056 false); 10057 10058 lang_append_dynamic_list (&link_info.dynamic_list, dynamic); 10059 } 10060 10061 /* Append the list of C++ operator new and delete dynamic symbols to the 10062 existing one. */ 10063 10064 void 10065 lang_append_dynamic_list_cpp_new (void) 10066 { 10067 const char *symbols[] = 10068 { 10069 "operator new*", 10070 "operator delete*" 10071 }; 10072 struct bfd_elf_version_expr *dynamic = NULL; 10073 unsigned int i; 10074 10075 for (i = 0; i < ARRAY_SIZE (symbols); i++) 10076 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++", 10077 false); 10078 10079 lang_append_dynamic_list (&link_info.dynamic_list, dynamic); 10080 } 10081 10082 /* Scan a space and/or comma separated string of features. */ 10083 10084 void 10085 lang_ld_feature (char *str) 10086 { 10087 char *p, *q; 10088 10089 p = str; 10090 while (*p) 10091 { 10092 char sep; 10093 while (*p == ',' || ISSPACE (*p)) 10094 ++p; 10095 if (!*p) 10096 break; 10097 q = p + 1; 10098 while (*q && *q != ',' && !ISSPACE (*q)) 10099 ++q; 10100 sep = *q; 10101 *q = 0; 10102 if (strcasecmp (p, "SANE_EXPR") == 0) 10103 config.sane_expr = true; 10104 else 10105 einfo (_("%X%P: unknown feature `%s'\n"), p); 10106 *q = sep; 10107 p = q; 10108 } 10109 } 10110 10111 /* Pretty print memory amount. */ 10112 10113 static void 10114 lang_print_memory_size (uint64_t sz) 10115 { 10116 if (sz == 0) 10117 printf (" %10" PRIu64 " B", sz); 10118 else if ((sz & 0x3fffffff) == 0) 10119 printf ("%10" PRIu64 " GB", sz >> 30); 10120 else if ((sz & 0xfffff) == 0) 10121 printf ("%10" PRIu64 " MB", sz >> 20); 10122 else if ((sz & 0x3ff) == 0) 10123 printf ("%10" PRIu64 " KB", sz >> 10); 10124 else 10125 printf (" %10" PRIu64 " B", sz); 10126 } 10127 10128 /* Implement --print-memory-usage: disply per region memory usage. */ 10129 10130 void 10131 lang_print_memory_usage (void) 10132 { 10133 lang_memory_region_type *r; 10134 10135 printf ("Memory region Used Size Region Size %%age Used\n"); 10136 for (r = lang_memory_region_list; r->next != NULL; r = r->next) 10137 { 10138 bfd_vma used_length = r->current - r->origin; 10139 10140 printf ("%16s: ",r->name_list.name); 10141 lang_print_memory_size (used_length); 10142 lang_print_memory_size (r->length); 10143 10144 if (r->length != 0) 10145 { 10146 double percent = used_length * 100.0 / r->length; 10147 printf (" %6.2f%%", percent); 10148 } 10149 printf ("\n"); 10150 } 10151 } 10152 10153 static void 10154 cmdline_lists_init (void) 10155 { 10156 cmdline_object_only_file_list.tail 10157 = &cmdline_object_only_file_list.head; 10158 cmdline_object_only_archive_list.tail 10159 = &cmdline_object_only_archive_list.head; 10160 cmdline_temp_object_only_list.tail 10161 = &cmdline_temp_object_only_list.head; 10162 } 10163 10164 /* Allocate an item with TYPE and DATA. */ 10165 10166 static cmdline_union_type * 10167 cmdline_list_new (cmdline_enum_type type, void *data) 10168 { 10169 cmdline_union_type *new_opt; 10170 10171 new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt)); 10172 new_opt->header.type = type; 10173 switch (type) 10174 { 10175 default: 10176 break; 10177 case cmdline_is_file_enum: 10178 new_opt->file.filename = (const char *) data; 10179 break; 10180 case cmdline_is_bfd_enum: 10181 new_opt->abfd.abfd = (bfd *) data; 10182 break; 10183 } 10184 return new_opt; 10185 } 10186 10187 /* Append an item with TYPE and DATA to LIST. */ 10188 10189 static void 10190 cmdline_list_append (cmdline_list_type *list, cmdline_enum_type type, 10191 void *data) 10192 { 10193 cmdline_union_type *new_opt = cmdline_list_new (type, data); 10194 new_opt->header.next = NULL; 10195 *list->tail = new_opt; 10196 list->tail = &new_opt->header.next; 10197 } 10198 10199 static void 10200 print_cmdline_list (cmdline_union_type *c) 10201 { 10202 for (; c != NULL; c = c->header.next) 10203 switch (c->header.type) 10204 { 10205 default: 10206 abort (); 10207 case cmdline_is_file_enum: 10208 info_msg (" %s", c->file.filename); 10209 break; 10210 case cmdline_is_bfd_enum: 10211 info_msg (" [%B]", c->abfd.abfd); 10212 break; 10213 } 10214 10215 info_msg ("\n"); 10216 } 10217 10218 /* Return TRUE if ABFD is on cmdline_object_only_archive_list. */ 10219 10220 static bool 10221 cmdline_on_object_only_archive_list_p (bfd *abfd) 10222 { 10223 cmdline_union_type *c, *next; 10224 bfd *archive, *obfd, *oarchive; 10225 ufile_ptr origin = abfd->origin; 10226 10227 archive = abfd->my_archive; 10228 for (c = cmdline_object_only_archive_list.head; c != NULL; c = next) 10229 { 10230 if (c->header.type != cmdline_is_bfd_enum) 10231 abort (); 10232 10233 next = c->header.next; 10234 obfd = c->abfd.abfd; 10235 oarchive = obfd->my_archive; 10236 10237 /* The list is grouped by archive file name and sorted by member 10238 origin. */ 10239 if (strcmp (archive->filename, oarchive->filename) != 0) 10240 continue; 10241 10242 if (origin == obfd->origin) 10243 return true; 10244 else if (origin < obfd->origin) 10245 return false; 10246 } 10247 10248 return false; 10249 } 10250 10251 /* Append an item with TYPE and DATA to cmdline_object_only_file_list 10252 or cmdline_object_only_archive_list if needed. */ 10253 10254 static void 10255 cmdline_object_only_list_append (cmdline_enum_type type, void *data) 10256 { 10257 cmdline_union_type *c; 10258 cmdline_union_type *new_opt, *next, **prev; 10259 bfd *abfd, *archive; 10260 bfd *obfd, *oarchive; 10261 bfd *nbfd, *narchive; 10262 ufile_ptr origin, norigin; 10263 10264 /* Put it on cmdline_object_only_file_list if it isn't an archive 10265 member. */ 10266 switch (type) 10267 { 10268 default: 10269 abort (); 10270 case cmdline_is_bfd_enum: 10271 abfd = (bfd *) data; 10272 archive = abfd->my_archive; 10273 if (archive) 10274 break; 10275 /* Fallthru */ 10276 case cmdline_is_file_enum: 10277 cmdline_list_append (&cmdline_object_only_file_list, type, data); 10278 return; 10279 } 10280 10281 /* Put archive member on cmdline_object_only_archive_list and sort 10282 the list by archive name and archive member origin. */ 10283 new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt)); 10284 new_opt->header.type = cmdline_is_bfd_enum; 10285 new_opt->header.next = NULL; 10286 new_opt->abfd.abfd = (bfd *) data; 10287 10288 c = cmdline_object_only_archive_list.head; 10289 if (c == NULL) 10290 { 10291 cmdline_object_only_archive_list.head = new_opt; 10292 cmdline_object_only_archive_list.tail = &new_opt->header.next; 10293 return; 10294 } 10295 10296 prev = NULL; 10297 origin = abfd->origin; 10298 for (; c != NULL; c = next) 10299 { 10300 if (c->header.type != cmdline_is_bfd_enum) 10301 abort (); 10302 10303 next = c->header.next; 10304 10305 obfd = c->abfd.abfd; 10306 oarchive = obfd->my_archive; 10307 10308 if (strcmp (archive->filename, oarchive->filename) == 0) 10309 { 10310 bool after; 10311 10312 if (origin < obfd->origin) 10313 { 10314 /* Insert it before the current. */ 10315 new_opt->header.next = c; 10316 if (prev) 10317 *prev = new_opt; 10318 else 10319 cmdline_object_only_archive_list.head = new_opt; 10320 return; 10321 } 10322 10323 after = true; 10324 10325 /* Check origin. */ 10326 while (next) 10327 { 10328 if (next->header.type != cmdline_is_bfd_enum) 10329 abort (); 10330 10331 nbfd = next->abfd.abfd; 10332 norigin = nbfd->origin; 10333 if (origin > norigin) 10334 { 10335 /* Insert it after NEXT. */ 10336 break; 10337 } 10338 10339 narchive = nbfd->my_archive; 10340 if (strcmp (archive->filename, narchive->filename) != 0) 10341 { 10342 /* Insert it befor NEXT. */ 10343 after = false; 10344 break; 10345 } 10346 10347 c = next; 10348 next = next->header.next; 10349 } 10350 10351 if (after && next) 10352 { 10353 c = next; 10354 next = next->header.next; 10355 } 10356 10357 if (*cmdline_object_only_archive_list.tail == c->header.next) 10358 cmdline_object_only_archive_list.tail 10359 = &new_opt->header.next; 10360 10361 prev = &c->header.next; 10362 new_opt->header.next = next; 10363 *prev = new_opt; 10364 return; 10365 } 10366 10367 prev = &c->header.next; 10368 } 10369 10370 *cmdline_object_only_archive_list.tail = new_opt; 10371 cmdline_object_only_archive_list.tail = &new_opt->header.next; 10372 } 10373 10374 /* Get object-only input files. */ 10375 10376 static void 10377 cmdline_get_object_only_input_files (void) 10378 { 10379 cmdline_union_type *c, *next; 10380 bfd *abfd, *archive; 10381 bfd *nbfd, *narchive; 10382 10383 /* Add files first. */ 10384 for (c = cmdline_object_only_file_list.head; 10385 c != NULL; c = c->header.next) 10386 switch (c->header.type) 10387 { 10388 default: 10389 abort (); 10390 case cmdline_is_file_enum: 10391 lang_add_input_file (c->file.filename, 10392 lang_input_file_is_file_enum, NULL); 10393 break; 10394 case cmdline_is_bfd_enum: 10395 abfd = c->abfd.abfd; 10396 if (abfd->my_archive) 10397 abort (); 10398 lang_add_input_file (abfd->filename, 10399 lang_input_file_is_file_enum, NULL); 10400 break; 10401 } 10402 10403 /* Add archive members next. */ 10404 for (c = cmdline_object_only_archive_list.head; c != NULL; c = next) 10405 { 10406 if (c->header.type != cmdline_is_bfd_enum) 10407 abort (); 10408 10409 next = c->header.next; 10410 10411 abfd = c->abfd.abfd; 10412 archive = abfd->my_archive; 10413 10414 /* Add the first archive of the archive member group. */ 10415 lang_add_input_file (archive->filename, 10416 lang_input_file_is_file_enum, NULL); 10417 10418 /* Skip the rest members in the archive member group. */ 10419 do 10420 { 10421 if (!next) 10422 break; 10423 10424 if (next->header.type != cmdline_is_bfd_enum) 10425 abort (); 10426 10427 next = next->header.next; 10428 if (!next) 10429 break; 10430 nbfd = next->abfd.abfd; 10431 narchive = nbfd->my_archive; 10432 } 10433 while (strcmp (archive->filename, narchive->filename) == 0); 10434 } 10435 } 10436 10437 struct cmdline_arg 10438 { 10439 bfd *obfd; 10440 asymbol **isympp; 10441 int status; 10442 }; 10443 10444 /* Create a section in OBFD with the same 10445 name and attributes as ISECTION in IBFD. */ 10446 10447 static void 10448 setup_section (bfd *ibfd, sec_ptr isection, void *p) 10449 { 10450 struct cmdline_arg *arg = (struct cmdline_arg *) p; 10451 bfd *obfd = arg->obfd; 10452 asymbol **isympp = arg->isympp; 10453 const char *name = isection->name; 10454 sec_ptr osection; 10455 const char *err; 10456 10457 /* Skip the object-only section. */ 10458 if (ibfd->object_only_section == isection) 10459 return; 10460 10461 /* If we have already failed earlier on, do not keep on generating 10462 complaints now. */ 10463 if (arg->status) 10464 return; 10465 10466 osection = bfd_make_section_anyway_with_flags (obfd, name, 10467 isection->flags); 10468 10469 if (osection == NULL) 10470 { 10471 err = _("failed to create output section"); 10472 goto loser; 10473 } 10474 10475 osection->size = isection->size; 10476 osection->vma = isection->vma; 10477 osection->lma = isection->lma; 10478 osection->alignment_power = isection->alignment_power; 10479 10480 /* Copy merge entity size. */ 10481 osection->entsize = isection->entsize; 10482 10483 /* This used to be mangle_section; we do here to avoid using 10484 bfd_get_section_by_name since some formats allow multiple 10485 sections with the same name. */ 10486 isection->output_section = osection; 10487 isection->output_offset = 0; 10488 10489 if ((isection->flags & SEC_GROUP) != 0) 10490 { 10491 asymbol *gsym = bfd_group_signature (isection, isympp); 10492 10493 if (gsym != NULL) 10494 { 10495 gsym->flags |= BSF_KEEP; 10496 if (ibfd->xvec->flavour == bfd_target_elf_flavour) 10497 elf_group_id (isection) = gsym; 10498 } 10499 } 10500 10501 /* Allow the BFD backend to copy any private data it understands 10502 from the input section to the output section. */ 10503 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection, NULL)) 10504 { 10505 err = _("failed to copy private data"); 10506 goto loser; 10507 } 10508 10509 /* All went well. */ 10510 return; 10511 10512 loser: 10513 arg->status = 1; 10514 fatal (_("%P: setup_section: %s: %s\n"), err, name); 10515 } 10516 10517 /* Copy the data of input section ISECTION of IBFD 10518 to an output section with the same name in OBFD. 10519 If stripping then don't copy any relocation info. */ 10520 10521 static void 10522 copy_section (bfd *ibfd, sec_ptr isection, void *p) 10523 { 10524 struct cmdline_arg *arg = (struct cmdline_arg *) p; 10525 bfd *obfd = arg->obfd; 10526 asymbol **isympp = arg->isympp; 10527 arelent **relpp; 10528 long relcount; 10529 sec_ptr osection; 10530 bfd_size_type size; 10531 long relsize; 10532 flagword flags; 10533 const char *err; 10534 10535 /* Skip the object-only section. */ 10536 if (ibfd->object_only_section == isection) 10537 return; 10538 10539 /* If we have already failed earlier on, do not keep on generating 10540 complaints now. */ 10541 if (arg->status) 10542 return; 10543 10544 flags = bfd_section_flags (isection); 10545 if ((flags & SEC_GROUP) != 0) 10546 return; 10547 10548 osection = isection->output_section; 10549 size = bfd_section_size (isection); 10550 10551 if (size == 0 || osection == 0) 10552 return; 10553 10554 relsize = bfd_get_reloc_upper_bound (ibfd, isection); 10555 10556 if (relsize < 0) 10557 { 10558 /* Do not complain if the target does not support relocations. */ 10559 if (relsize == -1 10560 && bfd_get_error () == bfd_error_invalid_operation) 10561 relsize = 0; 10562 else 10563 { 10564 err = bfd_errmsg (bfd_get_error ()); 10565 goto loser; 10566 } 10567 } 10568 10569 if (relsize == 0) 10570 bfd_set_reloc (obfd, osection, NULL, 0); 10571 else 10572 { 10573 relpp = (arelent **) xmalloc (relsize); 10574 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); 10575 if (relcount < 0) 10576 { 10577 err = _("relocation count is negative"); 10578 goto loser; 10579 } 10580 10581 bfd_set_reloc (obfd, osection, 10582 relcount == 0 ? NULL : relpp, relcount); 10583 if (relcount == 0) 10584 free (relpp); 10585 } 10586 10587 if (bfd_section_flags (isection) & SEC_HAS_CONTENTS) 10588 { 10589 bfd_byte *memhunk = NULL; 10590 10591 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)) 10592 { 10593 err = bfd_errmsg (bfd_get_error ()); 10594 goto loser; 10595 } 10596 10597 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size)) 10598 { 10599 err = bfd_errmsg (bfd_get_error ()); 10600 goto loser; 10601 } 10602 free (memhunk); 10603 } 10604 10605 /* All went well. */ 10606 return; 10607 10608 loser: 10609 fatal (_("%P: copy_section: %s: %s\n"), err, isection->name); 10610 } 10611 /* Open the temporary bfd created in the same directory as PATH. */ 10612 10613 static bfd * 10614 cmdline_fopen_temp (const char *path, const char *target, 10615 const char *mode) 10616 { 10617 #define template "ldXXXXXX" 10618 const char *slash = strrchr (path, '/'); 10619 char *tmpname; 10620 size_t len; 10621 int fd; 10622 10623 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 10624 { 10625 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */ 10626 char *bslash = strrchr (path, '\\'); 10627 10628 if (slash == NULL || (bslash != NULL && bslash > slash)) 10629 slash = bslash; 10630 if (slash == NULL && path[0] != '\0' && path[1] == ':') 10631 slash = path + 1; 10632 } 10633 #endif 10634 10635 if (slash != (char *) NULL) 10636 { 10637 len = slash - path; 10638 tmpname = (char *) xmalloc (len + sizeof (template) + 2); 10639 memcpy (tmpname, path, len); 10640 10641 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 10642 /* If tmpname is "X:", appending a slash will make it a root 10643 directory on drive X, which is NOT the same as the current 10644 directory on drive X. */ 10645 if (len == 2 && tmpname[1] == ':') 10646 tmpname[len++] = '.'; 10647 #endif 10648 tmpname[len++] = '/'; 10649 } 10650 else 10651 { 10652 tmpname = (char *) xmalloc (sizeof (template)); 10653 len = 0; 10654 } 10655 10656 memcpy (tmpname + len, template, sizeof (template)); 10657 #undef template 10658 10659 #ifdef HAVE_MKSTEMP 10660 fd = mkstemp (tmpname); 10661 #else 10662 tmpname = mktemp (tmpname); 10663 if (tmpname == NULL) 10664 return NULL; 10665 fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600); 10666 #endif 10667 if (fd == -1) 10668 return NULL; 10669 return bfd_fopen (tmpname, target, mode, fd); 10670 } 10671 10672 /* Add the object-only section. */ 10673 10674 static void 10675 cmdline_add_object_only_section (bfd_byte *contents, size_t size) 10676 { 10677 bfd_vma start; 10678 flagword flags; 10679 enum bfd_architecture iarch; 10680 unsigned int imach; 10681 long symcount; 10682 long symsize; 10683 asymbol **isympp = NULL; 10684 asymbol **osympp = NULL; 10685 bfd *obfd = NULL, *ibfd; 10686 const char *err; 10687 struct arg 10688 { 10689 bfd *obfd; 10690 asymbol **isympp; 10691 int status; 10692 } arg; 10693 char **matching; 10694 char *ofilename = NULL; 10695 asection *sec; 10696 10697 ibfd = bfd_openr (output_filename, output_target); 10698 if (!ibfd) 10699 { 10700 err = bfd_errmsg (bfd_get_error ()); 10701 goto loser; 10702 } 10703 10704 if (!bfd_check_format_matches (ibfd, bfd_object, &matching)) 10705 { 10706 err = bfd_errmsg (bfd_get_error ()); 10707 goto loser; 10708 } 10709 10710 obfd = cmdline_fopen_temp (output_filename, output_target, FOPEN_WB); 10711 if (!obfd) 10712 { 10713 err = bfd_errmsg (bfd_get_error ()); 10714 goto loser; 10715 } 10716 /* To be used after bfd_close (). */ 10717 ofilename = xstrdup (bfd_get_filename (obfd)); 10718 10719 if (!bfd_set_format (obfd, bfd_object)) 10720 { 10721 err = bfd_errmsg (bfd_get_error ()); 10722 goto loser; 10723 } 10724 10725 /* Copy the start address, flags and architecture of input file to 10726 output file. */ 10727 flags = bfd_get_file_flags (ibfd); 10728 start = bfd_get_start_address (ibfd); 10729 iarch = bfd_get_arch (ibfd); 10730 imach = bfd_get_mach (ibfd); 10731 if (!bfd_set_start_address (obfd, start) 10732 || !bfd_set_file_flags (obfd, flags) 10733 || !bfd_set_arch_mach (obfd, iarch, imach)) 10734 { 10735 err = bfd_errmsg (bfd_get_error ()); 10736 goto loser; 10737 } 10738 10739 symsize = bfd_get_symtab_upper_bound (ibfd); 10740 if (symsize < 0) 10741 { 10742 err = bfd_errmsg (bfd_get_error ()); 10743 goto loser; 10744 } 10745 10746 isympp = (asymbol **) xmalloc (symsize); 10747 symcount = bfd_canonicalize_symtab (ibfd, isympp); 10748 if (symcount < 0) 10749 { 10750 err = bfd_errmsg (bfd_get_error ()); 10751 goto loser; 10752 } 10753 10754 arg.obfd = obfd; 10755 arg.isympp = isympp; 10756 arg.status = 0; 10757 10758 /* BFD mandates that all output sections be created and sizes set before 10759 any output is done. Thus, we traverse all sections multiple times. */ 10760 bfd_map_over_sections (ibfd, setup_section, &arg); 10761 10762 if (arg.status) 10763 { 10764 err = _("error setting up sections"); 10765 goto loser; 10766 } 10767 10768 /* Allow the BFD backend to copy any private data it understands 10769 from the input section to the output section. */ 10770 if (! bfd_copy_private_header_data (ibfd, obfd)) 10771 { 10772 err = _("error copying private header data"); 10773 goto loser; 10774 } 10775 10776 /* Create the object-only section. */ 10777 sec = bfd_make_section_with_flags (obfd, 10778 GNU_OBJECT_ONLY_SECTION_NAME, 10779 (SEC_HAS_CONTENTS 10780 | SEC_READONLY 10781 | SEC_DATA 10782 | SEC_LINKER_CREATED)); 10783 if (sec == NULL) 10784 { 10785 err = _("can't create object-only section"); 10786 goto loser; 10787 } 10788 10789 if (! bfd_set_section_size (sec, size)) 10790 { 10791 err = _("can't set object-only section size"); 10792 goto loser; 10793 } 10794 10795 if (ibfd->object_only_section) 10796 { 10797 /* Filter out the object-only section symbol. */ 10798 long src_count = 0, dst_count = 0; 10799 asymbol **from, **to; 10800 10801 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *)); 10802 from = isympp; 10803 to = osympp; 10804 for (; src_count < symcount; src_count++) 10805 { 10806 asymbol *sym = from[src_count]; 10807 if (bfd_asymbol_section (sym) != ibfd->object_only_section) 10808 to[dst_count++] = sym; 10809 } 10810 to[dst_count] = NULL; 10811 symcount = dst_count; 10812 bfd_set_symtab (obfd, osympp, symcount); 10813 } 10814 else 10815 bfd_set_symtab (obfd, isympp, symcount); 10816 10817 /* This has to happen after the symbol table has been set. */ 10818 bfd_map_over_sections (ibfd, copy_section, &arg); 10819 10820 if (arg.status) 10821 { 10822 err = _("error copying sections"); 10823 goto loser; 10824 } 10825 10826 /* Copy the object-only section to the output. */ 10827 if (! bfd_set_section_contents (obfd, sec, contents, 0, size)) 10828 { 10829 err = _("error adding object-only section"); 10830 goto loser; 10831 } 10832 10833 /* Allow the BFD backend to copy any private data it understands 10834 from the input BFD to the output BFD. This is done last to 10835 permit the routine to look at the filtered symbol table, which is 10836 important for the ECOFF code at least. */ 10837 if (! bfd_copy_private_bfd_data (ibfd, obfd)) 10838 { 10839 err = _("error copying private BFD data"); 10840 goto loser; 10841 } 10842 10843 if (!bfd_close (obfd)) 10844 { 10845 unlink (ofilename); 10846 fatal (_("%P: failed to finish output with object-only section\n")); 10847 } 10848 10849 /* ibfd needs to be closed *after* obfd, otherwise ld may crash with a 10850 segmentation fault. */ 10851 if (!bfd_close (ibfd)) 10852 einfo (_("%P%F: failed to close input\n")); 10853 10854 /* Must be freed after bfd_close (). */ 10855 free (isympp); 10856 free (osympp); 10857 10858 /* Must unlink to ensure rename works on Windows. */ 10859 if (unlink (output_filename) && errno != ENOENT) 10860 einfo (_("%P%F: failed to unlink %s\n"), output_filename); 10861 10862 if (rename (ofilename, output_filename)) 10863 { 10864 unlink (ofilename); 10865 fatal (_("%P: failed to rename output with object-only section\n")); 10866 } 10867 10868 free (ofilename); 10869 return; 10870 10871 loser: 10872 if (obfd) 10873 bfd_close (obfd); 10874 /* ibfd needs to be closed *after* obfd, otherwise ld may crash with a 10875 segmentation fault. */ 10876 if (ibfd) 10877 bfd_close (ibfd); 10878 free (isympp); 10879 free (osympp); 10880 if (ofilename) 10881 { 10882 unlink (ofilename); 10883 free (ofilename); 10884 } 10885 fatal (_("%P: failed to add object-only section: %s\n"), err); 10886 } 10887 10888 /* Emit the final output with object-only section. */ 10889 10890 void 10891 cmdline_emit_object_only_section (void) 10892 { 10893 const char *saved_output_filename = output_filename; 10894 int fd; 10895 size_t size, off; 10896 bfd_byte *contents; 10897 struct stat st; 10898 10899 /* Get a temporary object-only file. */ 10900 output_filename = make_temp_file (".obj-only.o"); 10901 10902 had_output_filename = false; 10903 link_info.input_bfds = NULL; 10904 link_info.input_bfds_tail = &link_info.input_bfds; 10905 10906 lang_init (true); 10907 ldexp_init (true); 10908 10909 /* Allow lang_add_section to add new sections. */ 10910 map_head_is_link_order = false; 10911 10912 /* Set up the object-only output. */ 10913 lang_final (); 10914 10915 /* Open the object-only file for output. */ 10916 lang_for_each_statement (ldlang_open_output); 10917 10918 ldemul_create_output_section_statements (); 10919 10920 if (!bfd_section_already_linked_table_init ()) 10921 fatal (_("%P: Failed to create hash table\n")); 10922 10923 /* Call cmdline_on_object_only_archive_list_p to check which member 10924 should be loaded. */ 10925 input_flags.whole_archive = true; 10926 10927 /* Set it to avoid adding more to cmdline lists. */ 10928 config.emitting_gnu_object_only = true; 10929 10930 /* Get object-only input files. */ 10931 cmdline_get_object_only_input_files (); 10932 10933 /* Open object-only input files. */ 10934 open_input_bfds (statement_list.head, NULL, OPEN_BFD_NORMAL); 10935 10936 ldemul_after_open (); 10937 10938 bfd_section_already_linked_table_free (); 10939 10940 /* Make sure that we're not mixing architectures. We call this 10941 after all the input files have been opened, but before we do any 10942 other processing, so that any operations merge_private_bfd_data 10943 does on the output file will be known during the rest of the 10944 link. */ 10945 lang_check (); 10946 10947 /* Size up the common data. */ 10948 lang_common (); 10949 10950 /* Update wild statements. */ 10951 update_wild_statements (statement_list.head); 10952 10953 /* Run through the contours of the script and attach input sections 10954 to the correct output sections. */ 10955 map_input_to_output_sections (statement_list.head, NULL, NULL); 10956 10957 /* Find any sections not attached explicitly and handle them. */ 10958 lang_place_orphans (); 10959 10960 /* Do anything special before sizing sections. This is where ELF 10961 and other back-ends size dynamic sections. */ 10962 ldemul_before_allocation (); 10963 10964 /* Size up the sections. */ 10965 lang_size_sections (NULL, ! RELAXATION_ENABLED); 10966 10967 /* See if anything special should be done now we know how big 10968 everything is. This is where relaxation is done. */ 10969 ldemul_after_allocation (); 10970 10971 ldemul_finish (); 10972 10973 /* Make sure that the section addresses make sense. */ 10974 if (command_line.check_section_addresses) 10975 lang_check_section_addresses (); 10976 10977 lang_end (); 10978 10979 ldwrite (); 10980 10981 ldexp_finish (true); 10982 lang_finish (); 10983 10984 if (! bfd_close (link_info.output_bfd)) 10985 fatal (_("%P:%s: final close failed on object-only output: %E\n"), 10986 output_filename); 10987 10988 link_info.output_bfd = NULL; 10989 10990 /* Read in the object-only file. */ 10991 fd = open (output_filename, O_RDONLY | O_BINARY); 10992 if (fd < 0) 10993 { 10994 bfd_set_error (bfd_error_system_call); 10995 fatal (_("%P:%s: cannot open object-only output: %E\n"), 10996 output_filename); 10997 } 10998 10999 /* Get the object-only file size. */ 11000 if (fstat (fd, &st) != 0) 11001 { 11002 bfd_set_error (bfd_error_system_call); 11003 fatal (_("%P:%s: cannot stat object-only output: %E\n"), 11004 output_filename); 11005 } 11006 11007 size = st.st_size; 11008 off = 0; 11009 contents = (bfd_byte *) xmalloc (size); 11010 while (off != size) 11011 { 11012 ssize_t got; 11013 11014 got = read (fd, contents + off, size - off); 11015 if (got < 0) 11016 { 11017 bfd_set_error (bfd_error_system_call); 11018 fatal (_("%P:%s: read failed on object-only output: %E\n"), 11019 output_filename); 11020 } 11021 11022 off += got; 11023 } 11024 11025 close (fd); 11026 11027 /* Remove the temporary object-only file. */ 11028 unlink (output_filename); 11029 11030 output_filename = saved_output_filename; 11031 11032 cmdline_add_object_only_section (contents, size); 11033 11034 free (contents); 11035 } 11036 11037 /* Extract the object-only section. */ 11038 11039 static const char * 11040 cmdline_extract_object_only_section (bfd *abfd) 11041 { 11042 const char *name = bfd_extract_object_only_section (abfd); 11043 11044 if (name == NULL) 11045 fatal (_("%P: cannot extract object-only section from %B: %E\n"), abfd); 11046 11047 /* It should be removed after it is done. */ 11048 cmdline_list_append (&cmdline_temp_object_only_list, 11049 cmdline_is_file_enum, (void *) name); 11050 11051 return name; 11052 } 11053 11054 /* Load the object-only section. */ 11055 11056 static void 11057 cmdline_load_object_only_section (const char *name) 11058 { 11059 lang_input_statement_type *entry 11060 = new_afile (name, lang_input_file_is_file_enum, NULL, NULL); 11061 11062 if (!entry) 11063 abort (); 11064 11065 ldfile_open_file (entry); 11066 11067 if (trace_files || verbose) 11068 info_msg ("%pI\n", entry); 11069 11070 if (entry->flags.missing_file 11071 || bfd_get_format (entry->the_bfd) != bfd_object) 11072 abort (); 11073 11074 ldlang_add_file (entry); 11075 11076 if (bfd_link_add_symbols (entry->the_bfd, &link_info)) 11077 entry->flags.loaded = true; 11078 else 11079 fatal (_("%P: %pB: error adding symbols: %E\n"), entry->the_bfd); 11080 } 11081 11082 /* Check and handle the object-only section. */ 11083 11084 void 11085 cmdline_check_object_only_section (bfd *abfd, bool lto) 11086 { 11087 const char *filename; 11088 11089 if (config.emitting_gnu_object_only || abfd->format != bfd_object) 11090 return; 11091 11092 if (lto) 11093 { 11094 /* For LTO link, we only need to extract object-only section 11095 from the mixed object, add it to input, and put it on LTO 11096 claimed output. */ 11097 switch (bfd_get_lto_type (abfd)) 11098 { 11099 default: 11100 abort (); 11101 case lto_mixed_object: 11102 filename = cmdline_extract_object_only_section (abfd); 11103 cmdline_load_object_only_section (filename); 11104 break; 11105 case lto_non_ir_object: 11106 case lto_slim_ir_object: 11107 case lto_fat_ir_object: 11108 break; 11109 } 11110 } 11111 else if (bfd_link_relocatable (&link_info)) 11112 { 11113 /* For non-LTO relocatable link, we need to append non-IR object 11114 file and the object file in object-only section to the object 11115 only list. */ 11116 switch (bfd_get_lto_type (abfd)) 11117 { 11118 default: 11119 abort (); 11120 case lto_mixed_object: 11121 filename = cmdline_extract_object_only_section (abfd); 11122 cmdline_object_only_list_append (cmdline_is_file_enum, 11123 (void *) filename); 11124 break; 11125 case lto_non_ir_object: 11126 cmdline_object_only_list_append (cmdline_is_bfd_enum, abfd); 11127 break; 11128 case lto_slim_ir_object: 11129 case lto_fat_ir_object: 11130 break; 11131 } 11132 } 11133 } 11134 11135 /* Remove temporary object-only files. */ 11136 11137 void 11138 cmdline_remove_object_only_files (void) 11139 { 11140 cmdline_union_type *c; 11141 11142 if (config.plugin_save_temps) 11143 return; 11144 11145 c = cmdline_temp_object_only_list.head; 11146 for (; c != NULL; c = c->header.next) 11147 switch (c->header.type) 11148 { 11149 default: 11150 abort (); 11151 case cmdline_is_file_enum: 11152 unlink (c->file.filename); 11153 break; 11154 } 11155 } 11156