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->add_needed = add_needed;
458 p->as_needed = as_needed;
459 p->whole_archive = whole_archive;
461 lang_statement_append (&input_file_chain,
462 (lang_statement_union_type *) p,
467 lang_input_statement_type *
468 lang_add_input_file (const char *name,
469 lang_input_file_enum_type file_type,
472 lang_has_input_file = TRUE;
473 return new_afile (name, file_type, target, TRUE);
476 /* Build enough state so that the parser can build its tree. */
481 obstack_begin (&stat_obstack, 1000);
483 stat_ptr = &statement_list;
485 lang_list_init (stat_ptr);
487 lang_list_init (&input_file_chain);
488 lang_list_init (&lang_output_section_statement);
489 lang_list_init (&file_chain);
490 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
493 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
495 abs_output_section->bfd_section = bfd_abs_section_ptr;
497 /* The value "3" is ad-hoc, somewhat related to the expected number of
498 DEFINED expressions in a linker script. For most default linker
499 scripts, there are none. Why a hash table then? Well, it's somewhat
500 simpler to re-use working machinery than using a linked list in terms
501 of code-complexity here in ld, besides the initialization which just
502 looks like other code here. */
503 if (bfd_hash_table_init_n (&lang_definedness_table,
504 lang_definedness_newfunc, 3) != TRUE)
505 einfo (_("%P%F: out of memory during initialization"));
507 /* Callers of exp_fold_tree need to increment this. */
508 lang_statement_iteration = 0;
511 /*----------------------------------------------------------------------
512 A region is an area of memory declared with the
513 MEMORY { name:org=exp, len=exp ... }
516 We maintain a list of all the regions here.
518 If no regions are specified in the script, then the default is used
519 which is created when looked up to be the entire data space.
521 If create is true we are creating a region inside a MEMORY block.
522 In this case it is probably an error to create a region that has
523 already been created. If we are not inside a MEMORY block it is
524 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
525 and so we issue a warning. */
527 static lang_memory_region_type *lang_memory_region_list;
528 static lang_memory_region_type **lang_memory_region_list_tail
529 = &lang_memory_region_list;
531 lang_memory_region_type *
532 lang_memory_region_lookup (const char *const name, bfd_boolean create)
534 lang_memory_region_type *p;
535 lang_memory_region_type *new;
537 /* NAME is NULL for LMA memspecs if no region was specified. */
541 for (p = lang_memory_region_list; p != NULL; p = p->next)
542 if (strcmp (p->name, name) == 0)
545 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
551 /* This code used to always use the first region in the list as the
552 default region. I changed it to instead use a region
553 encompassing all of memory as the default region. This permits
554 NOLOAD sections to work reasonably without requiring a region.
555 People should specify what region they mean, if they really want
557 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
559 if (lang_memory_region_list != NULL)
560 return lang_memory_region_list;
564 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
565 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
567 new = stat_alloc (sizeof (lang_memory_region_type));
569 new->name = xstrdup (name);
572 *lang_memory_region_list_tail = new;
573 lang_memory_region_list_tail = &new->next;
577 new->length = ~(bfd_size_type) 0;
579 new->had_full_message = FALSE;
584 static lang_memory_region_type *
585 lang_memory_default (asection *section)
587 lang_memory_region_type *p;
589 flagword sec_flags = section->flags;
591 /* Override SEC_DATA to mean a writable section. */
592 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
593 sec_flags |= SEC_DATA;
595 for (p = lang_memory_region_list; p != NULL; p = p->next)
597 if ((p->flags & sec_flags) != 0
598 && (p->not_flags & sec_flags) == 0)
603 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
606 static lang_output_section_statement_type *
607 lang_output_section_find_1 (const char *const name, int constraint)
609 lang_statement_union_type *u;
610 lang_output_section_statement_type *lookup;
612 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
614 lookup = &u->output_section_statement;
615 if (strcmp (name, lookup->name) == 0
616 && lookup->constraint != -1
617 && (constraint == 0 || constraint == lookup->constraint))
623 lang_output_section_statement_type *
624 lang_output_section_find (const char *const name)
626 return lang_output_section_find_1 (name, 0);
629 static lang_output_section_statement_type *
630 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
632 lang_output_section_statement_type *lookup;
634 lookup = lang_output_section_find_1 (name, constraint);
637 lookup = new_stat (lang_output_section_statement, stat_ptr);
638 lookup->region = NULL;
639 lookup->lma_region = NULL;
641 lookup->block_value = 1;
645 lookup->bfd_section = NULL;
646 lookup->processed = 0;
647 lookup->constraint = constraint;
648 lookup->sectype = normal_section;
649 lookup->addr_tree = NULL;
650 lang_list_init (&lookup->children);
652 lookup->memspec = NULL;
654 lookup->subsection_alignment = -1;
655 lookup->section_alignment = -1;
656 lookup->load_base = NULL;
657 lookup->update_dot_tree = NULL;
658 lookup->phdrs = NULL;
660 lang_statement_append (&lang_output_section_statement,
661 (lang_statement_union_type *) lookup,
667 lang_output_section_statement_type *
668 lang_output_section_statement_lookup (const char *const name)
670 return lang_output_section_statement_lookup_1 (name, 0);
674 lang_map_flags (flagword flag)
676 if (flag & SEC_ALLOC)
682 if (flag & SEC_READONLY)
695 lang_memory_region_type *m;
698 minfo (_("\nMemory Configuration\n\n"));
699 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
700 _("Name"), _("Origin"), _("Length"), _("Attributes"));
702 for (m = lang_memory_region_list; m != NULL; m = m->next)
707 fprintf (config.map_file, "%-16s ", m->name);
709 sprintf_vma (buf, m->origin);
710 minfo ("0x%s ", buf);
718 minfo ("0x%V", m->length);
719 if (m->flags || m->not_flags)
727 lang_map_flags (m->flags);
733 lang_map_flags (m->not_flags);
740 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
742 if (! command_line.reduce_memory_overheads)
744 obstack_begin (&map_obstack, 1000);
745 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
746 bfd_map_over_sections (p, init_map_userdata, 0);
747 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
753 init_map_userdata (abfd, sec, data)
754 bfd *abfd ATTRIBUTE_UNUSED;
756 void *data ATTRIBUTE_UNUSED;
758 fat_section_userdata_type *new_data
759 = ((fat_section_userdata_type *) (stat_alloc
760 (sizeof (fat_section_userdata_type))));
762 ASSERT (get_userdata (sec) == NULL);
763 get_userdata (sec) = new_data;
764 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
768 sort_def_symbol (hash_entry, info)
769 struct bfd_link_hash_entry *hash_entry;
770 void *info ATTRIBUTE_UNUSED;
772 if (hash_entry->type == bfd_link_hash_defined
773 || hash_entry->type == bfd_link_hash_defweak)
775 struct fat_user_section_struct *ud;
776 struct map_symbol_def *def;
778 ud = get_userdata (hash_entry->u.def.section);
781 /* ??? What do we have to do to initialize this beforehand? */
782 /* The first time we get here is bfd_abs_section... */
783 init_map_userdata (0, hash_entry->u.def.section, 0);
784 ud = get_userdata (hash_entry->u.def.section);
786 else if (!ud->map_symbol_def_tail)
787 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
789 def = obstack_alloc (&map_obstack, sizeof *def);
790 def->entry = hash_entry;
791 *(ud->map_symbol_def_tail) = def;
792 ud->map_symbol_def_tail = &def->next;
797 /* Initialize an output section. */
800 init_os (lang_output_section_statement_type *s)
802 lean_section_userdata_type *new;
804 if (s->bfd_section != NULL)
807 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
808 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
810 new = stat_alloc (SECTION_USERDATA_SIZE);
811 memset (new, 0, SECTION_USERDATA_SIZE);
813 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
814 if (s->bfd_section == NULL)
815 s->bfd_section = bfd_make_section (output_bfd, s->name);
816 if (s->bfd_section == NULL)
818 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
819 output_bfd->xvec->name, s->name);
821 s->bfd_section->output_section = s->bfd_section;
823 /* We initialize an output sections output offset to minus its own
824 vma to allow us to output a section through itself. */
825 s->bfd_section->output_offset = 0;
826 get_userdata (s->bfd_section) = new;
828 /* If there is a base address, make sure that any sections it might
829 mention are initialized. */
830 if (s->addr_tree != NULL)
831 exp_init_os (s->addr_tree);
833 if (s->load_base != NULL)
834 exp_init_os (s->load_base);
837 /* Make sure that all output sections mentioned in an expression are
841 exp_init_os (etree_type *exp)
843 switch (exp->type.node_class)
846 exp_init_os (exp->assign.src);
850 exp_init_os (exp->binary.lhs);
851 exp_init_os (exp->binary.rhs);
855 exp_init_os (exp->trinary.cond);
856 exp_init_os (exp->trinary.lhs);
857 exp_init_os (exp->trinary.rhs);
861 exp_init_os (exp->assert_s.child);
865 exp_init_os (exp->unary.child);
869 switch (exp->type.node_code)
875 lang_output_section_statement_type *os;
877 os = lang_output_section_find (exp->name.name);
878 if (os != NULL && os->bfd_section == NULL)
890 section_already_linked (bfd *abfd, asection *sec, void *data)
892 lang_input_statement_type *entry = data;
894 /* If we are only reading symbols from this object, then we want to
895 discard all sections. */
896 if (entry->just_syms_flag)
898 bfd_link_just_syms (sec, &link_info);
902 bfd_section_already_linked (abfd, sec);
905 /* The wild routines.
907 These expand statements like *(.text) and foo.o to a list of
908 explicit actions, like foo.o(.text), bar.o(.text) and
909 foo.o(.text, .data). */
911 /* Return TRUE if the PATTERN argument is a wildcard pattern.
912 Although backslashes are treated specially if a pattern contains
913 wildcards, we do not consider the mere presence of a backslash to
914 be enough to cause the pattern to be treated as a wildcard.
915 That lets us handle DOS filenames more naturally. */
918 wildcardp (const char *pattern)
922 for (s = pattern; *s != '\0'; ++s)
930 /* Add SECTION to the output section OUTPUT. Do this by creating a
931 lang_input_section statement which is placed at PTR. FILE is the
932 input file which holds SECTION. */
935 lang_add_section (lang_statement_list_type *ptr,
937 lang_output_section_statement_type *output,
938 lang_input_statement_type *file)
940 flagword flags = section->flags;
943 /* Discard sections marked with SEC_EXCLUDE. */
944 discard = (flags & SEC_EXCLUDE) != 0;
946 /* Discard input sections which are assigned to a section named
947 DISCARD_SECTION_NAME. */
948 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
951 /* Discard debugging sections if we are stripping debugging
953 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
954 && (flags & SEC_DEBUGGING) != 0)
959 if (section->output_section == NULL)
961 /* This prevents future calls from assigning this section. */
962 section->output_section = bfd_abs_section_ptr;
967 if (section->output_section == NULL)
970 lang_input_section_type *new;
973 if (output->bfd_section == NULL)
976 first = ! output->bfd_section->linker_has_input;
977 output->bfd_section->linker_has_input = 1;
979 /* Add a section reference to the list. */
980 new = new_stat (lang_input_section, ptr);
982 new->section = section;
984 section->output_section = output->bfd_section;
986 flags = section->flags;
988 /* We don't copy the SEC_NEVER_LOAD flag from an input section
989 to an output section, because we want to be able to include a
990 SEC_NEVER_LOAD section in the middle of an otherwise loaded
991 section (I don't know why we want to do this, but we do).
992 build_link_order in ldwrite.c handles this case by turning
993 the embedded SEC_NEVER_LOAD section into a fill. */
995 flags &= ~ SEC_NEVER_LOAD;
997 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
998 already been processed. One reason to do this is that on pe
999 format targets, .text$foo sections go into .text and it's odd
1000 to see .text with SEC_LINK_ONCE set. */
1002 if (! link_info.relocatable)
1003 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1005 /* If this is not the first input section, and the SEC_READONLY
1006 flag is not currently set, then don't set it just because the
1007 input section has it set. */
1009 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1010 flags &= ~ SEC_READONLY;
1012 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1014 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1015 != (flags & (SEC_MERGE | SEC_STRINGS))
1016 || ((flags & SEC_MERGE)
1017 && section->output_section->entsize != section->entsize)))
1019 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1020 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1023 section->output_section->flags |= flags;
1025 if (flags & SEC_MERGE)
1026 section->output_section->entsize = section->entsize;
1028 /* If SEC_READONLY is not set in the input section, then clear
1029 it from the output section. */
1030 if ((section->flags & SEC_READONLY) == 0)
1031 section->output_section->flags &= ~SEC_READONLY;
1033 switch (output->sectype)
1035 case normal_section:
1040 case overlay_section:
1041 output->bfd_section->flags &= ~SEC_ALLOC;
1043 case noload_section:
1044 output->bfd_section->flags &= ~SEC_LOAD;
1045 output->bfd_section->flags |= SEC_NEVER_LOAD;
1049 /* Copy over SEC_SMALL_DATA. */
1050 if (section->flags & SEC_SMALL_DATA)
1051 section->output_section->flags |= SEC_SMALL_DATA;
1053 if (section->alignment_power > output->bfd_section->alignment_power)
1054 output->bfd_section->alignment_power = section->alignment_power;
1056 /* If supplied an alignment, then force it. */
1057 if (output->section_alignment != -1)
1058 output->bfd_section->alignment_power = output->section_alignment;
1060 if (section->flags & SEC_BLOCK)
1062 section->output_section->flags |= SEC_BLOCK;
1063 /* FIXME: This value should really be obtained from the bfd... */
1064 output->block_value = 128;
1069 /* Handle wildcard sorting. This returns the lang_input_section which
1070 should follow the one we are going to create for SECTION and FILE,
1071 based on the sorting requirements of WILD. It returns NULL if the
1072 new section should just go at the end of the current list. */
1074 static lang_statement_union_type *
1075 wild_sort (lang_wild_statement_type *wild,
1076 struct wildcard_list *sec,
1077 lang_input_statement_type *file,
1080 const char *section_name;
1081 lang_statement_union_type *l;
1083 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1086 section_name = bfd_get_section_name (file->the_bfd, section);
1087 for (l = wild->children.head; l != NULL; l = l->header.next)
1089 lang_input_section_type *ls;
1091 if (l->header.type != lang_input_section_enum)
1093 ls = &l->input_section;
1095 /* Sorting by filename takes precedence over sorting by section
1098 if (wild->filenames_sorted)
1100 const char *fn, *ln;
1104 /* The PE support for the .idata section as generated by
1105 dlltool assumes that files will be sorted by the name of
1106 the archive and then the name of the file within the
1109 if (file->the_bfd != NULL
1110 && bfd_my_archive (file->the_bfd) != NULL)
1112 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1117 fn = file->filename;
1121 if (ls->ifile->the_bfd != NULL
1122 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1124 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1129 ln = ls->ifile->filename;
1133 i = strcmp (fn, ln);
1142 fn = file->filename;
1144 ln = ls->ifile->filename;
1146 i = strcmp (fn, ln);
1154 /* Here either the files are not sorted by name, or we are
1155 looking at the sections for this file. */
1157 if (sec != NULL && sec->spec.sorted)
1159 if (strcmp (section_name,
1160 bfd_get_section_name (ls->ifile->the_bfd,
1170 /* Expand a wild statement for a particular FILE. SECTION may be
1171 NULL, in which case it is a wild card. */
1174 output_section_callback (lang_wild_statement_type *ptr,
1175 struct wildcard_list *sec,
1177 lang_input_statement_type *file,
1180 lang_statement_union_type *before;
1182 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1183 if (unique_section_p (section))
1186 before = wild_sort (ptr, sec, file, section);
1188 /* Here BEFORE points to the lang_input_section which
1189 should follow the one we are about to add. If BEFORE
1190 is NULL, then the section should just go at the end
1191 of the current list. */
1194 lang_add_section (&ptr->children, section,
1195 (lang_output_section_statement_type *) output,
1199 lang_statement_list_type list;
1200 lang_statement_union_type **pp;
1202 lang_list_init (&list);
1203 lang_add_section (&list, section,
1204 (lang_output_section_statement_type *) output,
1207 /* If we are discarding the section, LIST.HEAD will
1209 if (list.head != NULL)
1211 ASSERT (list.head->header.next == NULL);
1213 for (pp = &ptr->children.head;
1215 pp = &(*pp)->header.next)
1216 ASSERT (*pp != NULL);
1218 list.head->header.next = *pp;
1224 /* Check if all sections in a wild statement for a particular FILE
1228 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1229 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1231 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1234 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1235 if (unique_section_p (section))
1238 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1239 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1242 /* This is passed a file name which must have been seen already and
1243 added to the statement tree. We will see if it has been opened
1244 already and had its symbols read. If not then we'll read it. */
1246 static lang_input_statement_type *
1247 lookup_name (const char *name)
1249 lang_input_statement_type *search;
1251 for (search = (lang_input_statement_type *) input_file_chain.head;
1253 search = (lang_input_statement_type *) search->next_real_file)
1255 /* Use the local_sym_name as the name of the file that has
1256 already been loaded as filename might have been transformed
1257 via the search directory lookup mechanism. */
1258 const char * filename = search->local_sym_name;
1260 if (filename == NULL && name == NULL)
1262 if (filename != NULL
1264 && strcmp (filename, name) == 0)
1269 search = new_afile (name, lang_input_file_is_search_file_enum,
1270 default_target, FALSE);
1272 /* If we have already added this file, or this file is not real
1273 (FIXME: can that ever actually happen?) or the name is NULL
1274 (FIXME: can that ever actually happen?) don't add this file. */
1277 || search->filename == NULL)
1280 if (! load_symbols (search, NULL))
1286 /* Get the symbols for an input file. */
1289 load_symbols (lang_input_statement_type *entry,
1290 lang_statement_list_type *place)
1297 ldfile_open_file (entry);
1299 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1300 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1303 lang_statement_list_type *hold;
1304 bfd_boolean bad_load = TRUE;
1305 bfd_boolean save_ldlang_sysrooted_script;
1307 err = bfd_get_error ();
1309 /* See if the emulation has some special knowledge. */
1310 if (ldemul_unrecognized_file (entry))
1313 if (err == bfd_error_file_ambiguously_recognized)
1317 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1318 einfo (_("%B: matching formats:"), entry->the_bfd);
1319 for (p = matching; *p != NULL; p++)
1323 else if (err != bfd_error_file_not_recognized
1325 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1329 bfd_close (entry->the_bfd);
1330 entry->the_bfd = NULL;
1332 /* Try to interpret the file as a linker script. */
1333 ldfile_open_command_file (entry->filename);
1337 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1338 ldlang_sysrooted_script = entry->sysrooted;
1340 ldfile_assumed_script = TRUE;
1341 parser_input = input_script;
1343 ldfile_assumed_script = FALSE;
1345 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1351 if (ldemul_recognized_file (entry))
1354 /* We don't call ldlang_add_file for an archive. Instead, the
1355 add_symbols entry point will call ldlang_add_file, via the
1356 add_archive_element callback, for each element of the archive
1358 switch (bfd_get_format (entry->the_bfd))
1364 ldlang_add_file (entry);
1365 if (trace_files || trace_file_tries)
1366 info_msg ("%I\n", entry);
1370 if (entry->whole_archive)
1373 bfd_boolean loaded = TRUE;
1377 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1382 if (! bfd_check_format (member, bfd_object))
1384 einfo (_("%F%B: member %B in archive is not an object\n"),
1385 entry->the_bfd, member);
1389 if (! ((*link_info.callbacks->add_archive_element)
1390 (&link_info, member, "--whole-archive")))
1393 if (! bfd_link_add_symbols (member, &link_info))
1395 einfo (_("%F%B: could not read symbols: %E\n"), member);
1400 entry->loaded = loaded;
1406 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1407 entry->loaded = TRUE;
1409 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1411 return entry->loaded;
1414 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1415 may be NULL, indicating that it is a wildcard. Separate
1416 lang_input_section statements are created for each part of the
1417 expansion; they are added after the wild statement S. OUTPUT is
1418 the output section. */
1421 wild (lang_wild_statement_type *s,
1422 const char *target ATTRIBUTE_UNUSED,
1423 lang_output_section_statement_type *output)
1425 struct wildcard_list *sec;
1427 walk_wild (s, output_section_callback, output);
1429 for (sec = s->section_list; sec != NULL; sec = sec->next)
1431 if (default_common_section != NULL)
1433 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1435 /* Remember the section that common is going to in case we
1436 later get something which doesn't know where to put it. */
1437 default_common_section = output;
1442 /* Return TRUE iff target is the sought target. */
1445 get_target (const bfd_target *target, void *data)
1447 const char *sought = data;
1449 return strcmp (target->name, sought) == 0;
1452 /* Like strcpy() but convert to lower case as well. */
1455 stricpy (char *dest, char *src)
1459 while ((c = *src++) != 0)
1460 *dest++ = TOLOWER (c);
1465 /* Remove the first occurrence of needle (if any) in haystack
1469 strcut (char *haystack, char *needle)
1471 haystack = strstr (haystack, needle);
1477 for (src = haystack + strlen (needle); *src;)
1478 *haystack++ = *src++;
1484 /* Compare two target format name strings.
1485 Return a value indicating how "similar" they are. */
1488 name_compare (char *first, char *second)
1494 copy1 = xmalloc (strlen (first) + 1);
1495 copy2 = xmalloc (strlen (second) + 1);
1497 /* Convert the names to lower case. */
1498 stricpy (copy1, first);
1499 stricpy (copy2, second);
1501 /* Remove size and endian strings from the name. */
1502 strcut (copy1, "big");
1503 strcut (copy1, "little");
1504 strcut (copy2, "big");
1505 strcut (copy2, "little");
1507 /* Return a value based on how many characters match,
1508 starting from the beginning. If both strings are
1509 the same then return 10 * their length. */
1510 for (result = 0; copy1[result] == copy2[result]; result++)
1511 if (copy1[result] == 0)
1523 /* Set by closest_target_match() below. */
1524 static const bfd_target *winner;
1526 /* Scan all the valid bfd targets looking for one that has the endianness
1527 requirement that was specified on the command line, and is the nearest
1528 match to the original output target. */
1531 closest_target_match (const bfd_target *target, void *data)
1533 const bfd_target *original = data;
1535 if (command_line.endian == ENDIAN_BIG
1536 && target->byteorder != BFD_ENDIAN_BIG)
1539 if (command_line.endian == ENDIAN_LITTLE
1540 && target->byteorder != BFD_ENDIAN_LITTLE)
1543 /* Must be the same flavour. */
1544 if (target->flavour != original->flavour)
1547 /* If we have not found a potential winner yet, then record this one. */
1554 /* Oh dear, we now have two potential candidates for a successful match.
1555 Compare their names and choose the better one. */
1556 if (name_compare (target->name, original->name)
1557 > name_compare (winner->name, original->name))
1560 /* Keep on searching until wqe have checked them all. */
1564 /* Return the BFD target format of the first input file. */
1567 get_first_input_target (void)
1569 char *target = NULL;
1571 LANG_FOR_EACH_INPUT_STATEMENT (s)
1573 if (s->header.type == lang_input_statement_enum
1576 ldfile_open_file (s);
1578 if (s->the_bfd != NULL
1579 && bfd_check_format (s->the_bfd, bfd_object))
1581 target = bfd_get_target (s->the_bfd);
1593 lang_get_output_target (void)
1597 /* Has the user told us which output format to use? */
1598 if (output_target != NULL)
1599 return output_target;
1601 /* No - has the current target been set to something other than
1603 if (current_target != default_target)
1604 return current_target;
1606 /* No - can we determine the format of the first input file? */
1607 target = get_first_input_target ();
1611 /* Failed - use the default output target. */
1612 return default_target;
1615 /* Open the output file. */
1618 open_output (const char *name)
1622 output_target = lang_get_output_target ();
1624 /* Has the user requested a particular endianness on the command
1626 if (command_line.endian != ENDIAN_UNSET)
1628 const bfd_target *target;
1629 enum bfd_endian desired_endian;
1631 /* Get the chosen target. */
1632 target = bfd_search_for_target (get_target, (void *) output_target);
1634 /* If the target is not supported, we cannot do anything. */
1637 if (command_line.endian == ENDIAN_BIG)
1638 desired_endian = BFD_ENDIAN_BIG;
1640 desired_endian = BFD_ENDIAN_LITTLE;
1642 /* See if the target has the wrong endianness. This should
1643 not happen if the linker script has provided big and
1644 little endian alternatives, but some scrips don't do
1646 if (target->byteorder != desired_endian)
1648 /* If it does, then see if the target provides
1649 an alternative with the correct endianness. */
1650 if (target->alternative_target != NULL
1651 && (target->alternative_target->byteorder == desired_endian))
1652 output_target = target->alternative_target->name;
1655 /* Try to find a target as similar as possible to
1656 the default target, but which has the desired
1657 endian characteristic. */
1658 bfd_search_for_target (closest_target_match,
1661 /* Oh dear - we could not find any targets that
1662 satisfy our requirements. */
1664 einfo (_("%P: warning: could not find any targets"
1665 " that match endianness requirement\n"));
1667 output_target = winner->name;
1673 output = bfd_openw (name, output_target);
1677 if (bfd_get_error () == bfd_error_invalid_target)
1678 einfo (_("%P%F: target %s not found\n"), output_target);
1680 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1683 delete_output_file_on_failure = TRUE;
1686 output->flags |= D_PAGED;
1689 if (! bfd_set_format (output, bfd_object))
1690 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1691 if (! bfd_set_arch_mach (output,
1692 ldfile_output_architecture,
1693 ldfile_output_machine))
1694 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1696 link_info.hash = bfd_link_hash_table_create (output);
1697 if (link_info.hash == NULL)
1698 einfo (_("%P%F: can not create link hash table: %E\n"));
1700 bfd_set_gp_size (output, g_switch_value);
1705 ldlang_open_output (lang_statement_union_type *statement)
1707 switch (statement->header.type)
1709 case lang_output_statement_enum:
1710 ASSERT (output_bfd == NULL);
1711 output_bfd = open_output (statement->output_statement.name);
1712 ldemul_set_output_arch ();
1713 if (config.magic_demand_paged && !link_info.relocatable)
1714 output_bfd->flags |= D_PAGED;
1716 output_bfd->flags &= ~D_PAGED;
1717 if (config.text_read_only)
1718 output_bfd->flags |= WP_TEXT;
1720 output_bfd->flags &= ~WP_TEXT;
1721 if (link_info.traditional_format)
1722 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1724 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1727 case lang_target_statement_enum:
1728 current_target = statement->target_statement.target;
1735 /* Convert between addresses in bytes and sizes in octets.
1736 For currently supported targets, octets_per_byte is always a power
1737 of two, so we can use shifts. */
1738 #define TO_ADDR(X) ((X) >> opb_shift)
1739 #define TO_SIZE(X) ((X) << opb_shift)
1741 /* Support the above. */
1742 static unsigned int opb_shift = 0;
1747 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1748 ldfile_output_machine);
1751 while ((x & 1) == 0)
1759 /* Open all the input files. */
1762 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1764 for (; s != NULL; s = s->header.next)
1766 switch (s->header.type)
1768 case lang_constructors_statement_enum:
1769 open_input_bfds (constructor_list.head, force);
1771 case lang_output_section_statement_enum:
1772 open_input_bfds (s->output_section_statement.children.head, force);
1774 case lang_wild_statement_enum:
1775 /* Maybe we should load the file's symbols. */
1776 if (s->wild_statement.filename
1777 && ! wildcardp (s->wild_statement.filename))
1778 lookup_name (s->wild_statement.filename);
1779 open_input_bfds (s->wild_statement.children.head, force);
1781 case lang_group_statement_enum:
1783 struct bfd_link_hash_entry *undefs;
1785 /* We must continually search the entries in the group
1786 until no new symbols are added to the list of undefined
1791 undefs = link_info.hash->undefs_tail;
1792 open_input_bfds (s->group_statement.children.head, TRUE);
1794 while (undefs != link_info.hash->undefs_tail);
1797 case lang_target_statement_enum:
1798 current_target = s->target_statement.target;
1800 case lang_input_statement_enum:
1801 if (s->input_statement.real)
1803 lang_statement_list_type add;
1805 s->input_statement.target = current_target;
1807 /* If we are being called from within a group, and this
1808 is an archive which has already been searched, then
1809 force it to be researched unless the whole archive
1810 has been loaded already. */
1812 && !s->input_statement.whole_archive
1813 && s->input_statement.loaded
1814 && bfd_check_format (s->input_statement.the_bfd,
1816 s->input_statement.loaded = FALSE;
1818 lang_list_init (&add);
1820 if (! load_symbols (&s->input_statement, &add))
1821 config.make_executable = FALSE;
1823 if (add.head != NULL)
1825 *add.tail = s->header.next;
1826 s->header.next = add.head;
1836 /* If there are [COMMONS] statements, put a wild one into the bss
1840 lang_reasonable_defaults (void)
1843 lang_output_section_statement_lookup (".text");
1844 lang_output_section_statement_lookup (".data");
1846 default_common_section = lang_output_section_statement_lookup (".bss");
1848 if (!placed_commons)
1850 lang_wild_statement_type *new =
1851 new_stat (lang_wild_statement,
1852 &default_common_section->children);
1854 new->section_name = "COMMON";
1855 new->filename = NULL;
1856 lang_list_init (&new->children);
1861 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1864 lang_track_definedness (const char *name)
1866 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1867 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1870 /* New-function for the definedness hash table. */
1872 static struct bfd_hash_entry *
1873 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1874 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1875 const char *name ATTRIBUTE_UNUSED)
1877 struct lang_definedness_hash_entry *ret
1878 = (struct lang_definedness_hash_entry *) entry;
1881 ret = (struct lang_definedness_hash_entry *)
1882 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1885 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1887 ret->iteration = -1;
1891 /* Return the iteration when the definition of NAME was last updated. A
1892 value of -1 means that the symbol is not defined in the linker script
1893 or the command line, but may be defined in the linker symbol table. */
1896 lang_symbol_definition_iteration (const char *name)
1898 struct lang_definedness_hash_entry *defentry
1899 = (struct lang_definedness_hash_entry *)
1900 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1902 /* We've already created this one on the presence of DEFINED in the
1903 script, so it can't be NULL unless something is borked elsewhere in
1905 if (defentry == NULL)
1908 return defentry->iteration;
1911 /* Update the definedness state of NAME. */
1914 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1916 struct lang_definedness_hash_entry *defentry
1917 = (struct lang_definedness_hash_entry *)
1918 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1920 /* We don't keep track of symbols not tested with DEFINED. */
1921 if (defentry == NULL)
1924 /* If the symbol was already defined, and not from an earlier statement
1925 iteration, don't update the definedness iteration, because that'd
1926 make the symbol seem defined in the linker script at this point, and
1927 it wasn't; it was defined in some object. If we do anyway, DEFINED
1928 would start to yield false before this point and the construct "sym =
1929 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1931 if (h->type != bfd_link_hash_undefined
1932 && h->type != bfd_link_hash_common
1933 && h->type != bfd_link_hash_new
1934 && defentry->iteration == -1)
1937 defentry->iteration = lang_statement_iteration;
1940 /* Add the supplied name to the symbol table as an undefined reference.
1941 This is a two step process as the symbol table doesn't even exist at
1942 the time the ld command line is processed. First we put the name
1943 on a list, then, once the output file has been opened, transfer the
1944 name to the symbol table. */
1946 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
1948 #define ldlang_undef_chain_list_head entry_symbol.next
1951 ldlang_add_undef (const char *const name)
1953 ldlang_undef_chain_list_type *new =
1954 stat_alloc (sizeof (ldlang_undef_chain_list_type));
1956 new->next = ldlang_undef_chain_list_head;
1957 ldlang_undef_chain_list_head = new;
1959 new->name = xstrdup (name);
1961 if (output_bfd != NULL)
1962 insert_undefined (new->name);
1965 /* Insert NAME as undefined in the symbol table. */
1968 insert_undefined (const char *name)
1970 struct bfd_link_hash_entry *h;
1972 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
1974 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1975 if (h->type == bfd_link_hash_new)
1977 h->type = bfd_link_hash_undefined;
1978 h->u.undef.abfd = NULL;
1979 bfd_link_add_undef (link_info.hash, h);
1983 /* Run through the list of undefineds created above and place them
1984 into the linker hash table as undefined symbols belonging to the
1988 lang_place_undefineds (void)
1990 ldlang_undef_chain_list_type *ptr;
1992 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
1993 insert_undefined (ptr->name);
1996 /* Check for all readonly or some readwrite sections. */
1999 check_input_sections
2000 (lang_statement_union_type *s,
2001 lang_output_section_statement_type *output_section_statement)
2003 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2005 switch (s->header.type)
2007 case lang_wild_statement_enum:
2008 walk_wild (&s->wild_statement, check_section_callback,
2009 output_section_statement);
2010 if (! output_section_statement->all_input_readonly)
2013 case lang_constructors_statement_enum:
2014 check_input_sections (constructor_list.head,
2015 output_section_statement);
2016 if (! output_section_statement->all_input_readonly)
2019 case lang_group_statement_enum:
2020 check_input_sections (s->group_statement.children.head,
2021 output_section_statement);
2022 if (! output_section_statement->all_input_readonly)
2031 /* Open input files and attach to output sections. */
2034 map_input_to_output_sections
2035 (lang_statement_union_type *s, const char *target,
2036 lang_output_section_statement_type *output_section_statement)
2038 for (; s != NULL; s = s->header.next)
2040 switch (s->header.type)
2042 case lang_wild_statement_enum:
2043 wild (&s->wild_statement, target, output_section_statement);
2045 case lang_constructors_statement_enum:
2046 map_input_to_output_sections (constructor_list.head,
2048 output_section_statement);
2050 case lang_output_section_statement_enum:
2051 if (s->output_section_statement.constraint)
2053 if (s->output_section_statement.constraint == -1)
2055 s->output_section_statement.all_input_readonly = TRUE;
2056 check_input_sections (s->output_section_statement.children.head,
2057 &s->output_section_statement);
2058 if ((s->output_section_statement.all_input_readonly
2059 && s->output_section_statement.constraint == ONLY_IF_RW)
2060 || (!s->output_section_statement.all_input_readonly
2061 && s->output_section_statement.constraint == ONLY_IF_RO))
2063 s->output_section_statement.constraint = -1;
2068 map_input_to_output_sections (s->output_section_statement.children.head,
2070 &s->output_section_statement);
2072 case lang_output_statement_enum:
2074 case lang_target_statement_enum:
2075 target = s->target_statement.target;
2077 case lang_group_statement_enum:
2078 map_input_to_output_sections (s->group_statement.children.head,
2080 output_section_statement);
2082 case lang_data_statement_enum:
2083 /* Make sure that any sections mentioned in the expression
2085 exp_init_os (s->data_statement.exp);
2087 case lang_fill_statement_enum:
2088 case lang_input_section_enum:
2089 case lang_object_symbols_statement_enum:
2090 case lang_reloc_statement_enum:
2091 case lang_padding_statement_enum:
2092 case lang_input_statement_enum:
2093 if (output_section_statement != NULL
2094 && output_section_statement->bfd_section == NULL)
2095 init_os (output_section_statement);
2097 case lang_assignment_statement_enum:
2098 if (output_section_statement != NULL
2099 && output_section_statement->bfd_section == NULL)
2100 init_os (output_section_statement);
2102 /* Make sure that any sections mentioned in the assignment
2104 exp_init_os (s->assignment_statement.exp);
2106 case lang_afile_asection_pair_statement_enum:
2109 case lang_address_statement_enum:
2110 /* Mark the specified section with the supplied address. */
2112 lang_output_section_statement_type *os =
2113 lang_output_section_statement_lookup
2114 (s->address_statement.section_name);
2116 if (os->bfd_section == NULL)
2118 os->addr_tree = s->address_statement.address;
2125 /* An output section might have been removed after its statement was
2126 added. For example, ldemul_before_allocation can remove dynamic
2127 sections if they turn out to be not needed. Clean them up here. */
2130 strip_excluded_output_sections (void)
2132 lang_statement_union_type *u;
2134 for (u = lang_output_section_statement.head;
2136 u = u->output_section_statement.next)
2138 lang_output_section_statement_type *os;
2141 os = &u->output_section_statement;
2142 if (os->constraint == -1)
2144 s = os->bfd_section;
2145 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2149 os->bfd_section = NULL;
2151 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2154 bfd_section_list_remove (output_bfd, p);
2155 output_bfd->section_count--;
2163 print_output_section_statement
2164 (lang_output_section_statement_type *output_section_statement)
2166 asection *section = output_section_statement->bfd_section;
2169 if (output_section_statement != abs_output_section)
2171 minfo ("\n%s", output_section_statement->name);
2173 if (section != NULL)
2175 print_dot = section->vma;
2177 len = strlen (output_section_statement->name);
2178 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2183 while (len < SECTION_NAME_MAP_LENGTH)
2189 minfo ("0x%V %W", section->vma, section->size);
2191 if (output_section_statement->load_base != NULL)
2195 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2196 "load base", lang_final_phase_enum);
2197 minfo (_(" load address 0x%V"), addr);
2204 print_statement_list (output_section_statement->children.head,
2205 output_section_statement);
2209 print_assignment (lang_assignment_statement_type *assignment,
2210 lang_output_section_statement_type *output_section)
2213 etree_value_type result;
2215 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2218 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2219 lang_final_phase_enum, print_dot, &print_dot);
2225 value = result.value + result.section->bfd_section->vma;
2226 dst = assignment->exp->assign.dst;
2228 minfo ("0x%V", value);
2229 if (dst[0] == '.' && dst[1] == 0)
2242 exp_print_tree (assignment->exp);
2248 print_input_statement (lang_input_statement_type *statm)
2250 if (statm->filename != NULL)
2252 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2256 /* Print all symbols defined in a particular section. This is called
2257 via bfd_link_hash_traverse, or by print_all_symbols. */
2260 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2262 asection *sec = ptr;
2264 if ((hash_entry->type == bfd_link_hash_defined
2265 || hash_entry->type == bfd_link_hash_defweak)
2266 && sec == hash_entry->u.def.section)
2270 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2273 (hash_entry->u.def.value
2274 + hash_entry->u.def.section->output_offset
2275 + hash_entry->u.def.section->output_section->vma));
2277 minfo (" %T\n", hash_entry->root.string);
2284 print_all_symbols (sec)
2287 struct fat_user_section_struct *ud = get_userdata (sec);
2288 struct map_symbol_def *def;
2290 *ud->map_symbol_def_tail = 0;
2291 for (def = ud->map_symbol_def_head; def; def = def->next)
2292 print_one_symbol (def->entry, sec);
2295 /* Print information about an input section to the map file. */
2298 print_input_section (lang_input_section_type *in)
2300 asection *i = in->section;
2301 bfd_size_type size = i->size;
2310 minfo ("%s", i->name);
2312 len = 1 + strlen (i->name);
2313 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2318 while (len < SECTION_NAME_MAP_LENGTH)
2324 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2325 addr = i->output_section->vma + i->output_offset;
2332 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2334 if (size != i->rawsize && i->rawsize != 0)
2336 len = SECTION_NAME_MAP_LENGTH + 3;
2348 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2351 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2353 if (command_line.reduce_memory_overheads)
2354 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2356 print_all_symbols (i);
2358 print_dot = addr + TO_ADDR (size);
2364 print_fill_statement (lang_fill_statement_type *fill)
2368 fputs (" FILL mask 0x", config.map_file);
2369 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2370 fprintf (config.map_file, "%02x", *p);
2371 fputs ("\n", config.map_file);
2375 print_data_statement (lang_data_statement_type *data)
2383 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2386 addr = data->output_vma;
2387 if (data->output_section != NULL)
2388 addr += data->output_section->vma;
2416 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2418 if (data->exp->type.node_class != etree_value)
2421 exp_print_tree (data->exp);
2426 print_dot = addr + TO_ADDR (size);
2429 /* Print an address statement. These are generated by options like
2433 print_address_statement (lang_address_statement_type *address)
2435 minfo (_("Address of section %s set to "), address->section_name);
2436 exp_print_tree (address->address);
2440 /* Print a reloc statement. */
2443 print_reloc_statement (lang_reloc_statement_type *reloc)
2450 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2453 addr = reloc->output_vma;
2454 if (reloc->output_section != NULL)
2455 addr += reloc->output_section->vma;
2457 size = bfd_get_reloc_size (reloc->howto);
2459 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2461 if (reloc->name != NULL)
2462 minfo ("%s+", reloc->name);
2464 minfo ("%s+", reloc->section->name);
2466 exp_print_tree (reloc->addend_exp);
2470 print_dot = addr + TO_ADDR (size);
2474 print_padding_statement (lang_padding_statement_type *s)
2482 len = sizeof " *fill*" - 1;
2483 while (len < SECTION_NAME_MAP_LENGTH)
2489 addr = s->output_offset;
2490 if (s->output_section != NULL)
2491 addr += s->output_section->vma;
2492 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2494 if (s->fill->size != 0)
2498 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2499 fprintf (config.map_file, "%02x", *p);
2504 print_dot = addr + TO_ADDR (s->size);
2508 print_wild_statement (lang_wild_statement_type *w,
2509 lang_output_section_statement_type *os)
2511 struct wildcard_list *sec;
2515 if (w->filenames_sorted)
2517 if (w->filename != NULL)
2518 minfo ("%s", w->filename);
2521 if (w->filenames_sorted)
2525 for (sec = w->section_list; sec; sec = sec->next)
2527 if (sec->spec.sorted)
2529 if (sec->spec.exclude_name_list != NULL)
2532 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2533 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2534 minfo (" %s", tmp->name);
2537 if (sec->spec.name != NULL)
2538 minfo ("%s", sec->spec.name);
2541 if (sec->spec.sorted)
2550 print_statement_list (w->children.head, os);
2553 /* Print a group statement. */
2556 print_group (lang_group_statement_type *s,
2557 lang_output_section_statement_type *os)
2559 fprintf (config.map_file, "START GROUP\n");
2560 print_statement_list (s->children.head, os);
2561 fprintf (config.map_file, "END GROUP\n");
2564 /* Print the list of statements in S.
2565 This can be called for any statement type. */
2568 print_statement_list (lang_statement_union_type *s,
2569 lang_output_section_statement_type *os)
2573 print_statement (s, os);
2578 /* Print the first statement in statement list S.
2579 This can be called for any statement type. */
2582 print_statement (lang_statement_union_type *s,
2583 lang_output_section_statement_type *os)
2585 switch (s->header.type)
2588 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2591 case lang_constructors_statement_enum:
2592 if (constructor_list.head != NULL)
2594 if (constructors_sorted)
2595 minfo (" SORT (CONSTRUCTORS)\n");
2597 minfo (" CONSTRUCTORS\n");
2598 print_statement_list (constructor_list.head, os);
2601 case lang_wild_statement_enum:
2602 print_wild_statement (&s->wild_statement, os);
2604 case lang_address_statement_enum:
2605 print_address_statement (&s->address_statement);
2607 case lang_object_symbols_statement_enum:
2608 minfo (" CREATE_OBJECT_SYMBOLS\n");
2610 case lang_fill_statement_enum:
2611 print_fill_statement (&s->fill_statement);
2613 case lang_data_statement_enum:
2614 print_data_statement (&s->data_statement);
2616 case lang_reloc_statement_enum:
2617 print_reloc_statement (&s->reloc_statement);
2619 case lang_input_section_enum:
2620 print_input_section (&s->input_section);
2622 case lang_padding_statement_enum:
2623 print_padding_statement (&s->padding_statement);
2625 case lang_output_section_statement_enum:
2626 print_output_section_statement (&s->output_section_statement);
2628 case lang_assignment_statement_enum:
2629 print_assignment (&s->assignment_statement, os);
2631 case lang_target_statement_enum:
2632 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2634 case lang_output_statement_enum:
2635 minfo ("OUTPUT(%s", s->output_statement.name);
2636 if (output_target != NULL)
2637 minfo (" %s", output_target);
2640 case lang_input_statement_enum:
2641 print_input_statement (&s->input_statement);
2643 case lang_group_statement_enum:
2644 print_group (&s->group_statement, os);
2646 case lang_afile_asection_pair_statement_enum:
2653 print_statements (void)
2655 print_statement_list (statement_list.head, abs_output_section);
2658 /* Print the first N statements in statement list S to STDERR.
2659 If N == 0, nothing is printed.
2660 If N < 0, the entire list is printed.
2661 Intended to be called from GDB. */
2664 dprint_statement (lang_statement_union_type *s, int n)
2666 FILE *map_save = config.map_file;
2668 config.map_file = stderr;
2671 print_statement_list (s, abs_output_section);
2674 while (s && --n >= 0)
2676 print_statement (s, abs_output_section);
2681 config.map_file = map_save;
2685 insert_pad (lang_statement_union_type **ptr,
2687 unsigned int alignment_needed,
2688 asection *output_section,
2691 static fill_type zero_fill = { 1, { 0 } };
2692 lang_statement_union_type *pad;
2694 pad = ((lang_statement_union_type *)
2695 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2696 if (ptr != &statement_list.head
2697 && pad->header.type == lang_padding_statement_enum
2698 && pad->padding_statement.output_section == output_section)
2700 /* Use the existing pad statement. The above test on output
2701 section is probably redundant, but it doesn't hurt to check. */
2705 /* Make a new padding statement, linked into existing chain. */
2706 pad = stat_alloc (sizeof (lang_padding_statement_type));
2707 pad->header.next = *ptr;
2709 pad->header.type = lang_padding_statement_enum;
2710 pad->padding_statement.output_section = output_section;
2713 pad->padding_statement.fill = fill;
2715 pad->padding_statement.output_offset = dot - output_section->vma;
2716 pad->padding_statement.size = alignment_needed;
2717 output_section->size += alignment_needed;
2720 /* Work out how much this section will move the dot point. */
2724 (lang_statement_union_type **this_ptr,
2725 lang_output_section_statement_type *output_section_statement,
2729 lang_input_section_type *is = &((*this_ptr)->input_section);
2730 asection *i = is->section;
2732 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2734 unsigned int alignment_needed;
2737 /* Align this section first to the input sections requirement,
2738 then to the output section's requirement. If this alignment
2739 is greater than any seen before, then record it too. Perform
2740 the alignment by inserting a magic 'padding' statement. */
2742 if (output_section_statement->subsection_alignment != -1)
2743 i->alignment_power = output_section_statement->subsection_alignment;
2745 o = output_section_statement->bfd_section;
2746 if (o->alignment_power < i->alignment_power)
2747 o->alignment_power = i->alignment_power;
2749 alignment_needed = align_power (dot, i->alignment_power) - dot;
2751 if (alignment_needed != 0)
2753 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2754 dot += alignment_needed;
2757 /* Remember where in the output section this input section goes. */
2759 i->output_offset = dot - o->vma;
2761 /* Mark how big the output section must be to contain this now. */
2762 dot += TO_ADDR (i->size);
2763 o->size = TO_SIZE (dot - o->vma);
2767 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2773 #define IGNORE_SECTION(s) \
2774 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2775 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2776 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2779 /* Check to see if any allocated sections overlap with other allocated
2780 sections. This can happen when the linker script specifically specifies
2781 the output section addresses of the two sections. */
2784 lang_check_section_addresses (void)
2788 /* Scan all sections in the output list. */
2789 for (s = output_bfd->sections; s != NULL; s = s->next)
2793 /* Ignore sections which are not loaded or which have no contents. */
2794 if (IGNORE_SECTION (s))
2797 /* Once we reach section 's' stop our seach. This prevents two
2798 warning messages from being produced, one for 'section A overlaps
2799 section B' and one for 'section B overlaps section A'. */
2800 for (os = output_bfd->sections; os != s; os = os->next)
2807 /* Only consider loadable sections with real contents. */
2808 if (IGNORE_SECTION (os))
2811 /* We must check the sections' LMA addresses not their
2812 VMA addresses because overlay sections can have
2813 overlapping VMAs but they must have distinct LMAs. */
2814 s_start = bfd_section_lma (output_bfd, s);
2815 os_start = bfd_section_lma (output_bfd, os);
2816 s_end = s_start + TO_ADDR (s->size) - 1;
2817 os_end = os_start + TO_ADDR (os->size) - 1;
2819 /* Look for an overlap. */
2820 if ((s_end < os_start) || (s_start > os_end))
2824 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2825 s->name, s_start, s_end, os->name, os_start, os_end);
2827 /* Once we have found one overlap for this section,
2828 stop looking for others. */
2834 /* Make sure the new address is within the region. We explicitly permit the
2835 current address to be at the exact end of the region when the address is
2836 non-zero, in case the region is at the end of addressable memory and the
2837 calculation wraps around. */
2840 os_region_check (lang_output_section_statement_type *os,
2841 lang_memory_region_type *region,
2845 if ((region->current < region->origin
2846 || (region->current - region->origin > region->length))
2847 && ((region->current != region->origin + region->length)
2852 einfo (_("%X%P: address 0x%v of %B section %s"
2853 " is not within region %s\n"),
2855 os->bfd_section->owner,
2856 os->bfd_section->name,
2861 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2863 os->bfd_section->owner,
2864 os->bfd_section->name);
2866 /* Reset the region pointer. */
2867 region->current = region->origin;
2871 /* Set the sizes for all the output sections. */
2874 lang_size_sections_1
2875 (lang_statement_union_type *s,
2876 lang_output_section_statement_type *output_section_statement,
2877 lang_statement_union_type **prev,
2881 bfd_boolean check_regions)
2883 /* Size up the sections from their constituent parts. */
2884 for (; s != NULL; s = s->header.next)
2886 switch (s->header.type)
2888 case lang_output_section_statement_enum:
2891 lang_output_section_statement_type *os;
2893 os = &s->output_section_statement;
2894 if (os->bfd_section == NULL)
2895 /* This section was never actually created. */
2898 /* If this is a COFF shared library section, use the size and
2899 address from the input section. FIXME: This is COFF
2900 specific; it would be cleaner if there were some other way
2901 to do this, but nothing simple comes to mind. */
2902 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2906 if (os->children.head == NULL
2907 || os->children.head->header.next != NULL
2908 || (os->children.head->header.type
2909 != lang_input_section_enum))
2910 einfo (_("%P%X: Internal error on COFF shared library"
2911 " section %s\n"), os->name);
2913 input = os->children.head->input_section.section;
2914 bfd_set_section_vma (os->bfd_section->owner,
2916 bfd_section_vma (input->owner, input));
2917 os->bfd_section->size = input->size;
2921 if (bfd_is_abs_section (os->bfd_section))
2923 /* No matter what happens, an abs section starts at zero. */
2924 ASSERT (os->bfd_section->vma == 0);
2928 if (os->addr_tree == NULL)
2930 /* No address specified for this section, get one
2931 from the region specification. */
2932 if (os->region == NULL
2933 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
2934 && os->region->name[0] == '*'
2935 && strcmp (os->region->name,
2936 DEFAULT_MEMORY_REGION) == 0))
2938 os->region = lang_memory_default (os->bfd_section);
2941 /* If a loadable section is using the default memory
2942 region, and some non default memory regions were
2943 defined, issue an error message. */
2944 if (!IGNORE_SECTION (os->bfd_section)
2945 && ! link_info.relocatable
2947 && strcmp (os->region->name,
2948 DEFAULT_MEMORY_REGION) == 0
2949 && lang_memory_region_list != NULL
2950 && (strcmp (lang_memory_region_list->name,
2951 DEFAULT_MEMORY_REGION) != 0
2952 || lang_memory_region_list->next != NULL))
2954 /* By default this is an error rather than just a
2955 warning because if we allocate the section to the
2956 default memory region we can end up creating an
2957 excessively large binary, or even seg faulting when
2958 attempting to perform a negative seek. See
2959 sources.redhat.com/ml/binutils/2003-04/msg00423.html
2960 for an example of this. This behaviour can be
2961 overridden by the using the --no-check-sections
2963 if (command_line.check_section_addresses)
2964 einfo (_("%P%F: error: no memory region specified"
2965 " for loadable section `%s'\n"),
2966 bfd_get_section_name (output_bfd,
2969 einfo (_("%P: warning: no memory region specified"
2970 " for loadable section `%s'\n"),
2971 bfd_get_section_name (output_bfd,
2975 dot = os->region->current;
2977 if (os->section_alignment == -1)
2982 dot = align_power (dot,
2983 os->bfd_section->alignment_power);
2985 if (dot != olddot && config.warn_section_align)
2986 einfo (_("%P: warning: changing start of section"
2987 " %s by %u bytes\n"),
2988 os->name, (unsigned int) (dot - olddot));
2996 r = exp_fold_tree (os->addr_tree,
2998 lang_allocating_phase_enum,
3003 einfo (_("%F%S: non constant or forward reference"
3004 " address expression for section %s\n"),
3007 dot = r.value + r.section->bfd_section->vma;
3010 /* The section starts here.
3011 First, align to what the section needs. */
3013 if (os->section_alignment != -1)
3014 dot = align_power (dot, os->section_alignment);
3016 bfd_set_section_vma (0, os->bfd_section, dot);
3018 os->bfd_section->output_offset = 0;
3021 lang_size_sections_1 (os->children.head, os, &os->children.head,
3022 os->fill, dot, relax, check_regions);
3024 /* Put the section within the requested block size, or
3025 align at the block boundary. */
3026 after = ((os->bfd_section->vma
3027 + TO_ADDR (os->bfd_section->size)
3028 + os->block_value - 1)
3029 & - (bfd_vma) os->block_value);
3031 if (bfd_is_abs_section (os->bfd_section))
3032 ASSERT (after == os->bfd_section->vma);
3034 os->bfd_section->size
3035 = TO_SIZE (after - os->bfd_section->vma);
3037 dot = os->bfd_section->vma;
3038 /* .tbss sections effectively have zero size. */
3039 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3040 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3041 || link_info.relocatable)
3042 dot += TO_ADDR (os->bfd_section->size);
3046 if (os->update_dot_tree != 0)
3047 exp_fold_tree (os->update_dot_tree, abs_output_section,
3048 lang_allocating_phase_enum, dot, &dot);
3050 /* Update dot in the region ?
3051 We only do this if the section is going to be allocated,
3052 since unallocated sections do not contribute to the region's
3053 overall size in memory.
3055 If the SEC_NEVER_LOAD bit is not set, it will affect the
3056 addresses of sections after it. We have to update
3058 if (os->region != NULL
3059 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3060 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3062 os->region->current = dot;
3065 /* Make sure the new address is within the region. */
3066 os_region_check (os, os->region, os->addr_tree,
3067 os->bfd_section->vma);
3069 /* If there's no load address specified, use the run
3070 region as the load region. */
3071 if (os->lma_region == NULL && os->load_base == NULL)
3072 os->lma_region = os->region;
3074 if (os->lma_region != NULL && os->lma_region != os->region)
3076 /* Set load_base, which will be handled later. */
3077 os->load_base = exp_intop (os->lma_region->current);
3078 os->lma_region->current +=
3079 TO_ADDR (os->bfd_section->size);
3081 os_region_check (os, os->lma_region, NULL,
3082 os->bfd_section->lma);
3088 case lang_constructors_statement_enum:
3089 dot = lang_size_sections_1 (constructor_list.head,
3090 output_section_statement,
3091 &s->wild_statement.children.head,
3092 fill, dot, relax, check_regions);
3095 case lang_data_statement_enum:
3097 unsigned int size = 0;
3099 s->data_statement.output_vma =
3100 dot - output_section_statement->bfd_section->vma;
3101 s->data_statement.output_section =
3102 output_section_statement->bfd_section;
3104 /* We might refer to provided symbols in the expression, and
3105 need to mark them as needed. */
3106 exp_fold_tree (s->data_statement.exp, abs_output_section,
3107 lang_allocating_phase_enum, dot, &dot);
3109 switch (s->data_statement.type)
3127 if (size < TO_SIZE ((unsigned) 1))
3128 size = TO_SIZE ((unsigned) 1);
3129 dot += TO_ADDR (size);
3130 output_section_statement->bfd_section->size += size;
3131 /* The output section gets contents, and then we inspect for
3132 any flags set in the input script which override any ALLOC. */
3133 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3134 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3136 output_section_statement->bfd_section->flags |=
3137 SEC_ALLOC | SEC_LOAD;
3142 case lang_reloc_statement_enum:
3146 s->reloc_statement.output_vma =
3147 dot - output_section_statement->bfd_section->vma;
3148 s->reloc_statement.output_section =
3149 output_section_statement->bfd_section;
3150 size = bfd_get_reloc_size (s->reloc_statement.howto);
3151 dot += TO_ADDR (size);
3152 output_section_statement->bfd_section->size += size;
3156 case lang_wild_statement_enum:
3158 dot = lang_size_sections_1 (s->wild_statement.children.head,
3159 output_section_statement,
3160 &s->wild_statement.children.head,
3161 fill, dot, relax, check_regions);
3165 case lang_object_symbols_statement_enum:
3166 link_info.create_object_symbols_section =
3167 output_section_statement->bfd_section;
3169 case lang_output_statement_enum:
3170 case lang_target_statement_enum:
3172 case lang_input_section_enum:
3176 i = (*prev)->input_section.section;
3181 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3182 einfo (_("%P%F: can't relax section: %E\n"));
3186 dot = size_input_section (prev, output_section_statement,
3187 output_section_statement->fill, dot);
3190 case lang_input_statement_enum:
3192 case lang_fill_statement_enum:
3193 s->fill_statement.output_section =
3194 output_section_statement->bfd_section;
3196 fill = s->fill_statement.fill;
3198 case lang_assignment_statement_enum:
3200 bfd_vma newdot = dot;
3202 exp_fold_tree (s->assignment_statement.exp,
3203 output_section_statement,
3204 lang_allocating_phase_enum,
3210 if (output_section_statement == abs_output_section)
3212 /* If we don't have an output section, then just adjust
3213 the default memory address. */
3214 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3215 FALSE)->current = newdot;
3219 /* Insert a pad after this statement. We can't
3220 put the pad before when relaxing, in case the
3221 assignment references dot. */
3222 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3223 output_section_statement->bfd_section, dot);
3225 /* Don't neuter the pad below when relaxing. */
3229 /* If dot is advanced, this implies that the section should
3230 have space allocated to it, unless the user has explicitly
3231 stated that the section should never be loaded. */
3232 if (!(output_section_statement->flags
3233 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3234 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3241 case lang_padding_statement_enum:
3242 /* If this is the first time lang_size_sections is called,
3243 we won't have any padding statements. If this is the
3244 second or later passes when relaxing, we should allow
3245 padding to shrink. If padding is needed on this pass, it
3246 will be added back in. */
3247 s->padding_statement.size = 0;
3249 /* Make sure output_offset is valid. If relaxation shrinks
3250 the section and this pad isn't needed, it's possible to
3251 have output_offset larger than the final size of the
3252 section. bfd_set_section_contents will complain even for
3253 a pad size of zero. */
3254 s->padding_statement.output_offset
3255 = dot - output_section_statement->bfd_section->vma;
3258 case lang_group_statement_enum:
3259 dot = lang_size_sections_1 (s->group_statement.children.head,
3260 output_section_statement,
3261 &s->group_statement.children.head,
3262 fill, dot, relax, check_regions);
3269 /* We can only get here when relaxing is turned on. */
3270 case lang_address_statement_enum:
3273 prev = &s->header.next;
3280 (lang_statement_union_type *s,
3281 lang_output_section_statement_type *output_section_statement,
3282 lang_statement_union_type **prev,
3286 bfd_boolean check_regions)
3290 /* Callers of exp_fold_tree need to increment this. */
3291 lang_statement_iteration++;
3293 exp_data_seg.phase = exp_dataseg_none;
3294 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3295 dot, relax, check_regions);
3296 if (exp_data_seg.phase == exp_dataseg_end_seen
3297 && link_info.relro && exp_data_seg.relro_end)
3299 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3300 to put exp_data_seg.relro on a (common) page boundary. */
3302 exp_data_seg.phase = exp_dataseg_relro_adjust;
3303 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3304 dot, relax, check_regions);
3305 link_info.relro_start = exp_data_seg.base;
3306 link_info.relro_end = exp_data_seg.relro_end;
3308 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3310 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3311 a page could be saved in the data segment. */
3312 bfd_vma first, last;
3314 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3315 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3317 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3318 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3319 && first + last <= exp_data_seg.pagesize)
3321 exp_data_seg.phase = exp_dataseg_adjust;
3322 lang_statement_iteration++;
3323 result = lang_size_sections_1 (s, output_section_statement, prev,
3324 fill, dot, relax, check_regions);
3331 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3334 lang_do_assignments_1
3335 (lang_statement_union_type *s,
3336 lang_output_section_statement_type *output_section_statement,
3340 for (; s != NULL; s = s->header.next)
3342 switch (s->header.type)
3344 case lang_constructors_statement_enum:
3345 dot = lang_do_assignments_1 (constructor_list.head,
3346 output_section_statement,
3351 case lang_output_section_statement_enum:
3353 lang_output_section_statement_type *os;
3355 os = &(s->output_section_statement);
3356 if (os->bfd_section != NULL)
3358 dot = os->bfd_section->vma;
3359 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3360 /* .tbss sections effectively have zero size. */
3361 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3362 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3363 || link_info.relocatable)
3364 dot += TO_ADDR (os->bfd_section->size);
3368 /* If nothing has been placed into the output section then
3369 it won't have a bfd_section. */
3370 if (os->bfd_section)
3372 os->bfd_section->lma
3373 = exp_get_abs_int (os->load_base, 0, "load base",
3374 lang_final_phase_enum);
3379 case lang_wild_statement_enum:
3381 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3382 output_section_statement,
3387 case lang_object_symbols_statement_enum:
3388 case lang_output_statement_enum:
3389 case lang_target_statement_enum:
3391 case lang_common_statement_enum:
3394 case lang_data_statement_enum:
3396 etree_value_type value;
3398 value = exp_fold_tree (s->data_statement.exp,
3400 lang_final_phase_enum, dot, &dot);
3402 einfo (_("%F%P: invalid data statement\n"));
3403 s->data_statement.value
3404 = value.value + value.section->bfd_section->vma;
3408 switch (s->data_statement.type)
3426 if (size < TO_SIZE ((unsigned) 1))
3427 size = TO_SIZE ((unsigned) 1);
3428 dot += TO_ADDR (size);
3432 case lang_reloc_statement_enum:
3434 etree_value_type value;
3436 value = exp_fold_tree (s->reloc_statement.addend_exp,
3438 lang_final_phase_enum, dot, &dot);
3439 s->reloc_statement.addend_value = value.value;
3441 einfo (_("%F%P: invalid reloc statement\n"));
3443 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3446 case lang_input_section_enum:
3448 asection *in = s->input_section.section;
3450 if ((in->flags & SEC_EXCLUDE) == 0)
3451 dot += TO_ADDR (in->size);
3455 case lang_input_statement_enum:
3457 case lang_fill_statement_enum:
3458 fill = s->fill_statement.fill;
3460 case lang_assignment_statement_enum:
3462 exp_fold_tree (s->assignment_statement.exp,
3463 output_section_statement,
3464 lang_final_phase_enum,
3470 case lang_padding_statement_enum:
3471 dot += TO_ADDR (s->padding_statement.size);
3474 case lang_group_statement_enum:
3475 dot = lang_do_assignments_1 (s->group_statement.children.head,
3476 output_section_statement,
3484 case lang_address_statement_enum:
3494 (lang_statement_union_type *s,
3495 lang_output_section_statement_type *output_section_statement,
3499 /* Callers of exp_fold_tree need to increment this. */
3500 lang_statement_iteration++;
3501 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3504 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3505 operator .startof. (section_name), it produces an undefined symbol
3506 .startof.section_name. Similarly, when it sees
3507 .sizeof. (section_name), it produces an undefined symbol
3508 .sizeof.section_name. For all the output sections, we look for
3509 such symbols, and set them to the correct value. */
3512 lang_set_startof (void)
3516 if (link_info.relocatable)
3519 for (s = output_bfd->sections; s != NULL; s = s->next)
3521 const char *secname;
3523 struct bfd_link_hash_entry *h;
3525 secname = bfd_get_section_name (output_bfd, s);
3526 buf = xmalloc (10 + strlen (secname));
3528 sprintf (buf, ".startof.%s", secname);
3529 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3530 if (h != NULL && h->type == bfd_link_hash_undefined)
3532 h->type = bfd_link_hash_defined;
3533 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3534 h->u.def.section = bfd_abs_section_ptr;
3537 sprintf (buf, ".sizeof.%s", secname);
3538 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3539 if (h != NULL && h->type == bfd_link_hash_undefined)
3541 h->type = bfd_link_hash_defined;
3542 h->u.def.value = TO_ADDR (s->size);
3543 h->u.def.section = bfd_abs_section_ptr;
3553 struct bfd_link_hash_entry *h;
3556 if (link_info.relocatable || link_info.shared)
3561 if (entry_symbol.name == NULL)
3563 /* No entry has been specified. Look for start, but don't warn
3564 if we don't find it. */
3565 entry_symbol.name = "start";
3569 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3570 FALSE, FALSE, TRUE);
3572 && (h->type == bfd_link_hash_defined
3573 || h->type == bfd_link_hash_defweak)
3574 && h->u.def.section->output_section != NULL)
3578 val = (h->u.def.value
3579 + bfd_get_section_vma (output_bfd,
3580 h->u.def.section->output_section)
3581 + h->u.def.section->output_offset);
3582 if (! bfd_set_start_address (output_bfd, val))
3583 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3590 /* We couldn't find the entry symbol. Try parsing it as a
3592 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3595 if (! bfd_set_start_address (output_bfd, val))
3596 einfo (_("%P%F: can't set start address\n"));
3602 /* Can't find the entry symbol, and it's not a number. Use
3603 the first address in the text section. */
3604 ts = bfd_get_section_by_name (output_bfd, entry_section);
3608 einfo (_("%P: warning: cannot find entry symbol %s;"
3609 " defaulting to %V\n"),
3611 bfd_get_section_vma (output_bfd, ts));
3612 if (! bfd_set_start_address (output_bfd,
3613 bfd_get_section_vma (output_bfd,
3615 einfo (_("%P%F: can't set start address\n"));
3620 einfo (_("%P: warning: cannot find entry symbol %s;"
3621 " not setting start address\n"),
3627 /* Don't bfd_hash_table_free (&lang_definedness_table);
3628 map file output may result in a call of lang_track_definedness. */
3631 /* This is a small function used when we want to ignore errors from
3635 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3637 /* Don't do anything. */
3640 /* Check that the architecture of all the input files is compatible
3641 with the output file. Also call the backend to let it do any
3642 other checking that is needed. */
3647 lang_statement_union_type *file;
3649 const bfd_arch_info_type *compatible;
3651 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3653 input_bfd = file->input_statement.the_bfd;
3655 = bfd_arch_get_compatible (input_bfd, output_bfd,
3656 command_line.accept_unknown_input_arch);
3658 /* In general it is not possible to perform a relocatable
3659 link between differing object formats when the input
3660 file has relocations, because the relocations in the
3661 input format may not have equivalent representations in
3662 the output format (and besides BFD does not translate
3663 relocs for other link purposes than a final link). */
3664 if ((link_info.relocatable || link_info.emitrelocations)
3665 && (compatible == NULL
3666 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3667 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3669 einfo (_("%P%F: Relocatable linking with relocations from"
3670 " format %s (%B) to format %s (%B) is not supported\n"),
3671 bfd_get_target (input_bfd), input_bfd,
3672 bfd_get_target (output_bfd), output_bfd);
3673 /* einfo with %F exits. */
3676 if (compatible == NULL)
3678 if (command_line.warn_mismatch)
3679 einfo (_("%P: warning: %s architecture of input file `%B'"
3680 " is incompatible with %s output\n"),
3681 bfd_printable_name (input_bfd), input_bfd,
3682 bfd_printable_name (output_bfd));
3684 else if (bfd_count_sections (input_bfd))
3686 /* If the input bfd has no contents, it shouldn't set the
3687 private data of the output bfd. */
3689 bfd_error_handler_type pfn = NULL;
3691 /* If we aren't supposed to warn about mismatched input
3692 files, temporarily set the BFD error handler to a
3693 function which will do nothing. We still want to call
3694 bfd_merge_private_bfd_data, since it may set up
3695 information which is needed in the output file. */
3696 if (! command_line.warn_mismatch)
3697 pfn = bfd_set_error_handler (ignore_bfd_errors);
3698 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3700 if (command_line.warn_mismatch)
3701 einfo (_("%P%X: failed to merge target specific data"
3702 " of file %B\n"), input_bfd);
3704 if (! command_line.warn_mismatch)
3705 bfd_set_error_handler (pfn);
3710 /* Look through all the global common symbols and attach them to the
3711 correct section. The -sort-common command line switch may be used
3712 to roughly sort the entries by size. */
3717 if (command_line.inhibit_common_definition)
3719 if (link_info.relocatable
3720 && ! command_line.force_common_definition)
3723 if (! config.sort_common)
3724 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3729 for (power = 4; power >= 0; power--)
3730 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3734 /* Place one common symbol in the correct section. */
3737 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3739 unsigned int power_of_two;
3743 if (h->type != bfd_link_hash_common)
3747 power_of_two = h->u.c.p->alignment_power;
3749 if (config.sort_common
3750 && power_of_two < (unsigned int) *(int *) info)
3753 section = h->u.c.p->section;
3755 /* Increase the size of the section to align the common sym. */
3756 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3757 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3759 /* Adjust the alignment if necessary. */
3760 if (power_of_two > section->alignment_power)
3761 section->alignment_power = power_of_two;
3763 /* Change the symbol from common to defined. */
3764 h->type = bfd_link_hash_defined;
3765 h->u.def.section = section;
3766 h->u.def.value = section->size;
3768 /* Increase the size of the section. */
3769 section->size += size;
3771 /* Make sure the section is allocated in memory, and make sure that
3772 it is no longer a common section. */
3773 section->flags |= SEC_ALLOC;
3774 section->flags &= ~SEC_IS_COMMON;
3776 if (config.map_file != NULL)
3778 static bfd_boolean header_printed;
3783 if (! header_printed)
3785 minfo (_("\nAllocating common symbols\n"));
3786 minfo (_("Common symbol size file\n\n"));
3787 header_printed = TRUE;
3790 name = demangle (h->root.string);
3792 len = strlen (name);
3807 if (size <= 0xffffffff)
3808 sprintf (buf, "%lx", (unsigned long) size);
3810 sprintf_vma (buf, size);
3820 minfo ("%B\n", section->owner);
3826 /* Run through the input files and ensure that every input section has
3827 somewhere to go. If one is found without a destination then create
3828 an input request and place it into the statement tree. */
3831 lang_place_orphans (void)
3833 LANG_FOR_EACH_INPUT_STATEMENT (file)
3837 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3839 if (s->output_section == NULL)
3841 /* This section of the file is not attached, root
3842 around for a sensible place for it to go. */
3844 if (file->just_syms_flag)
3847 if ((s->flags & SEC_EXCLUDE) != 0)
3848 s->output_section = bfd_abs_section_ptr;
3849 else if (strcmp (s->name, "COMMON") == 0)
3851 /* This is a lonely common section which must have
3852 come from an archive. We attach to the section
3853 with the wildcard. */
3854 if (! link_info.relocatable
3855 || command_line.force_common_definition)
3857 if (default_common_section == NULL)
3860 /* This message happens when using the
3861 svr3.ifile linker script, so I have
3863 info_msg (_("%P: no [COMMON] command,"
3864 " defaulting to .bss\n"));
3866 default_common_section =
3867 lang_output_section_statement_lookup (".bss");
3870 lang_add_section (&default_common_section->children, s,
3871 default_common_section, file);
3874 else if (ldemul_place_orphan (file, s))
3878 lang_output_section_statement_type *os;
3880 os = lang_output_section_statement_lookup (s->name);
3881 lang_add_section (&os->children, s, os, file);
3889 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3891 flagword *ptr_flags;
3893 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3899 *ptr_flags |= SEC_ALLOC;
3903 *ptr_flags |= SEC_READONLY;
3907 *ptr_flags |= SEC_DATA;
3911 *ptr_flags |= SEC_CODE;
3916 *ptr_flags |= SEC_LOAD;
3920 einfo (_("%P%F: invalid syntax in flags\n"));
3927 /* Call a function on each input file. This function will be called
3928 on an archive, but not on the elements. */
3931 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3933 lang_input_statement_type *f;
3935 for (f = (lang_input_statement_type *) input_file_chain.head;
3937 f = (lang_input_statement_type *) f->next_real_file)
3941 /* Call a function on each file. The function will be called on all
3942 the elements of an archive which are included in the link, but will
3943 not be called on the archive file itself. */
3946 lang_for_each_file (void (*func) (lang_input_statement_type *))
3948 LANG_FOR_EACH_INPUT_STATEMENT (f)
3955 ldlang_add_file (lang_input_statement_type *entry)
3959 lang_statement_append (&file_chain,
3960 (lang_statement_union_type *) entry,
3963 /* The BFD linker needs to have a list of all input BFDs involved in
3965 ASSERT (entry->the_bfd->link_next == NULL);
3966 ASSERT (entry->the_bfd != output_bfd);
3967 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3969 *pp = entry->the_bfd;
3970 entry->the_bfd->usrdata = entry;
3971 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3973 /* Look through the sections and check for any which should not be
3974 included in the link. We need to do this now, so that we can
3975 notice when the backend linker tries to report multiple
3976 definition errors for symbols which are in sections we aren't
3977 going to link. FIXME: It might be better to entirely ignore
3978 symbols which are defined in sections which are going to be
3979 discarded. This would require modifying the backend linker for
3980 each backend which might set the SEC_LINK_ONCE flag. If we do
3981 this, we should probably handle SEC_EXCLUDE in the same way. */
3983 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
3987 lang_add_output (const char *name, int from_script)
3989 /* Make -o on command line override OUTPUT in script. */
3990 if (!had_output_filename || !from_script)
3992 output_filename = name;
3993 had_output_filename = TRUE;
3997 static lang_output_section_statement_type *current_section;
4008 for (l = 0; l < 32; l++)
4010 if (i >= (unsigned int) x)
4018 lang_output_section_statement_type *
4019 lang_enter_output_section_statement (const char *output_section_statement_name,
4020 etree_type *address_exp,
4021 enum section_type sectype,
4023 etree_type *subalign,
4027 lang_output_section_statement_type *os;
4031 lang_output_section_statement_lookup_1 (output_section_statement_name,
4034 /* Add this statement to tree. */
4036 add_statement (lang_output_section_statement_enum,
4037 output_section_statement);
4039 /* Make next things chain into subchain of this. */
4041 if (os->addr_tree == NULL)
4043 os->addr_tree = address_exp;
4045 os->sectype = sectype;
4046 if (sectype != noload_section)
4047 os->flags = SEC_NO_FLAGS;
4049 os->flags = SEC_NEVER_LOAD;
4050 os->block_value = 1;
4051 stat_ptr = &os->children;
4053 os->subsection_alignment =
4054 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4055 os->section_alignment =
4056 topower (exp_get_value_int (align, -1, "section alignment", 0));
4058 os->load_base = ebase;
4065 lang_output_statement_type *new =
4066 new_stat (lang_output_statement, stat_ptr);
4068 new->name = output_filename;
4071 /* Reset the current counters in the regions. */
4074 lang_reset_memory_regions (void)
4076 lang_memory_region_type *p = lang_memory_region_list;
4079 for (p = lang_memory_region_list; p != NULL; p = p->next)
4081 p->old_length = (bfd_size_type) (p->current - p->origin);
4082 p->current = p->origin;
4085 for (o = output_bfd->sections; o != NULL; o = o->next)
4087 /* Save the last size for possible use by bfd_relax_section. */
4088 o->rawsize = o->size;
4093 /* Worker for lang_gc_sections_1. */
4096 gc_section_callback (lang_wild_statement_type *ptr,
4097 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4099 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4100 void *data ATTRIBUTE_UNUSED)
4102 /* If the wild pattern was marked KEEP, the member sections
4103 should be as well. */
4104 if (ptr->keep_sections)
4105 section->flags |= SEC_KEEP;
4108 /* Iterate over sections marking them against GC. */
4111 lang_gc_sections_1 (lang_statement_union_type *s)
4113 for (; s != NULL; s = s->header.next)
4115 switch (s->header.type)
4117 case lang_wild_statement_enum:
4118 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4120 case lang_constructors_statement_enum:
4121 lang_gc_sections_1 (constructor_list.head);
4123 case lang_output_section_statement_enum:
4124 lang_gc_sections_1 (s->output_section_statement.children.head);
4126 case lang_group_statement_enum:
4127 lang_gc_sections_1 (s->group_statement.children.head);
4136 lang_gc_sections (void)
4138 struct bfd_link_hash_entry *h;
4139 ldlang_undef_chain_list_type *ulist;
4141 /* Keep all sections so marked in the link script. */
4143 lang_gc_sections_1 (statement_list.head);
4145 /* Keep all sections containing symbols undefined on the command-line,
4146 and the section containing the entry symbol. */
4148 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4150 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4151 FALSE, FALSE, FALSE);
4154 && (h->type == bfd_link_hash_defined
4155 || h->type == bfd_link_hash_defweak)
4156 && ! bfd_is_abs_section (h->u.def.section))
4158 h->u.def.section->flags |= SEC_KEEP;
4162 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4163 the special case of debug info. (See bfd/stabs.c)
4164 Twiddle the flag here, to simplify later linker code. */
4165 if (link_info.relocatable)
4167 LANG_FOR_EACH_INPUT_STATEMENT (f)
4170 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4171 if ((sec->flags & SEC_DEBUGGING) == 0)
4172 sec->flags &= ~SEC_EXCLUDE;
4176 if (command_line.gc_sections)
4177 bfd_gc_sections (output_bfd, &link_info);
4183 lang_reasonable_defaults ();
4184 current_target = default_target;
4186 /* Open the output file. */
4187 lang_for_each_statement (ldlang_open_output);
4190 ldemul_create_output_section_statements ();
4192 /* Add to the hash table all undefineds on the command line. */
4193 lang_place_undefineds ();
4195 if (!bfd_section_already_linked_table_init ())
4196 einfo (_("%P%F: Failed to create hash table\n"));
4198 /* Create a bfd for each input file. */
4199 current_target = default_target;
4200 open_input_bfds (statement_list.head, FALSE);
4202 link_info.gc_sym_list = &entry_symbol;
4203 if (entry_symbol.name == NULL)
4204 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4206 ldemul_after_open ();
4208 bfd_section_already_linked_table_free ();
4210 /* Make sure that we're not mixing architectures. We call this
4211 after all the input files have been opened, but before we do any
4212 other processing, so that any operations merge_private_bfd_data
4213 does on the output file will be known during the rest of the
4217 /* Handle .exports instead of a version script if we're told to do so. */
4218 if (command_line.version_exports_section)
4219 lang_do_version_exports_section ();
4221 /* Build all sets based on the information gathered from the input
4223 ldctor_build_sets ();
4225 /* Remove unreferenced sections if asked to. */
4226 lang_gc_sections ();
4228 /* Size up the common data. */
4231 /* Run through the contours of the script and attach input sections
4232 to the correct output sections. */
4233 map_input_to_output_sections (statement_list.head, NULL, NULL);
4235 /* Find any sections not attached explicitly and handle them. */
4236 lang_place_orphans ();
4238 if (! link_info.relocatable)
4242 /* Merge SEC_MERGE sections. This has to be done after GC of
4243 sections, so that GCed sections are not merged, but before
4244 assigning dynamic symbols, since removing whole input sections
4246 bfd_merge_sections (output_bfd, &link_info);
4248 /* Look for a text section and set the readonly attribute in it. */
4249 found = bfd_get_section_by_name (output_bfd, ".text");
4253 if (config.text_read_only)
4254 found->flags |= SEC_READONLY;
4256 found->flags &= ~SEC_READONLY;
4260 /* Do anything special before sizing sections. This is where ELF
4261 and other back-ends size dynamic sections. */
4262 ldemul_before_allocation ();
4264 if (!link_info.relocatable)
4265 strip_excluded_output_sections ();
4267 /* We must record the program headers before we try to fix the
4268 section positions, since they will affect SIZEOF_HEADERS. */
4269 lang_record_phdrs ();
4271 /* Size up the sections. */
4272 lang_size_sections (statement_list.head, abs_output_section,
4273 &statement_list.head, 0, 0, NULL,
4274 command_line.relax ? FALSE : TRUE);
4276 /* Now run around and relax if we can. */
4277 if (command_line.relax)
4279 /* Keep relaxing until bfd_relax_section gives up. */
4280 bfd_boolean relax_again;
4284 relax_again = FALSE;
4286 /* Note: pe-dll.c does something like this also. If you find
4287 you need to change this code, you probably need to change
4288 pe-dll.c also. DJ */
4290 /* Do all the assignments with our current guesses as to
4292 lang_do_assignments (statement_list.head, abs_output_section,
4295 /* We must do this after lang_do_assignments, because it uses
4297 lang_reset_memory_regions ();
4299 /* Perform another relax pass - this time we know where the
4300 globals are, so can make a better guess. */
4301 lang_size_sections (statement_list.head, abs_output_section,
4302 &statement_list.head, 0, 0, &relax_again, FALSE);
4304 /* If the normal relax is done and the relax finalize pass
4305 is not performed yet, we perform another relax pass. */
4306 if (!relax_again && link_info.need_relax_finalize)
4308 link_info.need_relax_finalize = FALSE;
4312 while (relax_again);
4314 /* Final extra sizing to report errors. */
4315 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4316 lang_reset_memory_regions ();
4317 lang_size_sections (statement_list.head, abs_output_section,
4318 &statement_list.head, 0, 0, NULL, TRUE);
4321 /* See if anything special should be done now we know how big
4323 ldemul_after_allocation ();
4325 /* Fix any .startof. or .sizeof. symbols. */
4326 lang_set_startof ();
4328 /* Do all the assignments, now that we know the final resting places
4329 of all the symbols. */
4331 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4333 /* Make sure that the section addresses make sense. */
4334 if (! link_info.relocatable
4335 && command_line.check_section_addresses)
4336 lang_check_section_addresses ();
4344 /* EXPORTED TO YACC */
4347 lang_add_wild (struct wildcard_spec *filespec,
4348 struct wildcard_list *section_list,
4349 bfd_boolean keep_sections)
4351 struct wildcard_list *curr, *next;
4352 lang_wild_statement_type *new;
4354 /* Reverse the list as the parser puts it back to front. */
4355 for (curr = section_list, section_list = NULL;
4357 section_list = curr, curr = next)
4359 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4360 placed_commons = TRUE;
4363 curr->next = section_list;
4366 if (filespec != NULL && filespec->name != NULL)
4368 if (strcmp (filespec->name, "*") == 0)
4369 filespec->name = NULL;
4370 else if (! wildcardp (filespec->name))
4371 lang_has_input_file = TRUE;
4374 new = new_stat (lang_wild_statement, stat_ptr);
4375 new->filename = NULL;
4376 new->filenames_sorted = FALSE;
4377 if (filespec != NULL)
4379 new->filename = filespec->name;
4380 new->filenames_sorted = filespec->sorted;
4382 new->section_list = section_list;
4383 new->keep_sections = keep_sections;
4384 lang_list_init (&new->children);
4388 lang_section_start (const char *name, etree_type *address)
4390 lang_address_statement_type *ad;
4392 ad = new_stat (lang_address_statement, stat_ptr);
4393 ad->section_name = name;
4394 ad->address = address;
4397 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4398 because of a -e argument on the command line, or zero if this is
4399 called by ENTRY in a linker script. Command line arguments take
4403 lang_add_entry (const char *name, bfd_boolean cmdline)
4405 if (entry_symbol.name == NULL
4407 || ! entry_from_cmdline)
4409 entry_symbol.name = name;
4410 entry_from_cmdline = cmdline;
4415 lang_add_target (const char *name)
4417 lang_target_statement_type *new = new_stat (lang_target_statement,
4425 lang_add_map (const char *name)
4432 map_option_f = TRUE;
4440 lang_add_fill (fill_type *fill)
4442 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4449 lang_add_data (int type, union etree_union *exp)
4452 lang_data_statement_type *new = new_stat (lang_data_statement,
4460 /* Create a new reloc statement. RELOC is the BFD relocation type to
4461 generate. HOWTO is the corresponding howto structure (we could
4462 look this up, but the caller has already done so). SECTION is the
4463 section to generate a reloc against, or NAME is the name of the
4464 symbol to generate a reloc against. Exactly one of SECTION and
4465 NAME must be NULL. ADDEND is an expression for the addend. */
4468 lang_add_reloc (bfd_reloc_code_real_type reloc,
4469 reloc_howto_type *howto,
4472 union etree_union *addend)
4474 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4478 p->section = section;
4480 p->addend_exp = addend;
4482 p->addend_value = 0;
4483 p->output_section = NULL;
4487 lang_assignment_statement_type *
4488 lang_add_assignment (etree_type *exp)
4490 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4498 lang_add_attribute (enum statement_enum attribute)
4500 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4504 lang_startup (const char *name)
4506 if (startup_file != NULL)
4508 einfo (_("%P%Fmultiple STARTUP files\n"));
4510 first_file->filename = name;
4511 first_file->local_sym_name = name;
4512 first_file->real = TRUE;
4514 startup_file = name;
4518 lang_float (bfd_boolean maybe)
4520 lang_float_flag = maybe;
4524 /* Work out the load- and run-time regions from a script statement, and
4525 store them in *LMA_REGION and *REGION respectively.
4527 MEMSPEC is the name of the run-time region, or the value of
4528 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4529 LMA_MEMSPEC is the name of the load-time region, or null if the
4530 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4531 had an explicit load address.
4533 It is an error to specify both a load region and a load address. */
4536 lang_get_regions (lang_memory_region_type **region,
4537 lang_memory_region_type **lma_region,
4538 const char *memspec,
4539 const char *lma_memspec,
4540 bfd_boolean have_lma,
4541 bfd_boolean have_vma)
4543 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4545 /* If no runtime region or VMA has been specified, but the load region
4546 has been specified, then use the load region for the runtime region
4548 if (lma_memspec != NULL
4550 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4551 *region = *lma_region;
4553 *region = lang_memory_region_lookup (memspec, FALSE);
4555 if (have_lma && lma_memspec != 0)
4556 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4560 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4561 lang_output_section_phdr_list *phdrs,
4562 const char *lma_memspec)
4564 lang_get_regions (¤t_section->region,
4565 ¤t_section->lma_region,
4566 memspec, lma_memspec,
4567 current_section->load_base != NULL,
4568 current_section->addr_tree != NULL);
4569 current_section->fill = fill;
4570 current_section->phdrs = phdrs;
4571 stat_ptr = &statement_list;
4574 /* Create an absolute symbol with the given name with the value of the
4575 address of first byte of the section named.
4577 If the symbol already exists, then do nothing. */
4580 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4582 struct bfd_link_hash_entry *h;
4584 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4586 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4588 if (h->type == bfd_link_hash_new
4589 || h->type == bfd_link_hash_undefined)
4593 h->type = bfd_link_hash_defined;
4595 sec = bfd_get_section_by_name (output_bfd, secname);
4599 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4601 h->u.def.section = bfd_abs_section_ptr;
4605 /* Create an absolute symbol with the given name with the value of the
4606 address of the first byte after the end of the section named.
4608 If the symbol already exists, then do nothing. */
4611 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4613 struct bfd_link_hash_entry *h;
4615 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4617 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4619 if (h->type == bfd_link_hash_new
4620 || h->type == bfd_link_hash_undefined)
4624 h->type = bfd_link_hash_defined;
4626 sec = bfd_get_section_by_name (output_bfd, secname);
4630 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4631 + TO_ADDR (sec->size));
4633 h->u.def.section = bfd_abs_section_ptr;
4638 lang_statement_append (lang_statement_list_type *list,
4639 lang_statement_union_type *element,
4640 lang_statement_union_type **field)
4642 *(list->tail) = element;
4646 /* Set the output format type. -oformat overrides scripts. */
4649 lang_add_output_format (const char *format,
4654 if (output_target == NULL || !from_script)
4656 if (command_line.endian == ENDIAN_BIG
4659 else if (command_line.endian == ENDIAN_LITTLE
4663 output_target = format;
4667 /* Enter a group. This creates a new lang_group_statement, and sets
4668 stat_ptr to build new statements within the group. */
4671 lang_enter_group (void)
4673 lang_group_statement_type *g;
4675 g = new_stat (lang_group_statement, stat_ptr);
4676 lang_list_init (&g->children);
4677 stat_ptr = &g->children;
4680 /* Leave a group. This just resets stat_ptr to start writing to the
4681 regular list of statements again. Note that this will not work if
4682 groups can occur inside anything else which can adjust stat_ptr,
4683 but currently they can't. */
4686 lang_leave_group (void)
4688 stat_ptr = &statement_list;
4691 /* Add a new program header. This is called for each entry in a PHDRS
4692 command in a linker script. */
4695 lang_new_phdr (const char *name,
4697 bfd_boolean filehdr,
4702 struct lang_phdr *n, **pp;
4704 n = stat_alloc (sizeof (struct lang_phdr));
4707 n->type = exp_get_value_int (type, 0, "program header type",
4708 lang_final_phase_enum);
4709 n->filehdr = filehdr;
4714 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4719 /* Record the program header information in the output BFD. FIXME: We
4720 should not be calling an ELF specific function here. */
4723 lang_record_phdrs (void)
4727 lang_output_section_phdr_list *last;
4728 struct lang_phdr *l;
4729 lang_statement_union_type *u;
4732 secs = xmalloc (alc * sizeof (asection *));
4734 for (l = lang_phdr_list; l != NULL; l = l->next)
4741 for (u = lang_output_section_statement.head;
4743 u = u->output_section_statement.next)
4745 lang_output_section_statement_type *os;
4746 lang_output_section_phdr_list *pl;
4748 os = &u->output_section_statement;
4749 if (os->constraint == -1)
4757 if (os->sectype == noload_section
4758 || os->bfd_section == NULL
4759 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4764 if (os->bfd_section == NULL)
4767 for (; pl != NULL; pl = pl->next)
4769 if (strcmp (pl->name, l->name) == 0)
4774 secs = xrealloc (secs, alc * sizeof (asection *));
4776 secs[c] = os->bfd_section;
4783 if (l->flags == NULL)
4786 flags = exp_get_vma (l->flags, 0, "phdr flags",
4787 lang_final_phase_enum);
4792 at = exp_get_vma (l->at, 0, "phdr load address",
4793 lang_final_phase_enum);
4795 if (! bfd_record_phdr (output_bfd, l->type,
4796 l->flags != NULL, flags, l->at != NULL,
4797 at, l->filehdr, l->phdrs, c, secs))
4798 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4803 /* Make sure all the phdr assignments succeeded. */
4804 for (u = lang_output_section_statement.head;
4806 u = u->output_section_statement.next)
4808 lang_output_section_phdr_list *pl;
4810 if (u->output_section_statement.constraint == -1
4811 || u->output_section_statement.bfd_section == NULL)
4814 for (pl = u->output_section_statement.phdrs;
4817 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4818 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4819 u->output_section_statement.name, pl->name);
4823 /* Record a list of sections which may not be cross referenced. */
4826 lang_add_nocrossref (lang_nocrossref_type *l)
4828 struct lang_nocrossrefs *n;
4830 n = xmalloc (sizeof *n);
4831 n->next = nocrossref_list;
4833 nocrossref_list = n;
4835 /* Set notice_all so that we get informed about all symbols. */
4836 link_info.notice_all = TRUE;
4839 /* Overlay handling. We handle overlays with some static variables. */
4841 /* The overlay virtual address. */
4842 static etree_type *overlay_vma;
4843 /* And subsection alignment. */
4844 static etree_type *overlay_subalign;
4846 /* An expression for the maximum section size seen so far. */
4847 static etree_type *overlay_max;
4849 /* A list of all the sections in this overlay. */
4851 struct overlay_list {
4852 struct overlay_list *next;
4853 lang_output_section_statement_type *os;
4856 static struct overlay_list *overlay_list;
4858 /* Start handling an overlay. */
4861 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4863 /* The grammar should prevent nested overlays from occurring. */
4864 ASSERT (overlay_vma == NULL
4865 && overlay_subalign == NULL
4866 && overlay_max == NULL);
4868 overlay_vma = vma_expr;
4869 overlay_subalign = subalign;
4872 /* Start a section in an overlay. We handle this by calling
4873 lang_enter_output_section_statement with the correct VMA.
4874 lang_leave_overlay sets up the LMA and memory regions. */
4877 lang_enter_overlay_section (const char *name)
4879 struct overlay_list *n;
4882 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4883 0, overlay_subalign, 0, 0);
4885 /* If this is the first section, then base the VMA of future
4886 sections on this one. This will work correctly even if `.' is
4887 used in the addresses. */
4888 if (overlay_list == NULL)
4889 overlay_vma = exp_nameop (ADDR, name);
4891 /* Remember the section. */
4892 n = xmalloc (sizeof *n);
4893 n->os = current_section;
4894 n->next = overlay_list;
4897 size = exp_nameop (SIZEOF, name);
4899 /* Arrange to work out the maximum section end address. */
4900 if (overlay_max == NULL)
4903 overlay_max = exp_binop (MAX_K, overlay_max, size);
4906 /* Finish a section in an overlay. There isn't any special to do
4910 lang_leave_overlay_section (fill_type *fill,
4911 lang_output_section_phdr_list *phdrs)
4918 name = current_section->name;
4920 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4921 region and that no load-time region has been specified. It doesn't
4922 really matter what we say here, since lang_leave_overlay will
4924 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
4926 /* Define the magic symbols. */
4928 clean = xmalloc (strlen (name) + 1);
4930 for (s1 = name; *s1 != '\0'; s1++)
4931 if (ISALNUM (*s1) || *s1 == '_')
4935 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4936 sprintf (buf, "__load_start_%s", clean);
4937 lang_add_assignment (exp_assop ('=', buf,
4938 exp_nameop (LOADADDR, name)));
4940 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4941 sprintf (buf, "__load_stop_%s", clean);
4942 lang_add_assignment (exp_assop ('=', buf,
4944 exp_nameop (LOADADDR, name),
4945 exp_nameop (SIZEOF, name))));
4950 /* Finish an overlay. If there are any overlay wide settings, this
4951 looks through all the sections in the overlay and sets them. */
4954 lang_leave_overlay (etree_type *lma_expr,
4957 const char *memspec,
4958 lang_output_section_phdr_list *phdrs,
4959 const char *lma_memspec)
4961 lang_memory_region_type *region;
4962 lang_memory_region_type *lma_region;
4963 struct overlay_list *l;
4964 lang_nocrossref_type *nocrossref;
4966 lang_get_regions (®ion, &lma_region,
4967 memspec, lma_memspec,
4968 lma_expr != NULL, FALSE);
4972 /* After setting the size of the last section, set '.' to end of the
4974 if (overlay_list != NULL)
4975 overlay_list->os->update_dot_tree
4976 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4981 struct overlay_list *next;
4983 if (fill != NULL && l->os->fill == NULL)
4986 l->os->region = region;
4987 l->os->lma_region = lma_region;
4989 /* The first section has the load address specified in the
4990 OVERLAY statement. The rest are worked out from that.
4991 The base address is not needed (and should be null) if
4992 an LMA region was specified. */
4994 l->os->load_base = lma_expr;
4995 else if (lma_region == 0)
4996 l->os->load_base = exp_binop ('+',
4997 exp_nameop (LOADADDR, l->next->os->name),
4998 exp_nameop (SIZEOF, l->next->os->name));
5000 if (phdrs != NULL && l->os->phdrs == NULL)
5001 l->os->phdrs = phdrs;
5005 lang_nocrossref_type *nc;
5007 nc = xmalloc (sizeof *nc);
5008 nc->name = l->os->name;
5009 nc->next = nocrossref;
5018 if (nocrossref != NULL)
5019 lang_add_nocrossref (nocrossref);
5022 overlay_list = NULL;
5026 /* Version handling. This is only useful for ELF. */
5028 /* This global variable holds the version tree that we build. */
5030 struct bfd_elf_version_tree *lang_elf_version_info;
5032 /* If PREV is NULL, return first version pattern matching particular symbol.
5033 If PREV is non-NULL, return first version pattern matching particular
5034 symbol after PREV (previously returned by lang_vers_match). */
5036 static struct bfd_elf_version_expr *
5037 lang_vers_match (struct bfd_elf_version_expr_head *head,
5038 struct bfd_elf_version_expr *prev,
5041 const char *cxx_sym = sym;
5042 const char *java_sym = sym;
5043 struct bfd_elf_version_expr *expr = NULL;
5045 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5047 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5051 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5053 java_sym = cplus_demangle (sym, DMGL_JAVA);
5058 if (head->htab && (prev == NULL || prev->symbol))
5060 struct bfd_elf_version_expr e;
5062 switch (prev ? prev->mask : 0)
5065 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5068 expr = htab_find (head->htab, &e);
5069 while (expr && strcmp (expr->symbol, sym) == 0)
5070 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5076 case BFD_ELF_VERSION_C_TYPE:
5077 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5080 expr = htab_find (head->htab, &e);
5081 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5082 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5088 case BFD_ELF_VERSION_CXX_TYPE:
5089 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5091 e.symbol = java_sym;
5092 expr = htab_find (head->htab, &e);
5093 while (expr && strcmp (expr->symbol, java_sym) == 0)
5094 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5105 /* Finally, try the wildcards. */
5106 if (prev == NULL || prev->symbol)
5107 expr = head->remaining;
5114 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5117 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5119 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5123 if (fnmatch (expr->pattern, s, 0) == 0)
5130 free ((char *) cxx_sym);
5131 if (java_sym != sym)
5132 free ((char *) java_sym);
5136 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5137 return a string pointing to the symbol name. */
5140 realsymbol (const char *pattern)
5143 bfd_boolean changed = FALSE, backslash = FALSE;
5144 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5146 for (p = pattern, s = symbol; *p != '\0'; ++p)
5148 /* It is a glob pattern only if there is no preceding
5150 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5158 /* Remove the preceding backslash. */
5165 backslash = *p == '\\';
5180 /* This is called for each variable name or match expression. */
5182 struct bfd_elf_version_expr *
5183 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5187 struct bfd_elf_version_expr *ret;
5189 ret = xmalloc (sizeof *ret);
5194 ret->symbol = realsymbol (new);
5196 if (lang == NULL || strcasecmp (lang, "C") == 0)
5197 ret->mask = BFD_ELF_VERSION_C_TYPE;
5198 else if (strcasecmp (lang, "C++") == 0)
5199 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5200 else if (strcasecmp (lang, "Java") == 0)
5201 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5204 einfo (_("%X%P: unknown language `%s' in version information\n"),
5206 ret->mask = BFD_ELF_VERSION_C_TYPE;
5209 return ldemul_new_vers_pattern (ret);
5212 /* This is called for each set of variable names and match
5215 struct bfd_elf_version_tree *
5216 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5217 struct bfd_elf_version_expr *locals)
5219 struct bfd_elf_version_tree *ret;
5221 ret = xcalloc (1, sizeof *ret);
5222 ret->globals.list = globals;
5223 ret->locals.list = locals;
5224 ret->match = lang_vers_match;
5225 ret->name_indx = (unsigned int) -1;
5229 /* This static variable keeps track of version indices. */
5231 static int version_index;
5234 version_expr_head_hash (const void *p)
5236 const struct bfd_elf_version_expr *e = p;
5238 return htab_hash_string (e->symbol);
5242 version_expr_head_eq (const void *p1, const void *p2)
5244 const struct bfd_elf_version_expr *e1 = p1;
5245 const struct bfd_elf_version_expr *e2 = p2;
5247 return strcmp (e1->symbol, e2->symbol) == 0;
5251 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5254 struct bfd_elf_version_expr *e, *next;
5255 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5257 for (e = head->list; e; e = e->next)
5261 head->mask |= e->mask;
5266 head->htab = htab_create (count * 2, version_expr_head_hash,
5267 version_expr_head_eq, NULL);
5268 list_loc = &head->list;
5269 remaining_loc = &head->remaining;
5270 for (e = head->list; e; e = next)
5276 remaining_loc = &e->next;
5280 void **loc = htab_find_slot (head->htab, e, INSERT);
5284 struct bfd_elf_version_expr *e1, *last;
5290 if (e1->mask == e->mask)
5298 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5302 /* This is a duplicate. */
5303 /* FIXME: Memory leak. Sometimes pattern is not
5304 xmalloced alone, but in larger chunk of memory. */
5305 /* free (e->symbol); */
5310 e->next = last->next;
5318 list_loc = &e->next;
5322 *remaining_loc = NULL;
5323 *list_loc = head->remaining;
5326 head->remaining = head->list;
5329 /* This is called when we know the name and dependencies of the
5333 lang_register_vers_node (const char *name,
5334 struct bfd_elf_version_tree *version,
5335 struct bfd_elf_version_deps *deps)
5337 struct bfd_elf_version_tree *t, **pp;
5338 struct bfd_elf_version_expr *e1;
5343 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5344 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5346 einfo (_("%X%P: anonymous version tag cannot be combined"
5347 " with other version tags\n"));
5352 /* Make sure this node has a unique name. */
5353 for (t = lang_elf_version_info; t != NULL; t = t->next)
5354 if (strcmp (t->name, name) == 0)
5355 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5357 lang_finalize_version_expr_head (&version->globals);
5358 lang_finalize_version_expr_head (&version->locals);
5360 /* Check the global and local match names, and make sure there
5361 aren't any duplicates. */
5363 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5365 for (t = lang_elf_version_info; t != NULL; t = t->next)
5367 struct bfd_elf_version_expr *e2;
5369 if (t->locals.htab && e1->symbol)
5371 e2 = htab_find (t->locals.htab, e1);
5372 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5374 if (e1->mask == e2->mask)
5375 einfo (_("%X%P: duplicate expression `%s'"
5376 " in version information\n"), e1->symbol);
5380 else if (!e1->symbol)
5381 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5382 if (strcmp (e1->pattern, e2->pattern) == 0
5383 && e1->mask == e2->mask)
5384 einfo (_("%X%P: duplicate expression `%s'"
5385 " in version information\n"), e1->pattern);
5389 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5391 for (t = lang_elf_version_info; t != NULL; t = t->next)
5393 struct bfd_elf_version_expr *e2;
5395 if (t->globals.htab && e1->symbol)
5397 e2 = htab_find (t->globals.htab, e1);
5398 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5400 if (e1->mask == e2->mask)
5401 einfo (_("%X%P: duplicate expression `%s'"
5402 " in version information\n"),
5407 else if (!e1->symbol)
5408 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5409 if (strcmp (e1->pattern, e2->pattern) == 0
5410 && e1->mask == e2->mask)
5411 einfo (_("%X%P: duplicate expression `%s'"
5412 " in version information\n"), e1->pattern);
5416 version->deps = deps;
5417 version->name = name;
5418 if (name[0] != '\0')
5421 version->vernum = version_index;
5424 version->vernum = 0;
5426 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5431 /* This is called when we see a version dependency. */
5433 struct bfd_elf_version_deps *
5434 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5436 struct bfd_elf_version_deps *ret;
5437 struct bfd_elf_version_tree *t;
5439 ret = xmalloc (sizeof *ret);
5442 for (t = lang_elf_version_info; t != NULL; t = t->next)
5444 if (strcmp (t->name, name) == 0)
5446 ret->version_needed = t;
5451 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5457 lang_do_version_exports_section (void)
5459 struct bfd_elf_version_expr *greg = NULL, *lreg;
5461 LANG_FOR_EACH_INPUT_STATEMENT (is)
5463 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5471 contents = xmalloc (len);
5472 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5473 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5476 while (p < contents + len)
5478 greg = lang_new_vers_pattern (greg, p, NULL);
5479 p = strchr (p, '\0') + 1;
5482 /* Do not free the contents, as we used them creating the regex. */
5484 /* Do not include this section in the link. */
5485 sec->flags |= SEC_EXCLUDE;
5488 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5489 lang_register_vers_node (command_line.version_exports_section,
5490 lang_new_vers_node (greg, lreg), NULL);
5494 lang_add_unique (const char *name)
5496 struct unique_sections *ent;
5498 for (ent = unique_section_list; ent; ent = ent->next)
5499 if (strcmp (ent->name, name) == 0)
5502 ent = xmalloc (sizeof *ent);
5503 ent->name = xstrdup (name);
5504 ent->next = unique_section_list;
5505 unique_section_list = ent;