1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
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)
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.
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
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static lang_input_statement_type *lookup_name (const char *);
71 static bfd_boolean load_symbols (lang_input_statement_type *,
72 lang_statement_list_type *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection *);
77 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
78 static void print_statement (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statement_list (lang_statement_union_type *,
81 lang_output_section_statement_type *);
82 static void print_statements (void);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
88 asection *, lang_input_statement_type *, void *);
90 /* Exported variables. */
91 lang_output_section_statement_type *abs_output_section;
92 lang_statement_list_type lang_output_section_statement;
93 lang_statement_list_type *stat_ptr = &statement_list;
94 lang_statement_list_type file_chain = { NULL, NULL };
95 struct bfd_sym_chain entry_symbol = { NULL, NULL };
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_nocrossrefs *nocrossref_list;
103 struct unique_sections *unique_section_list;
104 static bfd_boolean ldlang_sysrooted_script = FALSE;
105 int lang_statement_iteration = 0;
107 etree_type *base; /* Relocation base - or null */
109 /* Return TRUE if the PATTERN argument is a wildcard pattern.
110 Although backslashes are treated specially if a pattern contains
111 wildcards, we do not consider the mere presence of a backslash to
112 be enough to cause the pattern to be treated as a wildcard.
113 That lets us handle DOS filenames more naturally. */
114 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
116 #define new_stat(x, y) \
117 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
119 #define outside_section_address(q) \
120 ((q)->output_offset + (q)->output_section->vma)
122 #define outside_symbol_address(q) \
123 ((q)->value + outside_section_address (q->section))
125 #define SECTION_NAME_MAP_LENGTH (16)
128 stat_alloc (size_t size)
130 return obstack_alloc (&stat_obstack, size);
134 unique_section_p (const asection *sec)
136 struct unique_sections *unam;
139 if (link_info.relocatable
140 && sec->owner != NULL
141 && bfd_is_group_section (sec->owner, sec))
145 for (unam = unique_section_list; unam; unam = unam->next)
146 if (wildcardp (unam->name)
147 ? fnmatch (unam->name, secnam, 0) == 0
148 : strcmp (unam->name, secnam) == 0)
156 /* Generic traversal routines for finding matching sections. */
159 walk_wild_section (lang_wild_statement_type *ptr,
160 lang_input_statement_type *file,
166 if (file->just_syms_flag)
169 for (s = file->the_bfd->sections; s != NULL; s = s->next)
171 struct wildcard_list *sec;
173 sec = ptr->section_list;
175 (*callback) (ptr, sec, s, file, data);
179 bfd_boolean skip = FALSE;
180 struct name_list *list_tmp;
182 /* Don't process sections from files which were
184 for (list_tmp = sec->spec.exclude_name_list;
186 list_tmp = list_tmp->next)
188 if (wildcardp (list_tmp->name))
189 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
191 skip = strcmp (list_tmp->name, file->filename) == 0;
193 /* If this file is part of an archive, and the archive is
194 excluded, exclude this file. */
195 if (! skip && file->the_bfd != NULL
196 && file->the_bfd->my_archive != NULL
197 && file->the_bfd->my_archive->filename != NULL)
199 if (wildcardp (list_tmp->name))
200 skip = fnmatch (list_tmp->name,
201 file->the_bfd->my_archive->filename,
204 skip = strcmp (list_tmp->name,
205 file->the_bfd->my_archive->filename) == 0;
212 if (!skip && sec->spec.name != NULL)
214 const char *sname = bfd_get_section_name (file->the_bfd, s);
216 if (wildcardp (sec->spec.name))
217 skip = fnmatch (sec->spec.name, sname, 0) != 0;
219 skip = strcmp (sec->spec.name, sname) != 0;
223 (*callback) (ptr, sec, s, file, data);
230 /* Handle a wild statement for a single file F. */
233 walk_wild_file (lang_wild_statement_type *s,
234 lang_input_statement_type *f,
238 if (f->the_bfd == NULL
239 || ! bfd_check_format (f->the_bfd, bfd_archive))
240 walk_wild_section (s, f, callback, data);
245 /* This is an archive file. We must map each member of the
246 archive separately. */
247 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
248 while (member != NULL)
250 /* When lookup_name is called, it will call the add_symbols
251 entry point for the archive. For each element of the
252 archive which is included, BFD will call ldlang_add_file,
253 which will set the usrdata field of the member to the
254 lang_input_statement. */
255 if (member->usrdata != NULL)
257 walk_wild_section (s, member->usrdata, callback, data);
260 member = bfd_openr_next_archived_file (f->the_bfd, member);
266 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
268 const char *file_spec = s->filename;
270 if (file_spec == NULL)
272 /* Perform the iteration over all files in the list. */
273 LANG_FOR_EACH_INPUT_STATEMENT (f)
275 walk_wild_file (s, f, callback, data);
278 else if (wildcardp (file_spec))
280 LANG_FOR_EACH_INPUT_STATEMENT (f)
282 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
283 walk_wild_file (s, f, callback, data);
288 lang_input_statement_type *f;
290 /* Perform the iteration over a single file. */
291 f = lookup_name (file_spec);
293 walk_wild_file (s, f, callback, data);
297 /* lang_for_each_statement walks the parse tree and calls the provided
298 function for each node. */
301 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
302 lang_statement_union_type *s)
304 for (; s != NULL; s = s->header.next)
308 switch (s->header.type)
310 case lang_constructors_statement_enum:
311 lang_for_each_statement_worker (func, constructor_list.head);
313 case lang_output_section_statement_enum:
314 lang_for_each_statement_worker
315 (func, s->output_section_statement.children.head);
317 case lang_wild_statement_enum:
318 lang_for_each_statement_worker (func,
319 s->wild_statement.children.head);
321 case lang_group_statement_enum:
322 lang_for_each_statement_worker (func,
323 s->group_statement.children.head);
325 case lang_data_statement_enum:
326 case lang_reloc_statement_enum:
327 case lang_object_symbols_statement_enum:
328 case lang_output_statement_enum:
329 case lang_target_statement_enum:
330 case lang_input_section_enum:
331 case lang_input_statement_enum:
332 case lang_assignment_statement_enum:
333 case lang_padding_statement_enum:
334 case lang_address_statement_enum:
335 case lang_fill_statement_enum:
345 lang_for_each_statement (void (*func) (lang_statement_union_type *))
347 lang_for_each_statement_worker (func, statement_list.head);
350 /*----------------------------------------------------------------------*/
353 lang_list_init (lang_statement_list_type *list)
356 list->tail = &list->head;
359 /* Build a new statement node for the parse tree. */
361 static lang_statement_union_type *
362 new_statement (enum statement_enum type,
364 lang_statement_list_type *list)
366 lang_statement_union_type *new;
368 new = stat_alloc (size);
369 new->header.type = type;
370 new->header.next = NULL;
371 lang_statement_append (list, new, &new->header.next);
375 /* Build a new input file node for the language. There are several
376 ways in which we treat an input file, eg, we only look at symbols,
377 or prefix it with a -l etc.
379 We can be supplied with requests for input files more than once;
380 they may, for example be split over several lines like foo.o(.text)
381 foo.o(.data) etc, so when asked for a file we check that we haven't
382 got it already so we don't duplicate the bfd. */
384 static lang_input_statement_type *
385 new_afile (const char *name,
386 lang_input_file_enum_type file_type,
388 bfd_boolean add_to_list)
390 lang_input_statement_type *p;
393 p = new_stat (lang_input_statement, stat_ptr);
396 p = stat_alloc (sizeof (lang_input_statement_type));
397 p->header.next = NULL;
400 lang_has_input_file = TRUE;
402 p->sysrooted = FALSE;
405 case lang_input_file_is_symbols_only_enum:
407 p->is_archive = FALSE;
409 p->local_sym_name = name;
410 p->just_syms_flag = TRUE;
411 p->search_dirs_flag = FALSE;
413 case lang_input_file_is_fake_enum:
415 p->is_archive = FALSE;
417 p->local_sym_name = name;
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = FALSE;
421 case lang_input_file_is_l_enum:
422 p->is_archive = TRUE;
425 p->local_sym_name = concat ("-l", name, NULL);
426 p->just_syms_flag = FALSE;
427 p->search_dirs_flag = TRUE;
429 case lang_input_file_is_marker_enum:
431 p->is_archive = FALSE;
433 p->local_sym_name = name;
434 p->just_syms_flag = FALSE;
435 p->search_dirs_flag = TRUE;
437 case lang_input_file_is_search_file_enum:
438 p->sysrooted = ldlang_sysrooted_script;
440 p->is_archive = FALSE;
442 p->local_sym_name = name;
443 p->just_syms_flag = FALSE;
444 p->search_dirs_flag = TRUE;
446 case lang_input_file_is_file_enum:
448 p->is_archive = FALSE;
450 p->local_sym_name = name;
451 p->just_syms_flag = FALSE;
452 p->search_dirs_flag = FALSE;
459 p->next_real_file = NULL;
462 p->dynamic = config.dynamic_link;
463 p->add_needed = add_needed;
464 p->as_needed = as_needed;
465 p->whole_archive = whole_archive;
467 lang_statement_append (&input_file_chain,
468 (lang_statement_union_type *) p,
473 lang_input_statement_type *
474 lang_add_input_file (const char *name,
475 lang_input_file_enum_type file_type,
478 lang_has_input_file = TRUE;
479 return new_afile (name, file_type, target, TRUE);
482 /* Build enough state so that the parser can build its tree. */
487 obstack_begin (&stat_obstack, 1000);
489 stat_ptr = &statement_list;
491 lang_list_init (stat_ptr);
493 lang_list_init (&input_file_chain);
494 lang_list_init (&lang_output_section_statement);
495 lang_list_init (&file_chain);
496 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
499 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
501 abs_output_section->bfd_section = bfd_abs_section_ptr;
503 /* The value "3" is ad-hoc, somewhat related to the expected number of
504 DEFINED expressions in a linker script. For most default linker
505 scripts, there are none. Why a hash table then? Well, it's somewhat
506 simpler to re-use working machinery than using a linked list in terms
507 of code-complexity here in ld, besides the initialization which just
508 looks like other code here. */
509 if (!bfd_hash_table_init_n (&lang_definedness_table,
510 lang_definedness_newfunc, 3))
511 einfo (_("%P%F: out of memory during initialization"));
513 /* Callers of exp_fold_tree need to increment this. */
514 lang_statement_iteration = 0;
517 /*----------------------------------------------------------------------
518 A region is an area of memory declared with the
519 MEMORY { name:org=exp, len=exp ... }
522 We maintain a list of all the regions here.
524 If no regions are specified in the script, then the default is used
525 which is created when looked up to be the entire data space.
527 If create is true we are creating a region inside a MEMORY block.
528 In this case it is probably an error to create a region that has
529 already been created. If we are not inside a MEMORY block it is
530 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
531 and so we issue a warning. */
533 static lang_memory_region_type *lang_memory_region_list;
534 static lang_memory_region_type **lang_memory_region_list_tail
535 = &lang_memory_region_list;
537 lang_memory_region_type *
538 lang_memory_region_lookup (const char *const name, bfd_boolean create)
540 lang_memory_region_type *p;
541 lang_memory_region_type *new;
543 /* NAME is NULL for LMA memspecs if no region was specified. */
547 for (p = lang_memory_region_list; p != NULL; p = p->next)
548 if (strcmp (p->name, name) == 0)
551 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
557 /* This code used to always use the first region in the list as the
558 default region. I changed it to instead use a region
559 encompassing all of memory as the default region. This permits
560 NOLOAD sections to work reasonably without requiring a region.
561 People should specify what region they mean, if they really want
563 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
565 if (lang_memory_region_list != NULL)
566 return lang_memory_region_list;
570 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
571 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
573 new = stat_alloc (sizeof (lang_memory_region_type));
575 new->name = xstrdup (name);
578 *lang_memory_region_list_tail = new;
579 lang_memory_region_list_tail = &new->next;
583 new->length = ~(bfd_size_type) 0;
585 new->had_full_message = FALSE;
590 static lang_memory_region_type *
591 lang_memory_default (asection *section)
593 lang_memory_region_type *p;
595 flagword sec_flags = section->flags;
597 /* Override SEC_DATA to mean a writable section. */
598 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
599 sec_flags |= SEC_DATA;
601 for (p = lang_memory_region_list; p != NULL; p = p->next)
603 if ((p->flags & sec_flags) != 0
604 && (p->not_flags & sec_flags) == 0)
609 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
612 static lang_output_section_statement_type *
613 lang_output_section_find_1 (const char *const name, int constraint)
615 lang_statement_union_type *u;
616 lang_output_section_statement_type *lookup;
618 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
620 lookup = &u->output_section_statement;
622 if (strcmp (name, lookup->name) == 0
623 && lookup->constraint != -1
624 && (constraint == 0 || constraint == lookup->constraint))
630 lang_output_section_statement_type *
631 lang_output_section_find (const char *const name)
633 return lang_output_section_find_1 (name, 0);
636 static lang_output_section_statement_type *
637 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
639 lang_output_section_statement_type *lookup;
641 lookup = lang_output_section_find_1 (name, constraint);
644 lookup = new_stat (lang_output_section_statement, stat_ptr);
645 lookup->region = NULL;
646 lookup->lma_region = NULL;
648 lookup->block_value = 1;
652 lookup->bfd_section = NULL;
653 lookup->processed = 0;
654 lookup->constraint = constraint;
655 lookup->sectype = normal_section;
656 lookup->addr_tree = NULL;
657 lang_list_init (&lookup->children);
659 lookup->memspec = NULL;
661 lookup->subsection_alignment = -1;
662 lookup->section_alignment = -1;
663 lookup->load_base = NULL;
664 lookup->update_dot_tree = NULL;
665 lookup->phdrs = NULL;
667 lang_statement_append (&lang_output_section_statement,
668 (lang_statement_union_type *) lookup,
674 lang_output_section_statement_type *
675 lang_output_section_statement_lookup (const char *const name)
677 return lang_output_section_statement_lookup_1 (name, 0);
681 lang_map_flags (flagword flag)
683 if (flag & SEC_ALLOC)
689 if (flag & SEC_READONLY)
702 lang_memory_region_type *m;
705 minfo (_("\nMemory Configuration\n\n"));
706 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
707 _("Name"), _("Origin"), _("Length"), _("Attributes"));
709 for (m = lang_memory_region_list; m != NULL; m = m->next)
714 fprintf (config.map_file, "%-16s ", m->name);
716 sprintf_vma (buf, m->origin);
717 minfo ("0x%s ", buf);
725 minfo ("0x%V", m->length);
726 if (m->flags || m->not_flags)
734 lang_map_flags (m->flags);
740 lang_map_flags (m->not_flags);
747 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
749 if (! command_line.reduce_memory_overheads)
751 obstack_begin (&map_obstack, 1000);
752 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
753 bfd_map_over_sections (p, init_map_userdata, 0);
754 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
760 init_map_userdata (abfd, sec, data)
761 bfd *abfd ATTRIBUTE_UNUSED;
763 void *data ATTRIBUTE_UNUSED;
765 fat_section_userdata_type *new_data
766 = ((fat_section_userdata_type *) (stat_alloc
767 (sizeof (fat_section_userdata_type))));
769 ASSERT (get_userdata (sec) == NULL);
770 get_userdata (sec) = new_data;
771 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
775 sort_def_symbol (hash_entry, info)
776 struct bfd_link_hash_entry *hash_entry;
777 void *info ATTRIBUTE_UNUSED;
779 if (hash_entry->type == bfd_link_hash_defined
780 || hash_entry->type == bfd_link_hash_defweak)
782 struct fat_user_section_struct *ud;
783 struct map_symbol_def *def;
785 ud = get_userdata (hash_entry->u.def.section);
788 /* ??? What do we have to do to initialize this beforehand? */
789 /* The first time we get here is bfd_abs_section... */
790 init_map_userdata (0, hash_entry->u.def.section, 0);
791 ud = get_userdata (hash_entry->u.def.section);
793 else if (!ud->map_symbol_def_tail)
794 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
796 def = obstack_alloc (&map_obstack, sizeof *def);
797 def->entry = hash_entry;
798 *(ud->map_symbol_def_tail) = def;
799 ud->map_symbol_def_tail = &def->next;
804 /* Initialize an output section. */
807 init_os (lang_output_section_statement_type *s)
809 lean_section_userdata_type *new;
811 if (s->bfd_section != NULL)
814 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
815 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
817 new = stat_alloc (SECTION_USERDATA_SIZE);
818 memset (new, 0, SECTION_USERDATA_SIZE);
820 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
821 if (s->bfd_section == NULL)
822 s->bfd_section = bfd_make_section (output_bfd, s->name);
823 if (s->bfd_section == NULL)
825 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
826 output_bfd->xvec->name, s->name);
828 s->bfd_section->output_section = s->bfd_section;
830 /* We initialize an output sections output offset to minus its own
831 vma to allow us to output a section through itself. */
832 s->bfd_section->output_offset = 0;
833 get_userdata (s->bfd_section) = new;
835 /* If there is a base address, make sure that any sections it might
836 mention are initialized. */
837 if (s->addr_tree != NULL)
838 exp_init_os (s->addr_tree);
840 if (s->load_base != NULL)
841 exp_init_os (s->load_base);
844 /* Make sure that all output sections mentioned in an expression are
848 exp_init_os (etree_type *exp)
850 switch (exp->type.node_class)
853 exp_init_os (exp->assign.src);
857 exp_init_os (exp->binary.lhs);
858 exp_init_os (exp->binary.rhs);
862 exp_init_os (exp->trinary.cond);
863 exp_init_os (exp->trinary.lhs);
864 exp_init_os (exp->trinary.rhs);
868 exp_init_os (exp->assert_s.child);
872 exp_init_os (exp->unary.child);
876 switch (exp->type.node_code)
882 lang_output_section_statement_type *os;
884 os = lang_output_section_find (exp->name.name);
885 if (os != NULL && os->bfd_section == NULL)
897 section_already_linked (bfd *abfd, asection *sec, void *data)
899 lang_input_statement_type *entry = data;
901 /* If we are only reading symbols from this object, then we want to
902 discard all sections. */
903 if (entry->just_syms_flag)
905 bfd_link_just_syms (sec, &link_info);
909 bfd_section_already_linked (abfd, sec);
912 /* The wild routines.
914 These expand statements like *(.text) and foo.o to a list of
915 explicit actions, like foo.o(.text), bar.o(.text) and
916 foo.o(.text, .data). */
918 /* Add SECTION to the output section OUTPUT. Do this by creating a
919 lang_input_section statement which is placed at PTR. FILE is the
920 input file which holds SECTION. */
923 lang_add_section (lang_statement_list_type *ptr,
925 lang_output_section_statement_type *output,
926 lang_input_statement_type *file)
928 flagword flags = section->flags;
931 /* Discard sections marked with SEC_EXCLUDE. */
932 discard = (flags & SEC_EXCLUDE) != 0;
934 /* Discard input sections which are assigned to a section named
935 DISCARD_SECTION_NAME. */
936 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
939 /* Discard debugging sections if we are stripping debugging
941 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
942 && (flags & SEC_DEBUGGING) != 0)
947 if (section->output_section == NULL)
949 /* This prevents future calls from assigning this section. */
950 section->output_section = bfd_abs_section_ptr;
955 if (section->output_section == NULL)
958 lang_input_section_type *new;
961 if (output->bfd_section == NULL)
964 first = ! output->bfd_section->linker_has_input;
965 output->bfd_section->linker_has_input = 1;
967 /* Add a section reference to the list. */
968 new = new_stat (lang_input_section, ptr);
970 new->section = section;
972 section->output_section = output->bfd_section;
974 flags = section->flags;
976 /* We don't copy the SEC_NEVER_LOAD flag from an input section
977 to an output section, because we want to be able to include a
978 SEC_NEVER_LOAD section in the middle of an otherwise loaded
979 section (I don't know why we want to do this, but we do).
980 build_link_order in ldwrite.c handles this case by turning
981 the embedded SEC_NEVER_LOAD section into a fill. */
983 flags &= ~ SEC_NEVER_LOAD;
985 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
986 already been processed. One reason to do this is that on pe
987 format targets, .text$foo sections go into .text and it's odd
988 to see .text with SEC_LINK_ONCE set. */
990 if (! link_info.relocatable)
991 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
993 /* If this is not the first input section, and the SEC_READONLY
994 flag is not currently set, then don't set it just because the
995 input section has it set. */
997 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
998 flags &= ~ SEC_READONLY;
1000 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1002 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1003 != (flags & (SEC_MERGE | SEC_STRINGS))
1004 || ((flags & SEC_MERGE)
1005 && section->output_section->entsize != section->entsize)))
1007 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1008 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1011 section->output_section->flags |= flags;
1013 if (flags & SEC_MERGE)
1014 section->output_section->entsize = section->entsize;
1016 /* If SEC_READONLY is not set in the input section, then clear
1017 it from the output section. */
1018 if ((section->flags & SEC_READONLY) == 0)
1019 section->output_section->flags &= ~SEC_READONLY;
1021 switch (output->sectype)
1023 case normal_section:
1028 case overlay_section:
1029 output->bfd_section->flags &= ~SEC_ALLOC;
1031 case noload_section:
1032 output->bfd_section->flags &= ~SEC_LOAD;
1033 output->bfd_section->flags |= SEC_NEVER_LOAD;
1037 /* Copy over SEC_SMALL_DATA. */
1038 if (section->flags & SEC_SMALL_DATA)
1039 section->output_section->flags |= SEC_SMALL_DATA;
1041 if (section->alignment_power > output->bfd_section->alignment_power)
1042 output->bfd_section->alignment_power = section->alignment_power;
1044 /* If supplied an alignment, then force it. */
1045 if (output->section_alignment != -1)
1046 output->bfd_section->alignment_power = output->section_alignment;
1048 if (section->flags & SEC_BLOCK)
1050 section->output_section->flags |= SEC_BLOCK;
1051 /* FIXME: This value should really be obtained from the bfd... */
1052 output->block_value = 128;
1057 /* Handle wildcard sorting. This returns the lang_input_section which
1058 should follow the one we are going to create for SECTION and FILE,
1059 based on the sorting requirements of WILD. It returns NULL if the
1060 new section should just go at the end of the current list. */
1062 static lang_statement_union_type *
1063 wild_sort (lang_wild_statement_type *wild,
1064 struct wildcard_list *sec,
1065 lang_input_statement_type *file,
1068 const char *section_name;
1069 lang_statement_union_type *l;
1071 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1074 section_name = bfd_get_section_name (file->the_bfd, section);
1075 for (l = wild->children.head; l != NULL; l = l->header.next)
1077 lang_input_section_type *ls;
1079 if (l->header.type != lang_input_section_enum)
1081 ls = &l->input_section;
1083 /* Sorting by filename takes precedence over sorting by section
1086 if (wild->filenames_sorted)
1088 const char *fn, *ln;
1092 /* The PE support for the .idata section as generated by
1093 dlltool assumes that files will be sorted by the name of
1094 the archive and then the name of the file within the
1097 if (file->the_bfd != NULL
1098 && bfd_my_archive (file->the_bfd) != NULL)
1100 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1105 fn = file->filename;
1109 if (ls->ifile->the_bfd != NULL
1110 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1112 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1117 ln = ls->ifile->filename;
1121 i = strcmp (fn, ln);
1130 fn = file->filename;
1132 ln = ls->ifile->filename;
1134 i = strcmp (fn, ln);
1142 /* Here either the files are not sorted by name, or we are
1143 looking at the sections for this file. */
1145 if (sec != NULL && sec->spec.sorted)
1147 if (strcmp (section_name,
1148 bfd_get_section_name (ls->ifile->the_bfd,
1158 /* Expand a wild statement for a particular FILE. SECTION may be
1159 NULL, in which case it is a wild card. */
1162 output_section_callback (lang_wild_statement_type *ptr,
1163 struct wildcard_list *sec,
1165 lang_input_statement_type *file,
1168 lang_statement_union_type *before;
1170 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1171 if (unique_section_p (section))
1174 before = wild_sort (ptr, sec, file, section);
1176 /* Here BEFORE points to the lang_input_section which
1177 should follow the one we are about to add. If BEFORE
1178 is NULL, then the section should just go at the end
1179 of the current list. */
1182 lang_add_section (&ptr->children, section,
1183 (lang_output_section_statement_type *) output,
1187 lang_statement_list_type list;
1188 lang_statement_union_type **pp;
1190 lang_list_init (&list);
1191 lang_add_section (&list, section,
1192 (lang_output_section_statement_type *) output,
1195 /* If we are discarding the section, LIST.HEAD will
1197 if (list.head != NULL)
1199 ASSERT (list.head->header.next == NULL);
1201 for (pp = &ptr->children.head;
1203 pp = &(*pp)->header.next)
1204 ASSERT (*pp != NULL);
1206 list.head->header.next = *pp;
1212 /* Check if all sections in a wild statement for a particular FILE
1216 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1217 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1219 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1222 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1223 if (unique_section_p (section))
1226 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1227 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1230 /* This is passed a file name which must have been seen already and
1231 added to the statement tree. We will see if it has been opened
1232 already and had its symbols read. If not then we'll read it. */
1234 static lang_input_statement_type *
1235 lookup_name (const char *name)
1237 lang_input_statement_type *search;
1239 for (search = (lang_input_statement_type *) input_file_chain.head;
1241 search = (lang_input_statement_type *) search->next_real_file)
1243 /* Use the local_sym_name as the name of the file that has
1244 already been loaded as filename might have been transformed
1245 via the search directory lookup mechanism. */
1246 const char * filename = search->local_sym_name;
1248 if (filename == NULL && name == NULL)
1250 if (filename != NULL
1252 && strcmp (filename, name) == 0)
1257 search = new_afile (name, lang_input_file_is_search_file_enum,
1258 default_target, FALSE);
1260 /* If we have already added this file, or this file is not real
1261 (FIXME: can that ever actually happen?) or the name is NULL
1262 (FIXME: can that ever actually happen?) don't add this file. */
1265 || search->filename == NULL)
1268 if (! load_symbols (search, NULL))
1274 /* Get the symbols for an input file. */
1277 load_symbols (lang_input_statement_type *entry,
1278 lang_statement_list_type *place)
1285 ldfile_open_file (entry);
1287 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1288 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1291 lang_statement_list_type *hold;
1292 bfd_boolean bad_load = TRUE;
1293 bfd_boolean save_ldlang_sysrooted_script;
1295 err = bfd_get_error ();
1297 /* See if the emulation has some special knowledge. */
1298 if (ldemul_unrecognized_file (entry))
1301 if (err == bfd_error_file_ambiguously_recognized)
1305 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1306 einfo (_("%B: matching formats:"), entry->the_bfd);
1307 for (p = matching; *p != NULL; p++)
1311 else if (err != bfd_error_file_not_recognized
1313 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1317 bfd_close (entry->the_bfd);
1318 entry->the_bfd = NULL;
1320 /* Try to interpret the file as a linker script. */
1321 ldfile_open_command_file (entry->filename);
1325 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1326 ldlang_sysrooted_script = entry->sysrooted;
1328 ldfile_assumed_script = TRUE;
1329 parser_input = input_script;
1331 ldfile_assumed_script = FALSE;
1333 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1339 if (ldemul_recognized_file (entry))
1342 /* We don't call ldlang_add_file for an archive. Instead, the
1343 add_symbols entry point will call ldlang_add_file, via the
1344 add_archive_element callback, for each element of the archive
1346 switch (bfd_get_format (entry->the_bfd))
1352 ldlang_add_file (entry);
1353 if (trace_files || trace_file_tries)
1354 info_msg ("%I\n", entry);
1358 if (entry->whole_archive)
1361 bfd_boolean loaded = TRUE;
1365 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1370 if (! bfd_check_format (member, bfd_object))
1372 einfo (_("%F%B: member %B in archive is not an object\n"),
1373 entry->the_bfd, member);
1377 if (! ((*link_info.callbacks->add_archive_element)
1378 (&link_info, member, "--whole-archive")))
1381 if (! bfd_link_add_symbols (member, &link_info))
1383 einfo (_("%F%B: could not read symbols: %E\n"), member);
1388 entry->loaded = loaded;
1394 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1395 entry->loaded = TRUE;
1397 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1399 return entry->loaded;
1402 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1403 may be NULL, indicating that it is a wildcard. Separate
1404 lang_input_section statements are created for each part of the
1405 expansion; they are added after the wild statement S. OUTPUT is
1406 the output section. */
1409 wild (lang_wild_statement_type *s,
1410 const char *target ATTRIBUTE_UNUSED,
1411 lang_output_section_statement_type *output)
1413 struct wildcard_list *sec;
1415 walk_wild (s, output_section_callback, output);
1417 for (sec = s->section_list; sec != NULL; sec = sec->next)
1419 if (default_common_section != NULL)
1421 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1423 /* Remember the section that common is going to in case we
1424 later get something which doesn't know where to put it. */
1425 default_common_section = output;
1430 /* Return TRUE iff target is the sought target. */
1433 get_target (const bfd_target *target, void *data)
1435 const char *sought = data;
1437 return strcmp (target->name, sought) == 0;
1440 /* Like strcpy() but convert to lower case as well. */
1443 stricpy (char *dest, char *src)
1447 while ((c = *src++) != 0)
1448 *dest++ = TOLOWER (c);
1453 /* Remove the first occurrence of needle (if any) in haystack
1457 strcut (char *haystack, char *needle)
1459 haystack = strstr (haystack, needle);
1465 for (src = haystack + strlen (needle); *src;)
1466 *haystack++ = *src++;
1472 /* Compare two target format name strings.
1473 Return a value indicating how "similar" they are. */
1476 name_compare (char *first, char *second)
1482 copy1 = xmalloc (strlen (first) + 1);
1483 copy2 = xmalloc (strlen (second) + 1);
1485 /* Convert the names to lower case. */
1486 stricpy (copy1, first);
1487 stricpy (copy2, second);
1489 /* Remove size and endian strings from the name. */
1490 strcut (copy1, "big");
1491 strcut (copy1, "little");
1492 strcut (copy2, "big");
1493 strcut (copy2, "little");
1495 /* Return a value based on how many characters match,
1496 starting from the beginning. If both strings are
1497 the same then return 10 * their length. */
1498 for (result = 0; copy1[result] == copy2[result]; result++)
1499 if (copy1[result] == 0)
1511 /* Set by closest_target_match() below. */
1512 static const bfd_target *winner;
1514 /* Scan all the valid bfd targets looking for one that has the endianness
1515 requirement that was specified on the command line, and is the nearest
1516 match to the original output target. */
1519 closest_target_match (const bfd_target *target, void *data)
1521 const bfd_target *original = data;
1523 if (command_line.endian == ENDIAN_BIG
1524 && target->byteorder != BFD_ENDIAN_BIG)
1527 if (command_line.endian == ENDIAN_LITTLE
1528 && target->byteorder != BFD_ENDIAN_LITTLE)
1531 /* Must be the same flavour. */
1532 if (target->flavour != original->flavour)
1535 /* If we have not found a potential winner yet, then record this one. */
1542 /* Oh dear, we now have two potential candidates for a successful match.
1543 Compare their names and choose the better one. */
1544 if (name_compare (target->name, original->name)
1545 > name_compare (winner->name, original->name))
1548 /* Keep on searching until wqe have checked them all. */
1552 /* Return the BFD target format of the first input file. */
1555 get_first_input_target (void)
1557 char *target = NULL;
1559 LANG_FOR_EACH_INPUT_STATEMENT (s)
1561 if (s->header.type == lang_input_statement_enum
1564 ldfile_open_file (s);
1566 if (s->the_bfd != NULL
1567 && bfd_check_format (s->the_bfd, bfd_object))
1569 target = bfd_get_target (s->the_bfd);
1581 lang_get_output_target (void)
1585 /* Has the user told us which output format to use? */
1586 if (output_target != NULL)
1587 return output_target;
1589 /* No - has the current target been set to something other than
1591 if (current_target != default_target)
1592 return current_target;
1594 /* No - can we determine the format of the first input file? */
1595 target = get_first_input_target ();
1599 /* Failed - use the default output target. */
1600 return default_target;
1603 /* Open the output file. */
1606 open_output (const char *name)
1610 output_target = lang_get_output_target ();
1612 /* Has the user requested a particular endianness on the command
1614 if (command_line.endian != ENDIAN_UNSET)
1616 const bfd_target *target;
1617 enum bfd_endian desired_endian;
1619 /* Get the chosen target. */
1620 target = bfd_search_for_target (get_target, (void *) output_target);
1622 /* If the target is not supported, we cannot do anything. */
1625 if (command_line.endian == ENDIAN_BIG)
1626 desired_endian = BFD_ENDIAN_BIG;
1628 desired_endian = BFD_ENDIAN_LITTLE;
1630 /* See if the target has the wrong endianness. This should
1631 not happen if the linker script has provided big and
1632 little endian alternatives, but some scrips don't do
1634 if (target->byteorder != desired_endian)
1636 /* If it does, then see if the target provides
1637 an alternative with the correct endianness. */
1638 if (target->alternative_target != NULL
1639 && (target->alternative_target->byteorder == desired_endian))
1640 output_target = target->alternative_target->name;
1643 /* Try to find a target as similar as possible to
1644 the default target, but which has the desired
1645 endian characteristic. */
1646 bfd_search_for_target (closest_target_match,
1649 /* Oh dear - we could not find any targets that
1650 satisfy our requirements. */
1652 einfo (_("%P: warning: could not find any targets"
1653 " that match endianness requirement\n"));
1655 output_target = winner->name;
1661 output = bfd_openw (name, output_target);
1665 if (bfd_get_error () == bfd_error_invalid_target)
1666 einfo (_("%P%F: target %s not found\n"), output_target);
1668 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1671 delete_output_file_on_failure = TRUE;
1674 output->flags |= D_PAGED;
1677 if (! bfd_set_format (output, bfd_object))
1678 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1679 if (! bfd_set_arch_mach (output,
1680 ldfile_output_architecture,
1681 ldfile_output_machine))
1682 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1684 link_info.hash = bfd_link_hash_table_create (output);
1685 if (link_info.hash == NULL)
1686 einfo (_("%P%F: can not create link hash table: %E\n"));
1688 bfd_set_gp_size (output, g_switch_value);
1693 ldlang_open_output (lang_statement_union_type *statement)
1695 switch (statement->header.type)
1697 case lang_output_statement_enum:
1698 ASSERT (output_bfd == NULL);
1699 output_bfd = open_output (statement->output_statement.name);
1700 ldemul_set_output_arch ();
1701 if (config.magic_demand_paged && !link_info.relocatable)
1702 output_bfd->flags |= D_PAGED;
1704 output_bfd->flags &= ~D_PAGED;
1705 if (config.text_read_only)
1706 output_bfd->flags |= WP_TEXT;
1708 output_bfd->flags &= ~WP_TEXT;
1709 if (link_info.traditional_format)
1710 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1712 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1715 case lang_target_statement_enum:
1716 current_target = statement->target_statement.target;
1723 /* Convert between addresses in bytes and sizes in octets.
1724 For currently supported targets, octets_per_byte is always a power
1725 of two, so we can use shifts. */
1726 #define TO_ADDR(X) ((X) >> opb_shift)
1727 #define TO_SIZE(X) ((X) << opb_shift)
1729 /* Support the above. */
1730 static unsigned int opb_shift = 0;
1735 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1736 ldfile_output_machine);
1739 while ((x & 1) == 0)
1747 /* Open all the input files. */
1750 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1752 for (; s != NULL; s = s->header.next)
1754 switch (s->header.type)
1756 case lang_constructors_statement_enum:
1757 open_input_bfds (constructor_list.head, force);
1759 case lang_output_section_statement_enum:
1760 open_input_bfds (s->output_section_statement.children.head, force);
1762 case lang_wild_statement_enum:
1763 /* Maybe we should load the file's symbols. */
1764 if (s->wild_statement.filename
1765 && ! wildcardp (s->wild_statement.filename))
1766 lookup_name (s->wild_statement.filename);
1767 open_input_bfds (s->wild_statement.children.head, force);
1769 case lang_group_statement_enum:
1771 struct bfd_link_hash_entry *undefs;
1773 /* We must continually search the entries in the group
1774 until no new symbols are added to the list of undefined
1779 undefs = link_info.hash->undefs_tail;
1780 open_input_bfds (s->group_statement.children.head, TRUE);
1782 while (undefs != link_info.hash->undefs_tail);
1785 case lang_target_statement_enum:
1786 current_target = s->target_statement.target;
1788 case lang_input_statement_enum:
1789 if (s->input_statement.real)
1791 lang_statement_list_type add;
1793 s->input_statement.target = current_target;
1795 /* If we are being called from within a group, and this
1796 is an archive which has already been searched, then
1797 force it to be researched unless the whole archive
1798 has been loaded already. */
1800 && !s->input_statement.whole_archive
1801 && s->input_statement.loaded
1802 && bfd_check_format (s->input_statement.the_bfd,
1804 s->input_statement.loaded = FALSE;
1806 lang_list_init (&add);
1808 if (! load_symbols (&s->input_statement, &add))
1809 config.make_executable = FALSE;
1811 if (add.head != NULL)
1813 *add.tail = s->header.next;
1814 s->header.next = add.head;
1824 /* If there are [COMMONS] statements, put a wild one into the bss
1828 lang_reasonable_defaults (void)
1831 lang_output_section_statement_lookup (".text");
1832 lang_output_section_statement_lookup (".data");
1834 default_common_section = lang_output_section_statement_lookup (".bss");
1836 if (!placed_commons)
1838 lang_wild_statement_type *new =
1839 new_stat (lang_wild_statement,
1840 &default_common_section->children);
1842 new->section_name = "COMMON";
1843 new->filename = NULL;
1844 lang_list_init (&new->children);
1849 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1852 lang_track_definedness (const char *name)
1854 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1855 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1858 /* New-function for the definedness hash table. */
1860 static struct bfd_hash_entry *
1861 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1862 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1863 const char *name ATTRIBUTE_UNUSED)
1865 struct lang_definedness_hash_entry *ret
1866 = (struct lang_definedness_hash_entry *) entry;
1869 ret = (struct lang_definedness_hash_entry *)
1870 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1873 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1875 ret->iteration = -1;
1879 /* Return the iteration when the definition of NAME was last updated. A
1880 value of -1 means that the symbol is not defined in the linker script
1881 or the command line, but may be defined in the linker symbol table. */
1884 lang_symbol_definition_iteration (const char *name)
1886 struct lang_definedness_hash_entry *defentry
1887 = (struct lang_definedness_hash_entry *)
1888 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1890 /* We've already created this one on the presence of DEFINED in the
1891 script, so it can't be NULL unless something is borked elsewhere in
1893 if (defentry == NULL)
1896 return defentry->iteration;
1899 /* Update the definedness state of NAME. */
1902 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1904 struct lang_definedness_hash_entry *defentry
1905 = (struct lang_definedness_hash_entry *)
1906 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1908 /* We don't keep track of symbols not tested with DEFINED. */
1909 if (defentry == NULL)
1912 /* If the symbol was already defined, and not from an earlier statement
1913 iteration, don't update the definedness iteration, because that'd
1914 make the symbol seem defined in the linker script at this point, and
1915 it wasn't; it was defined in some object. If we do anyway, DEFINED
1916 would start to yield false before this point and the construct "sym =
1917 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1919 if (h->type != bfd_link_hash_undefined
1920 && h->type != bfd_link_hash_common
1921 && h->type != bfd_link_hash_new
1922 && defentry->iteration == -1)
1925 defentry->iteration = lang_statement_iteration;
1928 /* Add the supplied name to the symbol table as an undefined reference.
1929 This is a two step process as the symbol table doesn't even exist at
1930 the time the ld command line is processed. First we put the name
1931 on a list, then, once the output file has been opened, transfer the
1932 name to the symbol table. */
1934 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
1936 #define ldlang_undef_chain_list_head entry_symbol.next
1939 ldlang_add_undef (const char *const name)
1941 ldlang_undef_chain_list_type *new =
1942 stat_alloc (sizeof (ldlang_undef_chain_list_type));
1944 new->next = ldlang_undef_chain_list_head;
1945 ldlang_undef_chain_list_head = new;
1947 new->name = xstrdup (name);
1949 if (output_bfd != NULL)
1950 insert_undefined (new->name);
1953 /* Insert NAME as undefined in the symbol table. */
1956 insert_undefined (const char *name)
1958 struct bfd_link_hash_entry *h;
1960 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
1962 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1963 if (h->type == bfd_link_hash_new)
1965 h->type = bfd_link_hash_undefined;
1966 h->u.undef.abfd = NULL;
1967 bfd_link_add_undef (link_info.hash, h);
1971 /* Run through the list of undefineds created above and place them
1972 into the linker hash table as undefined symbols belonging to the
1976 lang_place_undefineds (void)
1978 ldlang_undef_chain_list_type *ptr;
1980 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
1981 insert_undefined (ptr->name);
1984 /* Check for all readonly or some readwrite sections. */
1987 check_input_sections
1988 (lang_statement_union_type *s,
1989 lang_output_section_statement_type *output_section_statement)
1991 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
1993 switch (s->header.type)
1995 case lang_wild_statement_enum:
1996 walk_wild (&s->wild_statement, check_section_callback,
1997 output_section_statement);
1998 if (! output_section_statement->all_input_readonly)
2001 case lang_constructors_statement_enum:
2002 check_input_sections (constructor_list.head,
2003 output_section_statement);
2004 if (! output_section_statement->all_input_readonly)
2007 case lang_group_statement_enum:
2008 check_input_sections (s->group_statement.children.head,
2009 output_section_statement);
2010 if (! output_section_statement->all_input_readonly)
2019 /* Open input files and attach to output sections. */
2022 map_input_to_output_sections
2023 (lang_statement_union_type *s, const char *target,
2024 lang_output_section_statement_type *output_section_statement)
2026 for (; s != NULL; s = s->header.next)
2028 switch (s->header.type)
2030 case lang_wild_statement_enum:
2031 wild (&s->wild_statement, target, output_section_statement);
2033 case lang_constructors_statement_enum:
2034 map_input_to_output_sections (constructor_list.head,
2036 output_section_statement);
2038 case lang_output_section_statement_enum:
2039 if (s->output_section_statement.constraint)
2041 if (s->output_section_statement.constraint == -1)
2043 s->output_section_statement.all_input_readonly = TRUE;
2044 check_input_sections (s->output_section_statement.children.head,
2045 &s->output_section_statement);
2046 if ((s->output_section_statement.all_input_readonly
2047 && s->output_section_statement.constraint == ONLY_IF_RW)
2048 || (!s->output_section_statement.all_input_readonly
2049 && s->output_section_statement.constraint == ONLY_IF_RO))
2051 s->output_section_statement.constraint = -1;
2056 map_input_to_output_sections (s->output_section_statement.children.head,
2058 &s->output_section_statement);
2060 case lang_output_statement_enum:
2062 case lang_target_statement_enum:
2063 target = s->target_statement.target;
2065 case lang_group_statement_enum:
2066 map_input_to_output_sections (s->group_statement.children.head,
2068 output_section_statement);
2070 case lang_data_statement_enum:
2071 /* Make sure that any sections mentioned in the expression
2073 exp_init_os (s->data_statement.exp);
2075 case lang_fill_statement_enum:
2076 case lang_input_section_enum:
2077 case lang_object_symbols_statement_enum:
2078 case lang_reloc_statement_enum:
2079 case lang_padding_statement_enum:
2080 case lang_input_statement_enum:
2081 if (output_section_statement != NULL
2082 && output_section_statement->bfd_section == NULL)
2083 init_os (output_section_statement);
2085 case lang_assignment_statement_enum:
2086 if (output_section_statement != NULL
2087 && output_section_statement->bfd_section == NULL)
2088 init_os (output_section_statement);
2090 /* Make sure that any sections mentioned in the assignment
2092 exp_init_os (s->assignment_statement.exp);
2094 case lang_afile_asection_pair_statement_enum:
2097 case lang_address_statement_enum:
2098 /* Mark the specified section with the supplied address. */
2100 lang_output_section_statement_type *os =
2101 lang_output_section_statement_lookup
2102 (s->address_statement.section_name);
2104 if (os->bfd_section == NULL)
2106 os->addr_tree = s->address_statement.address;
2113 /* An output section might have been removed after its statement was
2114 added. For example, ldemul_before_allocation can remove dynamic
2115 sections if they turn out to be not needed. Clean them up here. */
2118 strip_excluded_output_sections (void)
2120 lang_statement_union_type *u;
2122 for (u = lang_output_section_statement.head;
2124 u = u->output_section_statement.next)
2126 lang_output_section_statement_type *os;
2129 os = &u->output_section_statement;
2130 if (os->constraint == -1)
2132 s = os->bfd_section;
2133 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2137 os->bfd_section = NULL;
2139 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2142 bfd_section_list_remove (output_bfd, p);
2143 output_bfd->section_count--;
2151 print_output_section_statement
2152 (lang_output_section_statement_type *output_section_statement)
2154 asection *section = output_section_statement->bfd_section;
2157 if (output_section_statement != abs_output_section)
2159 minfo ("\n%s", output_section_statement->name);
2161 if (section != NULL)
2163 print_dot = section->vma;
2165 len = strlen (output_section_statement->name);
2166 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2171 while (len < SECTION_NAME_MAP_LENGTH)
2177 minfo ("0x%V %W", section->vma, section->size);
2179 if (output_section_statement->load_base != NULL)
2183 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2184 "load base", lang_final_phase_enum);
2185 minfo (_(" load address 0x%V"), addr);
2192 print_statement_list (output_section_statement->children.head,
2193 output_section_statement);
2197 print_assignment (lang_assignment_statement_type *assignment,
2198 lang_output_section_statement_type *output_section)
2201 etree_value_type result;
2203 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2206 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2207 lang_final_phase_enum, print_dot, &print_dot);
2213 value = result.value + result.section->bfd_section->vma;
2214 dst = assignment->exp->assign.dst;
2216 minfo ("0x%V", value);
2217 if (dst[0] == '.' && dst[1] == 0)
2230 exp_print_tree (assignment->exp);
2236 print_input_statement (lang_input_statement_type *statm)
2238 if (statm->filename != NULL)
2240 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2244 /* Print all symbols defined in a particular section. This is called
2245 via bfd_link_hash_traverse, or by print_all_symbols. */
2248 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2250 asection *sec = ptr;
2252 if ((hash_entry->type == bfd_link_hash_defined
2253 || hash_entry->type == bfd_link_hash_defweak)
2254 && sec == hash_entry->u.def.section)
2258 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2261 (hash_entry->u.def.value
2262 + hash_entry->u.def.section->output_offset
2263 + hash_entry->u.def.section->output_section->vma));
2265 minfo (" %T\n", hash_entry->root.string);
2272 print_all_symbols (sec)
2275 struct fat_user_section_struct *ud = get_userdata (sec);
2276 struct map_symbol_def *def;
2278 *ud->map_symbol_def_tail = 0;
2279 for (def = ud->map_symbol_def_head; def; def = def->next)
2280 print_one_symbol (def->entry, sec);
2283 /* Print information about an input section to the map file. */
2286 print_input_section (lang_input_section_type *in)
2288 asection *i = in->section;
2289 bfd_size_type size = i->size;
2298 minfo ("%s", i->name);
2300 len = 1 + strlen (i->name);
2301 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2306 while (len < SECTION_NAME_MAP_LENGTH)
2312 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2313 addr = i->output_section->vma + i->output_offset;
2320 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2322 if (size != i->rawsize && i->rawsize != 0)
2324 len = SECTION_NAME_MAP_LENGTH + 3;
2336 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2339 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2341 if (command_line.reduce_memory_overheads)
2342 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2344 print_all_symbols (i);
2346 print_dot = addr + TO_ADDR (size);
2352 print_fill_statement (lang_fill_statement_type *fill)
2356 fputs (" FILL mask 0x", config.map_file);
2357 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2358 fprintf (config.map_file, "%02x", *p);
2359 fputs ("\n", config.map_file);
2363 print_data_statement (lang_data_statement_type *data)
2371 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2374 addr = data->output_vma;
2375 if (data->output_section != NULL)
2376 addr += data->output_section->vma;
2404 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2406 if (data->exp->type.node_class != etree_value)
2409 exp_print_tree (data->exp);
2414 print_dot = addr + TO_ADDR (size);
2417 /* Print an address statement. These are generated by options like
2421 print_address_statement (lang_address_statement_type *address)
2423 minfo (_("Address of section %s set to "), address->section_name);
2424 exp_print_tree (address->address);
2428 /* Print a reloc statement. */
2431 print_reloc_statement (lang_reloc_statement_type *reloc)
2438 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2441 addr = reloc->output_vma;
2442 if (reloc->output_section != NULL)
2443 addr += reloc->output_section->vma;
2445 size = bfd_get_reloc_size (reloc->howto);
2447 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2449 if (reloc->name != NULL)
2450 minfo ("%s+", reloc->name);
2452 minfo ("%s+", reloc->section->name);
2454 exp_print_tree (reloc->addend_exp);
2458 print_dot = addr + TO_ADDR (size);
2462 print_padding_statement (lang_padding_statement_type *s)
2470 len = sizeof " *fill*" - 1;
2471 while (len < SECTION_NAME_MAP_LENGTH)
2477 addr = s->output_offset;
2478 if (s->output_section != NULL)
2479 addr += s->output_section->vma;
2480 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2482 if (s->fill->size != 0)
2486 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2487 fprintf (config.map_file, "%02x", *p);
2492 print_dot = addr + TO_ADDR (s->size);
2496 print_wild_statement (lang_wild_statement_type *w,
2497 lang_output_section_statement_type *os)
2499 struct wildcard_list *sec;
2503 if (w->filenames_sorted)
2505 if (w->filename != NULL)
2506 minfo ("%s", w->filename);
2509 if (w->filenames_sorted)
2513 for (sec = w->section_list; sec; sec = sec->next)
2515 if (sec->spec.sorted)
2517 if (sec->spec.exclude_name_list != NULL)
2520 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2521 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2522 minfo (" %s", tmp->name);
2525 if (sec->spec.name != NULL)
2526 minfo ("%s", sec->spec.name);
2529 if (sec->spec.sorted)
2538 print_statement_list (w->children.head, os);
2541 /* Print a group statement. */
2544 print_group (lang_group_statement_type *s,
2545 lang_output_section_statement_type *os)
2547 fprintf (config.map_file, "START GROUP\n");
2548 print_statement_list (s->children.head, os);
2549 fprintf (config.map_file, "END GROUP\n");
2552 /* Print the list of statements in S.
2553 This can be called for any statement type. */
2556 print_statement_list (lang_statement_union_type *s,
2557 lang_output_section_statement_type *os)
2561 print_statement (s, os);
2566 /* Print the first statement in statement list S.
2567 This can be called for any statement type. */
2570 print_statement (lang_statement_union_type *s,
2571 lang_output_section_statement_type *os)
2573 switch (s->header.type)
2576 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2579 case lang_constructors_statement_enum:
2580 if (constructor_list.head != NULL)
2582 if (constructors_sorted)
2583 minfo (" SORT (CONSTRUCTORS)\n");
2585 minfo (" CONSTRUCTORS\n");
2586 print_statement_list (constructor_list.head, os);
2589 case lang_wild_statement_enum:
2590 print_wild_statement (&s->wild_statement, os);
2592 case lang_address_statement_enum:
2593 print_address_statement (&s->address_statement);
2595 case lang_object_symbols_statement_enum:
2596 minfo (" CREATE_OBJECT_SYMBOLS\n");
2598 case lang_fill_statement_enum:
2599 print_fill_statement (&s->fill_statement);
2601 case lang_data_statement_enum:
2602 print_data_statement (&s->data_statement);
2604 case lang_reloc_statement_enum:
2605 print_reloc_statement (&s->reloc_statement);
2607 case lang_input_section_enum:
2608 print_input_section (&s->input_section);
2610 case lang_padding_statement_enum:
2611 print_padding_statement (&s->padding_statement);
2613 case lang_output_section_statement_enum:
2614 print_output_section_statement (&s->output_section_statement);
2616 case lang_assignment_statement_enum:
2617 print_assignment (&s->assignment_statement, os);
2619 case lang_target_statement_enum:
2620 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2622 case lang_output_statement_enum:
2623 minfo ("OUTPUT(%s", s->output_statement.name);
2624 if (output_target != NULL)
2625 minfo (" %s", output_target);
2628 case lang_input_statement_enum:
2629 print_input_statement (&s->input_statement);
2631 case lang_group_statement_enum:
2632 print_group (&s->group_statement, os);
2634 case lang_afile_asection_pair_statement_enum:
2641 print_statements (void)
2643 print_statement_list (statement_list.head, abs_output_section);
2646 /* Print the first N statements in statement list S to STDERR.
2647 If N == 0, nothing is printed.
2648 If N < 0, the entire list is printed.
2649 Intended to be called from GDB. */
2652 dprint_statement (lang_statement_union_type *s, int n)
2654 FILE *map_save = config.map_file;
2656 config.map_file = stderr;
2659 print_statement_list (s, abs_output_section);
2662 while (s && --n >= 0)
2664 print_statement (s, abs_output_section);
2669 config.map_file = map_save;
2673 insert_pad (lang_statement_union_type **ptr,
2675 unsigned int alignment_needed,
2676 asection *output_section,
2679 static fill_type zero_fill = { 1, { 0 } };
2680 lang_statement_union_type *pad;
2682 pad = ((lang_statement_union_type *)
2683 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2684 if (ptr != &statement_list.head
2685 && pad->header.type == lang_padding_statement_enum
2686 && pad->padding_statement.output_section == output_section)
2688 /* Use the existing pad statement. The above test on output
2689 section is probably redundant, but it doesn't hurt to check. */
2693 /* Make a new padding statement, linked into existing chain. */
2694 pad = stat_alloc (sizeof (lang_padding_statement_type));
2695 pad->header.next = *ptr;
2697 pad->header.type = lang_padding_statement_enum;
2698 pad->padding_statement.output_section = output_section;
2701 pad->padding_statement.fill = fill;
2703 pad->padding_statement.output_offset = dot - output_section->vma;
2704 pad->padding_statement.size = alignment_needed;
2705 output_section->size += alignment_needed;
2708 /* Work out how much this section will move the dot point. */
2712 (lang_statement_union_type **this_ptr,
2713 lang_output_section_statement_type *output_section_statement,
2717 lang_input_section_type *is = &((*this_ptr)->input_section);
2718 asection *i = is->section;
2720 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2722 unsigned int alignment_needed;
2725 /* Align this section first to the input sections requirement,
2726 then to the output section's requirement. If this alignment
2727 is greater than any seen before, then record it too. Perform
2728 the alignment by inserting a magic 'padding' statement. */
2730 if (output_section_statement->subsection_alignment != -1)
2731 i->alignment_power = output_section_statement->subsection_alignment;
2733 o = output_section_statement->bfd_section;
2734 if (o->alignment_power < i->alignment_power)
2735 o->alignment_power = i->alignment_power;
2737 alignment_needed = align_power (dot, i->alignment_power) - dot;
2739 if (alignment_needed != 0)
2741 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2742 dot += alignment_needed;
2745 /* Remember where in the output section this input section goes. */
2747 i->output_offset = dot - o->vma;
2749 /* Mark how big the output section must be to contain this now. */
2750 dot += TO_ADDR (i->size);
2751 o->size = TO_SIZE (dot - o->vma);
2755 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2761 #define IGNORE_SECTION(s) \
2762 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2763 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2764 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2767 /* Check to see if any allocated sections overlap with other allocated
2768 sections. This can happen when the linker script specifically specifies
2769 the output section addresses of the two sections. */
2772 lang_check_section_addresses (void)
2776 /* Scan all sections in the output list. */
2777 for (s = output_bfd->sections; s != NULL; s = s->next)
2781 /* Ignore sections which are not loaded or which have no contents. */
2782 if (IGNORE_SECTION (s))
2785 /* Once we reach section 's' stop our seach. This prevents two
2786 warning messages from being produced, one for 'section A overlaps
2787 section B' and one for 'section B overlaps section A'. */
2788 for (os = output_bfd->sections; os != s; os = os->next)
2795 /* Only consider loadable sections with real contents. */
2796 if (IGNORE_SECTION (os))
2799 /* We must check the sections' LMA addresses not their
2800 VMA addresses because overlay sections can have
2801 overlapping VMAs but they must have distinct LMAs. */
2802 s_start = bfd_section_lma (output_bfd, s);
2803 os_start = bfd_section_lma (output_bfd, os);
2804 s_end = s_start + TO_ADDR (s->size) - 1;
2805 os_end = os_start + TO_ADDR (os->size) - 1;
2807 /* Look for an overlap. */
2808 if ((s_end < os_start) || (s_start > os_end))
2812 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2813 s->name, s_start, s_end, os->name, os_start, os_end);
2815 /* Once we have found one overlap for this section,
2816 stop looking for others. */
2822 /* Make sure the new address is within the region. We explicitly permit the
2823 current address to be at the exact end of the region when the address is
2824 non-zero, in case the region is at the end of addressable memory and the
2825 calculation wraps around. */
2828 os_region_check (lang_output_section_statement_type *os,
2829 lang_memory_region_type *region,
2833 if ((region->current < region->origin
2834 || (region->current - region->origin > region->length))
2835 && ((region->current != region->origin + region->length)
2840 einfo (_("%X%P: address 0x%v of %B section %s"
2841 " is not within region %s\n"),
2843 os->bfd_section->owner,
2844 os->bfd_section->name,
2849 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2851 os->bfd_section->owner,
2852 os->bfd_section->name);
2854 /* Reset the region pointer. */
2855 region->current = region->origin;
2859 /* Set the sizes for all the output sections. */
2862 lang_size_sections_1
2863 (lang_statement_union_type *s,
2864 lang_output_section_statement_type *output_section_statement,
2865 lang_statement_union_type **prev,
2869 bfd_boolean check_regions)
2871 /* Size up the sections from their constituent parts. */
2872 for (; s != NULL; s = s->header.next)
2874 switch (s->header.type)
2876 case lang_output_section_statement_enum:
2879 lang_output_section_statement_type *os;
2881 os = &s->output_section_statement;
2882 if (os->bfd_section == NULL)
2883 /* This section was never actually created. */
2886 /* If this is a COFF shared library section, use the size and
2887 address from the input section. FIXME: This is COFF
2888 specific; it would be cleaner if there were some other way
2889 to do this, but nothing simple comes to mind. */
2890 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2894 if (os->children.head == NULL
2895 || os->children.head->header.next != NULL
2896 || (os->children.head->header.type
2897 != lang_input_section_enum))
2898 einfo (_("%P%X: Internal error on COFF shared library"
2899 " section %s\n"), os->name);
2901 input = os->children.head->input_section.section;
2902 bfd_set_section_vma (os->bfd_section->owner,
2904 bfd_section_vma (input->owner, input));
2905 os->bfd_section->size = input->size;
2909 if (bfd_is_abs_section (os->bfd_section))
2911 /* No matter what happens, an abs section starts at zero. */
2912 ASSERT (os->bfd_section->vma == 0);
2916 if (os->addr_tree == NULL)
2918 /* No address specified for this section, get one
2919 from the region specification. */
2920 if (os->region == NULL
2921 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
2922 && os->region->name[0] == '*'
2923 && strcmp (os->region->name,
2924 DEFAULT_MEMORY_REGION) == 0))
2926 os->region = lang_memory_default (os->bfd_section);
2929 /* If a loadable section is using the default memory
2930 region, and some non default memory regions were
2931 defined, issue an error message. */
2932 if (!IGNORE_SECTION (os->bfd_section)
2933 && ! link_info.relocatable
2935 && strcmp (os->region->name,
2936 DEFAULT_MEMORY_REGION) == 0
2937 && lang_memory_region_list != NULL
2938 && (strcmp (lang_memory_region_list->name,
2939 DEFAULT_MEMORY_REGION) != 0
2940 || lang_memory_region_list->next != NULL))
2942 /* By default this is an error rather than just a
2943 warning because if we allocate the section to the
2944 default memory region we can end up creating an
2945 excessively large binary, or even seg faulting when
2946 attempting to perform a negative seek. See
2947 sources.redhat.com/ml/binutils/2003-04/msg00423.html
2948 for an example of this. This behaviour can be
2949 overridden by the using the --no-check-sections
2951 if (command_line.check_section_addresses)
2952 einfo (_("%P%F: error: no memory region specified"
2953 " for loadable section `%s'\n"),
2954 bfd_get_section_name (output_bfd,
2957 einfo (_("%P: warning: no memory region specified"
2958 " for loadable section `%s'\n"),
2959 bfd_get_section_name (output_bfd,
2963 dot = os->region->current;
2965 if (os->section_alignment == -1)
2970 dot = align_power (dot,
2971 os->bfd_section->alignment_power);
2973 if (dot != olddot && config.warn_section_align)
2974 einfo (_("%P: warning: changing start of section"
2975 " %s by %u bytes\n"),
2976 os->name, (unsigned int) (dot - olddot));
2984 r = exp_fold_tree (os->addr_tree,
2986 lang_allocating_phase_enum,
2991 einfo (_("%F%S: non constant or forward reference"
2992 " address expression for section %s\n"),
2995 dot = r.value + r.section->bfd_section->vma;
2998 /* The section starts here.
2999 First, align to what the section needs. */
3001 if (os->section_alignment != -1)
3002 dot = align_power (dot, os->section_alignment);
3004 bfd_set_section_vma (0, os->bfd_section, dot);
3006 os->bfd_section->output_offset = 0;
3009 lang_size_sections_1 (os->children.head, os, &os->children.head,
3010 os->fill, dot, relax, check_regions);
3012 /* Put the section within the requested block size, or
3013 align at the block boundary. */
3014 after = ((os->bfd_section->vma
3015 + TO_ADDR (os->bfd_section->size)
3016 + os->block_value - 1)
3017 & - (bfd_vma) os->block_value);
3019 if (bfd_is_abs_section (os->bfd_section))
3020 ASSERT (after == os->bfd_section->vma);
3022 os->bfd_section->size
3023 = TO_SIZE (after - os->bfd_section->vma);
3025 dot = os->bfd_section->vma;
3026 /* .tbss sections effectively have zero size. */
3027 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3028 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3029 || link_info.relocatable)
3030 dot += TO_ADDR (os->bfd_section->size);
3034 if (os->update_dot_tree != 0)
3035 exp_fold_tree (os->update_dot_tree, abs_output_section,
3036 lang_allocating_phase_enum, dot, &dot);
3038 /* Update dot in the region ?
3039 We only do this if the section is going to be allocated,
3040 since unallocated sections do not contribute to the region's
3041 overall size in memory.
3043 If the SEC_NEVER_LOAD bit is not set, it will affect the
3044 addresses of sections after it. We have to update
3046 if (os->region != NULL
3047 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3048 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3050 os->region->current = dot;
3053 /* Make sure the new address is within the region. */
3054 os_region_check (os, os->region, os->addr_tree,
3055 os->bfd_section->vma);
3057 /* If there's no load address specified, use the run
3058 region as the load region. */
3059 if (os->lma_region == NULL && os->load_base == NULL)
3060 os->lma_region = os->region;
3062 if (os->lma_region != NULL && os->lma_region != os->region)
3064 /* Set load_base, which will be handled later. */
3065 os->load_base = exp_intop (os->lma_region->current);
3066 os->lma_region->current +=
3067 TO_ADDR (os->bfd_section->size);
3069 os_region_check (os, os->lma_region, NULL,
3070 os->bfd_section->lma);
3076 case lang_constructors_statement_enum:
3077 dot = lang_size_sections_1 (constructor_list.head,
3078 output_section_statement,
3079 &s->wild_statement.children.head,
3080 fill, dot, relax, check_regions);
3083 case lang_data_statement_enum:
3085 unsigned int size = 0;
3087 s->data_statement.output_vma =
3088 dot - output_section_statement->bfd_section->vma;
3089 s->data_statement.output_section =
3090 output_section_statement->bfd_section;
3092 /* We might refer to provided symbols in the expression, and
3093 need to mark them as needed. */
3094 exp_fold_tree (s->data_statement.exp, abs_output_section,
3095 lang_allocating_phase_enum, dot, &dot);
3097 switch (s->data_statement.type)
3115 if (size < TO_SIZE ((unsigned) 1))
3116 size = TO_SIZE ((unsigned) 1);
3117 dot += TO_ADDR (size);
3118 output_section_statement->bfd_section->size += size;
3119 /* The output section gets contents, and then we inspect for
3120 any flags set in the input script which override any ALLOC. */
3121 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3122 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3124 output_section_statement->bfd_section->flags |=
3125 SEC_ALLOC | SEC_LOAD;
3130 case lang_reloc_statement_enum:
3134 s->reloc_statement.output_vma =
3135 dot - output_section_statement->bfd_section->vma;
3136 s->reloc_statement.output_section =
3137 output_section_statement->bfd_section;
3138 size = bfd_get_reloc_size (s->reloc_statement.howto);
3139 dot += TO_ADDR (size);
3140 output_section_statement->bfd_section->size += size;
3144 case lang_wild_statement_enum:
3146 dot = lang_size_sections_1 (s->wild_statement.children.head,
3147 output_section_statement,
3148 &s->wild_statement.children.head,
3149 fill, dot, relax, check_regions);
3153 case lang_object_symbols_statement_enum:
3154 link_info.create_object_symbols_section =
3155 output_section_statement->bfd_section;
3157 case lang_output_statement_enum:
3158 case lang_target_statement_enum:
3160 case lang_input_section_enum:
3164 i = (*prev)->input_section.section;
3169 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3170 einfo (_("%P%F: can't relax section: %E\n"));
3174 dot = size_input_section (prev, output_section_statement,
3175 output_section_statement->fill, dot);
3178 case lang_input_statement_enum:
3180 case lang_fill_statement_enum:
3181 s->fill_statement.output_section =
3182 output_section_statement->bfd_section;
3184 fill = s->fill_statement.fill;
3186 case lang_assignment_statement_enum:
3188 bfd_vma newdot = dot;
3190 exp_fold_tree (s->assignment_statement.exp,
3191 output_section_statement,
3192 lang_allocating_phase_enum,
3198 if (output_section_statement == abs_output_section)
3200 /* If we don't have an output section, then just adjust
3201 the default memory address. */
3202 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3203 FALSE)->current = newdot;
3207 /* Insert a pad after this statement. We can't
3208 put the pad before when relaxing, in case the
3209 assignment references dot. */
3210 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3211 output_section_statement->bfd_section, dot);
3213 /* Don't neuter the pad below when relaxing. */
3217 /* If dot is advanced, this implies that the section should
3218 have space allocated to it, unless the user has explicitly
3219 stated that the section should never be loaded. */
3220 if (!(output_section_statement->flags
3221 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3222 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3229 case lang_padding_statement_enum:
3230 /* If this is the first time lang_size_sections is called,
3231 we won't have any padding statements. If this is the
3232 second or later passes when relaxing, we should allow
3233 padding to shrink. If padding is needed on this pass, it
3234 will be added back in. */
3235 s->padding_statement.size = 0;
3237 /* Make sure output_offset is valid. If relaxation shrinks
3238 the section and this pad isn't needed, it's possible to
3239 have output_offset larger than the final size of the
3240 section. bfd_set_section_contents will complain even for
3241 a pad size of zero. */
3242 s->padding_statement.output_offset
3243 = dot - output_section_statement->bfd_section->vma;
3246 case lang_group_statement_enum:
3247 dot = lang_size_sections_1 (s->group_statement.children.head,
3248 output_section_statement,
3249 &s->group_statement.children.head,
3250 fill, dot, relax, check_regions);
3257 /* We can only get here when relaxing is turned on. */
3258 case lang_address_statement_enum:
3261 prev = &s->header.next;
3268 (lang_statement_union_type *s,
3269 lang_output_section_statement_type *output_section_statement,
3270 lang_statement_union_type **prev,
3274 bfd_boolean check_regions)
3278 /* Callers of exp_fold_tree need to increment this. */
3279 lang_statement_iteration++;
3281 exp_data_seg.phase = exp_dataseg_none;
3282 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3283 dot, relax, check_regions);
3284 if (exp_data_seg.phase == exp_dataseg_end_seen
3285 && link_info.relro && exp_data_seg.relro_end)
3287 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3288 to put exp_data_seg.relro on a (common) page boundary. */
3289 bfd_vma old_base, relro_end;
3291 exp_data_seg.phase = exp_dataseg_relro_adjust;
3292 old_base = exp_data_seg.base;
3293 exp_data_seg.base += (-exp_data_seg.relro_end
3294 & (exp_data_seg.pagesize - 1));
3295 /* Compute the expected PT_GNU_RELRO segment end. */
3296 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
3297 & (exp_data_seg.pagesize - 1);
3298 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3299 dot, relax, check_regions);
3300 if (exp_data_seg.relro_end > relro_end)
3302 /* The alignment of sections between DATA_SEGMENT_ALIGN
3303 and DATA_SEGMENT_RELRO_END caused huge padding to be
3304 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
3306 unsigned int max_alignment_power = 0;
3308 /* Find maximum alignment power of sections between
3309 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
3310 for (sec = output_bfd->sections; sec; sec = sec->next)
3311 if (sec->vma >= exp_data_seg.base
3312 && sec->vma < exp_data_seg.relro_end
3313 && sec->alignment_power > max_alignment_power)
3314 max_alignment_power = sec->alignment_power;
3316 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
3318 if (exp_data_seg.base - (1 << max_alignment_power)
3320 exp_data_seg.base += exp_data_seg.pagesize;
3321 exp_data_seg.base -= (1 << max_alignment_power);
3322 result = lang_size_sections_1 (s, output_section_statement,
3323 prev, fill, dot, relax,
3327 link_info.relro_start = exp_data_seg.base;
3328 link_info.relro_end = exp_data_seg.relro_end;
3330 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3332 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3333 a page could be saved in the data segment. */
3334 bfd_vma first, last;
3336 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3337 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3339 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3340 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3341 && first + last <= exp_data_seg.pagesize)
3343 exp_data_seg.phase = exp_dataseg_adjust;
3344 lang_statement_iteration++;
3345 result = lang_size_sections_1 (s, output_section_statement, prev,
3346 fill, dot, relax, check_regions);
3353 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3356 lang_do_assignments_1
3357 (lang_statement_union_type *s,
3358 lang_output_section_statement_type *output_section_statement,
3362 for (; s != NULL; s = s->header.next)
3364 switch (s->header.type)
3366 case lang_constructors_statement_enum:
3367 dot = lang_do_assignments_1 (constructor_list.head,
3368 output_section_statement,
3373 case lang_output_section_statement_enum:
3375 lang_output_section_statement_type *os;
3377 os = &(s->output_section_statement);
3378 if (os->bfd_section != NULL)
3380 dot = os->bfd_section->vma;
3381 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3382 /* .tbss sections effectively have zero size. */
3383 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3384 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3385 || link_info.relocatable)
3386 dot += TO_ADDR (os->bfd_section->size);
3390 /* If nothing has been placed into the output section then
3391 it won't have a bfd_section. */
3392 if (os->bfd_section)
3394 os->bfd_section->lma
3395 = exp_get_abs_int (os->load_base, 0, "load base",
3396 lang_final_phase_enum);
3401 case lang_wild_statement_enum:
3403 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3404 output_section_statement,
3409 case lang_object_symbols_statement_enum:
3410 case lang_output_statement_enum:
3411 case lang_target_statement_enum:
3413 case lang_common_statement_enum:
3416 case lang_data_statement_enum:
3418 etree_value_type value;
3420 value = exp_fold_tree (s->data_statement.exp,
3422 lang_final_phase_enum, dot, &dot);
3424 einfo (_("%F%P: invalid data statement\n"));
3425 s->data_statement.value
3426 = value.value + value.section->bfd_section->vma;
3430 switch (s->data_statement.type)
3448 if (size < TO_SIZE ((unsigned) 1))
3449 size = TO_SIZE ((unsigned) 1);
3450 dot += TO_ADDR (size);
3454 case lang_reloc_statement_enum:
3456 etree_value_type value;
3458 value = exp_fold_tree (s->reloc_statement.addend_exp,
3460 lang_final_phase_enum, dot, &dot);
3461 s->reloc_statement.addend_value = value.value;
3463 einfo (_("%F%P: invalid reloc statement\n"));
3465 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3468 case lang_input_section_enum:
3470 asection *in = s->input_section.section;
3472 if ((in->flags & SEC_EXCLUDE) == 0)
3473 dot += TO_ADDR (in->size);
3477 case lang_input_statement_enum:
3479 case lang_fill_statement_enum:
3480 fill = s->fill_statement.fill;
3482 case lang_assignment_statement_enum:
3484 exp_fold_tree (s->assignment_statement.exp,
3485 output_section_statement,
3486 lang_final_phase_enum,
3492 case lang_padding_statement_enum:
3493 dot += TO_ADDR (s->padding_statement.size);
3496 case lang_group_statement_enum:
3497 dot = lang_do_assignments_1 (s->group_statement.children.head,
3498 output_section_statement,
3506 case lang_address_statement_enum:
3516 (lang_statement_union_type *s,
3517 lang_output_section_statement_type *output_section_statement,
3521 /* Callers of exp_fold_tree need to increment this. */
3522 lang_statement_iteration++;
3523 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3526 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3527 operator .startof. (section_name), it produces an undefined symbol
3528 .startof.section_name. Similarly, when it sees
3529 .sizeof. (section_name), it produces an undefined symbol
3530 .sizeof.section_name. For all the output sections, we look for
3531 such symbols, and set them to the correct value. */
3534 lang_set_startof (void)
3538 if (link_info.relocatable)
3541 for (s = output_bfd->sections; s != NULL; s = s->next)
3543 const char *secname;
3545 struct bfd_link_hash_entry *h;
3547 secname = bfd_get_section_name (output_bfd, s);
3548 buf = xmalloc (10 + strlen (secname));
3550 sprintf (buf, ".startof.%s", secname);
3551 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3552 if (h != NULL && h->type == bfd_link_hash_undefined)
3554 h->type = bfd_link_hash_defined;
3555 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3556 h->u.def.section = bfd_abs_section_ptr;
3559 sprintf (buf, ".sizeof.%s", secname);
3560 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3561 if (h != NULL && h->type == bfd_link_hash_undefined)
3563 h->type = bfd_link_hash_defined;
3564 h->u.def.value = TO_ADDR (s->size);
3565 h->u.def.section = bfd_abs_section_ptr;
3575 struct bfd_link_hash_entry *h;
3578 if (link_info.relocatable || link_info.shared)
3583 if (entry_symbol.name == NULL)
3585 /* No entry has been specified. Look for start, but don't warn
3586 if we don't find it. */
3587 entry_symbol.name = "start";
3591 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3592 FALSE, FALSE, TRUE);
3594 && (h->type == bfd_link_hash_defined
3595 || h->type == bfd_link_hash_defweak)
3596 && h->u.def.section->output_section != NULL)
3600 val = (h->u.def.value
3601 + bfd_get_section_vma (output_bfd,
3602 h->u.def.section->output_section)
3603 + h->u.def.section->output_offset);
3604 if (! bfd_set_start_address (output_bfd, val))
3605 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3612 /* We couldn't find the entry symbol. Try parsing it as a
3614 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3617 if (! bfd_set_start_address (output_bfd, val))
3618 einfo (_("%P%F: can't set start address\n"));
3624 /* Can't find the entry symbol, and it's not a number. Use
3625 the first address in the text section. */
3626 ts = bfd_get_section_by_name (output_bfd, entry_section);
3630 einfo (_("%P: warning: cannot find entry symbol %s;"
3631 " defaulting to %V\n"),
3633 bfd_get_section_vma (output_bfd, ts));
3634 if (! bfd_set_start_address (output_bfd,
3635 bfd_get_section_vma (output_bfd,
3637 einfo (_("%P%F: can't set start address\n"));
3642 einfo (_("%P: warning: cannot find entry symbol %s;"
3643 " not setting start address\n"),
3649 /* Don't bfd_hash_table_free (&lang_definedness_table);
3650 map file output may result in a call of lang_track_definedness. */
3653 /* This is a small function used when we want to ignore errors from
3657 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3659 /* Don't do anything. */
3662 /* Check that the architecture of all the input files is compatible
3663 with the output file. Also call the backend to let it do any
3664 other checking that is needed. */
3669 lang_statement_union_type *file;
3671 const bfd_arch_info_type *compatible;
3673 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3675 input_bfd = file->input_statement.the_bfd;
3677 = bfd_arch_get_compatible (input_bfd, output_bfd,
3678 command_line.accept_unknown_input_arch);
3680 /* In general it is not possible to perform a relocatable
3681 link between differing object formats when the input
3682 file has relocations, because the relocations in the
3683 input format may not have equivalent representations in
3684 the output format (and besides BFD does not translate
3685 relocs for other link purposes than a final link). */
3686 if ((link_info.relocatable || link_info.emitrelocations)
3687 && (compatible == NULL
3688 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3689 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3691 einfo (_("%P%F: Relocatable linking with relocations from"
3692 " format %s (%B) to format %s (%B) is not supported\n"),
3693 bfd_get_target (input_bfd), input_bfd,
3694 bfd_get_target (output_bfd), output_bfd);
3695 /* einfo with %F exits. */
3698 if (compatible == NULL)
3700 if (command_line.warn_mismatch)
3701 einfo (_("%P: warning: %s architecture of input file `%B'"
3702 " is incompatible with %s output\n"),
3703 bfd_printable_name (input_bfd), input_bfd,
3704 bfd_printable_name (output_bfd));
3706 else if (bfd_count_sections (input_bfd))
3708 /* If the input bfd has no contents, it shouldn't set the
3709 private data of the output bfd. */
3711 bfd_error_handler_type pfn = NULL;
3713 /* If we aren't supposed to warn about mismatched input
3714 files, temporarily set the BFD error handler to a
3715 function which will do nothing. We still want to call
3716 bfd_merge_private_bfd_data, since it may set up
3717 information which is needed in the output file. */
3718 if (! command_line.warn_mismatch)
3719 pfn = bfd_set_error_handler (ignore_bfd_errors);
3720 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3722 if (command_line.warn_mismatch)
3723 einfo (_("%P%X: failed to merge target specific data"
3724 " of file %B\n"), input_bfd);
3726 if (! command_line.warn_mismatch)
3727 bfd_set_error_handler (pfn);
3732 /* Look through all the global common symbols and attach them to the
3733 correct section. The -sort-common command line switch may be used
3734 to roughly sort the entries by size. */
3739 if (command_line.inhibit_common_definition)
3741 if (link_info.relocatable
3742 && ! command_line.force_common_definition)
3745 if (! config.sort_common)
3746 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3751 for (power = 4; power >= 0; power--)
3752 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3756 /* Place one common symbol in the correct section. */
3759 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3761 unsigned int power_of_two;
3765 if (h->type != bfd_link_hash_common)
3769 power_of_two = h->u.c.p->alignment_power;
3771 if (config.sort_common
3772 && power_of_two < (unsigned int) *(int *) info)
3775 section = h->u.c.p->section;
3777 /* Increase the size of the section to align the common sym. */
3778 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3779 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3781 /* Adjust the alignment if necessary. */
3782 if (power_of_two > section->alignment_power)
3783 section->alignment_power = power_of_two;
3785 /* Change the symbol from common to defined. */
3786 h->type = bfd_link_hash_defined;
3787 h->u.def.section = section;
3788 h->u.def.value = section->size;
3790 /* Increase the size of the section. */
3791 section->size += size;
3793 /* Make sure the section is allocated in memory, and make sure that
3794 it is no longer a common section. */
3795 section->flags |= SEC_ALLOC;
3796 section->flags &= ~SEC_IS_COMMON;
3798 if (config.map_file != NULL)
3800 static bfd_boolean header_printed;
3805 if (! header_printed)
3807 minfo (_("\nAllocating common symbols\n"));
3808 minfo (_("Common symbol size file\n\n"));
3809 header_printed = TRUE;
3812 name = demangle (h->root.string);
3814 len = strlen (name);
3829 if (size <= 0xffffffff)
3830 sprintf (buf, "%lx", (unsigned long) size);
3832 sprintf_vma (buf, size);
3842 minfo ("%B\n", section->owner);
3848 /* Run through the input files and ensure that every input section has
3849 somewhere to go. If one is found without a destination then create
3850 an input request and place it into the statement tree. */
3853 lang_place_orphans (void)
3855 LANG_FOR_EACH_INPUT_STATEMENT (file)
3859 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3861 if (s->output_section == NULL)
3863 /* This section of the file is not attached, root
3864 around for a sensible place for it to go. */
3866 if (file->just_syms_flag)
3869 if ((s->flags & SEC_EXCLUDE) != 0)
3870 s->output_section = bfd_abs_section_ptr;
3871 else if (strcmp (s->name, "COMMON") == 0)
3873 /* This is a lonely common section which must have
3874 come from an archive. We attach to the section
3875 with the wildcard. */
3876 if (! link_info.relocatable
3877 || command_line.force_common_definition)
3879 if (default_common_section == NULL)
3882 /* This message happens when using the
3883 svr3.ifile linker script, so I have
3885 info_msg (_("%P: no [COMMON] command,"
3886 " defaulting to .bss\n"));
3888 default_common_section =
3889 lang_output_section_statement_lookup (".bss");
3892 lang_add_section (&default_common_section->children, s,
3893 default_common_section, file);
3896 else if (ldemul_place_orphan (file, s))
3900 lang_output_section_statement_type *os;
3902 os = lang_output_section_statement_lookup (s->name);
3903 lang_add_section (&os->children, s, os, file);
3911 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3913 flagword *ptr_flags;
3915 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3921 *ptr_flags |= SEC_ALLOC;
3925 *ptr_flags |= SEC_READONLY;
3929 *ptr_flags |= SEC_DATA;
3933 *ptr_flags |= SEC_CODE;
3938 *ptr_flags |= SEC_LOAD;
3942 einfo (_("%P%F: invalid syntax in flags\n"));
3949 /* Call a function on each input file. This function will be called
3950 on an archive, but not on the elements. */
3953 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3955 lang_input_statement_type *f;
3957 for (f = (lang_input_statement_type *) input_file_chain.head;
3959 f = (lang_input_statement_type *) f->next_real_file)
3963 /* Call a function on each file. The function will be called on all
3964 the elements of an archive which are included in the link, but will
3965 not be called on the archive file itself. */
3968 lang_for_each_file (void (*func) (lang_input_statement_type *))
3970 LANG_FOR_EACH_INPUT_STATEMENT (f)
3977 ldlang_add_file (lang_input_statement_type *entry)
3981 lang_statement_append (&file_chain,
3982 (lang_statement_union_type *) entry,
3985 /* The BFD linker needs to have a list of all input BFDs involved in
3987 ASSERT (entry->the_bfd->link_next == NULL);
3988 ASSERT (entry->the_bfd != output_bfd);
3989 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3991 *pp = entry->the_bfd;
3992 entry->the_bfd->usrdata = entry;
3993 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3995 /* Look through the sections and check for any which should not be
3996 included in the link. We need to do this now, so that we can
3997 notice when the backend linker tries to report multiple
3998 definition errors for symbols which are in sections we aren't
3999 going to link. FIXME: It might be better to entirely ignore
4000 symbols which are defined in sections which are going to be
4001 discarded. This would require modifying the backend linker for
4002 each backend which might set the SEC_LINK_ONCE flag. If we do
4003 this, we should probably handle SEC_EXCLUDE in the same way. */
4005 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4009 lang_add_output (const char *name, int from_script)
4011 /* Make -o on command line override OUTPUT in script. */
4012 if (!had_output_filename || !from_script)
4014 output_filename = name;
4015 had_output_filename = TRUE;
4019 static lang_output_section_statement_type *current_section;
4030 for (l = 0; l < 32; l++)
4032 if (i >= (unsigned int) x)
4040 lang_output_section_statement_type *
4041 lang_enter_output_section_statement (const char *output_section_statement_name,
4042 etree_type *address_exp,
4043 enum section_type sectype,
4045 etree_type *subalign,
4049 lang_output_section_statement_type *os;
4053 lang_output_section_statement_lookup_1 (output_section_statement_name,
4056 /* Add this statement to tree. */
4058 add_statement (lang_output_section_statement_enum,
4059 output_section_statement);
4061 /* Make next things chain into subchain of this. */
4063 if (os->addr_tree == NULL)
4065 os->addr_tree = address_exp;
4067 os->sectype = sectype;
4068 if (sectype != noload_section)
4069 os->flags = SEC_NO_FLAGS;
4071 os->flags = SEC_NEVER_LOAD;
4072 os->block_value = 1;
4073 stat_ptr = &os->children;
4075 os->subsection_alignment =
4076 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4077 os->section_alignment =
4078 topower (exp_get_value_int (align, -1, "section alignment", 0));
4080 os->load_base = ebase;
4087 lang_output_statement_type *new =
4088 new_stat (lang_output_statement, stat_ptr);
4090 new->name = output_filename;
4093 /* Reset the current counters in the regions. */
4096 lang_reset_memory_regions (void)
4098 lang_memory_region_type *p = lang_memory_region_list;
4101 for (p = lang_memory_region_list; p != NULL; p = p->next)
4103 p->old_length = (bfd_size_type) (p->current - p->origin);
4104 p->current = p->origin;
4107 for (o = output_bfd->sections; o != NULL; o = o->next)
4109 /* Save the last size for possible use by bfd_relax_section. */
4110 o->rawsize = o->size;
4115 /* Worker for lang_gc_sections_1. */
4118 gc_section_callback (lang_wild_statement_type *ptr,
4119 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4121 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4122 void *data ATTRIBUTE_UNUSED)
4124 /* If the wild pattern was marked KEEP, the member sections
4125 should be as well. */
4126 if (ptr->keep_sections)
4127 section->flags |= SEC_KEEP;
4130 /* Iterate over sections marking them against GC. */
4133 lang_gc_sections_1 (lang_statement_union_type *s)
4135 for (; s != NULL; s = s->header.next)
4137 switch (s->header.type)
4139 case lang_wild_statement_enum:
4140 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4142 case lang_constructors_statement_enum:
4143 lang_gc_sections_1 (constructor_list.head);
4145 case lang_output_section_statement_enum:
4146 lang_gc_sections_1 (s->output_section_statement.children.head);
4148 case lang_group_statement_enum:
4149 lang_gc_sections_1 (s->group_statement.children.head);
4158 lang_gc_sections (void)
4160 struct bfd_link_hash_entry *h;
4161 ldlang_undef_chain_list_type *ulist;
4163 /* Keep all sections so marked in the link script. */
4165 lang_gc_sections_1 (statement_list.head);
4167 /* Keep all sections containing symbols undefined on the command-line,
4168 and the section containing the entry symbol. */
4170 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4172 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4173 FALSE, FALSE, FALSE);
4176 && (h->type == bfd_link_hash_defined
4177 || h->type == bfd_link_hash_defweak)
4178 && ! bfd_is_abs_section (h->u.def.section))
4180 h->u.def.section->flags |= SEC_KEEP;
4184 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4185 the special case of debug info. (See bfd/stabs.c)
4186 Twiddle the flag here, to simplify later linker code. */
4187 if (link_info.relocatable)
4189 LANG_FOR_EACH_INPUT_STATEMENT (f)
4192 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4193 if ((sec->flags & SEC_DEBUGGING) == 0)
4194 sec->flags &= ~SEC_EXCLUDE;
4198 if (command_line.gc_sections)
4199 bfd_gc_sections (output_bfd, &link_info);
4205 lang_reasonable_defaults ();
4206 current_target = default_target;
4208 /* Open the output file. */
4209 lang_for_each_statement (ldlang_open_output);
4212 ldemul_create_output_section_statements ();
4214 /* Add to the hash table all undefineds on the command line. */
4215 lang_place_undefineds ();
4217 if (!bfd_section_already_linked_table_init ())
4218 einfo (_("%P%F: Failed to create hash table\n"));
4220 /* Create a bfd for each input file. */
4221 current_target = default_target;
4222 open_input_bfds (statement_list.head, FALSE);
4224 link_info.gc_sym_list = &entry_symbol;
4225 if (entry_symbol.name == NULL)
4226 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4228 ldemul_after_open ();
4230 bfd_section_already_linked_table_free ();
4232 /* Make sure that we're not mixing architectures. We call this
4233 after all the input files have been opened, but before we do any
4234 other processing, so that any operations merge_private_bfd_data
4235 does on the output file will be known during the rest of the
4239 /* Handle .exports instead of a version script if we're told to do so. */
4240 if (command_line.version_exports_section)
4241 lang_do_version_exports_section ();
4243 /* Build all sets based on the information gathered from the input
4245 ldctor_build_sets ();
4247 /* Remove unreferenced sections if asked to. */
4248 lang_gc_sections ();
4250 /* Size up the common data. */
4253 /* Run through the contours of the script and attach input sections
4254 to the correct output sections. */
4255 map_input_to_output_sections (statement_list.head, NULL, NULL);
4257 /* Find any sections not attached explicitly and handle them. */
4258 lang_place_orphans ();
4260 if (! link_info.relocatable)
4264 /* Merge SEC_MERGE sections. This has to be done after GC of
4265 sections, so that GCed sections are not merged, but before
4266 assigning dynamic symbols, since removing whole input sections
4268 bfd_merge_sections (output_bfd, &link_info);
4270 /* Look for a text section and set the readonly attribute in it. */
4271 found = bfd_get_section_by_name (output_bfd, ".text");
4275 if (config.text_read_only)
4276 found->flags |= SEC_READONLY;
4278 found->flags &= ~SEC_READONLY;
4282 /* Do anything special before sizing sections. This is where ELF
4283 and other back-ends size dynamic sections. */
4284 ldemul_before_allocation ();
4286 if (!link_info.relocatable)
4287 strip_excluded_output_sections ();
4289 /* We must record the program headers before we try to fix the
4290 section positions, since they will affect SIZEOF_HEADERS. */
4291 lang_record_phdrs ();
4293 /* Size up the sections. */
4294 lang_size_sections (statement_list.head, abs_output_section,
4295 &statement_list.head, 0, 0, NULL,
4296 command_line.relax ? FALSE : TRUE);
4298 /* Now run around and relax if we can. */
4299 if (command_line.relax)
4301 /* Keep relaxing until bfd_relax_section gives up. */
4302 bfd_boolean relax_again;
4306 relax_again = FALSE;
4308 /* Note: pe-dll.c does something like this also. If you find
4309 you need to change this code, you probably need to change
4310 pe-dll.c also. DJ */
4312 /* Do all the assignments with our current guesses as to
4314 lang_do_assignments (statement_list.head, abs_output_section,
4317 /* We must do this after lang_do_assignments, because it uses
4319 lang_reset_memory_regions ();
4321 /* Perform another relax pass - this time we know where the
4322 globals are, so can make a better guess. */
4323 lang_size_sections (statement_list.head, abs_output_section,
4324 &statement_list.head, 0, 0, &relax_again, FALSE);
4326 /* If the normal relax is done and the relax finalize pass
4327 is not performed yet, we perform another relax pass. */
4328 if (!relax_again && link_info.need_relax_finalize)
4330 link_info.need_relax_finalize = FALSE;
4334 while (relax_again);
4336 /* Final extra sizing to report errors. */
4337 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4338 lang_reset_memory_regions ();
4339 lang_size_sections (statement_list.head, abs_output_section,
4340 &statement_list.head, 0, 0, NULL, TRUE);
4343 /* See if anything special should be done now we know how big
4345 ldemul_after_allocation ();
4347 /* Fix any .startof. or .sizeof. symbols. */
4348 lang_set_startof ();
4350 /* Do all the assignments, now that we know the final resting places
4351 of all the symbols. */
4353 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4355 /* Make sure that the section addresses make sense. */
4356 if (! link_info.relocatable
4357 && command_line.check_section_addresses)
4358 lang_check_section_addresses ();
4366 /* EXPORTED TO YACC */
4369 lang_add_wild (struct wildcard_spec *filespec,
4370 struct wildcard_list *section_list,
4371 bfd_boolean keep_sections)
4373 struct wildcard_list *curr, *next;
4374 lang_wild_statement_type *new;
4376 /* Reverse the list as the parser puts it back to front. */
4377 for (curr = section_list, section_list = NULL;
4379 section_list = curr, curr = next)
4381 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4382 placed_commons = TRUE;
4385 curr->next = section_list;
4388 if (filespec != NULL && filespec->name != NULL)
4390 if (strcmp (filespec->name, "*") == 0)
4391 filespec->name = NULL;
4392 else if (! wildcardp (filespec->name))
4393 lang_has_input_file = TRUE;
4396 new = new_stat (lang_wild_statement, stat_ptr);
4397 new->filename = NULL;
4398 new->filenames_sorted = FALSE;
4399 if (filespec != NULL)
4401 new->filename = filespec->name;
4402 new->filenames_sorted = filespec->sorted;
4404 new->section_list = section_list;
4405 new->keep_sections = keep_sections;
4406 lang_list_init (&new->children);
4410 lang_section_start (const char *name, etree_type *address)
4412 lang_address_statement_type *ad;
4414 ad = new_stat (lang_address_statement, stat_ptr);
4415 ad->section_name = name;
4416 ad->address = address;
4419 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4420 because of a -e argument on the command line, or zero if this is
4421 called by ENTRY in a linker script. Command line arguments take
4425 lang_add_entry (const char *name, bfd_boolean cmdline)
4427 if (entry_symbol.name == NULL
4429 || ! entry_from_cmdline)
4431 entry_symbol.name = name;
4432 entry_from_cmdline = cmdline;
4437 lang_add_target (const char *name)
4439 lang_target_statement_type *new = new_stat (lang_target_statement,
4447 lang_add_map (const char *name)
4454 map_option_f = TRUE;
4462 lang_add_fill (fill_type *fill)
4464 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4471 lang_add_data (int type, union etree_union *exp)
4474 lang_data_statement_type *new = new_stat (lang_data_statement,
4482 /* Create a new reloc statement. RELOC is the BFD relocation type to
4483 generate. HOWTO is the corresponding howto structure (we could
4484 look this up, but the caller has already done so). SECTION is the
4485 section to generate a reloc against, or NAME is the name of the
4486 symbol to generate a reloc against. Exactly one of SECTION and
4487 NAME must be NULL. ADDEND is an expression for the addend. */
4490 lang_add_reloc (bfd_reloc_code_real_type reloc,
4491 reloc_howto_type *howto,
4494 union etree_union *addend)
4496 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4500 p->section = section;
4502 p->addend_exp = addend;
4504 p->addend_value = 0;
4505 p->output_section = NULL;
4509 lang_assignment_statement_type *
4510 lang_add_assignment (etree_type *exp)
4512 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4520 lang_add_attribute (enum statement_enum attribute)
4522 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4526 lang_startup (const char *name)
4528 if (startup_file != NULL)
4530 einfo (_("%P%Fmultiple STARTUP files\n"));
4532 first_file->filename = name;
4533 first_file->local_sym_name = name;
4534 first_file->real = TRUE;
4536 startup_file = name;
4540 lang_float (bfd_boolean maybe)
4542 lang_float_flag = maybe;
4546 /* Work out the load- and run-time regions from a script statement, and
4547 store them in *LMA_REGION and *REGION respectively.
4549 MEMSPEC is the name of the run-time region, or the value of
4550 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4551 LMA_MEMSPEC is the name of the load-time region, or null if the
4552 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4553 had an explicit load address.
4555 It is an error to specify both a load region and a load address. */
4558 lang_get_regions (lang_memory_region_type **region,
4559 lang_memory_region_type **lma_region,
4560 const char *memspec,
4561 const char *lma_memspec,
4562 bfd_boolean have_lma,
4563 bfd_boolean have_vma)
4565 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4567 /* If no runtime region or VMA has been specified, but the load region
4568 has been specified, then use the load region for the runtime region
4570 if (lma_memspec != NULL
4572 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4573 *region = *lma_region;
4575 *region = lang_memory_region_lookup (memspec, FALSE);
4577 if (have_lma && lma_memspec != 0)
4578 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4582 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4583 lang_output_section_phdr_list *phdrs,
4584 const char *lma_memspec)
4586 lang_get_regions (¤t_section->region,
4587 ¤t_section->lma_region,
4588 memspec, lma_memspec,
4589 current_section->load_base != NULL,
4590 current_section->addr_tree != NULL);
4591 current_section->fill = fill;
4592 current_section->phdrs = phdrs;
4593 stat_ptr = &statement_list;
4596 /* Create an absolute symbol with the given name with the value of the
4597 address of first byte of the section named.
4599 If the symbol already exists, then do nothing. */
4602 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4604 struct bfd_link_hash_entry *h;
4606 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4608 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4610 if (h->type == bfd_link_hash_new
4611 || h->type == bfd_link_hash_undefined)
4615 h->type = bfd_link_hash_defined;
4617 sec = bfd_get_section_by_name (output_bfd, secname);
4621 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4623 h->u.def.section = bfd_abs_section_ptr;
4627 /* Create an absolute symbol with the given name with the value of the
4628 address of the first byte after the end of the section named.
4630 If the symbol already exists, then do nothing. */
4633 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4635 struct bfd_link_hash_entry *h;
4637 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4639 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4641 if (h->type == bfd_link_hash_new
4642 || h->type == bfd_link_hash_undefined)
4646 h->type = bfd_link_hash_defined;
4648 sec = bfd_get_section_by_name (output_bfd, secname);
4652 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4653 + TO_ADDR (sec->size));
4655 h->u.def.section = bfd_abs_section_ptr;
4660 lang_statement_append (lang_statement_list_type *list,
4661 lang_statement_union_type *element,
4662 lang_statement_union_type **field)
4664 *(list->tail) = element;
4668 /* Set the output format type. -oformat overrides scripts. */
4671 lang_add_output_format (const char *format,
4676 if (output_target == NULL || !from_script)
4678 if (command_line.endian == ENDIAN_BIG
4681 else if (command_line.endian == ENDIAN_LITTLE
4685 output_target = format;
4689 /* Enter a group. This creates a new lang_group_statement, and sets
4690 stat_ptr to build new statements within the group. */
4693 lang_enter_group (void)
4695 lang_group_statement_type *g;
4697 g = new_stat (lang_group_statement, stat_ptr);
4698 lang_list_init (&g->children);
4699 stat_ptr = &g->children;
4702 /* Leave a group. This just resets stat_ptr to start writing to the
4703 regular list of statements again. Note that this will not work if
4704 groups can occur inside anything else which can adjust stat_ptr,
4705 but currently they can't. */
4708 lang_leave_group (void)
4710 stat_ptr = &statement_list;
4713 /* Add a new program header. This is called for each entry in a PHDRS
4714 command in a linker script. */
4717 lang_new_phdr (const char *name,
4719 bfd_boolean filehdr,
4724 struct lang_phdr *n, **pp;
4726 n = stat_alloc (sizeof (struct lang_phdr));
4729 n->type = exp_get_value_int (type, 0, "program header type",
4730 lang_final_phase_enum);
4731 n->filehdr = filehdr;
4736 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4741 /* Record the program header information in the output BFD. FIXME: We
4742 should not be calling an ELF specific function here. */
4745 lang_record_phdrs (void)
4749 lang_output_section_phdr_list *last;
4750 struct lang_phdr *l;
4751 lang_statement_union_type *u;
4754 secs = xmalloc (alc * sizeof (asection *));
4756 for (l = lang_phdr_list; l != NULL; l = l->next)
4763 for (u = lang_output_section_statement.head;
4765 u = u->output_section_statement.next)
4767 lang_output_section_statement_type *os;
4768 lang_output_section_phdr_list *pl;
4770 os = &u->output_section_statement;
4771 if (os->constraint == -1)
4779 if (os->sectype == noload_section
4780 || os->bfd_section == NULL
4781 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4786 if (os->bfd_section == NULL)
4789 for (; pl != NULL; pl = pl->next)
4791 if (strcmp (pl->name, l->name) == 0)
4796 secs = xrealloc (secs, alc * sizeof (asection *));
4798 secs[c] = os->bfd_section;
4805 if (l->flags == NULL)
4808 flags = exp_get_vma (l->flags, 0, "phdr flags",
4809 lang_final_phase_enum);
4814 at = exp_get_vma (l->at, 0, "phdr load address",
4815 lang_final_phase_enum);
4817 if (! bfd_record_phdr (output_bfd, l->type,
4818 l->flags != NULL, flags, l->at != NULL,
4819 at, l->filehdr, l->phdrs, c, secs))
4820 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4825 /* Make sure all the phdr assignments succeeded. */
4826 for (u = lang_output_section_statement.head;
4828 u = u->output_section_statement.next)
4830 lang_output_section_phdr_list *pl;
4832 if (u->output_section_statement.constraint == -1
4833 || u->output_section_statement.bfd_section == NULL)
4836 for (pl = u->output_section_statement.phdrs;
4839 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4840 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4841 u->output_section_statement.name, pl->name);
4845 /* Record a list of sections which may not be cross referenced. */
4848 lang_add_nocrossref (lang_nocrossref_type *l)
4850 struct lang_nocrossrefs *n;
4852 n = xmalloc (sizeof *n);
4853 n->next = nocrossref_list;
4855 nocrossref_list = n;
4857 /* Set notice_all so that we get informed about all symbols. */
4858 link_info.notice_all = TRUE;
4861 /* Overlay handling. We handle overlays with some static variables. */
4863 /* The overlay virtual address. */
4864 static etree_type *overlay_vma;
4865 /* And subsection alignment. */
4866 static etree_type *overlay_subalign;
4868 /* An expression for the maximum section size seen so far. */
4869 static etree_type *overlay_max;
4871 /* A list of all the sections in this overlay. */
4873 struct overlay_list {
4874 struct overlay_list *next;
4875 lang_output_section_statement_type *os;
4878 static struct overlay_list *overlay_list;
4880 /* Start handling an overlay. */
4883 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4885 /* The grammar should prevent nested overlays from occurring. */
4886 ASSERT (overlay_vma == NULL
4887 && overlay_subalign == NULL
4888 && overlay_max == NULL);
4890 overlay_vma = vma_expr;
4891 overlay_subalign = subalign;
4894 /* Start a section in an overlay. We handle this by calling
4895 lang_enter_output_section_statement with the correct VMA.
4896 lang_leave_overlay sets up the LMA and memory regions. */
4899 lang_enter_overlay_section (const char *name)
4901 struct overlay_list *n;
4904 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4905 0, overlay_subalign, 0, 0);
4907 /* If this is the first section, then base the VMA of future
4908 sections on this one. This will work correctly even if `.' is
4909 used in the addresses. */
4910 if (overlay_list == NULL)
4911 overlay_vma = exp_nameop (ADDR, name);
4913 /* Remember the section. */
4914 n = xmalloc (sizeof *n);
4915 n->os = current_section;
4916 n->next = overlay_list;
4919 size = exp_nameop (SIZEOF, name);
4921 /* Arrange to work out the maximum section end address. */
4922 if (overlay_max == NULL)
4925 overlay_max = exp_binop (MAX_K, overlay_max, size);
4928 /* Finish a section in an overlay. There isn't any special to do
4932 lang_leave_overlay_section (fill_type *fill,
4933 lang_output_section_phdr_list *phdrs)
4940 name = current_section->name;
4942 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4943 region and that no load-time region has been specified. It doesn't
4944 really matter what we say here, since lang_leave_overlay will
4946 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
4948 /* Define the magic symbols. */
4950 clean = xmalloc (strlen (name) + 1);
4952 for (s1 = name; *s1 != '\0'; s1++)
4953 if (ISALNUM (*s1) || *s1 == '_')
4957 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4958 sprintf (buf, "__load_start_%s", clean);
4959 lang_add_assignment (exp_assop ('=', buf,
4960 exp_nameop (LOADADDR, name)));
4962 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4963 sprintf (buf, "__load_stop_%s", clean);
4964 lang_add_assignment (exp_assop ('=', buf,
4966 exp_nameop (LOADADDR, name),
4967 exp_nameop (SIZEOF, name))));
4972 /* Finish an overlay. If there are any overlay wide settings, this
4973 looks through all the sections in the overlay and sets them. */
4976 lang_leave_overlay (etree_type *lma_expr,
4979 const char *memspec,
4980 lang_output_section_phdr_list *phdrs,
4981 const char *lma_memspec)
4983 lang_memory_region_type *region;
4984 lang_memory_region_type *lma_region;
4985 struct overlay_list *l;
4986 lang_nocrossref_type *nocrossref;
4988 lang_get_regions (®ion, &lma_region,
4989 memspec, lma_memspec,
4990 lma_expr != NULL, FALSE);
4994 /* After setting the size of the last section, set '.' to end of the
4996 if (overlay_list != NULL)
4997 overlay_list->os->update_dot_tree
4998 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5003 struct overlay_list *next;
5005 if (fill != NULL && l->os->fill == NULL)
5008 l->os->region = region;
5009 l->os->lma_region = lma_region;
5011 /* The first section has the load address specified in the
5012 OVERLAY statement. The rest are worked out from that.
5013 The base address is not needed (and should be null) if
5014 an LMA region was specified. */
5016 l->os->load_base = lma_expr;
5017 else if (lma_region == 0)
5018 l->os->load_base = exp_binop ('+',
5019 exp_nameop (LOADADDR, l->next->os->name),
5020 exp_nameop (SIZEOF, l->next->os->name));
5022 if (phdrs != NULL && l->os->phdrs == NULL)
5023 l->os->phdrs = phdrs;
5027 lang_nocrossref_type *nc;
5029 nc = xmalloc (sizeof *nc);
5030 nc->name = l->os->name;
5031 nc->next = nocrossref;
5040 if (nocrossref != NULL)
5041 lang_add_nocrossref (nocrossref);
5044 overlay_list = NULL;
5048 /* Version handling. This is only useful for ELF. */
5050 /* This global variable holds the version tree that we build. */
5052 struct bfd_elf_version_tree *lang_elf_version_info;
5054 /* If PREV is NULL, return first version pattern matching particular symbol.
5055 If PREV is non-NULL, return first version pattern matching particular
5056 symbol after PREV (previously returned by lang_vers_match). */
5058 static struct bfd_elf_version_expr *
5059 lang_vers_match (struct bfd_elf_version_expr_head *head,
5060 struct bfd_elf_version_expr *prev,
5063 const char *cxx_sym = sym;
5064 const char *java_sym = sym;
5065 struct bfd_elf_version_expr *expr = NULL;
5067 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5069 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5073 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5075 java_sym = cplus_demangle (sym, DMGL_JAVA);
5080 if (head->htab && (prev == NULL || prev->symbol))
5082 struct bfd_elf_version_expr e;
5084 switch (prev ? prev->mask : 0)
5087 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5090 expr = htab_find (head->htab, &e);
5091 while (expr && strcmp (expr->symbol, sym) == 0)
5092 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5098 case BFD_ELF_VERSION_C_TYPE:
5099 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5102 expr = htab_find (head->htab, &e);
5103 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5104 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5110 case BFD_ELF_VERSION_CXX_TYPE:
5111 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5113 e.symbol = java_sym;
5114 expr = htab_find (head->htab, &e);
5115 while (expr && strcmp (expr->symbol, java_sym) == 0)
5116 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5127 /* Finally, try the wildcards. */
5128 if (prev == NULL || prev->symbol)
5129 expr = head->remaining;
5136 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5139 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5141 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5145 if (fnmatch (expr->pattern, s, 0) == 0)
5152 free ((char *) cxx_sym);
5153 if (java_sym != sym)
5154 free ((char *) java_sym);
5158 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5159 return a string pointing to the symbol name. */
5162 realsymbol (const char *pattern)
5165 bfd_boolean changed = FALSE, backslash = FALSE;
5166 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5168 for (p = pattern, s = symbol; *p != '\0'; ++p)
5170 /* It is a glob pattern only if there is no preceding
5172 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5180 /* Remove the preceding backslash. */
5187 backslash = *p == '\\';
5202 /* This is called for each variable name or match expression. */
5204 struct bfd_elf_version_expr *
5205 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5209 struct bfd_elf_version_expr *ret;
5211 ret = xmalloc (sizeof *ret);
5216 ret->symbol = realsymbol (new);
5218 if (lang == NULL || strcasecmp (lang, "C") == 0)
5219 ret->mask = BFD_ELF_VERSION_C_TYPE;
5220 else if (strcasecmp (lang, "C++") == 0)
5221 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5222 else if (strcasecmp (lang, "Java") == 0)
5223 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5226 einfo (_("%X%P: unknown language `%s' in version information\n"),
5228 ret->mask = BFD_ELF_VERSION_C_TYPE;
5231 return ldemul_new_vers_pattern (ret);
5234 /* This is called for each set of variable names and match
5237 struct bfd_elf_version_tree *
5238 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5239 struct bfd_elf_version_expr *locals)
5241 struct bfd_elf_version_tree *ret;
5243 ret = xcalloc (1, sizeof *ret);
5244 ret->globals.list = globals;
5245 ret->locals.list = locals;
5246 ret->match = lang_vers_match;
5247 ret->name_indx = (unsigned int) -1;
5251 /* This static variable keeps track of version indices. */
5253 static int version_index;
5256 version_expr_head_hash (const void *p)
5258 const struct bfd_elf_version_expr *e = p;
5260 return htab_hash_string (e->symbol);
5264 version_expr_head_eq (const void *p1, const void *p2)
5266 const struct bfd_elf_version_expr *e1 = p1;
5267 const struct bfd_elf_version_expr *e2 = p2;
5269 return strcmp (e1->symbol, e2->symbol) == 0;
5273 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5276 struct bfd_elf_version_expr *e, *next;
5277 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5279 for (e = head->list; e; e = e->next)
5283 head->mask |= e->mask;
5288 head->htab = htab_create (count * 2, version_expr_head_hash,
5289 version_expr_head_eq, NULL);
5290 list_loc = &head->list;
5291 remaining_loc = &head->remaining;
5292 for (e = head->list; e; e = next)
5298 remaining_loc = &e->next;
5302 void **loc = htab_find_slot (head->htab, e, INSERT);
5306 struct bfd_elf_version_expr *e1, *last;
5312 if (e1->mask == e->mask)
5320 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5324 /* This is a duplicate. */
5325 /* FIXME: Memory leak. Sometimes pattern is not
5326 xmalloced alone, but in larger chunk of memory. */
5327 /* free (e->symbol); */
5332 e->next = last->next;
5340 list_loc = &e->next;
5344 *remaining_loc = NULL;
5345 *list_loc = head->remaining;
5348 head->remaining = head->list;
5351 /* This is called when we know the name and dependencies of the
5355 lang_register_vers_node (const char *name,
5356 struct bfd_elf_version_tree *version,
5357 struct bfd_elf_version_deps *deps)
5359 struct bfd_elf_version_tree *t, **pp;
5360 struct bfd_elf_version_expr *e1;
5365 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5366 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5368 einfo (_("%X%P: anonymous version tag cannot be combined"
5369 " with other version tags\n"));
5374 /* Make sure this node has a unique name. */
5375 for (t = lang_elf_version_info; t != NULL; t = t->next)
5376 if (strcmp (t->name, name) == 0)
5377 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5379 lang_finalize_version_expr_head (&version->globals);
5380 lang_finalize_version_expr_head (&version->locals);
5382 /* Check the global and local match names, and make sure there
5383 aren't any duplicates. */
5385 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5387 for (t = lang_elf_version_info; t != NULL; t = t->next)
5389 struct bfd_elf_version_expr *e2;
5391 if (t->locals.htab && e1->symbol)
5393 e2 = htab_find (t->locals.htab, e1);
5394 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5396 if (e1->mask == e2->mask)
5397 einfo (_("%X%P: duplicate expression `%s'"
5398 " in version information\n"), e1->symbol);
5402 else if (!e1->symbol)
5403 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5404 if (strcmp (e1->pattern, e2->pattern) == 0
5405 && e1->mask == e2->mask)
5406 einfo (_("%X%P: duplicate expression `%s'"
5407 " in version information\n"), e1->pattern);
5411 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5413 for (t = lang_elf_version_info; t != NULL; t = t->next)
5415 struct bfd_elf_version_expr *e2;
5417 if (t->globals.htab && e1->symbol)
5419 e2 = htab_find (t->globals.htab, e1);
5420 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5422 if (e1->mask == e2->mask)
5423 einfo (_("%X%P: duplicate expression `%s'"
5424 " in version information\n"),
5429 else if (!e1->symbol)
5430 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5431 if (strcmp (e1->pattern, e2->pattern) == 0
5432 && e1->mask == e2->mask)
5433 einfo (_("%X%P: duplicate expression `%s'"
5434 " in version information\n"), e1->pattern);
5438 version->deps = deps;
5439 version->name = name;
5440 if (name[0] != '\0')
5443 version->vernum = version_index;
5446 version->vernum = 0;
5448 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5453 /* This is called when we see a version dependency. */
5455 struct bfd_elf_version_deps *
5456 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5458 struct bfd_elf_version_deps *ret;
5459 struct bfd_elf_version_tree *t;
5461 ret = xmalloc (sizeof *ret);
5464 for (t = lang_elf_version_info; t != NULL; t = t->next)
5466 if (strcmp (t->name, name) == 0)
5468 ret->version_needed = t;
5473 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5479 lang_do_version_exports_section (void)
5481 struct bfd_elf_version_expr *greg = NULL, *lreg;
5483 LANG_FOR_EACH_INPUT_STATEMENT (is)
5485 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5493 contents = xmalloc (len);
5494 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5495 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5498 while (p < contents + len)
5500 greg = lang_new_vers_pattern (greg, p, NULL);
5501 p = strchr (p, '\0') + 1;
5504 /* Do not free the contents, as we used them creating the regex. */
5506 /* Do not include this section in the link. */
5507 sec->flags |= SEC_EXCLUDE;
5510 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5511 lang_register_vers_node (command_line.version_exports_section,
5512 lang_new_vers_node (greg, lreg), NULL);
5516 lang_add_unique (const char *name)
5518 struct unique_sections *ent;
5520 for (ent = unique_section_list; ent; ent = ent->next)
5521 if (strcmp (ent->name, name) == 0)
5524 ent = xmalloc (sizeof *ent);
5525 ent->name = xstrdup (name);
5526 ent->next = unique_section_list;
5527 unique_section_list = ent;