| 1 | /* Linker command language support. |
| 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
| 3 | 2001, 2002, 2003 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GLD, the Gnu Linker. |
| 7 | |
| 8 | GLD 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 2, or (at your option) |
| 11 | any later version. |
| 12 | |
| 13 | GLD 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 GLD; see the file COPYING. If not, write to the Free |
| 20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 21 | 02111-1307, USA. */ |
| 22 | |
| 23 | #include "bfd.h" |
| 24 | #include "sysdep.h" |
| 25 | #include "libiberty.h" |
| 26 | #include "safe-ctype.h" |
| 27 | #include "obstack.h" |
| 28 | #include "bfdlink.h" |
| 29 | |
| 30 | #include "ld.h" |
| 31 | #include "ldmain.h" |
| 32 | #include "ldexp.h" |
| 33 | #include "ldlang.h" |
| 34 | #include <ldgram.h> |
| 35 | #include "ldlex.h" |
| 36 | #include "ldmisc.h" |
| 37 | #include "ldctor.h" |
| 38 | #include "ldfile.h" |
| 39 | #include "ldemul.h" |
| 40 | #include "fnmatch.h" |
| 41 | #include "demangle.h" |
| 42 | #include "hashtab.h" |
| 43 | |
| 44 | #ifndef offsetof |
| 45 | #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) |
| 46 | #endif |
| 47 | |
| 48 | /* Locals variables. */ |
| 49 | static struct obstack stat_obstack; |
| 50 | |
| 51 | #define obstack_chunk_alloc xmalloc |
| 52 | #define obstack_chunk_free free |
| 53 | static const char *startup_file; |
| 54 | static lang_statement_list_type input_file_chain; |
| 55 | static bfd_boolean placed_commons = FALSE; |
| 56 | static lang_output_section_statement_type *default_common_section; |
| 57 | static bfd_boolean map_option_f; |
| 58 | static bfd_vma print_dot; |
| 59 | static lang_input_statement_type *first_file; |
| 60 | static const char *current_target; |
| 61 | static const char *output_target; |
| 62 | static lang_statement_list_type statement_list; |
| 63 | static struct lang_phdr *lang_phdr_list; |
| 64 | static struct bfd_hash_table lang_definedness_table; |
| 65 | |
| 66 | /* Forward declarations. */ |
| 67 | static void exp_init_os (etree_type *); |
| 68 | static bfd_boolean wildcardp (const char *); |
| 69 | static lang_input_statement_type *lookup_name (const char *); |
| 70 | static bfd_boolean load_symbols (lang_input_statement_type *, |
| 71 | lang_statement_list_type *); |
| 72 | static struct bfd_hash_entry *lang_definedness_newfunc |
| 73 | (struct bfd_hash_entry *, struct bfd_hash_table *, const char *); |
| 74 | static void insert_undefined (const char *); |
| 75 | static void print_statement (lang_statement_union_type *, |
| 76 | lang_output_section_statement_type *); |
| 77 | static void print_statement_list (lang_statement_union_type *, |
| 78 | lang_output_section_statement_type *); |
| 79 | static void print_statements (void); |
| 80 | static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *); |
| 81 | static void lang_record_phdrs (void); |
| 82 | static void lang_do_version_exports_section (void); |
| 83 | |
| 84 | typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *, |
| 85 | asection *, lang_input_statement_type *, void *); |
| 86 | |
| 87 | /* Exported variables. */ |
| 88 | lang_output_section_statement_type *abs_output_section; |
| 89 | lang_statement_list_type lang_output_section_statement; |
| 90 | lang_statement_list_type *stat_ptr = &statement_list; |
| 91 | lang_statement_list_type file_chain = { NULL, NULL }; |
| 92 | struct bfd_sym_chain entry_symbol = { NULL, NULL }; |
| 93 | const char *entry_section = ".text"; |
| 94 | bfd_boolean entry_from_cmdline; |
| 95 | bfd_boolean lang_has_input_file = FALSE; |
| 96 | bfd_boolean had_output_filename = FALSE; |
| 97 | bfd_boolean lang_float_flag = FALSE; |
| 98 | bfd_boolean delete_output_file_on_failure = FALSE; |
| 99 | struct lang_nocrossrefs *nocrossref_list; |
| 100 | struct unique_sections *unique_section_list; |
| 101 | static bfd_boolean ldlang_sysrooted_script = FALSE; |
| 102 | int lang_statement_iteration = 0; |
| 103 | |
| 104 | etree_type *base; /* Relocation base - or null */ |
| 105 | |
| 106 | #define new_stat(x, y) \ |
| 107 | (x##_type *) new_statement (x##_enum, sizeof (x##_type), y) |
| 108 | |
| 109 | #define outside_section_address(q) \ |
| 110 | ((q)->output_offset + (q)->output_section->vma) |
| 111 | |
| 112 | #define outside_symbol_address(q) \ |
| 113 | ((q)->value + outside_section_address (q->section)) |
| 114 | |
| 115 | #define SECTION_NAME_MAP_LENGTH (16) |
| 116 | |
| 117 | void * |
| 118 | stat_alloc (size_t size) |
| 119 | { |
| 120 | return obstack_alloc (&stat_obstack, size); |
| 121 | } |
| 122 | |
| 123 | bfd_boolean |
| 124 | unique_section_p (const char *secnam) |
| 125 | { |
| 126 | struct unique_sections *unam; |
| 127 | |
| 128 | for (unam = unique_section_list; unam; unam = unam->next) |
| 129 | if (wildcardp (unam->name) |
| 130 | ? fnmatch (unam->name, secnam, 0) == 0 |
| 131 | : strcmp (unam->name, secnam) == 0) |
| 132 | { |
| 133 | return TRUE; |
| 134 | } |
| 135 | |
| 136 | return FALSE; |
| 137 | } |
| 138 | |
| 139 | /* Generic traversal routines for finding matching sections. */ |
| 140 | |
| 141 | static void |
| 142 | walk_wild_section (lang_wild_statement_type *ptr, |
| 143 | lang_input_statement_type *file, |
| 144 | callback_t callback, |
| 145 | void *data) |
| 146 | { |
| 147 | asection *s; |
| 148 | |
| 149 | if (file->just_syms_flag) |
| 150 | return; |
| 151 | |
| 152 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 153 | { |
| 154 | struct wildcard_list *sec; |
| 155 | |
| 156 | sec = ptr->section_list; |
| 157 | if (sec == NULL) |
| 158 | (*callback) (ptr, sec, s, file, data); |
| 159 | |
| 160 | while (sec != NULL) |
| 161 | { |
| 162 | bfd_boolean skip = FALSE; |
| 163 | struct name_list *list_tmp; |
| 164 | |
| 165 | /* Don't process sections from files which were |
| 166 | excluded. */ |
| 167 | for (list_tmp = sec->spec.exclude_name_list; |
| 168 | list_tmp; |
| 169 | list_tmp = list_tmp->next) |
| 170 | { |
| 171 | if (wildcardp (list_tmp->name)) |
| 172 | skip = fnmatch (list_tmp->name, file->filename, 0) == 0; |
| 173 | else |
| 174 | skip = strcmp (list_tmp->name, file->filename) == 0; |
| 175 | |
| 176 | /* If this file is part of an archive, and the archive is |
| 177 | excluded, exclude this file. */ |
| 178 | if (! skip && file->the_bfd != NULL |
| 179 | && file->the_bfd->my_archive != NULL |
| 180 | && file->the_bfd->my_archive->filename != NULL) |
| 181 | { |
| 182 | if (wildcardp (list_tmp->name)) |
| 183 | skip = fnmatch (list_tmp->name, |
| 184 | file->the_bfd->my_archive->filename, |
| 185 | 0) == 0; |
| 186 | else |
| 187 | skip = strcmp (list_tmp->name, |
| 188 | file->the_bfd->my_archive->filename) == 0; |
| 189 | } |
| 190 | |
| 191 | if (skip) |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | if (!skip && sec->spec.name != NULL) |
| 196 | { |
| 197 | const char *sname = bfd_get_section_name (file->the_bfd, s); |
| 198 | |
| 199 | if (wildcardp (sec->spec.name)) |
| 200 | skip = fnmatch (sec->spec.name, sname, 0) != 0; |
| 201 | else |
| 202 | skip = strcmp (sec->spec.name, sname) != 0; |
| 203 | } |
| 204 | |
| 205 | if (!skip) |
| 206 | (*callback) (ptr, sec, s, file, data); |
| 207 | |
| 208 | sec = sec->next; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* Handle a wild statement for a single file F. */ |
| 214 | |
| 215 | static void |
| 216 | walk_wild_file (lang_wild_statement_type *s, |
| 217 | lang_input_statement_type *f, |
| 218 | callback_t callback, |
| 219 | void *data) |
| 220 | { |
| 221 | if (f->the_bfd == NULL |
| 222 | || ! bfd_check_format (f->the_bfd, bfd_archive)) |
| 223 | walk_wild_section (s, f, callback, data); |
| 224 | else |
| 225 | { |
| 226 | bfd *member; |
| 227 | |
| 228 | /* This is an archive file. We must map each member of the |
| 229 | archive separately. */ |
| 230 | member = bfd_openr_next_archived_file (f->the_bfd, NULL); |
| 231 | while (member != NULL) |
| 232 | { |
| 233 | /* When lookup_name is called, it will call the add_symbols |
| 234 | entry point for the archive. For each element of the |
| 235 | archive which is included, BFD will call ldlang_add_file, |
| 236 | which will set the usrdata field of the member to the |
| 237 | lang_input_statement. */ |
| 238 | if (member->usrdata != NULL) |
| 239 | { |
| 240 | walk_wild_section (s, member->usrdata, callback, data); |
| 241 | } |
| 242 | |
| 243 | member = bfd_openr_next_archived_file (f->the_bfd, member); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) |
| 250 | { |
| 251 | const char *file_spec = s->filename; |
| 252 | |
| 253 | if (file_spec == NULL) |
| 254 | { |
| 255 | /* Perform the iteration over all files in the list. */ |
| 256 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 257 | { |
| 258 | walk_wild_file (s, f, callback, data); |
| 259 | } |
| 260 | } |
| 261 | else if (wildcardp (file_spec)) |
| 262 | { |
| 263 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 264 | { |
| 265 | if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0) |
| 266 | walk_wild_file (s, f, callback, data); |
| 267 | } |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | lang_input_statement_type *f; |
| 272 | |
| 273 | /* Perform the iteration over a single file. */ |
| 274 | f = lookup_name (file_spec); |
| 275 | if (f) |
| 276 | walk_wild_file (s, f, callback, data); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* lang_for_each_statement walks the parse tree and calls the provided |
| 281 | function for each node. */ |
| 282 | |
| 283 | static void |
| 284 | lang_for_each_statement_worker (void (*func) (lang_statement_union_type *), |
| 285 | lang_statement_union_type *s) |
| 286 | { |
| 287 | for (; s != NULL; s = s->header.next) |
| 288 | { |
| 289 | func (s); |
| 290 | |
| 291 | switch (s->header.type) |
| 292 | { |
| 293 | case lang_constructors_statement_enum: |
| 294 | lang_for_each_statement_worker (func, constructor_list.head); |
| 295 | break; |
| 296 | case lang_output_section_statement_enum: |
| 297 | lang_for_each_statement_worker |
| 298 | (func, |
| 299 | s->output_section_statement.children.head); |
| 300 | break; |
| 301 | case lang_wild_statement_enum: |
| 302 | lang_for_each_statement_worker |
| 303 | (func, |
| 304 | s->wild_statement.children.head); |
| 305 | break; |
| 306 | case lang_group_statement_enum: |
| 307 | lang_for_each_statement_worker (func, |
| 308 | s->group_statement.children.head); |
| 309 | break; |
| 310 | case lang_data_statement_enum: |
| 311 | case lang_reloc_statement_enum: |
| 312 | case lang_object_symbols_statement_enum: |
| 313 | case lang_output_statement_enum: |
| 314 | case lang_target_statement_enum: |
| 315 | case lang_input_section_enum: |
| 316 | case lang_input_statement_enum: |
| 317 | case lang_assignment_statement_enum: |
| 318 | case lang_padding_statement_enum: |
| 319 | case lang_address_statement_enum: |
| 320 | case lang_fill_statement_enum: |
| 321 | break; |
| 322 | default: |
| 323 | FAIL (); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | lang_for_each_statement (void (*func) (lang_statement_union_type *)) |
| 331 | { |
| 332 | lang_for_each_statement_worker (func, statement_list.head); |
| 333 | } |
| 334 | |
| 335 | /*----------------------------------------------------------------------*/ |
| 336 | |
| 337 | void |
| 338 | lang_list_init (lang_statement_list_type *list) |
| 339 | { |
| 340 | list->head = NULL; |
| 341 | list->tail = &list->head; |
| 342 | } |
| 343 | |
| 344 | /* Build a new statement node for the parse tree. */ |
| 345 | |
| 346 | static lang_statement_union_type * |
| 347 | new_statement (enum statement_enum type, |
| 348 | size_t size, |
| 349 | lang_statement_list_type *list) |
| 350 | { |
| 351 | lang_statement_union_type *new; |
| 352 | |
| 353 | new = stat_alloc (size); |
| 354 | new->header.type = type; |
| 355 | new->header.next = NULL; |
| 356 | lang_statement_append (list, new, &new->header.next); |
| 357 | return new; |
| 358 | } |
| 359 | |
| 360 | /* Build a new input file node for the language. There are several |
| 361 | ways in which we treat an input file, eg, we only look at symbols, |
| 362 | or prefix it with a -l etc. |
| 363 | |
| 364 | We can be supplied with requests for input files more than once; |
| 365 | they may, for example be split over several lines like foo.o(.text) |
| 366 | foo.o(.data) etc, so when asked for a file we check that we haven't |
| 367 | got it already so we don't duplicate the bfd. */ |
| 368 | |
| 369 | static lang_input_statement_type * |
| 370 | new_afile (const char *name, |
| 371 | lang_input_file_enum_type file_type, |
| 372 | const char *target, |
| 373 | bfd_boolean add_to_list) |
| 374 | { |
| 375 | lang_input_statement_type *p; |
| 376 | |
| 377 | if (add_to_list) |
| 378 | p = new_stat (lang_input_statement, stat_ptr); |
| 379 | else |
| 380 | { |
| 381 | p = stat_alloc (sizeof (lang_input_statement_type)); |
| 382 | p->header.next = NULL; |
| 383 | } |
| 384 | |
| 385 | lang_has_input_file = TRUE; |
| 386 | p->target = target; |
| 387 | p->sysrooted = FALSE; |
| 388 | switch (file_type) |
| 389 | { |
| 390 | case lang_input_file_is_symbols_only_enum: |
| 391 | p->filename = name; |
| 392 | p->is_archive = FALSE; |
| 393 | p->real = TRUE; |
| 394 | p->local_sym_name = name; |
| 395 | p->just_syms_flag = TRUE; |
| 396 | p->search_dirs_flag = FALSE; |
| 397 | break; |
| 398 | case lang_input_file_is_fake_enum: |
| 399 | p->filename = name; |
| 400 | p->is_archive = FALSE; |
| 401 | p->real = FALSE; |
| 402 | p->local_sym_name = name; |
| 403 | p->just_syms_flag = FALSE; |
| 404 | p->search_dirs_flag = FALSE; |
| 405 | break; |
| 406 | case lang_input_file_is_l_enum: |
| 407 | p->is_archive = TRUE; |
| 408 | p->filename = name; |
| 409 | p->real = TRUE; |
| 410 | p->local_sym_name = concat ("-l", name, NULL); |
| 411 | p->just_syms_flag = FALSE; |
| 412 | p->search_dirs_flag = TRUE; |
| 413 | break; |
| 414 | case lang_input_file_is_marker_enum: |
| 415 | p->filename = name; |
| 416 | p->is_archive = FALSE; |
| 417 | p->real = FALSE; |
| 418 | p->local_sym_name = name; |
| 419 | p->just_syms_flag = FALSE; |
| 420 | p->search_dirs_flag = TRUE; |
| 421 | break; |
| 422 | case lang_input_file_is_search_file_enum: |
| 423 | p->sysrooted = ldlang_sysrooted_script; |
| 424 | p->filename = name; |
| 425 | p->is_archive = FALSE; |
| 426 | p->real = TRUE; |
| 427 | p->local_sym_name = name; |
| 428 | p->just_syms_flag = FALSE; |
| 429 | p->search_dirs_flag = TRUE; |
| 430 | break; |
| 431 | case lang_input_file_is_file_enum: |
| 432 | p->filename = name; |
| 433 | p->is_archive = FALSE; |
| 434 | p->real = TRUE; |
| 435 | p->local_sym_name = name; |
| 436 | p->just_syms_flag = FALSE; |
| 437 | p->search_dirs_flag = FALSE; |
| 438 | break; |
| 439 | default: |
| 440 | FAIL (); |
| 441 | } |
| 442 | p->the_bfd = NULL; |
| 443 | p->asymbols = NULL; |
| 444 | p->next_real_file = NULL; |
| 445 | p->next = NULL; |
| 446 | p->symbol_count = 0; |
| 447 | p->dynamic = config.dynamic_link; |
| 448 | p->whole_archive = whole_archive; |
| 449 | p->loaded = FALSE; |
| 450 | lang_statement_append (&input_file_chain, |
| 451 | (lang_statement_union_type *) p, |
| 452 | &p->next_real_file); |
| 453 | return p; |
| 454 | } |
| 455 | |
| 456 | lang_input_statement_type * |
| 457 | lang_add_input_file (const char *name, |
| 458 | lang_input_file_enum_type file_type, |
| 459 | const char *target) |
| 460 | { |
| 461 | lang_has_input_file = TRUE; |
| 462 | return new_afile (name, file_type, target, TRUE); |
| 463 | } |
| 464 | |
| 465 | /* Build enough state so that the parser can build its tree. */ |
| 466 | |
| 467 | void |
| 468 | lang_init (void) |
| 469 | { |
| 470 | obstack_begin (&stat_obstack, 1000); |
| 471 | |
| 472 | stat_ptr = &statement_list; |
| 473 | |
| 474 | lang_list_init (stat_ptr); |
| 475 | |
| 476 | lang_list_init (&input_file_chain); |
| 477 | lang_list_init (&lang_output_section_statement); |
| 478 | lang_list_init (&file_chain); |
| 479 | first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum, |
| 480 | NULL); |
| 481 | abs_output_section = |
| 482 | lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME); |
| 483 | |
| 484 | abs_output_section->bfd_section = bfd_abs_section_ptr; |
| 485 | |
| 486 | /* The value "3" is ad-hoc, somewhat related to the expected number of |
| 487 | DEFINED expressions in a linker script. For most default linker |
| 488 | scripts, there are none. Why a hash table then? Well, it's somewhat |
| 489 | simpler to re-use working machinery than using a linked list in terms |
| 490 | of code-complexity here in ld, besides the initialization which just |
| 491 | looks like other code here. */ |
| 492 | if (bfd_hash_table_init_n (&lang_definedness_table, |
| 493 | lang_definedness_newfunc, 3) != TRUE) |
| 494 | einfo (_("%P%F: out of memory during initialization")); |
| 495 | |
| 496 | /* Callers of exp_fold_tree need to increment this. */ |
| 497 | lang_statement_iteration = 0; |
| 498 | } |
| 499 | |
| 500 | /*---------------------------------------------------------------------- |
| 501 | A region is an area of memory declared with the |
| 502 | MEMORY { name:org=exp, len=exp ... } |
| 503 | syntax. |
| 504 | |
| 505 | We maintain a list of all the regions here. |
| 506 | |
| 507 | If no regions are specified in the script, then the default is used |
| 508 | which is created when looked up to be the entire data space. |
| 509 | |
| 510 | If create is true we are creating a region inside a MEMORY block. |
| 511 | In this case it is probably an error to create a region that has |
| 512 | already been created. If we are not inside a MEMORY block it is |
| 513 | dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION) |
| 514 | and so we issue a warning. */ |
| 515 | |
| 516 | static lang_memory_region_type *lang_memory_region_list; |
| 517 | static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list; |
| 518 | |
| 519 | lang_memory_region_type * |
| 520 | lang_memory_region_lookup (const char *const name, bfd_boolean create) |
| 521 | { |
| 522 | lang_memory_region_type *p; |
| 523 | lang_memory_region_type *new; |
| 524 | |
| 525 | /* NAME is NULL for LMA memspecs if no region was specified. */ |
| 526 | if (name == NULL) |
| 527 | return NULL; |
| 528 | |
| 529 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
| 530 | if (strcmp (p->name, name) == 0) |
| 531 | { |
| 532 | if (create) |
| 533 | einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name); |
| 534 | return p; |
| 535 | } |
| 536 | |
| 537 | #if 0 |
| 538 | /* This code used to always use the first region in the list as the |
| 539 | default region. I changed it to instead use a region |
| 540 | encompassing all of memory as the default region. This permits |
| 541 | NOLOAD sections to work reasonably without requiring a region. |
| 542 | People should specify what region they mean, if they really want |
| 543 | a region. */ |
| 544 | if (strcmp (name, DEFAULT_MEMORY_REGION) == 0) |
| 545 | { |
| 546 | if (lang_memory_region_list != NULL) |
| 547 | return lang_memory_region_list; |
| 548 | } |
| 549 | #endif |
| 550 | |
| 551 | if (!create && strcmp (name, DEFAULT_MEMORY_REGION)) |
| 552 | einfo (_("%P:%S: warning: memory region %s not declared\n"), name); |
| 553 | |
| 554 | new = stat_alloc (sizeof (lang_memory_region_type)); |
| 555 | |
| 556 | new->name = xstrdup (name); |
| 557 | new->next = NULL; |
| 558 | |
| 559 | *lang_memory_region_list_tail = new; |
| 560 | lang_memory_region_list_tail = &new->next; |
| 561 | new->origin = 0; |
| 562 | new->flags = 0; |
| 563 | new->not_flags = 0; |
| 564 | new->length = ~(bfd_size_type) 0; |
| 565 | new->current = 0; |
| 566 | new->had_full_message = FALSE; |
| 567 | |
| 568 | return new; |
| 569 | } |
| 570 | |
| 571 | static lang_memory_region_type * |
| 572 | lang_memory_default (asection *section) |
| 573 | { |
| 574 | lang_memory_region_type *p; |
| 575 | |
| 576 | flagword sec_flags = section->flags; |
| 577 | |
| 578 | /* Override SEC_DATA to mean a writable section. */ |
| 579 | if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC) |
| 580 | sec_flags |= SEC_DATA; |
| 581 | |
| 582 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
| 583 | { |
| 584 | if ((p->flags & sec_flags) != 0 |
| 585 | && (p->not_flags & sec_flags) == 0) |
| 586 | { |
| 587 | return p; |
| 588 | } |
| 589 | } |
| 590 | return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE); |
| 591 | } |
| 592 | |
| 593 | lang_output_section_statement_type * |
| 594 | lang_output_section_find (const char *const name) |
| 595 | { |
| 596 | lang_statement_union_type *u; |
| 597 | lang_output_section_statement_type *lookup; |
| 598 | |
| 599 | for (u = lang_output_section_statement.head; u != NULL; u = lookup->next) |
| 600 | { |
| 601 | lookup = &u->output_section_statement; |
| 602 | if (strcmp (name, lookup->name) == 0) |
| 603 | return lookup; |
| 604 | } |
| 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | lang_output_section_statement_type * |
| 609 | lang_output_section_statement_lookup (const char *const name) |
| 610 | { |
| 611 | lang_output_section_statement_type *lookup; |
| 612 | |
| 613 | lookup = lang_output_section_find (name); |
| 614 | if (lookup == NULL) |
| 615 | { |
| 616 | lookup = new_stat (lang_output_section_statement, stat_ptr); |
| 617 | lookup->region = NULL; |
| 618 | lookup->lma_region = NULL; |
| 619 | lookup->fill = 0; |
| 620 | lookup->block_value = 1; |
| 621 | lookup->name = name; |
| 622 | |
| 623 | lookup->next = NULL; |
| 624 | lookup->bfd_section = NULL; |
| 625 | lookup->processed = FALSE; |
| 626 | lookup->sectype = normal_section; |
| 627 | lookup->addr_tree = NULL; |
| 628 | lang_list_init (&lookup->children); |
| 629 | |
| 630 | lookup->memspec = NULL; |
| 631 | lookup->flags = 0; |
| 632 | lookup->subsection_alignment = -1; |
| 633 | lookup->section_alignment = -1; |
| 634 | lookup->load_base = NULL; |
| 635 | lookup->update_dot_tree = NULL; |
| 636 | lookup->phdrs = NULL; |
| 637 | |
| 638 | lang_statement_append (&lang_output_section_statement, |
| 639 | (lang_statement_union_type *) lookup, |
| 640 | &lookup->next); |
| 641 | } |
| 642 | return lookup; |
| 643 | } |
| 644 | |
| 645 | static void |
| 646 | lang_map_flags (flagword flag) |
| 647 | { |
| 648 | if (flag & SEC_ALLOC) |
| 649 | minfo ("a"); |
| 650 | |
| 651 | if (flag & SEC_CODE) |
| 652 | minfo ("x"); |
| 653 | |
| 654 | if (flag & SEC_READONLY) |
| 655 | minfo ("r"); |
| 656 | |
| 657 | if (flag & SEC_DATA) |
| 658 | minfo ("w"); |
| 659 | |
| 660 | if (flag & SEC_LOAD) |
| 661 | minfo ("l"); |
| 662 | } |
| 663 | |
| 664 | void |
| 665 | lang_map (void) |
| 666 | { |
| 667 | lang_memory_region_type *m; |
| 668 | |
| 669 | minfo (_("\nMemory Configuration\n\n")); |
| 670 | fprintf (config.map_file, "%-16s %-18s %-18s %s\n", |
| 671 | _("Name"), _("Origin"), _("Length"), _("Attributes")); |
| 672 | |
| 673 | for (m = lang_memory_region_list; m != NULL; m = m->next) |
| 674 | { |
| 675 | char buf[100]; |
| 676 | int len; |
| 677 | |
| 678 | fprintf (config.map_file, "%-16s ", m->name); |
| 679 | |
| 680 | sprintf_vma (buf, m->origin); |
| 681 | minfo ("0x%s ", buf); |
| 682 | len = strlen (buf); |
| 683 | while (len < 16) |
| 684 | { |
| 685 | print_space (); |
| 686 | ++len; |
| 687 | } |
| 688 | |
| 689 | minfo ("0x%V", m->length); |
| 690 | if (m->flags || m->not_flags) |
| 691 | { |
| 692 | #ifndef BFD64 |
| 693 | minfo (" "); |
| 694 | #endif |
| 695 | if (m->flags) |
| 696 | { |
| 697 | print_space (); |
| 698 | lang_map_flags (m->flags); |
| 699 | } |
| 700 | |
| 701 | if (m->not_flags) |
| 702 | { |
| 703 | minfo (" !"); |
| 704 | lang_map_flags (m->not_flags); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | print_nl (); |
| 709 | } |
| 710 | |
| 711 | fprintf (config.map_file, _("\nLinker script and memory map\n\n")); |
| 712 | |
| 713 | print_statements (); |
| 714 | } |
| 715 | |
| 716 | /* Initialize an output section. */ |
| 717 | |
| 718 | static void |
| 719 | init_os (lang_output_section_statement_type *s) |
| 720 | { |
| 721 | section_userdata_type *new; |
| 722 | |
| 723 | if (s->bfd_section != NULL) |
| 724 | return; |
| 725 | |
| 726 | if (strcmp (s->name, DISCARD_SECTION_NAME) == 0) |
| 727 | einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME); |
| 728 | |
| 729 | new = stat_alloc (sizeof (section_userdata_type)); |
| 730 | |
| 731 | s->bfd_section = bfd_get_section_by_name (output_bfd, s->name); |
| 732 | if (s->bfd_section == NULL) |
| 733 | s->bfd_section = bfd_make_section (output_bfd, s->name); |
| 734 | if (s->bfd_section == NULL) |
| 735 | { |
| 736 | einfo (_("%P%F: output format %s cannot represent section called %s\n"), |
| 737 | output_bfd->xvec->name, s->name); |
| 738 | } |
| 739 | s->bfd_section->output_section = s->bfd_section; |
| 740 | |
| 741 | /* We initialize an output sections output offset to minus its own |
| 742 | vma to allow us to output a section through itself. */ |
| 743 | s->bfd_section->output_offset = 0; |
| 744 | get_userdata (s->bfd_section) = new; |
| 745 | |
| 746 | /* If there is a base address, make sure that any sections it might |
| 747 | mention are initialized. */ |
| 748 | if (s->addr_tree != NULL) |
| 749 | exp_init_os (s->addr_tree); |
| 750 | |
| 751 | if (s->load_base != NULL) |
| 752 | exp_init_os (s->load_base); |
| 753 | } |
| 754 | |
| 755 | /* Make sure that all output sections mentioned in an expression are |
| 756 | initialized. */ |
| 757 | |
| 758 | static void |
| 759 | exp_init_os (etree_type *exp) |
| 760 | { |
| 761 | switch (exp->type.node_class) |
| 762 | { |
| 763 | case etree_assign: |
| 764 | exp_init_os (exp->assign.src); |
| 765 | break; |
| 766 | |
| 767 | case etree_binary: |
| 768 | exp_init_os (exp->binary.lhs); |
| 769 | exp_init_os (exp->binary.rhs); |
| 770 | break; |
| 771 | |
| 772 | case etree_trinary: |
| 773 | exp_init_os (exp->trinary.cond); |
| 774 | exp_init_os (exp->trinary.lhs); |
| 775 | exp_init_os (exp->trinary.rhs); |
| 776 | break; |
| 777 | |
| 778 | case etree_unary: |
| 779 | exp_init_os (exp->unary.child); |
| 780 | break; |
| 781 | |
| 782 | case etree_name: |
| 783 | switch (exp->type.node_code) |
| 784 | { |
| 785 | case ADDR: |
| 786 | case LOADADDR: |
| 787 | case SIZEOF: |
| 788 | { |
| 789 | lang_output_section_statement_type *os; |
| 790 | |
| 791 | os = lang_output_section_find (exp->name.name); |
| 792 | if (os != NULL && os->bfd_section == NULL) |
| 793 | init_os (os); |
| 794 | } |
| 795 | } |
| 796 | break; |
| 797 | |
| 798 | default: |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | \f |
| 803 | /* Sections marked with the SEC_LINK_ONCE flag should only be linked |
| 804 | once into the output. This routine checks each section, and |
| 805 | arrange to discard it if a section of the same name has already |
| 806 | been linked. If the section has COMDAT information, then it uses |
| 807 | that to decide whether the section should be included. This code |
| 808 | assumes that all relevant sections have the SEC_LINK_ONCE flag set; |
| 809 | that is, it does not depend solely upon the section name. |
| 810 | section_already_linked is called via bfd_map_over_sections. */ |
| 811 | |
| 812 | /* This is the shape of the elements inside the already_linked hash |
| 813 | table. It maps a name onto a list of already_linked elements with |
| 814 | the same name. It's possible to get more than one element in a |
| 815 | list if the COMDAT sections have different names. */ |
| 816 | |
| 817 | struct already_linked_hash_entry |
| 818 | { |
| 819 | struct bfd_hash_entry root; |
| 820 | struct already_linked *entry; |
| 821 | }; |
| 822 | |
| 823 | struct already_linked |
| 824 | { |
| 825 | struct already_linked *next; |
| 826 | asection *sec; |
| 827 | }; |
| 828 | |
| 829 | /* The hash table. */ |
| 830 | |
| 831 | static struct bfd_hash_table already_linked_table; |
| 832 | |
| 833 | static void |
| 834 | section_already_linked (bfd *abfd, asection *sec, void *data) |
| 835 | { |
| 836 | lang_input_statement_type *entry = data; |
| 837 | flagword flags; |
| 838 | const char *name; |
| 839 | struct already_linked *l; |
| 840 | struct already_linked_hash_entry *already_linked_list; |
| 841 | |
| 842 | /* If we are only reading symbols from this object, then we want to |
| 843 | discard all sections. */ |
| 844 | if (entry->just_syms_flag) |
| 845 | { |
| 846 | bfd_link_just_syms (sec, &link_info); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | flags = bfd_get_section_flags (abfd, sec); |
| 851 | |
| 852 | if ((flags & SEC_LINK_ONCE) == 0) |
| 853 | return; |
| 854 | |
| 855 | /* FIXME: When doing a relocatable link, we may have trouble |
| 856 | copying relocations in other sections that refer to local symbols |
| 857 | in the section being discarded. Those relocations will have to |
| 858 | be converted somehow; as of this writing I'm not sure that any of |
| 859 | the backends handle that correctly. |
| 860 | |
| 861 | It is tempting to instead not discard link once sections when |
| 862 | doing a relocatable link (technically, they should be discarded |
| 863 | whenever we are building constructors). However, that fails, |
| 864 | because the linker winds up combining all the link once sections |
| 865 | into a single large link once section, which defeats the purpose |
| 866 | of having link once sections in the first place. |
| 867 | |
| 868 | Also, not merging link once sections in a relocatable link |
| 869 | causes trouble for MIPS ELF, which relies on link once semantics |
| 870 | to handle the .reginfo section correctly. */ |
| 871 | |
| 872 | name = bfd_get_section_name (abfd, sec); |
| 873 | |
| 874 | already_linked_list = |
| 875 | ((struct already_linked_hash_entry *) |
| 876 | bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE)); |
| 877 | |
| 878 | for (l = already_linked_list->entry; l != NULL; l = l->next) |
| 879 | { |
| 880 | if (sec->comdat == NULL |
| 881 | || l->sec->comdat == NULL |
| 882 | || strcmp (sec->comdat->name, l->sec->comdat->name) == 0) |
| 883 | { |
| 884 | /* The section has already been linked. See if we should |
| 885 | issue a warning. */ |
| 886 | switch (flags & SEC_LINK_DUPLICATES) |
| 887 | { |
| 888 | default: |
| 889 | abort (); |
| 890 | |
| 891 | case SEC_LINK_DUPLICATES_DISCARD: |
| 892 | break; |
| 893 | |
| 894 | case SEC_LINK_DUPLICATES_ONE_ONLY: |
| 895 | if (sec->comdat == NULL) |
| 896 | einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"), |
| 897 | abfd, name); |
| 898 | else |
| 899 | einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"), |
| 900 | abfd, name, sec->comdat->name); |
| 901 | break; |
| 902 | |
| 903 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: |
| 904 | /* FIXME: We should really dig out the contents of both |
| 905 | sections and memcmp them. The COFF/PE spec says that |
| 906 | the Microsoft linker does not implement this |
| 907 | correctly, so I'm not going to bother doing it |
| 908 | either. */ |
| 909 | /* Fall through. */ |
| 910 | case SEC_LINK_DUPLICATES_SAME_SIZE: |
| 911 | if (bfd_section_size (abfd, sec) |
| 912 | != bfd_section_size (l->sec->owner, l->sec)) |
| 913 | einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"), |
| 914 | abfd, name); |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | /* Set the output_section field so that lang_add_section |
| 919 | does not create a lang_input_section structure for this |
| 920 | section. Since there might be a symbol in the section |
| 921 | being discarded, we must retain a pointer to the section |
| 922 | which we are really going to use. */ |
| 923 | sec->output_section = bfd_abs_section_ptr; |
| 924 | sec->kept_section = l->sec; |
| 925 | |
| 926 | if (flags & SEC_GROUP) |
| 927 | bfd_discard_group (abfd, sec); |
| 928 | |
| 929 | return; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | /* This is the first section with this name. Record it. Allocate |
| 934 | the memory from the same obstack as the hash table is kept in. */ |
| 935 | |
| 936 | l = bfd_hash_allocate (&already_linked_table, sizeof *l); |
| 937 | |
| 938 | l->sec = sec; |
| 939 | l->next = already_linked_list->entry; |
| 940 | already_linked_list->entry = l; |
| 941 | } |
| 942 | |
| 943 | /* Support routines for the hash table used by section_already_linked, |
| 944 | initialize the table, fill in an entry and remove the table. */ |
| 945 | |
| 946 | static struct bfd_hash_entry * |
| 947 | already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED, |
| 948 | struct bfd_hash_table *table, |
| 949 | const char *string ATTRIBUTE_UNUSED) |
| 950 | { |
| 951 | struct already_linked_hash_entry *ret = |
| 952 | bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry)); |
| 953 | |
| 954 | ret->entry = NULL; |
| 955 | |
| 956 | return &ret->root; |
| 957 | } |
| 958 | |
| 959 | static void |
| 960 | already_linked_table_init (void) |
| 961 | { |
| 962 | if (! bfd_hash_table_init_n (&already_linked_table, |
| 963 | already_linked_newfunc, |
| 964 | 42)) |
| 965 | einfo (_("%P%F: Failed to create hash table\n")); |
| 966 | } |
| 967 | |
| 968 | static void |
| 969 | already_linked_table_free (void) |
| 970 | { |
| 971 | bfd_hash_table_free (&already_linked_table); |
| 972 | } |
| 973 | \f |
| 974 | /* The wild routines. |
| 975 | |
| 976 | These expand statements like *(.text) and foo.o to a list of |
| 977 | explicit actions, like foo.o(.text), bar.o(.text) and |
| 978 | foo.o(.text, .data). */ |
| 979 | |
| 980 | /* Return TRUE if the PATTERN argument is a wildcard pattern. |
| 981 | Although backslashes are treated specially if a pattern contains |
| 982 | wildcards, we do not consider the mere presence of a backslash to |
| 983 | be enough to cause the pattern to be treated as a wildcard. |
| 984 | That lets us handle DOS filenames more naturally. */ |
| 985 | |
| 986 | static bfd_boolean |
| 987 | wildcardp (const char *pattern) |
| 988 | { |
| 989 | const char *s; |
| 990 | |
| 991 | for (s = pattern; *s != '\0'; ++s) |
| 992 | if (*s == '?' |
| 993 | || *s == '*' |
| 994 | || *s == '[') |
| 995 | return TRUE; |
| 996 | return FALSE; |
| 997 | } |
| 998 | |
| 999 | /* Add SECTION to the output section OUTPUT. Do this by creating a |
| 1000 | lang_input_section statement which is placed at PTR. FILE is the |
| 1001 | input file which holds SECTION. */ |
| 1002 | |
| 1003 | void |
| 1004 | lang_add_section (lang_statement_list_type *ptr, |
| 1005 | asection *section, |
| 1006 | lang_output_section_statement_type *output, |
| 1007 | lang_input_statement_type *file) |
| 1008 | { |
| 1009 | flagword flags; |
| 1010 | bfd_boolean discard; |
| 1011 | |
| 1012 | flags = bfd_get_section_flags (section->owner, section); |
| 1013 | |
| 1014 | discard = FALSE; |
| 1015 | |
| 1016 | /* Discard sections marked with SEC_EXCLUDE if we are doing a final |
| 1017 | link. Discard debugging sections marked with SEC_EXCLUDE on a |
| 1018 | relocatable link too. */ |
| 1019 | if ((flags & SEC_EXCLUDE) != 0 |
| 1020 | && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable)) |
| 1021 | discard = TRUE; |
| 1022 | |
| 1023 | /* Discard input sections which are assigned to a section named |
| 1024 | DISCARD_SECTION_NAME. */ |
| 1025 | if (strcmp (output->name, DISCARD_SECTION_NAME) == 0) |
| 1026 | discard = TRUE; |
| 1027 | |
| 1028 | /* Discard debugging sections if we are stripping debugging |
| 1029 | information. */ |
| 1030 | if ((link_info.strip == strip_debugger || link_info.strip == strip_all) |
| 1031 | && (flags & SEC_DEBUGGING) != 0) |
| 1032 | discard = TRUE; |
| 1033 | |
| 1034 | if (discard) |
| 1035 | { |
| 1036 | if (section->output_section == NULL) |
| 1037 | { |
| 1038 | /* This prevents future calls from assigning this section. */ |
| 1039 | section->output_section = bfd_abs_section_ptr; |
| 1040 | } |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | if (section->output_section == NULL) |
| 1045 | { |
| 1046 | bfd_boolean first; |
| 1047 | lang_input_section_type *new; |
| 1048 | flagword flags; |
| 1049 | |
| 1050 | if (output->bfd_section == NULL) |
| 1051 | init_os (output); |
| 1052 | |
| 1053 | first = ! output->bfd_section->linker_has_input; |
| 1054 | output->bfd_section->linker_has_input = 1; |
| 1055 | |
| 1056 | /* Add a section reference to the list. */ |
| 1057 | new = new_stat (lang_input_section, ptr); |
| 1058 | |
| 1059 | new->section = section; |
| 1060 | new->ifile = file; |
| 1061 | section->output_section = output->bfd_section; |
| 1062 | |
| 1063 | flags = section->flags; |
| 1064 | |
| 1065 | /* We don't copy the SEC_NEVER_LOAD flag from an input section |
| 1066 | to an output section, because we want to be able to include a |
| 1067 | SEC_NEVER_LOAD section in the middle of an otherwise loaded |
| 1068 | section (I don't know why we want to do this, but we do). |
| 1069 | build_link_order in ldwrite.c handles this case by turning |
| 1070 | the embedded SEC_NEVER_LOAD section into a fill. */ |
| 1071 | |
| 1072 | flags &= ~ SEC_NEVER_LOAD; |
| 1073 | |
| 1074 | /* If final link, don't copy the SEC_LINK_ONCE flags, they've |
| 1075 | already been processed. One reason to do this is that on pe |
| 1076 | format targets, .text$foo sections go into .text and it's odd |
| 1077 | to see .text with SEC_LINK_ONCE set. */ |
| 1078 | |
| 1079 | if (! link_info.relocatable) |
| 1080 | flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES); |
| 1081 | |
| 1082 | /* If this is not the first input section, and the SEC_READONLY |
| 1083 | flag is not currently set, then don't set it just because the |
| 1084 | input section has it set. */ |
| 1085 | |
| 1086 | if (! first && (section->output_section->flags & SEC_READONLY) == 0) |
| 1087 | flags &= ~ SEC_READONLY; |
| 1088 | |
| 1089 | /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */ |
| 1090 | if (! first |
| 1091 | && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS)) |
| 1092 | != (flags & (SEC_MERGE | SEC_STRINGS)) |
| 1093 | || ((flags & SEC_MERGE) |
| 1094 | && section->output_section->entsize != section->entsize))) |
| 1095 | { |
| 1096 | section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS); |
| 1097 | flags &= ~ (SEC_MERGE | SEC_STRINGS); |
| 1098 | } |
| 1099 | |
| 1100 | /* For now make .tbss normal section. */ |
| 1101 | if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocatable) |
| 1102 | flags |= SEC_LOAD; |
| 1103 | |
| 1104 | section->output_section->flags |= flags; |
| 1105 | |
| 1106 | if (flags & SEC_MERGE) |
| 1107 | section->output_section->entsize = section->entsize; |
| 1108 | |
| 1109 | /* If SEC_READONLY is not set in the input section, then clear |
| 1110 | it from the output section. */ |
| 1111 | if ((section->flags & SEC_READONLY) == 0) |
| 1112 | section->output_section->flags &= ~SEC_READONLY; |
| 1113 | |
| 1114 | switch (output->sectype) |
| 1115 | { |
| 1116 | case normal_section: |
| 1117 | break; |
| 1118 | case dsect_section: |
| 1119 | case copy_section: |
| 1120 | case info_section: |
| 1121 | case overlay_section: |
| 1122 | output->bfd_section->flags &= ~SEC_ALLOC; |
| 1123 | break; |
| 1124 | case noload_section: |
| 1125 | output->bfd_section->flags &= ~SEC_LOAD; |
| 1126 | output->bfd_section->flags |= SEC_NEVER_LOAD; |
| 1127 | break; |
| 1128 | } |
| 1129 | |
| 1130 | /* Copy over SEC_SMALL_DATA. */ |
| 1131 | if (section->flags & SEC_SMALL_DATA) |
| 1132 | section->output_section->flags |= SEC_SMALL_DATA; |
| 1133 | |
| 1134 | if (section->alignment_power > output->bfd_section->alignment_power) |
| 1135 | output->bfd_section->alignment_power = section->alignment_power; |
| 1136 | |
| 1137 | /* If supplied an alignment, then force it. */ |
| 1138 | if (output->section_alignment != -1) |
| 1139 | output->bfd_section->alignment_power = output->section_alignment; |
| 1140 | |
| 1141 | if (section->flags & SEC_BLOCK) |
| 1142 | { |
| 1143 | section->output_section->flags |= SEC_BLOCK; |
| 1144 | /* FIXME: This value should really be obtained from the bfd... */ |
| 1145 | output->block_value = 128; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* Handle wildcard sorting. This returns the lang_input_section which |
| 1151 | should follow the one we are going to create for SECTION and FILE, |
| 1152 | based on the sorting requirements of WILD. It returns NULL if the |
| 1153 | new section should just go at the end of the current list. */ |
| 1154 | |
| 1155 | static lang_statement_union_type * |
| 1156 | wild_sort (lang_wild_statement_type *wild, |
| 1157 | struct wildcard_list *sec, |
| 1158 | lang_input_statement_type *file, |
| 1159 | asection *section) |
| 1160 | { |
| 1161 | const char *section_name; |
| 1162 | lang_statement_union_type *l; |
| 1163 | |
| 1164 | if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted)) |
| 1165 | return NULL; |
| 1166 | |
| 1167 | section_name = bfd_get_section_name (file->the_bfd, section); |
| 1168 | for (l = wild->children.head; l != NULL; l = l->header.next) |
| 1169 | { |
| 1170 | lang_input_section_type *ls; |
| 1171 | |
| 1172 | if (l->header.type != lang_input_section_enum) |
| 1173 | continue; |
| 1174 | ls = &l->input_section; |
| 1175 | |
| 1176 | /* Sorting by filename takes precedence over sorting by section |
| 1177 | name. */ |
| 1178 | |
| 1179 | if (wild->filenames_sorted) |
| 1180 | { |
| 1181 | const char *fn, *ln; |
| 1182 | bfd_boolean fa, la; |
| 1183 | int i; |
| 1184 | |
| 1185 | /* The PE support for the .idata section as generated by |
| 1186 | dlltool assumes that files will be sorted by the name of |
| 1187 | the archive and then the name of the file within the |
| 1188 | archive. */ |
| 1189 | |
| 1190 | if (file->the_bfd != NULL |
| 1191 | && bfd_my_archive (file->the_bfd) != NULL) |
| 1192 | { |
| 1193 | fn = bfd_get_filename (bfd_my_archive (file->the_bfd)); |
| 1194 | fa = TRUE; |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | fn = file->filename; |
| 1199 | fa = FALSE; |
| 1200 | } |
| 1201 | |
| 1202 | if (ls->ifile->the_bfd != NULL |
| 1203 | && bfd_my_archive (ls->ifile->the_bfd) != NULL) |
| 1204 | { |
| 1205 | ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd)); |
| 1206 | la = TRUE; |
| 1207 | } |
| 1208 | else |
| 1209 | { |
| 1210 | ln = ls->ifile->filename; |
| 1211 | la = FALSE; |
| 1212 | } |
| 1213 | |
| 1214 | i = strcmp (fn, ln); |
| 1215 | if (i > 0) |
| 1216 | continue; |
| 1217 | else if (i < 0) |
| 1218 | break; |
| 1219 | |
| 1220 | if (fa || la) |
| 1221 | { |
| 1222 | if (fa) |
| 1223 | fn = file->filename; |
| 1224 | if (la) |
| 1225 | ln = ls->ifile->filename; |
| 1226 | |
| 1227 | i = strcmp (fn, ln); |
| 1228 | if (i > 0) |
| 1229 | continue; |
| 1230 | else if (i < 0) |
| 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* Here either the files are not sorted by name, or we are |
| 1236 | looking at the sections for this file. */ |
| 1237 | |
| 1238 | if (sec != NULL && sec->spec.sorted) |
| 1239 | { |
| 1240 | if (strcmp (section_name, |
| 1241 | bfd_get_section_name (ls->ifile->the_bfd, |
| 1242 | ls->section)) |
| 1243 | < 0) |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | return l; |
| 1249 | } |
| 1250 | |
| 1251 | /* Expand a wild statement for a particular FILE. SECTION may be |
| 1252 | NULL, in which case it is a wild card. */ |
| 1253 | |
| 1254 | static void |
| 1255 | output_section_callback (lang_wild_statement_type *ptr, |
| 1256 | struct wildcard_list *sec, |
| 1257 | asection *section, |
| 1258 | lang_input_statement_type *file, |
| 1259 | void *output) |
| 1260 | { |
| 1261 | lang_statement_union_type *before; |
| 1262 | |
| 1263 | /* Exclude sections that match UNIQUE_SECTION_LIST. */ |
| 1264 | if (unique_section_p (bfd_get_section_name (file->the_bfd, section))) |
| 1265 | return; |
| 1266 | |
| 1267 | /* If the wild pattern was marked KEEP, the member sections |
| 1268 | should be as well. */ |
| 1269 | if (ptr->keep_sections) |
| 1270 | section->flags |= SEC_KEEP; |
| 1271 | |
| 1272 | before = wild_sort (ptr, sec, file, section); |
| 1273 | |
| 1274 | /* Here BEFORE points to the lang_input_section which |
| 1275 | should follow the one we are about to add. If BEFORE |
| 1276 | is NULL, then the section should just go at the end |
| 1277 | of the current list. */ |
| 1278 | |
| 1279 | if (before == NULL) |
| 1280 | lang_add_section (&ptr->children, section, |
| 1281 | (lang_output_section_statement_type *) output, |
| 1282 | file); |
| 1283 | else |
| 1284 | { |
| 1285 | lang_statement_list_type list; |
| 1286 | lang_statement_union_type **pp; |
| 1287 | |
| 1288 | lang_list_init (&list); |
| 1289 | lang_add_section (&list, section, |
| 1290 | (lang_output_section_statement_type *) output, |
| 1291 | file); |
| 1292 | |
| 1293 | /* If we are discarding the section, LIST.HEAD will |
| 1294 | be NULL. */ |
| 1295 | if (list.head != NULL) |
| 1296 | { |
| 1297 | ASSERT (list.head->header.next == NULL); |
| 1298 | |
| 1299 | for (pp = &ptr->children.head; |
| 1300 | *pp != before; |
| 1301 | pp = &(*pp)->header.next) |
| 1302 | ASSERT (*pp != NULL); |
| 1303 | |
| 1304 | list.head->header.next = *pp; |
| 1305 | *pp = list.head; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* This is passed a file name which must have been seen already and |
| 1311 | added to the statement tree. We will see if it has been opened |
| 1312 | already and had its symbols read. If not then we'll read it. */ |
| 1313 | |
| 1314 | static lang_input_statement_type * |
| 1315 | lookup_name (const char *name) |
| 1316 | { |
| 1317 | lang_input_statement_type *search; |
| 1318 | |
| 1319 | for (search = (lang_input_statement_type *) input_file_chain.head; |
| 1320 | search != NULL; |
| 1321 | search = (lang_input_statement_type *) search->next_real_file) |
| 1322 | { |
| 1323 | if (search->filename == NULL && name == NULL) |
| 1324 | return search; |
| 1325 | if (search->filename != NULL |
| 1326 | && name != NULL |
| 1327 | && strcmp (search->filename, name) == 0) |
| 1328 | break; |
| 1329 | } |
| 1330 | |
| 1331 | if (search == NULL) |
| 1332 | search = new_afile (name, lang_input_file_is_file_enum, default_target, |
| 1333 | FALSE); |
| 1334 | |
| 1335 | /* If we have already added this file, or this file is not real |
| 1336 | (FIXME: can that ever actually happen?) or the name is NULL |
| 1337 | (FIXME: can that ever actually happen?) don't add this file. */ |
| 1338 | if (search->loaded |
| 1339 | || ! search->real |
| 1340 | || search->filename == NULL) |
| 1341 | return search; |
| 1342 | |
| 1343 | if (! load_symbols (search, NULL)) |
| 1344 | return NULL; |
| 1345 | |
| 1346 | return search; |
| 1347 | } |
| 1348 | |
| 1349 | /* Get the symbols for an input file. */ |
| 1350 | |
| 1351 | static bfd_boolean |
| 1352 | load_symbols (lang_input_statement_type *entry, |
| 1353 | lang_statement_list_type *place) |
| 1354 | { |
| 1355 | char **matching; |
| 1356 | |
| 1357 | if (entry->loaded) |
| 1358 | return TRUE; |
| 1359 | |
| 1360 | ldfile_open_file (entry); |
| 1361 | |
| 1362 | if (! bfd_check_format (entry->the_bfd, bfd_archive) |
| 1363 | && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching)) |
| 1364 | { |
| 1365 | bfd_error_type err; |
| 1366 | lang_statement_list_type *hold; |
| 1367 | bfd_boolean bad_load = TRUE; |
| 1368 | bfd_boolean save_ldlang_sysrooted_script; |
| 1369 | |
| 1370 | err = bfd_get_error (); |
| 1371 | |
| 1372 | /* See if the emulation has some special knowledge. */ |
| 1373 | if (ldemul_unrecognized_file (entry)) |
| 1374 | return TRUE; |
| 1375 | |
| 1376 | if (err == bfd_error_file_ambiguously_recognized) |
| 1377 | { |
| 1378 | char **p; |
| 1379 | |
| 1380 | einfo (_("%B: file not recognized: %E\n"), entry->the_bfd); |
| 1381 | einfo (_("%B: matching formats:"), entry->the_bfd); |
| 1382 | for (p = matching; *p != NULL; p++) |
| 1383 | einfo (" %s", *p); |
| 1384 | einfo ("%F\n"); |
| 1385 | } |
| 1386 | else if (err != bfd_error_file_not_recognized |
| 1387 | || place == NULL) |
| 1388 | einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd); |
| 1389 | else |
| 1390 | bad_load = FALSE; |
| 1391 | |
| 1392 | bfd_close (entry->the_bfd); |
| 1393 | entry->the_bfd = NULL; |
| 1394 | |
| 1395 | /* Try to interpret the file as a linker script. */ |
| 1396 | ldfile_open_command_file (entry->filename); |
| 1397 | |
| 1398 | hold = stat_ptr; |
| 1399 | stat_ptr = place; |
| 1400 | save_ldlang_sysrooted_script = ldlang_sysrooted_script; |
| 1401 | ldlang_sysrooted_script = entry->sysrooted; |
| 1402 | |
| 1403 | ldfile_assumed_script = TRUE; |
| 1404 | parser_input = input_script; |
| 1405 | yyparse (); |
| 1406 | ldfile_assumed_script = FALSE; |
| 1407 | |
| 1408 | ldlang_sysrooted_script = save_ldlang_sysrooted_script; |
| 1409 | stat_ptr = hold; |
| 1410 | |
| 1411 | return ! bad_load; |
| 1412 | } |
| 1413 | |
| 1414 | if (ldemul_recognized_file (entry)) |
| 1415 | return TRUE; |
| 1416 | |
| 1417 | /* We don't call ldlang_add_file for an archive. Instead, the |
| 1418 | add_symbols entry point will call ldlang_add_file, via the |
| 1419 | add_archive_element callback, for each element of the archive |
| 1420 | which is used. */ |
| 1421 | switch (bfd_get_format (entry->the_bfd)) |
| 1422 | { |
| 1423 | default: |
| 1424 | break; |
| 1425 | |
| 1426 | case bfd_object: |
| 1427 | ldlang_add_file (entry); |
| 1428 | if (trace_files || trace_file_tries) |
| 1429 | info_msg ("%I\n", entry); |
| 1430 | break; |
| 1431 | |
| 1432 | case bfd_archive: |
| 1433 | if (entry->whole_archive) |
| 1434 | { |
| 1435 | bfd *member = NULL; |
| 1436 | bfd_boolean loaded = TRUE; |
| 1437 | |
| 1438 | for (;;) |
| 1439 | { |
| 1440 | member = bfd_openr_next_archived_file (entry->the_bfd, member); |
| 1441 | |
| 1442 | if (member == NULL) |
| 1443 | break; |
| 1444 | |
| 1445 | if (! bfd_check_format (member, bfd_object)) |
| 1446 | { |
| 1447 | einfo (_("%F%B: member %B in archive is not an object\n"), |
| 1448 | entry->the_bfd, member); |
| 1449 | loaded = FALSE; |
| 1450 | } |
| 1451 | |
| 1452 | if (! ((*link_info.callbacks->add_archive_element) |
| 1453 | (&link_info, member, "--whole-archive"))) |
| 1454 | abort (); |
| 1455 | |
| 1456 | if (! bfd_link_add_symbols (member, &link_info)) |
| 1457 | { |
| 1458 | einfo (_("%F%B: could not read symbols: %E\n"), member); |
| 1459 | loaded = FALSE; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | entry->loaded = loaded; |
| 1464 | return loaded; |
| 1465 | } |
| 1466 | break; |
| 1467 | } |
| 1468 | |
| 1469 | if (bfd_link_add_symbols (entry->the_bfd, &link_info)) |
| 1470 | entry->loaded = TRUE; |
| 1471 | else |
| 1472 | einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd); |
| 1473 | |
| 1474 | return entry->loaded; |
| 1475 | } |
| 1476 | |
| 1477 | /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both |
| 1478 | may be NULL, indicating that it is a wildcard. Separate |
| 1479 | lang_input_section statements are created for each part of the |
| 1480 | expansion; they are added after the wild statement S. OUTPUT is |
| 1481 | the output section. */ |
| 1482 | |
| 1483 | static void |
| 1484 | wild (lang_wild_statement_type *s, |
| 1485 | const char *target ATTRIBUTE_UNUSED, |
| 1486 | lang_output_section_statement_type *output) |
| 1487 | { |
| 1488 | struct wildcard_list *sec; |
| 1489 | |
| 1490 | walk_wild (s, output_section_callback, output); |
| 1491 | |
| 1492 | for (sec = s->section_list; sec != NULL; sec = sec->next) |
| 1493 | { |
| 1494 | if (default_common_section != NULL) |
| 1495 | break; |
| 1496 | if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0) |
| 1497 | { |
| 1498 | /* Remember the section that common is going to in case we |
| 1499 | later get something which doesn't know where to put it. */ |
| 1500 | default_common_section = output; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | /* Return TRUE iff target is the sought target. */ |
| 1506 | |
| 1507 | static int |
| 1508 | get_target (const bfd_target *target, void *data) |
| 1509 | { |
| 1510 | const char *sought = data; |
| 1511 | |
| 1512 | return strcmp (target->name, sought) == 0; |
| 1513 | } |
| 1514 | |
| 1515 | /* Like strcpy() but convert to lower case as well. */ |
| 1516 | |
| 1517 | static void |
| 1518 | stricpy (char *dest, char *src) |
| 1519 | { |
| 1520 | char c; |
| 1521 | |
| 1522 | while ((c = *src++) != 0) |
| 1523 | *dest++ = TOLOWER (c); |
| 1524 | |
| 1525 | *dest = 0; |
| 1526 | } |
| 1527 | |
| 1528 | /* Remove the first occurrence of needle (if any) in haystack |
| 1529 | from haystack. */ |
| 1530 | |
| 1531 | static void |
| 1532 | strcut (char *haystack, char *needle) |
| 1533 | { |
| 1534 | haystack = strstr (haystack, needle); |
| 1535 | |
| 1536 | if (haystack) |
| 1537 | { |
| 1538 | char *src; |
| 1539 | |
| 1540 | for (src = haystack + strlen (needle); *src;) |
| 1541 | *haystack++ = *src++; |
| 1542 | |
| 1543 | *haystack = 0; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* Compare two target format name strings. |
| 1548 | Return a value indicating how "similar" they are. */ |
| 1549 | |
| 1550 | static int |
| 1551 | name_compare (char *first, char *second) |
| 1552 | { |
| 1553 | char *copy1; |
| 1554 | char *copy2; |
| 1555 | int result; |
| 1556 | |
| 1557 | copy1 = xmalloc (strlen (first) + 1); |
| 1558 | copy2 = xmalloc (strlen (second) + 1); |
| 1559 | |
| 1560 | /* Convert the names to lower case. */ |
| 1561 | stricpy (copy1, first); |
| 1562 | stricpy (copy2, second); |
| 1563 | |
| 1564 | /* Remove size and endian strings from the name. */ |
| 1565 | strcut (copy1, "big"); |
| 1566 | strcut (copy1, "little"); |
| 1567 | strcut (copy2, "big"); |
| 1568 | strcut (copy2, "little"); |
| 1569 | |
| 1570 | /* Return a value based on how many characters match, |
| 1571 | starting from the beginning. If both strings are |
| 1572 | the same then return 10 * their length. */ |
| 1573 | for (result = 0; copy1[result] == copy2[result]; result++) |
| 1574 | if (copy1[result] == 0) |
| 1575 | { |
| 1576 | result *= 10; |
| 1577 | break; |
| 1578 | } |
| 1579 | |
| 1580 | free (copy1); |
| 1581 | free (copy2); |
| 1582 | |
| 1583 | return result; |
| 1584 | } |
| 1585 | |
| 1586 | /* Set by closest_target_match() below. */ |
| 1587 | static const bfd_target *winner; |
| 1588 | |
| 1589 | /* Scan all the valid bfd targets looking for one that has the endianness |
| 1590 | requirement that was specified on the command line, and is the nearest |
| 1591 | match to the original output target. */ |
| 1592 | |
| 1593 | static int |
| 1594 | closest_target_match (const bfd_target *target, void *data) |
| 1595 | { |
| 1596 | const bfd_target *original = data; |
| 1597 | |
| 1598 | if (command_line.endian == ENDIAN_BIG |
| 1599 | && target->byteorder != BFD_ENDIAN_BIG) |
| 1600 | return 0; |
| 1601 | |
| 1602 | if (command_line.endian == ENDIAN_LITTLE |
| 1603 | && target->byteorder != BFD_ENDIAN_LITTLE) |
| 1604 | return 0; |
| 1605 | |
| 1606 | /* Must be the same flavour. */ |
| 1607 | if (target->flavour != original->flavour) |
| 1608 | return 0; |
| 1609 | |
| 1610 | /* If we have not found a potential winner yet, then record this one. */ |
| 1611 | if (winner == NULL) |
| 1612 | { |
| 1613 | winner = target; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | /* Oh dear, we now have two potential candidates for a successful match. |
| 1618 | Compare their names and choose the better one. */ |
| 1619 | if (name_compare (target->name, original->name) |
| 1620 | > name_compare (winner->name, original->name)) |
| 1621 | winner = target; |
| 1622 | |
| 1623 | /* Keep on searching until wqe have checked them all. */ |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | /* Return the BFD target format of the first input file. */ |
| 1628 | |
| 1629 | static char * |
| 1630 | get_first_input_target (void) |
| 1631 | { |
| 1632 | char *target = NULL; |
| 1633 | |
| 1634 | LANG_FOR_EACH_INPUT_STATEMENT (s) |
| 1635 | { |
| 1636 | if (s->header.type == lang_input_statement_enum |
| 1637 | && s->real) |
| 1638 | { |
| 1639 | ldfile_open_file (s); |
| 1640 | |
| 1641 | if (s->the_bfd != NULL |
| 1642 | && bfd_check_format (s->the_bfd, bfd_object)) |
| 1643 | { |
| 1644 | target = bfd_get_target (s->the_bfd); |
| 1645 | |
| 1646 | if (target != NULL) |
| 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | return target; |
| 1653 | } |
| 1654 | |
| 1655 | const char * |
| 1656 | lang_get_output_target (void) |
| 1657 | { |
| 1658 | const char *target; |
| 1659 | |
| 1660 | /* Has the user told us which output format to use? */ |
| 1661 | if (output_target != NULL) |
| 1662 | return output_target; |
| 1663 | |
| 1664 | /* No - has the current target been set to something other than |
| 1665 | the default? */ |
| 1666 | if (current_target != default_target) |
| 1667 | return current_target; |
| 1668 | |
| 1669 | /* No - can we determine the format of the first input file? */ |
| 1670 | target = get_first_input_target (); |
| 1671 | if (target != NULL) |
| 1672 | return target; |
| 1673 | |
| 1674 | /* Failed - use the default output target. */ |
| 1675 | return default_target; |
| 1676 | } |
| 1677 | |
| 1678 | /* Open the output file. */ |
| 1679 | |
| 1680 | static bfd * |
| 1681 | open_output (const char *name) |
| 1682 | { |
| 1683 | bfd *output; |
| 1684 | |
| 1685 | output_target = lang_get_output_target (); |
| 1686 | |
| 1687 | /* Has the user requested a particular endianness on the command |
| 1688 | line? */ |
| 1689 | if (command_line.endian != ENDIAN_UNSET) |
| 1690 | { |
| 1691 | const bfd_target *target; |
| 1692 | enum bfd_endian desired_endian; |
| 1693 | |
| 1694 | /* Get the chosen target. */ |
| 1695 | target = bfd_search_for_target (get_target, (void *) output_target); |
| 1696 | |
| 1697 | /* If the target is not supported, we cannot do anything. */ |
| 1698 | if (target != NULL) |
| 1699 | { |
| 1700 | if (command_line.endian == ENDIAN_BIG) |
| 1701 | desired_endian = BFD_ENDIAN_BIG; |
| 1702 | else |
| 1703 | desired_endian = BFD_ENDIAN_LITTLE; |
| 1704 | |
| 1705 | /* See if the target has the wrong endianness. This should |
| 1706 | not happen if the linker script has provided big and |
| 1707 | little endian alternatives, but some scrips don't do |
| 1708 | this. */ |
| 1709 | if (target->byteorder != desired_endian) |
| 1710 | { |
| 1711 | /* If it does, then see if the target provides |
| 1712 | an alternative with the correct endianness. */ |
| 1713 | if (target->alternative_target != NULL |
| 1714 | && (target->alternative_target->byteorder == desired_endian)) |
| 1715 | output_target = target->alternative_target->name; |
| 1716 | else |
| 1717 | { |
| 1718 | /* Try to find a target as similar as possible to |
| 1719 | the default target, but which has the desired |
| 1720 | endian characteristic. */ |
| 1721 | bfd_search_for_target (closest_target_match, |
| 1722 | (void *) target); |
| 1723 | |
| 1724 | /* Oh dear - we could not find any targets that |
| 1725 | satisfy our requirements. */ |
| 1726 | if (winner == NULL) |
| 1727 | einfo (_("%P: warning: could not find any targets that match endianness requirement\n")); |
| 1728 | else |
| 1729 | output_target = winner->name; |
| 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | output = bfd_openw (name, output_target); |
| 1736 | |
| 1737 | if (output == NULL) |
| 1738 | { |
| 1739 | if (bfd_get_error () == bfd_error_invalid_target) |
| 1740 | einfo (_("%P%F: target %s not found\n"), output_target); |
| 1741 | |
| 1742 | einfo (_("%P%F: cannot open output file %s: %E\n"), name); |
| 1743 | } |
| 1744 | |
| 1745 | delete_output_file_on_failure = TRUE; |
| 1746 | |
| 1747 | #if 0 |
| 1748 | output->flags |= D_PAGED; |
| 1749 | #endif |
| 1750 | |
| 1751 | if (! bfd_set_format (output, bfd_object)) |
| 1752 | einfo (_("%P%F:%s: can not make object file: %E\n"), name); |
| 1753 | if (! bfd_set_arch_mach (output, |
| 1754 | ldfile_output_architecture, |
| 1755 | ldfile_output_machine)) |
| 1756 | einfo (_("%P%F:%s: can not set architecture: %E\n"), name); |
| 1757 | |
| 1758 | link_info.hash = bfd_link_hash_table_create (output); |
| 1759 | if (link_info.hash == NULL) |
| 1760 | einfo (_("%P%F: can not create link hash table: %E\n")); |
| 1761 | |
| 1762 | bfd_set_gp_size (output, g_switch_value); |
| 1763 | return output; |
| 1764 | } |
| 1765 | |
| 1766 | static void |
| 1767 | ldlang_open_output (lang_statement_union_type *statement) |
| 1768 | { |
| 1769 | switch (statement->header.type) |
| 1770 | { |
| 1771 | case lang_output_statement_enum: |
| 1772 | ASSERT (output_bfd == NULL); |
| 1773 | output_bfd = open_output (statement->output_statement.name); |
| 1774 | ldemul_set_output_arch (); |
| 1775 | if (config.magic_demand_paged && !link_info.relocatable) |
| 1776 | output_bfd->flags |= D_PAGED; |
| 1777 | else |
| 1778 | output_bfd->flags &= ~D_PAGED; |
| 1779 | if (config.text_read_only) |
| 1780 | output_bfd->flags |= WP_TEXT; |
| 1781 | else |
| 1782 | output_bfd->flags &= ~WP_TEXT; |
| 1783 | if (link_info.traditional_format) |
| 1784 | output_bfd->flags |= BFD_TRADITIONAL_FORMAT; |
| 1785 | else |
| 1786 | output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT; |
| 1787 | break; |
| 1788 | |
| 1789 | case lang_target_statement_enum: |
| 1790 | current_target = statement->target_statement.target; |
| 1791 | break; |
| 1792 | default: |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /* Open all the input files. */ |
| 1798 | |
| 1799 | static void |
| 1800 | open_input_bfds (lang_statement_union_type *s, bfd_boolean force) |
| 1801 | { |
| 1802 | for (; s != NULL; s = s->header.next) |
| 1803 | { |
| 1804 | switch (s->header.type) |
| 1805 | { |
| 1806 | case lang_constructors_statement_enum: |
| 1807 | open_input_bfds (constructor_list.head, force); |
| 1808 | break; |
| 1809 | case lang_output_section_statement_enum: |
| 1810 | open_input_bfds (s->output_section_statement.children.head, force); |
| 1811 | break; |
| 1812 | case lang_wild_statement_enum: |
| 1813 | /* Maybe we should load the file's symbols. */ |
| 1814 | if (s->wild_statement.filename |
| 1815 | && ! wildcardp (s->wild_statement.filename)) |
| 1816 | (void) lookup_name (s->wild_statement.filename); |
| 1817 | open_input_bfds (s->wild_statement.children.head, force); |
| 1818 | break; |
| 1819 | case lang_group_statement_enum: |
| 1820 | { |
| 1821 | struct bfd_link_hash_entry *undefs; |
| 1822 | |
| 1823 | /* We must continually search the entries in the group |
| 1824 | until no new symbols are added to the list of undefined |
| 1825 | symbols. */ |
| 1826 | |
| 1827 | do |
| 1828 | { |
| 1829 | undefs = link_info.hash->undefs_tail; |
| 1830 | open_input_bfds (s->group_statement.children.head, TRUE); |
| 1831 | } |
| 1832 | while (undefs != link_info.hash->undefs_tail); |
| 1833 | } |
| 1834 | break; |
| 1835 | case lang_target_statement_enum: |
| 1836 | current_target = s->target_statement.target; |
| 1837 | break; |
| 1838 | case lang_input_statement_enum: |
| 1839 | if (s->input_statement.real) |
| 1840 | { |
| 1841 | lang_statement_list_type add; |
| 1842 | |
| 1843 | s->input_statement.target = current_target; |
| 1844 | |
| 1845 | /* If we are being called from within a group, and this |
| 1846 | is an archive which has already been searched, then |
| 1847 | force it to be researched unless the whole archive |
| 1848 | has been loaded already. */ |
| 1849 | if (force |
| 1850 | && !s->input_statement.whole_archive |
| 1851 | && s->input_statement.loaded |
| 1852 | && bfd_check_format (s->input_statement.the_bfd, |
| 1853 | bfd_archive)) |
| 1854 | s->input_statement.loaded = FALSE; |
| 1855 | |
| 1856 | lang_list_init (&add); |
| 1857 | |
| 1858 | if (! load_symbols (&s->input_statement, &add)) |
| 1859 | config.make_executable = FALSE; |
| 1860 | |
| 1861 | if (add.head != NULL) |
| 1862 | { |
| 1863 | *add.tail = s->header.next; |
| 1864 | s->header.next = add.head; |
| 1865 | } |
| 1866 | } |
| 1867 | break; |
| 1868 | default: |
| 1869 | break; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | /* If there are [COMMONS] statements, put a wild one into the bss |
| 1875 | section. */ |
| 1876 | |
| 1877 | static void |
| 1878 | lang_reasonable_defaults (void) |
| 1879 | { |
| 1880 | #if 0 |
| 1881 | lang_output_section_statement_lookup (".text"); |
| 1882 | lang_output_section_statement_lookup (".data"); |
| 1883 | |
| 1884 | default_common_section = lang_output_section_statement_lookup (".bss"); |
| 1885 | |
| 1886 | if (!placed_commons) |
| 1887 | { |
| 1888 | lang_wild_statement_type *new = |
| 1889 | new_stat (lang_wild_statement, |
| 1890 | &default_common_section->children); |
| 1891 | |
| 1892 | new->section_name = "COMMON"; |
| 1893 | new->filename = NULL; |
| 1894 | lang_list_init (&new->children); |
| 1895 | } |
| 1896 | #endif |
| 1897 | } |
| 1898 | |
| 1899 | /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */ |
| 1900 | |
| 1901 | void |
| 1902 | lang_track_definedness (const char *name) |
| 1903 | { |
| 1904 | if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL) |
| 1905 | einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name); |
| 1906 | } |
| 1907 | |
| 1908 | /* New-function for the definedness hash table. */ |
| 1909 | |
| 1910 | static struct bfd_hash_entry * |
| 1911 | lang_definedness_newfunc (struct bfd_hash_entry *entry, |
| 1912 | struct bfd_hash_table *table ATTRIBUTE_UNUSED, |
| 1913 | const char *name ATTRIBUTE_UNUSED) |
| 1914 | { |
| 1915 | struct lang_definedness_hash_entry *ret |
| 1916 | = (struct lang_definedness_hash_entry *) entry; |
| 1917 | |
| 1918 | if (ret == NULL) |
| 1919 | ret = (struct lang_definedness_hash_entry *) |
| 1920 | bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry)); |
| 1921 | |
| 1922 | if (ret == NULL) |
| 1923 | einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name); |
| 1924 | |
| 1925 | ret->iteration = -1; |
| 1926 | return &ret->root; |
| 1927 | } |
| 1928 | |
| 1929 | /* Return the iteration when the definition of NAME was last updated. A |
| 1930 | value of -1 means that the symbol is not defined in the linker script |
| 1931 | or the command line, but may be defined in the linker symbol table. */ |
| 1932 | |
| 1933 | int |
| 1934 | lang_symbol_definition_iteration (const char *name) |
| 1935 | { |
| 1936 | struct lang_definedness_hash_entry *defentry |
| 1937 | = (struct lang_definedness_hash_entry *) |
| 1938 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); |
| 1939 | |
| 1940 | /* We've already created this one on the presence of DEFINED in the |
| 1941 | script, so it can't be NULL unless something is borked elsewhere in |
| 1942 | the code. */ |
| 1943 | if (defentry == NULL) |
| 1944 | FAIL (); |
| 1945 | |
| 1946 | return defentry->iteration; |
| 1947 | } |
| 1948 | |
| 1949 | /* Update the definedness state of NAME. */ |
| 1950 | |
| 1951 | void |
| 1952 | lang_update_definedness (const char *name, struct bfd_link_hash_entry *h) |
| 1953 | { |
| 1954 | struct lang_definedness_hash_entry *defentry |
| 1955 | = (struct lang_definedness_hash_entry *) |
| 1956 | bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE); |
| 1957 | |
| 1958 | /* We don't keep track of symbols not tested with DEFINED. */ |
| 1959 | if (defentry == NULL) |
| 1960 | return; |
| 1961 | |
| 1962 | /* If the symbol was already defined, and not from an earlier statement |
| 1963 | iteration, don't update the definedness iteration, because that'd |
| 1964 | make the symbol seem defined in the linker script at this point, and |
| 1965 | it wasn't; it was defined in some object. If we do anyway, DEFINED |
| 1966 | would start to yield false before this point and the construct "sym = |
| 1967 | DEFINED (sym) ? sym : X;" would change sym to X despite being defined |
| 1968 | in an object. */ |
| 1969 | if (h->type != bfd_link_hash_undefined |
| 1970 | && h->type != bfd_link_hash_common |
| 1971 | && h->type != bfd_link_hash_new |
| 1972 | && defentry->iteration == -1) |
| 1973 | return; |
| 1974 | |
| 1975 | defentry->iteration = lang_statement_iteration; |
| 1976 | } |
| 1977 | |
| 1978 | /* Add the supplied name to the symbol table as an undefined reference. |
| 1979 | This is a two step process as the symbol table doesn't even exist at |
| 1980 | the time the ld command line is processed. First we put the name |
| 1981 | on a list, then, once the output file has been opened, transfer the |
| 1982 | name to the symbol table. */ |
| 1983 | |
| 1984 | typedef struct bfd_sym_chain ldlang_undef_chain_list_type; |
| 1985 | |
| 1986 | #define ldlang_undef_chain_list_head entry_symbol.next |
| 1987 | |
| 1988 | void |
| 1989 | ldlang_add_undef (const char *const name) |
| 1990 | { |
| 1991 | ldlang_undef_chain_list_type *new = |
| 1992 | stat_alloc (sizeof (ldlang_undef_chain_list_type)); |
| 1993 | |
| 1994 | new->next = ldlang_undef_chain_list_head; |
| 1995 | ldlang_undef_chain_list_head = new; |
| 1996 | |
| 1997 | new->name = xstrdup (name); |
| 1998 | |
| 1999 | if (output_bfd != NULL) |
| 2000 | insert_undefined (new->name); |
| 2001 | } |
| 2002 | |
| 2003 | /* Insert NAME as undefined in the symbol table. */ |
| 2004 | |
| 2005 | static void |
| 2006 | insert_undefined (const char *name) |
| 2007 | { |
| 2008 | struct bfd_link_hash_entry *h; |
| 2009 | |
| 2010 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE); |
| 2011 | if (h == NULL) |
| 2012 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
| 2013 | if (h->type == bfd_link_hash_new) |
| 2014 | { |
| 2015 | h->type = bfd_link_hash_undefined; |
| 2016 | h->u.undef.abfd = NULL; |
| 2017 | bfd_link_add_undef (link_info.hash, h); |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | /* Run through the list of undefineds created above and place them |
| 2022 | into the linker hash table as undefined symbols belonging to the |
| 2023 | script file. */ |
| 2024 | |
| 2025 | static void |
| 2026 | lang_place_undefineds (void) |
| 2027 | { |
| 2028 | ldlang_undef_chain_list_type *ptr; |
| 2029 | |
| 2030 | for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next) |
| 2031 | insert_undefined (ptr->name); |
| 2032 | } |
| 2033 | |
| 2034 | /* Open input files and attach to output sections. */ |
| 2035 | |
| 2036 | static void |
| 2037 | map_input_to_output_sections |
| 2038 | (lang_statement_union_type *s, const char *target, |
| 2039 | lang_output_section_statement_type *output_section_statement) |
| 2040 | { |
| 2041 | for (; s != NULL; s = s->header.next) |
| 2042 | { |
| 2043 | switch (s->header.type) |
| 2044 | { |
| 2045 | case lang_wild_statement_enum: |
| 2046 | wild (&s->wild_statement, target, output_section_statement); |
| 2047 | break; |
| 2048 | case lang_constructors_statement_enum: |
| 2049 | map_input_to_output_sections (constructor_list.head, |
| 2050 | target, |
| 2051 | output_section_statement); |
| 2052 | break; |
| 2053 | case lang_output_section_statement_enum: |
| 2054 | map_input_to_output_sections (s->output_section_statement.children.head, |
| 2055 | target, |
| 2056 | &s->output_section_statement); |
| 2057 | break; |
| 2058 | case lang_output_statement_enum: |
| 2059 | break; |
| 2060 | case lang_target_statement_enum: |
| 2061 | target = s->target_statement.target; |
| 2062 | break; |
| 2063 | case lang_group_statement_enum: |
| 2064 | map_input_to_output_sections (s->group_statement.children.head, |
| 2065 | target, |
| 2066 | output_section_statement); |
| 2067 | break; |
| 2068 | case lang_fill_statement_enum: |
| 2069 | case lang_input_section_enum: |
| 2070 | case lang_object_symbols_statement_enum: |
| 2071 | case lang_data_statement_enum: |
| 2072 | case lang_reloc_statement_enum: |
| 2073 | case lang_padding_statement_enum: |
| 2074 | case lang_input_statement_enum: |
| 2075 | if (output_section_statement != NULL |
| 2076 | && output_section_statement->bfd_section == NULL) |
| 2077 | init_os (output_section_statement); |
| 2078 | break; |
| 2079 | case lang_assignment_statement_enum: |
| 2080 | if (output_section_statement != NULL |
| 2081 | && output_section_statement->bfd_section == NULL) |
| 2082 | init_os (output_section_statement); |
| 2083 | |
| 2084 | /* Make sure that any sections mentioned in the assignment |
| 2085 | are initialized. */ |
| 2086 | exp_init_os (s->assignment_statement.exp); |
| 2087 | break; |
| 2088 | case lang_afile_asection_pair_statement_enum: |
| 2089 | FAIL (); |
| 2090 | break; |
| 2091 | case lang_address_statement_enum: |
| 2092 | /* Mark the specified section with the supplied address. */ |
| 2093 | { |
| 2094 | lang_output_section_statement_type *os = |
| 2095 | lang_output_section_statement_lookup |
| 2096 | (s->address_statement.section_name); |
| 2097 | |
| 2098 | if (os->bfd_section == NULL) |
| 2099 | init_os (os); |
| 2100 | os->addr_tree = s->address_statement.address; |
| 2101 | } |
| 2102 | break; |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* An output section might have been removed after its statement was |
| 2108 | added. For example, ldemul_before_allocation can remove dynamic |
| 2109 | sections if they turn out to be not needed. Clean them up here. */ |
| 2110 | |
| 2111 | static void |
| 2112 | strip_excluded_output_sections (void) |
| 2113 | { |
| 2114 | lang_statement_union_type *u; |
| 2115 | |
| 2116 | for (u = lang_output_section_statement.head; |
| 2117 | u != NULL; |
| 2118 | u = u->output_section_statement.next) |
| 2119 | { |
| 2120 | lang_output_section_statement_type *os; |
| 2121 | asection *s; |
| 2122 | |
| 2123 | os = &u->output_section_statement; |
| 2124 | s = os->bfd_section; |
| 2125 | if (s != NULL && (s->flags & SEC_EXCLUDE) != 0) |
| 2126 | { |
| 2127 | asection **p; |
| 2128 | |
| 2129 | os->bfd_section = NULL; |
| 2130 | |
| 2131 | for (p = &output_bfd->sections; *p; p = &(*p)->next) |
| 2132 | if (*p == s) |
| 2133 | { |
| 2134 | bfd_section_list_remove (output_bfd, p); |
| 2135 | output_bfd->section_count--; |
| 2136 | break; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | static void |
| 2143 | print_output_section_statement |
| 2144 | (lang_output_section_statement_type *output_section_statement) |
| 2145 | { |
| 2146 | asection *section = output_section_statement->bfd_section; |
| 2147 | int len; |
| 2148 | |
| 2149 | if (output_section_statement != abs_output_section) |
| 2150 | { |
| 2151 | minfo ("\n%s", output_section_statement->name); |
| 2152 | |
| 2153 | if (section != NULL) |
| 2154 | { |
| 2155 | print_dot = section->vma; |
| 2156 | |
| 2157 | len = strlen (output_section_statement->name); |
| 2158 | if (len >= SECTION_NAME_MAP_LENGTH - 1) |
| 2159 | { |
| 2160 | print_nl (); |
| 2161 | len = 0; |
| 2162 | } |
| 2163 | while (len < SECTION_NAME_MAP_LENGTH) |
| 2164 | { |
| 2165 | print_space (); |
| 2166 | ++len; |
| 2167 | } |
| 2168 | |
| 2169 | minfo ("0x%V %W", section->vma, section->_raw_size); |
| 2170 | |
| 2171 | if (output_section_statement->load_base != NULL) |
| 2172 | { |
| 2173 | bfd_vma addr; |
| 2174 | |
| 2175 | addr = exp_get_abs_int (output_section_statement->load_base, 0, |
| 2176 | "load base", lang_final_phase_enum); |
| 2177 | minfo (_(" load address 0x%V"), addr); |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | print_nl (); |
| 2182 | } |
| 2183 | |
| 2184 | print_statement_list (output_section_statement->children.head, |
| 2185 | output_section_statement); |
| 2186 | } |
| 2187 | |
| 2188 | static void |
| 2189 | print_assignment (lang_assignment_statement_type *assignment, |
| 2190 | lang_output_section_statement_type *output_section) |
| 2191 | { |
| 2192 | int i; |
| 2193 | etree_value_type result; |
| 2194 | |
| 2195 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 2196 | print_space (); |
| 2197 | |
| 2198 | result = exp_fold_tree (assignment->exp->assign.src, output_section, |
| 2199 | lang_final_phase_enum, print_dot, &print_dot); |
| 2200 | if (result.valid_p) |
| 2201 | { |
| 2202 | const char *dst; |
| 2203 | bfd_vma value; |
| 2204 | |
| 2205 | value = result.value + result.section->bfd_section->vma; |
| 2206 | dst = assignment->exp->assign.dst; |
| 2207 | |
| 2208 | minfo ("0x%V", value); |
| 2209 | if (dst[0] == '.' && dst[1] == 0) |
| 2210 | print_dot = value; |
| 2211 | } |
| 2212 | else |
| 2213 | { |
| 2214 | minfo ("*undef* "); |
| 2215 | #ifdef BFD64 |
| 2216 | minfo (" "); |
| 2217 | #endif |
| 2218 | } |
| 2219 | |
| 2220 | minfo (" "); |
| 2221 | |
| 2222 | exp_print_tree (assignment->exp); |
| 2223 | |
| 2224 | print_nl (); |
| 2225 | } |
| 2226 | |
| 2227 | static void |
| 2228 | print_input_statement (lang_input_statement_type *statm) |
| 2229 | { |
| 2230 | if (statm->filename != NULL) |
| 2231 | { |
| 2232 | fprintf (config.map_file, "LOAD %s\n", statm->filename); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | /* Print all symbols defined in a particular section. This is called |
| 2237 | via bfd_link_hash_traverse. */ |
| 2238 | |
| 2239 | static bfd_boolean |
| 2240 | print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr) |
| 2241 | { |
| 2242 | asection *sec = ptr; |
| 2243 | |
| 2244 | if ((hash_entry->type == bfd_link_hash_defined |
| 2245 | || hash_entry->type == bfd_link_hash_defweak) |
| 2246 | && sec == hash_entry->u.def.section) |
| 2247 | { |
| 2248 | int i; |
| 2249 | |
| 2250 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 2251 | print_space (); |
| 2252 | minfo ("0x%V ", |
| 2253 | (hash_entry->u.def.value |
| 2254 | + hash_entry->u.def.section->output_offset |
| 2255 | + hash_entry->u.def.section->output_section->vma)); |
| 2256 | |
| 2257 | minfo (" %T\n", hash_entry->root.string); |
| 2258 | } |
| 2259 | |
| 2260 | return TRUE; |
| 2261 | } |
| 2262 | |
| 2263 | /* Print information about an input section to the map file. */ |
| 2264 | |
| 2265 | static void |
| 2266 | print_input_section (lang_input_section_type *in) |
| 2267 | { |
| 2268 | asection *i = in->section; |
| 2269 | bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size; |
| 2270 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2271 | ldfile_output_machine); |
| 2272 | if (size != 0) |
| 2273 | { |
| 2274 | print_space (); |
| 2275 | |
| 2276 | minfo ("%s", i->name); |
| 2277 | |
| 2278 | if (i->output_section != NULL) |
| 2279 | { |
| 2280 | int len; |
| 2281 | |
| 2282 | len = 1 + strlen (i->name); |
| 2283 | if (len >= SECTION_NAME_MAP_LENGTH - 1) |
| 2284 | { |
| 2285 | print_nl (); |
| 2286 | len = 0; |
| 2287 | } |
| 2288 | while (len < SECTION_NAME_MAP_LENGTH) |
| 2289 | { |
| 2290 | print_space (); |
| 2291 | ++len; |
| 2292 | } |
| 2293 | |
| 2294 | minfo ("0x%V %W %B\n", |
| 2295 | i->output_section->vma + i->output_offset, size / opb, |
| 2296 | i->owner); |
| 2297 | |
| 2298 | if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size) |
| 2299 | { |
| 2300 | len = SECTION_NAME_MAP_LENGTH + 3; |
| 2301 | #ifdef BFD64 |
| 2302 | len += 16; |
| 2303 | #else |
| 2304 | len += 8; |
| 2305 | #endif |
| 2306 | while (len > 0) |
| 2307 | { |
| 2308 | print_space (); |
| 2309 | --len; |
| 2310 | } |
| 2311 | |
| 2312 | minfo (_("%W (size before relaxing)\n"), i->_raw_size); |
| 2313 | } |
| 2314 | |
| 2315 | bfd_link_hash_traverse (link_info.hash, print_one_symbol, i); |
| 2316 | |
| 2317 | print_dot = i->output_section->vma + i->output_offset + size / opb; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | static void |
| 2323 | print_fill_statement (lang_fill_statement_type *fill) |
| 2324 | { |
| 2325 | size_t size; |
| 2326 | unsigned char *p; |
| 2327 | fputs (" FILL mask 0x", config.map_file); |
| 2328 | for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--) |
| 2329 | fprintf (config.map_file, "%02x", *p); |
| 2330 | fputs ("\n", config.map_file); |
| 2331 | } |
| 2332 | |
| 2333 | static void |
| 2334 | print_data_statement (lang_data_statement_type *data) |
| 2335 | { |
| 2336 | int i; |
| 2337 | bfd_vma addr; |
| 2338 | bfd_size_type size; |
| 2339 | const char *name; |
| 2340 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2341 | ldfile_output_machine); |
| 2342 | |
| 2343 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 2344 | print_space (); |
| 2345 | |
| 2346 | addr = data->output_vma; |
| 2347 | if (data->output_section != NULL) |
| 2348 | addr += data->output_section->vma; |
| 2349 | |
| 2350 | switch (data->type) |
| 2351 | { |
| 2352 | default: |
| 2353 | abort (); |
| 2354 | case BYTE: |
| 2355 | size = BYTE_SIZE; |
| 2356 | name = "BYTE"; |
| 2357 | break; |
| 2358 | case SHORT: |
| 2359 | size = SHORT_SIZE; |
| 2360 | name = "SHORT"; |
| 2361 | break; |
| 2362 | case LONG: |
| 2363 | size = LONG_SIZE; |
| 2364 | name = "LONG"; |
| 2365 | break; |
| 2366 | case QUAD: |
| 2367 | size = QUAD_SIZE; |
| 2368 | name = "QUAD"; |
| 2369 | break; |
| 2370 | case SQUAD: |
| 2371 | size = QUAD_SIZE; |
| 2372 | name = "SQUAD"; |
| 2373 | break; |
| 2374 | } |
| 2375 | |
| 2376 | minfo ("0x%V %W %s 0x%v", addr, size, name, data->value); |
| 2377 | |
| 2378 | if (data->exp->type.node_class != etree_value) |
| 2379 | { |
| 2380 | print_space (); |
| 2381 | exp_print_tree (data->exp); |
| 2382 | } |
| 2383 | |
| 2384 | print_nl (); |
| 2385 | |
| 2386 | print_dot = addr + size / opb; |
| 2387 | |
| 2388 | } |
| 2389 | |
| 2390 | /* Print an address statement. These are generated by options like |
| 2391 | -Ttext. */ |
| 2392 | |
| 2393 | static void |
| 2394 | print_address_statement (lang_address_statement_type *address) |
| 2395 | { |
| 2396 | minfo (_("Address of section %s set to "), address->section_name); |
| 2397 | exp_print_tree (address->address); |
| 2398 | print_nl (); |
| 2399 | } |
| 2400 | |
| 2401 | /* Print a reloc statement. */ |
| 2402 | |
| 2403 | static void |
| 2404 | print_reloc_statement (lang_reloc_statement_type *reloc) |
| 2405 | { |
| 2406 | int i; |
| 2407 | bfd_vma addr; |
| 2408 | bfd_size_type size; |
| 2409 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2410 | ldfile_output_machine); |
| 2411 | |
| 2412 | for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++) |
| 2413 | print_space (); |
| 2414 | |
| 2415 | addr = reloc->output_vma; |
| 2416 | if (reloc->output_section != NULL) |
| 2417 | addr += reloc->output_section->vma; |
| 2418 | |
| 2419 | size = bfd_get_reloc_size (reloc->howto); |
| 2420 | |
| 2421 | minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name); |
| 2422 | |
| 2423 | if (reloc->name != NULL) |
| 2424 | minfo ("%s+", reloc->name); |
| 2425 | else |
| 2426 | minfo ("%s+", reloc->section->name); |
| 2427 | |
| 2428 | exp_print_tree (reloc->addend_exp); |
| 2429 | |
| 2430 | print_nl (); |
| 2431 | |
| 2432 | print_dot = addr + size / opb; |
| 2433 | } |
| 2434 | |
| 2435 | static void |
| 2436 | print_padding_statement (lang_padding_statement_type *s) |
| 2437 | { |
| 2438 | int len; |
| 2439 | bfd_vma addr; |
| 2440 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2441 | ldfile_output_machine); |
| 2442 | |
| 2443 | minfo (" *fill*"); |
| 2444 | |
| 2445 | len = sizeof " *fill*" - 1; |
| 2446 | while (len < SECTION_NAME_MAP_LENGTH) |
| 2447 | { |
| 2448 | print_space (); |
| 2449 | ++len; |
| 2450 | } |
| 2451 | |
| 2452 | addr = s->output_offset; |
| 2453 | if (s->output_section != NULL) |
| 2454 | addr += s->output_section->vma; |
| 2455 | minfo ("0x%V %W ", addr, s->size); |
| 2456 | |
| 2457 | if (s->fill->size != 0) |
| 2458 | { |
| 2459 | size_t size; |
| 2460 | unsigned char *p; |
| 2461 | for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--) |
| 2462 | fprintf (config.map_file, "%02x", *p); |
| 2463 | } |
| 2464 | |
| 2465 | print_nl (); |
| 2466 | |
| 2467 | print_dot = addr + s->size / opb; |
| 2468 | } |
| 2469 | |
| 2470 | static void |
| 2471 | print_wild_statement (lang_wild_statement_type *w, |
| 2472 | lang_output_section_statement_type *os) |
| 2473 | { |
| 2474 | struct wildcard_list *sec; |
| 2475 | |
| 2476 | print_space (); |
| 2477 | |
| 2478 | if (w->filenames_sorted) |
| 2479 | minfo ("SORT("); |
| 2480 | if (w->filename != NULL) |
| 2481 | minfo ("%s", w->filename); |
| 2482 | else |
| 2483 | minfo ("*"); |
| 2484 | if (w->filenames_sorted) |
| 2485 | minfo (")"); |
| 2486 | |
| 2487 | minfo ("("); |
| 2488 | for (sec = w->section_list; sec; sec = sec->next) |
| 2489 | { |
| 2490 | if (sec->spec.sorted) |
| 2491 | minfo ("SORT("); |
| 2492 | if (sec->spec.exclude_name_list != NULL) |
| 2493 | { |
| 2494 | name_list *tmp; |
| 2495 | minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name); |
| 2496 | for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next) |
| 2497 | minfo (" %s", tmp->name); |
| 2498 | minfo (") "); |
| 2499 | } |
| 2500 | if (sec->spec.name != NULL) |
| 2501 | minfo ("%s", sec->spec.name); |
| 2502 | else |
| 2503 | minfo ("*"); |
| 2504 | if (sec->spec.sorted) |
| 2505 | minfo (")"); |
| 2506 | if (sec->next) |
| 2507 | minfo (" "); |
| 2508 | } |
| 2509 | minfo (")"); |
| 2510 | |
| 2511 | print_nl (); |
| 2512 | |
| 2513 | print_statement_list (w->children.head, os); |
| 2514 | } |
| 2515 | |
| 2516 | /* Print a group statement. */ |
| 2517 | |
| 2518 | static void |
| 2519 | print_group (lang_group_statement_type *s, |
| 2520 | lang_output_section_statement_type *os) |
| 2521 | { |
| 2522 | fprintf (config.map_file, "START GROUP\n"); |
| 2523 | print_statement_list (s->children.head, os); |
| 2524 | fprintf (config.map_file, "END GROUP\n"); |
| 2525 | } |
| 2526 | |
| 2527 | /* Print the list of statements in S. |
| 2528 | This can be called for any statement type. */ |
| 2529 | |
| 2530 | static void |
| 2531 | print_statement_list (lang_statement_union_type *s, |
| 2532 | lang_output_section_statement_type *os) |
| 2533 | { |
| 2534 | while (s != NULL) |
| 2535 | { |
| 2536 | print_statement (s, os); |
| 2537 | s = s->header.next; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | /* Print the first statement in statement list S. |
| 2542 | This can be called for any statement type. */ |
| 2543 | |
| 2544 | static void |
| 2545 | print_statement (lang_statement_union_type *s, |
| 2546 | lang_output_section_statement_type *os) |
| 2547 | { |
| 2548 | switch (s->header.type) |
| 2549 | { |
| 2550 | default: |
| 2551 | fprintf (config.map_file, _("Fail with %d\n"), s->header.type); |
| 2552 | FAIL (); |
| 2553 | break; |
| 2554 | case lang_constructors_statement_enum: |
| 2555 | if (constructor_list.head != NULL) |
| 2556 | { |
| 2557 | if (constructors_sorted) |
| 2558 | minfo (" SORT (CONSTRUCTORS)\n"); |
| 2559 | else |
| 2560 | minfo (" CONSTRUCTORS\n"); |
| 2561 | print_statement_list (constructor_list.head, os); |
| 2562 | } |
| 2563 | break; |
| 2564 | case lang_wild_statement_enum: |
| 2565 | print_wild_statement (&s->wild_statement, os); |
| 2566 | break; |
| 2567 | case lang_address_statement_enum: |
| 2568 | print_address_statement (&s->address_statement); |
| 2569 | break; |
| 2570 | case lang_object_symbols_statement_enum: |
| 2571 | minfo (" CREATE_OBJECT_SYMBOLS\n"); |
| 2572 | break; |
| 2573 | case lang_fill_statement_enum: |
| 2574 | print_fill_statement (&s->fill_statement); |
| 2575 | break; |
| 2576 | case lang_data_statement_enum: |
| 2577 | print_data_statement (&s->data_statement); |
| 2578 | break; |
| 2579 | case lang_reloc_statement_enum: |
| 2580 | print_reloc_statement (&s->reloc_statement); |
| 2581 | break; |
| 2582 | case lang_input_section_enum: |
| 2583 | print_input_section (&s->input_section); |
| 2584 | break; |
| 2585 | case lang_padding_statement_enum: |
| 2586 | print_padding_statement (&s->padding_statement); |
| 2587 | break; |
| 2588 | case lang_output_section_statement_enum: |
| 2589 | print_output_section_statement (&s->output_section_statement); |
| 2590 | break; |
| 2591 | case lang_assignment_statement_enum: |
| 2592 | print_assignment (&s->assignment_statement, os); |
| 2593 | break; |
| 2594 | case lang_target_statement_enum: |
| 2595 | fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target); |
| 2596 | break; |
| 2597 | case lang_output_statement_enum: |
| 2598 | minfo ("OUTPUT(%s", s->output_statement.name); |
| 2599 | if (output_target != NULL) |
| 2600 | minfo (" %s", output_target); |
| 2601 | minfo (")\n"); |
| 2602 | break; |
| 2603 | case lang_input_statement_enum: |
| 2604 | print_input_statement (&s->input_statement); |
| 2605 | break; |
| 2606 | case lang_group_statement_enum: |
| 2607 | print_group (&s->group_statement, os); |
| 2608 | break; |
| 2609 | case lang_afile_asection_pair_statement_enum: |
| 2610 | FAIL (); |
| 2611 | break; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | static void |
| 2616 | print_statements (void) |
| 2617 | { |
| 2618 | print_statement_list (statement_list.head, abs_output_section); |
| 2619 | } |
| 2620 | |
| 2621 | /* Print the first N statements in statement list S to STDERR. |
| 2622 | If N == 0, nothing is printed. |
| 2623 | If N < 0, the entire list is printed. |
| 2624 | Intended to be called from GDB. */ |
| 2625 | |
| 2626 | void |
| 2627 | dprint_statement (lang_statement_union_type *s, int n) |
| 2628 | { |
| 2629 | FILE *map_save = config.map_file; |
| 2630 | |
| 2631 | config.map_file = stderr; |
| 2632 | |
| 2633 | if (n < 0) |
| 2634 | print_statement_list (s, abs_output_section); |
| 2635 | else |
| 2636 | { |
| 2637 | while (s && --n >= 0) |
| 2638 | { |
| 2639 | print_statement (s, abs_output_section); |
| 2640 | s = s->header.next; |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | config.map_file = map_save; |
| 2645 | } |
| 2646 | |
| 2647 | static void |
| 2648 | insert_pad (lang_statement_union_type **ptr, |
| 2649 | fill_type *fill, |
| 2650 | unsigned int alignment_needed, |
| 2651 | asection *output_section, |
| 2652 | bfd_vma dot) |
| 2653 | { |
| 2654 | static fill_type zero_fill = { 1, { 0 } }; |
| 2655 | lang_statement_union_type *pad; |
| 2656 | |
| 2657 | pad = ((lang_statement_union_type *) |
| 2658 | ((char *) ptr - offsetof (lang_statement_union_type, header.next))); |
| 2659 | if (ptr != &statement_list.head |
| 2660 | && pad->header.type == lang_padding_statement_enum |
| 2661 | && pad->padding_statement.output_section == output_section) |
| 2662 | { |
| 2663 | /* Use the existing pad statement. The above test on output |
| 2664 | section is probably redundant, but it doesn't hurt to check. */ |
| 2665 | } |
| 2666 | else |
| 2667 | { |
| 2668 | /* Make a new padding statement, linked into existing chain. */ |
| 2669 | pad = stat_alloc (sizeof (lang_padding_statement_type)); |
| 2670 | pad->header.next = *ptr; |
| 2671 | *ptr = pad; |
| 2672 | pad->header.type = lang_padding_statement_enum; |
| 2673 | pad->padding_statement.output_section = output_section; |
| 2674 | if (fill == NULL) |
| 2675 | fill = &zero_fill; |
| 2676 | pad->padding_statement.fill = fill; |
| 2677 | } |
| 2678 | pad->padding_statement.output_offset = dot - output_section->vma; |
| 2679 | pad->padding_statement.size = alignment_needed; |
| 2680 | output_section->_raw_size += alignment_needed; |
| 2681 | } |
| 2682 | |
| 2683 | /* Work out how much this section will move the dot point. */ |
| 2684 | |
| 2685 | static bfd_vma |
| 2686 | size_input_section (lang_statement_union_type **this_ptr, |
| 2687 | lang_output_section_statement_type *output_section_statement, |
| 2688 | fill_type *fill, |
| 2689 | bfd_vma dot) |
| 2690 | { |
| 2691 | lang_input_section_type *is = &((*this_ptr)->input_section); |
| 2692 | asection *i = is->section; |
| 2693 | |
| 2694 | if (!is->ifile->just_syms_flag) |
| 2695 | { |
| 2696 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2697 | ldfile_output_machine); |
| 2698 | unsigned int alignment_needed; |
| 2699 | asection *o; |
| 2700 | |
| 2701 | /* Align this section first to the input sections requirement, |
| 2702 | then to the output section's requirement. If this alignment |
| 2703 | is greater than any seen before, then record it too. Perform |
| 2704 | the alignment by inserting a magic 'padding' statement. */ |
| 2705 | |
| 2706 | if (output_section_statement->subsection_alignment != -1) |
| 2707 | i->alignment_power = output_section_statement->subsection_alignment; |
| 2708 | |
| 2709 | o = output_section_statement->bfd_section; |
| 2710 | if (o->alignment_power < i->alignment_power) |
| 2711 | o->alignment_power = i->alignment_power; |
| 2712 | |
| 2713 | alignment_needed = align_power (dot, i->alignment_power) - dot; |
| 2714 | |
| 2715 | if (alignment_needed != 0) |
| 2716 | { |
| 2717 | insert_pad (this_ptr, fill, alignment_needed * opb, o, dot); |
| 2718 | dot += alignment_needed; |
| 2719 | } |
| 2720 | |
| 2721 | /* Remember where in the output section this input section goes. */ |
| 2722 | |
| 2723 | i->output_offset = dot - o->vma; |
| 2724 | |
| 2725 | /* Mark how big the output section must be to contain this now. */ |
| 2726 | if (i->_cooked_size != 0) |
| 2727 | dot += i->_cooked_size / opb; |
| 2728 | else |
| 2729 | dot += i->_raw_size / opb; |
| 2730 | o->_raw_size = (dot - o->vma) * opb; |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | i->output_offset = i->vma - output_section_statement->bfd_section->vma; |
| 2735 | } |
| 2736 | |
| 2737 | return dot; |
| 2738 | } |
| 2739 | |
| 2740 | #define IGNORE_SECTION(bfd, s) \ |
| 2741 | (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \ |
| 2742 | != (SEC_ALLOC | SEC_LOAD)) \ |
| 2743 | || bfd_section_size (bfd, s) == 0) |
| 2744 | |
| 2745 | /* Check to see if any allocated sections overlap with other allocated |
| 2746 | sections. This can happen when the linker script specifically specifies |
| 2747 | the output section addresses of the two sections. */ |
| 2748 | |
| 2749 | static void |
| 2750 | lang_check_section_addresses (void) |
| 2751 | { |
| 2752 | asection *s; |
| 2753 | unsigned opb = bfd_octets_per_byte (output_bfd); |
| 2754 | |
| 2755 | /* Scan all sections in the output list. */ |
| 2756 | for (s = output_bfd->sections; s != NULL; s = s->next) |
| 2757 | { |
| 2758 | asection *os; |
| 2759 | |
| 2760 | /* Ignore sections which are not loaded or which have no contents. */ |
| 2761 | if (IGNORE_SECTION (output_bfd, s)) |
| 2762 | continue; |
| 2763 | |
| 2764 | /* Once we reach section 's' stop our seach. This prevents two |
| 2765 | warning messages from being produced, one for 'section A overlaps |
| 2766 | section B' and one for 'section B overlaps section A'. */ |
| 2767 | for (os = output_bfd->sections; os != s; os = os->next) |
| 2768 | { |
| 2769 | bfd_vma s_start; |
| 2770 | bfd_vma s_end; |
| 2771 | bfd_vma os_start; |
| 2772 | bfd_vma os_end; |
| 2773 | |
| 2774 | /* Only consider loadable sections with real contents. */ |
| 2775 | if (IGNORE_SECTION (output_bfd, os)) |
| 2776 | continue; |
| 2777 | |
| 2778 | /* We must check the sections' LMA addresses not their |
| 2779 | VMA addresses because overlay sections can have |
| 2780 | overlapping VMAs but they must have distinct LMAs. */ |
| 2781 | s_start = bfd_section_lma (output_bfd, s); |
| 2782 | os_start = bfd_section_lma (output_bfd, os); |
| 2783 | s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1; |
| 2784 | os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1; |
| 2785 | |
| 2786 | /* Look for an overlap. */ |
| 2787 | if ((s_end < os_start) || (s_start > os_end)) |
| 2788 | continue; |
| 2789 | |
| 2790 | einfo ( |
| 2791 | _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"), |
| 2792 | s->name, s_start, s_end, os->name, os_start, os_end); |
| 2793 | |
| 2794 | /* Once we have found one overlap for this section, |
| 2795 | stop looking for others. */ |
| 2796 | break; |
| 2797 | } |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | /* Make sure the new address is within the region. We explicitly permit the |
| 2802 | current address to be at the exact end of the region when the address is |
| 2803 | non-zero, in case the region is at the end of addressable memory and the |
| 2804 | calculation wraps around. */ |
| 2805 | |
| 2806 | static void |
| 2807 | os_region_check (lang_output_section_statement_type *os, |
| 2808 | struct memory_region_struct *region, |
| 2809 | etree_type *tree, |
| 2810 | bfd_vma base) |
| 2811 | { |
| 2812 | if ((region->current < region->origin |
| 2813 | || (region->current - region->origin > region->length)) |
| 2814 | && ((region->current != region->origin + region->length) |
| 2815 | || base == 0)) |
| 2816 | { |
| 2817 | if (tree != NULL) |
| 2818 | { |
| 2819 | einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"), |
| 2820 | region->current, |
| 2821 | os->bfd_section->owner, |
| 2822 | os->bfd_section->name, |
| 2823 | region->name); |
| 2824 | } |
| 2825 | else |
| 2826 | { |
| 2827 | einfo (_("%X%P: region %s is full (%B section %s)\n"), |
| 2828 | region->name, |
| 2829 | os->bfd_section->owner, |
| 2830 | os->bfd_section->name); |
| 2831 | } |
| 2832 | /* Reset the region pointer. */ |
| 2833 | region->current = region->origin; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | /* Set the sizes for all the output sections. */ |
| 2838 | |
| 2839 | static bfd_vma |
| 2840 | lang_size_sections_1 |
| 2841 | (lang_statement_union_type *s, |
| 2842 | lang_output_section_statement_type *output_section_statement, |
| 2843 | lang_statement_union_type **prev, |
| 2844 | fill_type *fill, |
| 2845 | bfd_vma dot, |
| 2846 | bfd_boolean *relax, |
| 2847 | bfd_boolean check_regions) |
| 2848 | { |
| 2849 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 2850 | ldfile_output_machine); |
| 2851 | |
| 2852 | /* Size up the sections from their constituent parts. */ |
| 2853 | for (; s != NULL; s = s->header.next) |
| 2854 | { |
| 2855 | switch (s->header.type) |
| 2856 | { |
| 2857 | case lang_output_section_statement_enum: |
| 2858 | { |
| 2859 | bfd_vma after; |
| 2860 | lang_output_section_statement_type *os; |
| 2861 | |
| 2862 | os = &s->output_section_statement; |
| 2863 | if (os->bfd_section == NULL) |
| 2864 | /* This section was never actually created. */ |
| 2865 | break; |
| 2866 | |
| 2867 | /* If this is a COFF shared library section, use the size and |
| 2868 | address from the input section. FIXME: This is COFF |
| 2869 | specific; it would be cleaner if there were some other way |
| 2870 | to do this, but nothing simple comes to mind. */ |
| 2871 | if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
| 2872 | { |
| 2873 | asection *input; |
| 2874 | |
| 2875 | if (os->children.head == NULL |
| 2876 | || os->children.head->header.next != NULL |
| 2877 | || os->children.head->header.type != lang_input_section_enum) |
| 2878 | einfo (_("%P%X: Internal error on COFF shared library section %s\n"), |
| 2879 | os->name); |
| 2880 | |
| 2881 | input = os->children.head->input_section.section; |
| 2882 | bfd_set_section_vma (os->bfd_section->owner, |
| 2883 | os->bfd_section, |
| 2884 | bfd_section_vma (input->owner, input)); |
| 2885 | os->bfd_section->_raw_size = input->_raw_size; |
| 2886 | break; |
| 2887 | } |
| 2888 | |
| 2889 | if (bfd_is_abs_section (os->bfd_section)) |
| 2890 | { |
| 2891 | /* No matter what happens, an abs section starts at zero. */ |
| 2892 | ASSERT (os->bfd_section->vma == 0); |
| 2893 | } |
| 2894 | else |
| 2895 | { |
| 2896 | if (os->addr_tree == NULL) |
| 2897 | { |
| 2898 | /* No address specified for this section, get one |
| 2899 | from the region specification. */ |
| 2900 | if (os->region == NULL |
| 2901 | || (((bfd_get_section_flags (output_bfd, os->bfd_section) |
| 2902 | & (SEC_ALLOC | SEC_LOAD)) != 0) |
| 2903 | && os->region->name[0] == '*' |
| 2904 | && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0)) |
| 2905 | { |
| 2906 | os->region = lang_memory_default (os->bfd_section); |
| 2907 | } |
| 2908 | |
| 2909 | /* If a loadable section is using the default memory |
| 2910 | region, and some non default memory regions were |
| 2911 | defined, issue an error message. */ |
| 2912 | if (!IGNORE_SECTION (output_bfd, os->bfd_section) |
| 2913 | && (bfd_get_section_flags (output_bfd, os->bfd_section) |
| 2914 | & SEC_NEVER_LOAD) == 0 |
| 2915 | && ! link_info.relocatable |
| 2916 | && check_regions |
| 2917 | && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0 |
| 2918 | && lang_memory_region_list != NULL |
| 2919 | && (strcmp (lang_memory_region_list->name, |
| 2920 | DEFAULT_MEMORY_REGION) != 0 |
| 2921 | || lang_memory_region_list->next != NULL)) |
| 2922 | { |
| 2923 | /* By default this is an error rather than just a |
| 2924 | warning because if we allocate the section to the |
| 2925 | default memory region we can end up creating an |
| 2926 | excessively large binary, or even seg faulting when |
| 2927 | attempting to perform a negative seek. See |
| 2928 | http://sources.redhat.com/ml/binutils/2003-04/msg00423.html |
| 2929 | for an example of this. This behaviour can be |
| 2930 | overridden by the using the --no-check-sections |
| 2931 | switch. */ |
| 2932 | if (command_line.check_section_addresses) |
| 2933 | einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"), |
| 2934 | bfd_get_section_name (output_bfd, |
| 2935 | os->bfd_section)); |
| 2936 | else |
| 2937 | einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"), |
| 2938 | bfd_get_section_name (output_bfd, |
| 2939 | os->bfd_section)); |
| 2940 | } |
| 2941 | |
| 2942 | dot = os->region->current; |
| 2943 | |
| 2944 | if (os->section_alignment == -1) |
| 2945 | { |
| 2946 | bfd_vma olddot; |
| 2947 | |
| 2948 | olddot = dot; |
| 2949 | dot = align_power (dot, |
| 2950 | os->bfd_section->alignment_power); |
| 2951 | |
| 2952 | if (dot != olddot && config.warn_section_align) |
| 2953 | einfo (_("%P: warning: changing start of section %s by %u bytes\n"), |
| 2954 | os->name, (unsigned int) (dot - olddot)); |
| 2955 | } |
| 2956 | } |
| 2957 | else |
| 2958 | { |
| 2959 | etree_value_type r; |
| 2960 | |
| 2961 | r = exp_fold_tree (os->addr_tree, |
| 2962 | abs_output_section, |
| 2963 | lang_allocating_phase_enum, |
| 2964 | dot, &dot); |
| 2965 | if (!r.valid_p) |
| 2966 | einfo (_("%F%S: non constant address expression for section %s\n"), |
| 2967 | os->name); |
| 2968 | |
| 2969 | dot = r.value + r.section->bfd_section->vma; |
| 2970 | } |
| 2971 | |
| 2972 | /* The section starts here. |
| 2973 | First, align to what the section needs. */ |
| 2974 | |
| 2975 | if (os->section_alignment != -1) |
| 2976 | dot = align_power (dot, os->section_alignment); |
| 2977 | |
| 2978 | bfd_set_section_vma (0, os->bfd_section, dot); |
| 2979 | |
| 2980 | os->bfd_section->output_offset = 0; |
| 2981 | } |
| 2982 | |
| 2983 | lang_size_sections_1 (os->children.head, os, &os->children.head, |
| 2984 | os->fill, dot, relax, check_regions); |
| 2985 | |
| 2986 | /* Put the section within the requested block size, or |
| 2987 | align at the block boundary. */ |
| 2988 | after = align_n (os->bfd_section->vma |
| 2989 | + os->bfd_section->_raw_size / opb, |
| 2990 | (bfd_vma) os->block_value); |
| 2991 | |
| 2992 | if (bfd_is_abs_section (os->bfd_section)) |
| 2993 | ASSERT (after == os->bfd_section->vma); |
| 2994 | else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0 |
| 2995 | && (os->bfd_section->flags & SEC_THREAD_LOCAL) |
| 2996 | && ! link_info.relocatable) |
| 2997 | os->bfd_section->_raw_size = 0; |
| 2998 | else |
| 2999 | os->bfd_section->_raw_size = |
| 3000 | (after - os->bfd_section->vma) * opb; |
| 3001 | |
| 3002 | dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb; |
| 3003 | os->processed = TRUE; |
| 3004 | |
| 3005 | if (os->update_dot_tree != 0) |
| 3006 | exp_fold_tree (os->update_dot_tree, abs_output_section, |
| 3007 | lang_allocating_phase_enum, dot, &dot); |
| 3008 | |
| 3009 | /* Update dot in the region ? |
| 3010 | We only do this if the section is going to be allocated, |
| 3011 | since unallocated sections do not contribute to the region's |
| 3012 | overall size in memory. |
| 3013 | |
| 3014 | If the SEC_NEVER_LOAD bit is not set, it will affect the |
| 3015 | addresses of sections after it. We have to update |
| 3016 | dot. */ |
| 3017 | if (os->region != NULL |
| 3018 | && ((bfd_get_section_flags (output_bfd, os->bfd_section) |
| 3019 | & SEC_NEVER_LOAD) == 0 |
| 3020 | || (bfd_get_section_flags (output_bfd, os->bfd_section) |
| 3021 | & (SEC_ALLOC | SEC_LOAD)))) |
| 3022 | { |
| 3023 | os->region->current = dot; |
| 3024 | |
| 3025 | if (check_regions) |
| 3026 | /* Make sure the new address is within the region. */ |
| 3027 | os_region_check (os, os->region, os->addr_tree, |
| 3028 | os->bfd_section->vma); |
| 3029 | |
| 3030 | /* If there's no load address specified, use the run |
| 3031 | region as the load region. */ |
| 3032 | if (os->lma_region == NULL && os->load_base == NULL) |
| 3033 | os->lma_region = os->region; |
| 3034 | |
| 3035 | if (os->lma_region != NULL && os->lma_region != os->region) |
| 3036 | { |
| 3037 | /* Set load_base, which will be handled later. */ |
| 3038 | os->load_base = exp_intop (os->lma_region->current); |
| 3039 | os->lma_region->current += |
| 3040 | os->bfd_section->_raw_size / opb; |
| 3041 | if (check_regions) |
| 3042 | os_region_check (os, os->lma_region, NULL, |
| 3043 | os->bfd_section->lma); |
| 3044 | } |
| 3045 | } |
| 3046 | } |
| 3047 | break; |
| 3048 | |
| 3049 | case lang_constructors_statement_enum: |
| 3050 | dot = lang_size_sections_1 (constructor_list.head, |
| 3051 | output_section_statement, |
| 3052 | &s->wild_statement.children.head, |
| 3053 | fill, dot, relax, check_regions); |
| 3054 | break; |
| 3055 | |
| 3056 | case lang_data_statement_enum: |
| 3057 | { |
| 3058 | unsigned int size = 0; |
| 3059 | |
| 3060 | s->data_statement.output_vma = |
| 3061 | dot - output_section_statement->bfd_section->vma; |
| 3062 | s->data_statement.output_section = |
| 3063 | output_section_statement->bfd_section; |
| 3064 | |
| 3065 | switch (s->data_statement.type) |
| 3066 | { |
| 3067 | default: |
| 3068 | abort (); |
| 3069 | case QUAD: |
| 3070 | case SQUAD: |
| 3071 | size = QUAD_SIZE; |
| 3072 | break; |
| 3073 | case LONG: |
| 3074 | size = LONG_SIZE; |
| 3075 | break; |
| 3076 | case SHORT: |
| 3077 | size = SHORT_SIZE; |
| 3078 | break; |
| 3079 | case BYTE: |
| 3080 | size = BYTE_SIZE; |
| 3081 | break; |
| 3082 | } |
| 3083 | if (size < opb) |
| 3084 | size = opb; |
| 3085 | dot += size / opb; |
| 3086 | output_section_statement->bfd_section->_raw_size += size; |
| 3087 | /* The output section gets contents, and then we inspect for |
| 3088 | any flags set in the input script which override any ALLOC. */ |
| 3089 | output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS; |
| 3090 | if (!(output_section_statement->flags & SEC_NEVER_LOAD)) |
| 3091 | { |
| 3092 | output_section_statement->bfd_section->flags |= |
| 3093 | SEC_ALLOC | SEC_LOAD; |
| 3094 | } |
| 3095 | } |
| 3096 | break; |
| 3097 | |
| 3098 | case lang_reloc_statement_enum: |
| 3099 | { |
| 3100 | int size; |
| 3101 | |
| 3102 | s->reloc_statement.output_vma = |
| 3103 | dot - output_section_statement->bfd_section->vma; |
| 3104 | s->reloc_statement.output_section = |
| 3105 | output_section_statement->bfd_section; |
| 3106 | size = bfd_get_reloc_size (s->reloc_statement.howto); |
| 3107 | dot += size / opb; |
| 3108 | output_section_statement->bfd_section->_raw_size += size; |
| 3109 | } |
| 3110 | break; |
| 3111 | |
| 3112 | case lang_wild_statement_enum: |
| 3113 | |
| 3114 | dot = lang_size_sections_1 (s->wild_statement.children.head, |
| 3115 | output_section_statement, |
| 3116 | &s->wild_statement.children.head, |
| 3117 | fill, dot, relax, check_regions); |
| 3118 | |
| 3119 | break; |
| 3120 | |
| 3121 | case lang_object_symbols_statement_enum: |
| 3122 | link_info.create_object_symbols_section = |
| 3123 | output_section_statement->bfd_section; |
| 3124 | break; |
| 3125 | case lang_output_statement_enum: |
| 3126 | case lang_target_statement_enum: |
| 3127 | break; |
| 3128 | case lang_input_section_enum: |
| 3129 | { |
| 3130 | asection *i; |
| 3131 | |
| 3132 | i = (*prev)->input_section.section; |
| 3133 | if (! relax) |
| 3134 | { |
| 3135 | if (i->_cooked_size == 0) |
| 3136 | i->_cooked_size = i->_raw_size; |
| 3137 | } |
| 3138 | else |
| 3139 | { |
| 3140 | bfd_boolean again; |
| 3141 | |
| 3142 | if (! bfd_relax_section (i->owner, i, &link_info, &again)) |
| 3143 | einfo (_("%P%F: can't relax section: %E\n")); |
| 3144 | if (again) |
| 3145 | *relax = TRUE; |
| 3146 | } |
| 3147 | dot = size_input_section (prev, output_section_statement, |
| 3148 | output_section_statement->fill, dot); |
| 3149 | } |
| 3150 | break; |
| 3151 | case lang_input_statement_enum: |
| 3152 | break; |
| 3153 | case lang_fill_statement_enum: |
| 3154 | s->fill_statement.output_section = |
| 3155 | output_section_statement->bfd_section; |
| 3156 | |
| 3157 | fill = s->fill_statement.fill; |
| 3158 | break; |
| 3159 | case lang_assignment_statement_enum: |
| 3160 | { |
| 3161 | bfd_vma newdot = dot; |
| 3162 | |
| 3163 | exp_fold_tree (s->assignment_statement.exp, |
| 3164 | output_section_statement, |
| 3165 | lang_allocating_phase_enum, |
| 3166 | dot, |
| 3167 | &newdot); |
| 3168 | |
| 3169 | if (newdot != dot) |
| 3170 | { |
| 3171 | if (output_section_statement == abs_output_section) |
| 3172 | { |
| 3173 | /* If we don't have an output section, then just adjust |
| 3174 | the default memory address. */ |
| 3175 | lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot; |
| 3176 | } |
| 3177 | else |
| 3178 | { |
| 3179 | /* Insert a pad after this statement. We can't |
| 3180 | put the pad before when relaxing, in case the |
| 3181 | assignment references dot. */ |
| 3182 | insert_pad (&s->header.next, fill, (newdot - dot) * opb, |
| 3183 | output_section_statement->bfd_section, dot); |
| 3184 | |
| 3185 | /* Don't neuter the pad below when relaxing. */ |
| 3186 | s = s->header.next; |
| 3187 | } |
| 3188 | |
| 3189 | dot = newdot; |
| 3190 | } |
| 3191 | } |
| 3192 | break; |
| 3193 | |
| 3194 | case lang_padding_statement_enum: |
| 3195 | /* If this is the first time lang_size_sections is called, |
| 3196 | we won't have any padding statements. If this is the |
| 3197 | second or later passes when relaxing, we should allow |
| 3198 | padding to shrink. If padding is needed on this pass, it |
| 3199 | will be added back in. */ |
| 3200 | s->padding_statement.size = 0; |
| 3201 | |
| 3202 | /* Make sure output_offset is valid. If relaxation shrinks |
| 3203 | the section and this pad isn't needed, it's possible to |
| 3204 | have output_offset larger than the final size of the |
| 3205 | section. bfd_set_section_contents will complain even for |
| 3206 | a pad size of zero. */ |
| 3207 | s->padding_statement.output_offset |
| 3208 | = dot - output_section_statement->bfd_section->vma; |
| 3209 | break; |
| 3210 | |
| 3211 | case lang_group_statement_enum: |
| 3212 | dot = lang_size_sections_1 (s->group_statement.children.head, |
| 3213 | output_section_statement, |
| 3214 | &s->group_statement.children.head, |
| 3215 | fill, dot, relax, check_regions); |
| 3216 | break; |
| 3217 | |
| 3218 | default: |
| 3219 | FAIL (); |
| 3220 | break; |
| 3221 | |
| 3222 | /* We can only get here when relaxing is turned on. */ |
| 3223 | case lang_address_statement_enum: |
| 3224 | break; |
| 3225 | } |
| 3226 | prev = &s->header.next; |
| 3227 | } |
| 3228 | return dot; |
| 3229 | } |
| 3230 | |
| 3231 | bfd_vma |
| 3232 | lang_size_sections |
| 3233 | (lang_statement_union_type *s, |
| 3234 | lang_output_section_statement_type *output_section_statement, |
| 3235 | lang_statement_union_type **prev, |
| 3236 | fill_type *fill, |
| 3237 | bfd_vma dot, |
| 3238 | bfd_boolean *relax, |
| 3239 | bfd_boolean check_regions) |
| 3240 | { |
| 3241 | bfd_vma result; |
| 3242 | asection *o; |
| 3243 | |
| 3244 | /* Callers of exp_fold_tree need to increment this. */ |
| 3245 | lang_statement_iteration++; |
| 3246 | |
| 3247 | exp_data_seg.phase = exp_dataseg_none; |
| 3248 | result = lang_size_sections_1 (s, output_section_statement, prev, fill, |
| 3249 | dot, relax, check_regions); |
| 3250 | if (exp_data_seg.phase == exp_dataseg_end_seen) |
| 3251 | { |
| 3252 | /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether |
| 3253 | a page could be saved in the data segment. */ |
| 3254 | bfd_vma first, last; |
| 3255 | |
| 3256 | first = -exp_data_seg.base & (exp_data_seg.pagesize - 1); |
| 3257 | last = exp_data_seg.end & (exp_data_seg.pagesize - 1); |
| 3258 | if (first && last |
| 3259 | && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1)) |
| 3260 | != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1))) |
| 3261 | && first + last <= exp_data_seg.pagesize) |
| 3262 | { |
| 3263 | exp_data_seg.phase = exp_dataseg_adjust; |
| 3264 | result = lang_size_sections_1 (s, output_section_statement, prev, |
| 3265 | fill, dot, relax, check_regions); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | /* Some backend relaxers want to refer to the output section size. Give |
| 3270 | them a section size that does not change on the next call while they |
| 3271 | relax. We can't set this at top because lang_reset_memory_regions |
| 3272 | which is called before we get here, sets _raw_size to 0 on relaxing |
| 3273 | rounds. */ |
| 3274 | for (o = output_bfd->sections; o != NULL; o = o->next) |
| 3275 | o->_cooked_size = o->_raw_size; |
| 3276 | |
| 3277 | return result; |
| 3278 | } |
| 3279 | |
| 3280 | /* Worker function for lang_do_assignments. Recursiveness goes here. */ |
| 3281 | |
| 3282 | static bfd_vma |
| 3283 | lang_do_assignments_1 |
| 3284 | (lang_statement_union_type *s, |
| 3285 | lang_output_section_statement_type *output_section_statement, |
| 3286 | fill_type *fill, |
| 3287 | bfd_vma dot) |
| 3288 | { |
| 3289 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 3290 | ldfile_output_machine); |
| 3291 | |
| 3292 | for (; s != NULL; s = s->header.next) |
| 3293 | { |
| 3294 | switch (s->header.type) |
| 3295 | { |
| 3296 | case lang_constructors_statement_enum: |
| 3297 | dot = lang_do_assignments_1 (constructor_list.head, |
| 3298 | output_section_statement, |
| 3299 | fill, |
| 3300 | dot); |
| 3301 | break; |
| 3302 | |
| 3303 | case lang_output_section_statement_enum: |
| 3304 | { |
| 3305 | lang_output_section_statement_type *os; |
| 3306 | |
| 3307 | os = &(s->output_section_statement); |
| 3308 | if (os->bfd_section != NULL) |
| 3309 | { |
| 3310 | dot = os->bfd_section->vma; |
| 3311 | (void) lang_do_assignments_1 (os->children.head, os, |
| 3312 | os->fill, dot); |
| 3313 | dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb; |
| 3314 | |
| 3315 | } |
| 3316 | if (os->load_base) |
| 3317 | { |
| 3318 | /* If nothing has been placed into the output section then |
| 3319 | it won't have a bfd_section. */ |
| 3320 | if (os->bfd_section) |
| 3321 | { |
| 3322 | os->bfd_section->lma |
| 3323 | = exp_get_abs_int (os->load_base, 0, "load base", |
| 3324 | lang_final_phase_enum); |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | break; |
| 3329 | case lang_wild_statement_enum: |
| 3330 | |
| 3331 | dot = lang_do_assignments_1 (s->wild_statement.children.head, |
| 3332 | output_section_statement, |
| 3333 | fill, dot); |
| 3334 | |
| 3335 | break; |
| 3336 | |
| 3337 | case lang_object_symbols_statement_enum: |
| 3338 | case lang_output_statement_enum: |
| 3339 | case lang_target_statement_enum: |
| 3340 | #if 0 |
| 3341 | case lang_common_statement_enum: |
| 3342 | #endif |
| 3343 | break; |
| 3344 | case lang_data_statement_enum: |
| 3345 | { |
| 3346 | etree_value_type value; |
| 3347 | |
| 3348 | value = exp_fold_tree (s->data_statement.exp, |
| 3349 | abs_output_section, |
| 3350 | lang_final_phase_enum, dot, &dot); |
| 3351 | s->data_statement.value = value.value; |
| 3352 | if (!value.valid_p) |
| 3353 | einfo (_("%F%P: invalid data statement\n")); |
| 3354 | } |
| 3355 | { |
| 3356 | unsigned int size; |
| 3357 | switch (s->data_statement.type) |
| 3358 | { |
| 3359 | default: |
| 3360 | abort (); |
| 3361 | case QUAD: |
| 3362 | case SQUAD: |
| 3363 | size = QUAD_SIZE; |
| 3364 | break; |
| 3365 | case LONG: |
| 3366 | size = LONG_SIZE; |
| 3367 | break; |
| 3368 | case SHORT: |
| 3369 | size = SHORT_SIZE; |
| 3370 | break; |
| 3371 | case BYTE: |
| 3372 | size = BYTE_SIZE; |
| 3373 | break; |
| 3374 | } |
| 3375 | if (size < opb) |
| 3376 | size = opb; |
| 3377 | dot += size / opb; |
| 3378 | } |
| 3379 | break; |
| 3380 | |
| 3381 | case lang_reloc_statement_enum: |
| 3382 | { |
| 3383 | etree_value_type value; |
| 3384 | |
| 3385 | value = exp_fold_tree (s->reloc_statement.addend_exp, |
| 3386 | abs_output_section, |
| 3387 | lang_final_phase_enum, dot, &dot); |
| 3388 | s->reloc_statement.addend_value = value.value; |
| 3389 | if (!value.valid_p) |
| 3390 | einfo (_("%F%P: invalid reloc statement\n")); |
| 3391 | } |
| 3392 | dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb; |
| 3393 | break; |
| 3394 | |
| 3395 | case lang_input_section_enum: |
| 3396 | { |
| 3397 | asection *in = s->input_section.section; |
| 3398 | |
| 3399 | if (in->_cooked_size != 0) |
| 3400 | dot += in->_cooked_size / opb; |
| 3401 | else |
| 3402 | dot += in->_raw_size / opb; |
| 3403 | } |
| 3404 | break; |
| 3405 | |
| 3406 | case lang_input_statement_enum: |
| 3407 | break; |
| 3408 | case lang_fill_statement_enum: |
| 3409 | fill = s->fill_statement.fill; |
| 3410 | break; |
| 3411 | case lang_assignment_statement_enum: |
| 3412 | { |
| 3413 | exp_fold_tree (s->assignment_statement.exp, |
| 3414 | output_section_statement, |
| 3415 | lang_final_phase_enum, |
| 3416 | dot, |
| 3417 | &dot); |
| 3418 | } |
| 3419 | |
| 3420 | break; |
| 3421 | case lang_padding_statement_enum: |
| 3422 | dot += s->padding_statement.size / opb; |
| 3423 | break; |
| 3424 | |
| 3425 | case lang_group_statement_enum: |
| 3426 | dot = lang_do_assignments_1 (s->group_statement.children.head, |
| 3427 | output_section_statement, |
| 3428 | fill, dot); |
| 3429 | |
| 3430 | break; |
| 3431 | |
| 3432 | default: |
| 3433 | FAIL (); |
| 3434 | break; |
| 3435 | case lang_address_statement_enum: |
| 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | } |
| 3440 | return dot; |
| 3441 | } |
| 3442 | |
| 3443 | bfd_vma |
| 3444 | lang_do_assignments (lang_statement_union_type *s, |
| 3445 | lang_output_section_statement_type |
| 3446 | *output_section_statement, |
| 3447 | fill_type *fill, |
| 3448 | bfd_vma dot) |
| 3449 | { |
| 3450 | /* Callers of exp_fold_tree need to increment this. */ |
| 3451 | lang_statement_iteration++; |
| 3452 | lang_do_assignments_1 (s, output_section_statement, fill, dot); |
| 3453 | } |
| 3454 | |
| 3455 | /* Fix any .startof. or .sizeof. symbols. When the assemblers see the |
| 3456 | operator .startof. (section_name), it produces an undefined symbol |
| 3457 | .startof.section_name. Similarly, when it sees |
| 3458 | .sizeof. (section_name), it produces an undefined symbol |
| 3459 | .sizeof.section_name. For all the output sections, we look for |
| 3460 | such symbols, and set them to the correct value. */ |
| 3461 | |
| 3462 | static void |
| 3463 | lang_set_startof (void) |
| 3464 | { |
| 3465 | asection *s; |
| 3466 | |
| 3467 | if (link_info.relocatable) |
| 3468 | return; |
| 3469 | |
| 3470 | for (s = output_bfd->sections; s != NULL; s = s->next) |
| 3471 | { |
| 3472 | const char *secname; |
| 3473 | char *buf; |
| 3474 | struct bfd_link_hash_entry *h; |
| 3475 | |
| 3476 | secname = bfd_get_section_name (output_bfd, s); |
| 3477 | buf = xmalloc (10 + strlen (secname)); |
| 3478 | |
| 3479 | sprintf (buf, ".startof.%s", secname); |
| 3480 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
| 3481 | if (h != NULL && h->type == bfd_link_hash_undefined) |
| 3482 | { |
| 3483 | h->type = bfd_link_hash_defined; |
| 3484 | h->u.def.value = bfd_get_section_vma (output_bfd, s); |
| 3485 | h->u.def.section = bfd_abs_section_ptr; |
| 3486 | } |
| 3487 | |
| 3488 | sprintf (buf, ".sizeof.%s", secname); |
| 3489 | h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE); |
| 3490 | if (h != NULL && h->type == bfd_link_hash_undefined) |
| 3491 | { |
| 3492 | unsigned opb; |
| 3493 | |
| 3494 | opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 3495 | ldfile_output_machine); |
| 3496 | h->type = bfd_link_hash_defined; |
| 3497 | if (s->_cooked_size != 0) |
| 3498 | h->u.def.value = s->_cooked_size / opb; |
| 3499 | else |
| 3500 | h->u.def.value = s->_raw_size / opb; |
| 3501 | h->u.def.section = bfd_abs_section_ptr; |
| 3502 | } |
| 3503 | |
| 3504 | free (buf); |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | static void |
| 3509 | lang_finish (void) |
| 3510 | { |
| 3511 | struct bfd_link_hash_entry *h; |
| 3512 | bfd_boolean warn; |
| 3513 | |
| 3514 | if (link_info.relocatable || link_info.shared) |
| 3515 | warn = FALSE; |
| 3516 | else |
| 3517 | warn = TRUE; |
| 3518 | |
| 3519 | if (entry_symbol.name == NULL) |
| 3520 | { |
| 3521 | /* No entry has been specified. Look for start, but don't warn |
| 3522 | if we don't find it. */ |
| 3523 | entry_symbol.name = "start"; |
| 3524 | warn = FALSE; |
| 3525 | } |
| 3526 | |
| 3527 | h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name, |
| 3528 | FALSE, FALSE, TRUE); |
| 3529 | if (h != NULL |
| 3530 | && (h->type == bfd_link_hash_defined |
| 3531 | || h->type == bfd_link_hash_defweak) |
| 3532 | && h->u.def.section->output_section != NULL) |
| 3533 | { |
| 3534 | bfd_vma val; |
| 3535 | |
| 3536 | val = (h->u.def.value |
| 3537 | + bfd_get_section_vma (output_bfd, |
| 3538 | h->u.def.section->output_section) |
| 3539 | + h->u.def.section->output_offset); |
| 3540 | if (! bfd_set_start_address (output_bfd, val)) |
| 3541 | einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name); |
| 3542 | } |
| 3543 | else |
| 3544 | { |
| 3545 | bfd_vma val; |
| 3546 | const char *send; |
| 3547 | |
| 3548 | /* We couldn't find the entry symbol. Try parsing it as a |
| 3549 | number. */ |
| 3550 | val = bfd_scan_vma (entry_symbol.name, &send, 0); |
| 3551 | if (*send == '\0') |
| 3552 | { |
| 3553 | if (! bfd_set_start_address (output_bfd, val)) |
| 3554 | einfo (_("%P%F: can't set start address\n")); |
| 3555 | } |
| 3556 | else |
| 3557 | { |
| 3558 | asection *ts; |
| 3559 | |
| 3560 | /* Can't find the entry symbol, and it's not a number. Use |
| 3561 | the first address in the text section. */ |
| 3562 | ts = bfd_get_section_by_name (output_bfd, entry_section); |
| 3563 | if (ts != NULL) |
| 3564 | { |
| 3565 | if (warn) |
| 3566 | einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"), |
| 3567 | entry_symbol.name, |
| 3568 | bfd_get_section_vma (output_bfd, ts)); |
| 3569 | if (! bfd_set_start_address (output_bfd, |
| 3570 | bfd_get_section_vma (output_bfd, |
| 3571 | ts))) |
| 3572 | einfo (_("%P%F: can't set start address\n")); |
| 3573 | } |
| 3574 | else |
| 3575 | { |
| 3576 | if (warn) |
| 3577 | einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"), |
| 3578 | entry_symbol.name); |
| 3579 | } |
| 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | bfd_hash_table_free (&lang_definedness_table); |
| 3584 | } |
| 3585 | |
| 3586 | /* This is a small function used when we want to ignore errors from |
| 3587 | BFD. */ |
| 3588 | |
| 3589 | static void |
| 3590 | ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...) |
| 3591 | { |
| 3592 | /* Don't do anything. */ |
| 3593 | } |
| 3594 | |
| 3595 | /* Check that the architecture of all the input files is compatible |
| 3596 | with the output file. Also call the backend to let it do any |
| 3597 | other checking that is needed. */ |
| 3598 | |
| 3599 | static void |
| 3600 | lang_check (void) |
| 3601 | { |
| 3602 | lang_statement_union_type *file; |
| 3603 | bfd *input_bfd; |
| 3604 | const bfd_arch_info_type *compatible; |
| 3605 | |
| 3606 | for (file = file_chain.head; file != NULL; file = file->input_statement.next) |
| 3607 | { |
| 3608 | input_bfd = file->input_statement.the_bfd; |
| 3609 | compatible = bfd_arch_get_compatible (input_bfd, output_bfd, |
| 3610 | command_line.accept_unknown_input_arch); |
| 3611 | |
| 3612 | /* In general it is not possible to perform a relocatable |
| 3613 | link between differing object formats when the input |
| 3614 | file has relocations, because the relocations in the |
| 3615 | input format may not have equivalent representations in |
| 3616 | the output format (and besides BFD does not translate |
| 3617 | relocs for other link purposes than a final link). */ |
| 3618 | if ((link_info.relocatable || link_info.emitrelocations) |
| 3619 | && (compatible == NULL |
| 3620 | || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd)) |
| 3621 | && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0) |
| 3622 | { |
| 3623 | einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"), |
| 3624 | bfd_get_target (input_bfd), input_bfd, |
| 3625 | bfd_get_target (output_bfd), output_bfd); |
| 3626 | /* einfo with %F exits. */ |
| 3627 | } |
| 3628 | |
| 3629 | if (compatible == NULL) |
| 3630 | { |
| 3631 | if (command_line.warn_mismatch) |
| 3632 | einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"), |
| 3633 | bfd_printable_name (input_bfd), input_bfd, |
| 3634 | bfd_printable_name (output_bfd)); |
| 3635 | } |
| 3636 | else if (bfd_count_sections (input_bfd)) |
| 3637 | { |
| 3638 | /* If the input bfd has no contents, it shouldn't set the |
| 3639 | private data of the output bfd. */ |
| 3640 | |
| 3641 | bfd_error_handler_type pfn = NULL; |
| 3642 | |
| 3643 | /* If we aren't supposed to warn about mismatched input |
| 3644 | files, temporarily set the BFD error handler to a |
| 3645 | function which will do nothing. We still want to call |
| 3646 | bfd_merge_private_bfd_data, since it may set up |
| 3647 | information which is needed in the output file. */ |
| 3648 | if (! command_line.warn_mismatch) |
| 3649 | pfn = bfd_set_error_handler (ignore_bfd_errors); |
| 3650 | if (! bfd_merge_private_bfd_data (input_bfd, output_bfd)) |
| 3651 | { |
| 3652 | if (command_line.warn_mismatch) |
| 3653 | einfo (_("%E%X: failed to merge target specific data of file %B\n"), |
| 3654 | input_bfd); |
| 3655 | } |
| 3656 | if (! command_line.warn_mismatch) |
| 3657 | bfd_set_error_handler (pfn); |
| 3658 | } |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | /* Look through all the global common symbols and attach them to the |
| 3663 | correct section. The -sort-common command line switch may be used |
| 3664 | to roughly sort the entries by size. */ |
| 3665 | |
| 3666 | static void |
| 3667 | lang_common (void) |
| 3668 | { |
| 3669 | if (command_line.inhibit_common_definition) |
| 3670 | return; |
| 3671 | if (link_info.relocatable |
| 3672 | && ! command_line.force_common_definition) |
| 3673 | return; |
| 3674 | |
| 3675 | if (! config.sort_common) |
| 3676 | bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL); |
| 3677 | else |
| 3678 | { |
| 3679 | int power; |
| 3680 | |
| 3681 | for (power = 4; power >= 0; power--) |
| 3682 | bfd_link_hash_traverse (link_info.hash, lang_one_common, &power); |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | /* Place one common symbol in the correct section. */ |
| 3687 | |
| 3688 | static bfd_boolean |
| 3689 | lang_one_common (struct bfd_link_hash_entry *h, void *info) |
| 3690 | { |
| 3691 | unsigned int power_of_two; |
| 3692 | bfd_vma size; |
| 3693 | asection *section; |
| 3694 | unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture, |
| 3695 | ldfile_output_machine); |
| 3696 | |
| 3697 | if (h->type != bfd_link_hash_common) |
| 3698 | return TRUE; |
| 3699 | |
| 3700 | size = h->u.c.size; |
| 3701 | power_of_two = h->u.c.p->alignment_power; |
| 3702 | |
| 3703 | if (config.sort_common |
| 3704 | && power_of_two < (unsigned int) *(int *) info) |
| 3705 | return TRUE; |
| 3706 | |
| 3707 | section = h->u.c.p->section; |
| 3708 | |
| 3709 | /* Increase the size of the section. */ |
| 3710 | section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb, |
| 3711 | (bfd_vma) 1 << power_of_two) * opb; |
| 3712 | |
| 3713 | /* Adjust the alignment if necessary. */ |
| 3714 | if (power_of_two > section->alignment_power) |
| 3715 | section->alignment_power = power_of_two; |
| 3716 | |
| 3717 | /* Change the symbol from common to defined. */ |
| 3718 | h->type = bfd_link_hash_defined; |
| 3719 | h->u.def.section = section; |
| 3720 | h->u.def.value = section->_cooked_size; |
| 3721 | |
| 3722 | /* Increase the size of the section. */ |
| 3723 | section->_cooked_size += size; |
| 3724 | |
| 3725 | /* Make sure the section is allocated in memory, and make sure that |
| 3726 | it is no longer a common section. */ |
| 3727 | section->flags |= SEC_ALLOC; |
| 3728 | section->flags &= ~SEC_IS_COMMON; |
| 3729 | |
| 3730 | if (config.map_file != NULL) |
| 3731 | { |
| 3732 | static bfd_boolean header_printed; |
| 3733 | int len; |
| 3734 | char *name; |
| 3735 | char buf[50]; |
| 3736 | |
| 3737 | if (! header_printed) |
| 3738 | { |
| 3739 | minfo (_("\nAllocating common symbols\n")); |
| 3740 | minfo (_("Common symbol size file\n\n")); |
| 3741 | header_printed = TRUE; |
| 3742 | } |
| 3743 | |
| 3744 | name = demangle (h->root.string); |
| 3745 | minfo ("%s", name); |
| 3746 | len = strlen (name); |
| 3747 | free (name); |
| 3748 | |
| 3749 | if (len >= 19) |
| 3750 | { |
| 3751 | print_nl (); |
| 3752 | len = 0; |
| 3753 | } |
| 3754 | while (len < 20) |
| 3755 | { |
| 3756 | print_space (); |
| 3757 | ++len; |
| 3758 | } |
| 3759 | |
| 3760 | minfo ("0x"); |
| 3761 | if (size <= 0xffffffff) |
| 3762 | sprintf (buf, "%lx", (unsigned long) size); |
| 3763 | else |
| 3764 | sprintf_vma (buf, size); |
| 3765 | minfo ("%s", buf); |
| 3766 | len = strlen (buf); |
| 3767 | |
| 3768 | while (len < 16) |
| 3769 | { |
| 3770 | print_space (); |
| 3771 | ++len; |
| 3772 | } |
| 3773 | |
| 3774 | minfo ("%B\n", section->owner); |
| 3775 | } |
| 3776 | |
| 3777 | return TRUE; |
| 3778 | } |
| 3779 | |
| 3780 | /* Run through the input files and ensure that every input section has |
| 3781 | somewhere to go. If one is found without a destination then create |
| 3782 | an input request and place it into the statement tree. */ |
| 3783 | |
| 3784 | static void |
| 3785 | lang_place_orphans (void) |
| 3786 | { |
| 3787 | LANG_FOR_EACH_INPUT_STATEMENT (file) |
| 3788 | { |
| 3789 | asection *s; |
| 3790 | |
| 3791 | for (s = file->the_bfd->sections; s != NULL; s = s->next) |
| 3792 | { |
| 3793 | if (s->output_section == NULL) |
| 3794 | { |
| 3795 | /* This section of the file is not attached, root |
| 3796 | around for a sensible place for it to go. */ |
| 3797 | |
| 3798 | if (file->just_syms_flag) |
| 3799 | { |
| 3800 | abort (); |
| 3801 | } |
| 3802 | else if (strcmp (s->name, "COMMON") == 0) |
| 3803 | { |
| 3804 | /* This is a lonely common section which must have |
| 3805 | come from an archive. We attach to the section |
| 3806 | with the wildcard. */ |
| 3807 | if (! link_info.relocatable |
| 3808 | || command_line.force_common_definition) |
| 3809 | { |
| 3810 | if (default_common_section == NULL) |
| 3811 | { |
| 3812 | #if 0 |
| 3813 | /* This message happens when using the |
| 3814 | svr3.ifile linker script, so I have |
| 3815 | disabled it. */ |
| 3816 | info_msg (_("%P: no [COMMON] command, defaulting to .bss\n")); |
| 3817 | #endif |
| 3818 | default_common_section = |
| 3819 | lang_output_section_statement_lookup (".bss"); |
| 3820 | |
| 3821 | } |
| 3822 | lang_add_section (&default_common_section->children, s, |
| 3823 | default_common_section, file); |
| 3824 | } |
| 3825 | } |
| 3826 | else if (ldemul_place_orphan (file, s)) |
| 3827 | ; |
| 3828 | else |
| 3829 | { |
| 3830 | lang_output_section_statement_type *os; |
| 3831 | |
| 3832 | os = lang_output_section_statement_lookup (s->name); |
| 3833 | lang_add_section (&os->children, s, os, file); |
| 3834 | } |
| 3835 | } |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | void |
| 3841 | lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert) |
| 3842 | { |
| 3843 | flagword *ptr_flags; |
| 3844 | |
| 3845 | ptr_flags = invert ? &ptr->not_flags : &ptr->flags; |
| 3846 | while (*flags) |
| 3847 | { |
| 3848 | switch (*flags) |
| 3849 | { |
| 3850 | case 'A': case 'a': |
| 3851 | *ptr_flags |= SEC_ALLOC; |
| 3852 | break; |
| 3853 | |
| 3854 | case 'R': case 'r': |
| 3855 | *ptr_flags |= SEC_READONLY; |
| 3856 | break; |
| 3857 | |
| 3858 | case 'W': case 'w': |
| 3859 | *ptr_flags |= SEC_DATA; |
| 3860 | break; |
| 3861 | |
| 3862 | case 'X': case 'x': |
| 3863 | *ptr_flags |= SEC_CODE; |
| 3864 | break; |
| 3865 | |
| 3866 | case 'L': case 'l': |
| 3867 | case 'I': case 'i': |
| 3868 | *ptr_flags |= SEC_LOAD; |
| 3869 | break; |
| 3870 | |
| 3871 | default: |
| 3872 | einfo (_("%P%F: invalid syntax in flags\n")); |
| 3873 | break; |
| 3874 | } |
| 3875 | flags++; |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | /* Call a function on each input file. This function will be called |
| 3880 | on an archive, but not on the elements. */ |
| 3881 | |
| 3882 | void |
| 3883 | lang_for_each_input_file (void (*func) (lang_input_statement_type *)) |
| 3884 | { |
| 3885 | lang_input_statement_type *f; |
| 3886 | |
| 3887 | for (f = (lang_input_statement_type *) input_file_chain.head; |
| 3888 | f != NULL; |
| 3889 | f = (lang_input_statement_type *) f->next_real_file) |
| 3890 | func (f); |
| 3891 | } |
| 3892 | |
| 3893 | /* Call a function on each file. The function will be called on all |
| 3894 | the elements of an archive which are included in the link, but will |
| 3895 | not be called on the archive file itself. */ |
| 3896 | |
| 3897 | void |
| 3898 | lang_for_each_file (void (*func) (lang_input_statement_type *)) |
| 3899 | { |
| 3900 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 3901 | { |
| 3902 | func (f); |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | #if 0 |
| 3907 | |
| 3908 | /* Not used. */ |
| 3909 | |
| 3910 | void |
| 3911 | lang_for_each_input_section (void (*func) (bfd *ab, asection *as)) |
| 3912 | { |
| 3913 | LANG_FOR_EACH_INPUT_STATEMENT (f) |
| 3914 | { |
| 3915 | asection *s; |
| 3916 | |
| 3917 | for (s = f->the_bfd->sections; s != NULL; s = s->next) |
| 3918 | func (f->the_bfd, s); |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | #endif |
| 3923 | |
| 3924 | void |
| 3925 | ldlang_add_file (lang_input_statement_type *entry) |
| 3926 | { |
| 3927 | bfd **pp; |
| 3928 | |
| 3929 | lang_statement_append (&file_chain, |
| 3930 | (lang_statement_union_type *) entry, |
| 3931 | &entry->next); |
| 3932 | |
| 3933 | /* The BFD linker needs to have a list of all input BFDs involved in |
| 3934 | a link. */ |
| 3935 | ASSERT (entry->the_bfd->link_next == NULL); |
| 3936 | ASSERT (entry->the_bfd != output_bfd); |
| 3937 | for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next) |
| 3938 | ; |
| 3939 | *pp = entry->the_bfd; |
| 3940 | entry->the_bfd->usrdata = entry; |
| 3941 | bfd_set_gp_size (entry->the_bfd, g_switch_value); |
| 3942 | |
| 3943 | /* Look through the sections and check for any which should not be |
| 3944 | included in the link. We need to do this now, so that we can |
| 3945 | notice when the backend linker tries to report multiple |
| 3946 | definition errors for symbols which are in sections we aren't |
| 3947 | going to link. FIXME: It might be better to entirely ignore |
| 3948 | symbols which are defined in sections which are going to be |
| 3949 | discarded. This would require modifying the backend linker for |
| 3950 | each backend which might set the SEC_LINK_ONCE flag. If we do |
| 3951 | this, we should probably handle SEC_EXCLUDE in the same way. */ |
| 3952 | |
| 3953 | bfd_map_over_sections (entry->the_bfd, section_already_linked, entry); |
| 3954 | } |
| 3955 | |
| 3956 | void |
| 3957 | lang_add_output (const char *name, int from_script) |
| 3958 | { |
| 3959 | /* Make -o on command line override OUTPUT in script. */ |
| 3960 | if (!had_output_filename || !from_script) |
| 3961 | { |
| 3962 | output_filename = name; |
| 3963 | had_output_filename = TRUE; |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | static lang_output_section_statement_type *current_section; |
| 3968 | |
| 3969 | static int |
| 3970 | topower (int x) |
| 3971 | { |
| 3972 | unsigned int i = 1; |
| 3973 | int l; |
| 3974 | |
| 3975 | if (x < 0) |
| 3976 | return -1; |
| 3977 | |
| 3978 | for (l = 0; l < 32; l++) |
| 3979 | { |
| 3980 | if (i >= (unsigned int) x) |
| 3981 | return l; |
| 3982 | i <<= 1; |
| 3983 | } |
| 3984 | |
| 3985 | return 0; |
| 3986 | } |
| 3987 | |
| 3988 | lang_output_section_statement_type * |
| 3989 | lang_enter_output_section_statement (const char *output_section_statement_name, |
| 3990 | etree_type *address_exp, |
| 3991 | enum section_type sectype, |
| 3992 | bfd_vma block_value, |
| 3993 | etree_type *align, |
| 3994 | etree_type *subalign, |
| 3995 | etree_type *ebase) |
| 3996 | { |
| 3997 | lang_output_section_statement_type *os; |
| 3998 | |
| 3999 | current_section = |
| 4000 | os = |
| 4001 | lang_output_section_statement_lookup (output_section_statement_name); |
| 4002 | |
| 4003 | /* Add this statement to tree. */ |
| 4004 | #if 0 |
| 4005 | add_statement (lang_output_section_statement_enum, |
| 4006 | output_section_statement); |
| 4007 | #endif |
| 4008 | /* Make next things chain into subchain of this. */ |
| 4009 | |
| 4010 | if (os->addr_tree == NULL) |
| 4011 | { |
| 4012 | os->addr_tree = address_exp; |
| 4013 | } |
| 4014 | os->sectype = sectype; |
| 4015 | if (sectype != noload_section) |
| 4016 | os->flags = SEC_NO_FLAGS; |
| 4017 | else |
| 4018 | os->flags = SEC_NEVER_LOAD; |
| 4019 | os->block_value = block_value ? block_value : 1; |
| 4020 | stat_ptr = &os->children; |
| 4021 | |
| 4022 | os->subsection_alignment = |
| 4023 | topower (exp_get_value_int (subalign, -1, "subsection alignment", 0)); |
| 4024 | os->section_alignment = |
| 4025 | topower (exp_get_value_int (align, -1, "section alignment", 0)); |
| 4026 | |
| 4027 | os->load_base = ebase; |
| 4028 | return os; |
| 4029 | } |
| 4030 | |
| 4031 | void |
| 4032 | lang_final (void) |
| 4033 | { |
| 4034 | lang_output_statement_type *new = |
| 4035 | new_stat (lang_output_statement, stat_ptr); |
| 4036 | |
| 4037 | new->name = output_filename; |
| 4038 | } |
| 4039 | |
| 4040 | /* Reset the current counters in the regions. */ |
| 4041 | |
| 4042 | void |
| 4043 | lang_reset_memory_regions (void) |
| 4044 | { |
| 4045 | lang_memory_region_type *p = lang_memory_region_list; |
| 4046 | asection *o; |
| 4047 | |
| 4048 | for (p = lang_memory_region_list; p != NULL; p = p->next) |
| 4049 | { |
| 4050 | p->old_length = (bfd_size_type) (p->current - p->origin); |
| 4051 | p->current = p->origin; |
| 4052 | } |
| 4053 | |
| 4054 | for (o = output_bfd->sections; o != NULL; o = o->next) |
| 4055 | o->_raw_size = 0; |
| 4056 | } |
| 4057 | |
| 4058 | /* If the wild pattern was marked KEEP, the member sections |
| 4059 | should be as well. */ |
| 4060 | |
| 4061 | static void |
| 4062 | gc_section_callback (lang_wild_statement_type *ptr, |
| 4063 | struct wildcard_list *sec ATTRIBUTE_UNUSED, |
| 4064 | asection *section, |
| 4065 | lang_input_statement_type *file ATTRIBUTE_UNUSED, |
| 4066 | void *data ATTRIBUTE_UNUSED) |
| 4067 | { |
| 4068 | if (ptr->keep_sections) |
| 4069 | section->flags |= SEC_KEEP; |
| 4070 | } |
| 4071 | |
| 4072 | /* Handle a wild statement, marking it against GC. */ |
| 4073 | |
| 4074 | static void |
| 4075 | lang_gc_wild (lang_wild_statement_type *s) |
| 4076 | { |
| 4077 | walk_wild (s, gc_section_callback, NULL); |
| 4078 | } |
| 4079 | |
| 4080 | /* Iterate over sections marking them against GC. */ |
| 4081 | |
| 4082 | static void |
| 4083 | lang_gc_sections_1 (lang_statement_union_type *s) |
| 4084 | { |
| 4085 | for (; s != NULL; s = s->header.next) |
| 4086 | { |
| 4087 | switch (s->header.type) |
| 4088 | { |
| 4089 | case lang_wild_statement_enum: |
| 4090 | lang_gc_wild (&s->wild_statement); |
| 4091 | break; |
| 4092 | case lang_constructors_statement_enum: |
| 4093 | lang_gc_sections_1 (constructor_list.head); |
| 4094 | break; |
| 4095 | case lang_output_section_statement_enum: |
| 4096 | lang_gc_sections_1 (s->output_section_statement.children.head); |
| 4097 | break; |
| 4098 | case lang_group_statement_enum: |
| 4099 | lang_gc_sections_1 (s->group_statement.children.head); |
| 4100 | break; |
| 4101 | default: |
| 4102 | break; |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | static void |
| 4108 | lang_gc_sections (void) |
| 4109 | { |
| 4110 | struct bfd_link_hash_entry *h; |
| 4111 | ldlang_undef_chain_list_type *ulist; |
| 4112 | |
| 4113 | /* Keep all sections so marked in the link script. */ |
| 4114 | |
| 4115 | lang_gc_sections_1 (statement_list.head); |
| 4116 | |
| 4117 | /* Keep all sections containing symbols undefined on the command-line, |
| 4118 | and the section containing the entry symbol. */ |
| 4119 | |
| 4120 | for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next) |
| 4121 | { |
| 4122 | h = bfd_link_hash_lookup (link_info.hash, ulist->name, |
| 4123 | FALSE, FALSE, FALSE); |
| 4124 | |
| 4125 | if (h != NULL |
| 4126 | && (h->type == bfd_link_hash_defined |
| 4127 | || h->type == bfd_link_hash_defweak) |
| 4128 | && ! bfd_is_abs_section (h->u.def.section)) |
| 4129 | { |
| 4130 | h->u.def.section->flags |= SEC_KEEP; |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | bfd_gc_sections (output_bfd, &link_info); |
| 4135 | } |
| 4136 | |
| 4137 | void |
| 4138 | lang_process (void) |
| 4139 | { |
| 4140 | lang_reasonable_defaults (); |
| 4141 | current_target = default_target; |
| 4142 | |
| 4143 | /* Open the output file. */ |
| 4144 | lang_for_each_statement (ldlang_open_output); |
| 4145 | |
| 4146 | ldemul_create_output_section_statements (); |
| 4147 | |
| 4148 | /* Add to the hash table all undefineds on the command line. */ |
| 4149 | lang_place_undefineds (); |
| 4150 | |
| 4151 | already_linked_table_init (); |
| 4152 | |
| 4153 | /* Create a bfd for each input file. */ |
| 4154 | current_target = default_target; |
| 4155 | open_input_bfds (statement_list.head, FALSE); |
| 4156 | |
| 4157 | link_info.gc_sym_list = &entry_symbol; |
| 4158 | if (entry_symbol.name == NULL) |
| 4159 | link_info.gc_sym_list = ldlang_undef_chain_list_head; |
| 4160 | |
| 4161 | ldemul_after_open (); |
| 4162 | |
| 4163 | already_linked_table_free (); |
| 4164 | |
| 4165 | /* Make sure that we're not mixing architectures. We call this |
| 4166 | after all the input files have been opened, but before we do any |
| 4167 | other processing, so that any operations merge_private_bfd_data |
| 4168 | does on the output file will be known during the rest of the |
| 4169 | link. */ |
| 4170 | lang_check (); |
| 4171 | |
| 4172 | /* Handle .exports instead of a version script if we're told to do so. */ |
| 4173 | if (command_line.version_exports_section) |
| 4174 | lang_do_version_exports_section (); |
| 4175 | |
| 4176 | /* Build all sets based on the information gathered from the input |
| 4177 | files. */ |
| 4178 | ldctor_build_sets (); |
| 4179 | |
| 4180 | /* Remove unreferenced sections if asked to. */ |
| 4181 | if (command_line.gc_sections) |
| 4182 | lang_gc_sections (); |
| 4183 | |
| 4184 | /* If there were any SEC_MERGE sections, finish their merging, so that |
| 4185 | section sizes can be computed. This has to be done after GC of sections, |
| 4186 | so that GCed sections are not merged, but before assigning output |
| 4187 | sections, since removing whole input sections is hard then. */ |
| 4188 | bfd_merge_sections (output_bfd, &link_info); |
| 4189 | |
| 4190 | /* Size up the common data. */ |
| 4191 | lang_common (); |
| 4192 | |
| 4193 | /* Run through the contours of the script and attach input sections |
| 4194 | to the correct output sections. */ |
| 4195 | map_input_to_output_sections (statement_list.head, NULL, NULL); |
| 4196 | |
| 4197 | /* Find any sections not attached explicitly and handle them. */ |
| 4198 | lang_place_orphans (); |
| 4199 | |
| 4200 | if (! link_info.relocatable) |
| 4201 | { |
| 4202 | /* Look for a text section and set the readonly attribute in it. */ |
| 4203 | asection *found = bfd_get_section_by_name (output_bfd, ".text"); |
| 4204 | |
| 4205 | if (found != NULL) |
| 4206 | { |
| 4207 | if (config.text_read_only) |
| 4208 | found->flags |= SEC_READONLY; |
| 4209 | else |
| 4210 | found->flags &= ~SEC_READONLY; |
| 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | /* Do anything special before sizing sections. This is where ELF |
| 4215 | and other back-ends size dynamic sections. */ |
| 4216 | ldemul_before_allocation (); |
| 4217 | |
| 4218 | if (!link_info.relocatable) |
| 4219 | strip_excluded_output_sections (); |
| 4220 | |
| 4221 | /* We must record the program headers before we try to fix the |
| 4222 | section positions, since they will affect SIZEOF_HEADERS. */ |
| 4223 | lang_record_phdrs (); |
| 4224 | |
| 4225 | /* Size up the sections. */ |
| 4226 | lang_size_sections (statement_list.head, abs_output_section, |
| 4227 | &statement_list.head, 0, 0, NULL, |
| 4228 | command_line.relax ? FALSE : TRUE); |
| 4229 | |
| 4230 | /* Now run around and relax if we can. */ |
| 4231 | if (command_line.relax) |
| 4232 | { |
| 4233 | /* Keep relaxing until bfd_relax_section gives up. */ |
| 4234 | bfd_boolean relax_again; |
| 4235 | |
| 4236 | do |
| 4237 | { |
| 4238 | lang_reset_memory_regions (); |
| 4239 | |
| 4240 | relax_again = FALSE; |
| 4241 | |
| 4242 | /* Note: pe-dll.c does something like this also. If you find |
| 4243 | you need to change this code, you probably need to change |
| 4244 | pe-dll.c also. DJ */ |
| 4245 | |
| 4246 | /* Do all the assignments with our current guesses as to |
| 4247 | section sizes. */ |
| 4248 | lang_do_assignments (statement_list.head, abs_output_section, |
| 4249 | NULL, 0); |
| 4250 | |
| 4251 | /* Perform another relax pass - this time we know where the |
| 4252 | globals are, so can make a better guess. */ |
| 4253 | lang_size_sections (statement_list.head, abs_output_section, |
| 4254 | &statement_list.head, 0, 0, &relax_again, FALSE); |
| 4255 | |
| 4256 | /* If the normal relax is done and the relax finalize pass |
| 4257 | is not performed yet, we perform another relax pass. */ |
| 4258 | if (!relax_again && !link_info.relax_finalizing) |
| 4259 | { |
| 4260 | link_info.relax_finalizing = TRUE; |
| 4261 | relax_again = TRUE; |
| 4262 | } |
| 4263 | } |
| 4264 | while (relax_again); |
| 4265 | |
| 4266 | /* Final extra sizing to report errors. */ |
| 4267 | lang_reset_memory_regions (); |
| 4268 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
| 4269 | lang_size_sections (statement_list.head, abs_output_section, |
| 4270 | &statement_list.head, 0, 0, NULL, TRUE); |
| 4271 | } |
| 4272 | |
| 4273 | /* See if anything special should be done now we know how big |
| 4274 | everything is. */ |
| 4275 | ldemul_after_allocation (); |
| 4276 | |
| 4277 | /* Fix any .startof. or .sizeof. symbols. */ |
| 4278 | lang_set_startof (); |
| 4279 | |
| 4280 | /* Do all the assignments, now that we know the final resting places |
| 4281 | of all the symbols. */ |
| 4282 | |
| 4283 | lang_do_assignments (statement_list.head, abs_output_section, NULL, 0); |
| 4284 | |
| 4285 | /* Make sure that the section addresses make sense. */ |
| 4286 | if (! link_info.relocatable |
| 4287 | && command_line.check_section_addresses) |
| 4288 | lang_check_section_addresses (); |
| 4289 | |
| 4290 | /* Final stuffs. */ |
| 4291 | |
| 4292 | ldemul_finish (); |
| 4293 | lang_finish (); |
| 4294 | } |
| 4295 | |
| 4296 | /* EXPORTED TO YACC */ |
| 4297 | |
| 4298 | void |
| 4299 | lang_add_wild (struct wildcard_spec *filespec, |
| 4300 | struct wildcard_list *section_list, |
| 4301 | bfd_boolean keep_sections) |
| 4302 | { |
| 4303 | struct wildcard_list *curr, *next; |
| 4304 | lang_wild_statement_type *new; |
| 4305 | |
| 4306 | /* Reverse the list as the parser puts it back to front. */ |
| 4307 | for (curr = section_list, section_list = NULL; |
| 4308 | curr != NULL; |
| 4309 | section_list = curr, curr = next) |
| 4310 | { |
| 4311 | if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0) |
| 4312 | placed_commons = TRUE; |
| 4313 | |
| 4314 | next = curr->next; |
| 4315 | curr->next = section_list; |
| 4316 | } |
| 4317 | |
| 4318 | if (filespec != NULL && filespec->name != NULL) |
| 4319 | { |
| 4320 | if (strcmp (filespec->name, "*") == 0) |
| 4321 | filespec->name = NULL; |
| 4322 | else if (! wildcardp (filespec->name)) |
| 4323 | lang_has_input_file = TRUE; |
| 4324 | } |
| 4325 | |
| 4326 | new = new_stat (lang_wild_statement, stat_ptr); |
| 4327 | new->filename = NULL; |
| 4328 | new->filenames_sorted = FALSE; |
| 4329 | if (filespec != NULL) |
| 4330 | { |
| 4331 | new->filename = filespec->name; |
| 4332 | new->filenames_sorted = filespec->sorted; |
| 4333 | } |
| 4334 | new->section_list = section_list; |
| 4335 | new->keep_sections = keep_sections; |
| 4336 | lang_list_init (&new->children); |
| 4337 | } |
| 4338 | |
| 4339 | void |
| 4340 | lang_section_start (const char *name, etree_type *address) |
| 4341 | { |
| 4342 | lang_address_statement_type *ad; |
| 4343 | |
| 4344 | ad = new_stat (lang_address_statement, stat_ptr); |
| 4345 | ad->section_name = name; |
| 4346 | ad->address = address; |
| 4347 | } |
| 4348 | |
| 4349 | /* Set the start symbol to NAME. CMDLINE is nonzero if this is called |
| 4350 | because of a -e argument on the command line, or zero if this is |
| 4351 | called by ENTRY in a linker script. Command line arguments take |
| 4352 | precedence. */ |
| 4353 | |
| 4354 | void |
| 4355 | lang_add_entry (const char *name, bfd_boolean cmdline) |
| 4356 | { |
| 4357 | if (entry_symbol.name == NULL |
| 4358 | || cmdline |
| 4359 | || ! entry_from_cmdline) |
| 4360 | { |
| 4361 | entry_symbol.name = name; |
| 4362 | entry_from_cmdline = cmdline; |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | void |
| 4367 | lang_add_target (const char *name) |
| 4368 | { |
| 4369 | lang_target_statement_type *new = new_stat (lang_target_statement, |
| 4370 | stat_ptr); |
| 4371 | |
| 4372 | new->target = name; |
| 4373 | |
| 4374 | } |
| 4375 | |
| 4376 | void |
| 4377 | lang_add_map (const char *name) |
| 4378 | { |
| 4379 | while (*name) |
| 4380 | { |
| 4381 | switch (*name) |
| 4382 | { |
| 4383 | case 'F': |
| 4384 | map_option_f = TRUE; |
| 4385 | break; |
| 4386 | } |
| 4387 | name++; |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | void |
| 4392 | lang_add_fill (fill_type *fill) |
| 4393 | { |
| 4394 | lang_fill_statement_type *new = new_stat (lang_fill_statement, |
| 4395 | stat_ptr); |
| 4396 | |
| 4397 | new->fill = fill; |
| 4398 | } |
| 4399 | |
| 4400 | void |
| 4401 | lang_add_data (int type, union etree_union *exp) |
| 4402 | { |
| 4403 | |
| 4404 | lang_data_statement_type *new = new_stat (lang_data_statement, |
| 4405 | stat_ptr); |
| 4406 | |
| 4407 | new->exp = exp; |
| 4408 | new->type = type; |
| 4409 | |
| 4410 | } |
| 4411 | |
| 4412 | /* Create a new reloc statement. RELOC is the BFD relocation type to |
| 4413 | generate. HOWTO is the corresponding howto structure (we could |
| 4414 | look this up, but the caller has already done so). SECTION is the |
| 4415 | section to generate a reloc against, or NAME is the name of the |
| 4416 | symbol to generate a reloc against. Exactly one of SECTION and |
| 4417 | NAME must be NULL. ADDEND is an expression for the addend. */ |
| 4418 | |
| 4419 | void |
| 4420 | lang_add_reloc (bfd_reloc_code_real_type reloc, |
| 4421 | reloc_howto_type *howto, |
| 4422 | asection *section, |
| 4423 | const char *name, |
| 4424 | union etree_union *addend) |
| 4425 | { |
| 4426 | lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr); |
| 4427 | |
| 4428 | p->reloc = reloc; |
| 4429 | p->howto = howto; |
| 4430 | p->section = section; |
| 4431 | p->name = name; |
| 4432 | p->addend_exp = addend; |
| 4433 | |
| 4434 | p->addend_value = 0; |
| 4435 | p->output_section = NULL; |
| 4436 | p->output_vma = 0; |
| 4437 | } |
| 4438 | |
| 4439 | lang_assignment_statement_type * |
| 4440 | lang_add_assignment (etree_type *exp) |
| 4441 | { |
| 4442 | lang_assignment_statement_type *new = new_stat (lang_assignment_statement, |
| 4443 | stat_ptr); |
| 4444 | |
| 4445 | new->exp = exp; |
| 4446 | return new; |
| 4447 | } |
| 4448 | |
| 4449 | void |
| 4450 | lang_add_attribute (enum statement_enum attribute) |
| 4451 | { |
| 4452 | new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr); |
| 4453 | } |
| 4454 | |
| 4455 | void |
| 4456 | lang_startup (const char *name) |
| 4457 | { |
| 4458 | if (startup_file != NULL) |
| 4459 | { |
| 4460 | einfo (_("%P%Fmultiple STARTUP files\n")); |
| 4461 | } |
| 4462 | first_file->filename = name; |
| 4463 | first_file->local_sym_name = name; |
| 4464 | first_file->real = TRUE; |
| 4465 | |
| 4466 | startup_file = name; |
| 4467 | } |
| 4468 | |
| 4469 | void |
| 4470 | lang_float (bfd_boolean maybe) |
| 4471 | { |
| 4472 | lang_float_flag = maybe; |
| 4473 | } |
| 4474 | |
| 4475 | |
| 4476 | /* Work out the load- and run-time regions from a script statement, and |
| 4477 | store them in *LMA_REGION and *REGION respectively. |
| 4478 | |
| 4479 | MEMSPEC is the name of the run-time region, or the value of |
| 4480 | DEFAULT_MEMORY_REGION if the statement didn't specify one. |
| 4481 | LMA_MEMSPEC is the name of the load-time region, or null if the |
| 4482 | statement didn't specify one.HAVE_LMA_P is TRUE if the statement |
| 4483 | had an explicit load address. |
| 4484 | |
| 4485 | It is an error to specify both a load region and a load address. */ |
| 4486 | |
| 4487 | static void |
| 4488 | lang_get_regions (struct memory_region_struct **region, |
| 4489 | struct memory_region_struct **lma_region, |
| 4490 | const char *memspec, |
| 4491 | const char *lma_memspec, |
| 4492 | int have_lma_p) |
| 4493 | { |
| 4494 | *lma_region = lang_memory_region_lookup (lma_memspec, FALSE); |
| 4495 | |
| 4496 | /* If no runtime region has been given, but the load region has |
| 4497 | been, use the load region. */ |
| 4498 | if (lma_memspec != 0 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0) |
| 4499 | *region = *lma_region; |
| 4500 | else |
| 4501 | *region = lang_memory_region_lookup (memspec, FALSE); |
| 4502 | |
| 4503 | if (have_lma_p && lma_memspec != 0) |
| 4504 | einfo (_("%X%P:%S: section has both a load address and a load region\n")); |
| 4505 | } |
| 4506 | |
| 4507 | void |
| 4508 | lang_leave_output_section_statement |
| 4509 | (fill_type *fill, const char *memspec, |
| 4510 | struct lang_output_section_phdr_list *phdrs, const char *lma_memspec) |
| 4511 | { |
| 4512 | lang_get_regions (¤t_section->region, |
| 4513 | ¤t_section->lma_region, |
| 4514 | memspec, lma_memspec, |
| 4515 | current_section->load_base != 0); |
| 4516 | current_section->fill = fill; |
| 4517 | current_section->phdrs = phdrs; |
| 4518 | stat_ptr = &statement_list; |
| 4519 | } |
| 4520 | |
| 4521 | /* Create an absolute symbol with the given name with the value of the |
| 4522 | address of first byte of the section named. |
| 4523 | |
| 4524 | If the symbol already exists, then do nothing. */ |
| 4525 | |
| 4526 | void |
| 4527 | lang_abs_symbol_at_beginning_of (const char *secname, const char *name) |
| 4528 | { |
| 4529 | struct bfd_link_hash_entry *h; |
| 4530 | |
| 4531 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
| 4532 | if (h == NULL) |
| 4533 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
| 4534 | |
| 4535 | if (h->type == bfd_link_hash_new |
| 4536 | || h->type == bfd_link_hash_undefined) |
| 4537 | { |
| 4538 | asection *sec; |
| 4539 | |
| 4540 | h->type = bfd_link_hash_defined; |
| 4541 | |
| 4542 | sec = bfd_get_section_by_name (output_bfd, secname); |
| 4543 | if (sec == NULL) |
| 4544 | h->u.def.value = 0; |
| 4545 | else |
| 4546 | h->u.def.value = bfd_get_section_vma (output_bfd, sec); |
| 4547 | |
| 4548 | h->u.def.section = bfd_abs_section_ptr; |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | /* Create an absolute symbol with the given name with the value of the |
| 4553 | address of the first byte after the end of the section named. |
| 4554 | |
| 4555 | If the symbol already exists, then do nothing. */ |
| 4556 | |
| 4557 | void |
| 4558 | lang_abs_symbol_at_end_of (const char *secname, const char *name) |
| 4559 | { |
| 4560 | struct bfd_link_hash_entry *h; |
| 4561 | |
| 4562 | h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE); |
| 4563 | if (h == NULL) |
| 4564 | einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n")); |
| 4565 | |
| 4566 | if (h->type == bfd_link_hash_new |
| 4567 | || h->type == bfd_link_hash_undefined) |
| 4568 | { |
| 4569 | asection *sec; |
| 4570 | |
| 4571 | h->type = bfd_link_hash_defined; |
| 4572 | |
| 4573 | sec = bfd_get_section_by_name (output_bfd, secname); |
| 4574 | if (sec == NULL) |
| 4575 | h->u.def.value = 0; |
| 4576 | else |
| 4577 | h->u.def.value = (bfd_get_section_vma (output_bfd, sec) |
| 4578 | + bfd_section_size (output_bfd, sec) / |
| 4579 | bfd_octets_per_byte (output_bfd)); |
| 4580 | |
| 4581 | h->u.def.section = bfd_abs_section_ptr; |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | void |
| 4586 | lang_statement_append (lang_statement_list_type *list, |
| 4587 | lang_statement_union_type *element, |
| 4588 | lang_statement_union_type **field) |
| 4589 | { |
| 4590 | *(list->tail) = element; |
| 4591 | list->tail = field; |
| 4592 | } |
| 4593 | |
| 4594 | /* Set the output format type. -oformat overrides scripts. */ |
| 4595 | |
| 4596 | void |
| 4597 | lang_add_output_format (const char *format, |
| 4598 | const char *big, |
| 4599 | const char *little, |
| 4600 | int from_script) |
| 4601 | { |
| 4602 | if (output_target == NULL || !from_script) |
| 4603 | { |
| 4604 | if (command_line.endian == ENDIAN_BIG |
| 4605 | && big != NULL) |
| 4606 | format = big; |
| 4607 | else if (command_line.endian == ENDIAN_LITTLE |
| 4608 | && little != NULL) |
| 4609 | format = little; |
| 4610 | |
| 4611 | output_target = format; |
| 4612 | } |
| 4613 | } |
| 4614 | |
| 4615 | /* Enter a group. This creates a new lang_group_statement, and sets |
| 4616 | stat_ptr to build new statements within the group. */ |
| 4617 | |
| 4618 | void |
| 4619 | lang_enter_group (void) |
| 4620 | { |
| 4621 | lang_group_statement_type *g; |
| 4622 | |
| 4623 | g = new_stat (lang_group_statement, stat_ptr); |
| 4624 | lang_list_init (&g->children); |
| 4625 | stat_ptr = &g->children; |
| 4626 | } |
| 4627 | |
| 4628 | /* Leave a group. This just resets stat_ptr to start writing to the |
| 4629 | regular list of statements again. Note that this will not work if |
| 4630 | groups can occur inside anything else which can adjust stat_ptr, |
| 4631 | but currently they can't. */ |
| 4632 | |
| 4633 | void |
| 4634 | lang_leave_group (void) |
| 4635 | { |
| 4636 | stat_ptr = &statement_list; |
| 4637 | } |
| 4638 | |
| 4639 | /* Add a new program header. This is called for each entry in a PHDRS |
| 4640 | command in a linker script. */ |
| 4641 | |
| 4642 | void |
| 4643 | lang_new_phdr (const char *name, |
| 4644 | etree_type *type, |
| 4645 | bfd_boolean filehdr, |
| 4646 | bfd_boolean phdrs, |
| 4647 | etree_type *at, |
| 4648 | etree_type *flags) |
| 4649 | { |
| 4650 | struct lang_phdr *n, **pp; |
| 4651 | |
| 4652 | n = stat_alloc (sizeof (struct lang_phdr)); |
| 4653 | n->next = NULL; |
| 4654 | n->name = name; |
| 4655 | n->type = exp_get_value_int (type, 0, "program header type", |
| 4656 | lang_final_phase_enum); |
| 4657 | n->filehdr = filehdr; |
| 4658 | n->phdrs = phdrs; |
| 4659 | n->at = at; |
| 4660 | n->flags = flags; |
| 4661 | |
| 4662 | for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next) |
| 4663 | ; |
| 4664 | *pp = n; |
| 4665 | } |
| 4666 | |
| 4667 | /* Record the program header information in the output BFD. FIXME: We |
| 4668 | should not be calling an ELF specific function here. */ |
| 4669 | |
| 4670 | static void |
| 4671 | lang_record_phdrs (void) |
| 4672 | { |
| 4673 | unsigned int alc; |
| 4674 | asection **secs; |
| 4675 | struct lang_output_section_phdr_list *last; |
| 4676 | struct lang_phdr *l; |
| 4677 | lang_statement_union_type *u; |
| 4678 | |
| 4679 | alc = 10; |
| 4680 | secs = xmalloc (alc * sizeof (asection *)); |
| 4681 | last = NULL; |
| 4682 | for (l = lang_phdr_list; l != NULL; l = l->next) |
| 4683 | { |
| 4684 | unsigned int c; |
| 4685 | flagword flags; |
| 4686 | bfd_vma at; |
| 4687 | |
| 4688 | c = 0; |
| 4689 | for (u = lang_output_section_statement.head; |
| 4690 | u != NULL; |
| 4691 | u = u->output_section_statement.next) |
| 4692 | { |
| 4693 | lang_output_section_statement_type *os; |
| 4694 | struct lang_output_section_phdr_list *pl; |
| 4695 | |
| 4696 | os = &u->output_section_statement; |
| 4697 | |
| 4698 | pl = os->phdrs; |
| 4699 | if (pl != NULL) |
| 4700 | last = pl; |
| 4701 | else |
| 4702 | { |
| 4703 | if (os->sectype == noload_section |
| 4704 | || os->bfd_section == NULL |
| 4705 | || (os->bfd_section->flags & SEC_ALLOC) == 0) |
| 4706 | continue; |
| 4707 | pl = last; |
| 4708 | } |
| 4709 | |
| 4710 | if (os->bfd_section == NULL) |
| 4711 | continue; |
| 4712 | |
| 4713 | for (; pl != NULL; pl = pl->next) |
| 4714 | { |
| 4715 | if (strcmp (pl->name, l->name) == 0) |
| 4716 | { |
| 4717 | if (c >= alc) |
| 4718 | { |
| 4719 | alc *= 2; |
| 4720 | secs = xrealloc (secs, alc * sizeof (asection *)); |
| 4721 | } |
| 4722 | secs[c] = os->bfd_section; |
| 4723 | ++c; |
| 4724 | pl->used = TRUE; |
| 4725 | } |
| 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | if (l->flags == NULL) |
| 4730 | flags = 0; |
| 4731 | else |
| 4732 | flags = exp_get_vma (l->flags, 0, "phdr flags", |
| 4733 | lang_final_phase_enum); |
| 4734 | |
| 4735 | if (l->at == NULL) |
| 4736 | at = 0; |
| 4737 | else |
| 4738 | at = exp_get_vma (l->at, 0, "phdr load address", |
| 4739 | lang_final_phase_enum); |
| 4740 | |
| 4741 | if (! bfd_record_phdr (output_bfd, l->type, |
| 4742 | l->flags != NULL, flags, l->at != NULL, |
| 4743 | at, l->filehdr, l->phdrs, c, secs)) |
| 4744 | einfo (_("%F%P: bfd_record_phdr failed: %E\n")); |
| 4745 | } |
| 4746 | |
| 4747 | free (secs); |
| 4748 | |
| 4749 | /* Make sure all the phdr assignments succeeded. */ |
| 4750 | for (u = lang_output_section_statement.head; |
| 4751 | u != NULL; |
| 4752 | u = u->output_section_statement.next) |
| 4753 | { |
| 4754 | struct lang_output_section_phdr_list *pl; |
| 4755 | |
| 4756 | if (u->output_section_statement.bfd_section == NULL) |
| 4757 | continue; |
| 4758 | |
| 4759 | for (pl = u->output_section_statement.phdrs; |
| 4760 | pl != NULL; |
| 4761 | pl = pl->next) |
| 4762 | if (! pl->used && strcmp (pl->name, "NONE") != 0) |
| 4763 | einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"), |
| 4764 | u->output_section_statement.name, pl->name); |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | /* Record a list of sections which may not be cross referenced. */ |
| 4769 | |
| 4770 | void |
| 4771 | lang_add_nocrossref (struct lang_nocrossref *l) |
| 4772 | { |
| 4773 | struct lang_nocrossrefs *n; |
| 4774 | |
| 4775 | n = xmalloc (sizeof *n); |
| 4776 | n->next = nocrossref_list; |
| 4777 | n->list = l; |
| 4778 | nocrossref_list = n; |
| 4779 | |
| 4780 | /* Set notice_all so that we get informed about all symbols. */ |
| 4781 | link_info.notice_all = TRUE; |
| 4782 | } |
| 4783 | \f |
| 4784 | /* Overlay handling. We handle overlays with some static variables. */ |
| 4785 | |
| 4786 | /* The overlay virtual address. */ |
| 4787 | static etree_type *overlay_vma; |
| 4788 | /* And subsection alignment. */ |
| 4789 | static etree_type *overlay_subalign; |
| 4790 | |
| 4791 | /* An expression for the maximum section size seen so far. */ |
| 4792 | static etree_type *overlay_max; |
| 4793 | |
| 4794 | /* A list of all the sections in this overlay. */ |
| 4795 | |
| 4796 | struct overlay_list { |
| 4797 | struct overlay_list *next; |
| 4798 | lang_output_section_statement_type *os; |
| 4799 | }; |
| 4800 | |
| 4801 | static struct overlay_list *overlay_list; |
| 4802 | |
| 4803 | /* Start handling an overlay. */ |
| 4804 | |
| 4805 | void |
| 4806 | lang_enter_overlay (etree_type *vma_expr, etree_type *subalign) |
| 4807 | { |
| 4808 | /* The grammar should prevent nested overlays from occurring. */ |
| 4809 | ASSERT (overlay_vma == NULL |
| 4810 | && overlay_subalign == NULL |
| 4811 | && overlay_max == NULL); |
| 4812 | |
| 4813 | overlay_vma = vma_expr; |
| 4814 | overlay_subalign = subalign; |
| 4815 | } |
| 4816 | |
| 4817 | /* Start a section in an overlay. We handle this by calling |
| 4818 | lang_enter_output_section_statement with the correct VMA. |
| 4819 | lang_leave_overlay sets up the LMA and memory regions. */ |
| 4820 | |
| 4821 | void |
| 4822 | lang_enter_overlay_section (const char *name) |
| 4823 | { |
| 4824 | struct overlay_list *n; |
| 4825 | etree_type *size; |
| 4826 | |
| 4827 | lang_enter_output_section_statement (name, overlay_vma, normal_section, |
| 4828 | 0, 0, overlay_subalign, 0); |
| 4829 | |
| 4830 | /* If this is the first section, then base the VMA of future |
| 4831 | sections on this one. This will work correctly even if `.' is |
| 4832 | used in the addresses. */ |
| 4833 | if (overlay_list == NULL) |
| 4834 | overlay_vma = exp_nameop (ADDR, name); |
| 4835 | |
| 4836 | /* Remember the section. */ |
| 4837 | n = xmalloc (sizeof *n); |
| 4838 | n->os = current_section; |
| 4839 | n->next = overlay_list; |
| 4840 | overlay_list = n; |
| 4841 | |
| 4842 | size = exp_nameop (SIZEOF, name); |
| 4843 | |
| 4844 | /* Arrange to work out the maximum section end address. */ |
| 4845 | if (overlay_max == NULL) |
| 4846 | overlay_max = size; |
| 4847 | else |
| 4848 | overlay_max = exp_binop (MAX_K, overlay_max, size); |
| 4849 | } |
| 4850 | |
| 4851 | /* Finish a section in an overlay. There isn't any special to do |
| 4852 | here. */ |
| 4853 | |
| 4854 | void |
| 4855 | lang_leave_overlay_section (fill_type *fill, |
| 4856 | struct lang_output_section_phdr_list *phdrs) |
| 4857 | { |
| 4858 | const char *name; |
| 4859 | char *clean, *s2; |
| 4860 | const char *s1; |
| 4861 | char *buf; |
| 4862 | |
| 4863 | name = current_section->name; |
| 4864 | |
| 4865 | /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory |
| 4866 | region and that no load-time region has been specified. It doesn't |
| 4867 | really matter what we say here, since lang_leave_overlay will |
| 4868 | override it. */ |
| 4869 | lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0); |
| 4870 | |
| 4871 | /* Define the magic symbols. */ |
| 4872 | |
| 4873 | clean = xmalloc (strlen (name) + 1); |
| 4874 | s2 = clean; |
| 4875 | for (s1 = name; *s1 != '\0'; s1++) |
| 4876 | if (ISALNUM (*s1) || *s1 == '_') |
| 4877 | *s2++ = *s1; |
| 4878 | *s2 = '\0'; |
| 4879 | |
| 4880 | buf = xmalloc (strlen (clean) + sizeof "__load_start_"); |
| 4881 | sprintf (buf, "__load_start_%s", clean); |
| 4882 | lang_add_assignment (exp_assop ('=', buf, |
| 4883 | exp_nameop (LOADADDR, name))); |
| 4884 | |
| 4885 | buf = xmalloc (strlen (clean) + sizeof "__load_stop_"); |
| 4886 | sprintf (buf, "__load_stop_%s", clean); |
| 4887 | lang_add_assignment (exp_assop ('=', buf, |
| 4888 | exp_binop ('+', |
| 4889 | exp_nameop (LOADADDR, name), |
| 4890 | exp_nameop (SIZEOF, name)))); |
| 4891 | |
| 4892 | free (clean); |
| 4893 | } |
| 4894 | |
| 4895 | /* Finish an overlay. If there are any overlay wide settings, this |
| 4896 | looks through all the sections in the overlay and sets them. */ |
| 4897 | |
| 4898 | void |
| 4899 | lang_leave_overlay (etree_type *lma_expr, |
| 4900 | int nocrossrefs, |
| 4901 | fill_type *fill, |
| 4902 | const char *memspec, |
| 4903 | struct lang_output_section_phdr_list *phdrs, |
| 4904 | const char *lma_memspec) |
| 4905 | { |
| 4906 | lang_memory_region_type *region; |
| 4907 | lang_memory_region_type *lma_region; |
| 4908 | struct overlay_list *l; |
| 4909 | struct lang_nocrossref *nocrossref; |
| 4910 | |
| 4911 | lang_get_regions (®ion, &lma_region, |
| 4912 | memspec, lma_memspec, |
| 4913 | lma_expr != 0); |
| 4914 | |
| 4915 | nocrossref = NULL; |
| 4916 | |
| 4917 | /* After setting the size of the last section, set '.' to end of the |
| 4918 | overlay region. */ |
| 4919 | if (overlay_list != NULL) |
| 4920 | overlay_list->os->update_dot_tree |
| 4921 | = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max)); |
| 4922 | |
| 4923 | l = overlay_list; |
| 4924 | while (l != NULL) |
| 4925 | { |
| 4926 | struct overlay_list *next; |
| 4927 | |
| 4928 | if (fill != NULL && l->os->fill == NULL) |
| 4929 | l->os->fill = fill; |
| 4930 | |
| 4931 | l->os->region = region; |
| 4932 | l->os->lma_region = lma_region; |
| 4933 | |
| 4934 | /* The first section has the load address specified in the |
| 4935 | OVERLAY statement. The rest are worked out from that. |
| 4936 | The base address is not needed (and should be null) if |
| 4937 | an LMA region was specified. */ |
| 4938 | if (l->next == 0) |
| 4939 | l->os->load_base = lma_expr; |
| 4940 | else if (lma_region == 0) |
| 4941 | l->os->load_base = exp_binop ('+', |
| 4942 | exp_nameop (LOADADDR, l->next->os->name), |
| 4943 | exp_nameop (SIZEOF, l->next->os->name)); |
| 4944 | |
| 4945 | if (phdrs != NULL && l->os->phdrs == NULL) |
| 4946 | l->os->phdrs = phdrs; |
| 4947 | |
| 4948 | if (nocrossrefs) |
| 4949 | { |
| 4950 | struct lang_nocrossref *nc; |
| 4951 | |
| 4952 | nc = xmalloc (sizeof *nc); |
| 4953 | nc->name = l->os->name; |
| 4954 | nc->next = nocrossref; |
| 4955 | nocrossref = nc; |
| 4956 | } |
| 4957 | |
| 4958 | next = l->next; |
| 4959 | free (l); |
| 4960 | l = next; |
| 4961 | } |
| 4962 | |
| 4963 | if (nocrossref != NULL) |
| 4964 | lang_add_nocrossref (nocrossref); |
| 4965 | |
| 4966 | overlay_vma = NULL; |
| 4967 | overlay_list = NULL; |
| 4968 | overlay_max = NULL; |
| 4969 | } |
| 4970 | \f |
| 4971 | /* Version handling. This is only useful for ELF. */ |
| 4972 | |
| 4973 | /* This global variable holds the version tree that we build. */ |
| 4974 | |
| 4975 | struct bfd_elf_version_tree *lang_elf_version_info; |
| 4976 | |
| 4977 | /* If PREV is NULL, return first version pattern matching particular symbol. |
| 4978 | If PREV is non-NULL, return first version pattern matching particular |
| 4979 | symbol after PREV (previously returned by lang_vers_match). */ |
| 4980 | |
| 4981 | static struct bfd_elf_version_expr * |
| 4982 | lang_vers_match (struct bfd_elf_version_expr_head *head, |
| 4983 | struct bfd_elf_version_expr *prev, |
| 4984 | const char *sym) |
| 4985 | { |
| 4986 | const char *cxx_sym = sym; |
| 4987 | const char *java_sym = sym; |
| 4988 | struct bfd_elf_version_expr *expr = NULL; |
| 4989 | |
| 4990 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
| 4991 | { |
| 4992 | cxx_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0); |
| 4993 | if (!cxx_sym) |
| 4994 | cxx_sym = sym; |
| 4995 | } |
| 4996 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
| 4997 | { |
| 4998 | java_sym = cplus_demangle (sym, DMGL_JAVA); |
| 4999 | if (!java_sym) |
| 5000 | java_sym = sym; |
| 5001 | } |
| 5002 | |
| 5003 | if (head->htab && (prev == NULL || prev->symbol)) |
| 5004 | { |
| 5005 | struct bfd_elf_version_expr e; |
| 5006 | |
| 5007 | switch (prev ? prev->mask : 0) |
| 5008 | { |
| 5009 | case 0: |
| 5010 | if (head->mask & BFD_ELF_VERSION_C_TYPE) |
| 5011 | { |
| 5012 | e.symbol = sym; |
| 5013 | expr = htab_find (head->htab, &e); |
| 5014 | while (expr && strcmp (expr->symbol, sym) == 0) |
| 5015 | if (expr->mask == BFD_ELF_VERSION_C_TYPE) |
| 5016 | goto out_ret; |
| 5017 | else |
| 5018 | expr = expr->next; |
| 5019 | } |
| 5020 | /* Fallthrough */ |
| 5021 | case BFD_ELF_VERSION_C_TYPE: |
| 5022 | if (head->mask & BFD_ELF_VERSION_CXX_TYPE) |
| 5023 | { |
| 5024 | e.symbol = cxx_sym; |
| 5025 | expr = htab_find (head->htab, &e); |
| 5026 | while (expr && strcmp (expr->symbol, cxx_sym) == 0) |
| 5027 | if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
| 5028 | goto out_ret; |
| 5029 | else |
| 5030 | expr = expr->next; |
| 5031 | } |
| 5032 | /* Fallthrough */ |
| 5033 | case BFD_ELF_VERSION_CXX_TYPE: |
| 5034 | if (head->mask & BFD_ELF_VERSION_JAVA_TYPE) |
| 5035 | { |
| 5036 | e.symbol = java_sym; |
| 5037 | expr = htab_find (head->htab, &e); |
| 5038 | while (expr && strcmp (expr->symbol, java_sym) == 0) |
| 5039 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
| 5040 | goto out_ret; |
| 5041 | else |
| 5042 | expr = expr->next; |
| 5043 | } |
| 5044 | /* Fallthrough */ |
| 5045 | default: |
| 5046 | break; |
| 5047 | } |
| 5048 | } |
| 5049 | |
| 5050 | /* Finally, try the wildcards. */ |
| 5051 | if (prev == NULL || prev->symbol) |
| 5052 | expr = head->remaining; |
| 5053 | else |
| 5054 | expr = prev->next; |
| 5055 | while (expr) |
| 5056 | { |
| 5057 | const char *s; |
| 5058 | |
| 5059 | if (expr->pattern[0] == '*' && expr->pattern[1] == '\0') |
| 5060 | break; |
| 5061 | |
| 5062 | if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE) |
| 5063 | s = java_sym; |
| 5064 | else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE) |
| 5065 | s = cxx_sym; |
| 5066 | else |
| 5067 | s = sym; |
| 5068 | if (fnmatch (expr->pattern, s, 0) == 0) |
| 5069 | break; |
| 5070 | expr = expr->next; |
| 5071 | } |
| 5072 | |
| 5073 | out_ret: |
| 5074 | if (cxx_sym != sym) |
| 5075 | free ((char *) cxx_sym); |
| 5076 | if (java_sym != sym) |
| 5077 | free ((char *) java_sym); |
| 5078 | return expr; |
| 5079 | } |
| 5080 | |
| 5081 | /* Return NULL if the PATTERN argument is a glob pattern, otherwise, |
| 5082 | return a string pointing to the symbol name. */ |
| 5083 | |
| 5084 | static const char * |
| 5085 | realsymbol (const char *pattern) |
| 5086 | { |
| 5087 | const char *p; |
| 5088 | bfd_boolean changed = FALSE, backslash = FALSE; |
| 5089 | char *s, *symbol = xmalloc (strlen (pattern) + 1); |
| 5090 | |
| 5091 | for (p = pattern, s = symbol; *p != '\0'; ++p) |
| 5092 | { |
| 5093 | /* It is a glob pattern only if there is no preceding |
| 5094 | backslash. */ |
| 5095 | if (! backslash && (*p == '?' || *p == '*' || *p == '[')) |
| 5096 | { |
| 5097 | free (symbol); |
| 5098 | return NULL; |
| 5099 | } |
| 5100 | |
| 5101 | if (backslash) |
| 5102 | { |
| 5103 | /* Remove the preceding backslash. */ |
| 5104 | *(s - 1) = *p; |
| 5105 | changed = TRUE; |
| 5106 | } |
| 5107 | else |
| 5108 | *s++ = *p; |
| 5109 | |
| 5110 | backslash = *p == '\\'; |
| 5111 | } |
| 5112 | |
| 5113 | if (changed) |
| 5114 | { |
| 5115 | *s = '\0'; |
| 5116 | return symbol; |
| 5117 | } |
| 5118 | else |
| 5119 | { |
| 5120 | free (symbol); |
| 5121 | return pattern; |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | /* This is called for each variable name or match expression. */ |
| 5126 | |
| 5127 | struct bfd_elf_version_expr * |
| 5128 | lang_new_vers_pattern (struct bfd_elf_version_expr *orig, |
| 5129 | const char *new, |
| 5130 | const char *lang) |
| 5131 | { |
| 5132 | struct bfd_elf_version_expr *ret; |
| 5133 | |
| 5134 | ret = xmalloc (sizeof *ret); |
| 5135 | ret->next = orig; |
| 5136 | ret->pattern = new; |
| 5137 | ret->symver = 0; |
| 5138 | ret->script = 0; |
| 5139 | ret->symbol = realsymbol (new); |
| 5140 | |
| 5141 | if (lang == NULL || strcasecmp (lang, "C") == 0) |
| 5142 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
| 5143 | else if (strcasecmp (lang, "C++") == 0) |
| 5144 | ret->mask = BFD_ELF_VERSION_CXX_TYPE; |
| 5145 | else if (strcasecmp (lang, "Java") == 0) |
| 5146 | ret->mask = BFD_ELF_VERSION_JAVA_TYPE; |
| 5147 | else |
| 5148 | { |
| 5149 | einfo (_("%X%P: unknown language `%s' in version information\n"), |
| 5150 | lang); |
| 5151 | ret->mask = BFD_ELF_VERSION_C_TYPE; |
| 5152 | } |
| 5153 | |
| 5154 | return ldemul_new_vers_pattern (ret); |
| 5155 | } |
| 5156 | |
| 5157 | /* This is called for each set of variable names and match |
| 5158 | expressions. */ |
| 5159 | |
| 5160 | struct bfd_elf_version_tree * |
| 5161 | lang_new_vers_node (struct bfd_elf_version_expr *globals, |
| 5162 | struct bfd_elf_version_expr *locals) |
| 5163 | { |
| 5164 | struct bfd_elf_version_tree *ret; |
| 5165 | |
| 5166 | ret = xcalloc (1, sizeof *ret); |
| 5167 | ret->globals.list = globals; |
| 5168 | ret->locals.list = locals; |
| 5169 | ret->match = lang_vers_match; |
| 5170 | ret->name_indx = (unsigned int) -1; |
| 5171 | return ret; |
| 5172 | } |
| 5173 | |
| 5174 | /* This static variable keeps track of version indices. */ |
| 5175 | |
| 5176 | static int version_index; |
| 5177 | |
| 5178 | static hashval_t |
| 5179 | version_expr_head_hash (const void *p) |
| 5180 | { |
| 5181 | const struct bfd_elf_version_expr *e = p; |
| 5182 | |
| 5183 | return htab_hash_string (e->symbol); |
| 5184 | } |
| 5185 | |
| 5186 | static int |
| 5187 | version_expr_head_eq (const void *p1, const void *p2) |
| 5188 | { |
| 5189 | const struct bfd_elf_version_expr *e1 = p1; |
| 5190 | const struct bfd_elf_version_expr *e2 = p2; |
| 5191 | |
| 5192 | return strcmp (e1->symbol, e2->symbol) == 0; |
| 5193 | } |
| 5194 | |
| 5195 | static void |
| 5196 | lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head) |
| 5197 | { |
| 5198 | size_t count = 0; |
| 5199 | struct bfd_elf_version_expr *e, *next; |
| 5200 | struct bfd_elf_version_expr **list_loc, **remaining_loc; |
| 5201 | |
| 5202 | for (e = head->list; e; e = e->next) |
| 5203 | { |
| 5204 | if (e->symbol) |
| 5205 | count++; |
| 5206 | head->mask |= e->mask; |
| 5207 | } |
| 5208 | |
| 5209 | if (count) |
| 5210 | { |
| 5211 | head->htab = htab_create (count * 2, version_expr_head_hash, |
| 5212 | version_expr_head_eq, NULL); |
| 5213 | list_loc = &head->list; |
| 5214 | remaining_loc = &head->remaining; |
| 5215 | for (e = head->list; e; e = next) |
| 5216 | { |
| 5217 | next = e->next; |
| 5218 | if (!e->symbol) |
| 5219 | { |
| 5220 | *remaining_loc = e; |
| 5221 | remaining_loc = &e->next; |
| 5222 | } |
| 5223 | else |
| 5224 | { |
| 5225 | void **loc = htab_find_slot (head->htab, e, INSERT); |
| 5226 | |
| 5227 | if (*loc) |
| 5228 | { |
| 5229 | struct bfd_elf_version_expr *e1, *last; |
| 5230 | |
| 5231 | e1 = *loc; |
| 5232 | last = NULL; |
| 5233 | do |
| 5234 | { |
| 5235 | if (e1->mask == e->mask) |
| 5236 | { |
| 5237 | last = NULL; |
| 5238 | break; |
| 5239 | } |
| 5240 | last = e1; |
| 5241 | e1 = e1->next; |
| 5242 | } |
| 5243 | while (e1 && strcmp (e1->symbol, e->symbol) == 0); |
| 5244 | |
| 5245 | if (last == NULL) |
| 5246 | { |
| 5247 | /* This is a duplicate. */ |
| 5248 | /* FIXME: Memory leak. Sometimes pattern is not |
| 5249 | xmalloced alone, but in larger chunk of memory. */ |
| 5250 | /* free (e->symbol); */ |
| 5251 | free (e); |
| 5252 | } |
| 5253 | else |
| 5254 | { |
| 5255 | e->next = last->next; |
| 5256 | last->next = e; |
| 5257 | } |
| 5258 | } |
| 5259 | else |
| 5260 | { |
| 5261 | *loc = e; |
| 5262 | *list_loc = e; |
| 5263 | list_loc = &e->next; |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | *remaining_loc = NULL; |
| 5268 | *list_loc = head->remaining; |
| 5269 | } |
| 5270 | else |
| 5271 | head->remaining = head->list; |
| 5272 | } |
| 5273 | |
| 5274 | /* This is called when we know the name and dependencies of the |
| 5275 | version. */ |
| 5276 | |
| 5277 | void |
| 5278 | lang_register_vers_node (const char *name, |
| 5279 | struct bfd_elf_version_tree *version, |
| 5280 | struct bfd_elf_version_deps *deps) |
| 5281 | { |
| 5282 | struct bfd_elf_version_tree *t, **pp; |
| 5283 | struct bfd_elf_version_expr *e1; |
| 5284 | |
| 5285 | if (name == NULL) |
| 5286 | name = ""; |
| 5287 | |
| 5288 | if ((name[0] == '\0' && lang_elf_version_info != NULL) |
| 5289 | || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0')) |
| 5290 | { |
| 5291 | einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n")); |
| 5292 | free (version); |
| 5293 | return; |
| 5294 | } |
| 5295 | |
| 5296 | /* Make sure this node has a unique name. */ |
| 5297 | for (t = lang_elf_version_info; t != NULL; t = t->next) |
| 5298 | if (strcmp (t->name, name) == 0) |
| 5299 | einfo (_("%X%P: duplicate version tag `%s'\n"), name); |
| 5300 | |
| 5301 | lang_finalize_version_expr_head (&version->globals); |
| 5302 | lang_finalize_version_expr_head (&version->locals); |
| 5303 | |
| 5304 | /* Check the global and local match names, and make sure there |
| 5305 | aren't any duplicates. */ |
| 5306 | |
| 5307 | for (e1 = version->globals.list; e1 != NULL; e1 = e1->next) |
| 5308 | { |
| 5309 | for (t = lang_elf_version_info; t != NULL; t = t->next) |
| 5310 | { |
| 5311 | struct bfd_elf_version_expr *e2; |
| 5312 | |
| 5313 | if (t->locals.htab && e1->symbol) |
| 5314 | { |
| 5315 | e2 = htab_find (t->locals.htab, e1); |
| 5316 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
| 5317 | { |
| 5318 | if (e1->mask == e2->mask) |
| 5319 | einfo (_("%X%P: duplicate expression `%s' in version information\n"), |
| 5320 | e1->symbol); |
| 5321 | e2 = e2->next; |
| 5322 | } |
| 5323 | } |
| 5324 | else if (!e1->symbol) |
| 5325 | for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next) |
| 5326 | if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask) |
| 5327 | einfo (_("%X%P: duplicate expression `%s' in version information\n"), |
| 5328 | e1->pattern); |
| 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | for (e1 = version->locals.list; e1 != NULL; e1 = e1->next) |
| 5333 | { |
| 5334 | for (t = lang_elf_version_info; t != NULL; t = t->next) |
| 5335 | { |
| 5336 | struct bfd_elf_version_expr *e2; |
| 5337 | |
| 5338 | if (t->globals.htab && e1->symbol) |
| 5339 | { |
| 5340 | e2 = htab_find (t->globals.htab, e1); |
| 5341 | while (e2 && strcmp (e1->symbol, e2->symbol) == 0) |
| 5342 | { |
| 5343 | if (e1->mask == e2->mask) |
| 5344 | einfo (_("%X%P: duplicate expression `%s' in version information\n"), |
| 5345 | e1->symbol); |
| 5346 | e2 = e2->next; |
| 5347 | } |
| 5348 | } |
| 5349 | else if (!e1->symbol) |
| 5350 | for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next) |
| 5351 | if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask) |
| 5352 | einfo (_("%X%P: duplicate expression `%s' in version information\n"), |
| 5353 | e1->pattern); |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | version->deps = deps; |
| 5358 | version->name = name; |
| 5359 | if (name[0] != '\0') |
| 5360 | { |
| 5361 | ++version_index; |
| 5362 | version->vernum = version_index; |
| 5363 | } |
| 5364 | else |
| 5365 | version->vernum = 0; |
| 5366 | |
| 5367 | for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next) |
| 5368 | ; |
| 5369 | *pp = version; |
| 5370 | } |
| 5371 | |
| 5372 | /* This is called when we see a version dependency. */ |
| 5373 | |
| 5374 | struct bfd_elf_version_deps * |
| 5375 | lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name) |
| 5376 | { |
| 5377 | struct bfd_elf_version_deps *ret; |
| 5378 | struct bfd_elf_version_tree *t; |
| 5379 | |
| 5380 | ret = xmalloc (sizeof *ret); |
| 5381 | ret->next = list; |
| 5382 | |
| 5383 | for (t = lang_elf_version_info; t != NULL; t = t->next) |
| 5384 | { |
| 5385 | if (strcmp (t->name, name) == 0) |
| 5386 | { |
| 5387 | ret->version_needed = t; |
| 5388 | return ret; |
| 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | einfo (_("%X%P: unable to find version dependency `%s'\n"), name); |
| 5393 | |
| 5394 | return ret; |
| 5395 | } |
| 5396 | |
| 5397 | static void |
| 5398 | lang_do_version_exports_section (void) |
| 5399 | { |
| 5400 | struct bfd_elf_version_expr *greg = NULL, *lreg; |
| 5401 | |
| 5402 | LANG_FOR_EACH_INPUT_STATEMENT (is) |
| 5403 | { |
| 5404 | asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports"); |
| 5405 | char *contents, *p; |
| 5406 | bfd_size_type len; |
| 5407 | |
| 5408 | if (sec == NULL) |
| 5409 | continue; |
| 5410 | |
| 5411 | len = bfd_section_size (is->the_bfd, sec); |
| 5412 | contents = xmalloc (len); |
| 5413 | if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len)) |
| 5414 | einfo (_("%X%P: unable to read .exports section contents\n"), sec); |
| 5415 | |
| 5416 | p = contents; |
| 5417 | while (p < contents + len) |
| 5418 | { |
| 5419 | greg = lang_new_vers_pattern (greg, p, NULL); |
| 5420 | p = strchr (p, '\0') + 1; |
| 5421 | } |
| 5422 | |
| 5423 | /* Do not free the contents, as we used them creating the regex. */ |
| 5424 | |
| 5425 | /* Do not include this section in the link. */ |
| 5426 | bfd_set_section_flags (is->the_bfd, sec, |
| 5427 | bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE); |
| 5428 | } |
| 5429 | |
| 5430 | lreg = lang_new_vers_pattern (NULL, "*", NULL); |
| 5431 | lang_register_vers_node (command_line.version_exports_section, |
| 5432 | lang_new_vers_node (greg, lreg), NULL); |
| 5433 | } |
| 5434 | |
| 5435 | void |
| 5436 | lang_add_unique (const char *name) |
| 5437 | { |
| 5438 | struct unique_sections *ent; |
| 5439 | |
| 5440 | for (ent = unique_section_list; ent; ent = ent->next) |
| 5441 | if (strcmp (ent->name, name) == 0) |
| 5442 | return; |
| 5443 | |
| 5444 | ent = xmalloc (sizeof *ent); |
| 5445 | ent->name = xstrdup (name); |
| 5446 | ent->next = unique_section_list; |
| 5447 | unique_section_list = ent; |
| 5448 | } |