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