1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static bfd_boolean wildcardp (const char *);
71 static lang_input_statement_type *lookup_name (const char *);
72 static bfd_boolean load_symbols (lang_input_statement_type *,
73 lang_statement_list_type *);
74 static struct bfd_hash_entry *lang_definedness_newfunc
75 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
76 static void insert_undefined (const char *);
77 static void print_all_symbols (asection *);
78 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
79 static void print_statement (lang_statement_union_type *,
80 lang_output_section_statement_type *);
81 static void print_statement_list (lang_statement_union_type *,
82 lang_output_section_statement_type *);
83 static void print_statements (void);
84 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
85 static void lang_record_phdrs (void);
86 static void lang_do_version_exports_section (void);
88 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
89 asection *, lang_input_statement_type *, void *);
91 /* Exported variables. */
92 lang_output_section_statement_type *abs_output_section;
93 lang_statement_list_type lang_output_section_statement;
94 lang_statement_list_type *stat_ptr = &statement_list;
95 lang_statement_list_type file_chain = { NULL, NULL };
96 struct bfd_sym_chain entry_symbol = { NULL, NULL };
97 const char *entry_section = ".text";
98 bfd_boolean entry_from_cmdline;
99 bfd_boolean lang_has_input_file = FALSE;
100 bfd_boolean had_output_filename = FALSE;
101 bfd_boolean lang_float_flag = FALSE;
102 bfd_boolean delete_output_file_on_failure = FALSE;
103 struct lang_nocrossrefs *nocrossref_list;
104 struct unique_sections *unique_section_list;
105 static bfd_boolean ldlang_sysrooted_script = FALSE;
106 int lang_statement_iteration = 0;
108 etree_type *base; /* Relocation base - or null */
110 #define new_stat(x, y) \
111 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
113 #define outside_section_address(q) \
114 ((q)->output_offset + (q)->output_section->vma)
116 #define outside_symbol_address(q) \
117 ((q)->value + outside_section_address (q->section))
119 #define SECTION_NAME_MAP_LENGTH (16)
122 stat_alloc (size_t size)
124 return obstack_alloc (&stat_obstack, size);
128 unique_section_p (const asection *sec)
130 struct unique_sections *unam;
133 if (link_info.relocatable
134 && sec->owner != NULL
135 && bfd_is_group_section (sec->owner, sec))
139 for (unam = unique_section_list; unam; unam = unam->next)
140 if (wildcardp (unam->name)
141 ? fnmatch (unam->name, secnam, 0) == 0
142 : strcmp (unam->name, secnam) == 0)
150 /* Generic traversal routines for finding matching sections. */
153 walk_wild_section (lang_wild_statement_type *ptr,
154 lang_input_statement_type *file,
160 if (file->just_syms_flag)
163 for (s = file->the_bfd->sections; s != NULL; s = s->next)
165 struct wildcard_list *sec;
167 sec = ptr->section_list;
169 (*callback) (ptr, sec, s, file, data);
173 bfd_boolean skip = FALSE;
174 struct name_list *list_tmp;
176 /* Don't process sections from files which were
178 for (list_tmp = sec->spec.exclude_name_list;
180 list_tmp = list_tmp->next)
182 if (wildcardp (list_tmp->name))
183 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
185 skip = strcmp (list_tmp->name, file->filename) == 0;
187 /* If this file is part of an archive, and the archive is
188 excluded, exclude this file. */
189 if (! skip && file->the_bfd != NULL
190 && file->the_bfd->my_archive != NULL
191 && file->the_bfd->my_archive->filename != NULL)
193 if (wildcardp (list_tmp->name))
194 skip = fnmatch (list_tmp->name,
195 file->the_bfd->my_archive->filename,
198 skip = strcmp (list_tmp->name,
199 file->the_bfd->my_archive->filename) == 0;
206 if (!skip && sec->spec.name != NULL)
208 const char *sname = bfd_get_section_name (file->the_bfd, s);
210 if (wildcardp (sec->spec.name))
211 skip = fnmatch (sec->spec.name, sname, 0) != 0;
213 skip = strcmp (sec->spec.name, sname) != 0;
217 (*callback) (ptr, sec, s, file, data);
224 /* Handle a wild statement for a single file F. */
227 walk_wild_file (lang_wild_statement_type *s,
228 lang_input_statement_type *f,
232 if (f->the_bfd == NULL
233 || ! bfd_check_format (f->the_bfd, bfd_archive))
234 walk_wild_section (s, f, callback, data);
239 /* This is an archive file. We must map each member of the
240 archive separately. */
241 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
242 while (member != NULL)
244 /* When lookup_name is called, it will call the add_symbols
245 entry point for the archive. For each element of the
246 archive which is included, BFD will call ldlang_add_file,
247 which will set the usrdata field of the member to the
248 lang_input_statement. */
249 if (member->usrdata != NULL)
251 walk_wild_section (s, member->usrdata, callback, data);
254 member = bfd_openr_next_archived_file (f->the_bfd, member);
260 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
262 const char *file_spec = s->filename;
264 if (file_spec == NULL)
266 /* Perform the iteration over all files in the list. */
267 LANG_FOR_EACH_INPUT_STATEMENT (f)
269 walk_wild_file (s, f, callback, data);
272 else if (wildcardp (file_spec))
274 LANG_FOR_EACH_INPUT_STATEMENT (f)
276 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
277 walk_wild_file (s, f, callback, data);
282 lang_input_statement_type *f;
284 /* Perform the iteration over a single file. */
285 f = lookup_name (file_spec);
287 walk_wild_file (s, f, callback, data);
291 /* lang_for_each_statement walks the parse tree and calls the provided
292 function for each node. */
295 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
296 lang_statement_union_type *s)
298 for (; s != NULL; s = s->header.next)
302 switch (s->header.type)
304 case lang_constructors_statement_enum:
305 lang_for_each_statement_worker (func, constructor_list.head);
307 case lang_output_section_statement_enum:
308 lang_for_each_statement_worker
309 (func, s->output_section_statement.children.head);
311 case lang_wild_statement_enum:
312 lang_for_each_statement_worker (func,
313 s->wild_statement.children.head);
315 case lang_group_statement_enum:
316 lang_for_each_statement_worker (func,
317 s->group_statement.children.head);
319 case lang_data_statement_enum:
320 case lang_reloc_statement_enum:
321 case lang_object_symbols_statement_enum:
322 case lang_output_statement_enum:
323 case lang_target_statement_enum:
324 case lang_input_section_enum:
325 case lang_input_statement_enum:
326 case lang_assignment_statement_enum:
327 case lang_padding_statement_enum:
328 case lang_address_statement_enum:
329 case lang_fill_statement_enum:
339 lang_for_each_statement (void (*func) (lang_statement_union_type *))
341 lang_for_each_statement_worker (func, statement_list.head);
344 /*----------------------------------------------------------------------*/
347 lang_list_init (lang_statement_list_type *list)
350 list->tail = &list->head;
353 /* Build a new statement node for the parse tree. */
355 static lang_statement_union_type *
356 new_statement (enum statement_enum type,
358 lang_statement_list_type *list)
360 lang_statement_union_type *new;
362 new = stat_alloc (size);
363 new->header.type = type;
364 new->header.next = NULL;
365 lang_statement_append (list, new, &new->header.next);
369 /* Build a new input file node for the language. There are several
370 ways in which we treat an input file, eg, we only look at symbols,
371 or prefix it with a -l etc.
373 We can be supplied with requests for input files more than once;
374 they may, for example be split over several lines like foo.o(.text)
375 foo.o(.data) etc, so when asked for a file we check that we haven't
376 got it already so we don't duplicate the bfd. */
378 static lang_input_statement_type *
379 new_afile (const char *name,
380 lang_input_file_enum_type file_type,
382 bfd_boolean add_to_list)
384 lang_input_statement_type *p;
387 p = new_stat (lang_input_statement, stat_ptr);
390 p = stat_alloc (sizeof (lang_input_statement_type));
391 p->header.next = NULL;
394 lang_has_input_file = TRUE;
396 p->sysrooted = FALSE;
399 case lang_input_file_is_symbols_only_enum:
401 p->is_archive = FALSE;
403 p->local_sym_name = name;
404 p->just_syms_flag = TRUE;
405 p->search_dirs_flag = FALSE;
407 case lang_input_file_is_fake_enum:
409 p->is_archive = FALSE;
411 p->local_sym_name = name;
412 p->just_syms_flag = FALSE;
413 p->search_dirs_flag = FALSE;
415 case lang_input_file_is_l_enum:
416 p->is_archive = TRUE;
419 p->local_sym_name = concat ("-l", name, NULL);
420 p->just_syms_flag = FALSE;
421 p->search_dirs_flag = TRUE;
423 case lang_input_file_is_marker_enum:
425 p->is_archive = FALSE;
427 p->local_sym_name = name;
428 p->just_syms_flag = FALSE;
429 p->search_dirs_flag = TRUE;
431 case lang_input_file_is_search_file_enum:
432 p->sysrooted = ldlang_sysrooted_script;
434 p->is_archive = FALSE;
436 p->local_sym_name = name;
437 p->just_syms_flag = FALSE;
438 p->search_dirs_flag = TRUE;
440 case lang_input_file_is_file_enum:
442 p->is_archive = FALSE;
444 p->local_sym_name = name;
445 p->just_syms_flag = FALSE;
446 p->search_dirs_flag = FALSE;
453 p->next_real_file = NULL;
456 p->dynamic = config.dynamic_link;
457 p->as_needed = as_needed;
458 p->whole_archive = whole_archive;
460 lang_statement_append (&input_file_chain,
461 (lang_statement_union_type *) p,
466 lang_input_statement_type *
467 lang_add_input_file (const char *name,
468 lang_input_file_enum_type file_type,
471 lang_has_input_file = TRUE;
472 return new_afile (name, file_type, target, TRUE);
475 /* Build enough state so that the parser can build its tree. */
480 obstack_begin (&stat_obstack, 1000);
482 stat_ptr = &statement_list;
484 lang_list_init (stat_ptr);
486 lang_list_init (&input_file_chain);
487 lang_list_init (&lang_output_section_statement);
488 lang_list_init (&file_chain);
489 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
492 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
494 abs_output_section->bfd_section = bfd_abs_section_ptr;
496 /* The value "3" is ad-hoc, somewhat related to the expected number of
497 DEFINED expressions in a linker script. For most default linker
498 scripts, there are none. Why a hash table then? Well, it's somewhat
499 simpler to re-use working machinery than using a linked list in terms
500 of code-complexity here in ld, besides the initialization which just
501 looks like other code here. */
502 if (bfd_hash_table_init_n (&lang_definedness_table,
503 lang_definedness_newfunc, 3) != TRUE)
504 einfo (_("%P%F: out of memory during initialization"));
506 /* Callers of exp_fold_tree need to increment this. */
507 lang_statement_iteration = 0;
510 /*----------------------------------------------------------------------
511 A region is an area of memory declared with the
512 MEMORY { name:org=exp, len=exp ... }
515 We maintain a list of all the regions here.
517 If no regions are specified in the script, then the default is used
518 which is created when looked up to be the entire data space.
520 If create is true we are creating a region inside a MEMORY block.
521 In this case it is probably an error to create a region that has
522 already been created. If we are not inside a MEMORY block it is
523 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
524 and so we issue a warning. */
526 static lang_memory_region_type *lang_memory_region_list;
527 static lang_memory_region_type **lang_memory_region_list_tail
528 = &lang_memory_region_list;
530 lang_memory_region_type *
531 lang_memory_region_lookup (const char *const name, bfd_boolean create)
533 lang_memory_region_type *p;
534 lang_memory_region_type *new;
536 /* NAME is NULL for LMA memspecs if no region was specified. */
540 for (p = lang_memory_region_list; p != NULL; p = p->next)
541 if (strcmp (p->name, name) == 0)
544 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
550 /* This code used to always use the first region in the list as the
551 default region. I changed it to instead use a region
552 encompassing all of memory as the default region. This permits
553 NOLOAD sections to work reasonably without requiring a region.
554 People should specify what region they mean, if they really want
556 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
558 if (lang_memory_region_list != NULL)
559 return lang_memory_region_list;
563 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
564 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
566 new = stat_alloc (sizeof (lang_memory_region_type));
568 new->name = xstrdup (name);
571 *lang_memory_region_list_tail = new;
572 lang_memory_region_list_tail = &new->next;
576 new->length = ~(bfd_size_type) 0;
578 new->had_full_message = FALSE;
583 static lang_memory_region_type *
584 lang_memory_default (asection *section)
586 lang_memory_region_type *p;
588 flagword sec_flags = section->flags;
590 /* Override SEC_DATA to mean a writable section. */
591 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
592 sec_flags |= SEC_DATA;
594 for (p = lang_memory_region_list; p != NULL; p = p->next)
596 if ((p->flags & sec_flags) != 0
597 && (p->not_flags & sec_flags) == 0)
602 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
605 static lang_output_section_statement_type *
606 lang_output_section_find_1 (const char *const name, int constraint)
608 lang_statement_union_type *u;
609 lang_output_section_statement_type *lookup;
611 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
613 lookup = &u->output_section_statement;
614 if (strcmp (name, lookup->name) == 0
615 && lookup->constraint != -1
616 && (constraint == 0 || constraint == lookup->constraint))
622 lang_output_section_statement_type *
623 lang_output_section_find (const char *const name)
625 return lang_output_section_find_1 (name, 0);
628 static lang_output_section_statement_type *
629 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
631 lang_output_section_statement_type *lookup;
633 lookup = lang_output_section_find_1 (name, constraint);
636 lookup = new_stat (lang_output_section_statement, stat_ptr);
637 lookup->region = NULL;
638 lookup->lma_region = NULL;
640 lookup->block_value = 1;
644 lookup->bfd_section = NULL;
645 lookup->processed = 0;
646 lookup->constraint = constraint;
647 lookup->sectype = normal_section;
648 lookup->addr_tree = NULL;
649 lang_list_init (&lookup->children);
651 lookup->memspec = NULL;
653 lookup->subsection_alignment = -1;
654 lookup->section_alignment = -1;
655 lookup->load_base = NULL;
656 lookup->update_dot_tree = NULL;
657 lookup->phdrs = NULL;
659 lang_statement_append (&lang_output_section_statement,
660 (lang_statement_union_type *) lookup,
666 lang_output_section_statement_type *
667 lang_output_section_statement_lookup (const char *const name)
669 return lang_output_section_statement_lookup_1 (name, 0);
673 lang_map_flags (flagword flag)
675 if (flag & SEC_ALLOC)
681 if (flag & SEC_READONLY)
694 lang_memory_region_type *m;
697 minfo (_("\nMemory Configuration\n\n"));
698 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
699 _("Name"), _("Origin"), _("Length"), _("Attributes"));
701 for (m = lang_memory_region_list; m != NULL; m = m->next)
706 fprintf (config.map_file, "%-16s ", m->name);
708 sprintf_vma (buf, m->origin);
709 minfo ("0x%s ", buf);
717 minfo ("0x%V", m->length);
718 if (m->flags || m->not_flags)
726 lang_map_flags (m->flags);
732 lang_map_flags (m->not_flags);
739 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
741 if (! command_line.reduce_memory_overheads)
743 obstack_begin (&map_obstack, 1000);
744 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
745 bfd_map_over_sections (p, init_map_userdata, 0);
746 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
752 init_map_userdata (abfd, sec, data)
753 bfd *abfd ATTRIBUTE_UNUSED;
755 void *data ATTRIBUTE_UNUSED;
757 fat_section_userdata_type *new_data
758 = ((fat_section_userdata_type *) (stat_alloc
759 (sizeof (fat_section_userdata_type))));
761 ASSERT (get_userdata (sec) == NULL);
762 get_userdata (sec) = new_data;
763 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
767 sort_def_symbol (hash_entry, info)
768 struct bfd_link_hash_entry *hash_entry;
769 void *info ATTRIBUTE_UNUSED;
771 if (hash_entry->type == bfd_link_hash_defined
772 || hash_entry->type == bfd_link_hash_defweak)
774 struct fat_user_section_struct *ud;
775 struct map_symbol_def *def;
777 ud = get_userdata (hash_entry->u.def.section);
780 /* ??? What do we have to do to initialize this beforehand? */
781 /* The first time we get here is bfd_abs_section... */
782 init_map_userdata (0, hash_entry->u.def.section, 0);
783 ud = get_userdata (hash_entry->u.def.section);
785 else if (!ud->map_symbol_def_tail)
786 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
787 def = obstack_alloc (&map_obstack, sizeof *def);
788 def->entry = hash_entry;
789 *ud->map_symbol_def_tail = def;
790 ud->map_symbol_def_tail = &def->next;
795 /* Initialize an output section. */
798 init_os (lang_output_section_statement_type *s)
800 lean_section_userdata_type *new;
802 if (s->bfd_section != NULL)
805 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
806 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
808 new = stat_alloc (SECTION_USERDATA_SIZE);
810 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
811 if (s->bfd_section == NULL)
812 s->bfd_section = bfd_make_section (output_bfd, s->name);
813 if (s->bfd_section == NULL)
815 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
816 output_bfd->xvec->name, s->name);
818 s->bfd_section->output_section = s->bfd_section;
820 /* We initialize an output sections output offset to minus its own
821 vma to allow us to output a section through itself. */
822 s->bfd_section->output_offset = 0;
823 get_userdata (s->bfd_section) = new;
825 /* If there is a base address, make sure that any sections it might
826 mention are initialized. */
827 if (s->addr_tree != NULL)
828 exp_init_os (s->addr_tree);
830 if (s->load_base != NULL)
831 exp_init_os (s->load_base);
834 /* Make sure that all output sections mentioned in an expression are
838 exp_init_os (etree_type *exp)
840 switch (exp->type.node_class)
843 exp_init_os (exp->assign.src);
847 exp_init_os (exp->binary.lhs);
848 exp_init_os (exp->binary.rhs);
852 exp_init_os (exp->trinary.cond);
853 exp_init_os (exp->trinary.lhs);
854 exp_init_os (exp->trinary.rhs);
858 exp_init_os (exp->assert_s.child);
862 exp_init_os (exp->unary.child);
866 switch (exp->type.node_code)
872 lang_output_section_statement_type *os;
874 os = lang_output_section_find (exp->name.name);
875 if (os != NULL && os->bfd_section == NULL)
886 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
887 once into the output. This routine checks each section, and
888 arrange to discard it if a section of the same name has already
889 been linked. If the section has COMDAT information, then it uses
890 that to decide whether the section should be included. This code
891 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
892 that is, it does not depend solely upon the section name.
893 section_already_linked is called via bfd_map_over_sections. */
895 /* This is the shape of the elements inside the already_linked hash
896 table. It maps a name onto a list of already_linked elements with
897 the same name. It's possible to get more than one element in a
898 list if the COMDAT sections have different names. */
900 struct already_linked_hash_entry
902 struct bfd_hash_entry root;
903 struct already_linked *entry;
906 struct already_linked
908 struct already_linked *next;
912 /* The hash table. */
914 static struct bfd_hash_table already_linked_table;
917 section_already_linked (bfd *abfd, asection *sec, void *data)
919 lang_input_statement_type *entry = data;
922 struct already_linked *l;
923 struct already_linked_hash_entry *already_linked_list;
925 /* If we are only reading symbols from this object, then we want to
926 discard all sections. */
927 if (entry->just_syms_flag)
929 bfd_link_just_syms (sec, &link_info);
934 if ((flags & SEC_LINK_ONCE) == 0)
937 /* FIXME: When doing a relocatable link, we may have trouble
938 copying relocations in other sections that refer to local symbols
939 in the section being discarded. Those relocations will have to
940 be converted somehow; as of this writing I'm not sure that any of
941 the backends handle that correctly.
943 It is tempting to instead not discard link once sections when
944 doing a relocatable link (technically, they should be discarded
945 whenever we are building constructors). However, that fails,
946 because the linker winds up combining all the link once sections
947 into a single large link once section, which defeats the purpose
948 of having link once sections in the first place.
950 Also, not merging link once sections in a relocatable link
951 causes trouble for MIPS ELF, which relies on link once semantics
952 to handle the .reginfo section correctly. */
954 name = bfd_get_section_name (abfd, sec);
956 already_linked_list =
957 ((struct already_linked_hash_entry *)
958 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
960 for (l = already_linked_list->entry; l != NULL; l = l->next)
962 if (sec->comdat == NULL
963 || l->sec->comdat == NULL
964 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
966 /* The section has already been linked. See if we should
968 switch (flags & SEC_LINK_DUPLICATES)
973 case SEC_LINK_DUPLICATES_DISCARD:
976 case SEC_LINK_DUPLICATES_ONE_ONLY:
977 if (sec->comdat == NULL)
978 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
981 einfo (_("%P: %B: warning: ignoring duplicate `%s'"
982 " section symbol `%s'\n"),
983 abfd, name, sec->comdat->name);
986 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
987 /* FIXME: We should really dig out the contents of both
988 sections and memcmp them. The COFF/PE spec says that
989 the Microsoft linker does not implement this
990 correctly, so I'm not going to bother doing it
993 case SEC_LINK_DUPLICATES_SAME_SIZE:
994 if (sec->size != l->sec->size)
995 einfo (_("%P: %B: warning: duplicate section `%s'"
996 " has different size\n"),
1001 /* Set the output_section field so that lang_add_section
1002 does not create a lang_input_section structure for this
1003 section. Since there might be a symbol in the section
1004 being discarded, we must retain a pointer to the section
1005 which we are really going to use. */
1006 sec->output_section = bfd_abs_section_ptr;
1007 sec->kept_section = l->sec;
1009 if (flags & SEC_GROUP)
1010 bfd_discard_group (abfd, sec);
1016 /* This is the first section with this name. Record it. Allocate
1017 the memory from the same obstack as the hash table is kept in. */
1019 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
1022 l->next = already_linked_list->entry;
1023 already_linked_list->entry = l;
1026 /* Support routines for the hash table used by section_already_linked,
1027 initialize the table, fill in an entry and remove the table. */
1029 static struct bfd_hash_entry *
1030 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
1031 struct bfd_hash_table *table,
1032 const char *string ATTRIBUTE_UNUSED)
1034 struct already_linked_hash_entry *ret =
1035 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1043 already_linked_table_init (void)
1045 if (! bfd_hash_table_init_n (&already_linked_table,
1046 already_linked_newfunc,
1048 einfo (_("%P%F: Failed to create hash table\n"));
1052 already_linked_table_free (void)
1054 bfd_hash_table_free (&already_linked_table);
1057 /* The wild routines.
1059 These expand statements like *(.text) and foo.o to a list of
1060 explicit actions, like foo.o(.text), bar.o(.text) and
1061 foo.o(.text, .data). */
1063 /* Return TRUE if the PATTERN argument is a wildcard pattern.
1064 Although backslashes are treated specially if a pattern contains
1065 wildcards, we do not consider the mere presence of a backslash to
1066 be enough to cause the pattern to be treated as a wildcard.
1067 That lets us handle DOS filenames more naturally. */
1070 wildcardp (const char *pattern)
1074 for (s = pattern; *s != '\0'; ++s)
1082 /* Add SECTION to the output section OUTPUT. Do this by creating a
1083 lang_input_section statement which is placed at PTR. FILE is the
1084 input file which holds SECTION. */
1087 lang_add_section (lang_statement_list_type *ptr,
1089 lang_output_section_statement_type *output,
1090 lang_input_statement_type *file)
1092 flagword flags = section->flags;
1093 bfd_boolean discard;
1095 /* Discard sections marked with SEC_EXCLUDE. */
1096 discard = (flags & SEC_EXCLUDE) != 0;
1098 /* Discard input sections which are assigned to a section named
1099 DISCARD_SECTION_NAME. */
1100 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1103 /* Discard debugging sections if we are stripping debugging
1105 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1106 && (flags & SEC_DEBUGGING) != 0)
1111 if (section->output_section == NULL)
1113 /* This prevents future calls from assigning this section. */
1114 section->output_section = bfd_abs_section_ptr;
1119 if (section->output_section == NULL)
1122 lang_input_section_type *new;
1125 if (output->bfd_section == NULL)
1128 first = ! output->bfd_section->linker_has_input;
1129 output->bfd_section->linker_has_input = 1;
1131 /* Add a section reference to the list. */
1132 new = new_stat (lang_input_section, ptr);
1134 new->section = section;
1136 section->output_section = output->bfd_section;
1138 flags = section->flags;
1140 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1141 to an output section, because we want to be able to include a
1142 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1143 section (I don't know why we want to do this, but we do).
1144 build_link_order in ldwrite.c handles this case by turning
1145 the embedded SEC_NEVER_LOAD section into a fill. */
1147 flags &= ~ SEC_NEVER_LOAD;
1149 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1150 already been processed. One reason to do this is that on pe
1151 format targets, .text$foo sections go into .text and it's odd
1152 to see .text with SEC_LINK_ONCE set. */
1154 if (! link_info.relocatable)
1155 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1157 /* If this is not the first input section, and the SEC_READONLY
1158 flag is not currently set, then don't set it just because the
1159 input section has it set. */
1161 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1162 flags &= ~ SEC_READONLY;
1164 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1166 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1167 != (flags & (SEC_MERGE | SEC_STRINGS))
1168 || ((flags & SEC_MERGE)
1169 && section->output_section->entsize != section->entsize)))
1171 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1172 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1175 section->output_section->flags |= flags;
1177 if (flags & SEC_MERGE)
1178 section->output_section->entsize = section->entsize;
1180 /* If SEC_READONLY is not set in the input section, then clear
1181 it from the output section. */
1182 if ((section->flags & SEC_READONLY) == 0)
1183 section->output_section->flags &= ~SEC_READONLY;
1185 switch (output->sectype)
1187 case normal_section:
1192 case overlay_section:
1193 output->bfd_section->flags &= ~SEC_ALLOC;
1195 case noload_section:
1196 output->bfd_section->flags &= ~SEC_LOAD;
1197 output->bfd_section->flags |= SEC_NEVER_LOAD;
1201 /* Copy over SEC_SMALL_DATA. */
1202 if (section->flags & SEC_SMALL_DATA)
1203 section->output_section->flags |= SEC_SMALL_DATA;
1205 if (section->alignment_power > output->bfd_section->alignment_power)
1206 output->bfd_section->alignment_power = section->alignment_power;
1208 /* If supplied an alignment, then force it. */
1209 if (output->section_alignment != -1)
1210 output->bfd_section->alignment_power = output->section_alignment;
1212 if (section->flags & SEC_BLOCK)
1214 section->output_section->flags |= SEC_BLOCK;
1215 /* FIXME: This value should really be obtained from the bfd... */
1216 output->block_value = 128;
1221 /* Handle wildcard sorting. This returns the lang_input_section which
1222 should follow the one we are going to create for SECTION and FILE,
1223 based on the sorting requirements of WILD. It returns NULL if the
1224 new section should just go at the end of the current list. */
1226 static lang_statement_union_type *
1227 wild_sort (lang_wild_statement_type *wild,
1228 struct wildcard_list *sec,
1229 lang_input_statement_type *file,
1232 const char *section_name;
1233 lang_statement_union_type *l;
1235 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1238 section_name = bfd_get_section_name (file->the_bfd, section);
1239 for (l = wild->children.head; l != NULL; l = l->header.next)
1241 lang_input_section_type *ls;
1243 if (l->header.type != lang_input_section_enum)
1245 ls = &l->input_section;
1247 /* Sorting by filename takes precedence over sorting by section
1250 if (wild->filenames_sorted)
1252 const char *fn, *ln;
1256 /* The PE support for the .idata section as generated by
1257 dlltool assumes that files will be sorted by the name of
1258 the archive and then the name of the file within the
1261 if (file->the_bfd != NULL
1262 && bfd_my_archive (file->the_bfd) != NULL)
1264 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1269 fn = file->filename;
1273 if (ls->ifile->the_bfd != NULL
1274 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1276 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1281 ln = ls->ifile->filename;
1285 i = strcmp (fn, ln);
1294 fn = file->filename;
1296 ln = ls->ifile->filename;
1298 i = strcmp (fn, ln);
1306 /* Here either the files are not sorted by name, or we are
1307 looking at the sections for this file. */
1309 if (sec != NULL && sec->spec.sorted)
1311 if (strcmp (section_name,
1312 bfd_get_section_name (ls->ifile->the_bfd,
1322 /* Expand a wild statement for a particular FILE. SECTION may be
1323 NULL, in which case it is a wild card. */
1326 output_section_callback (lang_wild_statement_type *ptr,
1327 struct wildcard_list *sec,
1329 lang_input_statement_type *file,
1332 lang_statement_union_type *before;
1334 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1335 if (unique_section_p (section))
1338 before = wild_sort (ptr, sec, file, section);
1340 /* Here BEFORE points to the lang_input_section which
1341 should follow the one we are about to add. If BEFORE
1342 is NULL, then the section should just go at the end
1343 of the current list. */
1346 lang_add_section (&ptr->children, section,
1347 (lang_output_section_statement_type *) output,
1351 lang_statement_list_type list;
1352 lang_statement_union_type **pp;
1354 lang_list_init (&list);
1355 lang_add_section (&list, section,
1356 (lang_output_section_statement_type *) output,
1359 /* If we are discarding the section, LIST.HEAD will
1361 if (list.head != NULL)
1363 ASSERT (list.head->header.next == NULL);
1365 for (pp = &ptr->children.head;
1367 pp = &(*pp)->header.next)
1368 ASSERT (*pp != NULL);
1370 list.head->header.next = *pp;
1376 /* Check if all sections in a wild statement for a particular FILE
1380 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1381 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1383 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1386 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1387 if (unique_section_p (section))
1390 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1391 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1394 /* This is passed a file name which must have been seen already and
1395 added to the statement tree. We will see if it has been opened
1396 already and had its symbols read. If not then we'll read it. */
1398 static lang_input_statement_type *
1399 lookup_name (const char *name)
1401 lang_input_statement_type *search;
1403 for (search = (lang_input_statement_type *) input_file_chain.head;
1405 search = (lang_input_statement_type *) search->next_real_file)
1407 /* Use the local_sym_name as the name of the file that has
1408 already been loaded as filename might have been transformed
1409 via the search directory lookup mechanism. */
1410 const char * filename = search->local_sym_name;
1412 if (filename == NULL && name == NULL)
1414 if (filename != NULL
1416 && strcmp (filename, name) == 0)
1421 search = new_afile (name, lang_input_file_is_search_file_enum,
1422 default_target, FALSE);
1424 /* If we have already added this file, or this file is not real
1425 (FIXME: can that ever actually happen?) or the name is NULL
1426 (FIXME: can that ever actually happen?) don't add this file. */
1429 || search->filename == NULL)
1432 if (! load_symbols (search, NULL))
1438 /* Get the symbols for an input file. */
1441 load_symbols (lang_input_statement_type *entry,
1442 lang_statement_list_type *place)
1449 ldfile_open_file (entry);
1451 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1452 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1455 lang_statement_list_type *hold;
1456 bfd_boolean bad_load = TRUE;
1457 bfd_boolean save_ldlang_sysrooted_script;
1459 err = bfd_get_error ();
1461 /* See if the emulation has some special knowledge. */
1462 if (ldemul_unrecognized_file (entry))
1465 if (err == bfd_error_file_ambiguously_recognized)
1469 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1470 einfo (_("%B: matching formats:"), entry->the_bfd);
1471 for (p = matching; *p != NULL; p++)
1475 else if (err != bfd_error_file_not_recognized
1477 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1481 bfd_close (entry->the_bfd);
1482 entry->the_bfd = NULL;
1484 /* Try to interpret the file as a linker script. */
1485 ldfile_open_command_file (entry->filename);
1489 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1490 ldlang_sysrooted_script = entry->sysrooted;
1492 ldfile_assumed_script = TRUE;
1493 parser_input = input_script;
1495 ldfile_assumed_script = FALSE;
1497 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1503 if (ldemul_recognized_file (entry))
1506 /* We don't call ldlang_add_file for an archive. Instead, the
1507 add_symbols entry point will call ldlang_add_file, via the
1508 add_archive_element callback, for each element of the archive
1510 switch (bfd_get_format (entry->the_bfd))
1516 ldlang_add_file (entry);
1517 if (trace_files || trace_file_tries)
1518 info_msg ("%I\n", entry);
1522 if (entry->whole_archive)
1525 bfd_boolean loaded = TRUE;
1529 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1534 if (! bfd_check_format (member, bfd_object))
1536 einfo (_("%F%B: member %B in archive is not an object\n"),
1537 entry->the_bfd, member);
1541 if (! ((*link_info.callbacks->add_archive_element)
1542 (&link_info, member, "--whole-archive")))
1545 if (! bfd_link_add_symbols (member, &link_info))
1547 einfo (_("%F%B: could not read symbols: %E\n"), member);
1552 entry->loaded = loaded;
1558 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1559 entry->loaded = TRUE;
1561 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1563 return entry->loaded;
1566 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1567 may be NULL, indicating that it is a wildcard. Separate
1568 lang_input_section statements are created for each part of the
1569 expansion; they are added after the wild statement S. OUTPUT is
1570 the output section. */
1573 wild (lang_wild_statement_type *s,
1574 const char *target ATTRIBUTE_UNUSED,
1575 lang_output_section_statement_type *output)
1577 struct wildcard_list *sec;
1579 walk_wild (s, output_section_callback, output);
1581 for (sec = s->section_list; sec != NULL; sec = sec->next)
1583 if (default_common_section != NULL)
1585 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1587 /* Remember the section that common is going to in case we
1588 later get something which doesn't know where to put it. */
1589 default_common_section = output;
1594 /* Return TRUE iff target is the sought target. */
1597 get_target (const bfd_target *target, void *data)
1599 const char *sought = data;
1601 return strcmp (target->name, sought) == 0;
1604 /* Like strcpy() but convert to lower case as well. */
1607 stricpy (char *dest, char *src)
1611 while ((c = *src++) != 0)
1612 *dest++ = TOLOWER (c);
1617 /* Remove the first occurrence of needle (if any) in haystack
1621 strcut (char *haystack, char *needle)
1623 haystack = strstr (haystack, needle);
1629 for (src = haystack + strlen (needle); *src;)
1630 *haystack++ = *src++;
1636 /* Compare two target format name strings.
1637 Return a value indicating how "similar" they are. */
1640 name_compare (char *first, char *second)
1646 copy1 = xmalloc (strlen (first) + 1);
1647 copy2 = xmalloc (strlen (second) + 1);
1649 /* Convert the names to lower case. */
1650 stricpy (copy1, first);
1651 stricpy (copy2, second);
1653 /* Remove size and endian strings from the name. */
1654 strcut (copy1, "big");
1655 strcut (copy1, "little");
1656 strcut (copy2, "big");
1657 strcut (copy2, "little");
1659 /* Return a value based on how many characters match,
1660 starting from the beginning. If both strings are
1661 the same then return 10 * their length. */
1662 for (result = 0; copy1[result] == copy2[result]; result++)
1663 if (copy1[result] == 0)
1675 /* Set by closest_target_match() below. */
1676 static const bfd_target *winner;
1678 /* Scan all the valid bfd targets looking for one that has the endianness
1679 requirement that was specified on the command line, and is the nearest
1680 match to the original output target. */
1683 closest_target_match (const bfd_target *target, void *data)
1685 const bfd_target *original = data;
1687 if (command_line.endian == ENDIAN_BIG
1688 && target->byteorder != BFD_ENDIAN_BIG)
1691 if (command_line.endian == ENDIAN_LITTLE
1692 && target->byteorder != BFD_ENDIAN_LITTLE)
1695 /* Must be the same flavour. */
1696 if (target->flavour != original->flavour)
1699 /* If we have not found a potential winner yet, then record this one. */
1706 /* Oh dear, we now have two potential candidates for a successful match.
1707 Compare their names and choose the better one. */
1708 if (name_compare (target->name, original->name)
1709 > name_compare (winner->name, original->name))
1712 /* Keep on searching until wqe have checked them all. */
1716 /* Return the BFD target format of the first input file. */
1719 get_first_input_target (void)
1721 char *target = NULL;
1723 LANG_FOR_EACH_INPUT_STATEMENT (s)
1725 if (s->header.type == lang_input_statement_enum
1728 ldfile_open_file (s);
1730 if (s->the_bfd != NULL
1731 && bfd_check_format (s->the_bfd, bfd_object))
1733 target = bfd_get_target (s->the_bfd);
1745 lang_get_output_target (void)
1749 /* Has the user told us which output format to use? */
1750 if (output_target != NULL)
1751 return output_target;
1753 /* No - has the current target been set to something other than
1755 if (current_target != default_target)
1756 return current_target;
1758 /* No - can we determine the format of the first input file? */
1759 target = get_first_input_target ();
1763 /* Failed - use the default output target. */
1764 return default_target;
1767 /* Open the output file. */
1770 open_output (const char *name)
1774 output_target = lang_get_output_target ();
1776 /* Has the user requested a particular endianness on the command
1778 if (command_line.endian != ENDIAN_UNSET)
1780 const bfd_target *target;
1781 enum bfd_endian desired_endian;
1783 /* Get the chosen target. */
1784 target = bfd_search_for_target (get_target, (void *) output_target);
1786 /* If the target is not supported, we cannot do anything. */
1789 if (command_line.endian == ENDIAN_BIG)
1790 desired_endian = BFD_ENDIAN_BIG;
1792 desired_endian = BFD_ENDIAN_LITTLE;
1794 /* See if the target has the wrong endianness. This should
1795 not happen if the linker script has provided big and
1796 little endian alternatives, but some scrips don't do
1798 if (target->byteorder != desired_endian)
1800 /* If it does, then see if the target provides
1801 an alternative with the correct endianness. */
1802 if (target->alternative_target != NULL
1803 && (target->alternative_target->byteorder == desired_endian))
1804 output_target = target->alternative_target->name;
1807 /* Try to find a target as similar as possible to
1808 the default target, but which has the desired
1809 endian characteristic. */
1810 bfd_search_for_target (closest_target_match,
1813 /* Oh dear - we could not find any targets that
1814 satisfy our requirements. */
1816 einfo (_("%P: warning: could not find any targets"
1817 " that match endianness requirement\n"));
1819 output_target = winner->name;
1825 output = bfd_openw (name, output_target);
1829 if (bfd_get_error () == bfd_error_invalid_target)
1830 einfo (_("%P%F: target %s not found\n"), output_target);
1832 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1835 delete_output_file_on_failure = TRUE;
1838 output->flags |= D_PAGED;
1841 if (! bfd_set_format (output, bfd_object))
1842 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1843 if (! bfd_set_arch_mach (output,
1844 ldfile_output_architecture,
1845 ldfile_output_machine))
1846 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1848 link_info.hash = bfd_link_hash_table_create (output);
1849 if (link_info.hash == NULL)
1850 einfo (_("%P%F: can not create link hash table: %E\n"));
1852 bfd_set_gp_size (output, g_switch_value);
1857 ldlang_open_output (lang_statement_union_type *statement)
1859 switch (statement->header.type)
1861 case lang_output_statement_enum:
1862 ASSERT (output_bfd == NULL);
1863 output_bfd = open_output (statement->output_statement.name);
1864 ldemul_set_output_arch ();
1865 if (config.magic_demand_paged && !link_info.relocatable)
1866 output_bfd->flags |= D_PAGED;
1868 output_bfd->flags &= ~D_PAGED;
1869 if (config.text_read_only)
1870 output_bfd->flags |= WP_TEXT;
1872 output_bfd->flags &= ~WP_TEXT;
1873 if (link_info.traditional_format)
1874 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1876 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1879 case lang_target_statement_enum:
1880 current_target = statement->target_statement.target;
1887 /* Convert between addresses in bytes and sizes in octets.
1888 For currently supported targets, octets_per_byte is always a power
1889 of two, so we can use shifts. */
1890 #define TO_ADDR(X) ((X) >> opb_shift)
1891 #define TO_SIZE(X) ((X) << opb_shift)
1893 /* Support the above. */
1894 static unsigned int opb_shift = 0;
1899 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1900 ldfile_output_machine);
1903 while ((x & 1) == 0)
1911 /* Open all the input files. */
1914 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1916 for (; s != NULL; s = s->header.next)
1918 switch (s->header.type)
1920 case lang_constructors_statement_enum:
1921 open_input_bfds (constructor_list.head, force);
1923 case lang_output_section_statement_enum:
1924 open_input_bfds (s->output_section_statement.children.head, force);
1926 case lang_wild_statement_enum:
1927 /* Maybe we should load the file's symbols. */
1928 if (s->wild_statement.filename
1929 && ! wildcardp (s->wild_statement.filename))
1930 lookup_name (s->wild_statement.filename);
1931 open_input_bfds (s->wild_statement.children.head, force);
1933 case lang_group_statement_enum:
1935 struct bfd_link_hash_entry *undefs;
1937 /* We must continually search the entries in the group
1938 until no new symbols are added to the list of undefined
1943 undefs = link_info.hash->undefs_tail;
1944 open_input_bfds (s->group_statement.children.head, TRUE);
1946 while (undefs != link_info.hash->undefs_tail);
1949 case lang_target_statement_enum:
1950 current_target = s->target_statement.target;
1952 case lang_input_statement_enum:
1953 if (s->input_statement.real)
1955 lang_statement_list_type add;
1957 s->input_statement.target = current_target;
1959 /* If we are being called from within a group, and this
1960 is an archive which has already been searched, then
1961 force it to be researched unless the whole archive
1962 has been loaded already. */
1964 && !s->input_statement.whole_archive
1965 && s->input_statement.loaded
1966 && bfd_check_format (s->input_statement.the_bfd,
1968 s->input_statement.loaded = FALSE;
1970 lang_list_init (&add);
1972 if (! load_symbols (&s->input_statement, &add))
1973 config.make_executable = FALSE;
1975 if (add.head != NULL)
1977 *add.tail = s->header.next;
1978 s->header.next = add.head;
1988 /* If there are [COMMONS] statements, put a wild one into the bss
1992 lang_reasonable_defaults (void)
1995 lang_output_section_statement_lookup (".text");
1996 lang_output_section_statement_lookup (".data");
1998 default_common_section = lang_output_section_statement_lookup (".bss");
2000 if (!placed_commons)
2002 lang_wild_statement_type *new =
2003 new_stat (lang_wild_statement,
2004 &default_common_section->children);
2006 new->section_name = "COMMON";
2007 new->filename = NULL;
2008 lang_list_init (&new->children);
2013 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2016 lang_track_definedness (const char *name)
2018 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2019 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2022 /* New-function for the definedness hash table. */
2024 static struct bfd_hash_entry *
2025 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2026 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2027 const char *name ATTRIBUTE_UNUSED)
2029 struct lang_definedness_hash_entry *ret
2030 = (struct lang_definedness_hash_entry *) entry;
2033 ret = (struct lang_definedness_hash_entry *)
2034 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2037 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2039 ret->iteration = -1;
2043 /* Return the iteration when the definition of NAME was last updated. A
2044 value of -1 means that the symbol is not defined in the linker script
2045 or the command line, but may be defined in the linker symbol table. */
2048 lang_symbol_definition_iteration (const char *name)
2050 struct lang_definedness_hash_entry *defentry
2051 = (struct lang_definedness_hash_entry *)
2052 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2054 /* We've already created this one on the presence of DEFINED in the
2055 script, so it can't be NULL unless something is borked elsewhere in
2057 if (defentry == NULL)
2060 return defentry->iteration;
2063 /* Update the definedness state of NAME. */
2066 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2068 struct lang_definedness_hash_entry *defentry
2069 = (struct lang_definedness_hash_entry *)
2070 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2072 /* We don't keep track of symbols not tested with DEFINED. */
2073 if (defentry == NULL)
2076 /* If the symbol was already defined, and not from an earlier statement
2077 iteration, don't update the definedness iteration, because that'd
2078 make the symbol seem defined in the linker script at this point, and
2079 it wasn't; it was defined in some object. If we do anyway, DEFINED
2080 would start to yield false before this point and the construct "sym =
2081 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2083 if (h->type != bfd_link_hash_undefined
2084 && h->type != bfd_link_hash_common
2085 && h->type != bfd_link_hash_new
2086 && defentry->iteration == -1)
2089 defentry->iteration = lang_statement_iteration;
2092 /* Add the supplied name to the symbol table as an undefined reference.
2093 This is a two step process as the symbol table doesn't even exist at
2094 the time the ld command line is processed. First we put the name
2095 on a list, then, once the output file has been opened, transfer the
2096 name to the symbol table. */
2098 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2100 #define ldlang_undef_chain_list_head entry_symbol.next
2103 ldlang_add_undef (const char *const name)
2105 ldlang_undef_chain_list_type *new =
2106 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2108 new->next = ldlang_undef_chain_list_head;
2109 ldlang_undef_chain_list_head = new;
2111 new->name = xstrdup (name);
2113 if (output_bfd != NULL)
2114 insert_undefined (new->name);
2117 /* Insert NAME as undefined in the symbol table. */
2120 insert_undefined (const char *name)
2122 struct bfd_link_hash_entry *h;
2124 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2126 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2127 if (h->type == bfd_link_hash_new)
2129 h->type = bfd_link_hash_undefined;
2130 h->u.undef.abfd = NULL;
2131 bfd_link_add_undef (link_info.hash, h);
2135 /* Run through the list of undefineds created above and place them
2136 into the linker hash table as undefined symbols belonging to the
2140 lang_place_undefineds (void)
2142 ldlang_undef_chain_list_type *ptr;
2144 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2145 insert_undefined (ptr->name);
2148 /* Check for all readonly or some readwrite sections. */
2151 check_input_sections
2152 (lang_statement_union_type *s,
2153 lang_output_section_statement_type *output_section_statement)
2155 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2157 switch (s->header.type)
2159 case lang_wild_statement_enum:
2160 walk_wild (&s->wild_statement, check_section_callback,
2161 output_section_statement);
2162 if (! output_section_statement->all_input_readonly)
2165 case lang_constructors_statement_enum:
2166 check_input_sections (constructor_list.head,
2167 output_section_statement);
2168 if (! output_section_statement->all_input_readonly)
2171 case lang_group_statement_enum:
2172 check_input_sections (s->group_statement.children.head,
2173 output_section_statement);
2174 if (! output_section_statement->all_input_readonly)
2183 /* Open input files and attach to output sections. */
2186 map_input_to_output_sections
2187 (lang_statement_union_type *s, const char *target,
2188 lang_output_section_statement_type *output_section_statement)
2190 for (; s != NULL; s = s->header.next)
2192 switch (s->header.type)
2194 case lang_wild_statement_enum:
2195 wild (&s->wild_statement, target, output_section_statement);
2197 case lang_constructors_statement_enum:
2198 map_input_to_output_sections (constructor_list.head,
2200 output_section_statement);
2202 case lang_output_section_statement_enum:
2203 if (s->output_section_statement.constraint)
2205 if (s->output_section_statement.constraint == -1)
2207 s->output_section_statement.all_input_readonly = TRUE;
2208 check_input_sections (s->output_section_statement.children.head,
2209 &s->output_section_statement);
2210 if ((s->output_section_statement.all_input_readonly
2211 && s->output_section_statement.constraint == ONLY_IF_RW)
2212 || (!s->output_section_statement.all_input_readonly
2213 && s->output_section_statement.constraint == ONLY_IF_RO))
2215 s->output_section_statement.constraint = -1;
2220 map_input_to_output_sections (s->output_section_statement.children.head,
2222 &s->output_section_statement);
2224 case lang_output_statement_enum:
2226 case lang_target_statement_enum:
2227 target = s->target_statement.target;
2229 case lang_group_statement_enum:
2230 map_input_to_output_sections (s->group_statement.children.head,
2232 output_section_statement);
2234 case lang_data_statement_enum:
2235 /* Make sure that any sections mentioned in the expression
2237 exp_init_os (s->data_statement.exp);
2239 case lang_fill_statement_enum:
2240 case lang_input_section_enum:
2241 case lang_object_symbols_statement_enum:
2242 case lang_reloc_statement_enum:
2243 case lang_padding_statement_enum:
2244 case lang_input_statement_enum:
2245 if (output_section_statement != NULL
2246 && output_section_statement->bfd_section == NULL)
2247 init_os (output_section_statement);
2249 case lang_assignment_statement_enum:
2250 if (output_section_statement != NULL
2251 && output_section_statement->bfd_section == NULL)
2252 init_os (output_section_statement);
2254 /* Make sure that any sections mentioned in the assignment
2256 exp_init_os (s->assignment_statement.exp);
2258 case lang_afile_asection_pair_statement_enum:
2261 case lang_address_statement_enum:
2262 /* Mark the specified section with the supplied address. */
2264 lang_output_section_statement_type *os =
2265 lang_output_section_statement_lookup
2266 (s->address_statement.section_name);
2268 if (os->bfd_section == NULL)
2270 os->addr_tree = s->address_statement.address;
2277 /* An output section might have been removed after its statement was
2278 added. For example, ldemul_before_allocation can remove dynamic
2279 sections if they turn out to be not needed. Clean them up here. */
2282 strip_excluded_output_sections (void)
2284 lang_statement_union_type *u;
2286 for (u = lang_output_section_statement.head;
2288 u = u->output_section_statement.next)
2290 lang_output_section_statement_type *os;
2293 os = &u->output_section_statement;
2294 if (os->constraint == -1)
2296 s = os->bfd_section;
2297 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2301 os->bfd_section = NULL;
2303 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2306 bfd_section_list_remove (output_bfd, p);
2307 output_bfd->section_count--;
2315 print_output_section_statement
2316 (lang_output_section_statement_type *output_section_statement)
2318 asection *section = output_section_statement->bfd_section;
2321 if (output_section_statement != abs_output_section)
2323 minfo ("\n%s", output_section_statement->name);
2325 if (section != NULL)
2327 print_dot = section->vma;
2329 len = strlen (output_section_statement->name);
2330 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2335 while (len < SECTION_NAME_MAP_LENGTH)
2341 minfo ("0x%V %W", section->vma, section->size);
2343 if (output_section_statement->load_base != NULL)
2347 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2348 "load base", lang_final_phase_enum);
2349 minfo (_(" load address 0x%V"), addr);
2356 print_statement_list (output_section_statement->children.head,
2357 output_section_statement);
2361 print_assignment (lang_assignment_statement_type *assignment,
2362 lang_output_section_statement_type *output_section)
2365 etree_value_type result;
2367 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2370 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2371 lang_final_phase_enum, print_dot, &print_dot);
2377 value = result.value + result.section->bfd_section->vma;
2378 dst = assignment->exp->assign.dst;
2380 minfo ("0x%V", value);
2381 if (dst[0] == '.' && dst[1] == 0)
2394 exp_print_tree (assignment->exp);
2400 print_input_statement (lang_input_statement_type *statm)
2402 if (statm->filename != NULL)
2404 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2408 /* Print all symbols defined in a particular section. This is called
2409 via bfd_link_hash_traverse, or by print_all_symbols. */
2412 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2414 asection *sec = ptr;
2416 if ((hash_entry->type == bfd_link_hash_defined
2417 || hash_entry->type == bfd_link_hash_defweak)
2418 && sec == hash_entry->u.def.section)
2422 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2425 (hash_entry->u.def.value
2426 + hash_entry->u.def.section->output_offset
2427 + hash_entry->u.def.section->output_section->vma));
2429 minfo (" %T\n", hash_entry->root.string);
2436 print_all_symbols (sec)
2439 struct fat_user_section_struct *ud = get_userdata (sec);
2440 struct map_symbol_def *def;
2442 *ud->map_symbol_def_tail = 0;
2443 for (def = ud->map_symbol_def_head; def; def = def->next)
2444 print_one_symbol (def->entry, sec);
2447 /* Print information about an input section to the map file. */
2450 print_input_section (lang_input_section_type *in)
2452 asection *i = in->section;
2453 bfd_size_type size = i->size;
2462 minfo ("%s", i->name);
2464 len = 1 + strlen (i->name);
2465 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2470 while (len < SECTION_NAME_MAP_LENGTH)
2476 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2477 addr = i->output_section->vma + i->output_offset;
2484 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2486 if (size != i->rawsize && i->rawsize != 0)
2488 len = SECTION_NAME_MAP_LENGTH + 3;
2500 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2503 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2505 if (command_line.reduce_memory_overheads)
2506 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2508 print_all_symbols (i);
2510 print_dot = addr + TO_ADDR (size);
2516 print_fill_statement (lang_fill_statement_type *fill)
2520 fputs (" FILL mask 0x", config.map_file);
2521 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2522 fprintf (config.map_file, "%02x", *p);
2523 fputs ("\n", config.map_file);
2527 print_data_statement (lang_data_statement_type *data)
2535 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2538 addr = data->output_vma;
2539 if (data->output_section != NULL)
2540 addr += data->output_section->vma;
2568 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2570 if (data->exp->type.node_class != etree_value)
2573 exp_print_tree (data->exp);
2578 print_dot = addr + TO_ADDR (size);
2581 /* Print an address statement. These are generated by options like
2585 print_address_statement (lang_address_statement_type *address)
2587 minfo (_("Address of section %s set to "), address->section_name);
2588 exp_print_tree (address->address);
2592 /* Print a reloc statement. */
2595 print_reloc_statement (lang_reloc_statement_type *reloc)
2602 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2605 addr = reloc->output_vma;
2606 if (reloc->output_section != NULL)
2607 addr += reloc->output_section->vma;
2609 size = bfd_get_reloc_size (reloc->howto);
2611 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2613 if (reloc->name != NULL)
2614 minfo ("%s+", reloc->name);
2616 minfo ("%s+", reloc->section->name);
2618 exp_print_tree (reloc->addend_exp);
2622 print_dot = addr + TO_ADDR (size);
2626 print_padding_statement (lang_padding_statement_type *s)
2634 len = sizeof " *fill*" - 1;
2635 while (len < SECTION_NAME_MAP_LENGTH)
2641 addr = s->output_offset;
2642 if (s->output_section != NULL)
2643 addr += s->output_section->vma;
2644 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2646 if (s->fill->size != 0)
2650 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2651 fprintf (config.map_file, "%02x", *p);
2656 print_dot = addr + TO_ADDR (s->size);
2660 print_wild_statement (lang_wild_statement_type *w,
2661 lang_output_section_statement_type *os)
2663 struct wildcard_list *sec;
2667 if (w->filenames_sorted)
2669 if (w->filename != NULL)
2670 minfo ("%s", w->filename);
2673 if (w->filenames_sorted)
2677 for (sec = w->section_list; sec; sec = sec->next)
2679 if (sec->spec.sorted)
2681 if (sec->spec.exclude_name_list != NULL)
2684 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2685 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2686 minfo (" %s", tmp->name);
2689 if (sec->spec.name != NULL)
2690 minfo ("%s", sec->spec.name);
2693 if (sec->spec.sorted)
2702 print_statement_list (w->children.head, os);
2705 /* Print a group statement. */
2708 print_group (lang_group_statement_type *s,
2709 lang_output_section_statement_type *os)
2711 fprintf (config.map_file, "START GROUP\n");
2712 print_statement_list (s->children.head, os);
2713 fprintf (config.map_file, "END GROUP\n");
2716 /* Print the list of statements in S.
2717 This can be called for any statement type. */
2720 print_statement_list (lang_statement_union_type *s,
2721 lang_output_section_statement_type *os)
2725 print_statement (s, os);
2730 /* Print the first statement in statement list S.
2731 This can be called for any statement type. */
2734 print_statement (lang_statement_union_type *s,
2735 lang_output_section_statement_type *os)
2737 switch (s->header.type)
2740 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2743 case lang_constructors_statement_enum:
2744 if (constructor_list.head != NULL)
2746 if (constructors_sorted)
2747 minfo (" SORT (CONSTRUCTORS)\n");
2749 minfo (" CONSTRUCTORS\n");
2750 print_statement_list (constructor_list.head, os);
2753 case lang_wild_statement_enum:
2754 print_wild_statement (&s->wild_statement, os);
2756 case lang_address_statement_enum:
2757 print_address_statement (&s->address_statement);
2759 case lang_object_symbols_statement_enum:
2760 minfo (" CREATE_OBJECT_SYMBOLS\n");
2762 case lang_fill_statement_enum:
2763 print_fill_statement (&s->fill_statement);
2765 case lang_data_statement_enum:
2766 print_data_statement (&s->data_statement);
2768 case lang_reloc_statement_enum:
2769 print_reloc_statement (&s->reloc_statement);
2771 case lang_input_section_enum:
2772 print_input_section (&s->input_section);
2774 case lang_padding_statement_enum:
2775 print_padding_statement (&s->padding_statement);
2777 case lang_output_section_statement_enum:
2778 print_output_section_statement (&s->output_section_statement);
2780 case lang_assignment_statement_enum:
2781 print_assignment (&s->assignment_statement, os);
2783 case lang_target_statement_enum:
2784 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2786 case lang_output_statement_enum:
2787 minfo ("OUTPUT(%s", s->output_statement.name);
2788 if (output_target != NULL)
2789 minfo (" %s", output_target);
2792 case lang_input_statement_enum:
2793 print_input_statement (&s->input_statement);
2795 case lang_group_statement_enum:
2796 print_group (&s->group_statement, os);
2798 case lang_afile_asection_pair_statement_enum:
2805 print_statements (void)
2807 print_statement_list (statement_list.head, abs_output_section);
2810 /* Print the first N statements in statement list S to STDERR.
2811 If N == 0, nothing is printed.
2812 If N < 0, the entire list is printed.
2813 Intended to be called from GDB. */
2816 dprint_statement (lang_statement_union_type *s, int n)
2818 FILE *map_save = config.map_file;
2820 config.map_file = stderr;
2823 print_statement_list (s, abs_output_section);
2826 while (s && --n >= 0)
2828 print_statement (s, abs_output_section);
2833 config.map_file = map_save;
2837 insert_pad (lang_statement_union_type **ptr,
2839 unsigned int alignment_needed,
2840 asection *output_section,
2843 static fill_type zero_fill = { 1, { 0 } };
2844 lang_statement_union_type *pad;
2846 pad = ((lang_statement_union_type *)
2847 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2848 if (ptr != &statement_list.head
2849 && pad->header.type == lang_padding_statement_enum
2850 && pad->padding_statement.output_section == output_section)
2852 /* Use the existing pad statement. The above test on output
2853 section is probably redundant, but it doesn't hurt to check. */
2857 /* Make a new padding statement, linked into existing chain. */
2858 pad = stat_alloc (sizeof (lang_padding_statement_type));
2859 pad->header.next = *ptr;
2861 pad->header.type = lang_padding_statement_enum;
2862 pad->padding_statement.output_section = output_section;
2865 pad->padding_statement.fill = fill;
2867 pad->padding_statement.output_offset = dot - output_section->vma;
2868 pad->padding_statement.size = alignment_needed;
2869 output_section->size += alignment_needed;
2872 /* Work out how much this section will move the dot point. */
2876 (lang_statement_union_type **this_ptr,
2877 lang_output_section_statement_type *output_section_statement,
2881 lang_input_section_type *is = &((*this_ptr)->input_section);
2882 asection *i = is->section;
2884 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2886 unsigned int alignment_needed;
2889 /* Align this section first to the input sections requirement,
2890 then to the output section's requirement. If this alignment
2891 is greater than any seen before, then record it too. Perform
2892 the alignment by inserting a magic 'padding' statement. */
2894 if (output_section_statement->subsection_alignment != -1)
2895 i->alignment_power = output_section_statement->subsection_alignment;
2897 o = output_section_statement->bfd_section;
2898 if (o->alignment_power < i->alignment_power)
2899 o->alignment_power = i->alignment_power;
2901 alignment_needed = align_power (dot, i->alignment_power) - dot;
2903 if (alignment_needed != 0)
2905 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2906 dot += alignment_needed;
2909 /* Remember where in the output section this input section goes. */
2911 i->output_offset = dot - o->vma;
2913 /* Mark how big the output section must be to contain this now. */
2914 dot += TO_ADDR (i->size);
2915 o->size = TO_SIZE (dot - o->vma);
2919 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2925 #define IGNORE_SECTION(s) \
2926 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2927 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2928 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2931 /* Check to see if any allocated sections overlap with other allocated
2932 sections. This can happen when the linker script specifically specifies
2933 the output section addresses of the two sections. */
2936 lang_check_section_addresses (void)
2940 /* Scan all sections in the output list. */
2941 for (s = output_bfd->sections; s != NULL; s = s->next)
2945 /* Ignore sections which are not loaded or which have no contents. */
2946 if (IGNORE_SECTION (s))
2949 /* Once we reach section 's' stop our seach. This prevents two
2950 warning messages from being produced, one for 'section A overlaps
2951 section B' and one for 'section B overlaps section A'. */
2952 for (os = output_bfd->sections; os != s; os = os->next)
2959 /* Only consider loadable sections with real contents. */
2960 if (IGNORE_SECTION (os))
2963 /* We must check the sections' LMA addresses not their
2964 VMA addresses because overlay sections can have
2965 overlapping VMAs but they must have distinct LMAs. */
2966 s_start = bfd_section_lma (output_bfd, s);
2967 os_start = bfd_section_lma (output_bfd, os);
2968 s_end = s_start + TO_ADDR (s->size) - 1;
2969 os_end = os_start + TO_ADDR (os->size) - 1;
2971 /* Look for an overlap. */
2972 if ((s_end < os_start) || (s_start > os_end))
2976 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2977 s->name, s_start, s_end, os->name, os_start, os_end);
2979 /* Once we have found one overlap for this section,
2980 stop looking for others. */
2986 /* Make sure the new address is within the region. We explicitly permit the
2987 current address to be at the exact end of the region when the address is
2988 non-zero, in case the region is at the end of addressable memory and the
2989 calculation wraps around. */
2992 os_region_check (lang_output_section_statement_type *os,
2993 lang_memory_region_type *region,
2997 if ((region->current < region->origin
2998 || (region->current - region->origin > region->length))
2999 && ((region->current != region->origin + region->length)
3004 einfo (_("%X%P: address 0x%v of %B section %s"
3005 " is not within region %s\n"),
3007 os->bfd_section->owner,
3008 os->bfd_section->name,
3013 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3015 os->bfd_section->owner,
3016 os->bfd_section->name);
3018 /* Reset the region pointer. */
3019 region->current = region->origin;
3023 /* Set the sizes for all the output sections. */
3026 lang_size_sections_1
3027 (lang_statement_union_type *s,
3028 lang_output_section_statement_type *output_section_statement,
3029 lang_statement_union_type **prev,
3033 bfd_boolean check_regions)
3035 /* Size up the sections from their constituent parts. */
3036 for (; s != NULL; s = s->header.next)
3038 switch (s->header.type)
3040 case lang_output_section_statement_enum:
3043 lang_output_section_statement_type *os;
3045 os = &s->output_section_statement;
3046 if (os->bfd_section == NULL)
3047 /* This section was never actually created. */
3050 /* If this is a COFF shared library section, use the size and
3051 address from the input section. FIXME: This is COFF
3052 specific; it would be cleaner if there were some other way
3053 to do this, but nothing simple comes to mind. */
3054 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3058 if (os->children.head == NULL
3059 || os->children.head->header.next != NULL
3060 || (os->children.head->header.type
3061 != lang_input_section_enum))
3062 einfo (_("%P%X: Internal error on COFF shared library"
3063 " section %s\n"), os->name);
3065 input = os->children.head->input_section.section;
3066 bfd_set_section_vma (os->bfd_section->owner,
3068 bfd_section_vma (input->owner, input));
3069 os->bfd_section->size = input->size;
3073 if (bfd_is_abs_section (os->bfd_section))
3075 /* No matter what happens, an abs section starts at zero. */
3076 ASSERT (os->bfd_section->vma == 0);
3080 if (os->addr_tree == NULL)
3082 /* No address specified for this section, get one
3083 from the region specification. */
3084 if (os->region == NULL
3085 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3086 && os->region->name[0] == '*'
3087 && strcmp (os->region->name,
3088 DEFAULT_MEMORY_REGION) == 0))
3090 os->region = lang_memory_default (os->bfd_section);
3093 /* If a loadable section is using the default memory
3094 region, and some non default memory regions were
3095 defined, issue an error message. */
3096 if (!IGNORE_SECTION (os->bfd_section)
3097 && ! link_info.relocatable
3099 && strcmp (os->region->name,
3100 DEFAULT_MEMORY_REGION) == 0
3101 && lang_memory_region_list != NULL
3102 && (strcmp (lang_memory_region_list->name,
3103 DEFAULT_MEMORY_REGION) != 0
3104 || lang_memory_region_list->next != NULL))
3106 /* By default this is an error rather than just a
3107 warning because if we allocate the section to the
3108 default memory region we can end up creating an
3109 excessively large binary, or even seg faulting when
3110 attempting to perform a negative seek. See
3111 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3112 for an example of this. This behaviour can be
3113 overridden by the using the --no-check-sections
3115 if (command_line.check_section_addresses)
3116 einfo (_("%P%F: error: no memory region specified"
3117 " for loadable section `%s'\n"),
3118 bfd_get_section_name (output_bfd,
3121 einfo (_("%P: warning: no memory region specified"
3122 " for loadable section `%s'\n"),
3123 bfd_get_section_name (output_bfd,
3127 dot = os->region->current;
3129 if (os->section_alignment == -1)
3134 dot = align_power (dot,
3135 os->bfd_section->alignment_power);
3137 if (dot != olddot && config.warn_section_align)
3138 einfo (_("%P: warning: changing start of section"
3139 " %s by %u bytes\n"),
3140 os->name, (unsigned int) (dot - olddot));
3148 r = exp_fold_tree (os->addr_tree,
3150 lang_allocating_phase_enum,
3155 einfo (_("%F%S: non constant or forward reference"
3156 " address expression for section %s\n"),
3159 dot = r.value + r.section->bfd_section->vma;
3162 /* The section starts here.
3163 First, align to what the section needs. */
3165 if (os->section_alignment != -1)
3166 dot = align_power (dot, os->section_alignment);
3168 bfd_set_section_vma (0, os->bfd_section, dot);
3170 os->bfd_section->output_offset = 0;
3173 lang_size_sections_1 (os->children.head, os, &os->children.head,
3174 os->fill, dot, relax, check_regions);
3176 /* Put the section within the requested block size, or
3177 align at the block boundary. */
3178 after = ((os->bfd_section->vma
3179 + TO_ADDR (os->bfd_section->size)
3180 + os->block_value - 1)
3181 & - (bfd_vma) os->block_value);
3183 if (bfd_is_abs_section (os->bfd_section))
3184 ASSERT (after == os->bfd_section->vma);
3186 os->bfd_section->size
3187 = TO_SIZE (after - os->bfd_section->vma);
3189 dot = os->bfd_section->vma;
3190 /* .tbss sections effectively have zero size. */
3191 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3192 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3193 || link_info.relocatable)
3194 dot += TO_ADDR (os->bfd_section->size);
3198 if (os->update_dot_tree != 0)
3199 exp_fold_tree (os->update_dot_tree, abs_output_section,
3200 lang_allocating_phase_enum, dot, &dot);
3202 /* Update dot in the region ?
3203 We only do this if the section is going to be allocated,
3204 since unallocated sections do not contribute to the region's
3205 overall size in memory.
3207 If the SEC_NEVER_LOAD bit is not set, it will affect the
3208 addresses of sections after it. We have to update
3210 if (os->region != NULL
3211 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3212 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3214 os->region->current = dot;
3217 /* Make sure the new address is within the region. */
3218 os_region_check (os, os->region, os->addr_tree,
3219 os->bfd_section->vma);
3221 /* If there's no load address specified, use the run
3222 region as the load region. */
3223 if (os->lma_region == NULL && os->load_base == NULL)
3224 os->lma_region = os->region;
3226 if (os->lma_region != NULL && os->lma_region != os->region)
3228 /* Set load_base, which will be handled later. */
3229 os->load_base = exp_intop (os->lma_region->current);
3230 os->lma_region->current +=
3231 TO_ADDR (os->bfd_section->size);
3233 os_region_check (os, os->lma_region, NULL,
3234 os->bfd_section->lma);
3240 case lang_constructors_statement_enum:
3241 dot = lang_size_sections_1 (constructor_list.head,
3242 output_section_statement,
3243 &s->wild_statement.children.head,
3244 fill, dot, relax, check_regions);
3247 case lang_data_statement_enum:
3249 unsigned int size = 0;
3251 s->data_statement.output_vma =
3252 dot - output_section_statement->bfd_section->vma;
3253 s->data_statement.output_section =
3254 output_section_statement->bfd_section;
3256 /* We might refer to provided symbols in the expression, and
3257 need to mark them as needed. */
3258 exp_fold_tree (s->data_statement.exp, abs_output_section,
3259 lang_allocating_phase_enum, dot, &dot);
3261 switch (s->data_statement.type)
3279 if (size < TO_SIZE ((unsigned) 1))
3280 size = TO_SIZE ((unsigned) 1);
3281 dot += TO_ADDR (size);
3282 output_section_statement->bfd_section->size += size;
3283 /* The output section gets contents, and then we inspect for
3284 any flags set in the input script which override any ALLOC. */
3285 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3286 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3288 output_section_statement->bfd_section->flags |=
3289 SEC_ALLOC | SEC_LOAD;
3294 case lang_reloc_statement_enum:
3298 s->reloc_statement.output_vma =
3299 dot - output_section_statement->bfd_section->vma;
3300 s->reloc_statement.output_section =
3301 output_section_statement->bfd_section;
3302 size = bfd_get_reloc_size (s->reloc_statement.howto);
3303 dot += TO_ADDR (size);
3304 output_section_statement->bfd_section->size += size;
3308 case lang_wild_statement_enum:
3310 dot = lang_size_sections_1 (s->wild_statement.children.head,
3311 output_section_statement,
3312 &s->wild_statement.children.head,
3313 fill, dot, relax, check_regions);
3317 case lang_object_symbols_statement_enum:
3318 link_info.create_object_symbols_section =
3319 output_section_statement->bfd_section;
3321 case lang_output_statement_enum:
3322 case lang_target_statement_enum:
3324 case lang_input_section_enum:
3328 i = (*prev)->input_section.section;
3333 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3334 einfo (_("%P%F: can't relax section: %E\n"));
3338 dot = size_input_section (prev, output_section_statement,
3339 output_section_statement->fill, dot);
3342 case lang_input_statement_enum:
3344 case lang_fill_statement_enum:
3345 s->fill_statement.output_section =
3346 output_section_statement->bfd_section;
3348 fill = s->fill_statement.fill;
3350 case lang_assignment_statement_enum:
3352 bfd_vma newdot = dot;
3354 exp_fold_tree (s->assignment_statement.exp,
3355 output_section_statement,
3356 lang_allocating_phase_enum,
3362 if (output_section_statement == abs_output_section)
3364 /* If we don't have an output section, then just adjust
3365 the default memory address. */
3366 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3367 FALSE)->current = newdot;
3371 /* Insert a pad after this statement. We can't
3372 put the pad before when relaxing, in case the
3373 assignment references dot. */
3374 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3375 output_section_statement->bfd_section, dot);
3377 /* Don't neuter the pad below when relaxing. */
3381 /* If dot is advanced, this implies that the section should
3382 have space allocated to it, unless the user has explicitly
3383 stated that the section should never be loaded. */
3384 if (!(output_section_statement->flags
3385 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3386 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3393 case lang_padding_statement_enum:
3394 /* If this is the first time lang_size_sections is called,
3395 we won't have any padding statements. If this is the
3396 second or later passes when relaxing, we should allow
3397 padding to shrink. If padding is needed on this pass, it
3398 will be added back in. */
3399 s->padding_statement.size = 0;
3401 /* Make sure output_offset is valid. If relaxation shrinks
3402 the section and this pad isn't needed, it's possible to
3403 have output_offset larger than the final size of the
3404 section. bfd_set_section_contents will complain even for
3405 a pad size of zero. */
3406 s->padding_statement.output_offset
3407 = dot - output_section_statement->bfd_section->vma;
3410 case lang_group_statement_enum:
3411 dot = lang_size_sections_1 (s->group_statement.children.head,
3412 output_section_statement,
3413 &s->group_statement.children.head,
3414 fill, dot, relax, check_regions);
3421 /* We can only get here when relaxing is turned on. */
3422 case lang_address_statement_enum:
3425 prev = &s->header.next;
3432 (lang_statement_union_type *s,
3433 lang_output_section_statement_type *output_section_statement,
3434 lang_statement_union_type **prev,
3438 bfd_boolean check_regions)
3442 /* Callers of exp_fold_tree need to increment this. */
3443 lang_statement_iteration++;
3445 exp_data_seg.phase = exp_dataseg_none;
3446 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3447 dot, relax, check_regions);
3448 if (exp_data_seg.phase == exp_dataseg_end_seen
3449 && link_info.relro && exp_data_seg.relro_end)
3451 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3452 to put exp_data_seg.relro on a (common) page boundary. */
3454 exp_data_seg.phase = exp_dataseg_relro_adjust;
3455 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3456 dot, relax, check_regions);
3457 link_info.relro_start = exp_data_seg.base;
3458 link_info.relro_end = exp_data_seg.relro_end;
3460 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3462 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3463 a page could be saved in the data segment. */
3464 bfd_vma first, last;
3466 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3467 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3469 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3470 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3471 && first + last <= exp_data_seg.pagesize)
3473 exp_data_seg.phase = exp_dataseg_adjust;
3474 lang_statement_iteration++;
3475 result = lang_size_sections_1 (s, output_section_statement, prev,
3476 fill, dot, relax, check_regions);
3483 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3486 lang_do_assignments_1
3487 (lang_statement_union_type *s,
3488 lang_output_section_statement_type *output_section_statement,
3492 for (; s != NULL; s = s->header.next)
3494 switch (s->header.type)
3496 case lang_constructors_statement_enum:
3497 dot = lang_do_assignments_1 (constructor_list.head,
3498 output_section_statement,
3503 case lang_output_section_statement_enum:
3505 lang_output_section_statement_type *os;
3507 os = &(s->output_section_statement);
3508 if (os->bfd_section != NULL)
3510 dot = os->bfd_section->vma;
3511 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3512 /* .tbss sections effectively have zero size. */
3513 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3514 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3515 || link_info.relocatable)
3516 dot += TO_ADDR (os->bfd_section->size);
3520 /* If nothing has been placed into the output section then
3521 it won't have a bfd_section. */
3522 if (os->bfd_section)
3524 os->bfd_section->lma
3525 = exp_get_abs_int (os->load_base, 0, "load base",
3526 lang_final_phase_enum);
3531 case lang_wild_statement_enum:
3533 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3534 output_section_statement,
3539 case lang_object_symbols_statement_enum:
3540 case lang_output_statement_enum:
3541 case lang_target_statement_enum:
3543 case lang_common_statement_enum:
3546 case lang_data_statement_enum:
3548 etree_value_type value;
3550 value = exp_fold_tree (s->data_statement.exp,
3552 lang_final_phase_enum, dot, &dot);
3554 einfo (_("%F%P: invalid data statement\n"));
3555 s->data_statement.value
3556 = value.value + value.section->bfd_section->vma;
3560 switch (s->data_statement.type)
3578 if (size < TO_SIZE ((unsigned) 1))
3579 size = TO_SIZE ((unsigned) 1);
3580 dot += TO_ADDR (size);
3584 case lang_reloc_statement_enum:
3586 etree_value_type value;
3588 value = exp_fold_tree (s->reloc_statement.addend_exp,
3590 lang_final_phase_enum, dot, &dot);
3591 s->reloc_statement.addend_value = value.value;
3593 einfo (_("%F%P: invalid reloc statement\n"));
3595 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3598 case lang_input_section_enum:
3600 asection *in = s->input_section.section;
3602 if ((in->flags & SEC_EXCLUDE) == 0)
3603 dot += TO_ADDR (in->size);
3607 case lang_input_statement_enum:
3609 case lang_fill_statement_enum:
3610 fill = s->fill_statement.fill;
3612 case lang_assignment_statement_enum:
3614 exp_fold_tree (s->assignment_statement.exp,
3615 output_section_statement,
3616 lang_final_phase_enum,
3622 case lang_padding_statement_enum:
3623 dot += TO_ADDR (s->padding_statement.size);
3626 case lang_group_statement_enum:
3627 dot = lang_do_assignments_1 (s->group_statement.children.head,
3628 output_section_statement,
3636 case lang_address_statement_enum:
3646 (lang_statement_union_type *s,
3647 lang_output_section_statement_type *output_section_statement,
3651 /* Callers of exp_fold_tree need to increment this. */
3652 lang_statement_iteration++;
3653 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3656 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3657 operator .startof. (section_name), it produces an undefined symbol
3658 .startof.section_name. Similarly, when it sees
3659 .sizeof. (section_name), it produces an undefined symbol
3660 .sizeof.section_name. For all the output sections, we look for
3661 such symbols, and set them to the correct value. */
3664 lang_set_startof (void)
3668 if (link_info.relocatable)
3671 for (s = output_bfd->sections; s != NULL; s = s->next)
3673 const char *secname;
3675 struct bfd_link_hash_entry *h;
3677 secname = bfd_get_section_name (output_bfd, s);
3678 buf = xmalloc (10 + strlen (secname));
3680 sprintf (buf, ".startof.%s", secname);
3681 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3682 if (h != NULL && h->type == bfd_link_hash_undefined)
3684 h->type = bfd_link_hash_defined;
3685 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3686 h->u.def.section = bfd_abs_section_ptr;
3689 sprintf (buf, ".sizeof.%s", secname);
3690 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3691 if (h != NULL && h->type == bfd_link_hash_undefined)
3693 h->type = bfd_link_hash_defined;
3694 h->u.def.value = TO_ADDR (s->size);
3695 h->u.def.section = bfd_abs_section_ptr;
3705 struct bfd_link_hash_entry *h;
3708 if (link_info.relocatable || link_info.shared)
3713 if (entry_symbol.name == NULL)
3715 /* No entry has been specified. Look for start, but don't warn
3716 if we don't find it. */
3717 entry_symbol.name = "start";
3721 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3722 FALSE, FALSE, TRUE);
3724 && (h->type == bfd_link_hash_defined
3725 || h->type == bfd_link_hash_defweak)
3726 && h->u.def.section->output_section != NULL)
3730 val = (h->u.def.value
3731 + bfd_get_section_vma (output_bfd,
3732 h->u.def.section->output_section)
3733 + h->u.def.section->output_offset);
3734 if (! bfd_set_start_address (output_bfd, val))
3735 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3742 /* We couldn't find the entry symbol. Try parsing it as a
3744 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3747 if (! bfd_set_start_address (output_bfd, val))
3748 einfo (_("%P%F: can't set start address\n"));
3754 /* Can't find the entry symbol, and it's not a number. Use
3755 the first address in the text section. */
3756 ts = bfd_get_section_by_name (output_bfd, entry_section);
3760 einfo (_("%P: warning: cannot find entry symbol %s;"
3761 " defaulting to %V\n"),
3763 bfd_get_section_vma (output_bfd, ts));
3764 if (! bfd_set_start_address (output_bfd,
3765 bfd_get_section_vma (output_bfd,
3767 einfo (_("%P%F: can't set start address\n"));
3772 einfo (_("%P: warning: cannot find entry symbol %s;"
3773 " not setting start address\n"),
3779 /* Don't bfd_hash_table_free (&lang_definedness_table);
3780 map file output may result in a call of lang_track_definedness. */
3783 /* This is a small function used when we want to ignore errors from
3787 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3789 /* Don't do anything. */
3792 /* Check that the architecture of all the input files is compatible
3793 with the output file. Also call the backend to let it do any
3794 other checking that is needed. */
3799 lang_statement_union_type *file;
3801 const bfd_arch_info_type *compatible;
3803 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3805 input_bfd = file->input_statement.the_bfd;
3807 = bfd_arch_get_compatible (input_bfd, output_bfd,
3808 command_line.accept_unknown_input_arch);
3810 /* In general it is not possible to perform a relocatable
3811 link between differing object formats when the input
3812 file has relocations, because the relocations in the
3813 input format may not have equivalent representations in
3814 the output format (and besides BFD does not translate
3815 relocs for other link purposes than a final link). */
3816 if ((link_info.relocatable || link_info.emitrelocations)
3817 && (compatible == NULL
3818 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3819 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3821 einfo (_("%P%F: Relocatable linking with relocations from"
3822 " format %s (%B) to format %s (%B) is not supported\n"),
3823 bfd_get_target (input_bfd), input_bfd,
3824 bfd_get_target (output_bfd), output_bfd);
3825 /* einfo with %F exits. */
3828 if (compatible == NULL)
3830 if (command_line.warn_mismatch)
3831 einfo (_("%P: warning: %s architecture of input file `%B'"
3832 " is incompatible with %s output\n"),
3833 bfd_printable_name (input_bfd), input_bfd,
3834 bfd_printable_name (output_bfd));
3836 else if (bfd_count_sections (input_bfd))
3838 /* If the input bfd has no contents, it shouldn't set the
3839 private data of the output bfd. */
3841 bfd_error_handler_type pfn = NULL;
3843 /* If we aren't supposed to warn about mismatched input
3844 files, temporarily set the BFD error handler to a
3845 function which will do nothing. We still want to call
3846 bfd_merge_private_bfd_data, since it may set up
3847 information which is needed in the output file. */
3848 if (! command_line.warn_mismatch)
3849 pfn = bfd_set_error_handler (ignore_bfd_errors);
3850 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3852 if (command_line.warn_mismatch)
3853 einfo (_("%P%X: failed to merge target specific data"
3854 " of file %B\n"), input_bfd);
3856 if (! command_line.warn_mismatch)
3857 bfd_set_error_handler (pfn);
3862 /* Look through all the global common symbols and attach them to the
3863 correct section. The -sort-common command line switch may be used
3864 to roughly sort the entries by size. */
3869 if (command_line.inhibit_common_definition)
3871 if (link_info.relocatable
3872 && ! command_line.force_common_definition)
3875 if (! config.sort_common)
3876 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3881 for (power = 4; power >= 0; power--)
3882 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3886 /* Place one common symbol in the correct section. */
3889 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3891 unsigned int power_of_two;
3895 if (h->type != bfd_link_hash_common)
3899 power_of_two = h->u.c.p->alignment_power;
3901 if (config.sort_common
3902 && power_of_two < (unsigned int) *(int *) info)
3905 section = h->u.c.p->section;
3907 /* Increase the size of the section to align the common sym. */
3908 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3909 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3911 /* Adjust the alignment if necessary. */
3912 if (power_of_two > section->alignment_power)
3913 section->alignment_power = power_of_two;
3915 /* Change the symbol from common to defined. */
3916 h->type = bfd_link_hash_defined;
3917 h->u.def.section = section;
3918 h->u.def.value = section->size;
3920 /* Increase the size of the section. */
3921 section->size += size;
3923 /* Make sure the section is allocated in memory, and make sure that
3924 it is no longer a common section. */
3925 section->flags |= SEC_ALLOC;
3926 section->flags &= ~SEC_IS_COMMON;
3928 if (config.map_file != NULL)
3930 static bfd_boolean header_printed;
3935 if (! header_printed)
3937 minfo (_("\nAllocating common symbols\n"));
3938 minfo (_("Common symbol size file\n\n"));
3939 header_printed = TRUE;
3942 name = demangle (h->root.string);
3944 len = strlen (name);
3959 if (size <= 0xffffffff)
3960 sprintf (buf, "%lx", (unsigned long) size);
3962 sprintf_vma (buf, size);
3972 minfo ("%B\n", section->owner);
3978 /* Run through the input files and ensure that every input section has
3979 somewhere to go. If one is found without a destination then create
3980 an input request and place it into the statement tree. */
3983 lang_place_orphans (void)
3985 LANG_FOR_EACH_INPUT_STATEMENT (file)
3989 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3991 if (s->output_section == NULL)
3993 /* This section of the file is not attached, root
3994 around for a sensible place for it to go. */
3996 if (file->just_syms_flag)
3999 if ((s->flags & SEC_EXCLUDE) != 0)
4000 s->output_section = bfd_abs_section_ptr;
4001 else if (strcmp (s->name, "COMMON") == 0)
4003 /* This is a lonely common section which must have
4004 come from an archive. We attach to the section
4005 with the wildcard. */
4006 if (! link_info.relocatable
4007 || command_line.force_common_definition)
4009 if (default_common_section == NULL)
4012 /* This message happens when using the
4013 svr3.ifile linker script, so I have
4015 info_msg (_("%P: no [COMMON] command,"
4016 " defaulting to .bss\n"));
4018 default_common_section =
4019 lang_output_section_statement_lookup (".bss");
4022 lang_add_section (&default_common_section->children, s,
4023 default_common_section, file);
4026 else if (ldemul_place_orphan (file, s))
4030 lang_output_section_statement_type *os;
4032 os = lang_output_section_statement_lookup (s->name);
4033 lang_add_section (&os->children, s, os, file);
4041 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4043 flagword *ptr_flags;
4045 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4051 *ptr_flags |= SEC_ALLOC;
4055 *ptr_flags |= SEC_READONLY;
4059 *ptr_flags |= SEC_DATA;
4063 *ptr_flags |= SEC_CODE;
4068 *ptr_flags |= SEC_LOAD;
4072 einfo (_("%P%F: invalid syntax in flags\n"));
4079 /* Call a function on each input file. This function will be called
4080 on an archive, but not on the elements. */
4083 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4085 lang_input_statement_type *f;
4087 for (f = (lang_input_statement_type *) input_file_chain.head;
4089 f = (lang_input_statement_type *) f->next_real_file)
4093 /* Call a function on each file. The function will be called on all
4094 the elements of an archive which are included in the link, but will
4095 not be called on the archive file itself. */
4098 lang_for_each_file (void (*func) (lang_input_statement_type *))
4100 LANG_FOR_EACH_INPUT_STATEMENT (f)
4107 ldlang_add_file (lang_input_statement_type *entry)
4111 lang_statement_append (&file_chain,
4112 (lang_statement_union_type *) entry,
4115 /* The BFD linker needs to have a list of all input BFDs involved in
4117 ASSERT (entry->the_bfd->link_next == NULL);
4118 ASSERT (entry->the_bfd != output_bfd);
4119 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4121 *pp = entry->the_bfd;
4122 entry->the_bfd->usrdata = entry;
4123 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4125 /* Look through the sections and check for any which should not be
4126 included in the link. We need to do this now, so that we can
4127 notice when the backend linker tries to report multiple
4128 definition errors for symbols which are in sections we aren't
4129 going to link. FIXME: It might be better to entirely ignore
4130 symbols which are defined in sections which are going to be
4131 discarded. This would require modifying the backend linker for
4132 each backend which might set the SEC_LINK_ONCE flag. If we do
4133 this, we should probably handle SEC_EXCLUDE in the same way. */
4135 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4139 lang_add_output (const char *name, int from_script)
4141 /* Make -o on command line override OUTPUT in script. */
4142 if (!had_output_filename || !from_script)
4144 output_filename = name;
4145 had_output_filename = TRUE;
4149 static lang_output_section_statement_type *current_section;
4160 for (l = 0; l < 32; l++)
4162 if (i >= (unsigned int) x)
4170 lang_output_section_statement_type *
4171 lang_enter_output_section_statement (const char *output_section_statement_name,
4172 etree_type *address_exp,
4173 enum section_type sectype,
4175 etree_type *subalign,
4179 lang_output_section_statement_type *os;
4183 lang_output_section_statement_lookup_1 (output_section_statement_name,
4186 /* Add this statement to tree. */
4188 add_statement (lang_output_section_statement_enum,
4189 output_section_statement);
4191 /* Make next things chain into subchain of this. */
4193 if (os->addr_tree == NULL)
4195 os->addr_tree = address_exp;
4197 os->sectype = sectype;
4198 if (sectype != noload_section)
4199 os->flags = SEC_NO_FLAGS;
4201 os->flags = SEC_NEVER_LOAD;
4202 os->block_value = 1;
4203 stat_ptr = &os->children;
4205 os->subsection_alignment =
4206 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4207 os->section_alignment =
4208 topower (exp_get_value_int (align, -1, "section alignment", 0));
4210 os->load_base = ebase;
4217 lang_output_statement_type *new =
4218 new_stat (lang_output_statement, stat_ptr);
4220 new->name = output_filename;
4223 /* Reset the current counters in the regions. */
4226 lang_reset_memory_regions (void)
4228 lang_memory_region_type *p = lang_memory_region_list;
4231 for (p = lang_memory_region_list; p != NULL; p = p->next)
4233 p->old_length = (bfd_size_type) (p->current - p->origin);
4234 p->current = p->origin;
4237 for (o = output_bfd->sections; o != NULL; o = o->next)
4239 /* Save the last size for possible use by bfd_relax_section. */
4240 o->rawsize = o->size;
4245 /* Worker for lang_gc_sections_1. */
4248 gc_section_callback (lang_wild_statement_type *ptr,
4249 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4251 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4252 void *data ATTRIBUTE_UNUSED)
4254 /* If the wild pattern was marked KEEP, the member sections
4255 should be as well. */
4256 if (ptr->keep_sections)
4257 section->flags |= SEC_KEEP;
4260 /* Iterate over sections marking them against GC. */
4263 lang_gc_sections_1 (lang_statement_union_type *s)
4265 for (; s != NULL; s = s->header.next)
4267 switch (s->header.type)
4269 case lang_wild_statement_enum:
4270 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4272 case lang_constructors_statement_enum:
4273 lang_gc_sections_1 (constructor_list.head);
4275 case lang_output_section_statement_enum:
4276 lang_gc_sections_1 (s->output_section_statement.children.head);
4278 case lang_group_statement_enum:
4279 lang_gc_sections_1 (s->group_statement.children.head);
4288 lang_gc_sections (void)
4290 struct bfd_link_hash_entry *h;
4291 ldlang_undef_chain_list_type *ulist;
4293 /* Keep all sections so marked in the link script. */
4295 lang_gc_sections_1 (statement_list.head);
4297 /* Keep all sections containing symbols undefined on the command-line,
4298 and the section containing the entry symbol. */
4300 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4302 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4303 FALSE, FALSE, FALSE);
4306 && (h->type == bfd_link_hash_defined
4307 || h->type == bfd_link_hash_defweak)
4308 && ! bfd_is_abs_section (h->u.def.section))
4310 h->u.def.section->flags |= SEC_KEEP;
4314 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4315 the special case of debug info. (See bfd/stabs.c)
4316 Twiddle the flag here, to simplify later linker code. */
4317 if (link_info.relocatable)
4319 LANG_FOR_EACH_INPUT_STATEMENT (f)
4322 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4323 if ((sec->flags & SEC_DEBUGGING) == 0)
4324 sec->flags &= ~SEC_EXCLUDE;
4328 if (command_line.gc_sections)
4329 bfd_gc_sections (output_bfd, &link_info);
4335 lang_reasonable_defaults ();
4336 current_target = default_target;
4338 /* Open the output file. */
4339 lang_for_each_statement (ldlang_open_output);
4342 ldemul_create_output_section_statements ();
4344 /* Add to the hash table all undefineds on the command line. */
4345 lang_place_undefineds ();
4347 already_linked_table_init ();
4349 /* Create a bfd for each input file. */
4350 current_target = default_target;
4351 open_input_bfds (statement_list.head, FALSE);
4353 link_info.gc_sym_list = &entry_symbol;
4354 if (entry_symbol.name == NULL)
4355 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4357 ldemul_after_open ();
4359 already_linked_table_free ();
4361 /* Make sure that we're not mixing architectures. We call this
4362 after all the input files have been opened, but before we do any
4363 other processing, so that any operations merge_private_bfd_data
4364 does on the output file will be known during the rest of the
4368 /* Handle .exports instead of a version script if we're told to do so. */
4369 if (command_line.version_exports_section)
4370 lang_do_version_exports_section ();
4372 /* Build all sets based on the information gathered from the input
4374 ldctor_build_sets ();
4376 /* Remove unreferenced sections if asked to. */
4377 lang_gc_sections ();
4379 /* Size up the common data. */
4382 /* Run through the contours of the script and attach input sections
4383 to the correct output sections. */
4384 map_input_to_output_sections (statement_list.head, NULL, NULL);
4386 /* Find any sections not attached explicitly and handle them. */
4387 lang_place_orphans ();
4389 if (! link_info.relocatable)
4393 /* Merge SEC_MERGE sections. This has to be done after GC of
4394 sections, so that GCed sections are not merged, but before
4395 assigning dynamic symbols, since removing whole input sections
4397 bfd_merge_sections (output_bfd, &link_info);
4399 /* Look for a text section and set the readonly attribute in it. */
4400 found = bfd_get_section_by_name (output_bfd, ".text");
4404 if (config.text_read_only)
4405 found->flags |= SEC_READONLY;
4407 found->flags &= ~SEC_READONLY;
4411 /* Do anything special before sizing sections. This is where ELF
4412 and other back-ends size dynamic sections. */
4413 ldemul_before_allocation ();
4415 if (!link_info.relocatable)
4416 strip_excluded_output_sections ();
4418 /* We must record the program headers before we try to fix the
4419 section positions, since they will affect SIZEOF_HEADERS. */
4420 lang_record_phdrs ();
4422 /* Size up the sections. */
4423 lang_size_sections (statement_list.head, abs_output_section,
4424 &statement_list.head, 0, 0, NULL,
4425 command_line.relax ? FALSE : TRUE);
4427 /* Now run around and relax if we can. */
4428 if (command_line.relax)
4430 /* Keep relaxing until bfd_relax_section gives up. */
4431 bfd_boolean relax_again;
4435 relax_again = FALSE;
4437 /* Note: pe-dll.c does something like this also. If you find
4438 you need to change this code, you probably need to change
4439 pe-dll.c also. DJ */
4441 /* Do all the assignments with our current guesses as to
4443 lang_do_assignments (statement_list.head, abs_output_section,
4446 /* We must do this after lang_do_assignments, because it uses
4448 lang_reset_memory_regions ();
4450 /* Perform another relax pass - this time we know where the
4451 globals are, so can make a better guess. */
4452 lang_size_sections (statement_list.head, abs_output_section,
4453 &statement_list.head, 0, 0, &relax_again, FALSE);
4455 /* If the normal relax is done and the relax finalize pass
4456 is not performed yet, we perform another relax pass. */
4457 if (!relax_again && link_info.need_relax_finalize)
4459 link_info.need_relax_finalize = FALSE;
4463 while (relax_again);
4465 /* Final extra sizing to report errors. */
4466 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4467 lang_reset_memory_regions ();
4468 lang_size_sections (statement_list.head, abs_output_section,
4469 &statement_list.head, 0, 0, NULL, TRUE);
4472 /* See if anything special should be done now we know how big
4474 ldemul_after_allocation ();
4476 /* Fix any .startof. or .sizeof. symbols. */
4477 lang_set_startof ();
4479 /* Do all the assignments, now that we know the final resting places
4480 of all the symbols. */
4482 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4484 /* Make sure that the section addresses make sense. */
4485 if (! link_info.relocatable
4486 && command_line.check_section_addresses)
4487 lang_check_section_addresses ();
4495 /* EXPORTED TO YACC */
4498 lang_add_wild (struct wildcard_spec *filespec,
4499 struct wildcard_list *section_list,
4500 bfd_boolean keep_sections)
4502 struct wildcard_list *curr, *next;
4503 lang_wild_statement_type *new;
4505 /* Reverse the list as the parser puts it back to front. */
4506 for (curr = section_list, section_list = NULL;
4508 section_list = curr, curr = next)
4510 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4511 placed_commons = TRUE;
4514 curr->next = section_list;
4517 if (filespec != NULL && filespec->name != NULL)
4519 if (strcmp (filespec->name, "*") == 0)
4520 filespec->name = NULL;
4521 else if (! wildcardp (filespec->name))
4522 lang_has_input_file = TRUE;
4525 new = new_stat (lang_wild_statement, stat_ptr);
4526 new->filename = NULL;
4527 new->filenames_sorted = FALSE;
4528 if (filespec != NULL)
4530 new->filename = filespec->name;
4531 new->filenames_sorted = filespec->sorted;
4533 new->section_list = section_list;
4534 new->keep_sections = keep_sections;
4535 lang_list_init (&new->children);
4539 lang_section_start (const char *name, etree_type *address)
4541 lang_address_statement_type *ad;
4543 ad = new_stat (lang_address_statement, stat_ptr);
4544 ad->section_name = name;
4545 ad->address = address;
4548 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4549 because of a -e argument on the command line, or zero if this is
4550 called by ENTRY in a linker script. Command line arguments take
4554 lang_add_entry (const char *name, bfd_boolean cmdline)
4556 if (entry_symbol.name == NULL
4558 || ! entry_from_cmdline)
4560 entry_symbol.name = name;
4561 entry_from_cmdline = cmdline;
4566 lang_add_target (const char *name)
4568 lang_target_statement_type *new = new_stat (lang_target_statement,
4576 lang_add_map (const char *name)
4583 map_option_f = TRUE;
4591 lang_add_fill (fill_type *fill)
4593 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4600 lang_add_data (int type, union etree_union *exp)
4603 lang_data_statement_type *new = new_stat (lang_data_statement,
4611 /* Create a new reloc statement. RELOC is the BFD relocation type to
4612 generate. HOWTO is the corresponding howto structure (we could
4613 look this up, but the caller has already done so). SECTION is the
4614 section to generate a reloc against, or NAME is the name of the
4615 symbol to generate a reloc against. Exactly one of SECTION and
4616 NAME must be NULL. ADDEND is an expression for the addend. */
4619 lang_add_reloc (bfd_reloc_code_real_type reloc,
4620 reloc_howto_type *howto,
4623 union etree_union *addend)
4625 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4629 p->section = section;
4631 p->addend_exp = addend;
4633 p->addend_value = 0;
4634 p->output_section = NULL;
4638 lang_assignment_statement_type *
4639 lang_add_assignment (etree_type *exp)
4641 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4649 lang_add_attribute (enum statement_enum attribute)
4651 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4655 lang_startup (const char *name)
4657 if (startup_file != NULL)
4659 einfo (_("%P%Fmultiple STARTUP files\n"));
4661 first_file->filename = name;
4662 first_file->local_sym_name = name;
4663 first_file->real = TRUE;
4665 startup_file = name;
4669 lang_float (bfd_boolean maybe)
4671 lang_float_flag = maybe;
4675 /* Work out the load- and run-time regions from a script statement, and
4676 store them in *LMA_REGION and *REGION respectively.
4678 MEMSPEC is the name of the run-time region, or the value of
4679 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4680 LMA_MEMSPEC is the name of the load-time region, or null if the
4681 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4682 had an explicit load address.
4684 It is an error to specify both a load region and a load address. */
4687 lang_get_regions (lang_memory_region_type **region,
4688 lang_memory_region_type **lma_region,
4689 const char *memspec,
4690 const char *lma_memspec,
4691 bfd_boolean have_lma,
4692 bfd_boolean have_vma)
4694 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4696 /* If no runtime region or VMA has been specified, but the load region
4697 has been specified, then use the load region for the runtime region
4699 if (lma_memspec != NULL
4701 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4702 *region = *lma_region;
4704 *region = lang_memory_region_lookup (memspec, FALSE);
4706 if (have_lma && lma_memspec != 0)
4707 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4711 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4712 lang_output_section_phdr_list *phdrs,
4713 const char *lma_memspec)
4715 lang_get_regions (¤t_section->region,
4716 ¤t_section->lma_region,
4717 memspec, lma_memspec,
4718 current_section->load_base != NULL,
4719 current_section->addr_tree != NULL);
4720 current_section->fill = fill;
4721 current_section->phdrs = phdrs;
4722 stat_ptr = &statement_list;
4725 /* Create an absolute symbol with the given name with the value of the
4726 address of first byte of the section named.
4728 If the symbol already exists, then do nothing. */
4731 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4733 struct bfd_link_hash_entry *h;
4735 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4737 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4739 if (h->type == bfd_link_hash_new
4740 || h->type == bfd_link_hash_undefined)
4744 h->type = bfd_link_hash_defined;
4746 sec = bfd_get_section_by_name (output_bfd, secname);
4750 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4752 h->u.def.section = bfd_abs_section_ptr;
4756 /* Create an absolute symbol with the given name with the value of the
4757 address of the first byte after the end of the section named.
4759 If the symbol already exists, then do nothing. */
4762 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4764 struct bfd_link_hash_entry *h;
4766 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4768 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4770 if (h->type == bfd_link_hash_new
4771 || h->type == bfd_link_hash_undefined)
4775 h->type = bfd_link_hash_defined;
4777 sec = bfd_get_section_by_name (output_bfd, secname);
4781 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4782 + TO_ADDR (sec->size));
4784 h->u.def.section = bfd_abs_section_ptr;
4789 lang_statement_append (lang_statement_list_type *list,
4790 lang_statement_union_type *element,
4791 lang_statement_union_type **field)
4793 *(list->tail) = element;
4797 /* Set the output format type. -oformat overrides scripts. */
4800 lang_add_output_format (const char *format,
4805 if (output_target == NULL || !from_script)
4807 if (command_line.endian == ENDIAN_BIG
4810 else if (command_line.endian == ENDIAN_LITTLE
4814 output_target = format;
4818 /* Enter a group. This creates a new lang_group_statement, and sets
4819 stat_ptr to build new statements within the group. */
4822 lang_enter_group (void)
4824 lang_group_statement_type *g;
4826 g = new_stat (lang_group_statement, stat_ptr);
4827 lang_list_init (&g->children);
4828 stat_ptr = &g->children;
4831 /* Leave a group. This just resets stat_ptr to start writing to the
4832 regular list of statements again. Note that this will not work if
4833 groups can occur inside anything else which can adjust stat_ptr,
4834 but currently they can't. */
4837 lang_leave_group (void)
4839 stat_ptr = &statement_list;
4842 /* Add a new program header. This is called for each entry in a PHDRS
4843 command in a linker script. */
4846 lang_new_phdr (const char *name,
4848 bfd_boolean filehdr,
4853 struct lang_phdr *n, **pp;
4855 n = stat_alloc (sizeof (struct lang_phdr));
4858 n->type = exp_get_value_int (type, 0, "program header type",
4859 lang_final_phase_enum);
4860 n->filehdr = filehdr;
4865 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4870 /* Record the program header information in the output BFD. FIXME: We
4871 should not be calling an ELF specific function here. */
4874 lang_record_phdrs (void)
4878 lang_output_section_phdr_list *last;
4879 struct lang_phdr *l;
4880 lang_statement_union_type *u;
4883 secs = xmalloc (alc * sizeof (asection *));
4885 for (l = lang_phdr_list; l != NULL; l = l->next)
4892 for (u = lang_output_section_statement.head;
4894 u = u->output_section_statement.next)
4896 lang_output_section_statement_type *os;
4897 lang_output_section_phdr_list *pl;
4899 os = &u->output_section_statement;
4900 if (os->constraint == -1)
4908 if (os->sectype == noload_section
4909 || os->bfd_section == NULL
4910 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4915 if (os->bfd_section == NULL)
4918 for (; pl != NULL; pl = pl->next)
4920 if (strcmp (pl->name, l->name) == 0)
4925 secs = xrealloc (secs, alc * sizeof (asection *));
4927 secs[c] = os->bfd_section;
4934 if (l->flags == NULL)
4937 flags = exp_get_vma (l->flags, 0, "phdr flags",
4938 lang_final_phase_enum);
4943 at = exp_get_vma (l->at, 0, "phdr load address",
4944 lang_final_phase_enum);
4946 if (! bfd_record_phdr (output_bfd, l->type,
4947 l->flags != NULL, flags, l->at != NULL,
4948 at, l->filehdr, l->phdrs, c, secs))
4949 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4954 /* Make sure all the phdr assignments succeeded. */
4955 for (u = lang_output_section_statement.head;
4957 u = u->output_section_statement.next)
4959 lang_output_section_phdr_list *pl;
4961 if (u->output_section_statement.constraint == -1
4962 || u->output_section_statement.bfd_section == NULL)
4965 for (pl = u->output_section_statement.phdrs;
4968 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4969 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4970 u->output_section_statement.name, pl->name);
4974 /* Record a list of sections which may not be cross referenced. */
4977 lang_add_nocrossref (lang_nocrossref_type *l)
4979 struct lang_nocrossrefs *n;
4981 n = xmalloc (sizeof *n);
4982 n->next = nocrossref_list;
4984 nocrossref_list = n;
4986 /* Set notice_all so that we get informed about all symbols. */
4987 link_info.notice_all = TRUE;
4990 /* Overlay handling. We handle overlays with some static variables. */
4992 /* The overlay virtual address. */
4993 static etree_type *overlay_vma;
4994 /* And subsection alignment. */
4995 static etree_type *overlay_subalign;
4997 /* An expression for the maximum section size seen so far. */
4998 static etree_type *overlay_max;
5000 /* A list of all the sections in this overlay. */
5002 struct overlay_list {
5003 struct overlay_list *next;
5004 lang_output_section_statement_type *os;
5007 static struct overlay_list *overlay_list;
5009 /* Start handling an overlay. */
5012 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5014 /* The grammar should prevent nested overlays from occurring. */
5015 ASSERT (overlay_vma == NULL
5016 && overlay_subalign == NULL
5017 && overlay_max == NULL);
5019 overlay_vma = vma_expr;
5020 overlay_subalign = subalign;
5023 /* Start a section in an overlay. We handle this by calling
5024 lang_enter_output_section_statement with the correct VMA.
5025 lang_leave_overlay sets up the LMA and memory regions. */
5028 lang_enter_overlay_section (const char *name)
5030 struct overlay_list *n;
5033 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5034 0, overlay_subalign, 0, 0);
5036 /* If this is the first section, then base the VMA of future
5037 sections on this one. This will work correctly even if `.' is
5038 used in the addresses. */
5039 if (overlay_list == NULL)
5040 overlay_vma = exp_nameop (ADDR, name);
5042 /* Remember the section. */
5043 n = xmalloc (sizeof *n);
5044 n->os = current_section;
5045 n->next = overlay_list;
5048 size = exp_nameop (SIZEOF, name);
5050 /* Arrange to work out the maximum section end address. */
5051 if (overlay_max == NULL)
5054 overlay_max = exp_binop (MAX_K, overlay_max, size);
5057 /* Finish a section in an overlay. There isn't any special to do
5061 lang_leave_overlay_section (fill_type *fill,
5062 lang_output_section_phdr_list *phdrs)
5069 name = current_section->name;
5071 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5072 region and that no load-time region has been specified. It doesn't
5073 really matter what we say here, since lang_leave_overlay will
5075 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5077 /* Define the magic symbols. */
5079 clean = xmalloc (strlen (name) + 1);
5081 for (s1 = name; *s1 != '\0'; s1++)
5082 if (ISALNUM (*s1) || *s1 == '_')
5086 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5087 sprintf (buf, "__load_start_%s", clean);
5088 lang_add_assignment (exp_assop ('=', buf,
5089 exp_nameop (LOADADDR, name)));
5091 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5092 sprintf (buf, "__load_stop_%s", clean);
5093 lang_add_assignment (exp_assop ('=', buf,
5095 exp_nameop (LOADADDR, name),
5096 exp_nameop (SIZEOF, name))));
5101 /* Finish an overlay. If there are any overlay wide settings, this
5102 looks through all the sections in the overlay and sets them. */
5105 lang_leave_overlay (etree_type *lma_expr,
5108 const char *memspec,
5109 lang_output_section_phdr_list *phdrs,
5110 const char *lma_memspec)
5112 lang_memory_region_type *region;
5113 lang_memory_region_type *lma_region;
5114 struct overlay_list *l;
5115 lang_nocrossref_type *nocrossref;
5117 lang_get_regions (®ion, &lma_region,
5118 memspec, lma_memspec,
5119 lma_expr != NULL, FALSE);
5123 /* After setting the size of the last section, set '.' to end of the
5125 if (overlay_list != NULL)
5126 overlay_list->os->update_dot_tree
5127 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5132 struct overlay_list *next;
5134 if (fill != NULL && l->os->fill == NULL)
5137 l->os->region = region;
5138 l->os->lma_region = lma_region;
5140 /* The first section has the load address specified in the
5141 OVERLAY statement. The rest are worked out from that.
5142 The base address is not needed (and should be null) if
5143 an LMA region was specified. */
5145 l->os->load_base = lma_expr;
5146 else if (lma_region == 0)
5147 l->os->load_base = exp_binop ('+',
5148 exp_nameop (LOADADDR, l->next->os->name),
5149 exp_nameop (SIZEOF, l->next->os->name));
5151 if (phdrs != NULL && l->os->phdrs == NULL)
5152 l->os->phdrs = phdrs;
5156 lang_nocrossref_type *nc;
5158 nc = xmalloc (sizeof *nc);
5159 nc->name = l->os->name;
5160 nc->next = nocrossref;
5169 if (nocrossref != NULL)
5170 lang_add_nocrossref (nocrossref);
5173 overlay_list = NULL;
5177 /* Version handling. This is only useful for ELF. */
5179 /* This global variable holds the version tree that we build. */
5181 struct bfd_elf_version_tree *lang_elf_version_info;
5183 /* If PREV is NULL, return first version pattern matching particular symbol.
5184 If PREV is non-NULL, return first version pattern matching particular
5185 symbol after PREV (previously returned by lang_vers_match). */
5187 static struct bfd_elf_version_expr *
5188 lang_vers_match (struct bfd_elf_version_expr_head *head,
5189 struct bfd_elf_version_expr *prev,
5192 const char *cxx_sym = sym;
5193 const char *java_sym = sym;
5194 struct bfd_elf_version_expr *expr = NULL;
5196 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5198 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5202 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5204 java_sym = cplus_demangle (sym, DMGL_JAVA);
5209 if (head->htab && (prev == NULL || prev->symbol))
5211 struct bfd_elf_version_expr e;
5213 switch (prev ? prev->mask : 0)
5216 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5219 expr = htab_find (head->htab, &e);
5220 while (expr && strcmp (expr->symbol, sym) == 0)
5221 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5227 case BFD_ELF_VERSION_C_TYPE:
5228 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5231 expr = htab_find (head->htab, &e);
5232 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5233 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5239 case BFD_ELF_VERSION_CXX_TYPE:
5240 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5242 e.symbol = java_sym;
5243 expr = htab_find (head->htab, &e);
5244 while (expr && strcmp (expr->symbol, java_sym) == 0)
5245 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5256 /* Finally, try the wildcards. */
5257 if (prev == NULL || prev->symbol)
5258 expr = head->remaining;
5265 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5268 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5270 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5274 if (fnmatch (expr->pattern, s, 0) == 0)
5281 free ((char *) cxx_sym);
5282 if (java_sym != sym)
5283 free ((char *) java_sym);
5287 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5288 return a string pointing to the symbol name. */
5291 realsymbol (const char *pattern)
5294 bfd_boolean changed = FALSE, backslash = FALSE;
5295 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5297 for (p = pattern, s = symbol; *p != '\0'; ++p)
5299 /* It is a glob pattern only if there is no preceding
5301 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5309 /* Remove the preceding backslash. */
5316 backslash = *p == '\\';
5331 /* This is called for each variable name or match expression. */
5333 struct bfd_elf_version_expr *
5334 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5338 struct bfd_elf_version_expr *ret;
5340 ret = xmalloc (sizeof *ret);
5345 ret->symbol = realsymbol (new);
5347 if (lang == NULL || strcasecmp (lang, "C") == 0)
5348 ret->mask = BFD_ELF_VERSION_C_TYPE;
5349 else if (strcasecmp (lang, "C++") == 0)
5350 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5351 else if (strcasecmp (lang, "Java") == 0)
5352 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5355 einfo (_("%X%P: unknown language `%s' in version information\n"),
5357 ret->mask = BFD_ELF_VERSION_C_TYPE;
5360 return ldemul_new_vers_pattern (ret);
5363 /* This is called for each set of variable names and match
5366 struct bfd_elf_version_tree *
5367 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5368 struct bfd_elf_version_expr *locals)
5370 struct bfd_elf_version_tree *ret;
5372 ret = xcalloc (1, sizeof *ret);
5373 ret->globals.list = globals;
5374 ret->locals.list = locals;
5375 ret->match = lang_vers_match;
5376 ret->name_indx = (unsigned int) -1;
5380 /* This static variable keeps track of version indices. */
5382 static int version_index;
5385 version_expr_head_hash (const void *p)
5387 const struct bfd_elf_version_expr *e = p;
5389 return htab_hash_string (e->symbol);
5393 version_expr_head_eq (const void *p1, const void *p2)
5395 const struct bfd_elf_version_expr *e1 = p1;
5396 const struct bfd_elf_version_expr *e2 = p2;
5398 return strcmp (e1->symbol, e2->symbol) == 0;
5402 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5405 struct bfd_elf_version_expr *e, *next;
5406 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5408 for (e = head->list; e; e = e->next)
5412 head->mask |= e->mask;
5417 head->htab = htab_create (count * 2, version_expr_head_hash,
5418 version_expr_head_eq, NULL);
5419 list_loc = &head->list;
5420 remaining_loc = &head->remaining;
5421 for (e = head->list; e; e = next)
5427 remaining_loc = &e->next;
5431 void **loc = htab_find_slot (head->htab, e, INSERT);
5435 struct bfd_elf_version_expr *e1, *last;
5441 if (e1->mask == e->mask)
5449 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5453 /* This is a duplicate. */
5454 /* FIXME: Memory leak. Sometimes pattern is not
5455 xmalloced alone, but in larger chunk of memory. */
5456 /* free (e->symbol); */
5461 e->next = last->next;
5469 list_loc = &e->next;
5473 *remaining_loc = NULL;
5474 *list_loc = head->remaining;
5477 head->remaining = head->list;
5480 /* This is called when we know the name and dependencies of the
5484 lang_register_vers_node (const char *name,
5485 struct bfd_elf_version_tree *version,
5486 struct bfd_elf_version_deps *deps)
5488 struct bfd_elf_version_tree *t, **pp;
5489 struct bfd_elf_version_expr *e1;
5494 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5495 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5497 einfo (_("%X%P: anonymous version tag cannot be combined"
5498 " with other version tags\n"));
5503 /* Make sure this node has a unique name. */
5504 for (t = lang_elf_version_info; t != NULL; t = t->next)
5505 if (strcmp (t->name, name) == 0)
5506 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5508 lang_finalize_version_expr_head (&version->globals);
5509 lang_finalize_version_expr_head (&version->locals);
5511 /* Check the global and local match names, and make sure there
5512 aren't any duplicates. */
5514 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5516 for (t = lang_elf_version_info; t != NULL; t = t->next)
5518 struct bfd_elf_version_expr *e2;
5520 if (t->locals.htab && e1->symbol)
5522 e2 = htab_find (t->locals.htab, e1);
5523 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5525 if (e1->mask == e2->mask)
5526 einfo (_("%X%P: duplicate expression `%s'"
5527 " in version information\n"), e1->symbol);
5531 else if (!e1->symbol)
5532 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5533 if (strcmp (e1->pattern, e2->pattern) == 0
5534 && e1->mask == e2->mask)
5535 einfo (_("%X%P: duplicate expression `%s'"
5536 " in version information\n"), e1->pattern);
5540 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5542 for (t = lang_elf_version_info; t != NULL; t = t->next)
5544 struct bfd_elf_version_expr *e2;
5546 if (t->globals.htab && e1->symbol)
5548 e2 = htab_find (t->globals.htab, e1);
5549 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5551 if (e1->mask == e2->mask)
5552 einfo (_("%X%P: duplicate expression `%s'"
5553 " in version information\n"),
5558 else if (!e1->symbol)
5559 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5560 if (strcmp (e1->pattern, e2->pattern) == 0
5561 && e1->mask == e2->mask)
5562 einfo (_("%X%P: duplicate expression `%s'"
5563 " in version information\n"), e1->pattern);
5567 version->deps = deps;
5568 version->name = name;
5569 if (name[0] != '\0')
5572 version->vernum = version_index;
5575 version->vernum = 0;
5577 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5582 /* This is called when we see a version dependency. */
5584 struct bfd_elf_version_deps *
5585 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5587 struct bfd_elf_version_deps *ret;
5588 struct bfd_elf_version_tree *t;
5590 ret = xmalloc (sizeof *ret);
5593 for (t = lang_elf_version_info; t != NULL; t = t->next)
5595 if (strcmp (t->name, name) == 0)
5597 ret->version_needed = t;
5602 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5608 lang_do_version_exports_section (void)
5610 struct bfd_elf_version_expr *greg = NULL, *lreg;
5612 LANG_FOR_EACH_INPUT_STATEMENT (is)
5614 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5622 contents = xmalloc (len);
5623 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5624 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5627 while (p < contents + len)
5629 greg = lang_new_vers_pattern (greg, p, NULL);
5630 p = strchr (p, '\0') + 1;
5633 /* Do not free the contents, as we used them creating the regex. */
5635 /* Do not include this section in the link. */
5636 sec->flags |= SEC_EXCLUDE;
5639 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5640 lang_register_vers_node (command_line.version_exports_section,
5641 lang_new_vers_node (greg, lreg), NULL);
5645 lang_add_unique (const char *name)
5647 struct unique_sections *ent;
5649 for (ent = unique_section_list; ent; ent = ent->next)
5650 if (strcmp (ent->name, name) == 0)
5653 ent = xmalloc (sizeof *ent);
5654 ent->name = xstrdup (name);
5655 ent->next = unique_section_list;
5656 unique_section_list = ent;