1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "libiberty.h"
44 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
46 lang_statement_list_type*));
50 static struct obstack stat_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 boolean placed_commons = false;
57 static lang_output_section_statement_type *default_common_section;
58 static boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static lang_statement_list_type lang_output_section_statement;
62 static CONST char *current_target;
63 static CONST char *output_target;
64 static lang_statement_list_type statement_list;
65 static struct lang_phdr *lang_phdr_list;
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*func) (lang_statement_union_type *),
69 lang_statement_union_type *s));
70 static lang_input_statement_type *new_afile
71 PARAMS ((const char *name, lang_input_file_enum_type file_type,
72 const char *target, boolean add_to_list));
73 static void init_os PARAMS ((lang_output_section_statement_type *s));
74 static void exp_init_os PARAMS ((etree_type *));
75 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
76 static boolean wildcardp PARAMS ((const char *));
77 static lang_statement_union_type *wild_sort
78 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
80 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
81 static void load_symbols PARAMS ((lang_input_statement_type *entry,
82 lang_statement_list_type *));
83 static void wild PARAMS ((lang_wild_statement_type *s,
84 const char *section, const char *file,
86 lang_output_section_statement_type *output));
87 static bfd *open_output PARAMS ((const char *name));
88 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
89 static void open_input_bfds
90 PARAMS ((lang_statement_union_type *statement, boolean));
91 static void lang_reasonable_defaults PARAMS ((void));
92 static void lang_place_undefineds PARAMS ((void));
93 static void map_input_to_output_sections
94 PARAMS ((lang_statement_union_type *s,
96 lang_output_section_statement_type *output_section_statement));
97 static void print_output_section_statement
98 PARAMS ((lang_output_section_statement_type *output_section_statement));
99 static void print_assignment
100 PARAMS ((lang_assignment_statement_type *assignment,
101 lang_output_section_statement_type *output_section));
102 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
103 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
104 static void print_input_section PARAMS ((lang_input_section_type *in));
105 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
106 static void print_data_statement PARAMS ((lang_data_statement_type *data));
107 static void print_address_statement PARAMS ((lang_address_statement_type *));
108 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
109 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
110 static void print_wild_statement
111 PARAMS ((lang_wild_statement_type *w,
112 lang_output_section_statement_type *os));
113 static void print_group
114 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
115 static void print_statement PARAMS ((lang_statement_union_type *s,
116 lang_output_section_statement_type *os));
117 static void print_statement_list PARAMS ((lang_statement_union_type *s,
118 lang_output_section_statement_type *os));
119 static void print_statements PARAMS ((void));
120 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
121 fill_type fill, unsigned int power,
122 asection *output_section_statement,
124 static bfd_vma size_input_section
125 PARAMS ((lang_statement_union_type **this_ptr,
126 lang_output_section_statement_type *output_section_statement,
127 fill_type fill, bfd_vma dot, boolean relax));
128 static void lang_finish PARAMS ((void));
129 static void ignore_bfd_errors PARAMS ((const char *, ...));
130 static void lang_check PARAMS ((void));
131 static void lang_common PARAMS ((void));
132 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
133 static void lang_place_orphans PARAMS ((void));
134 static int topower PARAMS ((int));
135 static void lang_set_startof PARAMS ((void));
136 static void reset_memory_regions PARAMS ((void));
137 static void lang_record_phdrs PARAMS ((void));
138 static void lang_gc_wild
139 PARAMS ((lang_wild_statement_type *, const char *, const char *));
140 static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
141 static void lang_gc_sections PARAMS ((void));
142 static void lang_do_version_exports_section PARAMS ((void));
143 static void lang_check_section_addresses PARAMS ((void));
145 typedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
146 asection *, lang_input_statement_type *,
148 static void walk_wild_section
149 PARAMS ((lang_wild_statement_type *, const char *,
150 lang_input_statement_type *, callback_t, void *));
151 static void walk_wild_file
152 PARAMS ((lang_wild_statement_type *, const char *,
153 lang_input_statement_type *, callback_t, void *));
156 lang_output_section_statement_type *abs_output_section;
157 lang_statement_list_type *stat_ptr = &statement_list;
158 lang_statement_list_type file_chain = { 0 };
159 const char *entry_symbol = NULL;
160 boolean entry_from_cmdline;
161 boolean lang_has_input_file = false;
162 boolean had_output_filename = false;
163 boolean lang_float_flag = false;
164 boolean delete_output_file_on_failure = false;
165 struct lang_nocrossrefs *nocrossref_list;
167 etree_type *base; /* Relocation base - or null */
170 #if defined(__STDC__) || defined(ALMOST_STDC)
171 #define cat(a,b) a##b
173 #define cat(a,b) a/**/b
176 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
178 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
180 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
182 #define SECTION_NAME_MAP_LENGTH (16)
188 return obstack_alloc (&stat_obstack, size);
191 /*----------------------------------------------------------------------
192 Generic traversal routines for finding matching sections.
196 walk_wild_section (ptr, section, file, callback, data)
197 lang_wild_statement_type *ptr;
199 lang_input_statement_type *file;
203 /* Don't process sections from files which were excluded. */
204 if (ptr->exclude_filename != NULL)
208 if (wildcardp (ptr->exclude_filename))
209 match = fnmatch (ptr->exclude_filename, file->filename, 0) == 0 ? true : false;
211 match = strcmp (ptr->exclude_filename, file->filename) == 0 ? true : false;
217 if (file->just_syms_flag == false)
219 register asection *s;
225 wildcard = wildcardp (section);
227 for (s = file->the_bfd->sections; s != NULL; s = s->next)
237 name = bfd_get_section_name (file->the_bfd, s);
239 match = fnmatch (section, name, 0) == 0 ? true : false;
241 match = strcmp (section, name) == 0 ? true : false;
245 (*callback) (ptr, s, file, data);
250 /* Handle a wild statement for a single file F. */
253 walk_wild_file (s, section, f, callback, data)
254 lang_wild_statement_type *s;
256 lang_input_statement_type *f;
260 if (f->the_bfd == NULL
261 || ! bfd_check_format (f->the_bfd, bfd_archive))
262 walk_wild_section (s, section, f, callback, data);
267 /* This is an archive file. We must map each member of the
268 archive separately. */
269 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
270 while (member != NULL)
272 /* When lookup_name is called, it will call the add_symbols
273 entry point for the archive. For each element of the
274 archive which is included, BFD will call ldlang_add_file,
275 which will set the usrdata field of the member to the
276 lang_input_statement. */
277 if (member->usrdata != NULL)
279 walk_wild_section (s, section,
280 (lang_input_statement_type *) member->usrdata,
284 member = bfd_openr_next_archived_file (f->the_bfd, member);
290 walk_wild (s, section, file, callback, data)
291 lang_wild_statement_type *s;
297 lang_input_statement_type *f;
299 if (file == (char *) NULL)
301 /* Perform the iteration over all files in the list. */
302 for (f = (lang_input_statement_type *) file_chain.head;
303 f != (lang_input_statement_type *) NULL;
304 f = (lang_input_statement_type *) f->next)
306 walk_wild_file (s, section, f, callback, data);
309 else if (wildcardp (file))
311 for (f = (lang_input_statement_type *) file_chain.head;
312 f != (lang_input_statement_type *) NULL;
313 f = (lang_input_statement_type *) f->next)
315 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
316 walk_wild_file (s, section, f, callback, data);
321 /* Perform the iteration over a single file. */
322 f = lookup_name (file);
323 walk_wild_file (s, section, f, callback, data);
327 /*----------------------------------------------------------------------
328 lang_for_each_statement walks the parse tree and calls the provided
329 function for each node
333 lang_for_each_statement_worker (func, s)
334 void (*func) PARAMS ((lang_statement_union_type *));
335 lang_statement_union_type *s;
337 for (; s != (lang_statement_union_type *) NULL; s = s->next)
341 switch (s->header.type)
343 case lang_constructors_statement_enum:
344 lang_for_each_statement_worker (func, constructor_list.head);
346 case lang_output_section_statement_enum:
347 lang_for_each_statement_worker
349 s->output_section_statement.children.head);
351 case lang_wild_statement_enum:
352 lang_for_each_statement_worker
354 s->wild_statement.children.head);
356 case lang_group_statement_enum:
357 lang_for_each_statement_worker (func,
358 s->group_statement.children.head);
360 case lang_data_statement_enum:
361 case lang_reloc_statement_enum:
362 case lang_object_symbols_statement_enum:
363 case lang_output_statement_enum:
364 case lang_target_statement_enum:
365 case lang_input_section_enum:
366 case lang_input_statement_enum:
367 case lang_assignment_statement_enum:
368 case lang_padding_statement_enum:
369 case lang_address_statement_enum:
370 case lang_fill_statement_enum:
380 lang_for_each_statement (func)
381 void (*func) PARAMS ((lang_statement_union_type *));
383 lang_for_each_statement_worker (func,
384 statement_list.head);
387 /*----------------------------------------------------------------------*/
389 lang_list_init (list)
390 lang_statement_list_type *list;
392 list->head = (lang_statement_union_type *) NULL;
393 list->tail = &list->head;
396 /*----------------------------------------------------------------------
398 build a new statement node for the parse tree
403 lang_statement_union_type *
404 new_statement (type, size, list)
405 enum statement_enum type;
407 lang_statement_list_type * list;
409 lang_statement_union_type *new = (lang_statement_union_type *)
412 new->header.type = type;
413 new->header.next = (lang_statement_union_type *) NULL;
414 lang_statement_append (list, new, &new->header.next);
419 Build a new input file node for the language. There are several ways
420 in which we treat an input file, eg, we only look at symbols, or
421 prefix it with a -l etc.
423 We can be supplied with requests for input files more than once;
424 they may, for example be split over serveral lines like foo.o(.text)
425 foo.o(.data) etc, so when asked for a file we check that we havn't
426 got it already so we don't duplicate the bfd.
429 static lang_input_statement_type *
430 new_afile (name, file_type, target, add_to_list)
432 lang_input_file_enum_type file_type;
436 lang_input_statement_type *p;
439 p = new_stat (lang_input_statement, stat_ptr);
442 p = ((lang_input_statement_type *)
443 stat_alloc (sizeof (lang_input_statement_type)));
444 p->header.next = NULL;
447 lang_has_input_file = true;
451 case lang_input_file_is_symbols_only_enum:
453 p->is_archive = false;
455 p->local_sym_name = name;
456 p->just_syms_flag = true;
457 p->search_dirs_flag = false;
459 case lang_input_file_is_fake_enum:
461 p->is_archive = false;
463 p->local_sym_name = name;
464 p->just_syms_flag = false;
465 p->search_dirs_flag = false;
467 case lang_input_file_is_l_enum:
468 p->is_archive = true;
471 p->local_sym_name = concat ("-l", name, (const char *) NULL);
472 p->just_syms_flag = false;
473 p->search_dirs_flag = true;
475 case lang_input_file_is_marker_enum:
477 p->is_archive = false;
479 p->local_sym_name = name;
480 p->just_syms_flag = false;
481 p->search_dirs_flag = true;
483 case lang_input_file_is_search_file_enum:
485 p->is_archive = false;
487 p->local_sym_name = name;
488 p->just_syms_flag = false;
489 p->search_dirs_flag = true;
491 case lang_input_file_is_file_enum:
493 p->is_archive = false;
495 p->local_sym_name = name;
496 p->just_syms_flag = false;
497 p->search_dirs_flag = false;
502 p->the_bfd = (bfd *) NULL;
503 p->asymbols = (asymbol **) NULL;
504 p->next_real_file = (lang_statement_union_type *) NULL;
505 p->next = (lang_statement_union_type *) NULL;
507 p->dynamic = config.dynamic_link;
508 p->whole_archive = whole_archive;
510 lang_statement_append (&input_file_chain,
511 (lang_statement_union_type *) p,
516 lang_input_statement_type *
517 lang_add_input_file (name, file_type, target)
519 lang_input_file_enum_type file_type;
522 lang_has_input_file = true;
523 return new_afile (name, file_type, target, true);
526 /* Build enough state so that the parser can build its tree */
530 obstack_begin (&stat_obstack, 1000);
532 stat_ptr = &statement_list;
534 lang_list_init (stat_ptr);
536 lang_list_init (&input_file_chain);
537 lang_list_init (&lang_output_section_statement);
538 lang_list_init (&file_chain);
539 first_file = lang_add_input_file ((char *) NULL,
540 lang_input_file_is_marker_enum,
542 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
544 abs_output_section->bfd_section = bfd_abs_section_ptr;
548 /*----------------------------------------------------------------------
549 A region is an area of memory declared with the
550 MEMORY { name:org=exp, len=exp ... }
553 We maintain a list of all the regions here
555 If no regions are specified in the script, then the default is used
556 which is created when looked up to be the entire data space
559 static lang_memory_region_type *lang_memory_region_list;
560 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
562 lang_memory_region_type *
563 lang_memory_region_lookup (name)
564 CONST char *CONST name;
566 lang_memory_region_type *p;
568 for (p = lang_memory_region_list;
569 p != (lang_memory_region_type *) NULL;
572 if (strcmp (p->name, name) == 0)
579 /* This code used to always use the first region in the list as the
580 default region. I changed it to instead use a region
581 encompassing all of memory as the default region. This permits
582 NOLOAD sections to work reasonably without requiring a region.
583 People should specify what region they mean, if they really want
585 if (strcmp (name, "*default*") == 0)
587 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
589 return lang_memory_region_list;
595 lang_memory_region_type *new =
596 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
598 new->name = buystring (name);
599 new->next = (lang_memory_region_type *) NULL;
601 *lang_memory_region_list_tail = new;
602 lang_memory_region_list_tail = &new->next;
606 new->length = ~(bfd_size_type)0;
608 new->had_full_message = false;
615 lang_memory_region_type *
616 lang_memory_default (section)
619 lang_memory_region_type *p;
621 flagword sec_flags = section->flags;
623 /* Override SEC_DATA to mean a writable section. */
624 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
625 sec_flags |= SEC_DATA;
627 for (p = lang_memory_region_list;
628 p != (lang_memory_region_type *) NULL;
631 if ((p->flags & sec_flags) != 0
632 && (p->not_flags & sec_flags) == 0)
637 return lang_memory_region_lookup ("*default*");
640 lang_output_section_statement_type *
641 lang_output_section_find (name)
642 CONST char *CONST name;
644 lang_statement_union_type *u;
645 lang_output_section_statement_type *lookup;
647 for (u = lang_output_section_statement.head;
648 u != (lang_statement_union_type *) NULL;
651 lookup = &u->output_section_statement;
652 if (strcmp (name, lookup->name) == 0)
657 return (lang_output_section_statement_type *) NULL;
660 lang_output_section_statement_type *
661 lang_output_section_statement_lookup (name)
662 CONST char *CONST name;
664 lang_output_section_statement_type *lookup;
666 lookup = lang_output_section_find (name);
667 if (lookup == (lang_output_section_statement_type *) NULL)
670 lookup = (lang_output_section_statement_type *)
671 new_stat (lang_output_section_statement, stat_ptr);
672 lookup->region = (lang_memory_region_type *) NULL;
674 lookup->block_value = 1;
677 lookup->next = (lang_statement_union_type *) NULL;
678 lookup->bfd_section = (asection *) NULL;
679 lookup->processed = false;
680 lookup->sectype = normal_section;
681 lookup->addr_tree = (etree_type *) NULL;
682 lang_list_init (&lookup->children);
684 lookup->memspec = (CONST char *) NULL;
686 lookup->subsection_alignment = -1;
687 lookup->section_alignment = -1;
688 lookup->load_base = (union etree_union *) NULL;
689 lookup->phdrs = NULL;
691 lang_statement_append (&lang_output_section_statement,
692 (lang_statement_union_type *) lookup,
699 lang_map_flags (flag)
702 if (flag & SEC_ALLOC)
708 if (flag & SEC_READONLY)
721 lang_memory_region_type *m;
723 minfo (_("\nMemory Configuration\n\n"));
724 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
725 _("Name"), _("Origin"), _("Length"), _("Attributes"));
727 for (m = lang_memory_region_list;
728 m != (lang_memory_region_type *) NULL;
734 fprintf (config.map_file, "%-16s ", m->name);
736 sprintf_vma (buf, m->origin);
737 minfo ("0x%s ", buf);
745 minfo ("0x%V", m->length);
746 if (m->flags || m->not_flags)
754 lang_map_flags (m->flags);
760 lang_map_flags (m->not_flags);
767 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
772 /* Initialize an output section. */
776 lang_output_section_statement_type *s;
778 section_userdata_type *new;
780 if (s->bfd_section != NULL)
783 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
784 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
786 new = ((section_userdata_type *)
787 stat_alloc (sizeof (section_userdata_type)));
789 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
790 if (s->bfd_section == (asection *) NULL)
791 s->bfd_section = bfd_make_section (output_bfd, s->name);
792 if (s->bfd_section == (asection *) NULL)
794 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
795 output_bfd->xvec->name, s->name);
797 s->bfd_section->output_section = s->bfd_section;
799 /* We initialize an output sections output offset to minus its own */
800 /* vma to allow us to output a section through itself */
801 s->bfd_section->output_offset = 0;
802 get_userdata (s->bfd_section) = (PTR) new;
804 /* If there is a base address, make sure that any sections it might
805 mention are initialized. */
806 if (s->addr_tree != NULL)
807 exp_init_os (s->addr_tree);
810 /* Make sure that all output sections mentioned in an expression are
817 switch (exp->type.node_class)
820 exp_init_os (exp->assign.src);
824 exp_init_os (exp->binary.lhs);
825 exp_init_os (exp->binary.rhs);
829 exp_init_os (exp->trinary.cond);
830 exp_init_os (exp->trinary.lhs);
831 exp_init_os (exp->trinary.rhs);
835 exp_init_os (exp->unary.child);
839 switch (exp->type.node_code)
845 lang_output_section_statement_type *os;
847 os = lang_output_section_find (exp->name.name);
848 if (os != NULL && os->bfd_section == NULL)
859 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
860 once into the output. This routine checks each sections, and
861 arranges to discard it if a section of the same name has already
862 been linked. This code assumes that all relevant sections have the
863 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
864 section name. This is called via bfd_map_over_sections. */
868 section_already_linked (abfd, sec, data)
873 lang_input_statement_type *entry = (lang_input_statement_type *) data;
876 struct sec_link_once *next;
879 static struct sec_link_once *sec_link_once_list;
882 struct sec_link_once *l;
884 /* If we are only reading symbols from this object, then we want to
885 discard all sections. */
886 if (entry->just_syms_flag)
888 sec->output_section = bfd_abs_section_ptr;
889 sec->output_offset = sec->vma;
893 flags = bfd_get_section_flags (abfd, sec);
895 if ((flags & SEC_LINK_ONCE) == 0)
898 /* FIXME: When doing a relocateable link, we may have trouble
899 copying relocations in other sections that refer to local symbols
900 in the section being discarded. Those relocations will have to
901 be converted somehow; as of this writing I'm not sure that any of
902 the backends handle that correctly.
904 It is tempting to instead not discard link once sections when
905 doing a relocateable link (technically, they should be discarded
906 whenever we are building constructors). However, that fails,
907 because the linker winds up combining all the link once sections
908 into a single large link once section, which defeats the purpose
909 of having link once sections in the first place.
911 Also, not merging link once sections in a relocateable link
912 causes trouble for MIPS ELF, which relies in link once semantics
913 to handle the .reginfo section correctly. */
915 name = bfd_get_section_name (abfd, sec);
917 for (l = sec_link_once_list; l != NULL; l = l->next)
919 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
921 /* The section has already been linked. See if we should
923 switch (flags & SEC_LINK_DUPLICATES)
928 case SEC_LINK_DUPLICATES_DISCARD:
931 case SEC_LINK_DUPLICATES_ONE_ONLY:
932 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
936 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
937 /* FIXME: We should really dig out the contents of both
938 sections and memcmp them. The COFF/PE spec says that
939 the Microsoft linker does not implement this
940 correctly, so I'm not going to bother doing it
943 case SEC_LINK_DUPLICATES_SAME_SIZE:
944 if (bfd_section_size (abfd, sec)
945 != bfd_section_size (l->sec->owner, l->sec))
946 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
951 /* Set the output_section field so that wild_doit does not
952 create a lang_input_section structure for this section. */
953 sec->output_section = bfd_abs_section_ptr;
959 /* This is the first section with this name. Record it. */
961 l = (struct sec_link_once *) xmalloc (sizeof *l);
963 l->next = sec_link_once_list;
964 sec_link_once_list = l;
967 /* The wild routines.
969 These expand statements like *(.text) and foo.o to a list of
970 explicit actions, like foo.o(.text), bar.o(.text) and
971 foo.o(.text, .data). */
973 /* Return true if the PATTERN argument is a wildcard pattern.
974 Although backslashes are treated specially if a pattern contains
975 wildcards, we do not consider the mere presence of a backslash to
976 be enough to cause the the pattern to be treated as a wildcard.
977 That lets us handle DOS filenames more naturally. */
985 for (s = pattern; *s != '\0'; ++s)
993 /* Add SECTION to the output section OUTPUT. Do this by creating a
994 lang_input_section statement which is placed at PTR. FILE is the
995 input file which holds SECTION. */
998 wild_doit (ptr, section, output, file)
999 lang_statement_list_type *ptr;
1001 lang_output_section_statement_type *output;
1002 lang_input_statement_type *file;
1007 flags = bfd_get_section_flags (section->owner, section);
1011 /* If we are doing a final link, discard sections marked with
1013 if (! link_info.relocateable
1014 && (flags & SEC_EXCLUDE) != 0)
1017 /* Discard input sections which are assigned to a section named
1018 DISCARD_SECTION_NAME. */
1019 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1022 /* Discard debugging sections if we are stripping debugging
1024 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1025 && (flags & SEC_DEBUGGING) != 0)
1030 if (section->output_section == NULL)
1032 /* This prevents future calls from assigning this section. */
1033 section->output_section = bfd_abs_section_ptr;
1038 if (section->output_section == NULL)
1041 lang_input_section_type *new;
1044 if (output->bfd_section == NULL)
1052 /* Add a section reference to the list */
1053 new = new_stat (lang_input_section, ptr);
1055 new->section = section;
1057 section->output_section = output->bfd_section;
1059 flags = section->flags;
1061 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1062 to an output section, because we want to be able to include a
1063 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1064 section (I don't know why we want to do this, but we do).
1065 build_link_order in ldwrite.c handles this case by turning
1066 the embedded SEC_NEVER_LOAD section into a fill. */
1068 flags &= ~ SEC_NEVER_LOAD;
1070 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1071 already been processed. One reason to do this is that on pe
1072 format targets, .text$foo sections go into .text and it's odd
1073 to see .text with SEC_LINK_ONCE set. */
1075 if (! link_info.relocateable)
1076 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1078 /* If this is not the first input section, and the SEC_READONLY
1079 flag is not currently set, then don't set it just because the
1080 input section has it set. */
1082 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1083 flags &= ~ SEC_READONLY;
1085 section->output_section->flags |= flags;
1087 /* If SEC_READONLY is not set in the input section, then clear
1088 it from the output section. */
1089 if ((section->flags & SEC_READONLY) == 0)
1090 section->output_section->flags &= ~SEC_READONLY;
1092 switch (output->sectype)
1094 case normal_section:
1099 case overlay_section:
1100 output->bfd_section->flags &= ~SEC_ALLOC;
1102 case noload_section:
1103 output->bfd_section->flags &= ~SEC_LOAD;
1104 output->bfd_section->flags |= SEC_NEVER_LOAD;
1108 if (section->alignment_power > output->bfd_section->alignment_power)
1109 output->bfd_section->alignment_power = section->alignment_power;
1111 /* If supplied an aligment, then force it. */
1112 if (output->section_alignment != -1)
1113 output->bfd_section->alignment_power = output->section_alignment;
1117 /* Handle wildcard sorting. This returns the lang_input_section which
1118 should follow the one we are going to create for SECTION and FILE,
1119 based on the sorting requirements of WILD. It returns NULL if the
1120 new section should just go at the end of the current list. */
1122 static lang_statement_union_type *
1123 wild_sort (wild, file, section)
1124 lang_wild_statement_type *wild;
1125 lang_input_statement_type *file;
1128 const char *section_name;
1129 lang_statement_union_type *l;
1131 if (! wild->filenames_sorted && ! wild->sections_sorted)
1134 section_name = bfd_get_section_name (file->the_bfd, section);
1135 for (l = wild->children.head; l != NULL; l = l->next)
1137 lang_input_section_type *ls;
1139 if (l->header.type != lang_input_section_enum)
1141 ls = &l->input_section;
1143 /* Sorting by filename takes precedence over sorting by section
1146 if (wild->filenames_sorted)
1148 const char *fn, *ln;
1152 /* The PE support for the .idata section as generated by
1153 dlltool assumes that files will be sorted by the name of
1154 the archive and then the name of the file within the
1157 if (file->the_bfd != NULL
1158 && bfd_my_archive (file->the_bfd) != NULL)
1160 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1165 fn = file->filename;
1169 if (ls->ifile->the_bfd != NULL
1170 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1172 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1177 ln = ls->ifile->filename;
1181 i = strcmp (fn, ln);
1190 fn = file->filename;
1192 ln = ls->ifile->filename;
1194 i = strcmp (fn, ln);
1202 /* Here either the files are not sorted by name, or we are
1203 looking at the sections for this file. */
1205 if (wild->sections_sorted)
1207 if (strcmp (section_name,
1208 bfd_get_section_name (ls->ifile->the_bfd,
1218 /* Expand a wild statement for a particular FILE. SECTION may be
1219 NULL, in which case it is a wild card. */
1222 output_section_callback (ptr, section, file, output)
1223 lang_wild_statement_type *ptr;
1225 lang_input_statement_type *file;
1228 lang_statement_union_type *before;
1230 /* If the wild pattern was marked KEEP, the member sections
1231 should be as well. */
1232 if (ptr->keep_sections)
1233 section->flags |= SEC_KEEP;
1235 before = wild_sort (ptr, file, section);
1237 /* Here BEFORE points to the lang_input_section which
1238 should follow the one we are about to add. If BEFORE
1239 is NULL, then the section should just go at the end
1240 of the current list. */
1243 wild_doit (&ptr->children, section,
1244 (lang_output_section_statement_type *) output,
1248 lang_statement_list_type list;
1249 lang_statement_union_type **pp;
1251 lang_list_init (&list);
1252 wild_doit (&list, section,
1253 (lang_output_section_statement_type *) output,
1256 /* If we are discarding the section, LIST.HEAD will
1258 if (list.head != NULL)
1260 ASSERT (list.head->next == NULL);
1262 for (pp = &ptr->children.head;
1265 ASSERT (*pp != NULL);
1267 list.head->next = *pp;
1273 /* This is passed a file name which must have been seen already and
1274 added to the statement tree. We will see if it has been opened
1275 already and had its symbols read. If not then we'll read it. */
1277 static lang_input_statement_type *
1281 lang_input_statement_type *search;
1283 for (search = (lang_input_statement_type *) input_file_chain.head;
1284 search != (lang_input_statement_type *) NULL;
1285 search = (lang_input_statement_type *) search->next_real_file)
1287 if (search->filename == (char *) NULL && name == (char *) NULL)
1289 if (search->filename != (char *) NULL
1290 && name != (char *) NULL
1291 && strcmp (search->filename, name) == 0)
1295 if (search == (lang_input_statement_type *) NULL)
1296 search = new_afile (name, lang_input_file_is_file_enum, default_target,
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 == (const char *) NULL)
1307 load_symbols (search, (lang_statement_list_type *) NULL);
1312 /* Get the symbols for an input file. */
1315 load_symbols (entry, place)
1316 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;
1332 err = bfd_get_error ();
1333 if (err == bfd_error_file_ambiguously_recognized)
1337 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1338 einfo (_("%B: matching formats:"), entry->the_bfd);
1339 for (p = matching; *p != NULL; p++)
1343 else if (err != bfd_error_file_not_recognized
1345 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1347 bfd_close (entry->the_bfd);
1348 entry->the_bfd = NULL;
1350 /* See if the emulation has some special knowledge. */
1352 if (ldemul_unrecognized_file (entry))
1355 /* Try to interpret the file as a linker script. */
1357 ldfile_open_command_file (entry->filename);
1362 ldfile_assumed_script = true;
1363 parser_input = input_script;
1365 ldfile_assumed_script = false;
1372 if (ldemul_recognized_file (entry))
1375 /* We don't call ldlang_add_file for an archive. Instead, the
1376 add_symbols entry point will call ldlang_add_file, via the
1377 add_archive_element callback, for each element of the archive
1379 switch (bfd_get_format (entry->the_bfd))
1385 ldlang_add_file (entry);
1386 if (trace_files || trace_file_tries)
1387 info_msg ("%I\n", entry);
1391 if (entry->whole_archive)
1393 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1395 while (member != NULL)
1397 if (! bfd_check_format (member, bfd_object))
1398 einfo (_("%F%B: object %B in archive is not object\n"),
1399 entry->the_bfd, member);
1400 if (! ((*link_info.callbacks->add_archive_element)
1401 (&link_info, member, "--whole-archive")))
1403 if (! bfd_link_add_symbols (member, &link_info))
1404 einfo (_("%F%B: could not read symbols: %E\n"), member);
1405 member = bfd_openr_next_archived_file (entry->the_bfd,
1409 entry->loaded = true;
1415 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1416 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1418 entry->loaded = true;
1423 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1424 indicating that it is a wildcard. Separate lang_input_section
1425 statements are created for each part of the expansion; they are
1426 added after the wild statement S. OUTPUT is the output section. */
1429 wild (s, section, file, target, output)
1430 lang_wild_statement_type *s;
1431 const char *section;
1434 lang_output_section_statement_type *output;
1436 walk_wild (s, section, file, output_section_callback, (void *) output);
1438 if (section != (char *) NULL
1439 && strcmp (section, "COMMON") == 0
1440 && default_common_section == NULL)
1442 /* Remember the section that common is going to in case we later
1443 get something which doesn't know where to put it. */
1444 default_common_section = output;
1448 /* Open the output file. */
1456 if (output_target == (char *) NULL)
1458 if (current_target != (char *) NULL)
1459 output_target = current_target;
1461 output_target = default_target;
1463 output = bfd_openw (name, output_target);
1465 if (output == (bfd *) NULL)
1467 if (bfd_get_error () == bfd_error_invalid_target)
1469 einfo (_("%P%F: target %s not found\n"), output_target);
1471 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1474 delete_output_file_on_failure = true;
1476 /* output->flags |= D_PAGED;*/
1478 if (! bfd_set_format (output, bfd_object))
1479 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1480 if (! bfd_set_arch_mach (output,
1481 ldfile_output_architecture,
1482 ldfile_output_machine))
1483 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1485 link_info.hash = bfd_link_hash_table_create (output);
1486 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1487 einfo (_("%P%F: can not create link hash table: %E\n"));
1489 bfd_set_gp_size (output, g_switch_value);
1497 ldlang_open_output (statement)
1498 lang_statement_union_type * statement;
1500 switch (statement->header.type)
1502 case lang_output_statement_enum:
1503 ASSERT (output_bfd == (bfd *) NULL);
1504 output_bfd = open_output (statement->output_statement.name);
1505 ldemul_set_output_arch ();
1506 if (config.magic_demand_paged && !link_info.relocateable)
1507 output_bfd->flags |= D_PAGED;
1509 output_bfd->flags &= ~D_PAGED;
1510 if (config.text_read_only)
1511 output_bfd->flags |= WP_TEXT;
1513 output_bfd->flags &= ~WP_TEXT;
1514 if (link_info.traditional_format)
1515 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1517 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1520 case lang_target_statement_enum:
1521 current_target = statement->target_statement.target;
1528 /* Open all the input files. */
1531 open_input_bfds (s, force)
1532 lang_statement_union_type *s;
1535 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1537 switch (s->header.type)
1539 case lang_constructors_statement_enum:
1540 open_input_bfds (constructor_list.head, force);
1542 case lang_output_section_statement_enum:
1543 open_input_bfds (s->output_section_statement.children.head, force);
1545 case lang_wild_statement_enum:
1546 /* Maybe we should load the file's symbols */
1547 if (s->wild_statement.filename
1548 && ! wildcardp (s->wild_statement.filename))
1549 (void) lookup_name (s->wild_statement.filename);
1550 open_input_bfds (s->wild_statement.children.head, force);
1552 case lang_group_statement_enum:
1554 struct bfd_link_hash_entry *undefs;
1556 /* We must continually search the entries in the group
1557 until no new symbols are added to the list of undefined
1562 undefs = link_info.hash->undefs_tail;
1563 open_input_bfds (s->group_statement.children.head, true);
1565 while (undefs != link_info.hash->undefs_tail);
1568 case lang_target_statement_enum:
1569 current_target = s->target_statement.target;
1571 case lang_input_statement_enum:
1572 if (s->input_statement.real == true)
1574 lang_statement_list_type add;
1576 s->input_statement.target = current_target;
1578 /* If we are being called from within a group, and this
1579 is an archive which has already been searched, then
1580 force it to be researched. */
1582 && s->input_statement.loaded
1583 && bfd_check_format (s->input_statement.the_bfd,
1585 s->input_statement.loaded = false;
1587 lang_list_init (&add);
1589 load_symbols (&s->input_statement, &add);
1591 if (add.head != NULL)
1593 *add.tail = s->next;
1604 /* If there are [COMMONS] statements, put a wild one into the bss section */
1607 lang_reasonable_defaults ()
1610 lang_output_section_statement_lookup (".text");
1611 lang_output_section_statement_lookup (".data");
1613 default_common_section =
1614 lang_output_section_statement_lookup (".bss");
1617 if (placed_commons == false)
1619 lang_wild_statement_type *new =
1620 new_stat (lang_wild_statement,
1621 &default_common_section->children);
1623 new->section_name = "COMMON";
1624 new->filename = (char *) NULL;
1625 lang_list_init (&new->children);
1632 Add the supplied name to the symbol table as an undefined reference.
1633 Remove items from the chain as we open input bfds
1635 typedef struct ldlang_undef_chain_list
1637 struct ldlang_undef_chain_list *next;
1639 } ldlang_undef_chain_list_type;
1641 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1644 ldlang_add_undef (name)
1645 CONST char *CONST name;
1647 ldlang_undef_chain_list_type *new =
1648 ((ldlang_undef_chain_list_type *)
1649 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1651 new->next = ldlang_undef_chain_list_head;
1652 ldlang_undef_chain_list_head = new;
1654 new->name = buystring (name);
1657 /* Run through the list of undefineds created above and place them
1658 into the linker hash table as undefined symbols belonging to the
1662 lang_place_undefineds ()
1664 ldlang_undef_chain_list_type *ptr;
1666 for (ptr = ldlang_undef_chain_list_head;
1667 ptr != (ldlang_undef_chain_list_type *) NULL;
1670 struct bfd_link_hash_entry *h;
1672 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1673 if (h == (struct bfd_link_hash_entry *) NULL)
1674 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1675 if (h->type == bfd_link_hash_new)
1677 h->type = bfd_link_hash_undefined;
1678 h->u.undef.abfd = NULL;
1679 bfd_link_add_undef (link_info.hash, h);
1684 /* Open input files and attatch to output sections */
1686 map_input_to_output_sections (s, target, output_section_statement)
1687 lang_statement_union_type * s;
1689 lang_output_section_statement_type * output_section_statement;
1691 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1693 switch (s->header.type)
1697 case lang_wild_statement_enum:
1698 wild (&s->wild_statement, s->wild_statement.section_name,
1699 s->wild_statement.filename, target,
1700 output_section_statement);
1703 case lang_constructors_statement_enum:
1704 map_input_to_output_sections (constructor_list.head,
1706 output_section_statement);
1708 case lang_output_section_statement_enum:
1709 map_input_to_output_sections (s->output_section_statement.children.head,
1711 &s->output_section_statement);
1713 case lang_output_statement_enum:
1715 case lang_target_statement_enum:
1716 target = s->target_statement.target;
1718 case lang_group_statement_enum:
1719 map_input_to_output_sections (s->group_statement.children.head,
1721 output_section_statement);
1723 case lang_fill_statement_enum:
1724 case lang_input_section_enum:
1725 case lang_object_symbols_statement_enum:
1726 case lang_data_statement_enum:
1727 case lang_reloc_statement_enum:
1728 case lang_padding_statement_enum:
1729 case lang_input_statement_enum:
1730 if (output_section_statement != NULL
1731 && output_section_statement->bfd_section == NULL)
1732 init_os (output_section_statement);
1734 case lang_assignment_statement_enum:
1735 if (output_section_statement != NULL
1736 && output_section_statement->bfd_section == NULL)
1737 init_os (output_section_statement);
1739 /* Make sure that any sections mentioned in the assignment
1741 exp_init_os (s->assignment_statement.exp);
1743 case lang_afile_asection_pair_statement_enum:
1746 case lang_address_statement_enum:
1747 /* Mark the specified section with the supplied address */
1749 lang_output_section_statement_type *os =
1750 lang_output_section_statement_lookup
1751 (s->address_statement.section_name);
1753 if (os->bfd_section == NULL)
1755 os->addr_tree = s->address_statement.address;
1763 print_output_section_statement (output_section_statement)
1764 lang_output_section_statement_type * output_section_statement;
1766 asection *section = output_section_statement->bfd_section;
1769 if (output_section_statement != abs_output_section)
1771 minfo ("\n%s", output_section_statement->name);
1773 if (section != NULL)
1775 print_dot = section->vma;
1777 len = strlen (output_section_statement->name);
1778 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1783 while (len < SECTION_NAME_MAP_LENGTH)
1789 minfo ("0x%V %W", section->vma, section->_raw_size);
1791 if (output_section_statement->load_base != NULL)
1795 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1796 "load base", lang_final_phase_enum);
1797 minfo (_(" load address 0x%V"), addr);
1804 print_statement_list (output_section_statement->children.head,
1805 output_section_statement);
1809 print_assignment (assignment, output_section)
1810 lang_assignment_statement_type * assignment;
1811 lang_output_section_statement_type * output_section;
1814 etree_value_type result;
1816 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1819 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1820 lang_final_phase_enum, print_dot, &print_dot);
1822 minfo ("0x%V", result.value + result.section->bfd_section->vma);
1833 exp_print_tree (assignment->exp);
1839 print_input_statement (statm)
1840 lang_input_statement_type * statm;
1842 if (statm->filename != (char *) NULL)
1844 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1848 /* Print all symbols defined in a particular section. This is called
1849 via bfd_link_hash_traverse. */
1852 print_one_symbol (hash_entry, ptr)
1853 struct bfd_link_hash_entry *hash_entry;
1856 asection *sec = (asection *) ptr;
1858 if ((hash_entry->type == bfd_link_hash_defined
1859 || hash_entry->type == bfd_link_hash_defweak)
1860 && sec == hash_entry->u.def.section)
1864 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1867 (hash_entry->u.def.value
1868 + hash_entry->u.def.section->output_offset
1869 + hash_entry->u.def.section->output_section->vma));
1871 minfo (" %T\n", hash_entry->root.string);
1877 /* Print information about an input section to the map file. */
1880 print_input_section (in)
1881 lang_input_section_type * in;
1883 asection *i = in->section;
1884 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1890 minfo ("%s", i->name);
1892 if (i->output_section != NULL)
1896 len = 1 + strlen (i->name);
1897 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1902 while (len < SECTION_NAME_MAP_LENGTH)
1908 minfo ("0x%V %W %B\n",
1909 i->output_section->vma + i->output_offset, size,
1912 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1914 len = SECTION_NAME_MAP_LENGTH + 3;
1926 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
1929 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1931 print_dot = i->output_section->vma + i->output_offset + size;
1937 print_fill_statement (fill)
1938 lang_fill_statement_type * fill;
1940 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1944 print_data_statement (data)
1945 lang_data_statement_type * data;
1952 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1955 addr = data->output_vma;
1956 if (data->output_section != NULL)
1957 addr += data->output_section->vma;
1985 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1987 if (data->exp->type.node_class != etree_value)
1990 exp_print_tree (data->exp);
1995 print_dot = addr + size;
1998 /* Print an address statement. These are generated by options like
2002 print_address_statement (address)
2003 lang_address_statement_type *address;
2005 minfo (_("Address of section %s set to "), address->section_name);
2006 exp_print_tree (address->address);
2010 /* Print a reloc statement. */
2013 print_reloc_statement (reloc)
2014 lang_reloc_statement_type *reloc;
2020 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2023 addr = reloc->output_vma;
2024 if (reloc->output_section != NULL)
2025 addr += reloc->output_section->vma;
2027 size = bfd_get_reloc_size (reloc->howto);
2029 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2031 if (reloc->name != NULL)
2032 minfo ("%s+", reloc->name);
2034 minfo ("%s+", reloc->section->name);
2036 exp_print_tree (reloc->addend_exp);
2040 print_dot = addr + size;
2044 print_padding_statement (s)
2045 lang_padding_statement_type *s;
2052 len = sizeof " *fill*" - 1;
2053 while (len < SECTION_NAME_MAP_LENGTH)
2059 addr = s->output_offset;
2060 if (s->output_section != NULL)
2061 addr += s->output_section->vma;
2062 minfo ("0x%V %W", addr, s->size);
2065 minfo (" %u", s->fill);
2069 print_dot = addr + s->size;
2073 print_wild_statement (w, os)
2074 lang_wild_statement_type * w;
2075 lang_output_section_statement_type * os;
2079 if (w->filenames_sorted)
2081 if (w->exclude_filename != NULL)
2082 minfo ("EXCLUDE_FILE ( %s )", w->exclude_filename);
2083 if (w->filename != NULL)
2084 minfo ("%s", w->filename);
2087 if (w->filenames_sorted)
2091 if (w->sections_sorted)
2093 if (w->section_name != NULL)
2094 minfo ("%s", w->section_name);
2097 if (w->sections_sorted)
2103 print_statement_list (w->children.head, os);
2106 /* Print a group statement. */
2110 lang_group_statement_type *s;
2111 lang_output_section_statement_type *os;
2113 fprintf (config.map_file, "START GROUP\n");
2114 print_statement_list (s->children.head, os);
2115 fprintf (config.map_file, "END GROUP\n");
2118 /* Print the list of statements in S.
2119 This can be called for any statement type. */
2122 print_statement_list (s, os)
2123 lang_statement_union_type *s;
2124 lang_output_section_statement_type *os;
2128 print_statement (s, os);
2133 /* Print the first statement in statement list S.
2134 This can be called for any statement type. */
2137 print_statement (s, os)
2138 lang_statement_union_type *s;
2139 lang_output_section_statement_type *os;
2141 switch (s->header.type)
2144 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2147 case lang_constructors_statement_enum:
2148 if (constructor_list.head != NULL)
2150 if (constructors_sorted)
2151 minfo (" SORT (CONSTRUCTORS)\n");
2153 minfo (" CONSTRUCTORS\n");
2154 print_statement_list (constructor_list.head, os);
2157 case lang_wild_statement_enum:
2158 print_wild_statement (&s->wild_statement, os);
2160 case lang_address_statement_enum:
2161 print_address_statement (&s->address_statement);
2163 case lang_object_symbols_statement_enum:
2164 minfo (" CREATE_OBJECT_SYMBOLS\n");
2166 case lang_fill_statement_enum:
2167 print_fill_statement (&s->fill_statement);
2169 case lang_data_statement_enum:
2170 print_data_statement (&s->data_statement);
2172 case lang_reloc_statement_enum:
2173 print_reloc_statement (&s->reloc_statement);
2175 case lang_input_section_enum:
2176 print_input_section (&s->input_section);
2178 case lang_padding_statement_enum:
2179 print_padding_statement (&s->padding_statement);
2181 case lang_output_section_statement_enum:
2182 print_output_section_statement (&s->output_section_statement);
2184 case lang_assignment_statement_enum:
2185 print_assignment (&s->assignment_statement, os);
2187 case lang_target_statement_enum:
2188 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2190 case lang_output_statement_enum:
2191 minfo ("OUTPUT(%s", s->output_statement.name);
2192 if (output_target != NULL)
2193 minfo (" %s", output_target);
2196 case lang_input_statement_enum:
2197 print_input_statement (&s->input_statement);
2199 case lang_group_statement_enum:
2200 print_group (&s->group_statement, os);
2202 case lang_afile_asection_pair_statement_enum:
2211 print_statement_list (statement_list.head, abs_output_section);
2214 /* Print the first N statements in statement list S to STDERR.
2215 If N == 0, nothing is printed.
2216 If N < 0, the entire list is printed.
2217 Intended to be called from GDB. */
2220 dprint_statement (s, n)
2221 lang_statement_union_type * s;
2224 FILE *map_save = config.map_file;
2226 config.map_file = stderr;
2229 print_statement_list (s, abs_output_section);
2232 while (s && --n >= 0)
2234 print_statement (s, abs_output_section);
2239 config.map_file = map_save;
2243 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2244 lang_statement_union_type ** this_ptr;
2247 asection * output_section_statement;
2250 /* Align this section first to the
2251 input sections requirement, then
2252 to the output section's requirement.
2253 If this alignment is > than any seen before,
2254 then record it too. Perform the alignment by
2255 inserting a magic 'padding' statement.
2258 unsigned int alignment_needed = align_power (dot, power) - dot;
2260 if (alignment_needed != 0)
2262 lang_statement_union_type *new =
2263 ((lang_statement_union_type *)
2264 stat_alloc (sizeof (lang_padding_statement_type)));
2266 /* Link into existing chain */
2267 new->header.next = *this_ptr;
2269 new->header.type = lang_padding_statement_enum;
2270 new->padding_statement.output_section = output_section_statement;
2271 new->padding_statement.output_offset =
2272 dot - output_section_statement->vma;
2273 new->padding_statement.fill = fill;
2274 new->padding_statement.size = alignment_needed;
2278 /* Remember the most restrictive alignment */
2279 if (power > output_section_statement->alignment_power)
2281 output_section_statement->alignment_power = power;
2283 output_section_statement->_raw_size += alignment_needed;
2284 return alignment_needed + dot;
2288 /* Work out how much this section will move the dot point */
2290 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2291 lang_statement_union_type ** this_ptr;
2292 lang_output_section_statement_type * output_section_statement;
2297 lang_input_section_type *is = &((*this_ptr)->input_section);
2298 asection *i = is->section;
2300 if (is->ifile->just_syms_flag == false)
2302 if (output_section_statement->subsection_alignment != -1)
2303 i->alignment_power =
2304 output_section_statement->subsection_alignment;
2306 dot = insert_pad (this_ptr, fill, i->alignment_power,
2307 output_section_statement->bfd_section, dot);
2309 /* Remember where in the output section this input section goes */
2311 i->output_offset = dot - output_section_statement->bfd_section->vma;
2313 /* Mark how big the output section must be to contain this now
2315 if (i->_cooked_size != 0)
2316 dot += i->_cooked_size;
2318 dot += i->_raw_size;
2319 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2323 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2329 /* Check to see if any allocated sections overlap with other allocated
2330 sections. This can happen when the linker script specifically specifies
2331 the output section addresses of the two sections. */
2333 lang_check_section_addresses ()
2337 /* Scan all sections in the output list. */
2338 for (s = output_bfd->sections; s != NULL; s = s->next)
2339 /* Ignore sections which are not loaded or which have no contents. */
2340 if ((bfd_get_section_flags (output_bfd, s) & (SEC_ALLOC | SEC_LOAD))
2341 && bfd_section_size (output_bfd, s) != 0)
2345 /* Once we reach section 's' stop our seach. This prevents two
2346 warning messages from being produced, one for 'section A overlaps
2347 section B' and one for 'section B overlaps section A'. */
2348 for (os = output_bfd->sections; os != s; os = os->next)
2355 /* Only consider loadable sections with real contents. */
2356 if (((bfd_get_section_flags (output_bfd, os)
2357 & (SEC_ALLOC | SEC_LOAD)) == 0)
2358 || bfd_section_size (output_bfd, os) == 0)
2361 /* We must check the sections' LMA addresses not their
2362 VMA addresses because overlay sections can have
2363 overlapping VMAs but they must have distinct LMAs. */
2364 s_start = bfd_section_lma (output_bfd, s);
2365 os_start = bfd_section_lma (output_bfd, os);
2366 s_end = s_start + bfd_section_size (output_bfd, s) - 1;
2367 os_end = os_start + bfd_section_size (output_bfd, os) - 1;
2369 /* Look for an overlap. */
2370 if ((s_end < os_start) || (s_start > os_end))
2374 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2375 s->name, s_start, s_end, os->name, os_start, os_end);
2377 /* Once we have found one overlap for this section,
2378 stop looking for others. */
2384 /* This variable indicates whether bfd_relax_section should be called
2387 static boolean relax_again;
2389 /* Set the sizes for all the output sections. */
2392 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2393 lang_statement_union_type * s;
2394 lang_output_section_statement_type * output_section_statement;
2395 lang_statement_union_type ** prev;
2400 /* Size up the sections from their constituent parts. */
2401 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2403 switch (s->header.type)
2405 case lang_output_section_statement_enum:
2408 lang_output_section_statement_type *os = &s->output_section_statement;
2410 if (os->bfd_section == NULL)
2411 /* This section was never actually created. */
2414 /* If this is a COFF shared library section, use the size and
2415 address from the input section. FIXME: This is COFF
2416 specific; it would be cleaner if there were some other way
2417 to do this, but nothing simple comes to mind. */
2418 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2422 if (os->children.head == NULL
2423 || os->children.head->next != NULL
2424 || os->children.head->header.type != lang_input_section_enum)
2425 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2428 input = os->children.head->input_section.section;
2429 bfd_set_section_vma (os->bfd_section->owner,
2431 bfd_section_vma (input->owner, input));
2432 os->bfd_section->_raw_size = input->_raw_size;
2436 if (bfd_is_abs_section (os->bfd_section))
2438 /* No matter what happens, an abs section starts at zero. */
2439 ASSERT (os->bfd_section->vma == 0);
2443 if (os->addr_tree == (etree_type *) NULL)
2445 /* No address specified for this section, get one
2446 from the region specification. */
2447 if (os->region == (lang_memory_region_type *) NULL
2448 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2449 & (SEC_ALLOC | SEC_LOAD)) != 0)
2450 && os->region->name[0] == '*'
2451 && strcmp (os->region->name, "*default*") == 0))
2453 os->region = lang_memory_default (os->bfd_section);
2456 /* If a loadable section is using the default memory
2457 region, and some non default memory regions were
2458 defined, issue a warning. */
2459 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2460 & (SEC_ALLOC | SEC_LOAD)) != 0
2461 && ! link_info.relocateable
2462 && strcmp (os->region->name, "*default*") == 0
2463 && lang_memory_region_list != NULL
2464 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2465 || lang_memory_region_list->next != NULL))
2466 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2467 bfd_get_section_name (output_bfd, os->bfd_section));
2469 dot = os->region->current;
2471 if (os->section_alignment == -1)
2476 dot = align_power (dot, os->bfd_section->alignment_power);
2478 if (dot != olddot && config.warn_section_align)
2479 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2480 os->name, (unsigned int) (dot - olddot));
2487 r = exp_fold_tree (os->addr_tree,
2489 lang_allocating_phase_enum,
2491 if (r.valid_p == false)
2493 einfo (_("%F%S: non constant address expression for section %s\n"),
2496 dot = r.value + r.section->bfd_section->vma;
2499 /* The section starts here.
2500 First, align to what the section needs. */
2502 if (os->section_alignment != -1)
2503 dot = align_power (dot, os->section_alignment);
2505 bfd_set_section_vma (0, os->bfd_section, dot);
2507 os->bfd_section->output_offset = 0;
2510 (void) lang_size_sections (os->children.head, os, &os->children.head,
2511 os->fill, dot, relax);
2513 /* Ignore the size of the input sections, use the vma and size to
2516 after = ALIGN_N (os->bfd_section->vma +
2517 os->bfd_section->_raw_size,
2518 /* The coercion here is important, see ld.h. */
2519 (bfd_vma) os->block_value);
2521 if (bfd_is_abs_section (os->bfd_section))
2522 ASSERT (after == os->bfd_section->vma);
2524 os->bfd_section->_raw_size = after - os->bfd_section->vma;
2525 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2526 os->processed = true;
2528 /* Update dot in the region ?
2529 We only do this if the section is going to be allocated,
2530 since unallocated sections do not contribute to the region's
2531 overall size in memory. */
2532 if (os->region != (lang_memory_region_type *) NULL
2533 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2534 & (SEC_ALLOC | SEC_LOAD)))
2536 os->region->current = dot;
2538 /* Make sure this isn't silly. */
2539 if (os->region->current < os->region->origin
2540 || (os->region->current - os->region->origin
2541 > os->region->length))
2543 if (os->addr_tree != (etree_type *) NULL)
2545 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2546 os->region->current,
2547 os->bfd_section->owner,
2548 os->bfd_section->name,
2553 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2555 os->bfd_section->owner,
2556 os->bfd_section->name);
2558 /* Reset the region pointer. */
2559 os->region->current = os->region->origin;
2565 case lang_constructors_statement_enum:
2566 dot = lang_size_sections (constructor_list.head,
2567 output_section_statement,
2568 &s->wild_statement.children.head,
2573 case lang_data_statement_enum:
2575 unsigned int size = 0;
2577 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2578 s->data_statement.output_section =
2579 output_section_statement->bfd_section;
2581 switch (s->data_statement.type)
2599 output_section_statement->bfd_section->_raw_size += size;
2600 /* The output section gets contents, and then we inspect for
2601 any flags set in the input script which override any ALLOC. */
2602 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2603 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2604 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2609 case lang_reloc_statement_enum:
2613 s->reloc_statement.output_vma =
2614 dot - output_section_statement->bfd_section->vma;
2615 s->reloc_statement.output_section =
2616 output_section_statement->bfd_section;
2617 size = bfd_get_reloc_size (s->reloc_statement.howto);
2619 output_section_statement->bfd_section->_raw_size += size;
2623 case lang_wild_statement_enum:
2625 dot = lang_size_sections (s->wild_statement.children.head,
2626 output_section_statement,
2627 &s->wild_statement.children.head,
2633 case lang_object_symbols_statement_enum:
2634 link_info.create_object_symbols_section =
2635 output_section_statement->bfd_section;
2637 case lang_output_statement_enum:
2638 case lang_target_statement_enum:
2640 case lang_input_section_enum:
2644 i = (*prev)->input_section.section;
2647 if (i->_cooked_size == 0)
2648 i->_cooked_size = i->_raw_size;
2654 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2655 einfo (_("%P%F: can't relax section: %E\n"));
2659 dot = size_input_section (prev,
2660 output_section_statement,
2661 output_section_statement->fill,
2665 case lang_input_statement_enum:
2667 case lang_fill_statement_enum:
2668 s->fill_statement.output_section = output_section_statement->bfd_section;
2670 fill = s->fill_statement.fill;
2672 case lang_assignment_statement_enum:
2674 bfd_vma newdot = dot;
2676 exp_fold_tree (s->assignment_statement.exp,
2677 output_section_statement,
2678 lang_allocating_phase_enum,
2684 /* The assignment changed dot. Insert a pad. */
2685 if (output_section_statement == abs_output_section)
2687 /* If we don't have an output section, then just adjust
2688 the default memory address. */
2689 lang_memory_region_lookup ("*default*")->current = newdot;
2693 lang_statement_union_type *new =
2694 ((lang_statement_union_type *)
2695 stat_alloc (sizeof (lang_padding_statement_type)));
2697 /* Link into existing chain. */
2698 new->header.next = *prev;
2700 new->header.type = lang_padding_statement_enum;
2701 new->padding_statement.output_section =
2702 output_section_statement->bfd_section;
2703 new->padding_statement.output_offset =
2704 dot - output_section_statement->bfd_section->vma;
2705 new->padding_statement.fill = fill;
2706 new->padding_statement.size = newdot - dot;
2707 output_section_statement->bfd_section->_raw_size +=
2708 new->padding_statement.size;
2716 case lang_padding_statement_enum:
2717 /* If we are relaxing, and this is not the first pass, some
2718 padding statements may have been inserted during previous
2719 passes. We may have to move the padding statement to a new
2720 location if dot has a different value at this point in this
2721 pass than it did at this point in the previous pass. */
2722 s->padding_statement.output_offset =
2723 dot - output_section_statement->bfd_section->vma;
2724 dot += s->padding_statement.size;
2725 output_section_statement->bfd_section->_raw_size +=
2726 s->padding_statement.size;
2729 case lang_group_statement_enum:
2730 dot = lang_size_sections (s->group_statement.children.head,
2731 output_section_statement,
2732 &s->group_statement.children.head,
2740 /* This can only get here when relaxing is turned on. */
2742 case lang_address_statement_enum:
2745 prev = &s->header.next;
2751 lang_do_assignments (s, output_section_statement, fill, dot)
2752 lang_statement_union_type * s;
2753 lang_output_section_statement_type * output_section_statement;
2757 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2759 switch (s->header.type)
2761 case lang_constructors_statement_enum:
2762 dot = lang_do_assignments (constructor_list.head,
2763 output_section_statement,
2768 case lang_output_section_statement_enum:
2770 lang_output_section_statement_type *os =
2771 &(s->output_section_statement);
2773 if (os->bfd_section != NULL)
2775 dot = os->bfd_section->vma;
2776 (void) lang_do_assignments (os->children.head, os,
2778 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2782 /* If nothing has been placed into the output section then
2783 it won't have a bfd_section. */
2784 if (os->bfd_section)
2786 os->bfd_section->lma
2787 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2792 case lang_wild_statement_enum:
2794 dot = lang_do_assignments (s->wild_statement.children.head,
2795 output_section_statement,
2800 case lang_object_symbols_statement_enum:
2801 case lang_output_statement_enum:
2802 case lang_target_statement_enum:
2804 case lang_common_statement_enum:
2807 case lang_data_statement_enum:
2809 etree_value_type value;
2811 value = exp_fold_tree (s->data_statement.exp,
2813 lang_final_phase_enum, dot, &dot);
2814 s->data_statement.value = value.value;
2815 if (value.valid_p == false)
2816 einfo (_("%F%P: invalid data statement\n"));
2818 switch (s->data_statement.type)
2836 case lang_reloc_statement_enum:
2838 etree_value_type value;
2840 value = exp_fold_tree (s->reloc_statement.addend_exp,
2842 lang_final_phase_enum, dot, &dot);
2843 s->reloc_statement.addend_value = value.value;
2844 if (value.valid_p == false)
2845 einfo (_("%F%P: invalid reloc statement\n"));
2847 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2850 case lang_input_section_enum:
2852 asection *in = s->input_section.section;
2854 if (in->_cooked_size != 0)
2855 dot += in->_cooked_size;
2857 dot += in->_raw_size;
2861 case lang_input_statement_enum:
2863 case lang_fill_statement_enum:
2864 fill = s->fill_statement.fill;
2866 case lang_assignment_statement_enum:
2868 exp_fold_tree (s->assignment_statement.exp,
2869 output_section_statement,
2870 lang_final_phase_enum,
2876 case lang_padding_statement_enum:
2877 dot += s->padding_statement.size;
2880 case lang_group_statement_enum:
2881 dot = lang_do_assignments (s->group_statement.children.head,
2882 output_section_statement,
2890 case lang_address_statement_enum:
2898 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2899 operator .startof. (section_name), it produces an undefined symbol
2900 .startof.section_name. Similarly, when it sees
2901 .sizeof. (section_name), it produces an undefined symbol
2902 .sizeof.section_name. For all the output sections, we look for
2903 such symbols, and set them to the correct value. */
2910 if (link_info.relocateable)
2913 for (s = output_bfd->sections; s != NULL; s = s->next)
2915 const char *secname;
2917 struct bfd_link_hash_entry *h;
2919 secname = bfd_get_section_name (output_bfd, s);
2920 buf = xmalloc (10 + strlen (secname));
2922 sprintf (buf, ".startof.%s", secname);
2923 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2924 if (h != NULL && h->type == bfd_link_hash_undefined)
2926 h->type = bfd_link_hash_defined;
2927 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2928 h->u.def.section = bfd_abs_section_ptr;
2931 sprintf (buf, ".sizeof.%s", secname);
2932 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2933 if (h != NULL && h->type == bfd_link_hash_undefined)
2935 h->type = bfd_link_hash_defined;
2936 if (s->_cooked_size != 0)
2937 h->u.def.value = s->_cooked_size;
2939 h->u.def.value = s->_raw_size;
2940 h->u.def.section = bfd_abs_section_ptr;
2950 struct bfd_link_hash_entry *h;
2953 if (link_info.relocateable || link_info.shared)
2958 if (entry_symbol == (char *) NULL)
2960 /* No entry has been specified. Look for start, but don't warn
2961 if we don't find it. */
2962 entry_symbol = "start";
2966 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2967 if (h != (struct bfd_link_hash_entry *) NULL
2968 && (h->type == bfd_link_hash_defined
2969 || h->type == bfd_link_hash_defweak)
2970 && h->u.def.section->output_section != NULL)
2974 val = (h->u.def.value
2975 + bfd_get_section_vma (output_bfd,
2976 h->u.def.section->output_section)
2977 + h->u.def.section->output_offset);
2978 if (! bfd_set_start_address (output_bfd, val))
2979 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
2986 /* We couldn't find the entry symbol. Try parsing it as a
2988 val = bfd_scan_vma (entry_symbol, &send, 0);
2991 if (! bfd_set_start_address (output_bfd, val))
2992 einfo (_("%P%F: can't set start address\n"));
2998 /* Can't find the entry symbol, and it's not a number. Use
2999 the first address in the text section. */
3000 ts = bfd_get_section_by_name (output_bfd, ".text");
3001 if (ts != (asection *) NULL)
3004 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3005 entry_symbol, bfd_get_section_vma (output_bfd, ts));
3006 if (! bfd_set_start_address (output_bfd,
3007 bfd_get_section_vma (output_bfd,
3009 einfo (_("%P%F: can't set start address\n"));
3014 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3021 /* This is a small function used when we want to ignore errors from
3025 #ifdef ANSI_PROTOTYPES
3026 ignore_bfd_errors (const char *s, ...)
3028 ignore_bfd_errors (s)
3032 /* Don't do anything. */
3035 /* Check that the architecture of all the input files is compatible
3036 with the output file. Also call the backend to let it do any
3037 other checking that is needed. */
3042 lang_statement_union_type *file;
3044 CONST bfd_arch_info_type *compatible;
3046 for (file = file_chain.head;
3047 file != (lang_statement_union_type *) NULL;
3048 file = file->input_statement.next)
3050 input_bfd = file->input_statement.the_bfd;
3051 compatible = bfd_arch_get_compatible (input_bfd,
3053 if (compatible == NULL)
3055 if (command_line.warn_mismatch)
3056 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3057 bfd_printable_name (input_bfd), input_bfd,
3058 bfd_printable_name (output_bfd));
3062 bfd_error_handler_type pfn = NULL;
3064 /* If we aren't supposed to warn about mismatched input
3065 files, temporarily set the BFD error handler to a
3066 function which will do nothing. We still want to call
3067 bfd_merge_private_bfd_data, since it may set up
3068 information which is needed in the output file. */
3069 if (! command_line.warn_mismatch)
3070 pfn = bfd_set_error_handler (ignore_bfd_errors);
3071 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3073 if (command_line.warn_mismatch)
3074 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3077 if (! command_line.warn_mismatch)
3078 bfd_set_error_handler (pfn);
3083 /* Look through all the global common symbols and attach them to the
3084 correct section. The -sort-common command line switch may be used
3085 to roughly sort the entries by size. */
3090 if (link_info.relocateable
3091 && ! command_line.force_common_definition)
3094 if (! config.sort_common)
3095 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3100 for (power = 4; power >= 0; power--)
3101 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3106 /* Place one common symbol in the correct section. */
3109 lang_one_common (h, info)
3110 struct bfd_link_hash_entry *h;
3113 unsigned int power_of_two;
3117 if (h->type != bfd_link_hash_common)
3121 power_of_two = h->u.c.p->alignment_power;
3123 if (config.sort_common
3124 && power_of_two < (unsigned int) *(int *) info)
3127 section = h->u.c.p->section;
3129 /* Increase the size of the section. */
3130 section->_cooked_size = ALIGN_N (section->_cooked_size,
3131 (bfd_size_type) (1 << power_of_two));
3133 /* Adjust the alignment if necessary. */
3134 if (power_of_two > section->alignment_power)
3135 section->alignment_power = power_of_two;
3137 /* Change the symbol from common to defined. */
3138 h->type = bfd_link_hash_defined;
3139 h->u.def.section = section;
3140 h->u.def.value = section->_cooked_size;
3142 /* Increase the size of the section. */
3143 section->_cooked_size += size;
3145 /* Make sure the section is allocated in memory, and make sure that
3146 it is no longer a common section. */
3147 section->flags |= SEC_ALLOC;
3148 section->flags &= ~ SEC_IS_COMMON;
3150 if (config.map_file != NULL)
3152 static boolean header_printed;
3157 if (! header_printed)
3159 minfo (_("\nAllocating common symbols\n"));
3160 minfo (_("Common symbol size file\n\n"));
3161 header_printed = true;
3164 name = demangle (h->root.string);
3166 len = strlen (name);
3181 if (size <= 0xffffffff)
3182 sprintf (buf, "%lx", (unsigned long) size);
3184 sprintf_vma (buf, size);
3194 minfo ("%B\n", section->owner);
3201 run through the input files and ensure that every input
3202 section has somewhere to go. If one is found without
3203 a destination then create an input request and place it
3204 into the statement tree.
3208 lang_place_orphans ()
3210 lang_input_statement_type *file;
3212 for (file = (lang_input_statement_type *) file_chain.head;
3213 file != (lang_input_statement_type *) NULL;
3214 file = (lang_input_statement_type *) file->next)
3218 for (s = file->the_bfd->sections;
3219 s != (asection *) NULL;
3222 if (s->output_section == (asection *) NULL)
3224 /* This section of the file is not attatched, root
3225 around for a sensible place for it to go */
3227 if (file->just_syms_flag)
3229 /* We are only retrieving symbol values from this
3230 file. We want the symbols to act as though the
3231 values in the file are absolute. */
3232 s->output_section = bfd_abs_section_ptr;
3233 s->output_offset = s->vma;
3235 else if (strcmp (s->name, "COMMON") == 0)
3237 /* This is a lonely common section which must have
3238 come from an archive. We attach to the section
3239 with the wildcard. */
3240 if (! link_info.relocateable
3241 || command_line.force_common_definition)
3243 if (default_common_section == NULL)
3246 /* This message happens when using the
3247 svr3.ifile linker script, so I have
3249 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3251 default_common_section =
3252 lang_output_section_statement_lookup (".bss");
3255 wild_doit (&default_common_section->children, s,
3256 default_common_section, file);
3259 else if (ldemul_place_orphan (file, s))
3263 lang_output_section_statement_type *os =
3264 lang_output_section_statement_lookup (s->name);
3266 wild_doit (&os->children, s, os, file);
3275 lang_set_flags (ptr, flags, invert)
3276 lang_memory_region_type *ptr;
3280 flagword *ptr_flags;
3282 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3288 *ptr_flags |= SEC_ALLOC;
3292 *ptr_flags |= SEC_READONLY;
3296 *ptr_flags |= SEC_DATA;
3300 *ptr_flags |= SEC_CODE;
3305 *ptr_flags |= SEC_LOAD;
3309 einfo (_("%P%F: invalid syntax in flags\n"));
3316 /* Call a function on each input file. This function will be called
3317 on an archive, but not on the elements. */
3320 lang_for_each_input_file (func)
3321 void (*func) PARAMS ((lang_input_statement_type *));
3323 lang_input_statement_type *f;
3325 for (f = (lang_input_statement_type *) input_file_chain.head;
3327 f = (lang_input_statement_type *) f->next_real_file)
3331 /* Call a function on each file. The function will be called on all
3332 the elements of an archive which are included in the link, but will
3333 not be called on the archive file itself. */
3336 lang_for_each_file (func)
3337 void (*func) PARAMS ((lang_input_statement_type *));
3339 lang_input_statement_type *f;
3341 for (f = (lang_input_statement_type *) file_chain.head;
3342 f != (lang_input_statement_type *) NULL;
3343 f = (lang_input_statement_type *) f->next)
3354 lang_for_each_input_section (func)
3355 void (*func) PARAMS ((bfd * ab, asection * as));
3357 lang_input_statement_type *f;
3359 for (f = (lang_input_statement_type *) file_chain.head;
3360 f != (lang_input_statement_type *) NULL;
3361 f = (lang_input_statement_type *) f->next)
3365 for (s = f->the_bfd->sections;
3366 s != (asection *) NULL;
3369 func (f->the_bfd, s);
3377 ldlang_add_file (entry)
3378 lang_input_statement_type * entry;
3382 lang_statement_append (&file_chain,
3383 (lang_statement_union_type *) entry,
3386 /* The BFD linker needs to have a list of all input BFDs involved in
3388 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3389 ASSERT (entry->the_bfd != output_bfd);
3390 for (pp = &link_info.input_bfds;
3391 *pp != (bfd *) NULL;
3392 pp = &(*pp)->link_next)
3394 *pp = entry->the_bfd;
3395 entry->the_bfd->usrdata = (PTR) entry;
3396 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3398 /* Look through the sections and check for any which should not be
3399 included in the link. We need to do this now, so that we can
3400 notice when the backend linker tries to report multiple
3401 definition errors for symbols which are in sections we aren't
3402 going to link. FIXME: It might be better to entirely ignore
3403 symbols which are defined in sections which are going to be
3404 discarded. This would require modifying the backend linker for
3405 each backend which might set the SEC_LINK_ONCE flag. If we do
3406 this, we should probably handle SEC_EXCLUDE in the same way. */
3408 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3412 lang_add_output (name, from_script)
3416 /* Make -o on command line override OUTPUT in script. */
3417 if (had_output_filename == false || !from_script)
3419 output_filename = name;
3420 had_output_filename = true;
3425 static lang_output_section_statement_type *current_section;
3437 for (l = 0; l < 32; l++)
3439 if (i >= (unsigned int) x)
3448 lang_enter_output_section_statement (output_section_statement_name,
3449 address_exp, sectype, block_value,
3450 align, subalign, ebase)
3451 const char *output_section_statement_name;
3452 etree_type * address_exp;
3453 enum section_type sectype;
3454 bfd_vma block_value;
3456 etree_type *subalign;
3459 lang_output_section_statement_type *os;
3463 lang_output_section_statement_lookup (output_section_statement_name);
3467 /* Add this statement to tree */
3468 /* add_statement(lang_output_section_statement_enum,
3469 output_section_statement);*/
3470 /* Make next things chain into subchain of this */
3472 if (os->addr_tree ==
3473 (etree_type *) NULL)
3478 os->sectype = sectype;
3479 if (sectype != noload_section)
3480 os->flags = SEC_NO_FLAGS;
3482 os->flags = SEC_NEVER_LOAD;
3483 os->block_value = block_value ? block_value : 1;
3484 stat_ptr = &os->children;
3486 os->subsection_alignment = topower(
3487 exp_get_value_int(subalign, -1,
3488 "subsection alignment",
3490 os->section_alignment = topower(
3491 exp_get_value_int(align, -1,
3492 "section alignment", 0));
3494 os->load_base = ebase;
3501 lang_output_statement_type *new =
3502 new_stat (lang_output_statement, stat_ptr);
3504 new->name = output_filename;
3507 /* Reset the current counters in the regions */
3509 reset_memory_regions ()
3511 lang_memory_region_type *p = lang_memory_region_list;
3513 for (p = lang_memory_region_list;
3514 p != (lang_memory_region_type *) NULL;
3517 p->old_length = (bfd_size_type) (p->current - p->origin);
3518 p->current = p->origin;
3522 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3523 as needed. SECTION may be NULL, in which case it is a wild card. */
3526 gc_section_callback (ptr, section, file, data)
3527 lang_wild_statement_type *ptr;
3529 lang_input_statement_type *file;
3532 /* If the wild pattern was marked KEEP, the member sections
3533 should be as well. */
3534 if (ptr->keep_sections)
3535 section->flags |= SEC_KEEP;
3538 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3539 may be NULL, indicating that it is a wildcard. */
3542 lang_gc_wild (s, section, file)
3543 lang_wild_statement_type *s;
3544 const char *section;
3547 walk_wild (s, section, file, gc_section_callback, NULL);
3550 /* Iterate over sections marking them against GC. */
3553 lang_gc_sections_1 (s)
3554 lang_statement_union_type * s;
3556 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3558 switch (s->header.type)
3560 case lang_wild_statement_enum:
3561 lang_gc_wild (&s->wild_statement,
3562 s->wild_statement.section_name,
3563 s->wild_statement.filename);
3565 case lang_constructors_statement_enum:
3566 lang_gc_sections_1 (constructor_list.head);
3568 case lang_output_section_statement_enum:
3569 lang_gc_sections_1 (s->output_section_statement.children.head);
3571 case lang_group_statement_enum:
3572 lang_gc_sections_1 (s->group_statement.children.head);
3583 struct bfd_link_hash_entry *h;
3584 ldlang_undef_chain_list_type *ulist, fake_list_start;
3586 /* Keep all sections so marked in the link script. */
3588 lang_gc_sections_1 (statement_list.head);
3590 /* Keep all sections containing symbols undefined on the command-line.
3591 Handle the entry symbol at the same time. */
3593 fake_list_start.next = ldlang_undef_chain_list_head;
3594 if (entry_symbol == NULL)
3595 fake_list_start.name = "start";
3597 fake_list_start.name = (char *) entry_symbol;
3599 for (ulist = &fake_list_start; ulist; ulist = ulist->next)
3601 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3602 false, false, false);
3604 if (h != (struct bfd_link_hash_entry *) NULL
3605 && (h->type == bfd_link_hash_defined
3606 || h->type == bfd_link_hash_defweak)
3607 && ! bfd_is_abs_section (h->u.def.section))
3609 h->u.def.section->flags |= SEC_KEEP;
3613 bfd_gc_sections (output_bfd, &link_info);
3619 lang_reasonable_defaults ();
3620 current_target = default_target;
3622 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3624 ldemul_create_output_section_statements ();
3626 /* Add to the hash table all undefineds on the command line */
3627 lang_place_undefineds ();
3629 /* Create a bfd for each input file */
3630 current_target = default_target;
3631 open_input_bfds (statement_list.head, false);
3633 ldemul_after_open ();
3635 /* Make sure that we're not mixing architectures. We call this
3636 after all the input files have been opened, but before we do any
3637 other processing, so that any operations merge_private_bfd_data
3638 does on the output file will be known during the rest of the
3642 /* Handle .exports instead of a version script if we're told to do so. */
3643 if (command_line.version_exports_section)
3644 lang_do_version_exports_section ();
3646 /* Build all sets based on the information gathered from the input
3648 ldctor_build_sets ();
3650 /* Remove unreferenced sections if asked to. */
3651 if (command_line.gc_sections)
3652 lang_gc_sections ();
3654 /* Size up the common data */
3657 /* Run through the contours of the script and attach input sections
3658 to the correct output sections
3660 map_input_to_output_sections (statement_list.head, (char *) NULL,
3661 (lang_output_section_statement_type *) NULL);
3664 /* Find any sections not attached explicitly and handle them */
3665 lang_place_orphans ();
3667 ldemul_before_allocation ();
3669 /* We must record the program headers before we try to fix the
3670 section positions, since they will affect SIZEOF_HEADERS. */
3671 lang_record_phdrs ();
3673 /* Now run around and relax if we can */
3674 if (command_line.relax)
3676 /* First time round is a trial run to get the 'worst case'
3677 addresses of the objects if there was no relaxing. */
3678 lang_size_sections (statement_list.head,
3680 &(statement_list.head), 0, (bfd_vma) 0, false);
3682 /* Keep relaxing until bfd_relax_section gives up. */
3685 reset_memory_regions ();
3687 relax_again = false;
3689 /* Note: pe-dll.c does something like this also. If you find
3690 you need to change this code, you probably need to change
3691 pe-dll.c also. DJ */
3693 /* Do all the assignments with our current guesses as to
3695 lang_do_assignments (statement_list.head,
3697 (fill_type) 0, (bfd_vma) 0);
3699 /* Perform another relax pass - this time we know where the
3700 globals are, so can make better guess. */
3701 lang_size_sections (statement_list.head,
3703 &(statement_list.head), 0, (bfd_vma) 0, true);
3705 while (relax_again);
3709 /* Size up the sections. */
3710 lang_size_sections (statement_list.head,
3712 &(statement_list.head), 0, (bfd_vma) 0, false);
3715 /* See if anything special should be done now we know how big
3717 ldemul_after_allocation ();
3719 /* Fix any .startof. or .sizeof. symbols. */
3720 lang_set_startof ();
3722 /* Do all the assignments, now that we know the final restingplaces
3723 of all the symbols */
3725 lang_do_assignments (statement_list.head,
3727 (fill_type) 0, (bfd_vma) 0);
3729 /* Make sure that the section addresses make sense. */
3730 if (! link_info.relocateable
3731 && command_line.check_section_addresses)
3732 lang_check_section_addresses ();
3740 /* EXPORTED TO YACC */
3743 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
3744 keep_sections, exclude_filename)
3745 const char *const section_name;
3746 boolean sections_sorted;
3747 const char *const filename;
3748 boolean filenames_sorted;
3749 boolean keep_sections;
3750 const char *exclude_filename;
3752 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3755 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3757 placed_commons = true;
3759 if (filename != NULL && ! wildcardp (filename))
3761 lang_has_input_file = true;
3763 new->section_name = section_name;
3764 new->sections_sorted = sections_sorted;
3765 new->filename = filename;
3766 new->filenames_sorted = filenames_sorted;
3767 new->keep_sections = keep_sections;
3768 new->exclude_filename = exclude_filename;
3769 lang_list_init (&new->children);
3773 lang_section_start (name, address)
3775 etree_type * address;
3777 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3779 ad->section_name = name;
3780 ad->address = address;
3783 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3784 because of a -e argument on the command line, or zero if this is
3785 called by ENTRY in a linker script. Command line arguments take
3789 lang_add_entry (name, cmdline)
3793 if (entry_symbol == NULL
3795 || ! entry_from_cmdline)
3797 entry_symbol = name;
3798 entry_from_cmdline = cmdline;
3803 lang_add_target (name)
3806 lang_target_statement_type *new = new_stat (lang_target_statement,
3822 map_option_f = true;
3833 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3840 lang_add_data (type, exp)
3842 union etree_union *exp;
3845 lang_data_statement_type *new = new_stat (lang_data_statement,
3853 /* Create a new reloc statement. RELOC is the BFD relocation type to
3854 generate. HOWTO is the corresponding howto structure (we could
3855 look this up, but the caller has already done so). SECTION is the
3856 section to generate a reloc against, or NAME is the name of the
3857 symbol to generate a reloc against. Exactly one of SECTION and
3858 NAME must be NULL. ADDEND is an expression for the addend. */
3861 lang_add_reloc (reloc, howto, section, name, addend)
3862 bfd_reloc_code_real_type reloc;
3863 reloc_howto_type *howto;
3866 union etree_union *addend;
3868 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3872 p->section = section;
3874 p->addend_exp = addend;
3876 p->addend_value = 0;
3877 p->output_section = NULL;
3881 lang_assignment_statement_type *
3882 lang_add_assignment (exp)
3885 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3893 lang_add_attribute (attribute)
3894 enum statement_enum attribute;
3896 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3903 if (startup_file != (char *) NULL)
3905 einfo (_("%P%Fmultiple STARTUP files\n"));
3907 first_file->filename = name;
3908 first_file->local_sym_name = name;
3909 first_file->real = true;
3911 startup_file = name;
3918 lang_float_flag = maybe;
3922 lang_leave_output_section_statement (fill, memspec, phdrs)
3924 const char *memspec;
3925 struct lang_output_section_phdr_list *phdrs;
3927 current_section->fill = fill;
3928 current_section->region = lang_memory_region_lookup (memspec);
3929 current_section->phdrs = phdrs;
3930 stat_ptr = &statement_list;
3934 Create an absolute symbol with the given name with the value of the
3935 address of first byte of the section named.
3937 If the symbol already exists, then do nothing.
3940 lang_abs_symbol_at_beginning_of (secname, name)
3941 const char *secname;
3944 struct bfd_link_hash_entry *h;
3946 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3947 if (h == (struct bfd_link_hash_entry *) NULL)
3948 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3950 if (h->type == bfd_link_hash_new
3951 || h->type == bfd_link_hash_undefined)
3955 h->type = bfd_link_hash_defined;
3957 sec = bfd_get_section_by_name (output_bfd, secname);
3958 if (sec == (asection *) NULL)
3961 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3963 h->u.def.section = bfd_abs_section_ptr;
3968 Create an absolute symbol with the given name with the value of the
3969 address of the first byte after the end of the section named.
3971 If the symbol already exists, then do nothing.
3974 lang_abs_symbol_at_end_of (secname, name)
3975 const char *secname;
3978 struct bfd_link_hash_entry *h;
3980 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3981 if (h == (struct bfd_link_hash_entry *) NULL)
3982 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3984 if (h->type == bfd_link_hash_new
3985 || h->type == bfd_link_hash_undefined)
3989 h->type = bfd_link_hash_defined;
3991 sec = bfd_get_section_by_name (output_bfd, secname);
3992 if (sec == (asection *) NULL)
3995 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3996 + bfd_section_size (output_bfd, sec));
3998 h->u.def.section = bfd_abs_section_ptr;
4003 lang_statement_append (list, element, field)
4004 lang_statement_list_type * list;
4005 lang_statement_union_type * element;
4006 lang_statement_union_type ** field;
4008 *(list->tail) = element;
4012 /* Set the output format type. -oformat overrides scripts. */
4015 lang_add_output_format (format, big, little, from_script)
4021 if (output_target == NULL || !from_script)
4023 if (command_line.endian == ENDIAN_BIG
4026 else if (command_line.endian == ENDIAN_LITTLE
4030 output_target = format;
4034 /* Enter a group. This creates a new lang_group_statement, and sets
4035 stat_ptr to build new statements within the group. */
4040 lang_group_statement_type *g;
4042 g = new_stat (lang_group_statement, stat_ptr);
4043 lang_list_init (&g->children);
4044 stat_ptr = &g->children;
4047 /* Leave a group. This just resets stat_ptr to start writing to the
4048 regular list of statements again. Note that this will not work if
4049 groups can occur inside anything else which can adjust stat_ptr,
4050 but currently they can't. */
4055 stat_ptr = &statement_list;
4058 /* Add a new program header. This is called for each entry in a PHDRS
4059 command in a linker script. */
4062 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4070 struct lang_phdr *n, **pp;
4072 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4075 n->type = exp_get_value_int (type, 0, "program header type",
4076 lang_final_phase_enum);
4077 n->filehdr = filehdr;
4082 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4087 /* Record the program header information in the output BFD. FIXME: We
4088 should not be calling an ELF specific function here. */
4091 lang_record_phdrs ()
4095 struct lang_output_section_phdr_list *last;
4096 struct lang_phdr *l;
4097 lang_statement_union_type *u;
4100 secs = (asection **) xmalloc (alc * sizeof (asection *));
4102 for (l = lang_phdr_list; l != NULL; l = l->next)
4109 for (u = lang_output_section_statement.head;
4111 u = u->output_section_statement.next)
4113 lang_output_section_statement_type *os;
4114 struct lang_output_section_phdr_list *pl;
4116 os = &u->output_section_statement;
4123 if (os->sectype == noload_section
4124 || os->bfd_section == NULL
4125 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4130 if (os->bfd_section == NULL)
4133 for (; pl != NULL; pl = pl->next)
4135 if (strcmp (pl->name, l->name) == 0)
4140 secs = ((asection **)
4141 xrealloc (secs, alc * sizeof (asection *)));
4143 secs[c] = os->bfd_section;
4150 if (l->flags == NULL)
4153 flags = exp_get_vma (l->flags, 0, "phdr flags",
4154 lang_final_phase_enum);
4159 at = exp_get_vma (l->at, 0, "phdr load address",
4160 lang_final_phase_enum);
4162 if (! bfd_record_phdr (output_bfd, l->type,
4163 l->flags == NULL ? false : true,
4165 l->at == NULL ? false : true,
4166 at, l->filehdr, l->phdrs, c, secs))
4167 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4172 /* Make sure all the phdr assignments succeeded. */
4173 for (u = lang_output_section_statement.head;
4175 u = u->output_section_statement.next)
4177 struct lang_output_section_phdr_list *pl;
4179 if (u->output_section_statement.bfd_section == NULL)
4182 for (pl = u->output_section_statement.phdrs;
4185 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4186 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4187 u->output_section_statement.name, pl->name);
4191 /* Record a list of sections which may not be cross referenced. */
4194 lang_add_nocrossref (l)
4195 struct lang_nocrossref *l;
4197 struct lang_nocrossrefs *n;
4199 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4200 n->next = nocrossref_list;
4202 nocrossref_list = n;
4204 /* Set notice_all so that we get informed about all symbols. */
4205 link_info.notice_all = true;
4208 /* Overlay handling. We handle overlays with some static variables. */
4210 /* The overlay virtual address. */
4211 static etree_type *overlay_vma;
4213 /* The overlay load address. */
4214 static etree_type *overlay_lma;
4216 /* Whether nocrossrefs is set for this overlay. */
4217 static int overlay_nocrossrefs;
4219 /* An expression for the maximum section size seen so far. */
4220 static etree_type *overlay_max;
4222 /* A list of all the sections in this overlay. */
4226 struct overlay_list *next;
4227 lang_output_section_statement_type *os;
4230 static struct overlay_list *overlay_list;
4232 /* Start handling an overlay. */
4235 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4236 etree_type *vma_expr;
4237 etree_type *lma_expr;
4240 /* The grammar should prevent nested overlays from occurring. */
4241 ASSERT (overlay_vma == NULL
4242 && overlay_lma == NULL
4243 && overlay_list == NULL
4244 && overlay_max == NULL);
4246 overlay_vma = vma_expr;
4247 overlay_lma = lma_expr;
4248 overlay_nocrossrefs = nocrossrefs;
4251 /* Start a section in an overlay. We handle this by calling
4252 lang_enter_output_section_statement with the correct VMA and LMA. */
4255 lang_enter_overlay_section (name)
4258 struct overlay_list *n;
4261 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4262 0, 0, 0, overlay_lma);
4264 /* If this is the first section, then base the VMA and LMA of future
4265 sections on this one. This will work correctly even if `.' is
4266 used in the addresses. */
4267 if (overlay_list == NULL)
4269 overlay_vma = exp_nameop (ADDR, name);
4270 overlay_lma = exp_nameop (LOADADDR, name);
4273 /* Remember the section. */
4274 n = (struct overlay_list *) xmalloc (sizeof *n);
4275 n->os = current_section;
4276 n->next = overlay_list;
4279 size = exp_nameop (SIZEOF, name);
4281 /* Adjust the LMA for the next section. */
4282 overlay_lma = exp_binop ('+', overlay_lma, size);
4284 /* Arrange to work out the maximum section end address. */
4285 if (overlay_max == NULL)
4288 overlay_max = exp_binop (MAX_K, overlay_max, size);
4291 /* Finish a section in an overlay. There isn't any special to do
4295 lang_leave_overlay_section (fill, phdrs)
4297 struct lang_output_section_phdr_list *phdrs;
4304 name = current_section->name;
4306 lang_leave_output_section_statement (fill, "*default*", phdrs);
4308 /* Define the magic symbols. */
4310 clean = xmalloc (strlen (name) + 1);
4312 for (s1 = name; *s1 != '\0'; s1++)
4313 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4317 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4318 sprintf (buf, "__load_start_%s", clean);
4319 lang_add_assignment (exp_assop ('=', buf,
4320 exp_nameop (LOADADDR, name)));
4322 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4323 sprintf (buf, "__load_stop_%s", clean);
4324 lang_add_assignment (exp_assop ('=', buf,
4326 exp_nameop (LOADADDR, name),
4327 exp_nameop (SIZEOF, name))));
4332 /* Finish an overlay. If there are any overlay wide settings, this
4333 looks through all the sections in the overlay and sets them. */
4336 lang_leave_overlay (fill, memspec, phdrs)
4338 const char *memspec;
4339 struct lang_output_section_phdr_list *phdrs;
4341 lang_memory_region_type *region;
4342 struct overlay_list *l;
4343 struct lang_nocrossref *nocrossref;
4345 if (memspec == NULL)
4348 region = lang_memory_region_lookup (memspec);
4355 struct overlay_list *next;
4357 if (fill != 0 && l->os->fill == 0)
4359 if (region != NULL && l->os->region == NULL)
4360 l->os->region = region;
4361 if (phdrs != NULL && l->os->phdrs == NULL)
4362 l->os->phdrs = phdrs;
4364 if (overlay_nocrossrefs)
4366 struct lang_nocrossref *nc;
4368 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4369 nc->name = l->os->name;
4370 nc->next = nocrossref;
4379 if (nocrossref != NULL)
4380 lang_add_nocrossref (nocrossref);
4382 /* Update . for the end of the overlay. */
4383 lang_add_assignment (exp_assop ('=', ".",
4384 exp_binop ('+', overlay_vma, overlay_max)));
4388 overlay_nocrossrefs = 0;
4389 overlay_list = NULL;
4393 /* Version handling. This is only useful for ELF. */
4395 /* This global variable holds the version tree that we build. */
4397 struct bfd_elf_version_tree *lang_elf_version_info;
4400 lang_vers_match_lang_c (expr, sym)
4401 struct bfd_elf_version_expr *expr;
4404 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4406 return fnmatch (expr->pattern, sym, 0) == 0;
4410 lang_vers_match_lang_cplusplus (expr, sym)
4411 struct bfd_elf_version_expr *expr;
4417 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4420 alt_sym = cplus_demangle(sym, /* DMGL_NO_TPARAMS */ 0);
4423 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4424 Should we early out false in this case? */
4425 result = fnmatch (expr->pattern, sym, 0) == 0;
4429 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4437 lang_vers_match_lang_java (expr, sym)
4438 struct bfd_elf_version_expr *expr;
4444 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4447 alt_sym = cplus_demangle(sym, DMGL_JAVA);
4450 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4451 Should we early out false in this case? */
4452 result = fnmatch (expr->pattern, sym, 0) == 0;
4456 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4463 /* This is called for each variable name or match expression. */
4465 struct bfd_elf_version_expr *
4466 lang_new_vers_regex (orig, new, lang)
4467 struct bfd_elf_version_expr *orig;
4471 struct bfd_elf_version_expr *ret;
4473 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4477 if (lang == NULL || strcasecmp (lang, "C") == 0)
4478 ret->match = lang_vers_match_lang_c;
4479 else if (strcasecmp (lang, "C++") == 0)
4480 ret->match = lang_vers_match_lang_cplusplus;
4481 else if (strcasecmp (lang, "Java") == 0)
4482 ret->match = lang_vers_match_lang_java;
4485 einfo (_("%X%P: unknown language `%s' in version information\n"),
4487 ret->match = lang_vers_match_lang_c;
4493 /* This is called for each set of variable names and match
4496 struct bfd_elf_version_tree *
4497 lang_new_vers_node (globals, locals)
4498 struct bfd_elf_version_expr *globals;
4499 struct bfd_elf_version_expr *locals;
4501 struct bfd_elf_version_tree *ret;
4503 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4507 ret->globals = globals;
4508 ret->locals = locals;
4510 ret->name_indx = (unsigned int) -1;
4515 /* This static variable keeps track of version indices. */
4517 static int version_index;
4519 /* This is called when we know the name and dependencies of the
4523 lang_register_vers_node (name, version, deps)
4525 struct bfd_elf_version_tree *version;
4526 struct bfd_elf_version_deps *deps;
4528 struct bfd_elf_version_tree *t, **pp;
4529 struct bfd_elf_version_expr *e1;
4531 /* Make sure this node has a unique name. */
4532 for (t = lang_elf_version_info; t != NULL; t = t->next)
4533 if (strcmp (t->name, name) == 0)
4534 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4536 /* Check the global and local match names, and make sure there
4537 aren't any duplicates. */
4539 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4541 for (t = lang_elf_version_info; t != NULL; t = t->next)
4543 struct bfd_elf_version_expr *e2;
4545 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4546 if (strcmp (e1->pattern, e2->pattern) == 0)
4547 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4552 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4554 for (t = lang_elf_version_info; t != NULL; t = t->next)
4556 struct bfd_elf_version_expr *e2;
4558 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4559 if (strcmp (e1->pattern, e2->pattern) == 0)
4560 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4565 version->deps = deps;
4566 version->name = name;
4568 version->vernum = version_index;
4570 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4575 /* This is called when we see a version dependency. */
4577 struct bfd_elf_version_deps *
4578 lang_add_vers_depend (list, name)
4579 struct bfd_elf_version_deps *list;
4582 struct bfd_elf_version_deps *ret;
4583 struct bfd_elf_version_tree *t;
4585 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4588 for (t = lang_elf_version_info; t != NULL; t = t->next)
4590 if (strcmp (t->name, name) == 0)
4592 ret->version_needed = t;
4597 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
4603 lang_do_version_exports_section ()
4605 struct bfd_elf_version_expr *greg = NULL, *lreg;
4607 LANG_FOR_EACH_INPUT_STATEMENT (is)
4609 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
4616 len = bfd_section_size (is->the_bfd, sec);
4617 contents = xmalloc (len);
4618 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
4619 einfo (_("%X%P: unable to read .exports section contents"), sec);
4622 while (p < contents+len)
4624 greg = lang_new_vers_regex (greg, p, NULL);
4625 p = strchr (p, '\0') + 1;
4628 /* Do not free the contents, as we used them creating the regex. */
4630 /* Do not include this section in the link. */
4631 bfd_set_section_flags (is->the_bfd, sec,
4632 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
4635 lreg = lang_new_vers_regex (NULL, "*", NULL);
4636 lang_register_vers_node (command_line.version_exports_section,
4637 lang_new_vers_node (greg, lreg), NULL);