1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static lang_input_statement_type *lookup_name (const char *);
71 static bfd_boolean load_symbols (lang_input_statement_type *,
72 lang_statement_list_type *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection *);
77 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
78 static void print_statement (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statement_list (lang_statement_union_type *,
81 lang_output_section_statement_type *);
82 static void print_statements (void);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
88 asection *, lang_input_statement_type *, void *);
90 /* Exported variables. */
91 lang_output_section_statement_type *abs_output_section;
92 lang_statement_list_type lang_output_section_statement;
93 lang_statement_list_type *stat_ptr = &statement_list;
94 lang_statement_list_type file_chain = { NULL, NULL };
95 struct bfd_sym_chain entry_symbol = { NULL, NULL };
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_nocrossrefs *nocrossref_list;
103 struct unique_sections *unique_section_list;
104 static bfd_boolean ldlang_sysrooted_script = FALSE;
105 int lang_statement_iteration = 0;
107 etree_type *base; /* Relocation base - or null */
109 /* Return TRUE if the PATTERN argument is a wildcard pattern.
110 Although backslashes are treated specially if a pattern contains
111 wildcards, we do not consider the mere presence of a backslash to
112 be enough to cause the pattern to be treated as a wildcard.
113 That lets us handle DOS filenames more naturally. */
114 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
116 #define new_stat(x, y) \
117 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
119 #define outside_section_address(q) \
120 ((q)->output_offset + (q)->output_section->vma)
122 #define outside_symbol_address(q) \
123 ((q)->value + outside_section_address (q->section))
125 #define SECTION_NAME_MAP_LENGTH (16)
128 stat_alloc (size_t size)
130 return obstack_alloc (&stat_obstack, size);
134 unique_section_p (const asection *sec)
136 struct unique_sections *unam;
139 if (link_info.relocatable
140 && sec->owner != NULL
141 && bfd_is_group_section (sec->owner, sec))
145 for (unam = unique_section_list; unam; unam = unam->next)
146 if (wildcardp (unam->name)
147 ? fnmatch (unam->name, secnam, 0) == 0
148 : strcmp (unam->name, secnam) == 0)
156 /* Generic traversal routines for finding matching sections. */
159 walk_wild_section (lang_wild_statement_type *ptr,
160 lang_input_statement_type *file,
166 if (file->just_syms_flag)
169 for (s = file->the_bfd->sections; s != NULL; s = s->next)
171 struct wildcard_list *sec;
173 sec = ptr->section_list;
175 (*callback) (ptr, sec, s, file, data);
179 bfd_boolean skip = FALSE;
180 struct name_list *list_tmp;
182 /* Don't process sections from files which were
184 for (list_tmp = sec->spec.exclude_name_list;
186 list_tmp = list_tmp->next)
188 if (wildcardp (list_tmp->name))
189 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
191 skip = strcmp (list_tmp->name, file->filename) == 0;
193 /* If this file is part of an archive, and the archive is
194 excluded, exclude this file. */
195 if (! skip && file->the_bfd != NULL
196 && file->the_bfd->my_archive != NULL
197 && file->the_bfd->my_archive->filename != NULL)
199 if (wildcardp (list_tmp->name))
200 skip = fnmatch (list_tmp->name,
201 file->the_bfd->my_archive->filename,
204 skip = strcmp (list_tmp->name,
205 file->the_bfd->my_archive->filename) == 0;
212 if (!skip && sec->spec.name != NULL)
214 const char *sname = bfd_get_section_name (file->the_bfd, s);
216 if (wildcardp (sec->spec.name))
217 skip = fnmatch (sec->spec.name, sname, 0) != 0;
219 skip = strcmp (sec->spec.name, sname) != 0;
223 (*callback) (ptr, sec, s, file, data);
230 /* Handle a wild statement for a single file F. */
233 walk_wild_file (lang_wild_statement_type *s,
234 lang_input_statement_type *f,
238 if (f->the_bfd == NULL
239 || ! bfd_check_format (f->the_bfd, bfd_archive))
240 walk_wild_section (s, f, callback, data);
245 /* This is an archive file. We must map each member of the
246 archive separately. */
247 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
248 while (member != NULL)
250 /* When lookup_name is called, it will call the add_symbols
251 entry point for the archive. For each element of the
252 archive which is included, BFD will call ldlang_add_file,
253 which will set the usrdata field of the member to the
254 lang_input_statement. */
255 if (member->usrdata != NULL)
257 walk_wild_section (s, member->usrdata, callback, data);
260 member = bfd_openr_next_archived_file (f->the_bfd, member);
266 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
268 const char *file_spec = s->filename;
270 if (file_spec == NULL)
272 /* Perform the iteration over all files in the list. */
273 LANG_FOR_EACH_INPUT_STATEMENT (f)
275 walk_wild_file (s, f, callback, data);
278 else if (wildcardp (file_spec))
280 LANG_FOR_EACH_INPUT_STATEMENT (f)
282 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
283 walk_wild_file (s, f, callback, data);
288 lang_input_statement_type *f;
290 /* Perform the iteration over a single file. */
291 f = lookup_name (file_spec);
293 walk_wild_file (s, f, callback, data);
297 /* lang_for_each_statement walks the parse tree and calls the provided
298 function for each node. */
301 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
302 lang_statement_union_type *s)
304 for (; s != NULL; s = s->header.next)
308 switch (s->header.type)
310 case lang_constructors_statement_enum:
311 lang_for_each_statement_worker (func, constructor_list.head);
313 case lang_output_section_statement_enum:
314 lang_for_each_statement_worker
315 (func, s->output_section_statement.children.head);
317 case lang_wild_statement_enum:
318 lang_for_each_statement_worker (func,
319 s->wild_statement.children.head);
321 case lang_group_statement_enum:
322 lang_for_each_statement_worker (func,
323 s->group_statement.children.head);
325 case lang_data_statement_enum:
326 case lang_reloc_statement_enum:
327 case lang_object_symbols_statement_enum:
328 case lang_output_statement_enum:
329 case lang_target_statement_enum:
330 case lang_input_section_enum:
331 case lang_input_statement_enum:
332 case lang_assignment_statement_enum:
333 case lang_padding_statement_enum:
334 case lang_address_statement_enum:
335 case lang_fill_statement_enum:
345 lang_for_each_statement (void (*func) (lang_statement_union_type *))
347 lang_for_each_statement_worker (func, statement_list.head);
350 /*----------------------------------------------------------------------*/
353 lang_list_init (lang_statement_list_type *list)
356 list->tail = &list->head;
359 /* Build a new statement node for the parse tree. */
361 static lang_statement_union_type *
362 new_statement (enum statement_enum type,
364 lang_statement_list_type *list)
366 lang_statement_union_type *new;
368 new = stat_alloc (size);
369 new->header.type = type;
370 new->header.next = NULL;
371 lang_statement_append (list, new, &new->header.next);
375 /* Build a new input file node for the language. There are several
376 ways in which we treat an input file, eg, we only look at symbols,
377 or prefix it with a -l etc.
379 We can be supplied with requests for input files more than once;
380 they may, for example be split over several lines like foo.o(.text)
381 foo.o(.data) etc, so when asked for a file we check that we haven't
382 got it already so we don't duplicate the bfd. */
384 static lang_input_statement_type *
385 new_afile (const char *name,
386 lang_input_file_enum_type file_type,
388 bfd_boolean add_to_list)
390 lang_input_statement_type *p;
393 p = new_stat (lang_input_statement, stat_ptr);
396 p = stat_alloc (sizeof (lang_input_statement_type));
397 p->header.next = NULL;
400 lang_has_input_file = TRUE;
402 p->sysrooted = FALSE;
405 case lang_input_file_is_symbols_only_enum:
407 p->is_archive = FALSE;
409 p->local_sym_name = name;
410 p->just_syms_flag = TRUE;
411 p->search_dirs_flag = FALSE;
413 case lang_input_file_is_fake_enum:
415 p->is_archive = FALSE;
417 p->local_sym_name = name;
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = FALSE;
421 case lang_input_file_is_l_enum:
422 p->is_archive = TRUE;
425 p->local_sym_name = concat ("-l", name, NULL);
426 p->just_syms_flag = FALSE;
427 p->search_dirs_flag = TRUE;
429 case lang_input_file_is_marker_enum:
431 p->is_archive = FALSE;
433 p->local_sym_name = name;
434 p->just_syms_flag = FALSE;
435 p->search_dirs_flag = TRUE;
437 case lang_input_file_is_search_file_enum:
438 p->sysrooted = ldlang_sysrooted_script;
440 p->is_archive = FALSE;
442 p->local_sym_name = name;
443 p->just_syms_flag = FALSE;
444 p->search_dirs_flag = TRUE;
446 case lang_input_file_is_file_enum:
448 p->is_archive = FALSE;
450 p->local_sym_name = name;
451 p->just_syms_flag = FALSE;
452 p->search_dirs_flag = FALSE;
459 p->next_real_file = NULL;
462 p->dynamic = config.dynamic_link;
463 p->add_needed = add_needed;
464 p->as_needed = as_needed;
465 p->whole_archive = whole_archive;
467 lang_statement_append (&input_file_chain,
468 (lang_statement_union_type *) p,
473 lang_input_statement_type *
474 lang_add_input_file (const char *name,
475 lang_input_file_enum_type file_type,
478 lang_has_input_file = TRUE;
479 return new_afile (name, file_type, target, TRUE);
482 /* Build enough state so that the parser can build its tree. */
487 obstack_begin (&stat_obstack, 1000);
489 stat_ptr = &statement_list;
491 lang_list_init (stat_ptr);
493 lang_list_init (&input_file_chain);
494 lang_list_init (&lang_output_section_statement);
495 lang_list_init (&file_chain);
496 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
499 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
501 abs_output_section->bfd_section = bfd_abs_section_ptr;
503 /* The value "3" is ad-hoc, somewhat related to the expected number of
504 DEFINED expressions in a linker script. For most default linker
505 scripts, there are none. Why a hash table then? Well, it's somewhat
506 simpler to re-use working machinery than using a linked list in terms
507 of code-complexity here in ld, besides the initialization which just
508 looks like other code here. */
509 if (!bfd_hash_table_init_n (&lang_definedness_table,
510 lang_definedness_newfunc, 3))
511 einfo (_("%P%F: out of memory during initialization"));
513 /* Callers of exp_fold_tree need to increment this. */
514 lang_statement_iteration = 0;
517 /*----------------------------------------------------------------------
518 A region is an area of memory declared with the
519 MEMORY { name:org=exp, len=exp ... }
522 We maintain a list of all the regions here.
524 If no regions are specified in the script, then the default is used
525 which is created when looked up to be the entire data space.
527 If create is true we are creating a region inside a MEMORY block.
528 In this case it is probably an error to create a region that has
529 already been created. If we are not inside a MEMORY block it is
530 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
531 and so we issue a warning. */
533 static lang_memory_region_type *lang_memory_region_list;
534 static lang_memory_region_type **lang_memory_region_list_tail
535 = &lang_memory_region_list;
537 lang_memory_region_type *
538 lang_memory_region_lookup (const char *const name, bfd_boolean create)
540 lang_memory_region_type *p;
541 lang_memory_region_type *new;
543 /* NAME is NULL for LMA memspecs if no region was specified. */
547 for (p = lang_memory_region_list; p != NULL; p = p->next)
548 if (strcmp (p->name, name) == 0)
551 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
557 /* This code used to always use the first region in the list as the
558 default region. I changed it to instead use a region
559 encompassing all of memory as the default region. This permits
560 NOLOAD sections to work reasonably without requiring a region.
561 People should specify what region they mean, if they really want
563 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
565 if (lang_memory_region_list != NULL)
566 return lang_memory_region_list;
570 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
571 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
573 new = stat_alloc (sizeof (lang_memory_region_type));
575 new->name = xstrdup (name);
578 *lang_memory_region_list_tail = new;
579 lang_memory_region_list_tail = &new->next;
583 new->length = ~(bfd_size_type) 0;
585 new->had_full_message = FALSE;
590 static lang_memory_region_type *
591 lang_memory_default (asection *section)
593 lang_memory_region_type *p;
595 flagword sec_flags = section->flags;
597 /* Override SEC_DATA to mean a writable section. */
598 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
599 sec_flags |= SEC_DATA;
601 for (p = lang_memory_region_list; p != NULL; p = p->next)
603 if ((p->flags & sec_flags) != 0
604 && (p->not_flags & sec_flags) == 0)
609 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
612 static lang_output_section_statement_type *
613 lang_output_section_find_1 (const char *const name, int constraint)
615 lang_statement_union_type *u;
616 lang_output_section_statement_type *lookup;
618 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
620 lookup = &u->output_section_statement;
622 if (strcmp (name, lookup->name) == 0
623 && lookup->constraint != -1
624 && (constraint == 0 || constraint == lookup->constraint))
630 lang_output_section_statement_type *
631 lang_output_section_find (const char *const name)
633 return lang_output_section_find_1 (name, 0);
636 static lang_output_section_statement_type *
637 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
639 lang_output_section_statement_type *lookup;
641 lookup = lang_output_section_find_1 (name, constraint);
644 lookup = new_stat (lang_output_section_statement, stat_ptr);
645 lookup->region = NULL;
646 lookup->lma_region = NULL;
648 lookup->block_value = 1;
652 lookup->bfd_section = NULL;
653 lookup->processed = 0;
654 lookup->constraint = constraint;
655 lookup->sectype = normal_section;
656 lookup->addr_tree = NULL;
657 lang_list_init (&lookup->children);
659 lookup->memspec = NULL;
661 lookup->subsection_alignment = -1;
662 lookup->section_alignment = -1;
663 lookup->load_base = NULL;
664 lookup->update_dot_tree = NULL;
665 lookup->phdrs = NULL;
667 lang_statement_append (&lang_output_section_statement,
668 (lang_statement_union_type *) lookup,
674 lang_output_section_statement_type *
675 lang_output_section_statement_lookup (const char *const name)
677 return lang_output_section_statement_lookup_1 (name, 0);
681 lang_map_flags (flagword flag)
683 if (flag & SEC_ALLOC)
689 if (flag & SEC_READONLY)
702 lang_memory_region_type *m;
705 minfo (_("\nMemory Configuration\n\n"));
706 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
707 _("Name"), _("Origin"), _("Length"), _("Attributes"));
709 for (m = lang_memory_region_list; m != NULL; m = m->next)
714 fprintf (config.map_file, "%-16s ", m->name);
716 sprintf_vma (buf, m->origin);
717 minfo ("0x%s ", buf);
725 minfo ("0x%V", m->length);
726 if (m->flags || m->not_flags)
734 lang_map_flags (m->flags);
740 lang_map_flags (m->not_flags);
747 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
749 if (! command_line.reduce_memory_overheads)
751 obstack_begin (&map_obstack, 1000);
752 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
753 bfd_map_over_sections (p, init_map_userdata, 0);
754 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
760 init_map_userdata (abfd, sec, data)
761 bfd *abfd ATTRIBUTE_UNUSED;
763 void *data ATTRIBUTE_UNUSED;
765 fat_section_userdata_type *new_data
766 = ((fat_section_userdata_type *) (stat_alloc
767 (sizeof (fat_section_userdata_type))));
769 ASSERT (get_userdata (sec) == NULL);
770 get_userdata (sec) = new_data;
771 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
775 sort_def_symbol (hash_entry, info)
776 struct bfd_link_hash_entry *hash_entry;
777 void *info ATTRIBUTE_UNUSED;
779 if (hash_entry->type == bfd_link_hash_defined
780 || hash_entry->type == bfd_link_hash_defweak)
782 struct fat_user_section_struct *ud;
783 struct map_symbol_def *def;
785 ud = get_userdata (hash_entry->u.def.section);
788 /* ??? What do we have to do to initialize this beforehand? */
789 /* The first time we get here is bfd_abs_section... */
790 init_map_userdata (0, hash_entry->u.def.section, 0);
791 ud = get_userdata (hash_entry->u.def.section);
793 else if (!ud->map_symbol_def_tail)
794 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
796 def = obstack_alloc (&map_obstack, sizeof *def);
797 def->entry = hash_entry;
798 *(ud->map_symbol_def_tail) = def;
799 ud->map_symbol_def_tail = &def->next;
804 /* Initialize an output section. */
807 init_os (lang_output_section_statement_type *s)
809 lean_section_userdata_type *new;
811 if (s->bfd_section != NULL)
814 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
815 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
817 new = stat_alloc (SECTION_USERDATA_SIZE);
818 memset (new, 0, SECTION_USERDATA_SIZE);
820 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
821 if (s->bfd_section == NULL)
822 s->bfd_section = bfd_make_section (output_bfd, s->name);
823 if (s->bfd_section == NULL)
825 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
826 output_bfd->xvec->name, s->name);
828 s->bfd_section->output_section = s->bfd_section;
830 /* We initialize an output sections output offset to minus its own
831 vma to allow us to output a section through itself. */
832 s->bfd_section->output_offset = 0;
833 get_userdata (s->bfd_section) = new;
835 /* If there is a base address, make sure that any sections it might
836 mention are initialized. */
837 if (s->addr_tree != NULL)
838 exp_init_os (s->addr_tree);
840 if (s->load_base != NULL)
841 exp_init_os (s->load_base);
844 /* Make sure that all output sections mentioned in an expression are
848 exp_init_os (etree_type *exp)
850 switch (exp->type.node_class)
853 exp_init_os (exp->assign.src);
857 exp_init_os (exp->binary.lhs);
858 exp_init_os (exp->binary.rhs);
862 exp_init_os (exp->trinary.cond);
863 exp_init_os (exp->trinary.lhs);
864 exp_init_os (exp->trinary.rhs);
868 exp_init_os (exp->assert_s.child);
872 exp_init_os (exp->unary.child);
876 switch (exp->type.node_code)
882 lang_output_section_statement_type *os;
884 os = lang_output_section_find (exp->name.name);
885 if (os != NULL && os->bfd_section == NULL)
897 section_already_linked (bfd *abfd, asection *sec, void *data)
899 lang_input_statement_type *entry = data;
901 /* If we are only reading symbols from this object, then we want to
902 discard all sections. */
903 if (entry->just_syms_flag)
905 bfd_link_just_syms (sec, &link_info);
909 bfd_section_already_linked (abfd, sec);
912 /* The wild routines.
914 These expand statements like *(.text) and foo.o to a list of
915 explicit actions, like foo.o(.text), bar.o(.text) and
916 foo.o(.text, .data). */
918 /* Add SECTION to the output section OUTPUT. Do this by creating a
919 lang_input_section statement which is placed at PTR. FILE is the
920 input file which holds SECTION. */
923 lang_add_section (lang_statement_list_type *ptr,
925 lang_output_section_statement_type *output,
926 lang_input_statement_type *file)
928 flagword flags = section->flags;
931 /* Discard sections marked with SEC_EXCLUDE. */
932 discard = (flags & SEC_EXCLUDE) != 0;
934 /* Discard input sections which are assigned to a section named
935 DISCARD_SECTION_NAME. */
936 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
939 /* Discard debugging sections if we are stripping debugging
941 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
942 && (flags & SEC_DEBUGGING) != 0)
947 if (section->output_section == NULL)
949 /* This prevents future calls from assigning this section. */
950 section->output_section = bfd_abs_section_ptr;
955 if (section->output_section == NULL)
958 lang_input_section_type *new;
961 if (output->bfd_section == NULL)
964 first = ! output->bfd_section->linker_has_input;
965 output->bfd_section->linker_has_input = 1;
967 /* Add a section reference to the list. */
968 new = new_stat (lang_input_section, ptr);
970 new->section = section;
972 section->output_section = output->bfd_section;
974 flags = section->flags;
976 /* We don't copy the SEC_NEVER_LOAD flag from an input section
977 to an output section, because we want to be able to include a
978 SEC_NEVER_LOAD section in the middle of an otherwise loaded
979 section (I don't know why we want to do this, but we do).
980 build_link_order in ldwrite.c handles this case by turning
981 the embedded SEC_NEVER_LOAD section into a fill. */
983 flags &= ~ SEC_NEVER_LOAD;
985 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
986 already been processed. One reason to do this is that on pe
987 format targets, .text$foo sections go into .text and it's odd
988 to see .text with SEC_LINK_ONCE set. */
990 if (! link_info.relocatable)
991 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
993 /* If this is not the first input section, and the SEC_READONLY
994 flag is not currently set, then don't set it just because the
995 input section has it set. */
997 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
998 flags &= ~ SEC_READONLY;
1000 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1002 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1003 != (flags & (SEC_MERGE | SEC_STRINGS))
1004 || ((flags & SEC_MERGE)
1005 && section->output_section->entsize != section->entsize)))
1007 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1008 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1011 section->output_section->flags |= flags;
1013 if (flags & SEC_MERGE)
1014 section->output_section->entsize = section->entsize;
1016 /* If SEC_READONLY is not set in the input section, then clear
1017 it from the output section. */
1018 if ((section->flags & SEC_READONLY) == 0)
1019 section->output_section->flags &= ~SEC_READONLY;
1021 switch (output->sectype)
1023 case normal_section:
1028 case overlay_section:
1029 output->bfd_section->flags &= ~SEC_ALLOC;
1031 case noload_section:
1032 output->bfd_section->flags &= ~SEC_LOAD;
1033 output->bfd_section->flags |= SEC_NEVER_LOAD;
1037 /* Copy over SEC_SMALL_DATA. */
1038 if (section->flags & SEC_SMALL_DATA)
1039 section->output_section->flags |= SEC_SMALL_DATA;
1041 if (section->alignment_power > output->bfd_section->alignment_power)
1042 output->bfd_section->alignment_power = section->alignment_power;
1044 /* If supplied an alignment, then force it. */
1045 if (output->section_alignment != -1)
1046 output->bfd_section->alignment_power = output->section_alignment;
1048 if (section->flags & SEC_BLOCK)
1050 section->output_section->flags |= SEC_BLOCK;
1051 /* FIXME: This value should really be obtained from the bfd... */
1052 output->block_value = 128;
1057 /* Compare sections ASEC and BSEC according to SORT. */
1060 compare_section (sort_type sort, asection *asec, asection *bsec)
1069 case by_alignment_name:
1070 ret = (bfd_section_alignment (bsec->owner, bsec)
1071 - bfd_section_alignment (asec->owner, asec));
1077 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1078 bfd_get_section_name (bsec->owner, bsec));
1081 case by_name_alignment:
1082 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1083 bfd_get_section_name (bsec->owner, bsec));
1089 ret = (bfd_section_alignment (bsec->owner, bsec)
1090 - bfd_section_alignment (asec->owner, asec));
1097 /* Handle wildcard sorting. This returns the lang_input_section which
1098 should follow the one we are going to create for SECTION and FILE,
1099 based on the sorting requirements of WILD. It returns NULL if the
1100 new section should just go at the end of the current list. */
1102 static lang_statement_union_type *
1103 wild_sort (lang_wild_statement_type *wild,
1104 struct wildcard_list *sec,
1105 lang_input_statement_type *file,
1108 const char *section_name;
1109 lang_statement_union_type *l;
1111 if (!wild->filenames_sorted
1112 && (sec == NULL || sec->spec.sorted == none))
1115 section_name = bfd_get_section_name (file->the_bfd, section);
1116 for (l = wild->children.head; l != NULL; l = l->header.next)
1118 lang_input_section_type *ls;
1120 if (l->header.type != lang_input_section_enum)
1122 ls = &l->input_section;
1124 /* Sorting by filename takes precedence over sorting by section
1127 if (wild->filenames_sorted)
1129 const char *fn, *ln;
1133 /* The PE support for the .idata section as generated by
1134 dlltool assumes that files will be sorted by the name of
1135 the archive and then the name of the file within the
1138 if (file->the_bfd != NULL
1139 && bfd_my_archive (file->the_bfd) != NULL)
1141 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1146 fn = file->filename;
1150 if (ls->ifile->the_bfd != NULL
1151 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1153 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1158 ln = ls->ifile->filename;
1162 i = strcmp (fn, ln);
1171 fn = file->filename;
1173 ln = ls->ifile->filename;
1175 i = strcmp (fn, ln);
1183 /* Here either the files are not sorted by name, or we are
1184 looking at the sections for this file. */
1186 if (sec != NULL && sec->spec.sorted != none)
1188 if (compare_section (sec->spec.sorted, section,
1197 /* Expand a wild statement for a particular FILE. SECTION may be
1198 NULL, in which case it is a wild card. */
1201 output_section_callback (lang_wild_statement_type *ptr,
1202 struct wildcard_list *sec,
1204 lang_input_statement_type *file,
1207 lang_statement_union_type *before;
1209 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1210 if (unique_section_p (section))
1213 before = wild_sort (ptr, sec, file, section);
1215 /* Here BEFORE points to the lang_input_section which
1216 should follow the one we are about to add. If BEFORE
1217 is NULL, then the section should just go at the end
1218 of the current list. */
1221 lang_add_section (&ptr->children, section,
1222 (lang_output_section_statement_type *) output,
1226 lang_statement_list_type list;
1227 lang_statement_union_type **pp;
1229 lang_list_init (&list);
1230 lang_add_section (&list, section,
1231 (lang_output_section_statement_type *) output,
1234 /* If we are discarding the section, LIST.HEAD will
1236 if (list.head != NULL)
1238 ASSERT (list.head->header.next == NULL);
1240 for (pp = &ptr->children.head;
1242 pp = &(*pp)->header.next)
1243 ASSERT (*pp != NULL);
1245 list.head->header.next = *pp;
1251 /* Check if all sections in a wild statement for a particular FILE
1255 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1256 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1258 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1261 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1262 if (unique_section_p (section))
1265 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1266 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1269 /* This is passed a file name which must have been seen already and
1270 added to the statement tree. We will see if it has been opened
1271 already and had its symbols read. If not then we'll read it. */
1273 static lang_input_statement_type *
1274 lookup_name (const char *name)
1276 lang_input_statement_type *search;
1278 for (search = (lang_input_statement_type *) input_file_chain.head;
1280 search = (lang_input_statement_type *) search->next_real_file)
1282 /* Use the local_sym_name as the name of the file that has
1283 already been loaded as filename might have been transformed
1284 via the search directory lookup mechanism. */
1285 const char * filename = search->local_sym_name;
1287 if (filename == NULL && name == NULL)
1289 if (filename != NULL
1291 && strcmp (filename, name) == 0)
1296 search = new_afile (name, lang_input_file_is_search_file_enum,
1297 default_target, FALSE);
1299 /* If we have already added this file, or this file is not real
1300 (FIXME: can that ever actually happen?) or the name is NULL
1301 (FIXME: can that ever actually happen?) don't add this file. */
1304 || search->filename == NULL)
1307 if (! load_symbols (search, NULL))
1313 /* Get the symbols for an input file. */
1316 load_symbols (lang_input_statement_type *entry,
1317 lang_statement_list_type *place)
1324 ldfile_open_file (entry);
1326 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1327 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1330 lang_statement_list_type *hold;
1331 bfd_boolean bad_load = TRUE;
1332 bfd_boolean save_ldlang_sysrooted_script;
1334 err = bfd_get_error ();
1336 /* See if the emulation has some special knowledge. */
1337 if (ldemul_unrecognized_file (entry))
1340 if (err == bfd_error_file_ambiguously_recognized)
1344 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1345 einfo (_("%B: matching formats:"), entry->the_bfd);
1346 for (p = matching; *p != NULL; p++)
1350 else if (err != bfd_error_file_not_recognized
1352 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1356 bfd_close (entry->the_bfd);
1357 entry->the_bfd = NULL;
1359 /* Try to interpret the file as a linker script. */
1360 ldfile_open_command_file (entry->filename);
1364 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1365 ldlang_sysrooted_script = entry->sysrooted;
1367 ldfile_assumed_script = TRUE;
1368 parser_input = input_script;
1370 ldfile_assumed_script = FALSE;
1372 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1378 if (ldemul_recognized_file (entry))
1381 /* We don't call ldlang_add_file for an archive. Instead, the
1382 add_symbols entry point will call ldlang_add_file, via the
1383 add_archive_element callback, for each element of the archive
1385 switch (bfd_get_format (entry->the_bfd))
1391 ldlang_add_file (entry);
1392 if (trace_files || trace_file_tries)
1393 info_msg ("%I\n", entry);
1397 if (entry->whole_archive)
1400 bfd_boolean loaded = TRUE;
1404 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1409 if (! bfd_check_format (member, bfd_object))
1411 einfo (_("%F%B: member %B in archive is not an object\n"),
1412 entry->the_bfd, member);
1416 if (! ((*link_info.callbacks->add_archive_element)
1417 (&link_info, member, "--whole-archive")))
1420 if (! bfd_link_add_symbols (member, &link_info))
1422 einfo (_("%F%B: could not read symbols: %E\n"), member);
1427 entry->loaded = loaded;
1433 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1434 entry->loaded = TRUE;
1436 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1438 return entry->loaded;
1441 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1442 may be NULL, indicating that it is a wildcard. Separate
1443 lang_input_section statements are created for each part of the
1444 expansion; they are added after the wild statement S. OUTPUT is
1445 the output section. */
1448 wild (lang_wild_statement_type *s,
1449 const char *target ATTRIBUTE_UNUSED,
1450 lang_output_section_statement_type *output)
1452 struct wildcard_list *sec;
1454 walk_wild (s, output_section_callback, output);
1456 for (sec = s->section_list; sec != NULL; sec = sec->next)
1458 if (default_common_section != NULL)
1460 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1462 /* Remember the section that common is going to in case we
1463 later get something which doesn't know where to put it. */
1464 default_common_section = output;
1469 /* Return TRUE iff target is the sought target. */
1472 get_target (const bfd_target *target, void *data)
1474 const char *sought = data;
1476 return strcmp (target->name, sought) == 0;
1479 /* Like strcpy() but convert to lower case as well. */
1482 stricpy (char *dest, char *src)
1486 while ((c = *src++) != 0)
1487 *dest++ = TOLOWER (c);
1492 /* Remove the first occurrence of needle (if any) in haystack
1496 strcut (char *haystack, char *needle)
1498 haystack = strstr (haystack, needle);
1504 for (src = haystack + strlen (needle); *src;)
1505 *haystack++ = *src++;
1511 /* Compare two target format name strings.
1512 Return a value indicating how "similar" they are. */
1515 name_compare (char *first, char *second)
1521 copy1 = xmalloc (strlen (first) + 1);
1522 copy2 = xmalloc (strlen (second) + 1);
1524 /* Convert the names to lower case. */
1525 stricpy (copy1, first);
1526 stricpy (copy2, second);
1528 /* Remove size and endian strings from the name. */
1529 strcut (copy1, "big");
1530 strcut (copy1, "little");
1531 strcut (copy2, "big");
1532 strcut (copy2, "little");
1534 /* Return a value based on how many characters match,
1535 starting from the beginning. If both strings are
1536 the same then return 10 * their length. */
1537 for (result = 0; copy1[result] == copy2[result]; result++)
1538 if (copy1[result] == 0)
1550 /* Set by closest_target_match() below. */
1551 static const bfd_target *winner;
1553 /* Scan all the valid bfd targets looking for one that has the endianness
1554 requirement that was specified on the command line, and is the nearest
1555 match to the original output target. */
1558 closest_target_match (const bfd_target *target, void *data)
1560 const bfd_target *original = data;
1562 if (command_line.endian == ENDIAN_BIG
1563 && target->byteorder != BFD_ENDIAN_BIG)
1566 if (command_line.endian == ENDIAN_LITTLE
1567 && target->byteorder != BFD_ENDIAN_LITTLE)
1570 /* Must be the same flavour. */
1571 if (target->flavour != original->flavour)
1574 /* If we have not found a potential winner yet, then record this one. */
1581 /* Oh dear, we now have two potential candidates for a successful match.
1582 Compare their names and choose the better one. */
1583 if (name_compare (target->name, original->name)
1584 > name_compare (winner->name, original->name))
1587 /* Keep on searching until wqe have checked them all. */
1591 /* Return the BFD target format of the first input file. */
1594 get_first_input_target (void)
1596 char *target = NULL;
1598 LANG_FOR_EACH_INPUT_STATEMENT (s)
1600 if (s->header.type == lang_input_statement_enum
1603 ldfile_open_file (s);
1605 if (s->the_bfd != NULL
1606 && bfd_check_format (s->the_bfd, bfd_object))
1608 target = bfd_get_target (s->the_bfd);
1620 lang_get_output_target (void)
1624 /* Has the user told us which output format to use? */
1625 if (output_target != NULL)
1626 return output_target;
1628 /* No - has the current target been set to something other than
1630 if (current_target != default_target)
1631 return current_target;
1633 /* No - can we determine the format of the first input file? */
1634 target = get_first_input_target ();
1638 /* Failed - use the default output target. */
1639 return default_target;
1642 /* Open the output file. */
1645 open_output (const char *name)
1649 output_target = lang_get_output_target ();
1651 /* Has the user requested a particular endianness on the command
1653 if (command_line.endian != ENDIAN_UNSET)
1655 const bfd_target *target;
1656 enum bfd_endian desired_endian;
1658 /* Get the chosen target. */
1659 target = bfd_search_for_target (get_target, (void *) output_target);
1661 /* If the target is not supported, we cannot do anything. */
1664 if (command_line.endian == ENDIAN_BIG)
1665 desired_endian = BFD_ENDIAN_BIG;
1667 desired_endian = BFD_ENDIAN_LITTLE;
1669 /* See if the target has the wrong endianness. This should
1670 not happen if the linker script has provided big and
1671 little endian alternatives, but some scrips don't do
1673 if (target->byteorder != desired_endian)
1675 /* If it does, then see if the target provides
1676 an alternative with the correct endianness. */
1677 if (target->alternative_target != NULL
1678 && (target->alternative_target->byteorder == desired_endian))
1679 output_target = target->alternative_target->name;
1682 /* Try to find a target as similar as possible to
1683 the default target, but which has the desired
1684 endian characteristic. */
1685 bfd_search_for_target (closest_target_match,
1688 /* Oh dear - we could not find any targets that
1689 satisfy our requirements. */
1691 einfo (_("%P: warning: could not find any targets"
1692 " that match endianness requirement\n"));
1694 output_target = winner->name;
1700 output = bfd_openw (name, output_target);
1704 if (bfd_get_error () == bfd_error_invalid_target)
1705 einfo (_("%P%F: target %s not found\n"), output_target);
1707 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1710 delete_output_file_on_failure = TRUE;
1713 output->flags |= D_PAGED;
1716 if (! bfd_set_format (output, bfd_object))
1717 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1718 if (! bfd_set_arch_mach (output,
1719 ldfile_output_architecture,
1720 ldfile_output_machine))
1721 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1723 link_info.hash = bfd_link_hash_table_create (output);
1724 if (link_info.hash == NULL)
1725 einfo (_("%P%F: can not create link hash table: %E\n"));
1727 bfd_set_gp_size (output, g_switch_value);
1732 ldlang_open_output (lang_statement_union_type *statement)
1734 switch (statement->header.type)
1736 case lang_output_statement_enum:
1737 ASSERT (output_bfd == NULL);
1738 output_bfd = open_output (statement->output_statement.name);
1739 ldemul_set_output_arch ();
1740 if (config.magic_demand_paged && !link_info.relocatable)
1741 output_bfd->flags |= D_PAGED;
1743 output_bfd->flags &= ~D_PAGED;
1744 if (config.text_read_only)
1745 output_bfd->flags |= WP_TEXT;
1747 output_bfd->flags &= ~WP_TEXT;
1748 if (link_info.traditional_format)
1749 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1751 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1754 case lang_target_statement_enum:
1755 current_target = statement->target_statement.target;
1762 /* Convert between addresses in bytes and sizes in octets.
1763 For currently supported targets, octets_per_byte is always a power
1764 of two, so we can use shifts. */
1765 #define TO_ADDR(X) ((X) >> opb_shift)
1766 #define TO_SIZE(X) ((X) << opb_shift)
1768 /* Support the above. */
1769 static unsigned int opb_shift = 0;
1774 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1775 ldfile_output_machine);
1778 while ((x & 1) == 0)
1786 /* Open all the input files. */
1789 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1791 for (; s != NULL; s = s->header.next)
1793 switch (s->header.type)
1795 case lang_constructors_statement_enum:
1796 open_input_bfds (constructor_list.head, force);
1798 case lang_output_section_statement_enum:
1799 open_input_bfds (s->output_section_statement.children.head, force);
1801 case lang_wild_statement_enum:
1802 /* Maybe we should load the file's symbols. */
1803 if (s->wild_statement.filename
1804 && ! wildcardp (s->wild_statement.filename))
1805 lookup_name (s->wild_statement.filename);
1806 open_input_bfds (s->wild_statement.children.head, force);
1808 case lang_group_statement_enum:
1810 struct bfd_link_hash_entry *undefs;
1812 /* We must continually search the entries in the group
1813 until no new symbols are added to the list of undefined
1818 undefs = link_info.hash->undefs_tail;
1819 open_input_bfds (s->group_statement.children.head, TRUE);
1821 while (undefs != link_info.hash->undefs_tail);
1824 case lang_target_statement_enum:
1825 current_target = s->target_statement.target;
1827 case lang_input_statement_enum:
1828 if (s->input_statement.real)
1830 lang_statement_list_type add;
1832 s->input_statement.target = current_target;
1834 /* If we are being called from within a group, and this
1835 is an archive which has already been searched, then
1836 force it to be researched unless the whole archive
1837 has been loaded already. */
1839 && !s->input_statement.whole_archive
1840 && s->input_statement.loaded
1841 && bfd_check_format (s->input_statement.the_bfd,
1843 s->input_statement.loaded = FALSE;
1845 lang_list_init (&add);
1847 if (! load_symbols (&s->input_statement, &add))
1848 config.make_executable = FALSE;
1850 if (add.head != NULL)
1852 *add.tail = s->header.next;
1853 s->header.next = add.head;
1863 /* If there are [COMMONS] statements, put a wild one into the bss
1867 lang_reasonable_defaults (void)
1870 lang_output_section_statement_lookup (".text");
1871 lang_output_section_statement_lookup (".data");
1873 default_common_section = lang_output_section_statement_lookup (".bss");
1875 if (!placed_commons)
1877 lang_wild_statement_type *new =
1878 new_stat (lang_wild_statement,
1879 &default_common_section->children);
1881 new->section_name = "COMMON";
1882 new->filename = NULL;
1883 lang_list_init (&new->children);
1888 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1891 lang_track_definedness (const char *name)
1893 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1894 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1897 /* New-function for the definedness hash table. */
1899 static struct bfd_hash_entry *
1900 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1901 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1902 const char *name ATTRIBUTE_UNUSED)
1904 struct lang_definedness_hash_entry *ret
1905 = (struct lang_definedness_hash_entry *) entry;
1908 ret = (struct lang_definedness_hash_entry *)
1909 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1912 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1914 ret->iteration = -1;
1918 /* Return the iteration when the definition of NAME was last updated. A
1919 value of -1 means that the symbol is not defined in the linker script
1920 or the command line, but may be defined in the linker symbol table. */
1923 lang_symbol_definition_iteration (const char *name)
1925 struct lang_definedness_hash_entry *defentry
1926 = (struct lang_definedness_hash_entry *)
1927 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1929 /* We've already created this one on the presence of DEFINED in the
1930 script, so it can't be NULL unless something is borked elsewhere in
1932 if (defentry == NULL)
1935 return defentry->iteration;
1938 /* Update the definedness state of NAME. */
1941 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1943 struct lang_definedness_hash_entry *defentry
1944 = (struct lang_definedness_hash_entry *)
1945 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1947 /* We don't keep track of symbols not tested with DEFINED. */
1948 if (defentry == NULL)
1951 /* If the symbol was already defined, and not from an earlier statement
1952 iteration, don't update the definedness iteration, because that'd
1953 make the symbol seem defined in the linker script at this point, and
1954 it wasn't; it was defined in some object. If we do anyway, DEFINED
1955 would start to yield false before this point and the construct "sym =
1956 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1958 if (h->type != bfd_link_hash_undefined
1959 && h->type != bfd_link_hash_common
1960 && h->type != bfd_link_hash_new
1961 && defentry->iteration == -1)
1964 defentry->iteration = lang_statement_iteration;
1967 /* Add the supplied name to the symbol table as an undefined reference.
1968 This is a two step process as the symbol table doesn't even exist at
1969 the time the ld command line is processed. First we put the name
1970 on a list, then, once the output file has been opened, transfer the
1971 name to the symbol table. */
1973 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
1975 #define ldlang_undef_chain_list_head entry_symbol.next
1978 ldlang_add_undef (const char *const name)
1980 ldlang_undef_chain_list_type *new =
1981 stat_alloc (sizeof (ldlang_undef_chain_list_type));
1983 new->next = ldlang_undef_chain_list_head;
1984 ldlang_undef_chain_list_head = new;
1986 new->name = xstrdup (name);
1988 if (output_bfd != NULL)
1989 insert_undefined (new->name);
1992 /* Insert NAME as undefined in the symbol table. */
1995 insert_undefined (const char *name)
1997 struct bfd_link_hash_entry *h;
1999 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2001 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2002 if (h->type == bfd_link_hash_new)
2004 h->type = bfd_link_hash_undefined;
2005 h->u.undef.abfd = NULL;
2006 bfd_link_add_undef (link_info.hash, h);
2010 /* Run through the list of undefineds created above and place them
2011 into the linker hash table as undefined symbols belonging to the
2015 lang_place_undefineds (void)
2017 ldlang_undef_chain_list_type *ptr;
2019 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2020 insert_undefined (ptr->name);
2023 /* Check for all readonly or some readwrite sections. */
2026 check_input_sections
2027 (lang_statement_union_type *s,
2028 lang_output_section_statement_type *output_section_statement)
2030 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2032 switch (s->header.type)
2034 case lang_wild_statement_enum:
2035 walk_wild (&s->wild_statement, check_section_callback,
2036 output_section_statement);
2037 if (! output_section_statement->all_input_readonly)
2040 case lang_constructors_statement_enum:
2041 check_input_sections (constructor_list.head,
2042 output_section_statement);
2043 if (! output_section_statement->all_input_readonly)
2046 case lang_group_statement_enum:
2047 check_input_sections (s->group_statement.children.head,
2048 output_section_statement);
2049 if (! output_section_statement->all_input_readonly)
2058 /* Update wildcard statements if needed. */
2061 update_wild_statements (lang_statement_union_type *s)
2063 struct wildcard_list *sec;
2065 switch (sort_section)
2075 for (; s != NULL; s = s->header.next)
2077 switch (s->header.type)
2082 case lang_wild_statement_enum:
2083 sec = s->wild_statement.section_list;
2086 switch (sec->spec.sorted)
2089 sec->spec.sorted = sort_section;
2092 if (sort_section == by_alignment)
2093 sec->spec.sorted = by_name_alignment;
2096 if (sort_section == by_name)
2097 sec->spec.sorted = by_alignment_name;
2105 case lang_constructors_statement_enum:
2106 update_wild_statements (constructor_list.head);
2109 case lang_output_section_statement_enum:
2110 update_wild_statements
2111 (s->output_section_statement.children.head);
2114 case lang_group_statement_enum:
2115 update_wild_statements (s->group_statement.children.head);
2123 /* Open input files and attach to output sections. */
2126 map_input_to_output_sections
2127 (lang_statement_union_type *s, const char *target,
2128 lang_output_section_statement_type *output_section_statement)
2130 for (; s != NULL; s = s->header.next)
2132 switch (s->header.type)
2134 case lang_wild_statement_enum:
2135 wild (&s->wild_statement, target, output_section_statement);
2137 case lang_constructors_statement_enum:
2138 map_input_to_output_sections (constructor_list.head,
2140 output_section_statement);
2142 case lang_output_section_statement_enum:
2143 if (s->output_section_statement.constraint)
2145 if (s->output_section_statement.constraint == -1)
2147 s->output_section_statement.all_input_readonly = TRUE;
2148 check_input_sections (s->output_section_statement.children.head,
2149 &s->output_section_statement);
2150 if ((s->output_section_statement.all_input_readonly
2151 && s->output_section_statement.constraint == ONLY_IF_RW)
2152 || (!s->output_section_statement.all_input_readonly
2153 && s->output_section_statement.constraint == ONLY_IF_RO))
2155 s->output_section_statement.constraint = -1;
2160 map_input_to_output_sections (s->output_section_statement.children.head,
2162 &s->output_section_statement);
2164 case lang_output_statement_enum:
2166 case lang_target_statement_enum:
2167 target = s->target_statement.target;
2169 case lang_group_statement_enum:
2170 map_input_to_output_sections (s->group_statement.children.head,
2172 output_section_statement);
2174 case lang_data_statement_enum:
2175 /* Make sure that any sections mentioned in the expression
2177 exp_init_os (s->data_statement.exp);
2179 case lang_fill_statement_enum:
2180 case lang_input_section_enum:
2181 case lang_object_symbols_statement_enum:
2182 case lang_reloc_statement_enum:
2183 case lang_padding_statement_enum:
2184 case lang_input_statement_enum:
2185 if (output_section_statement != NULL
2186 && output_section_statement->bfd_section == NULL)
2187 init_os (output_section_statement);
2189 case lang_assignment_statement_enum:
2190 if (output_section_statement != NULL
2191 && output_section_statement->bfd_section == NULL)
2192 init_os (output_section_statement);
2194 /* Make sure that any sections mentioned in the assignment
2196 exp_init_os (s->assignment_statement.exp);
2198 case lang_afile_asection_pair_statement_enum:
2201 case lang_address_statement_enum:
2202 /* Mark the specified section with the supplied address. */
2204 lang_output_section_statement_type *os =
2205 lang_output_section_statement_lookup
2206 (s->address_statement.section_name);
2208 if (os->bfd_section == NULL)
2210 os->addr_tree = s->address_statement.address;
2217 /* An output section might have been removed after its statement was
2218 added. For example, ldemul_before_allocation can remove dynamic
2219 sections if they turn out to be not needed. Clean them up here. */
2222 strip_excluded_output_sections (void)
2224 lang_statement_union_type *u;
2226 for (u = lang_output_section_statement.head;
2228 u = u->output_section_statement.next)
2230 lang_output_section_statement_type *os;
2233 os = &u->output_section_statement;
2234 if (os->constraint == -1)
2236 s = os->bfd_section;
2237 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2241 os->bfd_section = NULL;
2243 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2246 bfd_section_list_remove (output_bfd, p);
2247 output_bfd->section_count--;
2255 print_output_section_statement
2256 (lang_output_section_statement_type *output_section_statement)
2258 asection *section = output_section_statement->bfd_section;
2261 if (output_section_statement != abs_output_section)
2263 minfo ("\n%s", output_section_statement->name);
2265 if (section != NULL)
2267 print_dot = section->vma;
2269 len = strlen (output_section_statement->name);
2270 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2275 while (len < SECTION_NAME_MAP_LENGTH)
2281 minfo ("0x%V %W", section->vma, section->size);
2283 if (output_section_statement->load_base != NULL)
2287 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2288 "load base", lang_final_phase_enum);
2289 minfo (_(" load address 0x%V"), addr);
2296 print_statement_list (output_section_statement->children.head,
2297 output_section_statement);
2301 print_assignment (lang_assignment_statement_type *assignment,
2302 lang_output_section_statement_type *output_section)
2305 etree_value_type result;
2307 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2310 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2311 lang_final_phase_enum, print_dot, &print_dot);
2317 value = result.value + result.section->bfd_section->vma;
2318 dst = assignment->exp->assign.dst;
2320 minfo ("0x%V", value);
2321 if (dst[0] == '.' && dst[1] == 0)
2334 exp_print_tree (assignment->exp);
2340 print_input_statement (lang_input_statement_type *statm)
2342 if (statm->filename != NULL)
2344 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2348 /* Print all symbols defined in a particular section. This is called
2349 via bfd_link_hash_traverse, or by print_all_symbols. */
2352 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2354 asection *sec = ptr;
2356 if ((hash_entry->type == bfd_link_hash_defined
2357 || hash_entry->type == bfd_link_hash_defweak)
2358 && sec == hash_entry->u.def.section)
2362 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2365 (hash_entry->u.def.value
2366 + hash_entry->u.def.section->output_offset
2367 + hash_entry->u.def.section->output_section->vma));
2369 minfo (" %T\n", hash_entry->root.string);
2376 print_all_symbols (sec)
2379 struct fat_user_section_struct *ud = get_userdata (sec);
2380 struct map_symbol_def *def;
2382 *ud->map_symbol_def_tail = 0;
2383 for (def = ud->map_symbol_def_head; def; def = def->next)
2384 print_one_symbol (def->entry, sec);
2387 /* Print information about an input section to the map file. */
2390 print_input_section (lang_input_section_type *in)
2392 asection *i = in->section;
2393 bfd_size_type size = i->size;
2402 minfo ("%s", i->name);
2404 len = 1 + strlen (i->name);
2405 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2410 while (len < SECTION_NAME_MAP_LENGTH)
2416 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2417 addr = i->output_section->vma + i->output_offset;
2424 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2426 if (size != i->rawsize && i->rawsize != 0)
2428 len = SECTION_NAME_MAP_LENGTH + 3;
2440 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2443 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2445 if (command_line.reduce_memory_overheads)
2446 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2448 print_all_symbols (i);
2450 print_dot = addr + TO_ADDR (size);
2456 print_fill_statement (lang_fill_statement_type *fill)
2460 fputs (" FILL mask 0x", config.map_file);
2461 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2462 fprintf (config.map_file, "%02x", *p);
2463 fputs ("\n", config.map_file);
2467 print_data_statement (lang_data_statement_type *data)
2475 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2478 addr = data->output_vma;
2479 if (data->output_section != NULL)
2480 addr += data->output_section->vma;
2508 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2510 if (data->exp->type.node_class != etree_value)
2513 exp_print_tree (data->exp);
2518 print_dot = addr + TO_ADDR (size);
2521 /* Print an address statement. These are generated by options like
2525 print_address_statement (lang_address_statement_type *address)
2527 minfo (_("Address of section %s set to "), address->section_name);
2528 exp_print_tree (address->address);
2532 /* Print a reloc statement. */
2535 print_reloc_statement (lang_reloc_statement_type *reloc)
2542 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2545 addr = reloc->output_vma;
2546 if (reloc->output_section != NULL)
2547 addr += reloc->output_section->vma;
2549 size = bfd_get_reloc_size (reloc->howto);
2551 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2553 if (reloc->name != NULL)
2554 minfo ("%s+", reloc->name);
2556 minfo ("%s+", reloc->section->name);
2558 exp_print_tree (reloc->addend_exp);
2562 print_dot = addr + TO_ADDR (size);
2566 print_padding_statement (lang_padding_statement_type *s)
2574 len = sizeof " *fill*" - 1;
2575 while (len < SECTION_NAME_MAP_LENGTH)
2581 addr = s->output_offset;
2582 if (s->output_section != NULL)
2583 addr += s->output_section->vma;
2584 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2586 if (s->fill->size != 0)
2590 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2591 fprintf (config.map_file, "%02x", *p);
2596 print_dot = addr + TO_ADDR (s->size);
2600 print_wild_statement (lang_wild_statement_type *w,
2601 lang_output_section_statement_type *os)
2603 struct wildcard_list *sec;
2607 if (w->filenames_sorted)
2609 if (w->filename != NULL)
2610 minfo ("%s", w->filename);
2613 if (w->filenames_sorted)
2617 for (sec = w->section_list; sec; sec = sec->next)
2619 if (sec->spec.sorted)
2621 if (sec->spec.exclude_name_list != NULL)
2624 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2625 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2626 minfo (" %s", tmp->name);
2629 if (sec->spec.name != NULL)
2630 minfo ("%s", sec->spec.name);
2633 if (sec->spec.sorted)
2642 print_statement_list (w->children.head, os);
2645 /* Print a group statement. */
2648 print_group (lang_group_statement_type *s,
2649 lang_output_section_statement_type *os)
2651 fprintf (config.map_file, "START GROUP\n");
2652 print_statement_list (s->children.head, os);
2653 fprintf (config.map_file, "END GROUP\n");
2656 /* Print the list of statements in S.
2657 This can be called for any statement type. */
2660 print_statement_list (lang_statement_union_type *s,
2661 lang_output_section_statement_type *os)
2665 print_statement (s, os);
2670 /* Print the first statement in statement list S.
2671 This can be called for any statement type. */
2674 print_statement (lang_statement_union_type *s,
2675 lang_output_section_statement_type *os)
2677 switch (s->header.type)
2680 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2683 case lang_constructors_statement_enum:
2684 if (constructor_list.head != NULL)
2686 if (constructors_sorted)
2687 minfo (" SORT (CONSTRUCTORS)\n");
2689 minfo (" CONSTRUCTORS\n");
2690 print_statement_list (constructor_list.head, os);
2693 case lang_wild_statement_enum:
2694 print_wild_statement (&s->wild_statement, os);
2696 case lang_address_statement_enum:
2697 print_address_statement (&s->address_statement);
2699 case lang_object_symbols_statement_enum:
2700 minfo (" CREATE_OBJECT_SYMBOLS\n");
2702 case lang_fill_statement_enum:
2703 print_fill_statement (&s->fill_statement);
2705 case lang_data_statement_enum:
2706 print_data_statement (&s->data_statement);
2708 case lang_reloc_statement_enum:
2709 print_reloc_statement (&s->reloc_statement);
2711 case lang_input_section_enum:
2712 print_input_section (&s->input_section);
2714 case lang_padding_statement_enum:
2715 print_padding_statement (&s->padding_statement);
2717 case lang_output_section_statement_enum:
2718 print_output_section_statement (&s->output_section_statement);
2720 case lang_assignment_statement_enum:
2721 print_assignment (&s->assignment_statement, os);
2723 case lang_target_statement_enum:
2724 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2726 case lang_output_statement_enum:
2727 minfo ("OUTPUT(%s", s->output_statement.name);
2728 if (output_target != NULL)
2729 minfo (" %s", output_target);
2732 case lang_input_statement_enum:
2733 print_input_statement (&s->input_statement);
2735 case lang_group_statement_enum:
2736 print_group (&s->group_statement, os);
2738 case lang_afile_asection_pair_statement_enum:
2745 print_statements (void)
2747 print_statement_list (statement_list.head, abs_output_section);
2750 /* Print the first N statements in statement list S to STDERR.
2751 If N == 0, nothing is printed.
2752 If N < 0, the entire list is printed.
2753 Intended to be called from GDB. */
2756 dprint_statement (lang_statement_union_type *s, int n)
2758 FILE *map_save = config.map_file;
2760 config.map_file = stderr;
2763 print_statement_list (s, abs_output_section);
2766 while (s && --n >= 0)
2768 print_statement (s, abs_output_section);
2773 config.map_file = map_save;
2777 insert_pad (lang_statement_union_type **ptr,
2779 unsigned int alignment_needed,
2780 asection *output_section,
2783 static fill_type zero_fill = { 1, { 0 } };
2784 lang_statement_union_type *pad;
2786 pad = ((lang_statement_union_type *)
2787 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2788 if (ptr != &statement_list.head
2789 && pad->header.type == lang_padding_statement_enum
2790 && pad->padding_statement.output_section == output_section)
2792 /* Use the existing pad statement. The above test on output
2793 section is probably redundant, but it doesn't hurt to check. */
2797 /* Make a new padding statement, linked into existing chain. */
2798 pad = stat_alloc (sizeof (lang_padding_statement_type));
2799 pad->header.next = *ptr;
2801 pad->header.type = lang_padding_statement_enum;
2802 pad->padding_statement.output_section = output_section;
2805 pad->padding_statement.fill = fill;
2807 pad->padding_statement.output_offset = dot - output_section->vma;
2808 pad->padding_statement.size = alignment_needed;
2809 output_section->size += alignment_needed;
2812 /* Work out how much this section will move the dot point. */
2816 (lang_statement_union_type **this_ptr,
2817 lang_output_section_statement_type *output_section_statement,
2821 lang_input_section_type *is = &((*this_ptr)->input_section);
2822 asection *i = is->section;
2824 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2826 unsigned int alignment_needed;
2829 /* Align this section first to the input sections requirement,
2830 then to the output section's requirement. If this alignment
2831 is greater than any seen before, then record it too. Perform
2832 the alignment by inserting a magic 'padding' statement. */
2834 if (output_section_statement->subsection_alignment != -1)
2835 i->alignment_power = output_section_statement->subsection_alignment;
2837 o = output_section_statement->bfd_section;
2838 if (o->alignment_power < i->alignment_power)
2839 o->alignment_power = i->alignment_power;
2841 alignment_needed = align_power (dot, i->alignment_power) - dot;
2843 if (alignment_needed != 0)
2845 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2846 dot += alignment_needed;
2849 /* Remember where in the output section this input section goes. */
2851 i->output_offset = dot - o->vma;
2853 /* Mark how big the output section must be to contain this now. */
2854 dot += TO_ADDR (i->size);
2855 o->size = TO_SIZE (dot - o->vma);
2859 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2865 #define IGNORE_SECTION(s) \
2866 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2867 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2868 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2871 /* Check to see if any allocated sections overlap with other allocated
2872 sections. This can happen when the linker script specifically specifies
2873 the output section addresses of the two sections. */
2876 lang_check_section_addresses (void)
2880 /* Scan all sections in the output list. */
2881 for (s = output_bfd->sections; s != NULL; s = s->next)
2885 /* Ignore sections which are not loaded or which have no contents. */
2886 if (IGNORE_SECTION (s))
2889 /* Once we reach section 's' stop our seach. This prevents two
2890 warning messages from being produced, one for 'section A overlaps
2891 section B' and one for 'section B overlaps section A'. */
2892 for (os = output_bfd->sections; os != s; os = os->next)
2899 /* Only consider loadable sections with real contents. */
2900 if (IGNORE_SECTION (os))
2903 /* We must check the sections' LMA addresses not their
2904 VMA addresses because overlay sections can have
2905 overlapping VMAs but they must have distinct LMAs. */
2906 s_start = bfd_section_lma (output_bfd, s);
2907 os_start = bfd_section_lma (output_bfd, os);
2908 s_end = s_start + TO_ADDR (s->size) - 1;
2909 os_end = os_start + TO_ADDR (os->size) - 1;
2911 /* Look for an overlap. */
2912 if ((s_end < os_start) || (s_start > os_end))
2916 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2917 s->name, s_start, s_end, os->name, os_start, os_end);
2919 /* Once we have found one overlap for this section,
2920 stop looking for others. */
2926 /* Make sure the new address is within the region. We explicitly permit the
2927 current address to be at the exact end of the region when the address is
2928 non-zero, in case the region is at the end of addressable memory and the
2929 calculation wraps around. */
2932 os_region_check (lang_output_section_statement_type *os,
2933 lang_memory_region_type *region,
2937 if ((region->current < region->origin
2938 || (region->current - region->origin > region->length))
2939 && ((region->current != region->origin + region->length)
2944 einfo (_("%X%P: address 0x%v of %B section %s"
2945 " is not within region %s\n"),
2947 os->bfd_section->owner,
2948 os->bfd_section->name,
2953 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2955 os->bfd_section->owner,
2956 os->bfd_section->name);
2958 /* Reset the region pointer. */
2959 region->current = region->origin;
2963 /* Set the sizes for all the output sections. */
2966 lang_size_sections_1
2967 (lang_statement_union_type *s,
2968 lang_output_section_statement_type *output_section_statement,
2969 lang_statement_union_type **prev,
2973 bfd_boolean check_regions)
2975 /* Size up the sections from their constituent parts. */
2976 for (; s != NULL; s = s->header.next)
2978 switch (s->header.type)
2980 case lang_output_section_statement_enum:
2983 lang_output_section_statement_type *os;
2985 os = &s->output_section_statement;
2986 if (os->bfd_section == NULL)
2987 /* This section was never actually created. */
2990 /* If this is a COFF shared library section, use the size and
2991 address from the input section. FIXME: This is COFF
2992 specific; it would be cleaner if there were some other way
2993 to do this, but nothing simple comes to mind. */
2994 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2998 if (os->children.head == NULL
2999 || os->children.head->header.next != NULL
3000 || (os->children.head->header.type
3001 != lang_input_section_enum))
3002 einfo (_("%P%X: Internal error on COFF shared library"
3003 " section %s\n"), os->name);
3005 input = os->children.head->input_section.section;
3006 bfd_set_section_vma (os->bfd_section->owner,
3008 bfd_section_vma (input->owner, input));
3009 os->bfd_section->size = input->size;
3013 if (bfd_is_abs_section (os->bfd_section))
3015 /* No matter what happens, an abs section starts at zero. */
3016 ASSERT (os->bfd_section->vma == 0);
3020 if (os->addr_tree == NULL)
3022 /* No address specified for this section, get one
3023 from the region specification. */
3024 if (os->region == NULL
3025 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3026 && os->region->name[0] == '*'
3027 && strcmp (os->region->name,
3028 DEFAULT_MEMORY_REGION) == 0))
3030 os->region = lang_memory_default (os->bfd_section);
3033 /* If a loadable section is using the default memory
3034 region, and some non default memory regions were
3035 defined, issue an error message. */
3036 if (!IGNORE_SECTION (os->bfd_section)
3037 && ! link_info.relocatable
3039 && strcmp (os->region->name,
3040 DEFAULT_MEMORY_REGION) == 0
3041 && lang_memory_region_list != NULL
3042 && (strcmp (lang_memory_region_list->name,
3043 DEFAULT_MEMORY_REGION) != 0
3044 || lang_memory_region_list->next != NULL))
3046 /* By default this is an error rather than just a
3047 warning because if we allocate the section to the
3048 default memory region we can end up creating an
3049 excessively large binary, or even seg faulting when
3050 attempting to perform a negative seek. See
3051 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3052 for an example of this. This behaviour can be
3053 overridden by the using the --no-check-sections
3055 if (command_line.check_section_addresses)
3056 einfo (_("%P%F: error: no memory region specified"
3057 " for loadable section `%s'\n"),
3058 bfd_get_section_name (output_bfd,
3061 einfo (_("%P: warning: no memory region specified"
3062 " for loadable section `%s'\n"),
3063 bfd_get_section_name (output_bfd,
3067 dot = os->region->current;
3069 if (os->section_alignment == -1)
3074 dot = align_power (dot,
3075 os->bfd_section->alignment_power);
3077 if (dot != olddot && config.warn_section_align)
3078 einfo (_("%P: warning: changing start of section"
3079 " %s by %u bytes\n"),
3080 os->name, (unsigned int) (dot - olddot));
3088 r = exp_fold_tree (os->addr_tree,
3090 lang_allocating_phase_enum,
3095 einfo (_("%F%S: non constant or forward reference"
3096 " address expression for section %s\n"),
3099 dot = r.value + r.section->bfd_section->vma;
3102 /* The section starts here.
3103 First, align to what the section needs. */
3105 if (os->section_alignment != -1)
3106 dot = align_power (dot, os->section_alignment);
3108 bfd_set_section_vma (0, os->bfd_section, dot);
3110 os->bfd_section->output_offset = 0;
3113 lang_size_sections_1 (os->children.head, os, &os->children.head,
3114 os->fill, dot, relax, check_regions);
3116 /* Put the section within the requested block size, or
3117 align at the block boundary. */
3118 after = ((os->bfd_section->vma
3119 + TO_ADDR (os->bfd_section->size)
3120 + os->block_value - 1)
3121 & - (bfd_vma) os->block_value);
3123 if (bfd_is_abs_section (os->bfd_section))
3124 ASSERT (after == os->bfd_section->vma);
3126 os->bfd_section->size
3127 = TO_SIZE (after - os->bfd_section->vma);
3129 dot = os->bfd_section->vma;
3130 /* .tbss sections effectively have zero size. */
3131 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3132 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3133 || link_info.relocatable)
3134 dot += TO_ADDR (os->bfd_section->size);
3138 if (os->update_dot_tree != 0)
3139 exp_fold_tree (os->update_dot_tree, abs_output_section,
3140 lang_allocating_phase_enum, dot, &dot);
3142 /* Update dot in the region ?
3143 We only do this if the section is going to be allocated,
3144 since unallocated sections do not contribute to the region's
3145 overall size in memory.
3147 If the SEC_NEVER_LOAD bit is not set, it will affect the
3148 addresses of sections after it. We have to update
3150 if (os->region != NULL
3151 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3152 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3154 os->region->current = dot;
3157 /* Make sure the new address is within the region. */
3158 os_region_check (os, os->region, os->addr_tree,
3159 os->bfd_section->vma);
3161 /* If there's no load address specified, use the run
3162 region as the load region. */
3163 if (os->lma_region == NULL && os->load_base == NULL)
3164 os->lma_region = os->region;
3166 if (os->lma_region != NULL && os->lma_region != os->region)
3168 /* Set load_base, which will be handled later. */
3169 os->load_base = exp_intop (os->lma_region->current);
3170 os->lma_region->current +=
3171 TO_ADDR (os->bfd_section->size);
3173 os_region_check (os, os->lma_region, NULL,
3174 os->bfd_section->lma);
3180 case lang_constructors_statement_enum:
3181 dot = lang_size_sections_1 (constructor_list.head,
3182 output_section_statement,
3183 &s->wild_statement.children.head,
3184 fill, dot, relax, check_regions);
3187 case lang_data_statement_enum:
3189 unsigned int size = 0;
3191 s->data_statement.output_vma =
3192 dot - output_section_statement->bfd_section->vma;
3193 s->data_statement.output_section =
3194 output_section_statement->bfd_section;
3196 /* We might refer to provided symbols in the expression, and
3197 need to mark them as needed. */
3198 exp_fold_tree (s->data_statement.exp, abs_output_section,
3199 lang_allocating_phase_enum, dot, &dot);
3201 switch (s->data_statement.type)
3219 if (size < TO_SIZE ((unsigned) 1))
3220 size = TO_SIZE ((unsigned) 1);
3221 dot += TO_ADDR (size);
3222 output_section_statement->bfd_section->size += size;
3223 /* The output section gets contents, and then we inspect for
3224 any flags set in the input script which override any ALLOC. */
3225 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3226 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3228 output_section_statement->bfd_section->flags |=
3229 SEC_ALLOC | SEC_LOAD;
3234 case lang_reloc_statement_enum:
3238 s->reloc_statement.output_vma =
3239 dot - output_section_statement->bfd_section->vma;
3240 s->reloc_statement.output_section =
3241 output_section_statement->bfd_section;
3242 size = bfd_get_reloc_size (s->reloc_statement.howto);
3243 dot += TO_ADDR (size);
3244 output_section_statement->bfd_section->size += size;
3248 case lang_wild_statement_enum:
3250 dot = lang_size_sections_1 (s->wild_statement.children.head,
3251 output_section_statement,
3252 &s->wild_statement.children.head,
3253 fill, dot, relax, check_regions);
3257 case lang_object_symbols_statement_enum:
3258 link_info.create_object_symbols_section =
3259 output_section_statement->bfd_section;
3261 case lang_output_statement_enum:
3262 case lang_target_statement_enum:
3264 case lang_input_section_enum:
3268 i = (*prev)->input_section.section;
3273 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3274 einfo (_("%P%F: can't relax section: %E\n"));
3278 dot = size_input_section (prev, output_section_statement,
3279 output_section_statement->fill, dot);
3282 case lang_input_statement_enum:
3284 case lang_fill_statement_enum:
3285 s->fill_statement.output_section =
3286 output_section_statement->bfd_section;
3288 fill = s->fill_statement.fill;
3290 case lang_assignment_statement_enum:
3292 bfd_vma newdot = dot;
3294 exp_fold_tree (s->assignment_statement.exp,
3295 output_section_statement,
3296 lang_allocating_phase_enum,
3302 if (output_section_statement == abs_output_section)
3304 /* If we don't have an output section, then just adjust
3305 the default memory address. */
3306 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3307 FALSE)->current = newdot;
3311 /* Insert a pad after this statement. We can't
3312 put the pad before when relaxing, in case the
3313 assignment references dot. */
3314 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3315 output_section_statement->bfd_section, dot);
3317 /* Don't neuter the pad below when relaxing. */
3321 /* If dot is advanced, this implies that the section should
3322 have space allocated to it, unless the user has explicitly
3323 stated that the section should never be loaded. */
3324 if (!(output_section_statement->flags
3325 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3326 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3333 case lang_padding_statement_enum:
3334 /* If this is the first time lang_size_sections is called,
3335 we won't have any padding statements. If this is the
3336 second or later passes when relaxing, we should allow
3337 padding to shrink. If padding is needed on this pass, it
3338 will be added back in. */
3339 s->padding_statement.size = 0;
3341 /* Make sure output_offset is valid. If relaxation shrinks
3342 the section and this pad isn't needed, it's possible to
3343 have output_offset larger than the final size of the
3344 section. bfd_set_section_contents will complain even for
3345 a pad size of zero. */
3346 s->padding_statement.output_offset
3347 = dot - output_section_statement->bfd_section->vma;
3350 case lang_group_statement_enum:
3351 dot = lang_size_sections_1 (s->group_statement.children.head,
3352 output_section_statement,
3353 &s->group_statement.children.head,
3354 fill, dot, relax, check_regions);
3361 /* We can only get here when relaxing is turned on. */
3362 case lang_address_statement_enum:
3365 prev = &s->header.next;
3372 (lang_statement_union_type *s,
3373 lang_output_section_statement_type *output_section_statement,
3374 lang_statement_union_type **prev,
3378 bfd_boolean check_regions)
3382 /* Callers of exp_fold_tree need to increment this. */
3383 lang_statement_iteration++;
3385 exp_data_seg.phase = exp_dataseg_none;
3386 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3387 dot, relax, check_regions);
3388 if (exp_data_seg.phase == exp_dataseg_end_seen
3389 && link_info.relro && exp_data_seg.relro_end)
3391 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3392 to put exp_data_seg.relro on a (common) page boundary. */
3393 bfd_vma old_base, relro_end;
3395 exp_data_seg.phase = exp_dataseg_relro_adjust;
3396 old_base = exp_data_seg.base;
3397 exp_data_seg.base += (-exp_data_seg.relro_end
3398 & (exp_data_seg.pagesize - 1));
3399 /* Compute the expected PT_GNU_RELRO segment end. */
3400 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
3401 & (exp_data_seg.pagesize - 1);
3402 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3403 dot, relax, check_regions);
3404 if (exp_data_seg.relro_end > relro_end)
3406 /* The alignment of sections between DATA_SEGMENT_ALIGN
3407 and DATA_SEGMENT_RELRO_END caused huge padding to be
3408 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
3410 unsigned int max_alignment_power = 0;
3412 /* Find maximum alignment power of sections between
3413 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
3414 for (sec = output_bfd->sections; sec; sec = sec->next)
3415 if (sec->vma >= exp_data_seg.base
3416 && sec->vma < exp_data_seg.relro_end
3417 && sec->alignment_power > max_alignment_power)
3418 max_alignment_power = sec->alignment_power;
3420 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
3422 if (exp_data_seg.base - (1 << max_alignment_power)
3424 exp_data_seg.base += exp_data_seg.pagesize;
3425 exp_data_seg.base -= (1 << max_alignment_power);
3426 result = lang_size_sections_1 (s, output_section_statement,
3427 prev, fill, dot, relax,
3431 link_info.relro_start = exp_data_seg.base;
3432 link_info.relro_end = exp_data_seg.relro_end;
3434 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3436 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3437 a page could be saved in the data segment. */
3438 bfd_vma first, last;
3440 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3441 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3443 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3444 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3445 && first + last <= exp_data_seg.pagesize)
3447 exp_data_seg.phase = exp_dataseg_adjust;
3448 lang_statement_iteration++;
3449 result = lang_size_sections_1 (s, output_section_statement, prev,
3450 fill, dot, relax, check_regions);
3457 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3460 lang_do_assignments_1
3461 (lang_statement_union_type *s,
3462 lang_output_section_statement_type *output_section_statement,
3466 for (; s != NULL; s = s->header.next)
3468 switch (s->header.type)
3470 case lang_constructors_statement_enum:
3471 dot = lang_do_assignments_1 (constructor_list.head,
3472 output_section_statement,
3477 case lang_output_section_statement_enum:
3479 lang_output_section_statement_type *os;
3481 os = &(s->output_section_statement);
3482 if (os->bfd_section != NULL)
3484 dot = os->bfd_section->vma;
3485 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3486 /* .tbss sections effectively have zero size. */
3487 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3488 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3489 || link_info.relocatable)
3490 dot += TO_ADDR (os->bfd_section->size);
3494 /* If nothing has been placed into the output section then
3495 it won't have a bfd_section. */
3496 if (os->bfd_section)
3498 os->bfd_section->lma
3499 = exp_get_abs_int (os->load_base, 0, "load base",
3500 lang_final_phase_enum);
3505 case lang_wild_statement_enum:
3507 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3508 output_section_statement,
3513 case lang_object_symbols_statement_enum:
3514 case lang_output_statement_enum:
3515 case lang_target_statement_enum:
3517 case lang_common_statement_enum:
3520 case lang_data_statement_enum:
3522 etree_value_type value;
3524 value = exp_fold_tree (s->data_statement.exp,
3526 lang_final_phase_enum, dot, &dot);
3528 einfo (_("%F%P: invalid data statement\n"));
3529 s->data_statement.value
3530 = value.value + value.section->bfd_section->vma;
3534 switch (s->data_statement.type)
3552 if (size < TO_SIZE ((unsigned) 1))
3553 size = TO_SIZE ((unsigned) 1);
3554 dot += TO_ADDR (size);
3558 case lang_reloc_statement_enum:
3560 etree_value_type value;
3562 value = exp_fold_tree (s->reloc_statement.addend_exp,
3564 lang_final_phase_enum, dot, &dot);
3565 s->reloc_statement.addend_value = value.value;
3567 einfo (_("%F%P: invalid reloc statement\n"));
3569 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3572 case lang_input_section_enum:
3574 asection *in = s->input_section.section;
3576 if ((in->flags & SEC_EXCLUDE) == 0)
3577 dot += TO_ADDR (in->size);
3581 case lang_input_statement_enum:
3583 case lang_fill_statement_enum:
3584 fill = s->fill_statement.fill;
3586 case lang_assignment_statement_enum:
3588 exp_fold_tree (s->assignment_statement.exp,
3589 output_section_statement,
3590 lang_final_phase_enum,
3596 case lang_padding_statement_enum:
3597 dot += TO_ADDR (s->padding_statement.size);
3600 case lang_group_statement_enum:
3601 dot = lang_do_assignments_1 (s->group_statement.children.head,
3602 output_section_statement,
3610 case lang_address_statement_enum:
3620 (lang_statement_union_type *s,
3621 lang_output_section_statement_type *output_section_statement,
3625 /* Callers of exp_fold_tree need to increment this. */
3626 lang_statement_iteration++;
3627 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3630 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3631 operator .startof. (section_name), it produces an undefined symbol
3632 .startof.section_name. Similarly, when it sees
3633 .sizeof. (section_name), it produces an undefined symbol
3634 .sizeof.section_name. For all the output sections, we look for
3635 such symbols, and set them to the correct value. */
3638 lang_set_startof (void)
3642 if (link_info.relocatable)
3645 for (s = output_bfd->sections; s != NULL; s = s->next)
3647 const char *secname;
3649 struct bfd_link_hash_entry *h;
3651 secname = bfd_get_section_name (output_bfd, s);
3652 buf = xmalloc (10 + strlen (secname));
3654 sprintf (buf, ".startof.%s", secname);
3655 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3656 if (h != NULL && h->type == bfd_link_hash_undefined)
3658 h->type = bfd_link_hash_defined;
3659 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3660 h->u.def.section = bfd_abs_section_ptr;
3663 sprintf (buf, ".sizeof.%s", secname);
3664 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3665 if (h != NULL && h->type == bfd_link_hash_undefined)
3667 h->type = bfd_link_hash_defined;
3668 h->u.def.value = TO_ADDR (s->size);
3669 h->u.def.section = bfd_abs_section_ptr;
3679 struct bfd_link_hash_entry *h;
3682 if (link_info.relocatable || link_info.shared)
3687 if (entry_symbol.name == NULL)
3689 /* No entry has been specified. Look for start, but don't warn
3690 if we don't find it. */
3691 entry_symbol.name = "start";
3695 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3696 FALSE, FALSE, TRUE);
3698 && (h->type == bfd_link_hash_defined
3699 || h->type == bfd_link_hash_defweak)
3700 && h->u.def.section->output_section != NULL)
3704 val = (h->u.def.value
3705 + bfd_get_section_vma (output_bfd,
3706 h->u.def.section->output_section)
3707 + h->u.def.section->output_offset);
3708 if (! bfd_set_start_address (output_bfd, val))
3709 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3716 /* We couldn't find the entry symbol. Try parsing it as a
3718 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3721 if (! bfd_set_start_address (output_bfd, val))
3722 einfo (_("%P%F: can't set start address\n"));
3728 /* Can't find the entry symbol, and it's not a number. Use
3729 the first address in the text section. */
3730 ts = bfd_get_section_by_name (output_bfd, entry_section);
3734 einfo (_("%P: warning: cannot find entry symbol %s;"
3735 " defaulting to %V\n"),
3737 bfd_get_section_vma (output_bfd, ts));
3738 if (! bfd_set_start_address (output_bfd,
3739 bfd_get_section_vma (output_bfd,
3741 einfo (_("%P%F: can't set start address\n"));
3746 einfo (_("%P: warning: cannot find entry symbol %s;"
3747 " not setting start address\n"),
3753 /* Don't bfd_hash_table_free (&lang_definedness_table);
3754 map file output may result in a call of lang_track_definedness. */
3757 /* This is a small function used when we want to ignore errors from
3761 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3763 /* Don't do anything. */
3766 /* Check that the architecture of all the input files is compatible
3767 with the output file. Also call the backend to let it do any
3768 other checking that is needed. */
3773 lang_statement_union_type *file;
3775 const bfd_arch_info_type *compatible;
3777 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3779 input_bfd = file->input_statement.the_bfd;
3781 = bfd_arch_get_compatible (input_bfd, output_bfd,
3782 command_line.accept_unknown_input_arch);
3784 /* In general it is not possible to perform a relocatable
3785 link between differing object formats when the input
3786 file has relocations, because the relocations in the
3787 input format may not have equivalent representations in
3788 the output format (and besides BFD does not translate
3789 relocs for other link purposes than a final link). */
3790 if ((link_info.relocatable || link_info.emitrelocations)
3791 && (compatible == NULL
3792 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3793 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3795 einfo (_("%P%F: Relocatable linking with relocations from"
3796 " format %s (%B) to format %s (%B) is not supported\n"),
3797 bfd_get_target (input_bfd), input_bfd,
3798 bfd_get_target (output_bfd), output_bfd);
3799 /* einfo with %F exits. */
3802 if (compatible == NULL)
3804 if (command_line.warn_mismatch)
3805 einfo (_("%P: warning: %s architecture of input file `%B'"
3806 " is incompatible with %s output\n"),
3807 bfd_printable_name (input_bfd), input_bfd,
3808 bfd_printable_name (output_bfd));
3810 else if (bfd_count_sections (input_bfd))
3812 /* If the input bfd has no contents, it shouldn't set the
3813 private data of the output bfd. */
3815 bfd_error_handler_type pfn = NULL;
3817 /* If we aren't supposed to warn about mismatched input
3818 files, temporarily set the BFD error handler to a
3819 function which will do nothing. We still want to call
3820 bfd_merge_private_bfd_data, since it may set up
3821 information which is needed in the output file. */
3822 if (! command_line.warn_mismatch)
3823 pfn = bfd_set_error_handler (ignore_bfd_errors);
3824 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3826 if (command_line.warn_mismatch)
3827 einfo (_("%P%X: failed to merge target specific data"
3828 " of file %B\n"), input_bfd);
3830 if (! command_line.warn_mismatch)
3831 bfd_set_error_handler (pfn);
3836 /* Look through all the global common symbols and attach them to the
3837 correct section. The -sort-common command line switch may be used
3838 to roughly sort the entries by size. */
3843 if (command_line.inhibit_common_definition)
3845 if (link_info.relocatable
3846 && ! command_line.force_common_definition)
3849 if (! config.sort_common)
3850 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3855 for (power = 4; power >= 0; power--)
3856 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3860 /* Place one common symbol in the correct section. */
3863 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3865 unsigned int power_of_two;
3869 if (h->type != bfd_link_hash_common)
3873 power_of_two = h->u.c.p->alignment_power;
3875 if (config.sort_common
3876 && power_of_two < (unsigned int) *(int *) info)
3879 section = h->u.c.p->section;
3881 /* Increase the size of the section to align the common sym. */
3882 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3883 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3885 /* Adjust the alignment if necessary. */
3886 if (power_of_two > section->alignment_power)
3887 section->alignment_power = power_of_two;
3889 /* Change the symbol from common to defined. */
3890 h->type = bfd_link_hash_defined;
3891 h->u.def.section = section;
3892 h->u.def.value = section->size;
3894 /* Increase the size of the section. */
3895 section->size += size;
3897 /* Make sure the section is allocated in memory, and make sure that
3898 it is no longer a common section. */
3899 section->flags |= SEC_ALLOC;
3900 section->flags &= ~SEC_IS_COMMON;
3902 if (config.map_file != NULL)
3904 static bfd_boolean header_printed;
3909 if (! header_printed)
3911 minfo (_("\nAllocating common symbols\n"));
3912 minfo (_("Common symbol size file\n\n"));
3913 header_printed = TRUE;
3916 name = demangle (h->root.string);
3918 len = strlen (name);
3933 if (size <= 0xffffffff)
3934 sprintf (buf, "%lx", (unsigned long) size);
3936 sprintf_vma (buf, size);
3946 minfo ("%B\n", section->owner);
3952 /* Run through the input files and ensure that every input section has
3953 somewhere to go. If one is found without a destination then create
3954 an input request and place it into the statement tree. */
3957 lang_place_orphans (void)
3959 LANG_FOR_EACH_INPUT_STATEMENT (file)
3963 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3965 if (s->output_section == NULL)
3967 /* This section of the file is not attached, root
3968 around for a sensible place for it to go. */
3970 if (file->just_syms_flag)
3973 if ((s->flags & SEC_EXCLUDE) != 0)
3974 s->output_section = bfd_abs_section_ptr;
3975 else if (strcmp (s->name, "COMMON") == 0)
3977 /* This is a lonely common section which must have
3978 come from an archive. We attach to the section
3979 with the wildcard. */
3980 if (! link_info.relocatable
3981 || command_line.force_common_definition)
3983 if (default_common_section == NULL)
3986 /* This message happens when using the
3987 svr3.ifile linker script, so I have
3989 info_msg (_("%P: no [COMMON] command,"
3990 " defaulting to .bss\n"));
3992 default_common_section =
3993 lang_output_section_statement_lookup (".bss");
3996 lang_add_section (&default_common_section->children, s,
3997 default_common_section, file);
4000 else if (ldemul_place_orphan (file, s))
4004 lang_output_section_statement_type *os;
4006 os = lang_output_section_statement_lookup (s->name);
4007 lang_add_section (&os->children, s, os, file);
4015 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4017 flagword *ptr_flags;
4019 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4025 *ptr_flags |= SEC_ALLOC;
4029 *ptr_flags |= SEC_READONLY;
4033 *ptr_flags |= SEC_DATA;
4037 *ptr_flags |= SEC_CODE;
4042 *ptr_flags |= SEC_LOAD;
4046 einfo (_("%P%F: invalid syntax in flags\n"));
4053 /* Call a function on each input file. This function will be called
4054 on an archive, but not on the elements. */
4057 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4059 lang_input_statement_type *f;
4061 for (f = (lang_input_statement_type *) input_file_chain.head;
4063 f = (lang_input_statement_type *) f->next_real_file)
4067 /* Call a function on each file. The function will be called on all
4068 the elements of an archive which are included in the link, but will
4069 not be called on the archive file itself. */
4072 lang_for_each_file (void (*func) (lang_input_statement_type *))
4074 LANG_FOR_EACH_INPUT_STATEMENT (f)
4081 ldlang_add_file (lang_input_statement_type *entry)
4085 lang_statement_append (&file_chain,
4086 (lang_statement_union_type *) entry,
4089 /* The BFD linker needs to have a list of all input BFDs involved in
4091 ASSERT (entry->the_bfd->link_next == NULL);
4092 ASSERT (entry->the_bfd != output_bfd);
4093 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4095 *pp = entry->the_bfd;
4096 entry->the_bfd->usrdata = entry;
4097 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4099 /* Look through the sections and check for any which should not be
4100 included in the link. We need to do this now, so that we can
4101 notice when the backend linker tries to report multiple
4102 definition errors for symbols which are in sections we aren't
4103 going to link. FIXME: It might be better to entirely ignore
4104 symbols which are defined in sections which are going to be
4105 discarded. This would require modifying the backend linker for
4106 each backend which might set the SEC_LINK_ONCE flag. If we do
4107 this, we should probably handle SEC_EXCLUDE in the same way. */
4109 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4113 lang_add_output (const char *name, int from_script)
4115 /* Make -o on command line override OUTPUT in script. */
4116 if (!had_output_filename || !from_script)
4118 output_filename = name;
4119 had_output_filename = TRUE;
4123 static lang_output_section_statement_type *current_section;
4134 for (l = 0; l < 32; l++)
4136 if (i >= (unsigned int) x)
4144 lang_output_section_statement_type *
4145 lang_enter_output_section_statement (const char *output_section_statement_name,
4146 etree_type *address_exp,
4147 enum section_type sectype,
4149 etree_type *subalign,
4153 lang_output_section_statement_type *os;
4157 lang_output_section_statement_lookup_1 (output_section_statement_name,
4160 /* Add this statement to tree. */
4162 add_statement (lang_output_section_statement_enum,
4163 output_section_statement);
4165 /* Make next things chain into subchain of this. */
4167 if (os->addr_tree == NULL)
4169 os->addr_tree = address_exp;
4171 os->sectype = sectype;
4172 if (sectype != noload_section)
4173 os->flags = SEC_NO_FLAGS;
4175 os->flags = SEC_NEVER_LOAD;
4176 os->block_value = 1;
4177 stat_ptr = &os->children;
4179 os->subsection_alignment =
4180 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4181 os->section_alignment =
4182 topower (exp_get_value_int (align, -1, "section alignment", 0));
4184 os->load_base = ebase;
4191 lang_output_statement_type *new =
4192 new_stat (lang_output_statement, stat_ptr);
4194 new->name = output_filename;
4197 /* Reset the current counters in the regions. */
4200 lang_reset_memory_regions (void)
4202 lang_memory_region_type *p = lang_memory_region_list;
4205 for (p = lang_memory_region_list; p != NULL; p = p->next)
4207 p->old_length = (bfd_size_type) (p->current - p->origin);
4208 p->current = p->origin;
4211 for (o = output_bfd->sections; o != NULL; o = o->next)
4213 /* Save the last size for possible use by bfd_relax_section. */
4214 o->rawsize = o->size;
4219 /* Worker for lang_gc_sections_1. */
4222 gc_section_callback (lang_wild_statement_type *ptr,
4223 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4225 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4226 void *data ATTRIBUTE_UNUSED)
4228 /* If the wild pattern was marked KEEP, the member sections
4229 should be as well. */
4230 if (ptr->keep_sections)
4231 section->flags |= SEC_KEEP;
4234 /* Iterate over sections marking them against GC. */
4237 lang_gc_sections_1 (lang_statement_union_type *s)
4239 for (; s != NULL; s = s->header.next)
4241 switch (s->header.type)
4243 case lang_wild_statement_enum:
4244 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4246 case lang_constructors_statement_enum:
4247 lang_gc_sections_1 (constructor_list.head);
4249 case lang_output_section_statement_enum:
4250 lang_gc_sections_1 (s->output_section_statement.children.head);
4252 case lang_group_statement_enum:
4253 lang_gc_sections_1 (s->group_statement.children.head);
4262 lang_gc_sections (void)
4264 struct bfd_link_hash_entry *h;
4265 ldlang_undef_chain_list_type *ulist;
4267 /* Keep all sections so marked in the link script. */
4269 lang_gc_sections_1 (statement_list.head);
4271 /* Keep all sections containing symbols undefined on the command-line,
4272 and the section containing the entry symbol. */
4274 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4276 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4277 FALSE, FALSE, FALSE);
4280 && (h->type == bfd_link_hash_defined
4281 || h->type == bfd_link_hash_defweak)
4282 && ! bfd_is_abs_section (h->u.def.section))
4284 h->u.def.section->flags |= SEC_KEEP;
4288 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4289 the special case of debug info. (See bfd/stabs.c)
4290 Twiddle the flag here, to simplify later linker code. */
4291 if (link_info.relocatable)
4293 LANG_FOR_EACH_INPUT_STATEMENT (f)
4296 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4297 if ((sec->flags & SEC_DEBUGGING) == 0)
4298 sec->flags &= ~SEC_EXCLUDE;
4302 if (command_line.gc_sections)
4303 bfd_gc_sections (output_bfd, &link_info);
4309 lang_reasonable_defaults ();
4310 current_target = default_target;
4312 /* Open the output file. */
4313 lang_for_each_statement (ldlang_open_output);
4316 ldemul_create_output_section_statements ();
4318 /* Add to the hash table all undefineds on the command line. */
4319 lang_place_undefineds ();
4321 if (!bfd_section_already_linked_table_init ())
4322 einfo (_("%P%F: Failed to create hash table\n"));
4324 /* Create a bfd for each input file. */
4325 current_target = default_target;
4326 open_input_bfds (statement_list.head, FALSE);
4328 link_info.gc_sym_list = &entry_symbol;
4329 if (entry_symbol.name == NULL)
4330 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4332 ldemul_after_open ();
4334 bfd_section_already_linked_table_free ();
4336 /* Make sure that we're not mixing architectures. We call this
4337 after all the input files have been opened, but before we do any
4338 other processing, so that any operations merge_private_bfd_data
4339 does on the output file will be known during the rest of the
4343 /* Handle .exports instead of a version script if we're told to do so. */
4344 if (command_line.version_exports_section)
4345 lang_do_version_exports_section ();
4347 /* Build all sets based on the information gathered from the input
4349 ldctor_build_sets ();
4351 /* Remove unreferenced sections if asked to. */
4352 lang_gc_sections ();
4354 /* Size up the common data. */
4357 /* Update wild statements. */
4358 update_wild_statements (statement_list.head);
4360 /* Run through the contours of the script and attach input sections
4361 to the correct output sections. */
4362 map_input_to_output_sections (statement_list.head, NULL, NULL);
4364 /* Find any sections not attached explicitly and handle them. */
4365 lang_place_orphans ();
4367 if (! link_info.relocatable)
4371 /* Merge SEC_MERGE sections. This has to be done after GC of
4372 sections, so that GCed sections are not merged, but before
4373 assigning dynamic symbols, since removing whole input sections
4375 bfd_merge_sections (output_bfd, &link_info);
4377 /* Look for a text section and set the readonly attribute in it. */
4378 found = bfd_get_section_by_name (output_bfd, ".text");
4382 if (config.text_read_only)
4383 found->flags |= SEC_READONLY;
4385 found->flags &= ~SEC_READONLY;
4389 /* Do anything special before sizing sections. This is where ELF
4390 and other back-ends size dynamic sections. */
4391 ldemul_before_allocation ();
4393 if (!link_info.relocatable)
4394 strip_excluded_output_sections ();
4396 /* We must record the program headers before we try to fix the
4397 section positions, since they will affect SIZEOF_HEADERS. */
4398 lang_record_phdrs ();
4400 /* Size up the sections. */
4401 lang_size_sections (statement_list.head, abs_output_section,
4402 &statement_list.head, 0, 0, NULL,
4403 command_line.relax ? FALSE : TRUE);
4405 /* Now run around and relax if we can. */
4406 if (command_line.relax)
4408 /* Keep relaxing until bfd_relax_section gives up. */
4409 bfd_boolean relax_again;
4413 relax_again = FALSE;
4415 /* Note: pe-dll.c does something like this also. If you find
4416 you need to change this code, you probably need to change
4417 pe-dll.c also. DJ */
4419 /* Do all the assignments with our current guesses as to
4421 lang_do_assignments (statement_list.head, abs_output_section,
4424 /* We must do this after lang_do_assignments, because it uses
4426 lang_reset_memory_regions ();
4428 /* Perform another relax pass - this time we know where the
4429 globals are, so can make a better guess. */
4430 lang_size_sections (statement_list.head, abs_output_section,
4431 &statement_list.head, 0, 0, &relax_again, FALSE);
4433 /* If the normal relax is done and the relax finalize pass
4434 is not performed yet, we perform another relax pass. */
4435 if (!relax_again && link_info.need_relax_finalize)
4437 link_info.need_relax_finalize = FALSE;
4441 while (relax_again);
4443 /* Final extra sizing to report errors. */
4444 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4445 lang_reset_memory_regions ();
4446 lang_size_sections (statement_list.head, abs_output_section,
4447 &statement_list.head, 0, 0, NULL, TRUE);
4450 /* See if anything special should be done now we know how big
4452 ldemul_after_allocation ();
4454 /* Fix any .startof. or .sizeof. symbols. */
4455 lang_set_startof ();
4457 /* Do all the assignments, now that we know the final resting places
4458 of all the symbols. */
4460 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4462 /* Make sure that the section addresses make sense. */
4463 if (! link_info.relocatable
4464 && command_line.check_section_addresses)
4465 lang_check_section_addresses ();
4473 /* EXPORTED TO YACC */
4476 lang_add_wild (struct wildcard_spec *filespec,
4477 struct wildcard_list *section_list,
4478 bfd_boolean keep_sections)
4480 struct wildcard_list *curr, *next;
4481 lang_wild_statement_type *new;
4483 /* Reverse the list as the parser puts it back to front. */
4484 for (curr = section_list, section_list = NULL;
4486 section_list = curr, curr = next)
4488 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4489 placed_commons = TRUE;
4492 curr->next = section_list;
4495 if (filespec != NULL && filespec->name != NULL)
4497 if (strcmp (filespec->name, "*") == 0)
4498 filespec->name = NULL;
4499 else if (! wildcardp (filespec->name))
4500 lang_has_input_file = TRUE;
4503 new = new_stat (lang_wild_statement, stat_ptr);
4504 new->filename = NULL;
4505 new->filenames_sorted = FALSE;
4506 if (filespec != NULL)
4508 new->filename = filespec->name;
4509 new->filenames_sorted = filespec->sorted == by_name;
4511 new->section_list = section_list;
4512 new->keep_sections = keep_sections;
4513 lang_list_init (&new->children);
4517 lang_section_start (const char *name, etree_type *address)
4519 lang_address_statement_type *ad;
4521 ad = new_stat (lang_address_statement, stat_ptr);
4522 ad->section_name = name;
4523 ad->address = address;
4526 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4527 because of a -e argument on the command line, or zero if this is
4528 called by ENTRY in a linker script. Command line arguments take
4532 lang_add_entry (const char *name, bfd_boolean cmdline)
4534 if (entry_symbol.name == NULL
4536 || ! entry_from_cmdline)
4538 entry_symbol.name = name;
4539 entry_from_cmdline = cmdline;
4544 lang_add_target (const char *name)
4546 lang_target_statement_type *new = new_stat (lang_target_statement,
4554 lang_add_map (const char *name)
4561 map_option_f = TRUE;
4569 lang_add_fill (fill_type *fill)
4571 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4578 lang_add_data (int type, union etree_union *exp)
4581 lang_data_statement_type *new = new_stat (lang_data_statement,
4589 /* Create a new reloc statement. RELOC is the BFD relocation type to
4590 generate. HOWTO is the corresponding howto structure (we could
4591 look this up, but the caller has already done so). SECTION is the
4592 section to generate a reloc against, or NAME is the name of the
4593 symbol to generate a reloc against. Exactly one of SECTION and
4594 NAME must be NULL. ADDEND is an expression for the addend. */
4597 lang_add_reloc (bfd_reloc_code_real_type reloc,
4598 reloc_howto_type *howto,
4601 union etree_union *addend)
4603 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4607 p->section = section;
4609 p->addend_exp = addend;
4611 p->addend_value = 0;
4612 p->output_section = NULL;
4616 lang_assignment_statement_type *
4617 lang_add_assignment (etree_type *exp)
4619 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4627 lang_add_attribute (enum statement_enum attribute)
4629 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4633 lang_startup (const char *name)
4635 if (startup_file != NULL)
4637 einfo (_("%P%Fmultiple STARTUP files\n"));
4639 first_file->filename = name;
4640 first_file->local_sym_name = name;
4641 first_file->real = TRUE;
4643 startup_file = name;
4647 lang_float (bfd_boolean maybe)
4649 lang_float_flag = maybe;
4653 /* Work out the load- and run-time regions from a script statement, and
4654 store them in *LMA_REGION and *REGION respectively.
4656 MEMSPEC is the name of the run-time region, or the value of
4657 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4658 LMA_MEMSPEC is the name of the load-time region, or null if the
4659 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4660 had an explicit load address.
4662 It is an error to specify both a load region and a load address. */
4665 lang_get_regions (lang_memory_region_type **region,
4666 lang_memory_region_type **lma_region,
4667 const char *memspec,
4668 const char *lma_memspec,
4669 bfd_boolean have_lma,
4670 bfd_boolean have_vma)
4672 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4674 /* If no runtime region or VMA has been specified, but the load region
4675 has been specified, then use the load region for the runtime region
4677 if (lma_memspec != NULL
4679 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4680 *region = *lma_region;
4682 *region = lang_memory_region_lookup (memspec, FALSE);
4684 if (have_lma && lma_memspec != 0)
4685 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4689 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4690 lang_output_section_phdr_list *phdrs,
4691 const char *lma_memspec)
4693 lang_get_regions (¤t_section->region,
4694 ¤t_section->lma_region,
4695 memspec, lma_memspec,
4696 current_section->load_base != NULL,
4697 current_section->addr_tree != NULL);
4698 current_section->fill = fill;
4699 current_section->phdrs = phdrs;
4700 stat_ptr = &statement_list;
4703 /* Create an absolute symbol with the given name with the value of the
4704 address of first byte of the section named.
4706 If the symbol already exists, then do nothing. */
4709 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4711 struct bfd_link_hash_entry *h;
4713 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4715 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4717 if (h->type == bfd_link_hash_new
4718 || h->type == bfd_link_hash_undefined)
4722 h->type = bfd_link_hash_defined;
4724 sec = bfd_get_section_by_name (output_bfd, secname);
4728 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4730 h->u.def.section = bfd_abs_section_ptr;
4734 /* Create an absolute symbol with the given name with the value of the
4735 address of the first byte after the end of the section named.
4737 If the symbol already exists, then do nothing. */
4740 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4742 struct bfd_link_hash_entry *h;
4744 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4746 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4748 if (h->type == bfd_link_hash_new
4749 || h->type == bfd_link_hash_undefined)
4753 h->type = bfd_link_hash_defined;
4755 sec = bfd_get_section_by_name (output_bfd, secname);
4759 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4760 + TO_ADDR (sec->size));
4762 h->u.def.section = bfd_abs_section_ptr;
4767 lang_statement_append (lang_statement_list_type *list,
4768 lang_statement_union_type *element,
4769 lang_statement_union_type **field)
4771 *(list->tail) = element;
4775 /* Set the output format type. -oformat overrides scripts. */
4778 lang_add_output_format (const char *format,
4783 if (output_target == NULL || !from_script)
4785 if (command_line.endian == ENDIAN_BIG
4788 else if (command_line.endian == ENDIAN_LITTLE
4792 output_target = format;
4796 /* Enter a group. This creates a new lang_group_statement, and sets
4797 stat_ptr to build new statements within the group. */
4800 lang_enter_group (void)
4802 lang_group_statement_type *g;
4804 g = new_stat (lang_group_statement, stat_ptr);
4805 lang_list_init (&g->children);
4806 stat_ptr = &g->children;
4809 /* Leave a group. This just resets stat_ptr to start writing to the
4810 regular list of statements again. Note that this will not work if
4811 groups can occur inside anything else which can adjust stat_ptr,
4812 but currently they can't. */
4815 lang_leave_group (void)
4817 stat_ptr = &statement_list;
4820 /* Add a new program header. This is called for each entry in a PHDRS
4821 command in a linker script. */
4824 lang_new_phdr (const char *name,
4826 bfd_boolean filehdr,
4831 struct lang_phdr *n, **pp;
4833 n = stat_alloc (sizeof (struct lang_phdr));
4836 n->type = exp_get_value_int (type, 0, "program header type",
4837 lang_final_phase_enum);
4838 n->filehdr = filehdr;
4843 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4848 /* Record the program header information in the output BFD. FIXME: We
4849 should not be calling an ELF specific function here. */
4852 lang_record_phdrs (void)
4856 lang_output_section_phdr_list *last;
4857 struct lang_phdr *l;
4858 lang_statement_union_type *u;
4861 secs = xmalloc (alc * sizeof (asection *));
4863 for (l = lang_phdr_list; l != NULL; l = l->next)
4870 for (u = lang_output_section_statement.head;
4872 u = u->output_section_statement.next)
4874 lang_output_section_statement_type *os;
4875 lang_output_section_phdr_list *pl;
4877 os = &u->output_section_statement;
4878 if (os->constraint == -1)
4886 if (os->sectype == noload_section
4887 || os->bfd_section == NULL
4888 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4893 if (os->bfd_section == NULL)
4896 for (; pl != NULL; pl = pl->next)
4898 if (strcmp (pl->name, l->name) == 0)
4903 secs = xrealloc (secs, alc * sizeof (asection *));
4905 secs[c] = os->bfd_section;
4912 if (l->flags == NULL)
4915 flags = exp_get_vma (l->flags, 0, "phdr flags",
4916 lang_final_phase_enum);
4921 at = exp_get_vma (l->at, 0, "phdr load address",
4922 lang_final_phase_enum);
4924 if (! bfd_record_phdr (output_bfd, l->type,
4925 l->flags != NULL, flags, l->at != NULL,
4926 at, l->filehdr, l->phdrs, c, secs))
4927 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4932 /* Make sure all the phdr assignments succeeded. */
4933 for (u = lang_output_section_statement.head;
4935 u = u->output_section_statement.next)
4937 lang_output_section_phdr_list *pl;
4939 if (u->output_section_statement.constraint == -1
4940 || u->output_section_statement.bfd_section == NULL)
4943 for (pl = u->output_section_statement.phdrs;
4946 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4947 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4948 u->output_section_statement.name, pl->name);
4952 /* Record a list of sections which may not be cross referenced. */
4955 lang_add_nocrossref (lang_nocrossref_type *l)
4957 struct lang_nocrossrefs *n;
4959 n = xmalloc (sizeof *n);
4960 n->next = nocrossref_list;
4962 nocrossref_list = n;
4964 /* Set notice_all so that we get informed about all symbols. */
4965 link_info.notice_all = TRUE;
4968 /* Overlay handling. We handle overlays with some static variables. */
4970 /* The overlay virtual address. */
4971 static etree_type *overlay_vma;
4972 /* And subsection alignment. */
4973 static etree_type *overlay_subalign;
4975 /* An expression for the maximum section size seen so far. */
4976 static etree_type *overlay_max;
4978 /* A list of all the sections in this overlay. */
4980 struct overlay_list {
4981 struct overlay_list *next;
4982 lang_output_section_statement_type *os;
4985 static struct overlay_list *overlay_list;
4987 /* Start handling an overlay. */
4990 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4992 /* The grammar should prevent nested overlays from occurring. */
4993 ASSERT (overlay_vma == NULL
4994 && overlay_subalign == NULL
4995 && overlay_max == NULL);
4997 overlay_vma = vma_expr;
4998 overlay_subalign = subalign;
5001 /* Start a section in an overlay. We handle this by calling
5002 lang_enter_output_section_statement with the correct VMA.
5003 lang_leave_overlay sets up the LMA and memory regions. */
5006 lang_enter_overlay_section (const char *name)
5008 struct overlay_list *n;
5011 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5012 0, overlay_subalign, 0, 0);
5014 /* If this is the first section, then base the VMA of future
5015 sections on this one. This will work correctly even if `.' is
5016 used in the addresses. */
5017 if (overlay_list == NULL)
5018 overlay_vma = exp_nameop (ADDR, name);
5020 /* Remember the section. */
5021 n = xmalloc (sizeof *n);
5022 n->os = current_section;
5023 n->next = overlay_list;
5026 size = exp_nameop (SIZEOF, name);
5028 /* Arrange to work out the maximum section end address. */
5029 if (overlay_max == NULL)
5032 overlay_max = exp_binop (MAX_K, overlay_max, size);
5035 /* Finish a section in an overlay. There isn't any special to do
5039 lang_leave_overlay_section (fill_type *fill,
5040 lang_output_section_phdr_list *phdrs)
5047 name = current_section->name;
5049 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5050 region and that no load-time region has been specified. It doesn't
5051 really matter what we say here, since lang_leave_overlay will
5053 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5055 /* Define the magic symbols. */
5057 clean = xmalloc (strlen (name) + 1);
5059 for (s1 = name; *s1 != '\0'; s1++)
5060 if (ISALNUM (*s1) || *s1 == '_')
5064 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5065 sprintf (buf, "__load_start_%s", clean);
5066 lang_add_assignment (exp_assop ('=', buf,
5067 exp_nameop (LOADADDR, name)));
5069 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5070 sprintf (buf, "__load_stop_%s", clean);
5071 lang_add_assignment (exp_assop ('=', buf,
5073 exp_nameop (LOADADDR, name),
5074 exp_nameop (SIZEOF, name))));
5079 /* Finish an overlay. If there are any overlay wide settings, this
5080 looks through all the sections in the overlay and sets them. */
5083 lang_leave_overlay (etree_type *lma_expr,
5086 const char *memspec,
5087 lang_output_section_phdr_list *phdrs,
5088 const char *lma_memspec)
5090 lang_memory_region_type *region;
5091 lang_memory_region_type *lma_region;
5092 struct overlay_list *l;
5093 lang_nocrossref_type *nocrossref;
5095 lang_get_regions (®ion, &lma_region,
5096 memspec, lma_memspec,
5097 lma_expr != NULL, FALSE);
5101 /* After setting the size of the last section, set '.' to end of the
5103 if (overlay_list != NULL)
5104 overlay_list->os->update_dot_tree
5105 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5110 struct overlay_list *next;
5112 if (fill != NULL && l->os->fill == NULL)
5115 l->os->region = region;
5116 l->os->lma_region = lma_region;
5118 /* The first section has the load address specified in the
5119 OVERLAY statement. The rest are worked out from that.
5120 The base address is not needed (and should be null) if
5121 an LMA region was specified. */
5123 l->os->load_base = lma_expr;
5124 else if (lma_region == 0)
5125 l->os->load_base = exp_binop ('+',
5126 exp_nameop (LOADADDR, l->next->os->name),
5127 exp_nameop (SIZEOF, l->next->os->name));
5129 if (phdrs != NULL && l->os->phdrs == NULL)
5130 l->os->phdrs = phdrs;
5134 lang_nocrossref_type *nc;
5136 nc = xmalloc (sizeof *nc);
5137 nc->name = l->os->name;
5138 nc->next = nocrossref;
5147 if (nocrossref != NULL)
5148 lang_add_nocrossref (nocrossref);
5151 overlay_list = NULL;
5155 /* Version handling. This is only useful for ELF. */
5157 /* This global variable holds the version tree that we build. */
5159 struct bfd_elf_version_tree *lang_elf_version_info;
5161 /* If PREV is NULL, return first version pattern matching particular symbol.
5162 If PREV is non-NULL, return first version pattern matching particular
5163 symbol after PREV (previously returned by lang_vers_match). */
5165 static struct bfd_elf_version_expr *
5166 lang_vers_match (struct bfd_elf_version_expr_head *head,
5167 struct bfd_elf_version_expr *prev,
5170 const char *cxx_sym = sym;
5171 const char *java_sym = sym;
5172 struct bfd_elf_version_expr *expr = NULL;
5174 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5176 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5180 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5182 java_sym = cplus_demangle (sym, DMGL_JAVA);
5187 if (head->htab && (prev == NULL || prev->symbol))
5189 struct bfd_elf_version_expr e;
5191 switch (prev ? prev->mask : 0)
5194 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5197 expr = htab_find (head->htab, &e);
5198 while (expr && strcmp (expr->symbol, sym) == 0)
5199 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5205 case BFD_ELF_VERSION_C_TYPE:
5206 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5209 expr = htab_find (head->htab, &e);
5210 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5211 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5217 case BFD_ELF_VERSION_CXX_TYPE:
5218 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5220 e.symbol = java_sym;
5221 expr = htab_find (head->htab, &e);
5222 while (expr && strcmp (expr->symbol, java_sym) == 0)
5223 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5234 /* Finally, try the wildcards. */
5235 if (prev == NULL || prev->symbol)
5236 expr = head->remaining;
5243 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5246 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5248 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5252 if (fnmatch (expr->pattern, s, 0) == 0)
5259 free ((char *) cxx_sym);
5260 if (java_sym != sym)
5261 free ((char *) java_sym);
5265 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5266 return a string pointing to the symbol name. */
5269 realsymbol (const char *pattern)
5272 bfd_boolean changed = FALSE, backslash = FALSE;
5273 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5275 for (p = pattern, s = symbol; *p != '\0'; ++p)
5277 /* It is a glob pattern only if there is no preceding
5279 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5287 /* Remove the preceding backslash. */
5294 backslash = *p == '\\';
5309 /* This is called for each variable name or match expression. */
5311 struct bfd_elf_version_expr *
5312 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5316 struct bfd_elf_version_expr *ret;
5318 ret = xmalloc (sizeof *ret);
5323 ret->symbol = realsymbol (new);
5325 if (lang == NULL || strcasecmp (lang, "C") == 0)
5326 ret->mask = BFD_ELF_VERSION_C_TYPE;
5327 else if (strcasecmp (lang, "C++") == 0)
5328 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5329 else if (strcasecmp (lang, "Java") == 0)
5330 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5333 einfo (_("%X%P: unknown language `%s' in version information\n"),
5335 ret->mask = BFD_ELF_VERSION_C_TYPE;
5338 return ldemul_new_vers_pattern (ret);
5341 /* This is called for each set of variable names and match
5344 struct bfd_elf_version_tree *
5345 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5346 struct bfd_elf_version_expr *locals)
5348 struct bfd_elf_version_tree *ret;
5350 ret = xcalloc (1, sizeof *ret);
5351 ret->globals.list = globals;
5352 ret->locals.list = locals;
5353 ret->match = lang_vers_match;
5354 ret->name_indx = (unsigned int) -1;
5358 /* This static variable keeps track of version indices. */
5360 static int version_index;
5363 version_expr_head_hash (const void *p)
5365 const struct bfd_elf_version_expr *e = p;
5367 return htab_hash_string (e->symbol);
5371 version_expr_head_eq (const void *p1, const void *p2)
5373 const struct bfd_elf_version_expr *e1 = p1;
5374 const struct bfd_elf_version_expr *e2 = p2;
5376 return strcmp (e1->symbol, e2->symbol) == 0;
5380 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5383 struct bfd_elf_version_expr *e, *next;
5384 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5386 for (e = head->list; e; e = e->next)
5390 head->mask |= e->mask;
5395 head->htab = htab_create (count * 2, version_expr_head_hash,
5396 version_expr_head_eq, NULL);
5397 list_loc = &head->list;
5398 remaining_loc = &head->remaining;
5399 for (e = head->list; e; e = next)
5405 remaining_loc = &e->next;
5409 void **loc = htab_find_slot (head->htab, e, INSERT);
5413 struct bfd_elf_version_expr *e1, *last;
5419 if (e1->mask == e->mask)
5427 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5431 /* This is a duplicate. */
5432 /* FIXME: Memory leak. Sometimes pattern is not
5433 xmalloced alone, but in larger chunk of memory. */
5434 /* free (e->symbol); */
5439 e->next = last->next;
5447 list_loc = &e->next;
5451 *remaining_loc = NULL;
5452 *list_loc = head->remaining;
5455 head->remaining = head->list;
5458 /* This is called when we know the name and dependencies of the
5462 lang_register_vers_node (const char *name,
5463 struct bfd_elf_version_tree *version,
5464 struct bfd_elf_version_deps *deps)
5466 struct bfd_elf_version_tree *t, **pp;
5467 struct bfd_elf_version_expr *e1;
5472 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5473 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5475 einfo (_("%X%P: anonymous version tag cannot be combined"
5476 " with other version tags\n"));
5481 /* Make sure this node has a unique name. */
5482 for (t = lang_elf_version_info; t != NULL; t = t->next)
5483 if (strcmp (t->name, name) == 0)
5484 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5486 lang_finalize_version_expr_head (&version->globals);
5487 lang_finalize_version_expr_head (&version->locals);
5489 /* Check the global and local match names, and make sure there
5490 aren't any duplicates. */
5492 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5494 for (t = lang_elf_version_info; t != NULL; t = t->next)
5496 struct bfd_elf_version_expr *e2;
5498 if (t->locals.htab && e1->symbol)
5500 e2 = htab_find (t->locals.htab, e1);
5501 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5503 if (e1->mask == e2->mask)
5504 einfo (_("%X%P: duplicate expression `%s'"
5505 " in version information\n"), e1->symbol);
5509 else if (!e1->symbol)
5510 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5511 if (strcmp (e1->pattern, e2->pattern) == 0
5512 && e1->mask == e2->mask)
5513 einfo (_("%X%P: duplicate expression `%s'"
5514 " in version information\n"), e1->pattern);
5518 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5520 for (t = lang_elf_version_info; t != NULL; t = t->next)
5522 struct bfd_elf_version_expr *e2;
5524 if (t->globals.htab && e1->symbol)
5526 e2 = htab_find (t->globals.htab, e1);
5527 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5529 if (e1->mask == e2->mask)
5530 einfo (_("%X%P: duplicate expression `%s'"
5531 " in version information\n"),
5536 else if (!e1->symbol)
5537 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5538 if (strcmp (e1->pattern, e2->pattern) == 0
5539 && e1->mask == e2->mask)
5540 einfo (_("%X%P: duplicate expression `%s'"
5541 " in version information\n"), e1->pattern);
5545 version->deps = deps;
5546 version->name = name;
5547 if (name[0] != '\0')
5550 version->vernum = version_index;
5553 version->vernum = 0;
5555 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5560 /* This is called when we see a version dependency. */
5562 struct bfd_elf_version_deps *
5563 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5565 struct bfd_elf_version_deps *ret;
5566 struct bfd_elf_version_tree *t;
5568 ret = xmalloc (sizeof *ret);
5571 for (t = lang_elf_version_info; t != NULL; t = t->next)
5573 if (strcmp (t->name, name) == 0)
5575 ret->version_needed = t;
5580 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5586 lang_do_version_exports_section (void)
5588 struct bfd_elf_version_expr *greg = NULL, *lreg;
5590 LANG_FOR_EACH_INPUT_STATEMENT (is)
5592 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5600 contents = xmalloc (len);
5601 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5602 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5605 while (p < contents + len)
5607 greg = lang_new_vers_pattern (greg, p, NULL);
5608 p = strchr (p, '\0') + 1;
5611 /* Do not free the contents, as we used them creating the regex. */
5613 /* Do not include this section in the link. */
5614 sec->flags |= SEC_EXCLUDE;
5617 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5618 lang_register_vers_node (command_line.version_exports_section,
5619 lang_new_vers_node (greg, lreg), NULL);
5623 lang_add_unique (const char *name)
5625 struct unique_sections *ent;
5627 for (ent = unique_section_list; ent; ent = ent->next)
5628 if (strcmp (ent->name, name) == 0)
5631 ent = xmalloc (sizeof *ent);
5632 ent->name = xstrdup (name);
5633 ent->next = unique_section_list;
5634 unique_section_list = ent;