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