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"
44 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
47 /* Locals variables. */
48 static struct obstack stat_obstack;
50 #define obstack_chunk_alloc xmalloc
51 #define obstack_chunk_free free
52 static const char *startup_file;
53 static lang_statement_list_type input_file_chain;
54 static bfd_boolean placed_commons = FALSE;
55 static lang_output_section_statement_type *default_common_section;
56 static bfd_boolean map_option_f;
57 static bfd_vma print_dot;
58 static lang_input_statement_type *first_file;
59 static const char *current_target;
60 static const char *output_target;
61 static lang_statement_list_type statement_list;
62 static struct lang_phdr *lang_phdr_list;
63 static struct bfd_hash_table lang_definedness_table;
65 /* Forward declarations. */
66 static void exp_init_os (etree_type *);
67 static bfd_boolean wildcardp (const char *);
68 static lang_input_statement_type *lookup_name (const char *);
69 static bfd_boolean load_symbols (lang_input_statement_type *,
70 lang_statement_list_type *);
71 static struct bfd_hash_entry *lang_definedness_newfunc
72 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
73 static void insert_undefined (const char *);
74 static void print_statement (lang_statement_union_type *,
75 lang_output_section_statement_type *);
76 static void print_statement_list (lang_statement_union_type *,
77 lang_output_section_statement_type *);
78 static void print_statements (void);
79 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
80 static void lang_record_phdrs (void);
81 static void lang_do_version_exports_section (void);
83 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
84 asection *, lang_input_statement_type *, void *);
86 /* Exported variables. */
87 lang_output_section_statement_type *abs_output_section;
88 lang_statement_list_type lang_output_section_statement;
89 lang_statement_list_type *stat_ptr = &statement_list;
90 lang_statement_list_type file_chain = { NULL, NULL };
91 struct bfd_sym_chain entry_symbol = { NULL, NULL };
92 const char *entry_section = ".text";
93 bfd_boolean entry_from_cmdline;
94 bfd_boolean lang_has_input_file = FALSE;
95 bfd_boolean had_output_filename = FALSE;
96 bfd_boolean lang_float_flag = FALSE;
97 bfd_boolean delete_output_file_on_failure = FALSE;
98 struct lang_nocrossrefs *nocrossref_list;
99 struct unique_sections *unique_section_list;
100 static bfd_boolean ldlang_sysrooted_script = FALSE;
101 int lang_statement_iteration = 0;
103 etree_type *base; /* Relocation base - or null */
105 #define new_stat(x, y) \
106 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
108 #define outside_section_address(q) \
109 ((q)->output_offset + (q)->output_section->vma)
111 #define outside_symbol_address(q) \
112 ((q)->value + outside_section_address (q->section))
114 #define SECTION_NAME_MAP_LENGTH (16)
117 stat_alloc (size_t size)
119 return obstack_alloc (&stat_obstack, size);
123 unique_section_p (const char *secnam)
125 struct unique_sections *unam;
127 for (unam = unique_section_list; unam; unam = unam->next)
128 if (wildcardp (unam->name)
129 ? fnmatch (unam->name, secnam, 0) == 0
130 : strcmp (unam->name, secnam) == 0)
138 /* Generic traversal routines for finding matching sections. */
141 walk_wild_section (lang_wild_statement_type *ptr,
142 lang_input_statement_type *file,
148 if (file->just_syms_flag)
151 for (s = file->the_bfd->sections; s != NULL; s = s->next)
153 struct wildcard_list *sec;
155 sec = ptr->section_list;
157 (*callback) (ptr, sec, s, file, data);
161 bfd_boolean skip = FALSE;
162 struct name_list *list_tmp;
164 /* Don't process sections from files which were
166 for (list_tmp = sec->spec.exclude_name_list;
168 list_tmp = list_tmp->next)
170 if (wildcardp (list_tmp->name))
171 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
173 skip = strcmp (list_tmp->name, file->filename) == 0;
175 /* If this file is part of an archive, and the archive is
176 excluded, exclude this file. */
177 if (! skip && file->the_bfd != NULL
178 && file->the_bfd->my_archive != NULL
179 && file->the_bfd->my_archive->filename != NULL)
181 if (wildcardp (list_tmp->name))
182 skip = fnmatch (list_tmp->name,
183 file->the_bfd->my_archive->filename,
186 skip = strcmp (list_tmp->name,
187 file->the_bfd->my_archive->filename) == 0;
194 if (!skip && sec->spec.name != NULL)
196 const char *sname = bfd_get_section_name (file->the_bfd, s);
198 if (wildcardp (sec->spec.name))
199 skip = fnmatch (sec->spec.name, sname, 0) != 0;
201 skip = strcmp (sec->spec.name, sname) != 0;
205 (*callback) (ptr, sec, s, file, data);
212 /* Handle a wild statement for a single file F. */
215 walk_wild_file (lang_wild_statement_type *s,
216 lang_input_statement_type *f,
220 if (f->the_bfd == NULL
221 || ! bfd_check_format (f->the_bfd, bfd_archive))
222 walk_wild_section (s, f, callback, data);
227 /* This is an archive file. We must map each member of the
228 archive separately. */
229 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
230 while (member != NULL)
232 /* When lookup_name is called, it will call the add_symbols
233 entry point for the archive. For each element of the
234 archive which is included, BFD will call ldlang_add_file,
235 which will set the usrdata field of the member to the
236 lang_input_statement. */
237 if (member->usrdata != NULL)
239 walk_wild_section (s, member->usrdata, callback, data);
242 member = bfd_openr_next_archived_file (f->the_bfd, member);
248 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
250 const char *file_spec = s->filename;
252 if (file_spec == NULL)
254 /* Perform the iteration over all files in the list. */
255 LANG_FOR_EACH_INPUT_STATEMENT (f)
257 walk_wild_file (s, f, callback, data);
260 else if (wildcardp (file_spec))
262 LANG_FOR_EACH_INPUT_STATEMENT (f)
264 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
265 walk_wild_file (s, f, callback, data);
270 lang_input_statement_type *f;
272 /* Perform the iteration over a single file. */
273 f = lookup_name (file_spec);
275 walk_wild_file (s, f, callback, data);
279 /* lang_for_each_statement walks the parse tree and calls the provided
280 function for each node. */
283 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
284 lang_statement_union_type *s)
286 for (; s != NULL; s = s->header.next)
290 switch (s->header.type)
292 case lang_constructors_statement_enum:
293 lang_for_each_statement_worker (func, constructor_list.head);
295 case lang_output_section_statement_enum:
296 lang_for_each_statement_worker
298 s->output_section_statement.children.head);
300 case lang_wild_statement_enum:
301 lang_for_each_statement_worker
303 s->wild_statement.children.head);
305 case lang_group_statement_enum:
306 lang_for_each_statement_worker (func,
307 s->group_statement.children.head);
309 case lang_data_statement_enum:
310 case lang_reloc_statement_enum:
311 case lang_object_symbols_statement_enum:
312 case lang_output_statement_enum:
313 case lang_target_statement_enum:
314 case lang_input_section_enum:
315 case lang_input_statement_enum:
316 case lang_assignment_statement_enum:
317 case lang_padding_statement_enum:
318 case lang_address_statement_enum:
319 case lang_fill_statement_enum:
329 lang_for_each_statement (void (*func) (lang_statement_union_type *))
331 lang_for_each_statement_worker (func, statement_list.head);
334 /*----------------------------------------------------------------------*/
337 lang_list_init (lang_statement_list_type *list)
340 list->tail = &list->head;
343 /* Build a new statement node for the parse tree. */
345 static lang_statement_union_type *
346 new_statement (enum statement_enum type,
348 lang_statement_list_type *list)
350 lang_statement_union_type *new;
352 new = stat_alloc (size);
353 new->header.type = type;
354 new->header.next = NULL;
355 lang_statement_append (list, new, &new->header.next);
359 /* Build a new input file node for the language. There are several
360 ways in which we treat an input file, eg, we only look at symbols,
361 or prefix it with a -l etc.
363 We can be supplied with requests for input files more than once;
364 they may, for example be split over several lines like foo.o(.text)
365 foo.o(.data) etc, so when asked for a file we check that we haven't
366 got it already so we don't duplicate the bfd. */
368 static lang_input_statement_type *
369 new_afile (const char *name,
370 lang_input_file_enum_type file_type,
372 bfd_boolean add_to_list)
374 lang_input_statement_type *p;
377 p = new_stat (lang_input_statement, stat_ptr);
380 p = stat_alloc (sizeof (lang_input_statement_type));
381 p->header.next = NULL;
384 lang_has_input_file = TRUE;
386 p->sysrooted = FALSE;
389 case lang_input_file_is_symbols_only_enum:
391 p->is_archive = FALSE;
393 p->local_sym_name = name;
394 p->just_syms_flag = TRUE;
395 p->search_dirs_flag = FALSE;
397 case lang_input_file_is_fake_enum:
399 p->is_archive = FALSE;
401 p->local_sym_name = name;
402 p->just_syms_flag = FALSE;
403 p->search_dirs_flag = FALSE;
405 case lang_input_file_is_l_enum:
406 p->is_archive = TRUE;
409 p->local_sym_name = concat ("-l", name, NULL);
410 p->just_syms_flag = FALSE;
411 p->search_dirs_flag = TRUE;
413 case lang_input_file_is_marker_enum:
415 p->is_archive = FALSE;
417 p->local_sym_name = name;
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = TRUE;
421 case lang_input_file_is_search_file_enum:
422 p->sysrooted = ldlang_sysrooted_script;
424 p->is_archive = FALSE;
426 p->local_sym_name = name;
427 p->just_syms_flag = FALSE;
428 p->search_dirs_flag = TRUE;
430 case lang_input_file_is_file_enum:
432 p->is_archive = FALSE;
434 p->local_sym_name = name;
435 p->just_syms_flag = FALSE;
436 p->search_dirs_flag = FALSE;
443 p->next_real_file = NULL;
446 p->dynamic = config.dynamic_link;
447 p->whole_archive = whole_archive;
449 lang_statement_append (&input_file_chain,
450 (lang_statement_union_type *) p,
455 lang_input_statement_type *
456 lang_add_input_file (const char *name,
457 lang_input_file_enum_type file_type,
460 lang_has_input_file = TRUE;
461 return new_afile (name, file_type, target, TRUE);
464 /* Build enough state so that the parser can build its tree. */
469 obstack_begin (&stat_obstack, 1000);
471 stat_ptr = &statement_list;
473 lang_list_init (stat_ptr);
475 lang_list_init (&input_file_chain);
476 lang_list_init (&lang_output_section_statement);
477 lang_list_init (&file_chain);
478 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
481 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
483 abs_output_section->bfd_section = bfd_abs_section_ptr;
485 /* The value "3" is ad-hoc, somewhat related to the expected number of
486 DEFINED expressions in a linker script. For most default linker
487 scripts, there are none. Why a hash table then? Well, it's somewhat
488 simpler to re-use working machinery than using a linked list in terms
489 of code-complexity here in ld, besides the initialization which just
490 looks like other code here. */
491 if (bfd_hash_table_init_n (&lang_definedness_table,
492 lang_definedness_newfunc, 3) != TRUE)
493 einfo (_("%P%F: out of memory during initialization"));
495 /* Callers of exp_fold_tree need to increment this. */
496 lang_statement_iteration = 0;
499 /*----------------------------------------------------------------------
500 A region is an area of memory declared with the
501 MEMORY { name:org=exp, len=exp ... }
504 We maintain a list of all the regions here.
506 If no regions are specified in the script, then the default is used
507 which is created when looked up to be the entire data space. */
509 static lang_memory_region_type *lang_memory_region_list;
510 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
512 lang_memory_region_type *
513 lang_memory_region_lookup (const char *const name)
515 lang_memory_region_type *p;
516 lang_memory_region_type *new;
518 /* NAME is NULL for LMA memspecs if no region was specified. */
522 for (p = lang_memory_region_list; p != NULL; p = p->next)
524 if (strcmp (p->name, name) == 0)
529 /* This code used to always use the first region in the list as the
530 default region. I changed it to instead use a region
531 encompassing all of memory as the default region. This permits
532 NOLOAD sections to work reasonably without requiring a region.
533 People should specify what region they mean, if they really want
535 if (strcmp (name, "*default*") == 0)
537 if (lang_memory_region_list != NULL)
538 return lang_memory_region_list;
542 new = stat_alloc (sizeof (lang_memory_region_type));
544 new->name = xstrdup (name);
547 *lang_memory_region_list_tail = new;
548 lang_memory_region_list_tail = &new->next;
552 new->length = ~(bfd_size_type) 0;
554 new->had_full_message = FALSE;
559 static lang_memory_region_type *
560 lang_memory_default (asection *section)
562 lang_memory_region_type *p;
564 flagword sec_flags = section->flags;
566 /* Override SEC_DATA to mean a writable section. */
567 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
568 sec_flags |= SEC_DATA;
570 for (p = lang_memory_region_list; p != NULL; p = p->next)
572 if ((p->flags & sec_flags) != 0
573 && (p->not_flags & sec_flags) == 0)
578 return lang_memory_region_lookup ("*default*");
581 lang_output_section_statement_type *
582 lang_output_section_find (const char *const name)
584 lang_statement_union_type *u;
585 lang_output_section_statement_type *lookup;
587 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
589 lookup = &u->output_section_statement;
590 if (strcmp (name, lookup->name) == 0)
596 lang_output_section_statement_type *
597 lang_output_section_statement_lookup (const char *const name)
599 lang_output_section_statement_type *lookup;
601 lookup = lang_output_section_find (name);
604 lookup = new_stat (lang_output_section_statement, stat_ptr);
605 lookup->region = NULL;
606 lookup->lma_region = NULL;
608 lookup->block_value = 1;
612 lookup->bfd_section = NULL;
613 lookup->processed = FALSE;
614 lookup->sectype = normal_section;
615 lookup->addr_tree = NULL;
616 lang_list_init (&lookup->children);
618 lookup->memspec = NULL;
620 lookup->subsection_alignment = -1;
621 lookup->section_alignment = -1;
622 lookup->load_base = NULL;
623 lookup->update_dot_tree = NULL;
624 lookup->phdrs = NULL;
626 lang_statement_append (&lang_output_section_statement,
627 (lang_statement_union_type *) lookup,
634 lang_map_flags (flagword flag)
636 if (flag & SEC_ALLOC)
642 if (flag & SEC_READONLY)
655 lang_memory_region_type *m;
657 minfo (_("\nMemory Configuration\n\n"));
658 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
659 _("Name"), _("Origin"), _("Length"), _("Attributes"));
661 for (m = lang_memory_region_list; m != NULL; m = m->next)
666 fprintf (config.map_file, "%-16s ", m->name);
668 sprintf_vma (buf, m->origin);
669 minfo ("0x%s ", buf);
677 minfo ("0x%V", m->length);
678 if (m->flags || m->not_flags)
686 lang_map_flags (m->flags);
692 lang_map_flags (m->not_flags);
699 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
704 /* Initialize an output section. */
707 init_os (lang_output_section_statement_type *s)
709 section_userdata_type *new;
711 if (s->bfd_section != NULL)
714 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
715 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
717 new = stat_alloc (sizeof (section_userdata_type));
719 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
720 if (s->bfd_section == NULL)
721 s->bfd_section = bfd_make_section (output_bfd, s->name);
722 if (s->bfd_section == NULL)
724 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
725 output_bfd->xvec->name, s->name);
727 s->bfd_section->output_section = s->bfd_section;
729 /* We initialize an output sections output offset to minus its own
730 vma to allow us to output a section through itself. */
731 s->bfd_section->output_offset = 0;
732 get_userdata (s->bfd_section) = new;
734 /* If there is a base address, make sure that any sections it might
735 mention are initialized. */
736 if (s->addr_tree != NULL)
737 exp_init_os (s->addr_tree);
739 if (s->load_base != NULL)
740 exp_init_os (s->load_base);
743 /* Make sure that all output sections mentioned in an expression are
747 exp_init_os (etree_type *exp)
749 switch (exp->type.node_class)
752 exp_init_os (exp->assign.src);
756 exp_init_os (exp->binary.lhs);
757 exp_init_os (exp->binary.rhs);
761 exp_init_os (exp->trinary.cond);
762 exp_init_os (exp->trinary.lhs);
763 exp_init_os (exp->trinary.rhs);
767 exp_init_os (exp->unary.child);
771 switch (exp->type.node_code)
777 lang_output_section_statement_type *os;
779 os = lang_output_section_find (exp->name.name);
780 if (os != NULL && os->bfd_section == NULL)
791 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
792 once into the output. This routine checks each section, and
793 arrange to discard it if a section of the same name has already
794 been linked. If the section has COMDAT information, then it uses
795 that to decide whether the section should be included. This code
796 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
797 that is, it does not depend solely upon the section name.
798 section_already_linked is called via bfd_map_over_sections. */
800 /* This is the shape of the elements inside the already_linked hash
801 table. It maps a name onto a list of already_linked elements with
802 the same name. It's possible to get more than one element in a
803 list if the COMDAT sections have different names. */
805 struct already_linked_hash_entry
807 struct bfd_hash_entry root;
808 struct already_linked *entry;
811 struct already_linked
813 struct already_linked *next;
817 /* The hash table. */
819 static struct bfd_hash_table already_linked_table;
822 section_already_linked (bfd *abfd, asection *sec, void *data)
824 lang_input_statement_type *entry = data;
827 struct already_linked *l;
828 struct already_linked_hash_entry *already_linked_list;
830 /* If we are only reading symbols from this object, then we want to
831 discard all sections. */
832 if (entry->just_syms_flag)
834 bfd_link_just_syms (sec, &link_info);
838 flags = bfd_get_section_flags (abfd, sec);
840 if ((flags & SEC_LINK_ONCE) == 0)
843 /* FIXME: When doing a relocatable link, we may have trouble
844 copying relocations in other sections that refer to local symbols
845 in the section being discarded. Those relocations will have to
846 be converted somehow; as of this writing I'm not sure that any of
847 the backends handle that correctly.
849 It is tempting to instead not discard link once sections when
850 doing a relocatable link (technically, they should be discarded
851 whenever we are building constructors). However, that fails,
852 because the linker winds up combining all the link once sections
853 into a single large link once section, which defeats the purpose
854 of having link once sections in the first place.
856 Also, not merging link once sections in a relocatable link
857 causes trouble for MIPS ELF, which relies on link once semantics
858 to handle the .reginfo section correctly. */
860 name = bfd_get_section_name (abfd, sec);
862 already_linked_list =
863 ((struct already_linked_hash_entry *)
864 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
866 for (l = already_linked_list->entry; l != NULL; l = l->next)
868 if (sec->comdat == NULL
869 || l->sec->comdat == NULL
870 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
872 /* The section has already been linked. See if we should
874 switch (flags & SEC_LINK_DUPLICATES)
879 case SEC_LINK_DUPLICATES_DISCARD:
882 case SEC_LINK_DUPLICATES_ONE_ONLY:
883 if (sec->comdat == NULL)
884 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
887 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
888 abfd, name, sec->comdat->name);
891 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
892 /* FIXME: We should really dig out the contents of both
893 sections and memcmp them. The COFF/PE spec says that
894 the Microsoft linker does not implement this
895 correctly, so I'm not going to bother doing it
898 case SEC_LINK_DUPLICATES_SAME_SIZE:
899 if (bfd_section_size (abfd, sec)
900 != bfd_section_size (l->sec->owner, l->sec))
901 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
906 /* Set the output_section field so that lang_add_section
907 does not create a lang_input_section structure for this
908 section. Since there might be a symbol in the section
909 being discarded, we must retain a pointer to the section
910 which we are really going to use. */
911 sec->output_section = bfd_abs_section_ptr;
912 sec->kept_section = l->sec;
914 if (flags & SEC_GROUP)
915 bfd_discard_group (abfd, sec);
921 /* This is the first section with this name. Record it. Allocate
922 the memory from the same obstack as the hash table is kept in. */
924 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
927 l->next = already_linked_list->entry;
928 already_linked_list->entry = l;
931 /* Support routines for the hash table used by section_already_linked,
932 initialize the table, fill in an entry and remove the table. */
934 static struct bfd_hash_entry *
935 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
936 struct bfd_hash_table *table,
937 const char *string ATTRIBUTE_UNUSED)
939 struct already_linked_hash_entry *ret =
940 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
948 already_linked_table_init (void)
950 if (! bfd_hash_table_init_n (&already_linked_table,
951 already_linked_newfunc,
953 einfo (_("%P%F: Failed to create hash table\n"));
957 already_linked_table_free (void)
959 bfd_hash_table_free (&already_linked_table);
962 /* The wild routines.
964 These expand statements like *(.text) and foo.o to a list of
965 explicit actions, like foo.o(.text), bar.o(.text) and
966 foo.o(.text, .data). */
968 /* Return TRUE if the PATTERN argument is a wildcard pattern.
969 Although backslashes are treated specially if a pattern contains
970 wildcards, we do not consider the mere presence of a backslash to
971 be enough to cause the pattern to be treated as a wildcard.
972 That lets us handle DOS filenames more naturally. */
975 wildcardp (const char *pattern)
979 for (s = pattern; *s != '\0'; ++s)
987 /* Add SECTION to the output section OUTPUT. Do this by creating a
988 lang_input_section statement which is placed at PTR. FILE is the
989 input file which holds SECTION. */
992 lang_add_section (lang_statement_list_type *ptr,
994 lang_output_section_statement_type *output,
995 lang_input_statement_type *file)
1000 flags = bfd_get_section_flags (section->owner, section);
1004 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1005 link. Discard debugging sections marked with SEC_EXCLUDE on a
1006 relocatable link too. */
1007 if ((flags & SEC_EXCLUDE) != 0
1008 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
1011 /* Discard input sections which are assigned to a section named
1012 DISCARD_SECTION_NAME. */
1013 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1016 /* Discard debugging sections if we are stripping debugging
1018 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1019 && (flags & SEC_DEBUGGING) != 0)
1024 if (section->output_section == NULL)
1026 /* This prevents future calls from assigning this section. */
1027 section->output_section = bfd_abs_section_ptr;
1032 if (section->output_section == NULL)
1035 lang_input_section_type *new;
1038 if (output->bfd_section == NULL)
1041 first = ! output->bfd_section->linker_has_input;
1042 output->bfd_section->linker_has_input = 1;
1044 /* Add a section reference to the list. */
1045 new = new_stat (lang_input_section, ptr);
1047 new->section = section;
1049 section->output_section = output->bfd_section;
1051 flags = section->flags;
1053 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1054 to an output section, because we want to be able to include a
1055 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1056 section (I don't know why we want to do this, but we do).
1057 build_link_order in ldwrite.c handles this case by turning
1058 the embedded SEC_NEVER_LOAD section into a fill. */
1060 flags &= ~ SEC_NEVER_LOAD;
1062 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1063 already been processed. One reason to do this is that on pe
1064 format targets, .text$foo sections go into .text and it's odd
1065 to see .text with SEC_LINK_ONCE set. */
1067 if (! link_info.relocatable)
1068 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1070 /* If this is not the first input section, and the SEC_READONLY
1071 flag is not currently set, then don't set it just because the
1072 input section has it set. */
1074 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1075 flags &= ~ SEC_READONLY;
1077 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1079 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1080 != (flags & (SEC_MERGE | SEC_STRINGS))
1081 || ((flags & SEC_MERGE)
1082 && section->output_section->entsize != section->entsize)))
1084 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1085 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1088 /* For now make .tbss normal section. */
1089 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocatable)
1092 section->output_section->flags |= flags;
1094 if (flags & SEC_MERGE)
1095 section->output_section->entsize = section->entsize;
1097 /* If SEC_READONLY is not set in the input section, then clear
1098 it from the output section. */
1099 if ((section->flags & SEC_READONLY) == 0)
1100 section->output_section->flags &= ~SEC_READONLY;
1102 switch (output->sectype)
1104 case normal_section:
1109 case overlay_section:
1110 output->bfd_section->flags &= ~SEC_ALLOC;
1112 case noload_section:
1113 output->bfd_section->flags &= ~SEC_LOAD;
1114 output->bfd_section->flags |= SEC_NEVER_LOAD;
1118 /* Copy over SEC_SMALL_DATA. */
1119 if (section->flags & SEC_SMALL_DATA)
1120 section->output_section->flags |= SEC_SMALL_DATA;
1122 if (section->alignment_power > output->bfd_section->alignment_power)
1123 output->bfd_section->alignment_power = section->alignment_power;
1125 /* If supplied an alignment, then force it. */
1126 if (output->section_alignment != -1)
1127 output->bfd_section->alignment_power = output->section_alignment;
1129 if (section->flags & SEC_BLOCK)
1131 section->output_section->flags |= SEC_BLOCK;
1132 /* FIXME: This value should really be obtained from the bfd... */
1133 output->block_value = 128;
1138 /* Handle wildcard sorting. This returns the lang_input_section which
1139 should follow the one we are going to create for SECTION and FILE,
1140 based on the sorting requirements of WILD. It returns NULL if the
1141 new section should just go at the end of the current list. */
1143 static lang_statement_union_type *
1144 wild_sort (lang_wild_statement_type *wild,
1145 struct wildcard_list *sec,
1146 lang_input_statement_type *file,
1149 const char *section_name;
1150 lang_statement_union_type *l;
1152 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1155 section_name = bfd_get_section_name (file->the_bfd, section);
1156 for (l = wild->children.head; l != NULL; l = l->header.next)
1158 lang_input_section_type *ls;
1160 if (l->header.type != lang_input_section_enum)
1162 ls = &l->input_section;
1164 /* Sorting by filename takes precedence over sorting by section
1167 if (wild->filenames_sorted)
1169 const char *fn, *ln;
1173 /* The PE support for the .idata section as generated by
1174 dlltool assumes that files will be sorted by the name of
1175 the archive and then the name of the file within the
1178 if (file->the_bfd != NULL
1179 && bfd_my_archive (file->the_bfd) != NULL)
1181 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1186 fn = file->filename;
1190 if (ls->ifile->the_bfd != NULL
1191 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1193 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1198 ln = ls->ifile->filename;
1202 i = strcmp (fn, ln);
1211 fn = file->filename;
1213 ln = ls->ifile->filename;
1215 i = strcmp (fn, ln);
1223 /* Here either the files are not sorted by name, or we are
1224 looking at the sections for this file. */
1226 if (sec != NULL && sec->spec.sorted)
1228 if (strcmp (section_name,
1229 bfd_get_section_name (ls->ifile->the_bfd,
1239 /* Expand a wild statement for a particular FILE. SECTION may be
1240 NULL, in which case it is a wild card. */
1243 output_section_callback (lang_wild_statement_type *ptr,
1244 struct wildcard_list *sec,
1246 lang_input_statement_type *file,
1249 lang_statement_union_type *before;
1251 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1252 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1255 /* If the wild pattern was marked KEEP, the member sections
1256 should be as well. */
1257 if (ptr->keep_sections)
1258 section->flags |= SEC_KEEP;
1260 before = wild_sort (ptr, sec, file, section);
1262 /* Here BEFORE points to the lang_input_section which
1263 should follow the one we are about to add. If BEFORE
1264 is NULL, then the section should just go at the end
1265 of the current list. */
1268 lang_add_section (&ptr->children, section,
1269 (lang_output_section_statement_type *) output,
1273 lang_statement_list_type list;
1274 lang_statement_union_type **pp;
1276 lang_list_init (&list);
1277 lang_add_section (&list, section,
1278 (lang_output_section_statement_type *) output,
1281 /* If we are discarding the section, LIST.HEAD will
1283 if (list.head != NULL)
1285 ASSERT (list.head->header.next == NULL);
1287 for (pp = &ptr->children.head;
1289 pp = &(*pp)->header.next)
1290 ASSERT (*pp != NULL);
1292 list.head->header.next = *pp;
1298 /* This is passed a file name which must have been seen already and
1299 added to the statement tree. We will see if it has been opened
1300 already and had its symbols read. If not then we'll read it. */
1302 static lang_input_statement_type *
1303 lookup_name (const char *name)
1305 lang_input_statement_type *search;
1307 for (search = (lang_input_statement_type *) input_file_chain.head;
1309 search = (lang_input_statement_type *) search->next_real_file)
1311 if (search->filename == NULL && name == NULL)
1313 if (search->filename != NULL
1315 && strcmp (search->filename, name) == 0)
1320 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1323 /* If we have already added this file, or this file is not real
1324 (FIXME: can that ever actually happen?) or the name is NULL
1325 (FIXME: can that ever actually happen?) don't add this file. */
1328 || search->filename == NULL)
1331 if (! load_symbols (search, NULL))
1337 /* Get the symbols for an input file. */
1340 load_symbols (lang_input_statement_type *entry,
1341 lang_statement_list_type *place)
1348 ldfile_open_file (entry);
1350 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1351 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1354 lang_statement_list_type *hold;
1355 bfd_boolean bad_load = TRUE;
1356 bfd_boolean save_ldlang_sysrooted_script;
1358 err = bfd_get_error ();
1360 /* See if the emulation has some special knowledge. */
1361 if (ldemul_unrecognized_file (entry))
1364 if (err == bfd_error_file_ambiguously_recognized)
1368 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1369 einfo (_("%B: matching formats:"), entry->the_bfd);
1370 for (p = matching; *p != NULL; p++)
1374 else if (err != bfd_error_file_not_recognized
1376 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1380 bfd_close (entry->the_bfd);
1381 entry->the_bfd = NULL;
1383 /* Try to interpret the file as a linker script. */
1384 ldfile_open_command_file (entry->filename);
1388 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1389 ldlang_sysrooted_script = entry->sysrooted;
1391 ldfile_assumed_script = TRUE;
1392 parser_input = input_script;
1394 ldfile_assumed_script = FALSE;
1396 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1402 if (ldemul_recognized_file (entry))
1405 /* We don't call ldlang_add_file for an archive. Instead, the
1406 add_symbols entry point will call ldlang_add_file, via the
1407 add_archive_element callback, for each element of the archive
1409 switch (bfd_get_format (entry->the_bfd))
1415 ldlang_add_file (entry);
1416 if (trace_files || trace_file_tries)
1417 info_msg ("%I\n", entry);
1421 if (entry->whole_archive)
1424 bfd_boolean loaded = TRUE;
1428 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1433 if (! bfd_check_format (member, bfd_object))
1435 einfo (_("%F%B: member %B in archive is not an object\n"),
1436 entry->the_bfd, member);
1440 if (! ((*link_info.callbacks->add_archive_element)
1441 (&link_info, member, "--whole-archive")))
1444 if (! bfd_link_add_symbols (member, &link_info))
1446 einfo (_("%F%B: could not read symbols: %E\n"), member);
1451 entry->loaded = loaded;
1457 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1458 entry->loaded = TRUE;
1460 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1462 return entry->loaded;
1465 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1466 may be NULL, indicating that it is a wildcard. Separate
1467 lang_input_section statements are created for each part of the
1468 expansion; they are added after the wild statement S. OUTPUT is
1469 the output section. */
1472 wild (lang_wild_statement_type *s,
1473 const char *target ATTRIBUTE_UNUSED,
1474 lang_output_section_statement_type *output)
1476 struct wildcard_list *sec;
1478 walk_wild (s, output_section_callback, output);
1480 for (sec = s->section_list; sec != NULL; sec = sec->next)
1482 if (default_common_section != NULL)
1484 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1486 /* Remember the section that common is going to in case we
1487 later get something which doesn't know where to put it. */
1488 default_common_section = output;
1493 /* Return TRUE iff target is the sought target. */
1496 get_target (const bfd_target *target, void *data)
1498 const char *sought = data;
1500 return strcmp (target->name, sought) == 0;
1503 /* Like strcpy() but convert to lower case as well. */
1506 stricpy (char *dest, char *src)
1510 while ((c = *src++) != 0)
1511 *dest++ = TOLOWER (c);
1516 /* Remove the first occurrence of needle (if any) in haystack
1520 strcut (char *haystack, char *needle)
1522 haystack = strstr (haystack, needle);
1528 for (src = haystack + strlen (needle); *src;)
1529 *haystack++ = *src++;
1535 /* Compare two target format name strings.
1536 Return a value indicating how "similar" they are. */
1539 name_compare (char *first, char *second)
1545 copy1 = xmalloc (strlen (first) + 1);
1546 copy2 = xmalloc (strlen (second) + 1);
1548 /* Convert the names to lower case. */
1549 stricpy (copy1, first);
1550 stricpy (copy2, second);
1552 /* Remove size and endian strings from the name. */
1553 strcut (copy1, "big");
1554 strcut (copy1, "little");
1555 strcut (copy2, "big");
1556 strcut (copy2, "little");
1558 /* Return a value based on how many characters match,
1559 starting from the beginning. If both strings are
1560 the same then return 10 * their length. */
1561 for (result = 0; copy1[result] == copy2[result]; result++)
1562 if (copy1[result] == 0)
1574 /* Set by closest_target_match() below. */
1575 static const bfd_target *winner;
1577 /* Scan all the valid bfd targets looking for one that has the endianness
1578 requirement that was specified on the command line, and is the nearest
1579 match to the original output target. */
1582 closest_target_match (const bfd_target *target, void *data)
1584 const bfd_target *original = data;
1586 if (command_line.endian == ENDIAN_BIG
1587 && target->byteorder != BFD_ENDIAN_BIG)
1590 if (command_line.endian == ENDIAN_LITTLE
1591 && target->byteorder != BFD_ENDIAN_LITTLE)
1594 /* Must be the same flavour. */
1595 if (target->flavour != original->flavour)
1598 /* If we have not found a potential winner yet, then record this one. */
1605 /* Oh dear, we now have two potential candidates for a successful match.
1606 Compare their names and choose the better one. */
1607 if (name_compare (target->name, original->name)
1608 > name_compare (winner->name, original->name))
1611 /* Keep on searching until wqe have checked them all. */
1615 /* Return the BFD target format of the first input file. */
1618 get_first_input_target (void)
1620 char *target = NULL;
1622 LANG_FOR_EACH_INPUT_STATEMENT (s)
1624 if (s->header.type == lang_input_statement_enum
1627 ldfile_open_file (s);
1629 if (s->the_bfd != NULL
1630 && bfd_check_format (s->the_bfd, bfd_object))
1632 target = bfd_get_target (s->the_bfd);
1644 lang_get_output_target (void)
1648 /* Has the user told us which output format to use? */
1649 if (output_target != NULL)
1650 return output_target;
1652 /* No - has the current target been set to something other than
1654 if (current_target != default_target)
1655 return current_target;
1657 /* No - can we determine the format of the first input file? */
1658 target = get_first_input_target ();
1662 /* Failed - use the default output target. */
1663 return default_target;
1666 /* Open the output file. */
1669 open_output (const char *name)
1673 output_target = lang_get_output_target ();
1675 /* Has the user requested a particular endianness on the command
1677 if (command_line.endian != ENDIAN_UNSET)
1679 const bfd_target *target;
1680 enum bfd_endian desired_endian;
1682 /* Get the chosen target. */
1683 target = bfd_search_for_target (get_target, (void *) output_target);
1685 /* If the target is not supported, we cannot do anything. */
1688 if (command_line.endian == ENDIAN_BIG)
1689 desired_endian = BFD_ENDIAN_BIG;
1691 desired_endian = BFD_ENDIAN_LITTLE;
1693 /* See if the target has the wrong endianness. This should
1694 not happen if the linker script has provided big and
1695 little endian alternatives, but some scrips don't do
1697 if (target->byteorder != desired_endian)
1699 /* If it does, then see if the target provides
1700 an alternative with the correct endianness. */
1701 if (target->alternative_target != NULL
1702 && (target->alternative_target->byteorder == desired_endian))
1703 output_target = target->alternative_target->name;
1706 /* Try to find a target as similar as possible to
1707 the default target, but which has the desired
1708 endian characteristic. */
1709 bfd_search_for_target (closest_target_match,
1712 /* Oh dear - we could not find any targets that
1713 satisfy our requirements. */
1715 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1717 output_target = winner->name;
1723 output = bfd_openw (name, output_target);
1727 if (bfd_get_error () == bfd_error_invalid_target)
1728 einfo (_("%P%F: target %s not found\n"), output_target);
1730 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1733 delete_output_file_on_failure = TRUE;
1736 output->flags |= D_PAGED;
1739 if (! bfd_set_format (output, bfd_object))
1740 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1741 if (! bfd_set_arch_mach (output,
1742 ldfile_output_architecture,
1743 ldfile_output_machine))
1744 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1746 link_info.hash = bfd_link_hash_table_create (output);
1747 if (link_info.hash == NULL)
1748 einfo (_("%P%F: can not create link hash table: %E\n"));
1750 bfd_set_gp_size (output, g_switch_value);
1755 ldlang_open_output (lang_statement_union_type *statement)
1757 switch (statement->header.type)
1759 case lang_output_statement_enum:
1760 ASSERT (output_bfd == NULL);
1761 output_bfd = open_output (statement->output_statement.name);
1762 ldemul_set_output_arch ();
1763 if (config.magic_demand_paged && !link_info.relocatable)
1764 output_bfd->flags |= D_PAGED;
1766 output_bfd->flags &= ~D_PAGED;
1767 if (config.text_read_only)
1768 output_bfd->flags |= WP_TEXT;
1770 output_bfd->flags &= ~WP_TEXT;
1771 if (link_info.traditional_format)
1772 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1774 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1777 case lang_target_statement_enum:
1778 current_target = statement->target_statement.target;
1785 /* Open all the input files. */
1788 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1790 for (; s != NULL; s = s->header.next)
1792 switch (s->header.type)
1794 case lang_constructors_statement_enum:
1795 open_input_bfds (constructor_list.head, force);
1797 case lang_output_section_statement_enum:
1798 open_input_bfds (s->output_section_statement.children.head, force);
1800 case lang_wild_statement_enum:
1801 /* Maybe we should load the file's symbols. */
1802 if (s->wild_statement.filename
1803 && ! wildcardp (s->wild_statement.filename))
1804 (void) lookup_name (s->wild_statement.filename);
1805 open_input_bfds (s->wild_statement.children.head, force);
1807 case lang_group_statement_enum:
1809 struct bfd_link_hash_entry *undefs;
1811 /* We must continually search the entries in the group
1812 until no new symbols are added to the list of undefined
1817 undefs = link_info.hash->undefs_tail;
1818 open_input_bfds (s->group_statement.children.head, TRUE);
1820 while (undefs != link_info.hash->undefs_tail);
1823 case lang_target_statement_enum:
1824 current_target = s->target_statement.target;
1826 case lang_input_statement_enum:
1827 if (s->input_statement.real)
1829 lang_statement_list_type add;
1831 s->input_statement.target = current_target;
1833 /* If we are being called from within a group, and this
1834 is an archive which has already been searched, then
1835 force it to be researched unless the whole archive
1836 has been loaded already. */
1838 && !s->input_statement.whole_archive
1839 && s->input_statement.loaded
1840 && bfd_check_format (s->input_statement.the_bfd,
1842 s->input_statement.loaded = FALSE;
1844 lang_list_init (&add);
1846 if (! load_symbols (&s->input_statement, &add))
1847 config.make_executable = FALSE;
1849 if (add.head != NULL)
1851 *add.tail = s->header.next;
1852 s->header.next = add.head;
1862 /* If there are [COMMONS] statements, put a wild one into the bss
1866 lang_reasonable_defaults (void)
1869 lang_output_section_statement_lookup (".text");
1870 lang_output_section_statement_lookup (".data");
1872 default_common_section = lang_output_section_statement_lookup (".bss");
1874 if (!placed_commons)
1876 lang_wild_statement_type *new =
1877 new_stat (lang_wild_statement,
1878 &default_common_section->children);
1880 new->section_name = "COMMON";
1881 new->filename = NULL;
1882 lang_list_init (&new->children);
1887 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1890 lang_track_definedness (const char *name)
1892 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1893 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1896 /* New-function for the definedness hash table. */
1898 static struct bfd_hash_entry *
1899 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1900 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1901 const char *name ATTRIBUTE_UNUSED)
1903 struct lang_definedness_hash_entry *ret
1904 = (struct lang_definedness_hash_entry *) entry;
1907 ret = (struct lang_definedness_hash_entry *)
1908 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1911 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1913 ret->iteration = -1;
1917 /* Return the iteration when the definition of NAME was last updated. A
1918 value of -1 means that the symbol is not defined in the linker script
1919 or the command line, but may be defined in the linker symbol table. */
1922 lang_symbol_definition_iteration (const char *name)
1924 struct lang_definedness_hash_entry *defentry
1925 = (struct lang_definedness_hash_entry *)
1926 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1928 /* We've already created this one on the presence of DEFINED in the
1929 script, so it can't be NULL unless something is borked elsewhere in
1931 if (defentry == NULL)
1934 return defentry->iteration;
1937 /* Update the definedness state of NAME. */
1940 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1942 struct lang_definedness_hash_entry *defentry
1943 = (struct lang_definedness_hash_entry *)
1944 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1946 /* We don't keep track of symbols not tested with DEFINED. */
1947 if (defentry == NULL)
1950 /* If the symbol was already defined, and not from an earlier statement
1951 iteration, don't update the definedness iteration, because that'd
1952 make the symbol seem defined in the linker script at this point, and
1953 it wasn't; it was defined in some object. If we do anyway, DEFINED
1954 would start to yield false before this point and the construct "sym =
1955 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1957 if (h->type != bfd_link_hash_undefined
1958 && h->type != bfd_link_hash_common
1959 && h->type != bfd_link_hash_new
1960 && defentry->iteration == -1)
1963 defentry->iteration = lang_statement_iteration;
1966 /* Add the supplied name to the symbol table as an undefined reference.
1967 This is a two step process as the symbol table doesn't even exist at
1968 the time the ld command line is processed. First we put the name
1969 on a list, then, once the output file has been opened, transfer the
1970 name to the symbol table. */
1972 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
1974 #define ldlang_undef_chain_list_head entry_symbol.next
1977 ldlang_add_undef (const char *const name)
1979 ldlang_undef_chain_list_type *new =
1980 stat_alloc (sizeof (ldlang_undef_chain_list_type));
1982 new->next = ldlang_undef_chain_list_head;
1983 ldlang_undef_chain_list_head = new;
1985 new->name = xstrdup (name);
1987 if (output_bfd != NULL)
1988 insert_undefined (new->name);
1991 /* Insert NAME as undefined in the symbol table. */
1994 insert_undefined (const char *name)
1996 struct bfd_link_hash_entry *h;
1998 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2000 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2001 if (h->type == bfd_link_hash_new)
2003 h->type = bfd_link_hash_undefined;
2004 h->u.undef.abfd = NULL;
2005 bfd_link_add_undef (link_info.hash, h);
2009 /* Run through the list of undefineds created above and place them
2010 into the linker hash table as undefined symbols belonging to the
2014 lang_place_undefineds (void)
2016 ldlang_undef_chain_list_type *ptr;
2018 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2019 insert_undefined (ptr->name);
2022 /* Open input files and attach to output sections. */
2025 map_input_to_output_sections
2026 (lang_statement_union_type *s, const char *target,
2027 lang_output_section_statement_type *output_section_statement)
2029 for (; s != NULL; s = s->header.next)
2031 switch (s->header.type)
2033 case lang_wild_statement_enum:
2034 wild (&s->wild_statement, target, output_section_statement);
2036 case lang_constructors_statement_enum:
2037 map_input_to_output_sections (constructor_list.head,
2039 output_section_statement);
2041 case lang_output_section_statement_enum:
2042 map_input_to_output_sections (s->output_section_statement.children.head,
2044 &s->output_section_statement);
2046 case lang_output_statement_enum:
2048 case lang_target_statement_enum:
2049 target = s->target_statement.target;
2051 case lang_group_statement_enum:
2052 map_input_to_output_sections (s->group_statement.children.head,
2054 output_section_statement);
2056 case lang_fill_statement_enum:
2057 case lang_input_section_enum:
2058 case lang_object_symbols_statement_enum:
2059 case lang_data_statement_enum:
2060 case lang_reloc_statement_enum:
2061 case lang_padding_statement_enum:
2062 case lang_input_statement_enum:
2063 if (output_section_statement != NULL
2064 && output_section_statement->bfd_section == NULL)
2065 init_os (output_section_statement);
2067 case lang_assignment_statement_enum:
2068 if (output_section_statement != NULL
2069 && output_section_statement->bfd_section == NULL)
2070 init_os (output_section_statement);
2072 /* Make sure that any sections mentioned in the assignment
2074 exp_init_os (s->assignment_statement.exp);
2076 case lang_afile_asection_pair_statement_enum:
2079 case lang_address_statement_enum:
2080 /* Mark the specified section with the supplied address. */
2082 lang_output_section_statement_type *os =
2083 lang_output_section_statement_lookup
2084 (s->address_statement.section_name);
2086 if (os->bfd_section == NULL)
2088 os->addr_tree = s->address_statement.address;
2095 /* An output section might have been removed after its statement was
2096 added. For example, ldemul_before_allocation can remove dynamic
2097 sections if they turn out to be not needed. Clean them up here. */
2100 strip_excluded_output_sections (void)
2102 lang_statement_union_type *u;
2104 for (u = lang_output_section_statement.head;
2106 u = u->output_section_statement.next)
2108 lang_output_section_statement_type *os;
2111 os = &u->output_section_statement;
2112 s = os->bfd_section;
2113 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2117 os->bfd_section = NULL;
2119 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2122 bfd_section_list_remove (output_bfd, p);
2123 output_bfd->section_count--;
2131 print_output_section_statement
2132 (lang_output_section_statement_type *output_section_statement)
2134 asection *section = output_section_statement->bfd_section;
2137 if (output_section_statement != abs_output_section)
2139 minfo ("\n%s", output_section_statement->name);
2141 if (section != NULL)
2143 print_dot = section->vma;
2145 len = strlen (output_section_statement->name);
2146 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2151 while (len < SECTION_NAME_MAP_LENGTH)
2157 minfo ("0x%V %W", section->vma, section->_raw_size);
2159 if (output_section_statement->load_base != NULL)
2163 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2164 "load base", lang_final_phase_enum);
2165 minfo (_(" load address 0x%V"), addr);
2172 print_statement_list (output_section_statement->children.head,
2173 output_section_statement);
2177 print_assignment (lang_assignment_statement_type *assignment,
2178 lang_output_section_statement_type *output_section)
2181 etree_value_type result;
2183 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2186 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2187 lang_final_phase_enum, print_dot, &print_dot);
2193 value = result.value + result.section->bfd_section->vma;
2194 dst = assignment->exp->assign.dst;
2196 minfo ("0x%V", value);
2197 if (dst[0] == '.' && dst[1] == 0)
2210 exp_print_tree (assignment->exp);
2216 print_input_statement (lang_input_statement_type *statm)
2218 if (statm->filename != NULL)
2220 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2224 /* Print all symbols defined in a particular section. This is called
2225 via bfd_link_hash_traverse. */
2228 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2230 asection *sec = ptr;
2232 if ((hash_entry->type == bfd_link_hash_defined
2233 || hash_entry->type == bfd_link_hash_defweak)
2234 && sec == hash_entry->u.def.section)
2238 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2241 (hash_entry->u.def.value
2242 + hash_entry->u.def.section->output_offset
2243 + hash_entry->u.def.section->output_section->vma));
2245 minfo (" %T\n", hash_entry->root.string);
2251 /* Print information about an input section to the map file. */
2254 print_input_section (lang_input_section_type *in)
2256 asection *i = in->section;
2257 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2258 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2259 ldfile_output_machine);
2264 minfo ("%s", i->name);
2266 if (i->output_section != NULL)
2270 len = 1 + strlen (i->name);
2271 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2276 while (len < SECTION_NAME_MAP_LENGTH)
2282 minfo ("0x%V %W %B\n",
2283 i->output_section->vma + i->output_offset, size / opb,
2286 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2288 len = SECTION_NAME_MAP_LENGTH + 3;
2300 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2303 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2305 print_dot = i->output_section->vma + i->output_offset + size / opb;
2311 print_fill_statement (lang_fill_statement_type *fill)
2315 fputs (" FILL mask 0x", config.map_file);
2316 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2317 fprintf (config.map_file, "%02x", *p);
2318 fputs ("\n", config.map_file);
2322 print_data_statement (lang_data_statement_type *data)
2328 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2329 ldfile_output_machine);
2331 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2334 addr = data->output_vma;
2335 if (data->output_section != NULL)
2336 addr += data->output_section->vma;
2364 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2366 if (data->exp->type.node_class != etree_value)
2369 exp_print_tree (data->exp);
2374 print_dot = addr + size / opb;
2378 /* Print an address statement. These are generated by options like
2382 print_address_statement (lang_address_statement_type *address)
2384 minfo (_("Address of section %s set to "), address->section_name);
2385 exp_print_tree (address->address);
2389 /* Print a reloc statement. */
2392 print_reloc_statement (lang_reloc_statement_type *reloc)
2397 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2398 ldfile_output_machine);
2400 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2403 addr = reloc->output_vma;
2404 if (reloc->output_section != NULL)
2405 addr += reloc->output_section->vma;
2407 size = bfd_get_reloc_size (reloc->howto);
2409 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2411 if (reloc->name != NULL)
2412 minfo ("%s+", reloc->name);
2414 minfo ("%s+", reloc->section->name);
2416 exp_print_tree (reloc->addend_exp);
2420 print_dot = addr + size / opb;
2424 print_padding_statement (lang_padding_statement_type *s)
2428 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2429 ldfile_output_machine);
2433 len = sizeof " *fill*" - 1;
2434 while (len < SECTION_NAME_MAP_LENGTH)
2440 addr = s->output_offset;
2441 if (s->output_section != NULL)
2442 addr += s->output_section->vma;
2443 minfo ("0x%V %W ", addr, s->size);
2445 if (s->fill->size != 0)
2449 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2450 fprintf (config.map_file, "%02x", *p);
2455 print_dot = addr + s->size / opb;
2459 print_wild_statement (lang_wild_statement_type *w,
2460 lang_output_section_statement_type *os)
2462 struct wildcard_list *sec;
2466 if (w->filenames_sorted)
2468 if (w->filename != NULL)
2469 minfo ("%s", w->filename);
2472 if (w->filenames_sorted)
2476 for (sec = w->section_list; sec; sec = sec->next)
2478 if (sec->spec.sorted)
2480 if (sec->spec.exclude_name_list != NULL)
2483 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2484 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2485 minfo (" %s", tmp->name);
2488 if (sec->spec.name != NULL)
2489 minfo ("%s", sec->spec.name);
2492 if (sec->spec.sorted)
2501 print_statement_list (w->children.head, os);
2504 /* Print a group statement. */
2507 print_group (lang_group_statement_type *s,
2508 lang_output_section_statement_type *os)
2510 fprintf (config.map_file, "START GROUP\n");
2511 print_statement_list (s->children.head, os);
2512 fprintf (config.map_file, "END GROUP\n");
2515 /* Print the list of statements in S.
2516 This can be called for any statement type. */
2519 print_statement_list (lang_statement_union_type *s,
2520 lang_output_section_statement_type *os)
2524 print_statement (s, os);
2529 /* Print the first statement in statement list S.
2530 This can be called for any statement type. */
2533 print_statement (lang_statement_union_type *s,
2534 lang_output_section_statement_type *os)
2536 switch (s->header.type)
2539 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2542 case lang_constructors_statement_enum:
2543 if (constructor_list.head != NULL)
2545 if (constructors_sorted)
2546 minfo (" SORT (CONSTRUCTORS)\n");
2548 minfo (" CONSTRUCTORS\n");
2549 print_statement_list (constructor_list.head, os);
2552 case lang_wild_statement_enum:
2553 print_wild_statement (&s->wild_statement, os);
2555 case lang_address_statement_enum:
2556 print_address_statement (&s->address_statement);
2558 case lang_object_symbols_statement_enum:
2559 minfo (" CREATE_OBJECT_SYMBOLS\n");
2561 case lang_fill_statement_enum:
2562 print_fill_statement (&s->fill_statement);
2564 case lang_data_statement_enum:
2565 print_data_statement (&s->data_statement);
2567 case lang_reloc_statement_enum:
2568 print_reloc_statement (&s->reloc_statement);
2570 case lang_input_section_enum:
2571 print_input_section (&s->input_section);
2573 case lang_padding_statement_enum:
2574 print_padding_statement (&s->padding_statement);
2576 case lang_output_section_statement_enum:
2577 print_output_section_statement (&s->output_section_statement);
2579 case lang_assignment_statement_enum:
2580 print_assignment (&s->assignment_statement, os);
2582 case lang_target_statement_enum:
2583 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2585 case lang_output_statement_enum:
2586 minfo ("OUTPUT(%s", s->output_statement.name);
2587 if (output_target != NULL)
2588 minfo (" %s", output_target);
2591 case lang_input_statement_enum:
2592 print_input_statement (&s->input_statement);
2594 case lang_group_statement_enum:
2595 print_group (&s->group_statement, os);
2597 case lang_afile_asection_pair_statement_enum:
2604 print_statements (void)
2606 print_statement_list (statement_list.head, abs_output_section);
2609 /* Print the first N statements in statement list S to STDERR.
2610 If N == 0, nothing is printed.
2611 If N < 0, the entire list is printed.
2612 Intended to be called from GDB. */
2615 dprint_statement (lang_statement_union_type *s, int n)
2617 FILE *map_save = config.map_file;
2619 config.map_file = stderr;
2622 print_statement_list (s, abs_output_section);
2625 while (s && --n >= 0)
2627 print_statement (s, abs_output_section);
2632 config.map_file = map_save;
2636 insert_pad (lang_statement_union_type **ptr,
2638 unsigned int alignment_needed,
2639 asection *output_section,
2642 static fill_type zero_fill = { 1, { 0 } };
2643 lang_statement_union_type *pad;
2645 pad = ((lang_statement_union_type *)
2646 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2647 if (ptr != &statement_list.head
2648 && pad->header.type == lang_padding_statement_enum
2649 && pad->padding_statement.output_section == output_section)
2651 /* Use the existing pad statement. The above test on output
2652 section is probably redundant, but it doesn't hurt to check. */
2656 /* Make a new padding statement, linked into existing chain. */
2657 pad = stat_alloc (sizeof (lang_padding_statement_type));
2658 pad->header.next = *ptr;
2660 pad->header.type = lang_padding_statement_enum;
2661 pad->padding_statement.output_section = output_section;
2664 pad->padding_statement.fill = fill;
2666 pad->padding_statement.output_offset = dot - output_section->vma;
2667 pad->padding_statement.size = alignment_needed;
2668 output_section->_raw_size += alignment_needed;
2671 /* Work out how much this section will move the dot point. */
2674 size_input_section (lang_statement_union_type **this_ptr,
2675 lang_output_section_statement_type *output_section_statement,
2679 lang_input_section_type *is = &((*this_ptr)->input_section);
2680 asection *i = is->section;
2682 if (!is->ifile->just_syms_flag)
2684 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2685 ldfile_output_machine);
2686 unsigned int alignment_needed;
2689 /* Align this section first to the input sections requirement,
2690 then to the output section's requirement. If this alignment
2691 is greater than any seen before, then record it too. Perform
2692 the alignment by inserting a magic 'padding' statement. */
2694 if (output_section_statement->subsection_alignment != -1)
2695 i->alignment_power = output_section_statement->subsection_alignment;
2697 o = output_section_statement->bfd_section;
2698 if (o->alignment_power < i->alignment_power)
2699 o->alignment_power = i->alignment_power;
2701 alignment_needed = align_power (dot, i->alignment_power) - dot;
2703 if (alignment_needed != 0)
2705 insert_pad (this_ptr, fill, alignment_needed * opb, o, dot);
2706 dot += alignment_needed;
2709 /* Remember where in the output section this input section goes. */
2711 i->output_offset = dot - o->vma;
2713 /* Mark how big the output section must be to contain this now. */
2714 if (i->_cooked_size != 0)
2715 dot += i->_cooked_size / opb;
2717 dot += i->_raw_size / opb;
2718 o->_raw_size = (dot - o->vma) * opb;
2722 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2728 #define IGNORE_SECTION(bfd, s) \
2729 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2730 != (SEC_ALLOC | SEC_LOAD)) \
2731 || bfd_section_size (bfd, s) == 0)
2733 /* Check to see if any allocated sections overlap with other allocated
2734 sections. This can happen when the linker script specifically specifies
2735 the output section addresses of the two sections. */
2738 lang_check_section_addresses (void)
2741 unsigned opb = bfd_octets_per_byte (output_bfd);
2743 /* Scan all sections in the output list. */
2744 for (s = output_bfd->sections; s != NULL; s = s->next)
2748 /* Ignore sections which are not loaded or which have no contents. */
2749 if (IGNORE_SECTION (output_bfd, s))
2752 /* Once we reach section 's' stop our seach. This prevents two
2753 warning messages from being produced, one for 'section A overlaps
2754 section B' and one for 'section B overlaps section A'. */
2755 for (os = output_bfd->sections; os != s; os = os->next)
2762 /* Only consider loadable sections with real contents. */
2763 if (IGNORE_SECTION (output_bfd, os))
2766 /* We must check the sections' LMA addresses not their
2767 VMA addresses because overlay sections can have
2768 overlapping VMAs but they must have distinct LMAs. */
2769 s_start = bfd_section_lma (output_bfd, s);
2770 os_start = bfd_section_lma (output_bfd, os);
2771 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2772 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2774 /* Look for an overlap. */
2775 if ((s_end < os_start) || (s_start > os_end))
2779 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2780 s->name, s_start, s_end, os->name, os_start, os_end);
2782 /* Once we have found one overlap for this section,
2783 stop looking for others. */
2789 /* Make sure the new address is within the region. We explicitly permit the
2790 current address to be at the exact end of the region when the address is
2791 non-zero, in case the region is at the end of addressable memory and the
2792 calculation wraps around. */
2795 os_region_check (lang_output_section_statement_type *os,
2796 struct memory_region_struct *region,
2800 if ((region->current < region->origin
2801 || (region->current - region->origin > region->length))
2802 && ((region->current != region->origin + region->length)
2807 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2809 os->bfd_section->owner,
2810 os->bfd_section->name,
2815 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2817 os->bfd_section->owner,
2818 os->bfd_section->name);
2820 /* Reset the region pointer. */
2821 region->current = region->origin;
2825 /* Set the sizes for all the output sections. */
2828 lang_size_sections_1
2829 (lang_statement_union_type *s,
2830 lang_output_section_statement_type *output_section_statement,
2831 lang_statement_union_type **prev,
2835 bfd_boolean check_regions)
2837 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2838 ldfile_output_machine);
2840 /* Size up the sections from their constituent parts. */
2841 for (; s != NULL; s = s->header.next)
2843 switch (s->header.type)
2845 case lang_output_section_statement_enum:
2848 lang_output_section_statement_type *os;
2850 os = &s->output_section_statement;
2851 if (os->bfd_section == NULL)
2852 /* This section was never actually created. */
2855 /* If this is a COFF shared library section, use the size and
2856 address from the input section. FIXME: This is COFF
2857 specific; it would be cleaner if there were some other way
2858 to do this, but nothing simple comes to mind. */
2859 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2863 if (os->children.head == NULL
2864 || os->children.head->header.next != NULL
2865 || os->children.head->header.type != lang_input_section_enum)
2866 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2869 input = os->children.head->input_section.section;
2870 bfd_set_section_vma (os->bfd_section->owner,
2872 bfd_section_vma (input->owner, input));
2873 os->bfd_section->_raw_size = input->_raw_size;
2877 if (bfd_is_abs_section (os->bfd_section))
2879 /* No matter what happens, an abs section starts at zero. */
2880 ASSERT (os->bfd_section->vma == 0);
2884 if (os->addr_tree == NULL)
2886 /* No address specified for this section, get one
2887 from the region specification. */
2888 if (os->region == NULL
2889 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2890 & (SEC_ALLOC | SEC_LOAD)) != 0)
2891 && os->region->name[0] == '*'
2892 && strcmp (os->region->name, "*default*") == 0))
2894 os->region = lang_memory_default (os->bfd_section);
2897 /* If a loadable section is using the default memory
2898 region, and some non default memory regions were
2899 defined, issue an error message. */
2900 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2901 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2902 & SEC_NEVER_LOAD) == 0
2903 && ! link_info.relocatable
2905 && strcmp (os->region->name, "*default*") == 0
2906 && lang_memory_region_list != NULL
2907 && (strcmp (lang_memory_region_list->name,
2909 || lang_memory_region_list->next != NULL))
2911 /* By default this is an error rather than just a
2912 warning because if we allocate the section to the
2913 default memory region we can end up creating an
2914 excessivly large binary, or even seg faulting when
2915 attmepting to perform a negative seek. See
2916 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2917 for an example of this. This behaviour can be
2918 overridden by the using the --no-check-sections
2920 if (command_line.check_section_addresses)
2921 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2922 bfd_get_section_name (output_bfd,
2925 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2926 bfd_get_section_name (output_bfd,
2930 dot = os->region->current;
2932 if (os->section_alignment == -1)
2937 dot = align_power (dot,
2938 os->bfd_section->alignment_power);
2940 if (dot != olddot && config.warn_section_align)
2941 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2942 os->name, (unsigned int) (dot - olddot));
2949 r = exp_fold_tree (os->addr_tree,
2951 lang_allocating_phase_enum,
2954 einfo (_("%F%S: non constant address expression for section %s\n"),
2957 dot = r.value + r.section->bfd_section->vma;
2960 /* The section starts here.
2961 First, align to what the section needs. */
2963 if (os->section_alignment != -1)
2964 dot = align_power (dot, os->section_alignment);
2966 bfd_set_section_vma (0, os->bfd_section, dot);
2968 os->bfd_section->output_offset = 0;
2971 lang_size_sections_1 (os->children.head, os, &os->children.head,
2972 os->fill, dot, relax, check_regions);
2974 /* Put the section within the requested block size, or
2975 align at the block boundary. */
2976 after = align_n (os->bfd_section->vma
2977 + os->bfd_section->_raw_size / opb,
2978 (bfd_vma) os->block_value);
2980 if (bfd_is_abs_section (os->bfd_section))
2981 ASSERT (after == os->bfd_section->vma);
2982 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
2983 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
2984 && ! link_info.relocatable)
2985 os->bfd_section->_raw_size = 0;
2987 os->bfd_section->_raw_size =
2988 (after - os->bfd_section->vma) * opb;
2990 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
2991 os->processed = TRUE;
2993 if (os->update_dot_tree != 0)
2994 exp_fold_tree (os->update_dot_tree, abs_output_section,
2995 lang_allocating_phase_enum, dot, &dot);
2997 /* Update dot in the region ?
2998 We only do this if the section is going to be allocated,
2999 since unallocated sections do not contribute to the region's
3000 overall size in memory.
3002 If the SEC_NEVER_LOAD bit is not set, it will affect the
3003 addresses of sections after it. We have to update
3005 if (os->region != NULL
3006 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3007 & SEC_NEVER_LOAD) == 0
3008 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3009 & (SEC_ALLOC | SEC_LOAD))))
3011 os->region->current = dot;
3014 /* Make sure the new address is within the region. */
3015 os_region_check (os, os->region, os->addr_tree,
3016 os->bfd_section->vma);
3018 /* If there's no load address specified, use the run
3019 region as the load region. */
3020 if (os->lma_region == NULL && os->load_base == NULL)
3021 os->lma_region = os->region;
3023 if (os->lma_region != NULL && os->lma_region != os->region)
3025 /* Set load_base, which will be handled later. */
3026 os->load_base = exp_intop (os->lma_region->current);
3027 os->lma_region->current +=
3028 os->bfd_section->_raw_size / opb;
3030 os_region_check (os, os->lma_region, NULL,
3031 os->bfd_section->lma);
3037 case lang_constructors_statement_enum:
3038 dot = lang_size_sections_1 (constructor_list.head,
3039 output_section_statement,
3040 &s->wild_statement.children.head,
3041 fill, dot, relax, check_regions);
3044 case lang_data_statement_enum:
3046 unsigned int size = 0;
3048 s->data_statement.output_vma =
3049 dot - output_section_statement->bfd_section->vma;
3050 s->data_statement.output_section =
3051 output_section_statement->bfd_section;
3053 switch (s->data_statement.type)
3074 output_section_statement->bfd_section->_raw_size += size;
3075 /* The output section gets contents, and then we inspect for
3076 any flags set in the input script which override any ALLOC. */
3077 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3078 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3080 output_section_statement->bfd_section->flags |=
3081 SEC_ALLOC | SEC_LOAD;
3086 case lang_reloc_statement_enum:
3090 s->reloc_statement.output_vma =
3091 dot - output_section_statement->bfd_section->vma;
3092 s->reloc_statement.output_section =
3093 output_section_statement->bfd_section;
3094 size = bfd_get_reloc_size (s->reloc_statement.howto);
3096 output_section_statement->bfd_section->_raw_size += size;
3100 case lang_wild_statement_enum:
3102 dot = lang_size_sections_1 (s->wild_statement.children.head,
3103 output_section_statement,
3104 &s->wild_statement.children.head,
3105 fill, dot, relax, check_regions);
3109 case lang_object_symbols_statement_enum:
3110 link_info.create_object_symbols_section =
3111 output_section_statement->bfd_section;
3113 case lang_output_statement_enum:
3114 case lang_target_statement_enum:
3116 case lang_input_section_enum:
3120 i = (*prev)->input_section.section;
3123 if (i->_cooked_size == 0)
3124 i->_cooked_size = i->_raw_size;
3130 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3131 einfo (_("%P%F: can't relax section: %E\n"));
3135 dot = size_input_section (prev, output_section_statement,
3136 output_section_statement->fill, dot);
3139 case lang_input_statement_enum:
3141 case lang_fill_statement_enum:
3142 s->fill_statement.output_section =
3143 output_section_statement->bfd_section;
3145 fill = s->fill_statement.fill;
3147 case lang_assignment_statement_enum:
3149 bfd_vma newdot = dot;
3151 exp_fold_tree (s->assignment_statement.exp,
3152 output_section_statement,
3153 lang_allocating_phase_enum,
3159 if (output_section_statement == abs_output_section)
3161 /* If we don't have an output section, then just adjust
3162 the default memory address. */
3163 lang_memory_region_lookup ("*default*")->current = newdot;
3167 /* Insert a pad after this statement. We can't
3168 put the pad before when relaxing, in case the
3169 assignment references dot. */
3170 insert_pad (&s->header.next, fill, (newdot - dot) * opb,
3171 output_section_statement->bfd_section, dot);
3173 /* Don't neuter the pad below when relaxing. */
3182 case lang_padding_statement_enum:
3183 /* If this is the first time lang_size_sections is called,
3184 we won't have any padding statements. If this is the
3185 second or later passes when relaxing, we should allow
3186 padding to shrink. If padding is needed on this pass, it
3187 will be added back in. */
3188 s->padding_statement.size = 0;
3190 /* Make sure output_offset is valid. If relaxation shrinks
3191 the section and this pad isn't needed, it's possible to
3192 have output_offset larger than the final size of the
3193 section. bfd_set_section_contents will complain even for
3194 a pad size of zero. */
3195 s->padding_statement.output_offset
3196 = dot - output_section_statement->bfd_section->vma;
3199 case lang_group_statement_enum:
3200 dot = lang_size_sections_1 (s->group_statement.children.head,
3201 output_section_statement,
3202 &s->group_statement.children.head,
3203 fill, dot, relax, check_regions);
3210 /* We can only get here when relaxing is turned on. */
3211 case lang_address_statement_enum:
3214 prev = &s->header.next;
3221 (lang_statement_union_type *s,
3222 lang_output_section_statement_type *output_section_statement,
3223 lang_statement_union_type **prev,
3227 bfd_boolean check_regions)
3232 /* Callers of exp_fold_tree need to increment this. */
3233 lang_statement_iteration++;
3235 exp_data_seg.phase = exp_dataseg_none;
3236 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3237 dot, relax, check_regions);
3238 if (exp_data_seg.phase == exp_dataseg_end_seen)
3240 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3241 a page could be saved in the data segment. */
3242 bfd_vma first, last;
3244 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3245 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3247 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3248 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3249 && first + last <= exp_data_seg.pagesize)
3251 exp_data_seg.phase = exp_dataseg_adjust;
3252 result = lang_size_sections_1 (s, output_section_statement, prev,
3253 fill, dot, relax, check_regions);
3257 /* Some backend relaxers want to refer to the output section size. Give
3258 them a section size that does not change on the next call while they
3259 relax. We can't set this at top because lang_reset_memory_regions
3260 which is called before we get here, sets _raw_size to 0 on relaxing
3262 for (o = output_bfd->sections; o != NULL; o = o->next)
3263 o->_cooked_size = o->_raw_size;
3268 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3271 lang_do_assignments_1
3272 (lang_statement_union_type *s,
3273 lang_output_section_statement_type *output_section_statement,
3277 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3278 ldfile_output_machine);
3280 for (; s != NULL; s = s->header.next)
3282 switch (s->header.type)
3284 case lang_constructors_statement_enum:
3285 dot = lang_do_assignments_1 (constructor_list.head,
3286 output_section_statement,
3291 case lang_output_section_statement_enum:
3293 lang_output_section_statement_type *os;
3295 os = &(s->output_section_statement);
3296 if (os->bfd_section != NULL)
3298 dot = os->bfd_section->vma;
3299 (void) lang_do_assignments_1 (os->children.head, os,
3301 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3306 /* If nothing has been placed into the output section then
3307 it won't have a bfd_section. */
3308 if (os->bfd_section)
3310 os->bfd_section->lma
3311 = exp_get_abs_int (os->load_base, 0, "load base",
3312 lang_final_phase_enum);
3317 case lang_wild_statement_enum:
3319 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3320 output_section_statement,
3325 case lang_object_symbols_statement_enum:
3326 case lang_output_statement_enum:
3327 case lang_target_statement_enum:
3329 case lang_common_statement_enum:
3332 case lang_data_statement_enum:
3334 etree_value_type value;
3336 value = exp_fold_tree (s->data_statement.exp,
3338 lang_final_phase_enum, dot, &dot);
3339 s->data_statement.value = value.value;
3341 einfo (_("%F%P: invalid data statement\n"));
3345 switch (s->data_statement.type)
3369 case lang_reloc_statement_enum:
3371 etree_value_type value;
3373 value = exp_fold_tree (s->reloc_statement.addend_exp,
3375 lang_final_phase_enum, dot, &dot);
3376 s->reloc_statement.addend_value = value.value;
3378 einfo (_("%F%P: invalid reloc statement\n"));
3380 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
3383 case lang_input_section_enum:
3385 asection *in = s->input_section.section;
3387 if (in->_cooked_size != 0)
3388 dot += in->_cooked_size / opb;
3390 dot += in->_raw_size / opb;
3394 case lang_input_statement_enum:
3396 case lang_fill_statement_enum:
3397 fill = s->fill_statement.fill;
3399 case lang_assignment_statement_enum:
3401 exp_fold_tree (s->assignment_statement.exp,
3402 output_section_statement,
3403 lang_final_phase_enum,
3409 case lang_padding_statement_enum:
3410 dot += s->padding_statement.size / opb;
3413 case lang_group_statement_enum:
3414 dot = lang_do_assignments_1 (s->group_statement.children.head,
3415 output_section_statement,
3423 case lang_address_statement_enum:
3432 lang_do_assignments (lang_statement_union_type *s,
3433 lang_output_section_statement_type
3434 *output_section_statement,
3438 /* Callers of exp_fold_tree need to increment this. */
3439 lang_statement_iteration++;
3440 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3443 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3444 operator .startof. (section_name), it produces an undefined symbol
3445 .startof.section_name. Similarly, when it sees
3446 .sizeof. (section_name), it produces an undefined symbol
3447 .sizeof.section_name. For all the output sections, we look for
3448 such symbols, and set them to the correct value. */
3451 lang_set_startof (void)
3455 if (link_info.relocatable)
3458 for (s = output_bfd->sections; s != NULL; s = s->next)
3460 const char *secname;
3462 struct bfd_link_hash_entry *h;
3464 secname = bfd_get_section_name (output_bfd, s);
3465 buf = xmalloc (10 + strlen (secname));
3467 sprintf (buf, ".startof.%s", secname);
3468 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3469 if (h != NULL && h->type == bfd_link_hash_undefined)
3471 h->type = bfd_link_hash_defined;
3472 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3473 h->u.def.section = bfd_abs_section_ptr;
3476 sprintf (buf, ".sizeof.%s", secname);
3477 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3478 if (h != NULL && h->type == bfd_link_hash_undefined)
3482 opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3483 ldfile_output_machine);
3484 h->type = bfd_link_hash_defined;
3485 if (s->_cooked_size != 0)
3486 h->u.def.value = s->_cooked_size / opb;
3488 h->u.def.value = s->_raw_size / opb;
3489 h->u.def.section = bfd_abs_section_ptr;
3499 struct bfd_link_hash_entry *h;
3502 if (link_info.relocatable || link_info.shared)
3507 if (entry_symbol.name == NULL)
3509 /* No entry has been specified. Look for start, but don't warn
3510 if we don't find it. */
3511 entry_symbol.name = "start";
3515 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3516 FALSE, FALSE, TRUE);
3518 && (h->type == bfd_link_hash_defined
3519 || h->type == bfd_link_hash_defweak)
3520 && h->u.def.section->output_section != NULL)
3524 val = (h->u.def.value
3525 + bfd_get_section_vma (output_bfd,
3526 h->u.def.section->output_section)
3527 + h->u.def.section->output_offset);
3528 if (! bfd_set_start_address (output_bfd, val))
3529 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3536 /* We couldn't find the entry symbol. Try parsing it as a
3538 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3541 if (! bfd_set_start_address (output_bfd, val))
3542 einfo (_("%P%F: can't set start address\n"));
3548 /* Can't find the entry symbol, and it's not a number. Use
3549 the first address in the text section. */
3550 ts = bfd_get_section_by_name (output_bfd, entry_section);
3554 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3556 bfd_get_section_vma (output_bfd, ts));
3557 if (! bfd_set_start_address (output_bfd,
3558 bfd_get_section_vma (output_bfd,
3560 einfo (_("%P%F: can't set start address\n"));
3565 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3571 bfd_hash_table_free (&lang_definedness_table);
3574 /* This is a small function used when we want to ignore errors from
3578 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3580 /* Don't do anything. */
3583 /* Check that the architecture of all the input files is compatible
3584 with the output file. Also call the backend to let it do any
3585 other checking that is needed. */
3590 lang_statement_union_type *file;
3592 const bfd_arch_info_type *compatible;
3594 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3596 input_bfd = file->input_statement.the_bfd;
3597 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3598 command_line.accept_unknown_input_arch);
3600 /* In general it is not possible to perform a relocatable
3601 link between differing object formats when the input
3602 file has relocations, because the relocations in the
3603 input format may not have equivalent representations in
3604 the output format (and besides BFD does not translate
3605 relocs for other link purposes than a final link). */
3606 if ((link_info.relocatable || link_info.emitrelocations)
3607 && (compatible == NULL
3608 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3609 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3611 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3612 bfd_get_target (input_bfd), input_bfd,
3613 bfd_get_target (output_bfd), output_bfd);
3614 /* einfo with %F exits. */
3617 if (compatible == NULL)
3619 if (command_line.warn_mismatch)
3620 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3621 bfd_printable_name (input_bfd), input_bfd,
3622 bfd_printable_name (output_bfd));
3624 else if (bfd_count_sections (input_bfd))
3626 /* If the input bfd has no contents, it shouldn't set the
3627 private data of the output bfd. */
3629 bfd_error_handler_type pfn = NULL;
3631 /* If we aren't supposed to warn about mismatched input
3632 files, temporarily set the BFD error handler to a
3633 function which will do nothing. We still want to call
3634 bfd_merge_private_bfd_data, since it may set up
3635 information which is needed in the output file. */
3636 if (! command_line.warn_mismatch)
3637 pfn = bfd_set_error_handler (ignore_bfd_errors);
3638 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3640 if (command_line.warn_mismatch)
3641 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3644 if (! command_line.warn_mismatch)
3645 bfd_set_error_handler (pfn);
3650 /* Look through all the global common symbols and attach them to the
3651 correct section. The -sort-common command line switch may be used
3652 to roughly sort the entries by size. */
3657 if (command_line.inhibit_common_definition)
3659 if (link_info.relocatable
3660 && ! command_line.force_common_definition)
3663 if (! config.sort_common)
3664 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3669 for (power = 4; power >= 0; power--)
3670 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3674 /* Place one common symbol in the correct section. */
3677 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3679 unsigned int power_of_two;
3682 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3683 ldfile_output_machine);
3685 if (h->type != bfd_link_hash_common)
3689 power_of_two = h->u.c.p->alignment_power;
3691 if (config.sort_common
3692 && power_of_two < (unsigned int) *(int *) info)
3695 section = h->u.c.p->section;
3697 /* Increase the size of the section. */
3698 section->_cooked_size = align_n ((section->_cooked_size + opb - 1) / opb,
3699 (bfd_vma) 1 << power_of_two) * opb;
3701 /* Adjust the alignment if necessary. */
3702 if (power_of_two > section->alignment_power)
3703 section->alignment_power = power_of_two;
3705 /* Change the symbol from common to defined. */
3706 h->type = bfd_link_hash_defined;
3707 h->u.def.section = section;
3708 h->u.def.value = section->_cooked_size;
3710 /* Increase the size of the section. */
3711 section->_cooked_size += size;
3713 /* Make sure the section is allocated in memory, and make sure that
3714 it is no longer a common section. */
3715 section->flags |= SEC_ALLOC;
3716 section->flags &= ~SEC_IS_COMMON;
3718 if (config.map_file != NULL)
3720 static bfd_boolean header_printed;
3725 if (! header_printed)
3727 minfo (_("\nAllocating common symbols\n"));
3728 minfo (_("Common symbol size file\n\n"));
3729 header_printed = TRUE;
3732 name = demangle (h->root.string);
3734 len = strlen (name);
3749 if (size <= 0xffffffff)
3750 sprintf (buf, "%lx", (unsigned long) size);
3752 sprintf_vma (buf, size);
3762 minfo ("%B\n", section->owner);
3768 /* Run through the input files and ensure that every input section has
3769 somewhere to go. If one is found without a destination then create
3770 an input request and place it into the statement tree. */
3773 lang_place_orphans (void)
3775 LANG_FOR_EACH_INPUT_STATEMENT (file)
3779 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3781 if (s->output_section == NULL)
3783 /* This section of the file is not attached, root
3784 around for a sensible place for it to go. */
3786 if (file->just_syms_flag)
3790 else if (strcmp (s->name, "COMMON") == 0)
3792 /* This is a lonely common section which must have
3793 come from an archive. We attach to the section
3794 with the wildcard. */
3795 if (! link_info.relocatable
3796 || command_line.force_common_definition)
3798 if (default_common_section == NULL)
3801 /* This message happens when using the
3802 svr3.ifile linker script, so I have
3804 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3806 default_common_section =
3807 lang_output_section_statement_lookup (".bss");
3810 lang_add_section (&default_common_section->children, s,
3811 default_common_section, file);
3814 else if (ldemul_place_orphan (file, s))
3818 lang_output_section_statement_type *os;
3820 os = lang_output_section_statement_lookup (s->name);
3821 lang_add_section (&os->children, s, os, file);
3829 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3831 flagword *ptr_flags;
3833 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3839 *ptr_flags |= SEC_ALLOC;
3843 *ptr_flags |= SEC_READONLY;
3847 *ptr_flags |= SEC_DATA;
3851 *ptr_flags |= SEC_CODE;
3856 *ptr_flags |= SEC_LOAD;
3860 einfo (_("%P%F: invalid syntax in flags\n"));
3867 /* Call a function on each input file. This function will be called
3868 on an archive, but not on the elements. */
3871 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3873 lang_input_statement_type *f;
3875 for (f = (lang_input_statement_type *) input_file_chain.head;
3877 f = (lang_input_statement_type *) f->next_real_file)
3881 /* Call a function on each file. The function will be called on all
3882 the elements of an archive which are included in the link, but will
3883 not be called on the archive file itself. */
3886 lang_for_each_file (void (*func) (lang_input_statement_type *))
3888 LANG_FOR_EACH_INPUT_STATEMENT (f)
3899 lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
3901 LANG_FOR_EACH_INPUT_STATEMENT (f)
3905 for (s = f->the_bfd->sections; s != NULL; s = s->next)
3906 func (f->the_bfd, s);
3913 ldlang_add_file (lang_input_statement_type *entry)
3917 lang_statement_append (&file_chain,
3918 (lang_statement_union_type *) entry,
3921 /* The BFD linker needs to have a list of all input BFDs involved in
3923 ASSERT (entry->the_bfd->link_next == NULL);
3924 ASSERT (entry->the_bfd != output_bfd);
3925 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3927 *pp = entry->the_bfd;
3928 entry->the_bfd->usrdata = entry;
3929 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3931 /* Look through the sections and check for any which should not be
3932 included in the link. We need to do this now, so that we can
3933 notice when the backend linker tries to report multiple
3934 definition errors for symbols which are in sections we aren't
3935 going to link. FIXME: It might be better to entirely ignore
3936 symbols which are defined in sections which are going to be
3937 discarded. This would require modifying the backend linker for
3938 each backend which might set the SEC_LINK_ONCE flag. If we do
3939 this, we should probably handle SEC_EXCLUDE in the same way. */
3941 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
3945 lang_add_output (const char *name, int from_script)
3947 /* Make -o on command line override OUTPUT in script. */
3948 if (!had_output_filename || !from_script)
3950 output_filename = name;
3951 had_output_filename = TRUE;
3955 static lang_output_section_statement_type *current_section;
3966 for (l = 0; l < 32; l++)
3968 if (i >= (unsigned int) x)
3976 lang_output_section_statement_type *
3977 lang_enter_output_section_statement (const char *output_section_statement_name,
3978 etree_type *address_exp,
3979 enum section_type sectype,
3980 bfd_vma block_value,
3982 etree_type *subalign,
3985 lang_output_section_statement_type *os;
3989 lang_output_section_statement_lookup (output_section_statement_name);
3991 /* Add this statement to tree. */
3993 add_statement (lang_output_section_statement_enum,
3994 output_section_statement);
3996 /* Make next things chain into subchain of this. */
3998 if (os->addr_tree == NULL)
4000 os->addr_tree = address_exp;
4002 os->sectype = sectype;
4003 if (sectype != noload_section)
4004 os->flags = SEC_NO_FLAGS;
4006 os->flags = SEC_NEVER_LOAD;
4007 os->block_value = block_value ? block_value : 1;
4008 stat_ptr = &os->children;
4010 os->subsection_alignment =
4011 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4012 os->section_alignment =
4013 topower (exp_get_value_int (align, -1, "section alignment", 0));
4015 os->load_base = ebase;
4022 lang_output_statement_type *new =
4023 new_stat (lang_output_statement, stat_ptr);
4025 new->name = output_filename;
4028 /* Reset the current counters in the regions. */
4031 lang_reset_memory_regions (void)
4033 lang_memory_region_type *p = lang_memory_region_list;
4036 for (p = lang_memory_region_list; p != NULL; p = p->next)
4038 p->old_length = (bfd_size_type) (p->current - p->origin);
4039 p->current = p->origin;
4042 for (o = output_bfd->sections; o != NULL; o = o->next)
4046 /* If the wild pattern was marked KEEP, the member sections
4047 should be as well. */
4050 gc_section_callback (lang_wild_statement_type *ptr,
4051 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4053 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4054 void *data ATTRIBUTE_UNUSED)
4056 if (ptr->keep_sections)
4057 section->flags |= SEC_KEEP;
4060 /* Handle a wild statement, marking it against GC. */
4063 lang_gc_wild (lang_wild_statement_type *s)
4065 walk_wild (s, gc_section_callback, NULL);
4068 /* Iterate over sections marking them against GC. */
4071 lang_gc_sections_1 (lang_statement_union_type *s)
4073 for (; s != NULL; s = s->header.next)
4075 switch (s->header.type)
4077 case lang_wild_statement_enum:
4078 lang_gc_wild (&s->wild_statement);
4080 case lang_constructors_statement_enum:
4081 lang_gc_sections_1 (constructor_list.head);
4083 case lang_output_section_statement_enum:
4084 lang_gc_sections_1 (s->output_section_statement.children.head);
4086 case lang_group_statement_enum:
4087 lang_gc_sections_1 (s->group_statement.children.head);
4096 lang_gc_sections (void)
4098 struct bfd_link_hash_entry *h;
4099 ldlang_undef_chain_list_type *ulist;
4101 /* Keep all sections so marked in the link script. */
4103 lang_gc_sections_1 (statement_list.head);
4105 /* Keep all sections containing symbols undefined on the command-line,
4106 and the section containing the entry symbol. */
4108 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4110 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4111 FALSE, FALSE, FALSE);
4114 && (h->type == bfd_link_hash_defined
4115 || h->type == bfd_link_hash_defweak)
4116 && ! bfd_is_abs_section (h->u.def.section))
4118 h->u.def.section->flags |= SEC_KEEP;
4122 bfd_gc_sections (output_bfd, &link_info);
4128 lang_reasonable_defaults ();
4129 current_target = default_target;
4131 /* Open the output file. */
4132 lang_for_each_statement (ldlang_open_output);
4134 ldemul_create_output_section_statements ();
4136 /* Add to the hash table all undefineds on the command line. */
4137 lang_place_undefineds ();
4139 already_linked_table_init ();
4141 /* Create a bfd for each input file. */
4142 current_target = default_target;
4143 open_input_bfds (statement_list.head, FALSE);
4145 link_info.gc_sym_list = &entry_symbol;
4146 if (entry_symbol.name == NULL)
4147 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4149 ldemul_after_open ();
4151 already_linked_table_free ();
4153 /* Make sure that we're not mixing architectures. We call this
4154 after all the input files have been opened, but before we do any
4155 other processing, so that any operations merge_private_bfd_data
4156 does on the output file will be known during the rest of the
4160 /* Handle .exports instead of a version script if we're told to do so. */
4161 if (command_line.version_exports_section)
4162 lang_do_version_exports_section ();
4164 /* Build all sets based on the information gathered from the input
4166 ldctor_build_sets ();
4168 /* Remove unreferenced sections if asked to. */
4169 if (command_line.gc_sections)
4170 lang_gc_sections ();
4172 /* If there were any SEC_MERGE sections, finish their merging, so that
4173 section sizes can be computed. This has to be done after GC of sections,
4174 so that GCed sections are not merged, but before assigning output
4175 sections, since removing whole input sections is hard then. */
4176 bfd_merge_sections (output_bfd, &link_info);
4178 /* Size up the common data. */
4181 /* Run through the contours of the script and attach input sections
4182 to the correct output sections. */
4183 map_input_to_output_sections (statement_list.head, NULL, NULL);
4185 /* Find any sections not attached explicitly and handle them. */
4186 lang_place_orphans ();
4188 if (! link_info.relocatable)
4190 /* Look for a text section and set the readonly attribute in it. */
4191 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4195 if (config.text_read_only)
4196 found->flags |= SEC_READONLY;
4198 found->flags &= ~SEC_READONLY;
4202 /* Do anything special before sizing sections. This is where ELF
4203 and other back-ends size dynamic sections. */
4204 ldemul_before_allocation ();
4206 if (!link_info.relocatable)
4207 strip_excluded_output_sections ();
4209 /* We must record the program headers before we try to fix the
4210 section positions, since they will affect SIZEOF_HEADERS. */
4211 lang_record_phdrs ();
4213 /* Size up the sections. */
4214 lang_size_sections (statement_list.head, abs_output_section,
4215 &statement_list.head, 0, 0, NULL,
4216 command_line.relax ? FALSE : TRUE);
4218 /* Now run around and relax if we can. */
4219 if (command_line.relax)
4221 /* Keep relaxing until bfd_relax_section gives up. */
4222 bfd_boolean relax_again;
4226 lang_reset_memory_regions ();
4228 relax_again = FALSE;
4230 /* Note: pe-dll.c does something like this also. If you find
4231 you need to change this code, you probably need to change
4232 pe-dll.c also. DJ */
4234 /* Do all the assignments with our current guesses as to
4236 lang_do_assignments (statement_list.head, abs_output_section,
4239 /* Perform another relax pass - this time we know where the
4240 globals are, so can make a better guess. */
4241 lang_size_sections (statement_list.head, abs_output_section,
4242 &statement_list.head, 0, 0, &relax_again, FALSE);
4244 /* If the normal relax is done and the relax finalize pass
4245 is not performed yet, we perform another relax pass. */
4246 if (!relax_again && !link_info.relax_finalizing)
4248 link_info.relax_finalizing = TRUE;
4252 while (relax_again);
4254 /* Final extra sizing to report errors. */
4255 lang_reset_memory_regions ();
4256 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4257 lang_size_sections (statement_list.head, abs_output_section,
4258 &statement_list.head, 0, 0, NULL, TRUE);
4261 /* See if anything special should be done now we know how big
4263 ldemul_after_allocation ();
4265 /* Fix any .startof. or .sizeof. symbols. */
4266 lang_set_startof ();
4268 /* Do all the assignments, now that we know the final resting places
4269 of all the symbols. */
4271 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4273 /* Make sure that the section addresses make sense. */
4274 if (! link_info.relocatable
4275 && command_line.check_section_addresses)
4276 lang_check_section_addresses ();
4284 /* EXPORTED TO YACC */
4287 lang_add_wild (struct wildcard_spec *filespec,
4288 struct wildcard_list *section_list,
4289 bfd_boolean keep_sections)
4291 struct wildcard_list *curr, *next;
4292 lang_wild_statement_type *new;
4294 /* Reverse the list as the parser puts it back to front. */
4295 for (curr = section_list, section_list = NULL;
4297 section_list = curr, curr = next)
4299 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4300 placed_commons = TRUE;
4303 curr->next = section_list;
4306 if (filespec != NULL && filespec->name != NULL)
4308 if (strcmp (filespec->name, "*") == 0)
4309 filespec->name = NULL;
4310 else if (! wildcardp (filespec->name))
4311 lang_has_input_file = TRUE;
4314 new = new_stat (lang_wild_statement, stat_ptr);
4315 new->filename = NULL;
4316 new->filenames_sorted = FALSE;
4317 if (filespec != NULL)
4319 new->filename = filespec->name;
4320 new->filenames_sorted = filespec->sorted;
4322 new->section_list = section_list;
4323 new->keep_sections = keep_sections;
4324 lang_list_init (&new->children);
4328 lang_section_start (const char *name, etree_type *address)
4330 lang_address_statement_type *ad;
4332 ad = new_stat (lang_address_statement, stat_ptr);
4333 ad->section_name = name;
4334 ad->address = address;
4337 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4338 because of a -e argument on the command line, or zero if this is
4339 called by ENTRY in a linker script. Command line arguments take
4343 lang_add_entry (const char *name, bfd_boolean cmdline)
4345 if (entry_symbol.name == NULL
4347 || ! entry_from_cmdline)
4349 entry_symbol.name = name;
4350 entry_from_cmdline = cmdline;
4355 lang_add_target (const char *name)
4357 lang_target_statement_type *new = new_stat (lang_target_statement,
4365 lang_add_map (const char *name)
4372 map_option_f = TRUE;
4380 lang_add_fill (fill_type *fill)
4382 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4389 lang_add_data (int type, union etree_union *exp)
4392 lang_data_statement_type *new = new_stat (lang_data_statement,
4400 /* Create a new reloc statement. RELOC is the BFD relocation type to
4401 generate. HOWTO is the corresponding howto structure (we could
4402 look this up, but the caller has already done so). SECTION is the
4403 section to generate a reloc against, or NAME is the name of the
4404 symbol to generate a reloc against. Exactly one of SECTION and
4405 NAME must be NULL. ADDEND is an expression for the addend. */
4408 lang_add_reloc (bfd_reloc_code_real_type reloc,
4409 reloc_howto_type *howto,
4412 union etree_union *addend)
4414 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4418 p->section = section;
4420 p->addend_exp = addend;
4422 p->addend_value = 0;
4423 p->output_section = NULL;
4427 lang_assignment_statement_type *
4428 lang_add_assignment (etree_type *exp)
4430 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4438 lang_add_attribute (enum statement_enum attribute)
4440 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4444 lang_startup (const char *name)
4446 if (startup_file != NULL)
4448 einfo (_("%P%Fmultiple STARTUP files\n"));
4450 first_file->filename = name;
4451 first_file->local_sym_name = name;
4452 first_file->real = TRUE;
4454 startup_file = name;
4458 lang_float (bfd_boolean maybe)
4460 lang_float_flag = maybe;
4464 /* Work out the load- and run-time regions from a script statement, and
4465 store them in *LMA_REGION and *REGION respectively.
4467 MEMSPEC is the name of the run-time region, or "*default*" if the
4468 statement didn't specify one. LMA_MEMSPEC is the name of the
4469 load-time region, or null if the statement didn't specify one.
4470 HAVE_LMA_P is TRUE if the statement had an explicit load address.
4472 It is an error to specify both a load region and a load address. */
4475 lang_get_regions (struct memory_region_struct **region,
4476 struct memory_region_struct **lma_region,
4477 const char *memspec,
4478 const char *lma_memspec,
4481 *lma_region = lang_memory_region_lookup (lma_memspec);
4483 /* If no runtime region has been given, but the load region has
4484 been, use the load region. */
4485 if (lma_memspec != 0 && strcmp (memspec, "*default*") == 0)
4486 *region = *lma_region;
4488 *region = lang_memory_region_lookup (memspec);
4490 if (have_lma_p && lma_memspec != 0)
4491 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4495 lang_leave_output_section_statement
4496 (fill_type *fill, const char *memspec,
4497 struct lang_output_section_phdr_list *phdrs, const char *lma_memspec)
4499 lang_get_regions (¤t_section->region,
4500 ¤t_section->lma_region,
4501 memspec, lma_memspec,
4502 current_section->load_base != 0);
4503 current_section->fill = fill;
4504 current_section->phdrs = phdrs;
4505 stat_ptr = &statement_list;
4508 /* Create an absolute symbol with the given name with the value of the
4509 address of first byte of the section named.
4511 If the symbol already exists, then do nothing. */
4514 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4516 struct bfd_link_hash_entry *h;
4518 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4520 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4522 if (h->type == bfd_link_hash_new
4523 || h->type == bfd_link_hash_undefined)
4527 h->type = bfd_link_hash_defined;
4529 sec = bfd_get_section_by_name (output_bfd, secname);
4533 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4535 h->u.def.section = bfd_abs_section_ptr;
4539 /* Create an absolute symbol with the given name with the value of the
4540 address of the first byte after the end of the section named.
4542 If the symbol already exists, then do nothing. */
4545 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4547 struct bfd_link_hash_entry *h;
4549 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4551 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4553 if (h->type == bfd_link_hash_new
4554 || h->type == bfd_link_hash_undefined)
4558 h->type = bfd_link_hash_defined;
4560 sec = bfd_get_section_by_name (output_bfd, secname);
4564 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4565 + bfd_section_size (output_bfd, sec) /
4566 bfd_octets_per_byte (output_bfd));
4568 h->u.def.section = bfd_abs_section_ptr;
4573 lang_statement_append (lang_statement_list_type *list,
4574 lang_statement_union_type *element,
4575 lang_statement_union_type **field)
4577 *(list->tail) = element;
4581 /* Set the output format type. -oformat overrides scripts. */
4584 lang_add_output_format (const char *format,
4589 if (output_target == NULL || !from_script)
4591 if (command_line.endian == ENDIAN_BIG
4594 else if (command_line.endian == ENDIAN_LITTLE
4598 output_target = format;
4602 /* Enter a group. This creates a new lang_group_statement, and sets
4603 stat_ptr to build new statements within the group. */
4606 lang_enter_group (void)
4608 lang_group_statement_type *g;
4610 g = new_stat (lang_group_statement, stat_ptr);
4611 lang_list_init (&g->children);
4612 stat_ptr = &g->children;
4615 /* Leave a group. This just resets stat_ptr to start writing to the
4616 regular list of statements again. Note that this will not work if
4617 groups can occur inside anything else which can adjust stat_ptr,
4618 but currently they can't. */
4621 lang_leave_group (void)
4623 stat_ptr = &statement_list;
4626 /* Add a new program header. This is called for each entry in a PHDRS
4627 command in a linker script. */
4630 lang_new_phdr (const char *name,
4632 bfd_boolean filehdr,
4637 struct lang_phdr *n, **pp;
4639 n = stat_alloc (sizeof (struct lang_phdr));
4642 n->type = exp_get_value_int (type, 0, "program header type",
4643 lang_final_phase_enum);
4644 n->filehdr = filehdr;
4649 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4654 /* Record the program header information in the output BFD. FIXME: We
4655 should not be calling an ELF specific function here. */
4658 lang_record_phdrs (void)
4662 struct lang_output_section_phdr_list *last;
4663 struct lang_phdr *l;
4664 lang_statement_union_type *u;
4667 secs = xmalloc (alc * sizeof (asection *));
4669 for (l = lang_phdr_list; l != NULL; l = l->next)
4676 for (u = lang_output_section_statement.head;
4678 u = u->output_section_statement.next)
4680 lang_output_section_statement_type *os;
4681 struct lang_output_section_phdr_list *pl;
4683 os = &u->output_section_statement;
4690 if (os->sectype == noload_section
4691 || os->bfd_section == NULL
4692 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4697 if (os->bfd_section == NULL)
4700 for (; pl != NULL; pl = pl->next)
4702 if (strcmp (pl->name, l->name) == 0)
4707 secs = xrealloc (secs, alc * sizeof (asection *));
4709 secs[c] = os->bfd_section;
4716 if (l->flags == NULL)
4719 flags = exp_get_vma (l->flags, 0, "phdr flags",
4720 lang_final_phase_enum);
4725 at = exp_get_vma (l->at, 0, "phdr load address",
4726 lang_final_phase_enum);
4728 if (! bfd_record_phdr (output_bfd, l->type,
4729 l->flags != NULL, flags, l->at != NULL,
4730 at, l->filehdr, l->phdrs, c, secs))
4731 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4736 /* Make sure all the phdr assignments succeeded. */
4737 for (u = lang_output_section_statement.head;
4739 u = u->output_section_statement.next)
4741 struct lang_output_section_phdr_list *pl;
4743 if (u->output_section_statement.bfd_section == NULL)
4746 for (pl = u->output_section_statement.phdrs;
4749 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4750 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4751 u->output_section_statement.name, pl->name);
4755 /* Record a list of sections which may not be cross referenced. */
4758 lang_add_nocrossref (struct lang_nocrossref *l)
4760 struct lang_nocrossrefs *n;
4762 n = xmalloc (sizeof *n);
4763 n->next = nocrossref_list;
4765 nocrossref_list = n;
4767 /* Set notice_all so that we get informed about all symbols. */
4768 link_info.notice_all = TRUE;
4771 /* Overlay handling. We handle overlays with some static variables. */
4773 /* The overlay virtual address. */
4774 static etree_type *overlay_vma;
4775 /* And subsection alignment. */
4776 static etree_type *overlay_subalign;
4778 /* An expression for the maximum section size seen so far. */
4779 static etree_type *overlay_max;
4781 /* A list of all the sections in this overlay. */
4783 struct overlay_list {
4784 struct overlay_list *next;
4785 lang_output_section_statement_type *os;
4788 static struct overlay_list *overlay_list;
4790 /* Start handling an overlay. */
4793 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4795 /* The grammar should prevent nested overlays from occurring. */
4796 ASSERT (overlay_vma == NULL
4797 && overlay_subalign == NULL
4798 && overlay_max == NULL);
4800 overlay_vma = vma_expr;
4801 overlay_subalign = subalign;
4804 /* Start a section in an overlay. We handle this by calling
4805 lang_enter_output_section_statement with the correct VMA.
4806 lang_leave_overlay sets up the LMA and memory regions. */
4809 lang_enter_overlay_section (const char *name)
4811 struct overlay_list *n;
4814 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4815 0, 0, overlay_subalign, 0);
4817 /* If this is the first section, then base the VMA of future
4818 sections on this one. This will work correctly even if `.' is
4819 used in the addresses. */
4820 if (overlay_list == NULL)
4821 overlay_vma = exp_nameop (ADDR, name);
4823 /* Remember the section. */
4824 n = xmalloc (sizeof *n);
4825 n->os = current_section;
4826 n->next = overlay_list;
4829 size = exp_nameop (SIZEOF, name);
4831 /* Arrange to work out the maximum section end address. */
4832 if (overlay_max == NULL)
4835 overlay_max = exp_binop (MAX_K, overlay_max, size);
4838 /* Finish a section in an overlay. There isn't any special to do
4842 lang_leave_overlay_section (fill_type *fill,
4843 struct lang_output_section_phdr_list *phdrs)
4850 name = current_section->name;
4852 /* For now, assume that "*default*" is the run-time memory region and
4853 that no load-time region has been specified. It doesn't really
4854 matter what we say here, since lang_leave_overlay will override it. */
4855 lang_leave_output_section_statement (fill, "*default*", phdrs, 0);
4857 /* Define the magic symbols. */
4859 clean = xmalloc (strlen (name) + 1);
4861 for (s1 = name; *s1 != '\0'; s1++)
4862 if (ISALNUM (*s1) || *s1 == '_')
4866 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4867 sprintf (buf, "__load_start_%s", clean);
4868 lang_add_assignment (exp_assop ('=', buf,
4869 exp_nameop (LOADADDR, name)));
4871 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4872 sprintf (buf, "__load_stop_%s", clean);
4873 lang_add_assignment (exp_assop ('=', buf,
4875 exp_nameop (LOADADDR, name),
4876 exp_nameop (SIZEOF, name))));
4881 /* Finish an overlay. If there are any overlay wide settings, this
4882 looks through all the sections in the overlay and sets them. */
4885 lang_leave_overlay (etree_type *lma_expr,
4888 const char *memspec,
4889 struct lang_output_section_phdr_list *phdrs,
4890 const char *lma_memspec)
4892 lang_memory_region_type *region;
4893 lang_memory_region_type *lma_region;
4894 struct overlay_list *l;
4895 struct lang_nocrossref *nocrossref;
4897 lang_get_regions (®ion, &lma_region,
4898 memspec, lma_memspec,
4903 /* After setting the size of the last section, set '.' to end of the
4905 if (overlay_list != NULL)
4906 overlay_list->os->update_dot_tree
4907 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4912 struct overlay_list *next;
4914 if (fill != NULL && l->os->fill == NULL)
4917 l->os->region = region;
4918 l->os->lma_region = lma_region;
4920 /* The first section has the load address specified in the
4921 OVERLAY statement. The rest are worked out from that.
4922 The base address is not needed (and should be null) if
4923 an LMA region was specified. */
4925 l->os->load_base = lma_expr;
4926 else if (lma_region == 0)
4927 l->os->load_base = exp_binop ('+',
4928 exp_nameop (LOADADDR, l->next->os->name),
4929 exp_nameop (SIZEOF, l->next->os->name));
4931 if (phdrs != NULL && l->os->phdrs == NULL)
4932 l->os->phdrs = phdrs;
4936 struct lang_nocrossref *nc;
4938 nc = xmalloc (sizeof *nc);
4939 nc->name = l->os->name;
4940 nc->next = nocrossref;
4949 if (nocrossref != NULL)
4950 lang_add_nocrossref (nocrossref);
4953 overlay_list = NULL;
4957 /* Version handling. This is only useful for ELF. */
4959 /* This global variable holds the version tree that we build. */
4961 struct bfd_elf_version_tree *lang_elf_version_info;
4964 lang_vers_match_lang_c (struct bfd_elf_version_expr *expr,
4967 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4969 return fnmatch (expr->pattern, sym, 0) == 0;
4973 lang_vers_match_lang_cplusplus (struct bfd_elf_version_expr *expr,
4979 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4982 alt_sym = cplus_demangle (sym, /* DMGL_NO_TPARAMS */ 0);
4985 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4986 Should we early out FALSE in this case? */
4987 result = fnmatch (expr->pattern, sym, 0) == 0;
4991 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4999 lang_vers_match_lang_java (struct bfd_elf_version_expr *expr,
5005 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5008 alt_sym = cplus_demangle (sym, DMGL_JAVA);
5011 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
5012 Should we early out FALSE in this case? */
5013 result = fnmatch (expr->pattern, sym, 0) == 0;
5017 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
5024 /* This is called for each variable name or match expression. */
5026 struct bfd_elf_version_expr *
5027 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5031 struct bfd_elf_version_expr *ret;
5033 ret = xmalloc (sizeof *ret);
5039 if (lang == NULL || strcasecmp (lang, "C") == 0)
5040 ret->match = lang_vers_match_lang_c;
5041 else if (strcasecmp (lang, "C++") == 0)
5042 ret->match = lang_vers_match_lang_cplusplus;
5043 else if (strcasecmp (lang, "Java") == 0)
5044 ret->match = lang_vers_match_lang_java;
5047 einfo (_("%X%P: unknown language `%s' in version information\n"),
5049 ret->match = lang_vers_match_lang_c;
5052 return ldemul_new_vers_pattern (ret);
5055 /* This is called for each set of variable names and match
5058 struct bfd_elf_version_tree *
5059 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5060 struct bfd_elf_version_expr *locals)
5062 struct bfd_elf_version_tree *ret;
5064 ret = xmalloc (sizeof *ret);
5068 ret->globals = globals;
5069 ret->locals = locals;
5071 ret->name_indx = (unsigned int) -1;
5076 /* This static variable keeps track of version indices. */
5078 static int version_index;
5080 /* This is called when we know the name and dependencies of the
5084 lang_register_vers_node (const char *name,
5085 struct bfd_elf_version_tree *version,
5086 struct bfd_elf_version_deps *deps)
5088 struct bfd_elf_version_tree *t, **pp;
5089 struct bfd_elf_version_expr *e1;
5094 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5095 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5097 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5102 /* Make sure this node has a unique name. */
5103 for (t = lang_elf_version_info; t != NULL; t = t->next)
5104 if (strcmp (t->name, name) == 0)
5105 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5107 /* Check the global and local match names, and make sure there
5108 aren't any duplicates. */
5110 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
5112 for (t = lang_elf_version_info; t != NULL; t = t->next)
5114 struct bfd_elf_version_expr *e2;
5116 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
5117 if (strcmp (e1->pattern, e2->pattern) == 0)
5118 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5123 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
5125 for (t = lang_elf_version_info; t != NULL; t = t->next)
5127 struct bfd_elf_version_expr *e2;
5129 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
5130 if (strcmp (e1->pattern, e2->pattern) == 0)
5131 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5136 version->deps = deps;
5137 version->name = name;
5138 if (name[0] != '\0')
5141 version->vernum = version_index;
5144 version->vernum = 0;
5146 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5151 /* This is called when we see a version dependency. */
5153 struct bfd_elf_version_deps *
5154 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5156 struct bfd_elf_version_deps *ret;
5157 struct bfd_elf_version_tree *t;
5159 ret = xmalloc (sizeof *ret);
5162 for (t = lang_elf_version_info; t != NULL; t = t->next)
5164 if (strcmp (t->name, name) == 0)
5166 ret->version_needed = t;
5171 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5177 lang_do_version_exports_section (void)
5179 struct bfd_elf_version_expr *greg = NULL, *lreg;
5181 LANG_FOR_EACH_INPUT_STATEMENT (is)
5183 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5190 len = bfd_section_size (is->the_bfd, sec);
5191 contents = xmalloc (len);
5192 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5193 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5196 while (p < contents + len)
5198 greg = lang_new_vers_pattern (greg, p, NULL);
5199 p = strchr (p, '\0') + 1;
5202 /* Do not free the contents, as we used them creating the regex. */
5204 /* Do not include this section in the link. */
5205 bfd_set_section_flags (is->the_bfd, sec,
5206 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5209 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5210 lang_register_vers_node (command_line.version_exports_section,
5211 lang_new_vers_node (greg, lreg), NULL);
5215 lang_add_unique (const char *name)
5217 struct unique_sections *ent;
5219 for (ent = unique_section_list; ent; ent = ent->next)
5220 if (strcmp (ent->name, name) == 0)
5223 ent = xmalloc (sizeof *ent);
5224 ent->name = xstrdup (name);
5225 ent->next = unique_section_list;
5226 unique_section_list = ent;