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