1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static lang_input_statement_type *lookup_name (const char *);
71 static bfd_boolean load_symbols (lang_input_statement_type *,
72 lang_statement_list_type *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection *);
77 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
78 static void print_statement (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statement_list (lang_statement_union_type *,
81 lang_output_section_statement_type *);
82 static void print_statements (void);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 /* Exported variables. */
88 lang_output_section_statement_type *abs_output_section;
89 lang_statement_list_type lang_output_section_statement;
90 lang_statement_list_type *stat_ptr = &statement_list;
91 lang_statement_list_type file_chain = { NULL, NULL };
92 struct bfd_sym_chain entry_symbol = { NULL, NULL };
93 const char *entry_section = ".text";
94 bfd_boolean entry_from_cmdline;
95 bfd_boolean lang_has_input_file = FALSE;
96 bfd_boolean had_output_filename = FALSE;
97 bfd_boolean lang_float_flag = FALSE;
98 bfd_boolean delete_output_file_on_failure = FALSE;
99 struct lang_nocrossrefs *nocrossref_list;
100 struct unique_sections *unique_section_list;
101 static bfd_boolean ldlang_sysrooted_script = FALSE;
102 int lang_statement_iteration = 0;
104 etree_type *base; /* Relocation base - or null */
106 /* Return TRUE if the PATTERN argument is a wildcard pattern.
107 Although backslashes are treated specially if a pattern contains
108 wildcards, we do not consider the mere presence of a backslash to
109 be enough to cause the pattern to be treated as a wildcard.
110 That lets us handle DOS filenames more naturally. */
111 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
113 #define new_stat(x, y) \
114 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
116 #define outside_section_address(q) \
117 ((q)->output_offset + (q)->output_section->vma)
119 #define outside_symbol_address(q) \
120 ((q)->value + outside_section_address (q->section))
122 #define SECTION_NAME_MAP_LENGTH (16)
125 stat_alloc (size_t size)
127 return obstack_alloc (&stat_obstack, size);
131 unique_section_p (const asection *sec)
133 struct unique_sections *unam;
136 if (link_info.relocatable
137 && sec->owner != NULL
138 && bfd_is_group_section (sec->owner, sec))
142 for (unam = unique_section_list; unam; unam = unam->next)
143 if (wildcardp (unam->name)
144 ? fnmatch (unam->name, secnam, 0) == 0
145 : strcmp (unam->name, secnam) == 0)
153 /* Generic traversal routines for finding matching sections. */
155 /* Try processing a section against a wildcard. This just calls
156 the callback unless the filename exclusion list is present
157 and excludes the file. It's hardly ever present so this
158 function is very fast. */
161 walk_wild_consider_section (lang_wild_statement_type *ptr,
162 lang_input_statement_type *file,
164 struct wildcard_list *sec,
168 bfd_boolean skip = FALSE;
169 struct name_list *list_tmp;
171 /* Don't process sections from files which were
173 for (list_tmp = sec->spec.exclude_name_list;
175 list_tmp = list_tmp->next)
177 bfd_boolean is_wildcard = wildcardp (list_tmp->name);
179 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
181 skip = strcmp (list_tmp->name, file->filename) == 0;
183 /* If this file is part of an archive, and the archive is
184 excluded, exclude this file. */
185 if (! skip && file->the_bfd != NULL
186 && file->the_bfd->my_archive != NULL
187 && file->the_bfd->my_archive->filename != NULL)
190 skip = fnmatch (list_tmp->name,
191 file->the_bfd->my_archive->filename,
194 skip = strcmp (list_tmp->name,
195 file->the_bfd->my_archive->filename) == 0;
203 (*callback) (ptr, sec, s, file, data);
206 /* Lowest common denominator routine that can handle everything correctly,
210 walk_wild_section_general (lang_wild_statement_type *ptr,
211 lang_input_statement_type *file,
216 struct wildcard_list *sec;
218 for (s = file->the_bfd->sections; s != NULL; s = s->next)
220 sec = ptr->section_list;
222 (*callback) (ptr, sec, s, file, data);
226 bfd_boolean skip = FALSE;
228 if (sec->spec.name != NULL)
230 const char *sname = bfd_get_section_name (file->the_bfd, s);
232 if (wildcardp (sec->spec.name))
233 skip = fnmatch (sec->spec.name, sname, 0) != 0;
235 skip = strcmp (sec->spec.name, sname) != 0;
239 walk_wild_consider_section (ptr, file, s, sec, callback, data);
246 /* Routines to find a single section given its name. If there's more
247 than one section with that name, we report that. */
251 asection *found_section;
252 bfd_boolean multiple_sections_found;
253 } section_iterator_callback_data;
256 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
258 section_iterator_callback_data *d = data;
260 if (d->found_section != NULL)
262 d->multiple_sections_found = TRUE;
266 d->found_section = s;
271 find_section (lang_input_statement_type *file,
272 struct wildcard_list *sec,
273 bfd_boolean *multiple_sections_found)
275 section_iterator_callback_data cb_data = { NULL, FALSE };
277 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
278 section_iterator_callback, &cb_data);
279 *multiple_sections_found = cb_data.multiple_sections_found;
280 return cb_data.found_section;
283 /* Code for handling simple wildcards without going through fnmatch,
284 which can be expensive because of charset translations etc. */
286 /* A simple wild is a literal string followed by a single '*',
287 where the literal part is at least 4 characters long. */
290 is_simple_wild (const char *name)
292 size_t len = strcspn (name, "*?[");
293 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
297 match_simple_wild (const char *pattern, const char *name)
299 /* The first four characters of the pattern are guaranteed valid
300 non-wildcard characters. So we can go faster. */
301 if (pattern[0] != name[0] || pattern[1] != name[1]
302 || pattern[2] != name[2] || pattern[3] != name[3])
307 while (*pattern != '*')
308 if (*name++ != *pattern++)
314 /* Specialized, optimized routines for handling different kinds of
318 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
319 lang_input_statement_type *file,
323 /* We can just do a hash lookup for the section with the right name.
324 But if that lookup discovers more than one section with the name
325 (should be rare), we fall back to the general algorithm because
326 we would otherwise have to sort the sections to make sure they
327 get processed in the bfd's order. */
328 bfd_boolean multiple_sections_found;
329 struct wildcard_list *sec0 = ptr->handler_data[0];
330 asection *s0 = find_section (file, sec0, &multiple_sections_found);
332 if (multiple_sections_found)
333 walk_wild_section_general (ptr, file, callback, data);
335 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
339 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
340 lang_input_statement_type *file,
345 struct wildcard_list *wildsec0 = ptr->handler_data[0];
347 for (s = file->the_bfd->sections; s != NULL; s = s->next)
349 const char *sname = bfd_get_section_name (file->the_bfd, s);
350 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
353 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
358 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
359 lang_input_statement_type *file,
364 struct wildcard_list *sec0 = ptr->handler_data[0];
365 struct wildcard_list *wildsec1 = ptr->handler_data[1];
366 bfd_boolean multiple_sections_found;
367 asection *s0 = find_section (file, sec0, &multiple_sections_found);
369 if (multiple_sections_found)
371 walk_wild_section_general (ptr, file, callback, data);
375 /* Note that if the section was not found, s0 is NULL and
376 we'll simply never succeed the s == s0 test below. */
377 for (s = file->the_bfd->sections; s != NULL; s = s->next)
379 /* Recall that in this code path, a section cannot satisfy more
380 than one spec, so if s == s0 then it cannot match
383 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
386 const char *sname = bfd_get_section_name (file->the_bfd, s);
387 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
390 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
397 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
398 lang_input_statement_type *file,
403 struct wildcard_list *sec0 = ptr->handler_data[0];
404 struct wildcard_list *wildsec1 = ptr->handler_data[1];
405 struct wildcard_list *wildsec2 = ptr->handler_data[2];
406 bfd_boolean multiple_sections_found;
407 asection *s0 = find_section (file, sec0, &multiple_sections_found);
409 if (multiple_sections_found)
411 walk_wild_section_general (ptr, file, callback, data);
415 for (s = file->the_bfd->sections; s != NULL; s = s->next)
418 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
421 const char *sname = bfd_get_section_name (file->the_bfd, s);
422 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
425 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
428 skip = !match_simple_wild (wildsec2->spec.name, sname);
430 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
438 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
439 lang_input_statement_type *file,
444 struct wildcard_list *sec0 = ptr->handler_data[0];
445 struct wildcard_list *sec1 = ptr->handler_data[1];
446 struct wildcard_list *wildsec2 = ptr->handler_data[2];
447 struct wildcard_list *wildsec3 = ptr->handler_data[3];
448 bfd_boolean multiple_sections_found;
449 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
451 if (multiple_sections_found)
453 walk_wild_section_general (ptr, file, callback, data);
457 s1 = find_section (file, sec1, &multiple_sections_found);
458 if (multiple_sections_found)
460 walk_wild_section_general (ptr, file, callback, data);
464 for (s = file->the_bfd->sections; s != NULL; s = s->next)
467 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
470 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
473 const char *sname = bfd_get_section_name (file->the_bfd, s);
474 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
478 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
482 skip = !match_simple_wild (wildsec3->spec.name, sname);
484 walk_wild_consider_section (ptr, file, s, wildsec3,
492 walk_wild_section (lang_wild_statement_type *ptr,
493 lang_input_statement_type *file,
497 if (file->just_syms_flag)
500 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
503 /* Returns TRUE when name1 is a wildcard spec that might match
504 something name2 can match. We're conservative: we return FALSE
505 only if the prefixes of name1 and name2 are different up to the
506 first wildcard character. */
509 wild_spec_can_overlap (const char *name1, const char *name2)
511 size_t prefix1_len = strcspn (name1, "?*[");
512 size_t prefix2_len = strcspn (name2, "?*[");
513 size_t min_prefix_len;
515 /* Note that if there is no wildcard character, then we treat the
516 terminating 0 as part of the prefix. Thus ".text" won't match
517 ".text." or ".text.*", for example. */
518 if (name1[prefix1_len] == '\0')
520 if (name2[prefix2_len] == '\0')
523 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
525 return memcmp (name1, name2, min_prefix_len) == 0;
528 /* Select specialized code to handle various kinds of wildcard
532 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
535 int wild_name_count = 0;
536 struct wildcard_list *sec;
540 ptr->walk_wild_section_handler = walk_wild_section_general;
542 /* Count how many wildcard_specs there are, and how many of those
543 actually use wildcards in the name. Also, bail out if any of the
544 wildcard names are NULL. (Can this actually happen?
545 walk_wild_section used to test for it.) And bail out if any
546 of the wildcards are more complex than a simple string
547 ending in a single '*'. */
548 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
551 if (sec->spec.name == NULL)
553 if (wildcardp (sec->spec.name))
556 if (!is_simple_wild (sec->spec.name))
561 /* The zero-spec case would be easy to optimize but it doesn't
562 happen in practice. Likewise, more than 4 specs doesn't
563 happen in practice. */
564 if (sec_count == 0 || sec_count > 4)
567 /* Check that no two specs can match the same section. */
568 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
570 struct wildcard_list *sec2;
571 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
573 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
578 signature = (sec_count << 8) + wild_name_count;
582 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
585 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
588 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
591 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
594 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
600 /* Now fill the data array with pointers to the specs, first the
601 specs with non-wildcard names, then the specs with wildcard
602 names. It's OK to process the specs in different order from the
603 given order, because we've already determined that no section
604 will match more than one spec. */
606 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
607 if (!wildcardp (sec->spec.name))
608 ptr->handler_data[data_counter++] = sec;
609 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
610 if (wildcardp (sec->spec.name))
611 ptr->handler_data[data_counter++] = sec;
614 /* Handle a wild statement for a single file F. */
617 walk_wild_file (lang_wild_statement_type *s,
618 lang_input_statement_type *f,
622 if (f->the_bfd == NULL
623 || ! bfd_check_format (f->the_bfd, bfd_archive))
624 walk_wild_section (s, f, callback, data);
629 /* This is an archive file. We must map each member of the
630 archive separately. */
631 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
632 while (member != NULL)
634 /* When lookup_name is called, it will call the add_symbols
635 entry point for the archive. For each element of the
636 archive which is included, BFD will call ldlang_add_file,
637 which will set the usrdata field of the member to the
638 lang_input_statement. */
639 if (member->usrdata != NULL)
641 walk_wild_section (s, member->usrdata, callback, data);
644 member = bfd_openr_next_archived_file (f->the_bfd, member);
650 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
652 const char *file_spec = s->filename;
654 if (file_spec == NULL)
656 /* Perform the iteration over all files in the list. */
657 LANG_FOR_EACH_INPUT_STATEMENT (f)
659 walk_wild_file (s, f, callback, data);
662 else if (wildcardp (file_spec))
664 LANG_FOR_EACH_INPUT_STATEMENT (f)
666 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
667 walk_wild_file (s, f, callback, data);
672 lang_input_statement_type *f;
674 /* Perform the iteration over a single file. */
675 f = lookup_name (file_spec);
677 walk_wild_file (s, f, callback, data);
681 /* lang_for_each_statement walks the parse tree and calls the provided
682 function for each node. */
685 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
686 lang_statement_union_type *s)
688 for (; s != NULL; s = s->header.next)
692 switch (s->header.type)
694 case lang_constructors_statement_enum:
695 lang_for_each_statement_worker (func, constructor_list.head);
697 case lang_output_section_statement_enum:
698 lang_for_each_statement_worker
699 (func, s->output_section_statement.children.head);
701 case lang_wild_statement_enum:
702 lang_for_each_statement_worker (func,
703 s->wild_statement.children.head);
705 case lang_group_statement_enum:
706 lang_for_each_statement_worker (func,
707 s->group_statement.children.head);
709 case lang_data_statement_enum:
710 case lang_reloc_statement_enum:
711 case lang_object_symbols_statement_enum:
712 case lang_output_statement_enum:
713 case lang_target_statement_enum:
714 case lang_input_section_enum:
715 case lang_input_statement_enum:
716 case lang_assignment_statement_enum:
717 case lang_padding_statement_enum:
718 case lang_address_statement_enum:
719 case lang_fill_statement_enum:
729 lang_for_each_statement (void (*func) (lang_statement_union_type *))
731 lang_for_each_statement_worker (func, statement_list.head);
734 /*----------------------------------------------------------------------*/
737 lang_list_init (lang_statement_list_type *list)
740 list->tail = &list->head;
743 /* Build a new statement node for the parse tree. */
745 static lang_statement_union_type *
746 new_statement (enum statement_enum type,
748 lang_statement_list_type *list)
750 lang_statement_union_type *new;
752 new = stat_alloc (size);
753 new->header.type = type;
754 new->header.next = NULL;
755 lang_statement_append (list, new, &new->header.next);
759 /* Build a new input file node for the language. There are several
760 ways in which we treat an input file, eg, we only look at symbols,
761 or prefix it with a -l etc.
763 We can be supplied with requests for input files more than once;
764 they may, for example be split over several lines like foo.o(.text)
765 foo.o(.data) etc, so when asked for a file we check that we haven't
766 got it already so we don't duplicate the bfd. */
768 static lang_input_statement_type *
769 new_afile (const char *name,
770 lang_input_file_enum_type file_type,
772 bfd_boolean add_to_list)
774 lang_input_statement_type *p;
777 p = new_stat (lang_input_statement, stat_ptr);
780 p = stat_alloc (sizeof (lang_input_statement_type));
781 p->header.next = NULL;
784 lang_has_input_file = TRUE;
786 p->sysrooted = FALSE;
789 case lang_input_file_is_symbols_only_enum:
791 p->is_archive = FALSE;
793 p->local_sym_name = name;
794 p->just_syms_flag = TRUE;
795 p->search_dirs_flag = FALSE;
797 case lang_input_file_is_fake_enum:
799 p->is_archive = FALSE;
801 p->local_sym_name = name;
802 p->just_syms_flag = FALSE;
803 p->search_dirs_flag = FALSE;
805 case lang_input_file_is_l_enum:
806 p->is_archive = TRUE;
809 p->local_sym_name = concat ("-l", name, NULL);
810 p->just_syms_flag = FALSE;
811 p->search_dirs_flag = TRUE;
813 case lang_input_file_is_marker_enum:
815 p->is_archive = FALSE;
817 p->local_sym_name = name;
818 p->just_syms_flag = FALSE;
819 p->search_dirs_flag = TRUE;
821 case lang_input_file_is_search_file_enum:
822 p->sysrooted = ldlang_sysrooted_script;
824 p->is_archive = FALSE;
826 p->local_sym_name = name;
827 p->just_syms_flag = FALSE;
828 p->search_dirs_flag = TRUE;
830 case lang_input_file_is_file_enum:
832 p->is_archive = FALSE;
834 p->local_sym_name = name;
835 p->just_syms_flag = FALSE;
836 p->search_dirs_flag = FALSE;
843 p->next_real_file = NULL;
846 p->dynamic = config.dynamic_link;
847 p->add_needed = add_needed;
848 p->as_needed = as_needed;
849 p->whole_archive = whole_archive;
851 lang_statement_append (&input_file_chain,
852 (lang_statement_union_type *) p,
857 lang_input_statement_type *
858 lang_add_input_file (const char *name,
859 lang_input_file_enum_type file_type,
862 lang_has_input_file = TRUE;
863 return new_afile (name, file_type, target, TRUE);
866 /* Build enough state so that the parser can build its tree. */
871 obstack_begin (&stat_obstack, 1000);
873 stat_ptr = &statement_list;
875 lang_list_init (stat_ptr);
877 lang_list_init (&input_file_chain);
878 lang_list_init (&lang_output_section_statement);
879 lang_list_init (&file_chain);
880 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
883 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
885 abs_output_section->bfd_section = bfd_abs_section_ptr;
887 /* The value "3" is ad-hoc, somewhat related to the expected number of
888 DEFINED expressions in a linker script. For most default linker
889 scripts, there are none. Why a hash table then? Well, it's somewhat
890 simpler to re-use working machinery than using a linked list in terms
891 of code-complexity here in ld, besides the initialization which just
892 looks like other code here. */
893 if (!bfd_hash_table_init_n (&lang_definedness_table,
894 lang_definedness_newfunc, 3))
895 einfo (_("%P%F: out of memory during initialization"));
897 /* Callers of exp_fold_tree need to increment this. */
898 lang_statement_iteration = 0;
901 /*----------------------------------------------------------------------
902 A region is an area of memory declared with the
903 MEMORY { name:org=exp, len=exp ... }
906 We maintain a list of all the regions here.
908 If no regions are specified in the script, then the default is used
909 which is created when looked up to be the entire data space.
911 If create is true we are creating a region inside a MEMORY block.
912 In this case it is probably an error to create a region that has
913 already been created. If we are not inside a MEMORY block it is
914 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
915 and so we issue a warning. */
917 static lang_memory_region_type *lang_memory_region_list;
918 static lang_memory_region_type **lang_memory_region_list_tail
919 = &lang_memory_region_list;
921 lang_memory_region_type *
922 lang_memory_region_lookup (const char *const name, bfd_boolean create)
924 lang_memory_region_type *p;
925 lang_memory_region_type *new;
927 /* NAME is NULL for LMA memspecs if no region was specified. */
931 for (p = lang_memory_region_list; p != NULL; p = p->next)
932 if (strcmp (p->name, name) == 0)
935 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
940 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
941 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
943 new = stat_alloc (sizeof (lang_memory_region_type));
945 new->name = xstrdup (name);
948 *lang_memory_region_list_tail = new;
949 lang_memory_region_list_tail = &new->next;
953 new->length = ~(bfd_size_type) 0;
955 new->had_full_message = FALSE;
960 static lang_memory_region_type *
961 lang_memory_default (asection *section)
963 lang_memory_region_type *p;
965 flagword sec_flags = section->flags;
967 /* Override SEC_DATA to mean a writable section. */
968 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
969 sec_flags |= SEC_DATA;
971 for (p = lang_memory_region_list; p != NULL; p = p->next)
973 if ((p->flags & sec_flags) != 0
974 && (p->not_flags & sec_flags) == 0)
979 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
982 static lang_output_section_statement_type *
983 lang_output_section_find_1 (const char *const name, int constraint)
985 lang_output_section_statement_type *lookup;
987 for (lookup = &lang_output_section_statement.head->output_section_statement;
989 lookup = lookup->next)
991 if (strcmp (name, lookup->name) == 0
992 && lookup->constraint != -1
993 && (constraint == 0 || constraint == lookup->constraint))
999 lang_output_section_statement_type *
1000 lang_output_section_find (const char *const name)
1002 return lang_output_section_find_1 (name, 0);
1005 static lang_output_section_statement_type *
1006 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
1008 lang_output_section_statement_type *lookup;
1010 lookup = lang_output_section_find_1 (name, constraint);
1013 lookup = new_stat (lang_output_section_statement, stat_ptr);
1014 lookup->region = NULL;
1015 lookup->lma_region = NULL;
1017 lookup->block_value = 1;
1018 lookup->name = name;
1020 lookup->next = NULL;
1021 lookup->bfd_section = NULL;
1022 lookup->processed = 0;
1023 lookup->constraint = constraint;
1024 lookup->sectype = normal_section;
1025 lookup->addr_tree = NULL;
1026 lang_list_init (&lookup->children);
1028 lookup->memspec = NULL;
1030 lookup->subsection_alignment = -1;
1031 lookup->section_alignment = -1;
1032 lookup->load_base = NULL;
1033 lookup->update_dot_tree = NULL;
1034 lookup->phdrs = NULL;
1036 lang_statement_append (&lang_output_section_statement,
1037 (lang_statement_union_type *) lookup,
1038 (lang_statement_union_type **) &lookup->next);
1043 lang_output_section_statement_type *
1044 lang_output_section_statement_lookup (const char *const name)
1046 return lang_output_section_statement_lookup_1 (name, 0);
1049 /* A variant of lang_output_section_find used by place_orphan.
1050 Returns the output statement that should precede a new output
1051 statement for SEC. If an exact match is found on certain flags,
1054 lang_output_section_statement_type *
1055 lang_output_section_find_by_flags (const asection *sec,
1056 lang_output_section_statement_type **exact)
1058 lang_output_section_statement_type *first, *look, *found;
1061 /* We know the first statement on this list is *ABS*. May as well
1063 first = &lang_output_section_statement.head->output_section_statement;
1064 first = first->next;
1066 /* First try for an exact match. */
1068 for (look = first; look; look = look->next)
1070 flags = look->flags;
1071 if (look->bfd_section != NULL)
1072 flags = look->bfd_section->flags;
1073 flags ^= sec->flags;
1074 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1075 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1084 if (sec->flags & SEC_CODE)
1086 /* Try for a rw code section. */
1087 for (look = first; look; look = look->next)
1089 flags = look->flags;
1090 if (look->bfd_section != NULL)
1091 flags = look->bfd_section->flags;
1092 flags ^= sec->flags;
1093 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1094 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1100 if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
1102 /* .rodata can go after .text, .sdata2 after .rodata. */
1103 for (look = first; look; look = look->next)
1105 flags = look->flags;
1106 if (look->bfd_section != NULL)
1107 flags = look->bfd_section->flags;
1108 flags ^= sec->flags;
1109 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1111 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1117 if (sec->flags & SEC_SMALL_DATA)
1119 /* .sdata goes after .data, .sbss after .sdata. */
1120 for (look = first; look; look = look->next)
1122 flags = look->flags;
1123 if (look->bfd_section != NULL)
1124 flags = look->bfd_section->flags;
1125 flags ^= sec->flags;
1126 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1127 | SEC_THREAD_LOCAL))
1128 || ((look->flags & SEC_SMALL_DATA)
1129 && !(sec->flags & SEC_HAS_CONTENTS)))
1135 if (sec->flags & SEC_HAS_CONTENTS)
1137 /* .data goes after .rodata. */
1138 for (look = first; look; look = look->next)
1140 flags = look->flags;
1141 if (look->bfd_section != NULL)
1142 flags = look->bfd_section->flags;
1143 flags ^= sec->flags;
1144 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1145 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1151 /* .bss goes last. */
1152 for (look = first; look; look = look->next)
1154 flags = look->flags;
1155 if (look->bfd_section != NULL)
1156 flags = look->bfd_section->flags;
1157 flags ^= sec->flags;
1158 if (!(flags & SEC_ALLOC))
1165 /* Find the last output section before given output statement.
1166 Used by place_orphan. */
1169 output_prev_sec_find (lang_output_section_statement_type *os)
1171 asection *s = (asection *) NULL;
1172 lang_output_section_statement_type *lookup;
1174 for (lookup = &lang_output_section_statement.head->output_section_statement;
1176 lookup = lookup->next)
1178 if (lookup->constraint == -1)
1183 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1184 s = lookup->bfd_section;
1190 lang_output_section_statement_type *
1191 lang_insert_orphan (lang_input_statement_type *file,
1193 const char *secname,
1194 lang_output_section_statement_type *after,
1195 struct orphan_save *place,
1196 etree_type *address,
1197 lang_statement_list_type *add_child)
1199 lang_statement_list_type *old;
1200 lang_statement_list_type add;
1202 etree_type *load_base;
1203 lang_output_section_statement_type *os;
1204 lang_output_section_statement_type **os_tail;
1205 asection **bfd_tail;
1207 /* Start building a list of statements for this section.
1208 First save the current statement pointer. */
1211 /* If we have found an appropriate place for the output section
1212 statements for this orphan, add them to our own private list,
1213 inserting them later into the global statement list. */
1217 lang_list_init (stat_ptr);
1221 if (config.build_constructors)
1223 /* If the name of the section is representable in C, then create
1224 symbols to mark the start and the end of the section. */
1225 for (ps = secname; *ps != '\0'; ps++)
1226 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1231 etree_type *e_align;
1233 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1234 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1235 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1236 e_align = exp_unop (ALIGN_K,
1237 exp_intop ((bfd_vma) 1 << s->alignment_power));
1238 lang_add_assignment (exp_assop ('=', ".", e_align));
1239 lang_add_assignment (exp_assop ('=', symname,
1240 exp_nameop (NAME, ".")));
1244 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1245 address = exp_intop (0);
1248 if (after != NULL && after->load_base != NULL)
1250 etree_type *lma_from_vma;
1251 lma_from_vma = exp_binop ('-', after->load_base,
1252 exp_nameop (ADDR, after->name));
1253 load_base = exp_binop ('+', lma_from_vma,
1254 exp_nameop (ADDR, secname));
1257 os_tail = ((lang_output_section_statement_type **)
1258 lang_output_section_statement.tail);
1259 bfd_tail = output_bfd->section_tail;
1260 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1263 if (add_child == NULL)
1264 add_child = &os->children;
1265 lang_add_section (add_child, s, os, file);
1267 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1269 if (config.build_constructors && *ps == '\0')
1273 /* lang_leave_ouput_section_statement resets stat_ptr.
1274 Put stat_ptr back where we want it. */
1278 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1279 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1280 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1281 lang_add_assignment (exp_assop ('=', symname,
1282 exp_nameop (NAME, ".")));
1285 /* Restore the global list pointer. */
1289 if (after != NULL && os->bfd_section != NULL)
1293 snew = os->bfd_section;
1295 /* Shuffle the bfd section list to make the output file look
1296 neater. This is really only cosmetic. */
1297 if (place->section == NULL
1298 && after != (&lang_output_section_statement.head
1299 ->output_section_statement))
1301 asection *bfd_section = after->bfd_section;
1303 /* If the output statement hasn't been used to place any input
1304 sections (and thus doesn't have an output bfd_section),
1305 look for the closest prior output statement having an
1307 if (bfd_section == NULL)
1308 bfd_section = output_prev_sec_find (after);
1310 if (bfd_section != NULL && bfd_section != snew)
1311 place->section = &bfd_section->next;
1314 if (place->section == NULL)
1315 place->section = &output_bfd->sections;
1317 /* Unlink the section. */
1318 ASSERT (*bfd_tail == snew);
1319 bfd_section_list_remove (output_bfd, bfd_tail);
1321 /* Now tack it back on in the right place. */
1322 bfd_section_list_insert (output_bfd, place->section, snew);
1324 /* Save the end of this list. Further ophans of this type will
1325 follow the one we've just added. */
1326 place->section = &snew->next;
1328 /* The following is non-cosmetic. We try to put the output
1329 statements in some sort of reasonable order here, because they
1330 determine the final load addresses of the orphan sections.
1331 In addition, placing output statements in the wrong order may
1332 require extra segments. For instance, given a typical
1333 situation of all read-only sections placed in one segment and
1334 following that a segment containing all the read-write
1335 sections, we wouldn't want to place an orphan read/write
1336 section before or amongst the read-only ones. */
1337 if (add.head != NULL)
1339 lang_output_section_statement_type *newly_added_os;
1341 if (place->stmt == NULL)
1343 lang_statement_union_type **where;
1344 lang_statement_union_type **assign = NULL;
1346 /* Look for a suitable place for the new statement list.
1347 The idea is to skip over anything that might be inside
1348 a SECTIONS {} statement in a script, before we find
1349 another output_section_statement. Assignments to "dot"
1350 before an output section statement are assumed to
1352 for (where = &after->header.next;
1354 where = &(*where)->header.next)
1356 switch ((*where)->header.type)
1358 case lang_assignment_statement_enum:
1361 lang_assignment_statement_type *ass;
1362 ass = &(*where)->assignment_statement;
1363 if (ass->exp->type.node_class != etree_assert
1364 && ass->exp->assign.dst[0] == '.'
1365 && ass->exp->assign.dst[1] == 0)
1369 case lang_wild_statement_enum:
1370 case lang_input_section_enum:
1371 case lang_object_symbols_statement_enum:
1372 case lang_fill_statement_enum:
1373 case lang_data_statement_enum:
1374 case lang_reloc_statement_enum:
1375 case lang_padding_statement_enum:
1376 case lang_constructors_statement_enum:
1379 case lang_output_section_statement_enum:
1382 case lang_input_statement_enum:
1383 case lang_address_statement_enum:
1384 case lang_target_statement_enum:
1385 case lang_output_statement_enum:
1386 case lang_group_statement_enum:
1387 case lang_afile_asection_pair_statement_enum:
1396 place->os_tail = &after->next;
1400 /* Put it after the last orphan statement we added. */
1401 *add.tail = *place->stmt;
1402 *place->stmt = add.head;
1405 /* Fix the global list pointer if we happened to tack our
1406 new list at the tail. */
1407 if (*old->tail == add.head)
1408 old->tail = add.tail;
1410 /* Save the end of this list. */
1411 place->stmt = add.tail;
1413 /* Do the same for the list of output section statements. */
1414 newly_added_os = *os_tail;
1416 newly_added_os->next = *place->os_tail;
1417 *place->os_tail = newly_added_os;
1418 place->os_tail = &newly_added_os->next;
1420 /* Fixing the global list pointer here is a little different.
1421 We added to the list in lang_enter_output_section_statement,
1422 trimmed off the new output_section_statment above when
1423 assigning *os_tail = NULL, but possibly added it back in
1424 the same place when assigning *place->os_tail. */
1425 if (*os_tail == NULL)
1426 lang_output_section_statement.tail
1427 = (lang_statement_union_type **) os_tail;
1434 lang_map_flags (flagword flag)
1436 if (flag & SEC_ALLOC)
1439 if (flag & SEC_CODE)
1442 if (flag & SEC_READONLY)
1445 if (flag & SEC_DATA)
1448 if (flag & SEC_LOAD)
1455 lang_memory_region_type *m;
1458 minfo (_("\nMemory Configuration\n\n"));
1459 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1460 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1462 for (m = lang_memory_region_list; m != NULL; m = m->next)
1467 fprintf (config.map_file, "%-16s ", m->name);
1469 sprintf_vma (buf, m->origin);
1470 minfo ("0x%s ", buf);
1478 minfo ("0x%V", m->length);
1479 if (m->flags || m->not_flags)
1487 lang_map_flags (m->flags);
1493 lang_map_flags (m->not_flags);
1500 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1502 if (! command_line.reduce_memory_overheads)
1504 obstack_begin (&map_obstack, 1000);
1505 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1506 bfd_map_over_sections (p, init_map_userdata, 0);
1507 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1509 print_statements ();
1513 init_map_userdata (abfd, sec, data)
1514 bfd *abfd ATTRIBUTE_UNUSED;
1516 void *data ATTRIBUTE_UNUSED;
1518 fat_section_userdata_type *new_data
1519 = ((fat_section_userdata_type *) (stat_alloc
1520 (sizeof (fat_section_userdata_type))));
1522 ASSERT (get_userdata (sec) == NULL);
1523 get_userdata (sec) = new_data;
1524 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1528 sort_def_symbol (hash_entry, info)
1529 struct bfd_link_hash_entry *hash_entry;
1530 void *info ATTRIBUTE_UNUSED;
1532 if (hash_entry->type == bfd_link_hash_defined
1533 || hash_entry->type == bfd_link_hash_defweak)
1535 struct fat_user_section_struct *ud;
1536 struct map_symbol_def *def;
1538 ud = get_userdata (hash_entry->u.def.section);
1541 /* ??? What do we have to do to initialize this beforehand? */
1542 /* The first time we get here is bfd_abs_section... */
1543 init_map_userdata (0, hash_entry->u.def.section, 0);
1544 ud = get_userdata (hash_entry->u.def.section);
1546 else if (!ud->map_symbol_def_tail)
1547 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1549 def = obstack_alloc (&map_obstack, sizeof *def);
1550 def->entry = hash_entry;
1551 *(ud->map_symbol_def_tail) = def;
1552 ud->map_symbol_def_tail = &def->next;
1557 /* Initialize an output section. */
1560 init_os (lang_output_section_statement_type *s)
1562 if (s->bfd_section != NULL)
1565 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1566 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1568 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1569 if (s->bfd_section == NULL)
1570 s->bfd_section = bfd_make_section (output_bfd, s->name);
1571 if (s->bfd_section == NULL)
1573 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1574 output_bfd->xvec->name, s->name);
1576 s->bfd_section->output_section = s->bfd_section;
1578 /* We initialize an output sections output offset to minus its own
1579 vma to allow us to output a section through itself. */
1580 s->bfd_section->output_offset = 0;
1581 if (!command_line.reduce_memory_overheads)
1583 fat_section_userdata_type *new
1584 = stat_alloc (sizeof (fat_section_userdata_type));
1585 memset (new, 0, sizeof (fat_section_userdata_type));
1586 get_userdata (s->bfd_section) = new;
1590 /* If there is a base address, make sure that any sections it might
1591 mention are initialized. */
1592 if (s->addr_tree != NULL)
1593 exp_init_os (s->addr_tree);
1595 if (s->load_base != NULL)
1596 exp_init_os (s->load_base);
1599 /* Make sure that all output sections mentioned in an expression are
1603 exp_init_os (etree_type *exp)
1605 switch (exp->type.node_class)
1608 exp_init_os (exp->assign.src);
1612 exp_init_os (exp->binary.lhs);
1613 exp_init_os (exp->binary.rhs);
1617 exp_init_os (exp->trinary.cond);
1618 exp_init_os (exp->trinary.lhs);
1619 exp_init_os (exp->trinary.rhs);
1623 exp_init_os (exp->assert_s.child);
1627 exp_init_os (exp->unary.child);
1631 switch (exp->type.node_code)
1637 lang_output_section_statement_type *os;
1639 os = lang_output_section_find (exp->name.name);
1640 if (os != NULL && os->bfd_section == NULL)
1652 section_already_linked (bfd *abfd, asection *sec, void *data)
1654 lang_input_statement_type *entry = data;
1656 /* If we are only reading symbols from this object, then we want to
1657 discard all sections. */
1658 if (entry->just_syms_flag)
1660 bfd_link_just_syms (abfd, sec, &link_info);
1664 if (!(abfd->flags & DYNAMIC))
1665 bfd_section_already_linked (abfd, sec);
1668 /* The wild routines.
1670 These expand statements like *(.text) and foo.o to a list of
1671 explicit actions, like foo.o(.text), bar.o(.text) and
1672 foo.o(.text, .data). */
1674 /* Add SECTION to the output section OUTPUT. Do this by creating a
1675 lang_input_section statement which is placed at PTR. FILE is the
1676 input file which holds SECTION. */
1679 lang_add_section (lang_statement_list_type *ptr,
1681 lang_output_section_statement_type *output,
1682 lang_input_statement_type *file)
1684 flagword flags = section->flags;
1685 bfd_boolean discard;
1687 /* Discard sections marked with SEC_EXCLUDE. */
1688 discard = (flags & SEC_EXCLUDE) != 0;
1690 /* Discard input sections which are assigned to a section named
1691 DISCARD_SECTION_NAME. */
1692 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1695 /* Discard debugging sections if we are stripping debugging
1697 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1698 && (flags & SEC_DEBUGGING) != 0)
1703 if (section->output_section == NULL)
1705 /* This prevents future calls from assigning this section. */
1706 section->output_section = bfd_abs_section_ptr;
1711 if (section->output_section == NULL)
1714 lang_input_section_type *new;
1717 if (output->bfd_section == NULL)
1720 first = ! output->bfd_section->linker_has_input;
1721 output->bfd_section->linker_has_input = 1;
1723 /* Add a section reference to the list. */
1724 new = new_stat (lang_input_section, ptr);
1726 new->section = section;
1728 section->output_section = output->bfd_section;
1730 flags = section->flags;
1732 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1733 to an output section, because we want to be able to include a
1734 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1735 section (I don't know why we want to do this, but we do).
1736 build_link_order in ldwrite.c handles this case by turning
1737 the embedded SEC_NEVER_LOAD section into a fill. */
1739 flags &= ~ SEC_NEVER_LOAD;
1741 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1742 already been processed. One reason to do this is that on pe
1743 format targets, .text$foo sections go into .text and it's odd
1744 to see .text with SEC_LINK_ONCE set. */
1746 if (! link_info.relocatable)
1747 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1749 /* If this is not the first input section, and the SEC_READONLY
1750 flag is not currently set, then don't set it just because the
1751 input section has it set. */
1753 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
1754 flags &= ~ SEC_READONLY;
1756 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1758 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
1759 != (flags & (SEC_MERGE | SEC_STRINGS))
1760 || ((flags & SEC_MERGE)
1761 && output->bfd_section->entsize != section->entsize)))
1763 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1764 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1767 output->bfd_section->flags |= flags;
1769 if (flags & SEC_MERGE)
1770 output->bfd_section->entsize = section->entsize;
1772 /* If SEC_READONLY is not set in the input section, then clear
1773 it from the output section. */
1774 if ((section->flags & SEC_READONLY) == 0)
1775 output->bfd_section->flags &= ~SEC_READONLY;
1777 switch (output->sectype)
1779 case normal_section:
1784 case overlay_section:
1785 output->bfd_section->flags &= ~SEC_ALLOC;
1787 case noload_section:
1788 output->bfd_section->flags &= ~SEC_LOAD;
1789 output->bfd_section->flags |= SEC_NEVER_LOAD;
1793 /* Copy over SEC_SMALL_DATA. */
1794 if (section->flags & SEC_SMALL_DATA)
1795 output->bfd_section->flags |= SEC_SMALL_DATA;
1797 if (section->alignment_power > output->bfd_section->alignment_power)
1798 output->bfd_section->alignment_power = section->alignment_power;
1800 /* If supplied an alignment, then force it. */
1801 if (output->section_alignment != -1)
1802 output->bfd_section->alignment_power = output->section_alignment;
1804 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
1805 && (section->flags & SEC_TIC54X_BLOCK) != 0)
1807 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
1808 /* FIXME: This value should really be obtained from the bfd... */
1809 output->block_value = 128;
1814 /* Compare sections ASEC and BSEC according to SORT. */
1817 compare_section (sort_type sort, asection *asec, asection *bsec)
1826 case by_alignment_name:
1827 ret = (bfd_section_alignment (bsec->owner, bsec)
1828 - bfd_section_alignment (asec->owner, asec));
1834 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1835 bfd_get_section_name (bsec->owner, bsec));
1838 case by_name_alignment:
1839 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1840 bfd_get_section_name (bsec->owner, bsec));
1846 ret = (bfd_section_alignment (bsec->owner, bsec)
1847 - bfd_section_alignment (asec->owner, asec));
1854 /* Handle wildcard sorting. This returns the lang_input_section which
1855 should follow the one we are going to create for SECTION and FILE,
1856 based on the sorting requirements of WILD. It returns NULL if the
1857 new section should just go at the end of the current list. */
1859 static lang_statement_union_type *
1860 wild_sort (lang_wild_statement_type *wild,
1861 struct wildcard_list *sec,
1862 lang_input_statement_type *file,
1865 const char *section_name;
1866 lang_statement_union_type *l;
1868 if (!wild->filenames_sorted
1869 && (sec == NULL || sec->spec.sorted == none))
1872 section_name = bfd_get_section_name (file->the_bfd, section);
1873 for (l = wild->children.head; l != NULL; l = l->header.next)
1875 lang_input_section_type *ls;
1877 if (l->header.type != lang_input_section_enum)
1879 ls = &l->input_section;
1881 /* Sorting by filename takes precedence over sorting by section
1884 if (wild->filenames_sorted)
1886 const char *fn, *ln;
1890 /* The PE support for the .idata section as generated by
1891 dlltool assumes that files will be sorted by the name of
1892 the archive and then the name of the file within the
1895 if (file->the_bfd != NULL
1896 && bfd_my_archive (file->the_bfd) != NULL)
1898 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1903 fn = file->filename;
1907 if (ls->ifile->the_bfd != NULL
1908 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1910 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1915 ln = ls->ifile->filename;
1919 i = strcmp (fn, ln);
1928 fn = file->filename;
1930 ln = ls->ifile->filename;
1932 i = strcmp (fn, ln);
1940 /* Here either the files are not sorted by name, or we are
1941 looking at the sections for this file. */
1943 if (sec != NULL && sec->spec.sorted != none)
1945 if (compare_section (sec->spec.sorted, section,
1954 /* Expand a wild statement for a particular FILE. SECTION may be
1955 NULL, in which case it is a wild card. */
1958 output_section_callback (lang_wild_statement_type *ptr,
1959 struct wildcard_list *sec,
1961 lang_input_statement_type *file,
1964 lang_statement_union_type *before;
1966 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1967 if (unique_section_p (section))
1970 before = wild_sort (ptr, sec, file, section);
1972 /* Here BEFORE points to the lang_input_section which
1973 should follow the one we are about to add. If BEFORE
1974 is NULL, then the section should just go at the end
1975 of the current list. */
1978 lang_add_section (&ptr->children, section,
1979 (lang_output_section_statement_type *) output,
1983 lang_statement_list_type list;
1984 lang_statement_union_type **pp;
1986 lang_list_init (&list);
1987 lang_add_section (&list, section,
1988 (lang_output_section_statement_type *) output,
1991 /* If we are discarding the section, LIST.HEAD will
1993 if (list.head != NULL)
1995 ASSERT (list.head->header.next == NULL);
1997 for (pp = &ptr->children.head;
1999 pp = &(*pp)->header.next)
2000 ASSERT (*pp != NULL);
2002 list.head->header.next = *pp;
2008 /* Check if all sections in a wild statement for a particular FILE
2012 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2013 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2015 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2018 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2019 if (unique_section_p (section))
2022 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2023 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2026 /* This is passed a file name which must have been seen already and
2027 added to the statement tree. We will see if it has been opened
2028 already and had its symbols read. If not then we'll read it. */
2030 static lang_input_statement_type *
2031 lookup_name (const char *name)
2033 lang_input_statement_type *search;
2035 for (search = (lang_input_statement_type *) input_file_chain.head;
2037 search = (lang_input_statement_type *) search->next_real_file)
2039 /* Use the local_sym_name as the name of the file that has
2040 already been loaded as filename might have been transformed
2041 via the search directory lookup mechanism. */
2042 const char * filename = search->local_sym_name;
2044 if (filename == NULL && name == NULL)
2046 if (filename != NULL
2048 && strcmp (filename, name) == 0)
2053 search = new_afile (name, lang_input_file_is_search_file_enum,
2054 default_target, FALSE);
2056 /* If we have already added this file, or this file is not real
2057 (FIXME: can that ever actually happen?) or the name is NULL
2058 (FIXME: can that ever actually happen?) don't add this file. */
2061 || search->filename == NULL)
2064 if (! load_symbols (search, NULL))
2070 /* Save LIST as a list of libraries whose symbols should not be exported. */
2075 struct excluded_lib *next;
2077 static struct excluded_lib *excluded_libs;
2080 add_excluded_libs (const char *list)
2082 const char *p = list, *end;
2086 struct excluded_lib *entry;
2087 end = strpbrk (p, ",:");
2089 end = p + strlen (p);
2090 entry = xmalloc (sizeof (*entry));
2091 entry->next = excluded_libs;
2092 entry->name = xmalloc (end - p + 1);
2093 memcpy (entry->name, p, end - p);
2094 entry->name[end - p] = '\0';
2095 excluded_libs = entry;
2103 check_excluded_libs (bfd *abfd)
2105 struct excluded_lib *lib = excluded_libs;
2109 int len = strlen (lib->name);
2110 const char *filename = lbasename (abfd->filename);
2112 if (strcmp (lib->name, "ALL") == 0)
2114 abfd->no_export = TRUE;
2118 if (strncmp (lib->name, filename, len) == 0
2119 && (filename[len] == '\0'
2120 || (filename[len] == '.' && filename[len + 1] == 'a'
2121 && filename[len + 2] == '\0')))
2123 abfd->no_export = TRUE;
2131 /* Get the symbols for an input file. */
2134 load_symbols (lang_input_statement_type *entry,
2135 lang_statement_list_type *place)
2142 ldfile_open_file (entry);
2144 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2145 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2148 lang_statement_list_type *hold;
2149 bfd_boolean bad_load = TRUE;
2150 bfd_boolean save_ldlang_sysrooted_script;
2152 err = bfd_get_error ();
2154 /* See if the emulation has some special knowledge. */
2155 if (ldemul_unrecognized_file (entry))
2158 if (err == bfd_error_file_ambiguously_recognized)
2162 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2163 einfo (_("%B: matching formats:"), entry->the_bfd);
2164 for (p = matching; *p != NULL; p++)
2168 else if (err != bfd_error_file_not_recognized
2170 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2174 bfd_close (entry->the_bfd);
2175 entry->the_bfd = NULL;
2177 /* Try to interpret the file as a linker script. */
2178 ldfile_open_command_file (entry->filename);
2182 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2183 ldlang_sysrooted_script = entry->sysrooted;
2185 ldfile_assumed_script = TRUE;
2186 parser_input = input_script;
2187 /* We want to use the same -Bdynamic/-Bstatic as the one for
2189 config.dynamic_link = entry->dynamic;
2191 ldfile_assumed_script = FALSE;
2193 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2199 if (ldemul_recognized_file (entry))
2202 /* We don't call ldlang_add_file for an archive. Instead, the
2203 add_symbols entry point will call ldlang_add_file, via the
2204 add_archive_element callback, for each element of the archive
2206 switch (bfd_get_format (entry->the_bfd))
2212 ldlang_add_file (entry);
2213 if (trace_files || trace_file_tries)
2214 info_msg ("%I\n", entry);
2218 check_excluded_libs (entry->the_bfd);
2220 if (entry->whole_archive)
2223 bfd_boolean loaded = TRUE;
2227 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2232 if (! bfd_check_format (member, bfd_object))
2234 einfo (_("%F%B: member %B in archive is not an object\n"),
2235 entry->the_bfd, member);
2239 if (! ((*link_info.callbacks->add_archive_element)
2240 (&link_info, member, "--whole-archive")))
2243 if (! bfd_link_add_symbols (member, &link_info))
2245 einfo (_("%F%B: could not read symbols: %E\n"), member);
2250 entry->loaded = loaded;
2256 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2257 entry->loaded = TRUE;
2259 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2261 return entry->loaded;
2264 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2265 may be NULL, indicating that it is a wildcard. Separate
2266 lang_input_section statements are created for each part of the
2267 expansion; they are added after the wild statement S. OUTPUT is
2268 the output section. */
2271 wild (lang_wild_statement_type *s,
2272 const char *target ATTRIBUTE_UNUSED,
2273 lang_output_section_statement_type *output)
2275 struct wildcard_list *sec;
2277 walk_wild (s, output_section_callback, output);
2279 for (sec = s->section_list; sec != NULL; sec = sec->next)
2281 if (default_common_section != NULL)
2283 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2285 /* Remember the section that common is going to in case we
2286 later get something which doesn't know where to put it. */
2287 default_common_section = output;
2292 /* Return TRUE iff target is the sought target. */
2295 get_target (const bfd_target *target, void *data)
2297 const char *sought = data;
2299 return strcmp (target->name, sought) == 0;
2302 /* Like strcpy() but convert to lower case as well. */
2305 stricpy (char *dest, char *src)
2309 while ((c = *src++) != 0)
2310 *dest++ = TOLOWER (c);
2315 /* Remove the first occurrence of needle (if any) in haystack
2319 strcut (char *haystack, char *needle)
2321 haystack = strstr (haystack, needle);
2327 for (src = haystack + strlen (needle); *src;)
2328 *haystack++ = *src++;
2334 /* Compare two target format name strings.
2335 Return a value indicating how "similar" they are. */
2338 name_compare (char *first, char *second)
2344 copy1 = xmalloc (strlen (first) + 1);
2345 copy2 = xmalloc (strlen (second) + 1);
2347 /* Convert the names to lower case. */
2348 stricpy (copy1, first);
2349 stricpy (copy2, second);
2351 /* Remove size and endian strings from the name. */
2352 strcut (copy1, "big");
2353 strcut (copy1, "little");
2354 strcut (copy2, "big");
2355 strcut (copy2, "little");
2357 /* Return a value based on how many characters match,
2358 starting from the beginning. If both strings are
2359 the same then return 10 * their length. */
2360 for (result = 0; copy1[result] == copy2[result]; result++)
2361 if (copy1[result] == 0)
2373 /* Set by closest_target_match() below. */
2374 static const bfd_target *winner;
2376 /* Scan all the valid bfd targets looking for one that has the endianness
2377 requirement that was specified on the command line, and is the nearest
2378 match to the original output target. */
2381 closest_target_match (const bfd_target *target, void *data)
2383 const bfd_target *original = data;
2385 if (command_line.endian == ENDIAN_BIG
2386 && target->byteorder != BFD_ENDIAN_BIG)
2389 if (command_line.endian == ENDIAN_LITTLE
2390 && target->byteorder != BFD_ENDIAN_LITTLE)
2393 /* Must be the same flavour. */
2394 if (target->flavour != original->flavour)
2397 /* If we have not found a potential winner yet, then record this one. */
2404 /* Oh dear, we now have two potential candidates for a successful match.
2405 Compare their names and choose the better one. */
2406 if (name_compare (target->name, original->name)
2407 > name_compare (winner->name, original->name))
2410 /* Keep on searching until wqe have checked them all. */
2414 /* Return the BFD target format of the first input file. */
2417 get_first_input_target (void)
2419 char *target = NULL;
2421 LANG_FOR_EACH_INPUT_STATEMENT (s)
2423 if (s->header.type == lang_input_statement_enum
2426 ldfile_open_file (s);
2428 if (s->the_bfd != NULL
2429 && bfd_check_format (s->the_bfd, bfd_object))
2431 target = bfd_get_target (s->the_bfd);
2443 lang_get_output_target (void)
2447 /* Has the user told us which output format to use? */
2448 if (output_target != NULL)
2449 return output_target;
2451 /* No - has the current target been set to something other than
2453 if (current_target != default_target)
2454 return current_target;
2456 /* No - can we determine the format of the first input file? */
2457 target = get_first_input_target ();
2461 /* Failed - use the default output target. */
2462 return default_target;
2465 /* Open the output file. */
2468 open_output (const char *name)
2472 output_target = lang_get_output_target ();
2474 /* Has the user requested a particular endianness on the command
2476 if (command_line.endian != ENDIAN_UNSET)
2478 const bfd_target *target;
2479 enum bfd_endian desired_endian;
2481 /* Get the chosen target. */
2482 target = bfd_search_for_target (get_target, (void *) output_target);
2484 /* If the target is not supported, we cannot do anything. */
2487 if (command_line.endian == ENDIAN_BIG)
2488 desired_endian = BFD_ENDIAN_BIG;
2490 desired_endian = BFD_ENDIAN_LITTLE;
2492 /* See if the target has the wrong endianness. This should
2493 not happen if the linker script has provided big and
2494 little endian alternatives, but some scrips don't do
2496 if (target->byteorder != desired_endian)
2498 /* If it does, then see if the target provides
2499 an alternative with the correct endianness. */
2500 if (target->alternative_target != NULL
2501 && (target->alternative_target->byteorder == desired_endian))
2502 output_target = target->alternative_target->name;
2505 /* Try to find a target as similar as possible to
2506 the default target, but which has the desired
2507 endian characteristic. */
2508 bfd_search_for_target (closest_target_match,
2511 /* Oh dear - we could not find any targets that
2512 satisfy our requirements. */
2514 einfo (_("%P: warning: could not find any targets"
2515 " that match endianness requirement\n"));
2517 output_target = winner->name;
2523 output = bfd_openw (name, output_target);
2527 if (bfd_get_error () == bfd_error_invalid_target)
2528 einfo (_("%P%F: target %s not found\n"), output_target);
2530 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2533 delete_output_file_on_failure = TRUE;
2535 if (! bfd_set_format (output, bfd_object))
2536 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2537 if (! bfd_set_arch_mach (output,
2538 ldfile_output_architecture,
2539 ldfile_output_machine))
2540 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2542 link_info.hash = bfd_link_hash_table_create (output);
2543 if (link_info.hash == NULL)
2544 einfo (_("%P%F: can not create link hash table: %E\n"));
2546 bfd_set_gp_size (output, g_switch_value);
2551 ldlang_open_output (lang_statement_union_type *statement)
2553 switch (statement->header.type)
2555 case lang_output_statement_enum:
2556 ASSERT (output_bfd == NULL);
2557 output_bfd = open_output (statement->output_statement.name);
2558 ldemul_set_output_arch ();
2559 if (config.magic_demand_paged && !link_info.relocatable)
2560 output_bfd->flags |= D_PAGED;
2562 output_bfd->flags &= ~D_PAGED;
2563 if (config.text_read_only)
2564 output_bfd->flags |= WP_TEXT;
2566 output_bfd->flags &= ~WP_TEXT;
2567 if (link_info.traditional_format)
2568 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2570 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2573 case lang_target_statement_enum:
2574 current_target = statement->target_statement.target;
2581 /* Convert between addresses in bytes and sizes in octets.
2582 For currently supported targets, octets_per_byte is always a power
2583 of two, so we can use shifts. */
2584 #define TO_ADDR(X) ((X) >> opb_shift)
2585 #define TO_SIZE(X) ((X) << opb_shift)
2587 /* Support the above. */
2588 static unsigned int opb_shift = 0;
2593 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2594 ldfile_output_machine);
2597 while ((x & 1) == 0)
2605 /* Open all the input files. */
2608 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2610 for (; s != NULL; s = s->header.next)
2612 switch (s->header.type)
2614 case lang_constructors_statement_enum:
2615 open_input_bfds (constructor_list.head, force);
2617 case lang_output_section_statement_enum:
2618 open_input_bfds (s->output_section_statement.children.head, force);
2620 case lang_wild_statement_enum:
2621 /* Maybe we should load the file's symbols. */
2622 if (s->wild_statement.filename
2623 && ! wildcardp (s->wild_statement.filename))
2624 lookup_name (s->wild_statement.filename);
2625 open_input_bfds (s->wild_statement.children.head, force);
2627 case lang_group_statement_enum:
2629 struct bfd_link_hash_entry *undefs;
2631 /* We must continually search the entries in the group
2632 until no new symbols are added to the list of undefined
2637 undefs = link_info.hash->undefs_tail;
2638 open_input_bfds (s->group_statement.children.head, TRUE);
2640 while (undefs != link_info.hash->undefs_tail);
2643 case lang_target_statement_enum:
2644 current_target = s->target_statement.target;
2646 case lang_input_statement_enum:
2647 if (s->input_statement.real)
2649 lang_statement_list_type add;
2651 s->input_statement.target = current_target;
2653 /* If we are being called from within a group, and this
2654 is an archive which has already been searched, then
2655 force it to be researched unless the whole archive
2656 has been loaded already. */
2658 && !s->input_statement.whole_archive
2659 && s->input_statement.loaded
2660 && bfd_check_format (s->input_statement.the_bfd,
2662 s->input_statement.loaded = FALSE;
2664 lang_list_init (&add);
2666 if (! load_symbols (&s->input_statement, &add))
2667 config.make_executable = FALSE;
2669 if (add.head != NULL)
2671 *add.tail = s->header.next;
2672 s->header.next = add.head;
2682 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2685 lang_track_definedness (const char *name)
2687 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2688 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2691 /* New-function for the definedness hash table. */
2693 static struct bfd_hash_entry *
2694 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2695 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2696 const char *name ATTRIBUTE_UNUSED)
2698 struct lang_definedness_hash_entry *ret
2699 = (struct lang_definedness_hash_entry *) entry;
2702 ret = (struct lang_definedness_hash_entry *)
2703 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2706 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2708 ret->iteration = -1;
2712 /* Return the iteration when the definition of NAME was last updated. A
2713 value of -1 means that the symbol is not defined in the linker script
2714 or the command line, but may be defined in the linker symbol table. */
2717 lang_symbol_definition_iteration (const char *name)
2719 struct lang_definedness_hash_entry *defentry
2720 = (struct lang_definedness_hash_entry *)
2721 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2723 /* We've already created this one on the presence of DEFINED in the
2724 script, so it can't be NULL unless something is borked elsewhere in
2726 if (defentry == NULL)
2729 return defentry->iteration;
2732 /* Update the definedness state of NAME. */
2735 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2737 struct lang_definedness_hash_entry *defentry
2738 = (struct lang_definedness_hash_entry *)
2739 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2741 /* We don't keep track of symbols not tested with DEFINED. */
2742 if (defentry == NULL)
2745 /* If the symbol was already defined, and not from an earlier statement
2746 iteration, don't update the definedness iteration, because that'd
2747 make the symbol seem defined in the linker script at this point, and
2748 it wasn't; it was defined in some object. If we do anyway, DEFINED
2749 would start to yield false before this point and the construct "sym =
2750 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2752 if (h->type != bfd_link_hash_undefined
2753 && h->type != bfd_link_hash_common
2754 && h->type != bfd_link_hash_new
2755 && defentry->iteration == -1)
2758 defentry->iteration = lang_statement_iteration;
2761 /* Add the supplied name to the symbol table as an undefined reference.
2762 This is a two step process as the symbol table doesn't even exist at
2763 the time the ld command line is processed. First we put the name
2764 on a list, then, once the output file has been opened, transfer the
2765 name to the symbol table. */
2767 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2769 #define ldlang_undef_chain_list_head entry_symbol.next
2772 ldlang_add_undef (const char *const name)
2774 ldlang_undef_chain_list_type *new =
2775 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2777 new->next = ldlang_undef_chain_list_head;
2778 ldlang_undef_chain_list_head = new;
2780 new->name = xstrdup (name);
2782 if (output_bfd != NULL)
2783 insert_undefined (new->name);
2786 /* Insert NAME as undefined in the symbol table. */
2789 insert_undefined (const char *name)
2791 struct bfd_link_hash_entry *h;
2793 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2795 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2796 if (h->type == bfd_link_hash_new)
2798 h->type = bfd_link_hash_undefined;
2799 h->u.undef.abfd = NULL;
2800 bfd_link_add_undef (link_info.hash, h);
2804 /* Run through the list of undefineds created above and place them
2805 into the linker hash table as undefined symbols belonging to the
2809 lang_place_undefineds (void)
2811 ldlang_undef_chain_list_type *ptr;
2813 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2814 insert_undefined (ptr->name);
2817 /* Check for all readonly or some readwrite sections. */
2820 check_input_sections
2821 (lang_statement_union_type *s,
2822 lang_output_section_statement_type *output_section_statement)
2824 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2826 switch (s->header.type)
2828 case lang_wild_statement_enum:
2829 walk_wild (&s->wild_statement, check_section_callback,
2830 output_section_statement);
2831 if (! output_section_statement->all_input_readonly)
2834 case lang_constructors_statement_enum:
2835 check_input_sections (constructor_list.head,
2836 output_section_statement);
2837 if (! output_section_statement->all_input_readonly)
2840 case lang_group_statement_enum:
2841 check_input_sections (s->group_statement.children.head,
2842 output_section_statement);
2843 if (! output_section_statement->all_input_readonly)
2852 /* Update wildcard statements if needed. */
2855 update_wild_statements (lang_statement_union_type *s)
2857 struct wildcard_list *sec;
2859 switch (sort_section)
2869 for (; s != NULL; s = s->header.next)
2871 switch (s->header.type)
2876 case lang_wild_statement_enum:
2877 sec = s->wild_statement.section_list;
2880 switch (sec->spec.sorted)
2883 sec->spec.sorted = sort_section;
2886 if (sort_section == by_alignment)
2887 sec->spec.sorted = by_name_alignment;
2890 if (sort_section == by_name)
2891 sec->spec.sorted = by_alignment_name;
2899 case lang_constructors_statement_enum:
2900 update_wild_statements (constructor_list.head);
2903 case lang_output_section_statement_enum:
2904 update_wild_statements
2905 (s->output_section_statement.children.head);
2908 case lang_group_statement_enum:
2909 update_wild_statements (s->group_statement.children.head);
2917 /* Open input files and attach to output sections. */
2920 map_input_to_output_sections
2921 (lang_statement_union_type *s, const char *target,
2922 lang_output_section_statement_type *os)
2924 for (; s != NULL; s = s->header.next)
2926 switch (s->header.type)
2928 case lang_wild_statement_enum:
2929 wild (&s->wild_statement, target, os);
2931 case lang_constructors_statement_enum:
2932 map_input_to_output_sections (constructor_list.head,
2936 case lang_output_section_statement_enum:
2937 if (s->output_section_statement.constraint)
2939 if (s->output_section_statement.constraint == -1)
2941 s->output_section_statement.all_input_readonly = TRUE;
2942 check_input_sections (s->output_section_statement.children.head,
2943 &s->output_section_statement);
2944 if ((s->output_section_statement.all_input_readonly
2945 && s->output_section_statement.constraint == ONLY_IF_RW)
2946 || (!s->output_section_statement.all_input_readonly
2947 && s->output_section_statement.constraint == ONLY_IF_RO))
2949 s->output_section_statement.constraint = -1;
2954 map_input_to_output_sections (s->output_section_statement.children.head,
2956 &s->output_section_statement);
2958 case lang_output_statement_enum:
2960 case lang_target_statement_enum:
2961 target = s->target_statement.target;
2963 case lang_group_statement_enum:
2964 map_input_to_output_sections (s->group_statement.children.head,
2968 case lang_data_statement_enum:
2969 /* Make sure that any sections mentioned in the expression
2971 exp_init_os (s->data_statement.exp);
2972 if (os != NULL && os->bfd_section == NULL)
2974 /* The output section gets contents, and then we inspect for
2975 any flags set in the input script which override any ALLOC. */
2976 os->bfd_section->flags |= SEC_HAS_CONTENTS;
2977 if (!(os->flags & SEC_NEVER_LOAD))
2978 os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2980 case lang_fill_statement_enum:
2981 case lang_input_section_enum:
2982 case lang_object_symbols_statement_enum:
2983 case lang_reloc_statement_enum:
2984 case lang_padding_statement_enum:
2985 case lang_input_statement_enum:
2986 if (os != NULL && os->bfd_section == NULL)
2989 case lang_assignment_statement_enum:
2990 if (os != NULL && os->bfd_section == NULL)
2993 /* Make sure that any sections mentioned in the assignment
2995 exp_init_os (s->assignment_statement.exp);
2997 case lang_afile_asection_pair_statement_enum:
3000 case lang_address_statement_enum:
3001 /* Mark the specified section with the supplied address.
3003 If this section was actually a segment marker, then the
3004 directive is ignored if the linker script explicitly
3005 processed the segment marker. Originally, the linker
3006 treated segment directives (like -Ttext on the
3007 command-line) as section directives. We honor the
3008 section directive semantics for backwards compatibilty;
3009 linker scripts that do not specifically check for
3010 SEGMENT_START automatically get the old semantics. */
3011 if (!s->address_statement.segment
3012 || !s->address_statement.segment->used)
3014 lang_output_section_statement_type *aos
3015 = (lang_output_section_statement_lookup
3016 (s->address_statement.section_name));
3018 if (aos->bfd_section == NULL)
3020 aos->addr_tree = s->address_statement.address;
3027 /* An output section might have been removed after its statement was
3028 added. For example, ldemul_before_allocation can remove dynamic
3029 sections if they turn out to be not needed. Clean them up here. */
3032 strip_excluded_output_sections (void)
3034 lang_output_section_statement_type *os;
3036 for (os = &lang_output_section_statement.head->output_section_statement;
3042 if (os->constraint == -1)
3044 s = os->bfd_section;
3045 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
3049 os->bfd_section = NULL;
3051 for (p = &output_bfd->sections; *p; p = &(*p)->next)
3054 bfd_section_list_remove (output_bfd, p);
3055 output_bfd->section_count--;
3063 print_output_section_statement
3064 (lang_output_section_statement_type *output_section_statement)
3066 asection *section = output_section_statement->bfd_section;
3069 if (output_section_statement != abs_output_section)
3071 minfo ("\n%s", output_section_statement->name);
3073 if (section != NULL)
3075 print_dot = section->vma;
3077 len = strlen (output_section_statement->name);
3078 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3083 while (len < SECTION_NAME_MAP_LENGTH)
3089 minfo ("0x%V %W", section->vma, section->size);
3091 if (output_section_statement->load_base != NULL)
3095 addr = exp_get_abs_int (output_section_statement->load_base, 0,
3096 "load base", lang_final_phase_enum);
3097 minfo (_(" load address 0x%V"), addr);
3104 print_statement_list (output_section_statement->children.head,
3105 output_section_statement);
3109 print_assignment (lang_assignment_statement_type *assignment,
3110 lang_output_section_statement_type *output_section)
3115 etree_value_type result;
3117 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3120 if (assignment->exp->type.node_class == etree_assert)
3123 tree = assignment->exp->assert_s.child;
3127 const char *dst = assignment->exp->assign.dst;
3128 is_dot = dst[0] == '.' && dst[1] == 0;
3129 tree = assignment->exp->assign.src;
3132 result = exp_fold_tree (tree, output_section, lang_final_phase_enum,
3133 print_dot, &print_dot);
3138 value = result.value + result.section->bfd_section->vma;
3140 minfo ("0x%V", value);
3153 exp_print_tree (assignment->exp);
3158 print_input_statement (lang_input_statement_type *statm)
3160 if (statm->filename != NULL)
3162 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3166 /* Print all symbols defined in a particular section. This is called
3167 via bfd_link_hash_traverse, or by print_all_symbols. */
3170 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3172 asection *sec = ptr;
3174 if ((hash_entry->type == bfd_link_hash_defined
3175 || hash_entry->type == bfd_link_hash_defweak)
3176 && sec == hash_entry->u.def.section)
3180 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3183 (hash_entry->u.def.value
3184 + hash_entry->u.def.section->output_offset
3185 + hash_entry->u.def.section->output_section->vma));
3187 minfo (" %T\n", hash_entry->root.string);
3194 print_all_symbols (sec)
3197 struct fat_user_section_struct *ud = get_userdata (sec);
3198 struct map_symbol_def *def;
3203 *ud->map_symbol_def_tail = 0;
3204 for (def = ud->map_symbol_def_head; def; def = def->next)
3205 print_one_symbol (def->entry, sec);
3208 /* Print information about an input section to the map file. */
3211 print_input_section (lang_input_section_type *in)
3213 asection *i = in->section;
3214 bfd_size_type size = i->size;
3223 minfo ("%s", i->name);
3225 len = 1 + strlen (i->name);
3226 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3231 while (len < SECTION_NAME_MAP_LENGTH)
3237 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
3238 addr = i->output_section->vma + i->output_offset;
3245 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3247 if (size != i->rawsize && i->rawsize != 0)
3249 len = SECTION_NAME_MAP_LENGTH + 3;
3261 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3264 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
3266 if (command_line.reduce_memory_overheads)
3267 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3269 print_all_symbols (i);
3271 print_dot = addr + TO_ADDR (size);
3277 print_fill_statement (lang_fill_statement_type *fill)
3281 fputs (" FILL mask 0x", config.map_file);
3282 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3283 fprintf (config.map_file, "%02x", *p);
3284 fputs ("\n", config.map_file);
3288 print_data_statement (lang_data_statement_type *data)
3296 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3299 addr = data->output_vma;
3300 if (data->output_section != NULL)
3301 addr += data->output_section->vma;
3329 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3331 if (data->exp->type.node_class != etree_value)
3334 exp_print_tree (data->exp);
3339 print_dot = addr + TO_ADDR (size);
3342 /* Print an address statement. These are generated by options like
3346 print_address_statement (lang_address_statement_type *address)
3348 minfo (_("Address of section %s set to "), address->section_name);
3349 exp_print_tree (address->address);
3353 /* Print a reloc statement. */
3356 print_reloc_statement (lang_reloc_statement_type *reloc)
3363 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3366 addr = reloc->output_vma;
3367 if (reloc->output_section != NULL)
3368 addr += reloc->output_section->vma;
3370 size = bfd_get_reloc_size (reloc->howto);
3372 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3374 if (reloc->name != NULL)
3375 minfo ("%s+", reloc->name);
3377 minfo ("%s+", reloc->section->name);
3379 exp_print_tree (reloc->addend_exp);
3383 print_dot = addr + TO_ADDR (size);
3387 print_padding_statement (lang_padding_statement_type *s)
3395 len = sizeof " *fill*" - 1;
3396 while (len < SECTION_NAME_MAP_LENGTH)
3402 addr = s->output_offset;
3403 if (s->output_section != NULL)
3404 addr += s->output_section->vma;
3405 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3407 if (s->fill->size != 0)
3411 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3412 fprintf (config.map_file, "%02x", *p);
3417 print_dot = addr + TO_ADDR (s->size);
3421 print_wild_statement (lang_wild_statement_type *w,
3422 lang_output_section_statement_type *os)
3424 struct wildcard_list *sec;
3428 if (w->filenames_sorted)
3430 if (w->filename != NULL)
3431 minfo ("%s", w->filename);
3434 if (w->filenames_sorted)
3438 for (sec = w->section_list; sec; sec = sec->next)
3440 if (sec->spec.sorted)
3442 if (sec->spec.exclude_name_list != NULL)
3445 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3446 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3447 minfo (" %s", tmp->name);
3450 if (sec->spec.name != NULL)
3451 minfo ("%s", sec->spec.name);
3454 if (sec->spec.sorted)
3463 print_statement_list (w->children.head, os);
3466 /* Print a group statement. */
3469 print_group (lang_group_statement_type *s,
3470 lang_output_section_statement_type *os)
3472 fprintf (config.map_file, "START GROUP\n");
3473 print_statement_list (s->children.head, os);
3474 fprintf (config.map_file, "END GROUP\n");
3477 /* Print the list of statements in S.
3478 This can be called for any statement type. */
3481 print_statement_list (lang_statement_union_type *s,
3482 lang_output_section_statement_type *os)
3486 print_statement (s, os);
3491 /* Print the first statement in statement list S.
3492 This can be called for any statement type. */
3495 print_statement (lang_statement_union_type *s,
3496 lang_output_section_statement_type *os)
3498 switch (s->header.type)
3501 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3504 case lang_constructors_statement_enum:
3505 if (constructor_list.head != NULL)
3507 if (constructors_sorted)
3508 minfo (" SORT (CONSTRUCTORS)\n");
3510 minfo (" CONSTRUCTORS\n");
3511 print_statement_list (constructor_list.head, os);
3514 case lang_wild_statement_enum:
3515 print_wild_statement (&s->wild_statement, os);
3517 case lang_address_statement_enum:
3518 print_address_statement (&s->address_statement);
3520 case lang_object_symbols_statement_enum:
3521 minfo (" CREATE_OBJECT_SYMBOLS\n");
3523 case lang_fill_statement_enum:
3524 print_fill_statement (&s->fill_statement);
3526 case lang_data_statement_enum:
3527 print_data_statement (&s->data_statement);
3529 case lang_reloc_statement_enum:
3530 print_reloc_statement (&s->reloc_statement);
3532 case lang_input_section_enum:
3533 print_input_section (&s->input_section);
3535 case lang_padding_statement_enum:
3536 print_padding_statement (&s->padding_statement);
3538 case lang_output_section_statement_enum:
3539 print_output_section_statement (&s->output_section_statement);
3541 case lang_assignment_statement_enum:
3542 print_assignment (&s->assignment_statement, os);
3544 case lang_target_statement_enum:
3545 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3547 case lang_output_statement_enum:
3548 minfo ("OUTPUT(%s", s->output_statement.name);
3549 if (output_target != NULL)
3550 minfo (" %s", output_target);
3553 case lang_input_statement_enum:
3554 print_input_statement (&s->input_statement);
3556 case lang_group_statement_enum:
3557 print_group (&s->group_statement, os);
3559 case lang_afile_asection_pair_statement_enum:
3566 print_statements (void)
3568 print_statement_list (statement_list.head, abs_output_section);
3571 /* Print the first N statements in statement list S to STDERR.
3572 If N == 0, nothing is printed.
3573 If N < 0, the entire list is printed.
3574 Intended to be called from GDB. */
3577 dprint_statement (lang_statement_union_type *s, int n)
3579 FILE *map_save = config.map_file;
3581 config.map_file = stderr;
3584 print_statement_list (s, abs_output_section);
3587 while (s && --n >= 0)
3589 print_statement (s, abs_output_section);
3594 config.map_file = map_save;
3598 insert_pad (lang_statement_union_type **ptr,
3600 unsigned int alignment_needed,
3601 asection *output_section,
3604 static fill_type zero_fill = { 1, { 0 } };
3605 lang_statement_union_type *pad;
3607 pad = ((lang_statement_union_type *)
3608 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
3609 if (ptr != &statement_list.head
3610 && pad->header.type == lang_padding_statement_enum
3611 && pad->padding_statement.output_section == output_section)
3613 /* Use the existing pad statement. The above test on output
3614 section is probably redundant, but it doesn't hurt to check. */
3618 /* Make a new padding statement, linked into existing chain. */
3619 pad = stat_alloc (sizeof (lang_padding_statement_type));
3620 pad->header.next = *ptr;
3622 pad->header.type = lang_padding_statement_enum;
3623 pad->padding_statement.output_section = output_section;
3626 pad->padding_statement.fill = fill;
3628 pad->padding_statement.output_offset = dot - output_section->vma;
3629 pad->padding_statement.size = alignment_needed;
3630 output_section->size += alignment_needed;
3633 /* Work out how much this section will move the dot point. */
3637 (lang_statement_union_type **this_ptr,
3638 lang_output_section_statement_type *output_section_statement,
3642 lang_input_section_type *is = &((*this_ptr)->input_section);
3643 asection *i = is->section;
3645 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
3647 unsigned int alignment_needed;
3650 /* Align this section first to the input sections requirement,
3651 then to the output section's requirement. If this alignment
3652 is greater than any seen before, then record it too. Perform
3653 the alignment by inserting a magic 'padding' statement. */
3655 if (output_section_statement->subsection_alignment != -1)
3656 i->alignment_power = output_section_statement->subsection_alignment;
3658 o = output_section_statement->bfd_section;
3659 if (o->alignment_power < i->alignment_power)
3660 o->alignment_power = i->alignment_power;
3662 alignment_needed = align_power (dot, i->alignment_power) - dot;
3664 if (alignment_needed != 0)
3666 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
3667 dot += alignment_needed;
3670 /* Remember where in the output section this input section goes. */
3672 i->output_offset = dot - o->vma;
3674 /* Mark how big the output section must be to contain this now. */
3675 dot += TO_ADDR (i->size);
3676 o->size = TO_SIZE (dot - o->vma);
3680 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
3686 #define IGNORE_SECTION(s) \
3687 ((s->flags & SEC_NEVER_LOAD) != 0 \
3688 || (s->flags & SEC_ALLOC) == 0 \
3689 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
3690 && (s->flags & SEC_LOAD) == 0))
3692 /* Check to see if any allocated sections overlap with other allocated
3693 sections. This can happen if a linker script specifies the output
3694 section addresses of the two sections. */
3697 lang_check_section_addresses (void)
3701 /* Scan all sections in the output list. */
3702 for (s = output_bfd->sections; s != NULL; s = s->next)
3706 /* Ignore sections which are not loaded or which have no contents. */
3707 if (IGNORE_SECTION (s) || s->size == 0)
3710 /* Once we reach section 's' stop our seach. This prevents two
3711 warning messages from being produced, one for 'section A overlaps
3712 section B' and one for 'section B overlaps section A'. */
3713 for (os = output_bfd->sections; os != s; os = os->next)
3720 /* Only consider loadable sections with real contents. */
3721 if (IGNORE_SECTION (os) || os->size == 0)
3724 /* We must check the sections' LMA addresses not their
3725 VMA addresses because overlay sections can have
3726 overlapping VMAs but they must have distinct LMAs. */
3727 s_start = bfd_section_lma (output_bfd, s);
3728 os_start = bfd_section_lma (output_bfd, os);
3729 s_end = s_start + TO_ADDR (s->size) - 1;
3730 os_end = os_start + TO_ADDR (os->size) - 1;
3732 /* Look for an overlap. */
3733 if ((s_end < os_start) || (s_start > os_end))
3737 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
3738 s->name, s_start, s_end, os->name, os_start, os_end);
3740 /* Once we have found one overlap for this section,
3741 stop looking for others. */
3747 /* Make sure the new address is within the region. We explicitly permit the
3748 current address to be at the exact end of the region when the address is
3749 non-zero, in case the region is at the end of addressable memory and the
3750 calculation wraps around. */
3753 os_region_check (lang_output_section_statement_type *os,
3754 lang_memory_region_type *region,
3758 if ((region->current < region->origin
3759 || (region->current - region->origin > region->length))
3760 && ((region->current != region->origin + region->length)
3765 einfo (_("%X%P: address 0x%v of %B section %s"
3766 " is not within region %s\n"),
3768 os->bfd_section->owner,
3769 os->bfd_section->name,
3774 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3776 os->bfd_section->owner,
3777 os->bfd_section->name);
3779 /* Reset the region pointer. */
3780 region->current = region->origin;
3784 /* Set the sizes for all the output sections. */
3787 lang_size_sections_1
3788 (lang_statement_union_type *s,
3789 lang_output_section_statement_type *output_section_statement,
3790 lang_statement_union_type **prev,
3794 bfd_boolean check_regions)
3796 /* Size up the sections from their constituent parts. */
3797 for (; s != NULL; s = s->header.next)
3799 switch (s->header.type)
3801 case lang_output_section_statement_enum:
3804 lang_output_section_statement_type *os;
3806 os = &s->output_section_statement;
3807 if (os->bfd_section == NULL)
3808 /* This section was never actually created. */
3811 /* If this is a COFF shared library section, use the size and
3812 address from the input section. FIXME: This is COFF
3813 specific; it would be cleaner if there were some other way
3814 to do this, but nothing simple comes to mind. */
3815 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
3816 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
3817 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3821 if (os->children.head == NULL
3822 || os->children.head->header.next != NULL
3823 || (os->children.head->header.type
3824 != lang_input_section_enum))
3825 einfo (_("%P%X: Internal error on COFF shared library"
3826 " section %s\n"), os->name);
3828 input = os->children.head->input_section.section;
3829 bfd_set_section_vma (os->bfd_section->owner,
3831 bfd_section_vma (input->owner, input));
3832 os->bfd_section->size = input->size;
3836 if (bfd_is_abs_section (os->bfd_section))
3838 /* No matter what happens, an abs section starts at zero. */
3839 ASSERT (os->bfd_section->vma == 0);
3843 if (os->addr_tree == NULL)
3845 /* No address specified for this section, get one
3846 from the region specification. */
3847 if (os->region == NULL
3848 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3849 && os->region->name[0] == '*'
3850 && strcmp (os->region->name,
3851 DEFAULT_MEMORY_REGION) == 0))
3853 os->region = lang_memory_default (os->bfd_section);
3856 /* If a loadable section is using the default memory
3857 region, and some non default memory regions were
3858 defined, issue an error message. */
3859 if (!IGNORE_SECTION (os->bfd_section)
3860 && ! link_info.relocatable
3862 && strcmp (os->region->name,
3863 DEFAULT_MEMORY_REGION) == 0
3864 && lang_memory_region_list != NULL
3865 && (strcmp (lang_memory_region_list->name,
3866 DEFAULT_MEMORY_REGION) != 0
3867 || lang_memory_region_list->next != NULL))
3869 /* By default this is an error rather than just a
3870 warning because if we allocate the section to the
3871 default memory region we can end up creating an
3872 excessively large binary, or even seg faulting when
3873 attempting to perform a negative seek. See
3874 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3875 for an example of this. This behaviour can be
3876 overridden by the using the --no-check-sections
3878 if (command_line.check_section_addresses)
3879 einfo (_("%P%F: error: no memory region specified"
3880 " for loadable section `%s'\n"),
3881 bfd_get_section_name (output_bfd,
3884 einfo (_("%P: warning: no memory region specified"
3885 " for loadable section `%s'\n"),
3886 bfd_get_section_name (output_bfd,
3890 dot = os->region->current;
3892 if (os->section_alignment == -1)
3897 dot = align_power (dot,
3898 os->bfd_section->alignment_power);
3900 if (dot != olddot && config.warn_section_align)
3901 einfo (_("%P: warning: changing start of section"
3902 " %s by %u bytes\n"),
3903 os->name, (unsigned int) (dot - olddot));
3911 r = exp_fold_tree (os->addr_tree,
3913 lang_allocating_phase_enum,
3918 einfo (_("%F%S: non constant or forward reference"
3919 " address expression for section %s\n"),
3922 dot = r.value + r.section->bfd_section->vma;
3925 /* The section starts here.
3926 First, align to what the section needs. */
3928 if (os->section_alignment != -1)
3929 dot = align_power (dot, os->section_alignment);
3931 bfd_set_section_vma (0, os->bfd_section, dot);
3933 os->bfd_section->output_offset = 0;
3936 lang_size_sections_1 (os->children.head, os, &os->children.head,
3937 os->fill, dot, relax, check_regions);
3939 /* Put the section within the requested block size, or
3940 align at the block boundary. */
3941 after = ((os->bfd_section->vma
3942 + TO_ADDR (os->bfd_section->size)
3943 + os->block_value - 1)
3944 & - (bfd_vma) os->block_value);
3946 if (bfd_is_abs_section (os->bfd_section))
3947 ASSERT (after == os->bfd_section->vma);
3949 os->bfd_section->size
3950 = TO_SIZE (after - os->bfd_section->vma);
3952 dot = os->bfd_section->vma;
3953 /* .tbss sections effectively have zero size. */
3954 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3955 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3956 || link_info.relocatable)
3957 dot += TO_ADDR (os->bfd_section->size);
3961 if (os->update_dot_tree != 0)
3962 exp_fold_tree (os->update_dot_tree, abs_output_section,
3963 lang_allocating_phase_enum, dot, &dot);
3965 /* Update dot in the region ?
3966 We only do this if the section is going to be allocated,
3967 since unallocated sections do not contribute to the region's
3968 overall size in memory.
3970 If the SEC_NEVER_LOAD bit is not set, it will affect the
3971 addresses of sections after it. We have to update
3973 if (os->region != NULL
3974 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3975 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3977 os->region->current = dot;
3980 /* Make sure the new address is within the region. */
3981 os_region_check (os, os->region, os->addr_tree,
3982 os->bfd_section->vma);
3984 /* If there's no load address specified, use the run
3985 region as the load region. */
3986 if (os->lma_region == NULL && os->load_base == NULL)
3987 os->lma_region = os->region;
3989 if (os->lma_region != NULL && os->lma_region != os->region)
3991 /* Set load_base, which will be handled later. */
3992 os->load_base = exp_intop (os->lma_region->current);
3993 os->lma_region->current +=
3994 TO_ADDR (os->bfd_section->size);
3996 os_region_check (os, os->lma_region, NULL,
3997 os->bfd_section->lma);
4003 case lang_constructors_statement_enum:
4004 dot = lang_size_sections_1 (constructor_list.head,
4005 output_section_statement,
4006 &s->wild_statement.children.head,
4007 fill, dot, relax, check_regions);
4010 case lang_data_statement_enum:
4012 unsigned int size = 0;
4014 s->data_statement.output_vma =
4015 dot - output_section_statement->bfd_section->vma;
4016 s->data_statement.output_section =
4017 output_section_statement->bfd_section;
4019 /* We might refer to provided symbols in the expression, and
4020 need to mark them as needed. */
4021 exp_fold_tree (s->data_statement.exp, abs_output_section,
4022 lang_allocating_phase_enum, dot, &dot);
4024 switch (s->data_statement.type)
4042 if (size < TO_SIZE ((unsigned) 1))
4043 size = TO_SIZE ((unsigned) 1);
4044 dot += TO_ADDR (size);
4045 output_section_statement->bfd_section->size += size;
4049 case lang_reloc_statement_enum:
4053 s->reloc_statement.output_vma =
4054 dot - output_section_statement->bfd_section->vma;
4055 s->reloc_statement.output_section =
4056 output_section_statement->bfd_section;
4057 size = bfd_get_reloc_size (s->reloc_statement.howto);
4058 dot += TO_ADDR (size);
4059 output_section_statement->bfd_section->size += size;
4063 case lang_wild_statement_enum:
4065 dot = lang_size_sections_1 (s->wild_statement.children.head,
4066 output_section_statement,
4067 &s->wild_statement.children.head,
4068 fill, dot, relax, check_regions);
4072 case lang_object_symbols_statement_enum:
4073 link_info.create_object_symbols_section =
4074 output_section_statement->bfd_section;
4076 case lang_output_statement_enum:
4077 case lang_target_statement_enum:
4079 case lang_input_section_enum:
4083 i = (*prev)->input_section.section;
4088 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4089 einfo (_("%P%F: can't relax section: %E\n"));
4093 dot = size_input_section (prev, output_section_statement,
4094 output_section_statement->fill, dot);
4097 case lang_input_statement_enum:
4099 case lang_fill_statement_enum:
4100 s->fill_statement.output_section =
4101 output_section_statement->bfd_section;
4103 fill = s->fill_statement.fill;
4105 case lang_assignment_statement_enum:
4107 bfd_vma newdot = dot;
4109 exp_fold_tree (s->assignment_statement.exp,
4110 output_section_statement,
4111 lang_allocating_phase_enum,
4117 if (output_section_statement == abs_output_section)
4119 /* If we don't have an output section, then just adjust
4120 the default memory address. */
4121 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4122 FALSE)->current = newdot;
4126 /* Insert a pad after this statement. We can't
4127 put the pad before when relaxing, in case the
4128 assignment references dot. */
4129 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4130 output_section_statement->bfd_section, dot);
4132 /* Don't neuter the pad below when relaxing. */
4136 /* If dot is advanced, this implies that the section should
4137 have space allocated to it, unless the user has explicitly
4138 stated that the section should never be loaded. */
4139 if (!(output_section_statement->flags
4140 & (SEC_NEVER_LOAD | SEC_ALLOC)))
4141 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4148 case lang_padding_statement_enum:
4149 /* If this is the first time lang_size_sections is called,
4150 we won't have any padding statements. If this is the
4151 second or later passes when relaxing, we should allow
4152 padding to shrink. If padding is needed on this pass, it
4153 will be added back in. */
4154 s->padding_statement.size = 0;
4156 /* Make sure output_offset is valid. If relaxation shrinks
4157 the section and this pad isn't needed, it's possible to
4158 have output_offset larger than the final size of the
4159 section. bfd_set_section_contents will complain even for
4160 a pad size of zero. */
4161 s->padding_statement.output_offset
4162 = dot - output_section_statement->bfd_section->vma;
4165 case lang_group_statement_enum:
4166 dot = lang_size_sections_1 (s->group_statement.children.head,
4167 output_section_statement,
4168 &s->group_statement.children.head,
4169 fill, dot, relax, check_regions);
4176 /* We can only get here when relaxing is turned on. */
4177 case lang_address_statement_enum:
4180 prev = &s->header.next;
4187 (lang_statement_union_type *s,
4188 lang_output_section_statement_type *output_section_statement,
4189 lang_statement_union_type **prev,
4193 bfd_boolean check_regions)
4197 /* Callers of exp_fold_tree need to increment this. */
4198 lang_statement_iteration++;
4200 exp_data_seg.phase = exp_dataseg_none;
4201 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
4202 dot, relax, check_regions);
4203 if (exp_data_seg.phase == exp_dataseg_end_seen
4204 && link_info.relro && exp_data_seg.relro_end)
4206 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4207 to put exp_data_seg.relro on a (common) page boundary. */
4208 bfd_vma old_min_base, relro_end, maxpage;
4210 exp_data_seg.phase = exp_dataseg_relro_adjust;
4211 old_min_base = exp_data_seg.min_base;
4212 maxpage = exp_data_seg.maxpagesize;
4213 exp_data_seg.base += (-exp_data_seg.relro_end
4214 & (exp_data_seg.pagesize - 1));
4215 /* Compute the expected PT_GNU_RELRO segment end. */
4216 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
4217 & ~(exp_data_seg.pagesize - 1);
4218 if (old_min_base + maxpage < exp_data_seg.base)
4220 exp_data_seg.base -= maxpage;
4221 relro_end -= maxpage;
4223 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
4224 dot, relax, check_regions);
4225 if (exp_data_seg.relro_end > relro_end)
4227 /* The alignment of sections between DATA_SEGMENT_ALIGN
4228 and DATA_SEGMENT_RELRO_END caused huge padding to be
4229 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4231 unsigned int max_alignment_power = 0;
4233 /* Find maximum alignment power of sections between
4234 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4235 for (sec = output_bfd->sections; sec; sec = sec->next)
4236 if (sec->vma >= exp_data_seg.base
4237 && sec->vma < exp_data_seg.relro_end
4238 && sec->alignment_power > max_alignment_power)
4239 max_alignment_power = sec->alignment_power;
4241 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
4243 if (exp_data_seg.base - (1 << max_alignment_power)
4245 exp_data_seg.base += exp_data_seg.pagesize;
4246 exp_data_seg.base -= (1 << max_alignment_power);
4247 result = lang_size_sections_1 (s, output_section_statement,
4248 prev, fill, dot, relax,
4252 link_info.relro_start = exp_data_seg.base;
4253 link_info.relro_end = exp_data_seg.relro_end;
4255 else if (exp_data_seg.phase == exp_dataseg_end_seen)
4257 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4258 a page could be saved in the data segment. */
4259 bfd_vma first, last;
4261 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
4262 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
4264 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
4265 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
4266 && first + last <= exp_data_seg.pagesize)
4268 exp_data_seg.phase = exp_dataseg_adjust;
4269 lang_statement_iteration++;
4270 result = lang_size_sections_1 (s, output_section_statement, prev,
4271 fill, dot, relax, check_regions);
4278 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4281 lang_do_assignments_1
4282 (lang_statement_union_type *s,
4283 lang_output_section_statement_type *output_section_statement,
4287 for (; s != NULL; s = s->header.next)
4289 switch (s->header.type)
4291 case lang_constructors_statement_enum:
4292 dot = lang_do_assignments_1 (constructor_list.head,
4293 output_section_statement,
4298 case lang_output_section_statement_enum:
4300 lang_output_section_statement_type *os;
4302 os = &(s->output_section_statement);
4303 if (os->bfd_section != NULL)
4305 dot = os->bfd_section->vma;
4306 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
4307 /* .tbss sections effectively have zero size. */
4308 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4309 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4310 || link_info.relocatable)
4311 dot += TO_ADDR (os->bfd_section->size);
4315 /* If nothing has been placed into the output section then
4316 it won't have a bfd_section. */
4317 if (os->bfd_section)
4319 os->bfd_section->lma
4320 = exp_get_abs_int (os->load_base, 0, "load base",
4321 lang_final_phase_enum);
4326 case lang_wild_statement_enum:
4328 dot = lang_do_assignments_1 (s->wild_statement.children.head,
4329 output_section_statement,
4334 case lang_object_symbols_statement_enum:
4335 case lang_output_statement_enum:
4336 case lang_target_statement_enum:
4338 case lang_data_statement_enum:
4340 etree_value_type value;
4342 value = exp_fold_tree (s->data_statement.exp,
4344 lang_final_phase_enum, dot, &dot);
4346 einfo (_("%F%P: invalid data statement\n"));
4347 s->data_statement.value
4348 = value.value + value.section->bfd_section->vma;
4352 switch (s->data_statement.type)
4370 if (size < TO_SIZE ((unsigned) 1))
4371 size = TO_SIZE ((unsigned) 1);
4372 dot += TO_ADDR (size);
4376 case lang_reloc_statement_enum:
4378 etree_value_type value;
4380 value = exp_fold_tree (s->reloc_statement.addend_exp,
4382 lang_final_phase_enum, dot, &dot);
4383 s->reloc_statement.addend_value = value.value;
4385 einfo (_("%F%P: invalid reloc statement\n"));
4387 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4390 case lang_input_section_enum:
4392 asection *in = s->input_section.section;
4394 if ((in->flags & SEC_EXCLUDE) == 0)
4395 dot += TO_ADDR (in->size);
4399 case lang_input_statement_enum:
4401 case lang_fill_statement_enum:
4402 fill = s->fill_statement.fill;
4404 case lang_assignment_statement_enum:
4406 exp_fold_tree (s->assignment_statement.exp,
4407 output_section_statement,
4408 lang_final_phase_enum,
4414 case lang_padding_statement_enum:
4415 dot += TO_ADDR (s->padding_statement.size);
4418 case lang_group_statement_enum:
4419 dot = lang_do_assignments_1 (s->group_statement.children.head,
4420 output_section_statement,
4428 case lang_address_statement_enum:
4438 (lang_statement_union_type *s,
4439 lang_output_section_statement_type *output_section_statement,
4443 /* Callers of exp_fold_tree need to increment this. */
4444 lang_statement_iteration++;
4445 lang_do_assignments_1 (s, output_section_statement, fill, dot);
4448 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4449 operator .startof. (section_name), it produces an undefined symbol
4450 .startof.section_name. Similarly, when it sees
4451 .sizeof. (section_name), it produces an undefined symbol
4452 .sizeof.section_name. For all the output sections, we look for
4453 such symbols, and set them to the correct value. */
4456 lang_set_startof (void)
4460 if (link_info.relocatable)
4463 for (s = output_bfd->sections; s != NULL; s = s->next)
4465 const char *secname;
4467 struct bfd_link_hash_entry *h;
4469 secname = bfd_get_section_name (output_bfd, s);
4470 buf = xmalloc (10 + strlen (secname));
4472 sprintf (buf, ".startof.%s", secname);
4473 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4474 if (h != NULL && h->type == bfd_link_hash_undefined)
4476 h->type = bfd_link_hash_defined;
4477 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4478 h->u.def.section = bfd_abs_section_ptr;
4481 sprintf (buf, ".sizeof.%s", secname);
4482 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4483 if (h != NULL && h->type == bfd_link_hash_undefined)
4485 h->type = bfd_link_hash_defined;
4486 h->u.def.value = TO_ADDR (s->size);
4487 h->u.def.section = bfd_abs_section_ptr;
4497 struct bfd_link_hash_entry *h;
4500 if (link_info.relocatable || link_info.shared)
4505 if (entry_symbol.name == NULL)
4507 /* No entry has been specified. Look for start, but don't warn
4508 if we don't find it. */
4509 entry_symbol.name = "start";
4513 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4514 FALSE, FALSE, TRUE);
4516 && (h->type == bfd_link_hash_defined
4517 || h->type == bfd_link_hash_defweak)
4518 && h->u.def.section->output_section != NULL)
4522 val = (h->u.def.value
4523 + bfd_get_section_vma (output_bfd,
4524 h->u.def.section->output_section)
4525 + h->u.def.section->output_offset);
4526 if (! bfd_set_start_address (output_bfd, val))
4527 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4534 /* We couldn't find the entry symbol. Try parsing it as a
4536 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4539 if (! bfd_set_start_address (output_bfd, val))
4540 einfo (_("%P%F: can't set start address\n"));
4546 /* Can't find the entry symbol, and it's not a number. Use
4547 the first address in the text section. */
4548 ts = bfd_get_section_by_name (output_bfd, entry_section);
4552 einfo (_("%P: warning: cannot find entry symbol %s;"
4553 " defaulting to %V\n"),
4555 bfd_get_section_vma (output_bfd, ts));
4556 if (! bfd_set_start_address (output_bfd,
4557 bfd_get_section_vma (output_bfd,
4559 einfo (_("%P%F: can't set start address\n"));
4564 einfo (_("%P: warning: cannot find entry symbol %s;"
4565 " not setting start address\n"),
4571 /* Don't bfd_hash_table_free (&lang_definedness_table);
4572 map file output may result in a call of lang_track_definedness. */
4575 /* This is a small function used when we want to ignore errors from
4579 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
4581 /* Don't do anything. */
4584 /* Check that the architecture of all the input files is compatible
4585 with the output file. Also call the backend to let it do any
4586 other checking that is needed. */
4591 lang_statement_union_type *file;
4593 const bfd_arch_info_type *compatible;
4595 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
4597 input_bfd = file->input_statement.the_bfd;
4599 = bfd_arch_get_compatible (input_bfd, output_bfd,
4600 command_line.accept_unknown_input_arch);
4602 /* In general it is not possible to perform a relocatable
4603 link between differing object formats when the input
4604 file has relocations, because the relocations in the
4605 input format may not have equivalent representations in
4606 the output format (and besides BFD does not translate
4607 relocs for other link purposes than a final link). */
4608 if ((link_info.relocatable || link_info.emitrelocations)
4609 && (compatible == NULL
4610 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
4611 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
4613 einfo (_("%P%F: Relocatable linking with relocations from"
4614 " format %s (%B) to format %s (%B) is not supported\n"),
4615 bfd_get_target (input_bfd), input_bfd,
4616 bfd_get_target (output_bfd), output_bfd);
4617 /* einfo with %F exits. */
4620 if (compatible == NULL)
4622 if (command_line.warn_mismatch)
4623 einfo (_("%P: warning: %s architecture of input file `%B'"
4624 " is incompatible with %s output\n"),
4625 bfd_printable_name (input_bfd), input_bfd,
4626 bfd_printable_name (output_bfd));
4628 else if (bfd_count_sections (input_bfd))
4630 /* If the input bfd has no contents, it shouldn't set the
4631 private data of the output bfd. */
4633 bfd_error_handler_type pfn = NULL;
4635 /* If we aren't supposed to warn about mismatched input
4636 files, temporarily set the BFD error handler to a
4637 function which will do nothing. We still want to call
4638 bfd_merge_private_bfd_data, since it may set up
4639 information which is needed in the output file. */
4640 if (! command_line.warn_mismatch)
4641 pfn = bfd_set_error_handler (ignore_bfd_errors);
4642 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
4644 if (command_line.warn_mismatch)
4645 einfo (_("%P%X: failed to merge target specific data"
4646 " of file %B\n"), input_bfd);
4648 if (! command_line.warn_mismatch)
4649 bfd_set_error_handler (pfn);
4654 /* Look through all the global common symbols and attach them to the
4655 correct section. The -sort-common command line switch may be used
4656 to roughly sort the entries by size. */
4661 if (command_line.inhibit_common_definition)
4663 if (link_info.relocatable
4664 && ! command_line.force_common_definition)
4667 if (! config.sort_common)
4668 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
4673 for (power = 4; power >= 0; power--)
4674 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
4678 /* Place one common symbol in the correct section. */
4681 lang_one_common (struct bfd_link_hash_entry *h, void *info)
4683 unsigned int power_of_two;
4687 if (h->type != bfd_link_hash_common)
4691 power_of_two = h->u.c.p->alignment_power;
4693 if (config.sort_common
4694 && power_of_two < (unsigned int) *(int *) info)
4697 section = h->u.c.p->section;
4699 /* Increase the size of the section to align the common sym. */
4700 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
4701 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
4703 /* Adjust the alignment if necessary. */
4704 if (power_of_two > section->alignment_power)
4705 section->alignment_power = power_of_two;
4707 /* Change the symbol from common to defined. */
4708 h->type = bfd_link_hash_defined;
4709 h->u.def.section = section;
4710 h->u.def.value = section->size;
4712 /* Increase the size of the section. */
4713 section->size += size;
4715 /* Make sure the section is allocated in memory, and make sure that
4716 it is no longer a common section. */
4717 section->flags |= SEC_ALLOC;
4718 section->flags &= ~SEC_IS_COMMON;
4720 if (config.map_file != NULL)
4722 static bfd_boolean header_printed;
4727 if (! header_printed)
4729 minfo (_("\nAllocating common symbols\n"));
4730 minfo (_("Common symbol size file\n\n"));
4731 header_printed = TRUE;
4734 name = demangle (h->root.string);
4736 len = strlen (name);
4751 if (size <= 0xffffffff)
4752 sprintf (buf, "%lx", (unsigned long) size);
4754 sprintf_vma (buf, size);
4764 minfo ("%B\n", section->owner);
4770 /* Run through the input files and ensure that every input section has
4771 somewhere to go. If one is found without a destination then create
4772 an input request and place it into the statement tree. */
4775 lang_place_orphans (void)
4777 LANG_FOR_EACH_INPUT_STATEMENT (file)
4781 for (s = file->the_bfd->sections; s != NULL; s = s->next)
4783 if (s->output_section == NULL)
4785 /* This section of the file is not attached, root
4786 around for a sensible place for it to go. */
4788 if (file->just_syms_flag)
4789 bfd_link_just_syms (file->the_bfd, s, &link_info);
4790 else if ((s->flags & SEC_EXCLUDE) != 0)
4791 s->output_section = bfd_abs_section_ptr;
4792 else if (strcmp (s->name, "COMMON") == 0)
4794 /* This is a lonely common section which must have
4795 come from an archive. We attach to the section
4796 with the wildcard. */
4797 if (! link_info.relocatable
4798 || command_line.force_common_definition)
4800 if (default_common_section == NULL)
4802 default_common_section =
4803 lang_output_section_statement_lookup (".bss");
4806 lang_add_section (&default_common_section->children, s,
4807 default_common_section, file);
4810 else if (ldemul_place_orphan (file, s))
4814 lang_output_section_statement_type *os;
4816 os = lang_output_section_statement_lookup (s->name);
4817 lang_add_section (&os->children, s, os, file);
4825 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4827 flagword *ptr_flags;
4829 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4835 *ptr_flags |= SEC_ALLOC;
4839 *ptr_flags |= SEC_READONLY;
4843 *ptr_flags |= SEC_DATA;
4847 *ptr_flags |= SEC_CODE;
4852 *ptr_flags |= SEC_LOAD;
4856 einfo (_("%P%F: invalid syntax in flags\n"));
4863 /* Call a function on each input file. This function will be called
4864 on an archive, but not on the elements. */
4867 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4869 lang_input_statement_type *f;
4871 for (f = (lang_input_statement_type *) input_file_chain.head;
4873 f = (lang_input_statement_type *) f->next_real_file)
4877 /* Call a function on each file. The function will be called on all
4878 the elements of an archive which are included in the link, but will
4879 not be called on the archive file itself. */
4882 lang_for_each_file (void (*func) (lang_input_statement_type *))
4884 LANG_FOR_EACH_INPUT_STATEMENT (f)
4891 ldlang_add_file (lang_input_statement_type *entry)
4895 lang_statement_append (&file_chain,
4896 (lang_statement_union_type *) entry,
4899 /* The BFD linker needs to have a list of all input BFDs involved in
4901 ASSERT (entry->the_bfd->link_next == NULL);
4902 ASSERT (entry->the_bfd != output_bfd);
4903 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4905 *pp = entry->the_bfd;
4906 entry->the_bfd->usrdata = entry;
4907 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4909 /* Look through the sections and check for any which should not be
4910 included in the link. We need to do this now, so that we can
4911 notice when the backend linker tries to report multiple
4912 definition errors for symbols which are in sections we aren't
4913 going to link. FIXME: It might be better to entirely ignore
4914 symbols which are defined in sections which are going to be
4915 discarded. This would require modifying the backend linker for
4916 each backend which might set the SEC_LINK_ONCE flag. If we do
4917 this, we should probably handle SEC_EXCLUDE in the same way. */
4919 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4923 lang_add_output (const char *name, int from_script)
4925 /* Make -o on command line override OUTPUT in script. */
4926 if (!had_output_filename || !from_script)
4928 output_filename = name;
4929 had_output_filename = TRUE;
4933 static lang_output_section_statement_type *current_section;
4944 for (l = 0; l < 32; l++)
4946 if (i >= (unsigned int) x)
4954 lang_output_section_statement_type *
4955 lang_enter_output_section_statement (const char *output_section_statement_name,
4956 etree_type *address_exp,
4957 enum section_type sectype,
4959 etree_type *subalign,
4963 lang_output_section_statement_type *os;
4967 lang_output_section_statement_lookup_1 (output_section_statement_name,
4970 /* Make next things chain into subchain of this. */
4972 if (os->addr_tree == NULL)
4974 os->addr_tree = address_exp;
4976 os->sectype = sectype;
4977 if (sectype != noload_section)
4978 os->flags = SEC_NO_FLAGS;
4980 os->flags = SEC_NEVER_LOAD;
4981 os->block_value = 1;
4982 stat_ptr = &os->children;
4984 os->subsection_alignment =
4985 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4986 os->section_alignment =
4987 topower (exp_get_value_int (align, -1, "section alignment", 0));
4989 os->load_base = ebase;
4996 lang_output_statement_type *new =
4997 new_stat (lang_output_statement, stat_ptr);
4999 new->name = output_filename;
5002 /* Reset the current counters in the regions. */
5005 lang_reset_memory_regions (void)
5007 lang_memory_region_type *p = lang_memory_region_list;
5010 for (p = lang_memory_region_list; p != NULL; p = p->next)
5012 p->old_length = (bfd_size_type) (p->current - p->origin);
5013 p->current = p->origin;
5016 for (o = output_bfd->sections; o != NULL; o = o->next)
5018 /* Save the last size for possible use by bfd_relax_section. */
5019 o->rawsize = o->size;
5024 /* Worker for lang_gc_sections_1. */
5027 gc_section_callback (lang_wild_statement_type *ptr,
5028 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5030 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5031 void *data ATTRIBUTE_UNUSED)
5033 /* If the wild pattern was marked KEEP, the member sections
5034 should be as well. */
5035 if (ptr->keep_sections)
5036 section->flags |= SEC_KEEP;
5039 /* Iterate over sections marking them against GC. */
5042 lang_gc_sections_1 (lang_statement_union_type *s)
5044 for (; s != NULL; s = s->header.next)
5046 switch (s->header.type)
5048 case lang_wild_statement_enum:
5049 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5051 case lang_constructors_statement_enum:
5052 lang_gc_sections_1 (constructor_list.head);
5054 case lang_output_section_statement_enum:
5055 lang_gc_sections_1 (s->output_section_statement.children.head);
5057 case lang_group_statement_enum:
5058 lang_gc_sections_1 (s->group_statement.children.head);
5067 lang_gc_sections (void)
5069 struct bfd_link_hash_entry *h;
5070 ldlang_undef_chain_list_type *ulist;
5072 /* Keep all sections so marked in the link script. */
5074 lang_gc_sections_1 (statement_list.head);
5076 /* Keep all sections containing symbols undefined on the command-line,
5077 and the section containing the entry symbol. */
5079 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
5081 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
5082 FALSE, FALSE, FALSE);
5085 && (h->type == bfd_link_hash_defined
5086 || h->type == bfd_link_hash_defweak)
5087 && ! bfd_is_abs_section (h->u.def.section))
5089 h->u.def.section->flags |= SEC_KEEP;
5093 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5094 the special case of debug info. (See bfd/stabs.c)
5095 Twiddle the flag here, to simplify later linker code. */
5096 if (link_info.relocatable)
5098 LANG_FOR_EACH_INPUT_STATEMENT (f)
5101 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5102 if ((sec->flags & SEC_DEBUGGING) == 0)
5103 sec->flags &= ~SEC_EXCLUDE;
5107 if (link_info.gc_sections)
5108 bfd_gc_sections (output_bfd, &link_info);
5112 lang_mark_used_section (void)
5114 unsigned int gc_sections = link_info.gc_sections;
5116 link_info.gc_sections = 0;
5117 bfd_gc_sections (output_bfd, &link_info);
5118 link_info.gc_sections = gc_sections;
5124 current_target = default_target;
5126 /* Open the output file. */
5127 lang_for_each_statement (ldlang_open_output);
5130 ldemul_create_output_section_statements ();
5132 /* Add to the hash table all undefineds on the command line. */
5133 lang_place_undefineds ();
5135 if (!bfd_section_already_linked_table_init ())
5136 einfo (_("%P%F: Failed to create hash table\n"));
5138 /* Create a bfd for each input file. */
5139 current_target = default_target;
5140 open_input_bfds (statement_list.head, FALSE);
5142 link_info.gc_sym_list = &entry_symbol;
5143 if (entry_symbol.name == NULL)
5144 link_info.gc_sym_list = ldlang_undef_chain_list_head;
5146 ldemul_after_open ();
5148 bfd_section_already_linked_table_free ();
5150 /* Make sure that we're not mixing architectures. We call this
5151 after all the input files have been opened, but before we do any
5152 other processing, so that any operations merge_private_bfd_data
5153 does on the output file will be known during the rest of the
5157 /* Handle .exports instead of a version script if we're told to do so. */
5158 if (command_line.version_exports_section)
5159 lang_do_version_exports_section ();
5161 /* Build all sets based on the information gathered from the input
5163 ldctor_build_sets ();
5165 /* Remove unreferenced sections if asked to. */
5166 lang_gc_sections ();
5168 /* Size up the common data. */
5171 /* Update wild statements. */
5172 update_wild_statements (statement_list.head);
5174 /* Run through the contours of the script and attach input sections
5175 to the correct output sections. */
5176 map_input_to_output_sections (statement_list.head, NULL, NULL);
5178 /* Find any sections not attached explicitly and handle them. */
5179 lang_place_orphans ();
5181 if (! link_info.relocatable)
5185 /* Merge SEC_MERGE sections. This has to be done after GC of
5186 sections, so that GCed sections are not merged, but before
5187 assigning dynamic symbols, since removing whole input sections
5189 bfd_merge_sections (output_bfd, &link_info);
5191 /* Look for a text section and set the readonly attribute in it. */
5192 found = bfd_get_section_by_name (output_bfd, ".text");
5196 if (config.text_read_only)
5197 found->flags |= SEC_READONLY;
5199 found->flags &= ~SEC_READONLY;
5203 /* Do anything special before sizing sections. This is where ELF
5204 and other back-ends size dynamic sections. */
5205 ldemul_before_allocation ();
5207 if (!link_info.relocatable)
5208 strip_excluded_output_sections ();
5210 /* We must record the program headers before we try to fix the
5211 section positions, since they will affect SIZEOF_HEADERS. */
5212 lang_record_phdrs ();
5214 /* Size up the sections. */
5215 lang_size_sections (statement_list.head, abs_output_section,
5216 &statement_list.head, 0, 0, NULL,
5217 command_line.relax ? FALSE : TRUE);
5219 /* Now run around and relax if we can. */
5220 if (command_line.relax)
5222 /* Keep relaxing until bfd_relax_section gives up. */
5223 bfd_boolean relax_again;
5227 relax_again = FALSE;
5229 /* Note: pe-dll.c does something like this also. If you find
5230 you need to change this code, you probably need to change
5231 pe-dll.c also. DJ */
5233 /* Do all the assignments with our current guesses as to
5235 lang_do_assignments (statement_list.head, abs_output_section,
5238 /* We must do this after lang_do_assignments, because it uses
5240 lang_reset_memory_regions ();
5242 /* Perform another relax pass - this time we know where the
5243 globals are, so can make a better guess. */
5244 lang_size_sections (statement_list.head, abs_output_section,
5245 &statement_list.head, 0, 0, &relax_again, FALSE);
5247 /* If the normal relax is done and the relax finalize pass
5248 is not performed yet, we perform another relax pass. */
5249 if (!relax_again && link_info.need_relax_finalize)
5251 link_info.need_relax_finalize = FALSE;
5255 while (relax_again);
5257 /* Final extra sizing to report errors. */
5258 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
5259 lang_reset_memory_regions ();
5260 lang_size_sections (statement_list.head, abs_output_section,
5261 &statement_list.head, 0, 0, NULL, TRUE);
5264 /* See if anything special should be done now we know how big
5266 ldemul_after_allocation ();
5268 /* Fix any .startof. or .sizeof. symbols. */
5269 lang_set_startof ();
5271 /* Do all the assignments, now that we know the final resting places
5272 of all the symbols. */
5274 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
5276 /* Make sure that the section addresses make sense. */
5277 if (! link_info.relocatable
5278 && command_line.check_section_addresses)
5279 lang_check_section_addresses ();
5282 lang_mark_used_section ();
5287 /* EXPORTED TO YACC */
5290 lang_add_wild (struct wildcard_spec *filespec,
5291 struct wildcard_list *section_list,
5292 bfd_boolean keep_sections)
5294 struct wildcard_list *curr, *next;
5295 lang_wild_statement_type *new;
5297 /* Reverse the list as the parser puts it back to front. */
5298 for (curr = section_list, section_list = NULL;
5300 section_list = curr, curr = next)
5302 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
5303 placed_commons = TRUE;
5306 curr->next = section_list;
5309 if (filespec != NULL && filespec->name != NULL)
5311 if (strcmp (filespec->name, "*") == 0)
5312 filespec->name = NULL;
5313 else if (! wildcardp (filespec->name))
5314 lang_has_input_file = TRUE;
5317 new = new_stat (lang_wild_statement, stat_ptr);
5318 new->filename = NULL;
5319 new->filenames_sorted = FALSE;
5320 if (filespec != NULL)
5322 new->filename = filespec->name;
5323 new->filenames_sorted = filespec->sorted == by_name;
5325 new->section_list = section_list;
5326 new->keep_sections = keep_sections;
5327 lang_list_init (&new->children);
5328 analyze_walk_wild_section_handler (new);
5332 lang_section_start (const char *name, etree_type *address,
5333 const segment_type *segment)
5335 lang_address_statement_type *ad;
5337 ad = new_stat (lang_address_statement, stat_ptr);
5338 ad->section_name = name;
5339 ad->address = address;
5340 ad->segment = segment;
5343 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5344 because of a -e argument on the command line, or zero if this is
5345 called by ENTRY in a linker script. Command line arguments take
5349 lang_add_entry (const char *name, bfd_boolean cmdline)
5351 if (entry_symbol.name == NULL
5353 || ! entry_from_cmdline)
5355 entry_symbol.name = name;
5356 entry_from_cmdline = cmdline;
5361 lang_add_target (const char *name)
5363 lang_target_statement_type *new = new_stat (lang_target_statement,
5371 lang_add_map (const char *name)
5378 map_option_f = TRUE;
5386 lang_add_fill (fill_type *fill)
5388 lang_fill_statement_type *new = new_stat (lang_fill_statement,
5395 lang_add_data (int type, union etree_union *exp)
5398 lang_data_statement_type *new = new_stat (lang_data_statement,
5406 /* Create a new reloc statement. RELOC is the BFD relocation type to
5407 generate. HOWTO is the corresponding howto structure (we could
5408 look this up, but the caller has already done so). SECTION is the
5409 section to generate a reloc against, or NAME is the name of the
5410 symbol to generate a reloc against. Exactly one of SECTION and
5411 NAME must be NULL. ADDEND is an expression for the addend. */
5414 lang_add_reloc (bfd_reloc_code_real_type reloc,
5415 reloc_howto_type *howto,
5418 union etree_union *addend)
5420 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5424 p->section = section;
5426 p->addend_exp = addend;
5428 p->addend_value = 0;
5429 p->output_section = NULL;
5433 lang_assignment_statement_type *
5434 lang_add_assignment (etree_type *exp)
5436 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
5444 lang_add_attribute (enum statement_enum attribute)
5446 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
5450 lang_startup (const char *name)
5452 if (startup_file != NULL)
5454 einfo (_("%P%Fmultiple STARTUP files\n"));
5456 first_file->filename = name;
5457 first_file->local_sym_name = name;
5458 first_file->real = TRUE;
5460 startup_file = name;
5464 lang_float (bfd_boolean maybe)
5466 lang_float_flag = maybe;
5470 /* Work out the load- and run-time regions from a script statement, and
5471 store them in *LMA_REGION and *REGION respectively.
5473 MEMSPEC is the name of the run-time region, or the value of
5474 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5475 LMA_MEMSPEC is the name of the load-time region, or null if the
5476 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5477 had an explicit load address.
5479 It is an error to specify both a load region and a load address. */
5482 lang_get_regions (lang_memory_region_type **region,
5483 lang_memory_region_type **lma_region,
5484 const char *memspec,
5485 const char *lma_memspec,
5486 bfd_boolean have_lma,
5487 bfd_boolean have_vma)
5489 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5491 /* If no runtime region or VMA has been specified, but the load region
5492 has been specified, then use the load region for the runtime region
5494 if (lma_memspec != NULL
5496 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5497 *region = *lma_region;
5499 *region = lang_memory_region_lookup (memspec, FALSE);
5501 if (have_lma && lma_memspec != 0)
5502 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5506 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5507 lang_output_section_phdr_list *phdrs,
5508 const char *lma_memspec)
5510 lang_get_regions (¤t_section->region,
5511 ¤t_section->lma_region,
5512 memspec, lma_memspec,
5513 current_section->load_base != NULL,
5514 current_section->addr_tree != NULL);
5515 current_section->fill = fill;
5516 current_section->phdrs = phdrs;
5517 stat_ptr = &statement_list;
5520 /* Create an absolute symbol with the given name with the value of the
5521 address of first byte of the section named.
5523 If the symbol already exists, then do nothing. */
5526 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5528 struct bfd_link_hash_entry *h;
5530 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5532 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5534 if (h->type == bfd_link_hash_new
5535 || h->type == bfd_link_hash_undefined)
5539 h->type = bfd_link_hash_defined;
5541 sec = bfd_get_section_by_name (output_bfd, secname);
5545 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5547 h->u.def.section = bfd_abs_section_ptr;
5551 /* Create an absolute symbol with the given name with the value of the
5552 address of the first byte after the end of the section named.
5554 If the symbol already exists, then do nothing. */
5557 lang_abs_symbol_at_end_of (const char *secname, const char *name)
5559 struct bfd_link_hash_entry *h;
5561 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5563 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5565 if (h->type == bfd_link_hash_new
5566 || h->type == bfd_link_hash_undefined)
5570 h->type = bfd_link_hash_defined;
5572 sec = bfd_get_section_by_name (output_bfd, secname);
5576 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
5577 + TO_ADDR (sec->size));
5579 h->u.def.section = bfd_abs_section_ptr;
5584 lang_statement_append (lang_statement_list_type *list,
5585 lang_statement_union_type *element,
5586 lang_statement_union_type **field)
5588 *(list->tail) = element;
5592 /* Set the output format type. -oformat overrides scripts. */
5595 lang_add_output_format (const char *format,
5600 if (output_target == NULL || !from_script)
5602 if (command_line.endian == ENDIAN_BIG
5605 else if (command_line.endian == ENDIAN_LITTLE
5609 output_target = format;
5613 /* Enter a group. This creates a new lang_group_statement, and sets
5614 stat_ptr to build new statements within the group. */
5617 lang_enter_group (void)
5619 lang_group_statement_type *g;
5621 g = new_stat (lang_group_statement, stat_ptr);
5622 lang_list_init (&g->children);
5623 stat_ptr = &g->children;
5626 /* Leave a group. This just resets stat_ptr to start writing to the
5627 regular list of statements again. Note that this will not work if
5628 groups can occur inside anything else which can adjust stat_ptr,
5629 but currently they can't. */
5632 lang_leave_group (void)
5634 stat_ptr = &statement_list;
5637 /* Add a new program header. This is called for each entry in a PHDRS
5638 command in a linker script. */
5641 lang_new_phdr (const char *name,
5643 bfd_boolean filehdr,
5648 struct lang_phdr *n, **pp;
5650 n = stat_alloc (sizeof (struct lang_phdr));
5653 n->type = exp_get_value_int (type, 0, "program header type",
5654 lang_final_phase_enum);
5655 n->filehdr = filehdr;
5660 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
5665 /* Record the program header information in the output BFD. FIXME: We
5666 should not be calling an ELF specific function here. */
5669 lang_record_phdrs (void)
5673 lang_output_section_phdr_list *last;
5674 struct lang_phdr *l;
5675 lang_output_section_statement_type *os;
5678 secs = xmalloc (alc * sizeof (asection *));
5680 for (l = lang_phdr_list; l != NULL; l = l->next)
5687 for (os = &lang_output_section_statement.head->output_section_statement;
5691 lang_output_section_phdr_list *pl;
5693 if (os->constraint == -1)
5701 if (os->sectype == noload_section
5702 || os->bfd_section == NULL
5703 || (os->bfd_section->flags & SEC_ALLOC) == 0)
5708 if (os->bfd_section == NULL)
5711 for (; pl != NULL; pl = pl->next)
5713 if (strcmp (pl->name, l->name) == 0)
5718 secs = xrealloc (secs, alc * sizeof (asection *));
5720 secs[c] = os->bfd_section;
5727 if (l->flags == NULL)
5730 flags = exp_get_vma (l->flags, 0, "phdr flags",
5731 lang_final_phase_enum);
5736 at = exp_get_vma (l->at, 0, "phdr load address",
5737 lang_final_phase_enum);
5739 if (! bfd_record_phdr (output_bfd, l->type,
5740 l->flags != NULL, flags, l->at != NULL,
5741 at, l->filehdr, l->phdrs, c, secs))
5742 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
5747 /* Make sure all the phdr assignments succeeded. */
5748 for (os = &lang_output_section_statement.head->output_section_statement;
5752 lang_output_section_phdr_list *pl;
5754 if (os->constraint == -1
5755 || os->bfd_section == NULL)
5758 for (pl = os->phdrs;
5761 if (! pl->used && strcmp (pl->name, "NONE") != 0)
5762 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
5763 os->name, pl->name);
5767 /* Record a list of sections which may not be cross referenced. */
5770 lang_add_nocrossref (lang_nocrossref_type *l)
5772 struct lang_nocrossrefs *n;
5774 n = xmalloc (sizeof *n);
5775 n->next = nocrossref_list;
5777 nocrossref_list = n;
5779 /* Set notice_all so that we get informed about all symbols. */
5780 link_info.notice_all = TRUE;
5783 /* Overlay handling. We handle overlays with some static variables. */
5785 /* The overlay virtual address. */
5786 static etree_type *overlay_vma;
5787 /* And subsection alignment. */
5788 static etree_type *overlay_subalign;
5790 /* An expression for the maximum section size seen so far. */
5791 static etree_type *overlay_max;
5793 /* A list of all the sections in this overlay. */
5795 struct overlay_list {
5796 struct overlay_list *next;
5797 lang_output_section_statement_type *os;
5800 static struct overlay_list *overlay_list;
5802 /* Start handling an overlay. */
5805 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5807 /* The grammar should prevent nested overlays from occurring. */
5808 ASSERT (overlay_vma == NULL
5809 && overlay_subalign == NULL
5810 && overlay_max == NULL);
5812 overlay_vma = vma_expr;
5813 overlay_subalign = subalign;
5816 /* Start a section in an overlay. We handle this by calling
5817 lang_enter_output_section_statement with the correct VMA.
5818 lang_leave_overlay sets up the LMA and memory regions. */
5821 lang_enter_overlay_section (const char *name)
5823 struct overlay_list *n;
5826 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5827 0, overlay_subalign, 0, 0);
5829 /* If this is the first section, then base the VMA of future
5830 sections on this one. This will work correctly even if `.' is
5831 used in the addresses. */
5832 if (overlay_list == NULL)
5833 overlay_vma = exp_nameop (ADDR, name);
5835 /* Remember the section. */
5836 n = xmalloc (sizeof *n);
5837 n->os = current_section;
5838 n->next = overlay_list;
5841 size = exp_nameop (SIZEOF, name);
5843 /* Arrange to work out the maximum section end address. */
5844 if (overlay_max == NULL)
5847 overlay_max = exp_binop (MAX_K, overlay_max, size);
5850 /* Finish a section in an overlay. There isn't any special to do
5854 lang_leave_overlay_section (fill_type *fill,
5855 lang_output_section_phdr_list *phdrs)
5862 name = current_section->name;
5864 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5865 region and that no load-time region has been specified. It doesn't
5866 really matter what we say here, since lang_leave_overlay will
5868 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5870 /* Define the magic symbols. */
5872 clean = xmalloc (strlen (name) + 1);
5874 for (s1 = name; *s1 != '\0'; s1++)
5875 if (ISALNUM (*s1) || *s1 == '_')
5879 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5880 sprintf (buf, "__load_start_%s", clean);
5881 lang_add_assignment (exp_assop ('=', buf,
5882 exp_nameop (LOADADDR, name)));
5884 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5885 sprintf (buf, "__load_stop_%s", clean);
5886 lang_add_assignment (exp_assop ('=', buf,
5888 exp_nameop (LOADADDR, name),
5889 exp_nameop (SIZEOF, name))));
5894 /* Finish an overlay. If there are any overlay wide settings, this
5895 looks through all the sections in the overlay and sets them. */
5898 lang_leave_overlay (etree_type *lma_expr,
5901 const char *memspec,
5902 lang_output_section_phdr_list *phdrs,
5903 const char *lma_memspec)
5905 lang_memory_region_type *region;
5906 lang_memory_region_type *lma_region;
5907 struct overlay_list *l;
5908 lang_nocrossref_type *nocrossref;
5910 lang_get_regions (®ion, &lma_region,
5911 memspec, lma_memspec,
5912 lma_expr != NULL, FALSE);
5916 /* After setting the size of the last section, set '.' to end of the
5918 if (overlay_list != NULL)
5919 overlay_list->os->update_dot_tree
5920 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5925 struct overlay_list *next;
5927 if (fill != NULL && l->os->fill == NULL)
5930 l->os->region = region;
5931 l->os->lma_region = lma_region;
5933 /* The first section has the load address specified in the
5934 OVERLAY statement. The rest are worked out from that.
5935 The base address is not needed (and should be null) if
5936 an LMA region was specified. */
5938 l->os->load_base = lma_expr;
5939 else if (lma_region == 0)
5940 l->os->load_base = exp_binop ('+',
5941 exp_nameop (LOADADDR, l->next->os->name),
5942 exp_nameop (SIZEOF, l->next->os->name));
5944 if (phdrs != NULL && l->os->phdrs == NULL)
5945 l->os->phdrs = phdrs;
5949 lang_nocrossref_type *nc;
5951 nc = xmalloc (sizeof *nc);
5952 nc->name = l->os->name;
5953 nc->next = nocrossref;
5962 if (nocrossref != NULL)
5963 lang_add_nocrossref (nocrossref);
5966 overlay_list = NULL;
5970 /* Version handling. This is only useful for ELF. */
5972 /* This global variable holds the version tree that we build. */
5974 struct bfd_elf_version_tree *lang_elf_version_info;
5976 /* If PREV is NULL, return first version pattern matching particular symbol.
5977 If PREV is non-NULL, return first version pattern matching particular
5978 symbol after PREV (previously returned by lang_vers_match). */
5980 static struct bfd_elf_version_expr *
5981 lang_vers_match (struct bfd_elf_version_expr_head *head,
5982 struct bfd_elf_version_expr *prev,
5985 const char *cxx_sym = sym;
5986 const char *java_sym = sym;
5987 struct bfd_elf_version_expr *expr = NULL;
5989 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5991 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5995 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5997 java_sym = cplus_demangle (sym, DMGL_JAVA);
6002 if (head->htab && (prev == NULL || prev->symbol))
6004 struct bfd_elf_version_expr e;
6006 switch (prev ? prev->mask : 0)
6009 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6012 expr = htab_find (head->htab, &e);
6013 while (expr && strcmp (expr->symbol, sym) == 0)
6014 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6020 case BFD_ELF_VERSION_C_TYPE:
6021 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6024 expr = htab_find (head->htab, &e);
6025 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6026 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6032 case BFD_ELF_VERSION_CXX_TYPE:
6033 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6035 e.symbol = java_sym;
6036 expr = htab_find (head->htab, &e);
6037 while (expr && strcmp (expr->symbol, java_sym) == 0)
6038 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6049 /* Finally, try the wildcards. */
6050 if (prev == NULL || prev->symbol)
6051 expr = head->remaining;
6058 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6061 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6063 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6067 if (fnmatch (expr->pattern, s, 0) == 0)
6074 free ((char *) cxx_sym);
6075 if (java_sym != sym)
6076 free ((char *) java_sym);
6080 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6081 return a string pointing to the symbol name. */
6084 realsymbol (const char *pattern)
6087 bfd_boolean changed = FALSE, backslash = FALSE;
6088 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6090 for (p = pattern, s = symbol; *p != '\0'; ++p)
6092 /* It is a glob pattern only if there is no preceding
6094 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
6102 /* Remove the preceding backslash. */
6109 backslash = *p == '\\';
6124 /* This is called for each variable name or match expression. */
6126 struct bfd_elf_version_expr *
6127 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
6131 struct bfd_elf_version_expr *ret;
6133 ret = xmalloc (sizeof *ret);
6138 ret->symbol = realsymbol (new);
6140 if (lang == NULL || strcasecmp (lang, "C") == 0)
6141 ret->mask = BFD_ELF_VERSION_C_TYPE;
6142 else if (strcasecmp (lang, "C++") == 0)
6143 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
6144 else if (strcasecmp (lang, "Java") == 0)
6145 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
6148 einfo (_("%X%P: unknown language `%s' in version information\n"),
6150 ret->mask = BFD_ELF_VERSION_C_TYPE;
6153 return ldemul_new_vers_pattern (ret);
6156 /* This is called for each set of variable names and match
6159 struct bfd_elf_version_tree *
6160 lang_new_vers_node (struct bfd_elf_version_expr *globals,
6161 struct bfd_elf_version_expr *locals)
6163 struct bfd_elf_version_tree *ret;
6165 ret = xcalloc (1, sizeof *ret);
6166 ret->globals.list = globals;
6167 ret->locals.list = locals;
6168 ret->match = lang_vers_match;
6169 ret->name_indx = (unsigned int) -1;
6173 /* This static variable keeps track of version indices. */
6175 static int version_index;
6178 version_expr_head_hash (const void *p)
6180 const struct bfd_elf_version_expr *e = p;
6182 return htab_hash_string (e->symbol);
6186 version_expr_head_eq (const void *p1, const void *p2)
6188 const struct bfd_elf_version_expr *e1 = p1;
6189 const struct bfd_elf_version_expr *e2 = p2;
6191 return strcmp (e1->symbol, e2->symbol) == 0;
6195 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
6198 struct bfd_elf_version_expr *e, *next;
6199 struct bfd_elf_version_expr **list_loc, **remaining_loc;
6201 for (e = head->list; e; e = e->next)
6205 head->mask |= e->mask;
6210 head->htab = htab_create (count * 2, version_expr_head_hash,
6211 version_expr_head_eq, NULL);
6212 list_loc = &head->list;
6213 remaining_loc = &head->remaining;
6214 for (e = head->list; e; e = next)
6220 remaining_loc = &e->next;
6224 void **loc = htab_find_slot (head->htab, e, INSERT);
6228 struct bfd_elf_version_expr *e1, *last;
6234 if (e1->mask == e->mask)
6242 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
6246 /* This is a duplicate. */
6247 /* FIXME: Memory leak. Sometimes pattern is not
6248 xmalloced alone, but in larger chunk of memory. */
6249 /* free (e->symbol); */
6254 e->next = last->next;
6262 list_loc = &e->next;
6266 *remaining_loc = NULL;
6267 *list_loc = head->remaining;
6270 head->remaining = head->list;
6273 /* This is called when we know the name and dependencies of the
6277 lang_register_vers_node (const char *name,
6278 struct bfd_elf_version_tree *version,
6279 struct bfd_elf_version_deps *deps)
6281 struct bfd_elf_version_tree *t, **pp;
6282 struct bfd_elf_version_expr *e1;
6287 if ((name[0] == '\0' && lang_elf_version_info != NULL)
6288 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
6290 einfo (_("%X%P: anonymous version tag cannot be combined"
6291 " with other version tags\n"));
6296 /* Make sure this node has a unique name. */
6297 for (t = lang_elf_version_info; t != NULL; t = t->next)
6298 if (strcmp (t->name, name) == 0)
6299 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
6301 lang_finalize_version_expr_head (&version->globals);
6302 lang_finalize_version_expr_head (&version->locals);
6304 /* Check the global and local match names, and make sure there
6305 aren't any duplicates. */
6307 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
6309 for (t = lang_elf_version_info; t != NULL; t = t->next)
6311 struct bfd_elf_version_expr *e2;
6313 if (t->locals.htab && e1->symbol)
6315 e2 = htab_find (t->locals.htab, e1);
6316 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6318 if (e1->mask == e2->mask)
6319 einfo (_("%X%P: duplicate expression `%s'"
6320 " in version information\n"), e1->symbol);
6324 else if (!e1->symbol)
6325 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
6326 if (strcmp (e1->pattern, e2->pattern) == 0
6327 && e1->mask == e2->mask)
6328 einfo (_("%X%P: duplicate expression `%s'"
6329 " in version information\n"), e1->pattern);
6333 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
6335 for (t = lang_elf_version_info; t != NULL; t = t->next)
6337 struct bfd_elf_version_expr *e2;
6339 if (t->globals.htab && e1->symbol)
6341 e2 = htab_find (t->globals.htab, e1);
6342 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6344 if (e1->mask == e2->mask)
6345 einfo (_("%X%P: duplicate expression `%s'"
6346 " in version information\n"),
6351 else if (!e1->symbol)
6352 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6353 if (strcmp (e1->pattern, e2->pattern) == 0
6354 && e1->mask == e2->mask)
6355 einfo (_("%X%P: duplicate expression `%s'"
6356 " in version information\n"), e1->pattern);
6360 version->deps = deps;
6361 version->name = name;
6362 if (name[0] != '\0')
6365 version->vernum = version_index;
6368 version->vernum = 0;
6370 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6375 /* This is called when we see a version dependency. */
6377 struct bfd_elf_version_deps *
6378 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6380 struct bfd_elf_version_deps *ret;
6381 struct bfd_elf_version_tree *t;
6383 ret = xmalloc (sizeof *ret);
6386 for (t = lang_elf_version_info; t != NULL; t = t->next)
6388 if (strcmp (t->name, name) == 0)
6390 ret->version_needed = t;
6395 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6401 lang_do_version_exports_section (void)
6403 struct bfd_elf_version_expr *greg = NULL, *lreg;
6405 LANG_FOR_EACH_INPUT_STATEMENT (is)
6407 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6415 contents = xmalloc (len);
6416 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6417 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6420 while (p < contents + len)
6422 greg = lang_new_vers_pattern (greg, p, NULL);
6423 p = strchr (p, '\0') + 1;
6426 /* Do not free the contents, as we used them creating the regex. */
6428 /* Do not include this section in the link. */
6429 sec->flags |= SEC_EXCLUDE;
6432 lreg = lang_new_vers_pattern (NULL, "*", NULL);
6433 lang_register_vers_node (command_line.version_exports_section,
6434 lang_new_vers_node (greg, lreg), NULL);
6438 lang_add_unique (const char *name)
6440 struct unique_sections *ent;
6442 for (ent = unique_section_list; ent; ent = ent->next)
6443 if (strcmp (ent->name, name) == 0)
6446 ent = xmalloc (sizeof *ent);
6447 ent->name = xstrdup (name);
6448 ent->next = unique_section_list;
6449 unique_section_list = ent;