1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static lang_input_statement_type *lookup_name (const char *);
71 static bfd_boolean load_symbols (lang_input_statement_type *,
72 lang_statement_list_type *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection *);
77 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
78 static void print_statement (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statement_list (lang_statement_union_type *,
81 lang_output_section_statement_type *);
82 static void print_statements (void);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
88 asection *, lang_input_statement_type *, void *);
90 /* Exported variables. */
91 lang_output_section_statement_type *abs_output_section;
92 lang_statement_list_type lang_output_section_statement;
93 lang_statement_list_type *stat_ptr = &statement_list;
94 lang_statement_list_type file_chain = { NULL, NULL };
95 struct bfd_sym_chain entry_symbol = { NULL, NULL };
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_nocrossrefs *nocrossref_list;
103 struct unique_sections *unique_section_list;
104 static bfd_boolean ldlang_sysrooted_script = FALSE;
105 int lang_statement_iteration = 0;
107 etree_type *base; /* Relocation base - or null */
109 /* Return TRUE if the PATTERN argument is a wildcard pattern.
110 Although backslashes are treated specially if a pattern contains
111 wildcards, we do not consider the mere presence of a backslash to
112 be enough to cause the pattern to be treated as a wildcard.
113 That lets us handle DOS filenames more naturally. */
114 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
116 #define new_stat(x, y) \
117 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
119 #define outside_section_address(q) \
120 ((q)->output_offset + (q)->output_section->vma)
122 #define outside_symbol_address(q) \
123 ((q)->value + outside_section_address (q->section))
125 #define SECTION_NAME_MAP_LENGTH (16)
128 stat_alloc (size_t size)
130 return obstack_alloc (&stat_obstack, size);
134 unique_section_p (const asection *sec)
136 struct unique_sections *unam;
139 if (link_info.relocatable
140 && sec->owner != NULL
141 && bfd_is_group_section (sec->owner, sec))
145 for (unam = unique_section_list; unam; unam = unam->next)
146 if (wildcardp (unam->name)
147 ? fnmatch (unam->name, secnam, 0) == 0
148 : strcmp (unam->name, secnam) == 0)
156 /* Generic traversal routines for finding matching sections. */
159 walk_wild_section (lang_wild_statement_type *ptr,
160 lang_input_statement_type *file,
166 if (file->just_syms_flag)
169 for (s = file->the_bfd->sections; s != NULL; s = s->next)
171 struct wildcard_list *sec;
173 sec = ptr->section_list;
175 (*callback) (ptr, sec, s, file, data);
179 bfd_boolean skip = FALSE;
180 struct name_list *list_tmp;
182 /* Don't process sections from files which were
184 for (list_tmp = sec->spec.exclude_name_list;
186 list_tmp = list_tmp->next)
188 if (wildcardp (list_tmp->name))
189 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
191 skip = strcmp (list_tmp->name, file->filename) == 0;
193 /* If this file is part of an archive, and the archive is
194 excluded, exclude this file. */
195 if (! skip && file->the_bfd != NULL
196 && file->the_bfd->my_archive != NULL
197 && file->the_bfd->my_archive->filename != NULL)
199 if (wildcardp (list_tmp->name))
200 skip = fnmatch (list_tmp->name,
201 file->the_bfd->my_archive->filename,
204 skip = strcmp (list_tmp->name,
205 file->the_bfd->my_archive->filename) == 0;
212 if (!skip && sec->spec.name != NULL)
214 const char *sname = bfd_get_section_name (file->the_bfd, s);
216 if (wildcardp (sec->spec.name))
217 skip = fnmatch (sec->spec.name, sname, 0) != 0;
219 skip = strcmp (sec->spec.name, sname) != 0;
223 (*callback) (ptr, sec, s, file, data);
230 /* Handle a wild statement for a single file F. */
233 walk_wild_file (lang_wild_statement_type *s,
234 lang_input_statement_type *f,
238 if (f->the_bfd == NULL
239 || ! bfd_check_format (f->the_bfd, bfd_archive))
240 walk_wild_section (s, f, callback, data);
245 /* This is an archive file. We must map each member of the
246 archive separately. */
247 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
248 while (member != NULL)
250 /* When lookup_name is called, it will call the add_symbols
251 entry point for the archive. For each element of the
252 archive which is included, BFD will call ldlang_add_file,
253 which will set the usrdata field of the member to the
254 lang_input_statement. */
255 if (member->usrdata != NULL)
257 walk_wild_section (s, member->usrdata, callback, data);
260 member = bfd_openr_next_archived_file (f->the_bfd, member);
266 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
268 const char *file_spec = s->filename;
270 if (file_spec == NULL)
272 /* Perform the iteration over all files in the list. */
273 LANG_FOR_EACH_INPUT_STATEMENT (f)
275 walk_wild_file (s, f, callback, data);
278 else if (wildcardp (file_spec))
280 LANG_FOR_EACH_INPUT_STATEMENT (f)
282 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
283 walk_wild_file (s, f, callback, data);
288 lang_input_statement_type *f;
290 /* Perform the iteration over a single file. */
291 f = lookup_name (file_spec);
293 walk_wild_file (s, f, callback, data);
297 /* lang_for_each_statement walks the parse tree and calls the provided
298 function for each node. */
301 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
302 lang_statement_union_type *s)
304 for (; s != NULL; s = s->header.next)
308 switch (s->header.type)
310 case lang_constructors_statement_enum:
311 lang_for_each_statement_worker (func, constructor_list.head);
313 case lang_output_section_statement_enum:
314 lang_for_each_statement_worker
315 (func, s->output_section_statement.children.head);
317 case lang_wild_statement_enum:
318 lang_for_each_statement_worker (func,
319 s->wild_statement.children.head);
321 case lang_group_statement_enum:
322 lang_for_each_statement_worker (func,
323 s->group_statement.children.head);
325 case lang_data_statement_enum:
326 case lang_reloc_statement_enum:
327 case lang_object_symbols_statement_enum:
328 case lang_output_statement_enum:
329 case lang_target_statement_enum:
330 case lang_input_section_enum:
331 case lang_input_statement_enum:
332 case lang_assignment_statement_enum:
333 case lang_padding_statement_enum:
334 case lang_address_statement_enum:
335 case lang_fill_statement_enum:
345 lang_for_each_statement (void (*func) (lang_statement_union_type *))
347 lang_for_each_statement_worker (func, statement_list.head);
350 /*----------------------------------------------------------------------*/
353 lang_list_init (lang_statement_list_type *list)
356 list->tail = &list->head;
359 /* Build a new statement node for the parse tree. */
361 static lang_statement_union_type *
362 new_statement (enum statement_enum type,
364 lang_statement_list_type *list)
366 lang_statement_union_type *new;
368 new = stat_alloc (size);
369 new->header.type = type;
370 new->header.next = NULL;
371 lang_statement_append (list, new, &new->header.next);
375 /* Build a new input file node for the language. There are several
376 ways in which we treat an input file, eg, we only look at symbols,
377 or prefix it with a -l etc.
379 We can be supplied with requests for input files more than once;
380 they may, for example be split over several lines like foo.o(.text)
381 foo.o(.data) etc, so when asked for a file we check that we haven't
382 got it already so we don't duplicate the bfd. */
384 static lang_input_statement_type *
385 new_afile (const char *name,
386 lang_input_file_enum_type file_type,
388 bfd_boolean add_to_list)
390 lang_input_statement_type *p;
393 p = new_stat (lang_input_statement, stat_ptr);
396 p = stat_alloc (sizeof (lang_input_statement_type));
397 p->header.next = NULL;
400 lang_has_input_file = TRUE;
402 p->sysrooted = FALSE;
405 case lang_input_file_is_symbols_only_enum:
407 p->is_archive = FALSE;
409 p->local_sym_name = name;
410 p->just_syms_flag = TRUE;
411 p->search_dirs_flag = FALSE;
413 case lang_input_file_is_fake_enum:
415 p->is_archive = FALSE;
417 p->local_sym_name = name;
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = FALSE;
421 case lang_input_file_is_l_enum:
422 p->is_archive = TRUE;
425 p->local_sym_name = concat ("-l", name, NULL);
426 p->just_syms_flag = FALSE;
427 p->search_dirs_flag = TRUE;
429 case lang_input_file_is_marker_enum:
431 p->is_archive = FALSE;
433 p->local_sym_name = name;
434 p->just_syms_flag = FALSE;
435 p->search_dirs_flag = TRUE;
437 case lang_input_file_is_search_file_enum:
438 p->sysrooted = ldlang_sysrooted_script;
440 p->is_archive = FALSE;
442 p->local_sym_name = name;
443 p->just_syms_flag = FALSE;
444 p->search_dirs_flag = TRUE;
446 case lang_input_file_is_file_enum:
448 p->is_archive = FALSE;
450 p->local_sym_name = name;
451 p->just_syms_flag = FALSE;
452 p->search_dirs_flag = FALSE;
459 p->next_real_file = NULL;
462 p->dynamic = config.dynamic_link;
463 p->add_needed = add_needed;
464 p->as_needed = as_needed;
465 p->whole_archive = whole_archive;
467 lang_statement_append (&input_file_chain,
468 (lang_statement_union_type *) p,
473 lang_input_statement_type *
474 lang_add_input_file (const char *name,
475 lang_input_file_enum_type file_type,
478 lang_has_input_file = TRUE;
479 return new_afile (name, file_type, target, TRUE);
482 /* Build enough state so that the parser can build its tree. */
487 obstack_begin (&stat_obstack, 1000);
489 stat_ptr = &statement_list;
491 lang_list_init (stat_ptr);
493 lang_list_init (&input_file_chain);
494 lang_list_init (&lang_output_section_statement);
495 lang_list_init (&file_chain);
496 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
499 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
501 abs_output_section->bfd_section = bfd_abs_section_ptr;
503 /* The value "3" is ad-hoc, somewhat related to the expected number of
504 DEFINED expressions in a linker script. For most default linker
505 scripts, there are none. Why a hash table then? Well, it's somewhat
506 simpler to re-use working machinery than using a linked list in terms
507 of code-complexity here in ld, besides the initialization which just
508 looks like other code here. */
509 if (!bfd_hash_table_init_n (&lang_definedness_table,
510 lang_definedness_newfunc, 3))
511 einfo (_("%P%F: out of memory during initialization"));
513 /* Callers of exp_fold_tree need to increment this. */
514 lang_statement_iteration = 0;
517 /*----------------------------------------------------------------------
518 A region is an area of memory declared with the
519 MEMORY { name:org=exp, len=exp ... }
522 We maintain a list of all the regions here.
524 If no regions are specified in the script, then the default is used
525 which is created when looked up to be the entire data space.
527 If create is true we are creating a region inside a MEMORY block.
528 In this case it is probably an error to create a region that has
529 already been created. If we are not inside a MEMORY block it is
530 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
531 and so we issue a warning. */
533 static lang_memory_region_type *lang_memory_region_list;
534 static lang_memory_region_type **lang_memory_region_list_tail
535 = &lang_memory_region_list;
537 lang_memory_region_type *
538 lang_memory_region_lookup (const char *const name, bfd_boolean create)
540 lang_memory_region_type *p;
541 lang_memory_region_type *new;
543 /* NAME is NULL for LMA memspecs if no region was specified. */
547 for (p = lang_memory_region_list; p != NULL; p = p->next)
548 if (strcmp (p->name, name) == 0)
551 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
557 /* This code used to always use the first region in the list as the
558 default region. I changed it to instead use a region
559 encompassing all of memory as the default region. This permits
560 NOLOAD sections to work reasonably without requiring a region.
561 People should specify what region they mean, if they really want
563 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
565 if (lang_memory_region_list != NULL)
566 return lang_memory_region_list;
570 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
571 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
573 new = stat_alloc (sizeof (lang_memory_region_type));
575 new->name = xstrdup (name);
578 *lang_memory_region_list_tail = new;
579 lang_memory_region_list_tail = &new->next;
583 new->length = ~(bfd_size_type) 0;
585 new->had_full_message = FALSE;
590 static lang_memory_region_type *
591 lang_memory_default (asection *section)
593 lang_memory_region_type *p;
595 flagword sec_flags = section->flags;
597 /* Override SEC_DATA to mean a writable section. */
598 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
599 sec_flags |= SEC_DATA;
601 for (p = lang_memory_region_list; p != NULL; p = p->next)
603 if ((p->flags & sec_flags) != 0
604 && (p->not_flags & sec_flags) == 0)
609 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
612 static lang_output_section_statement_type *
613 lang_output_section_find_1 (const char *const name, int constraint)
615 lang_output_section_statement_type *lookup;
617 for (lookup = &lang_output_section_statement.head->output_section_statement;
619 lookup = lookup->next)
621 if (strcmp (name, lookup->name) == 0
622 && lookup->constraint != -1
623 && (constraint == 0 || constraint == lookup->constraint))
629 lang_output_section_statement_type *
630 lang_output_section_find (const char *const name)
632 return lang_output_section_find_1 (name, 0);
635 static lang_output_section_statement_type *
636 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
638 lang_output_section_statement_type *lookup;
640 lookup = lang_output_section_find_1 (name, constraint);
643 lookup = new_stat (lang_output_section_statement, stat_ptr);
644 lookup->region = NULL;
645 lookup->lma_region = NULL;
647 lookup->block_value = 1;
651 lookup->bfd_section = NULL;
652 lookup->processed = 0;
653 lookup->constraint = constraint;
654 lookup->sectype = normal_section;
655 lookup->addr_tree = NULL;
656 lang_list_init (&lookup->children);
658 lookup->memspec = NULL;
660 lookup->subsection_alignment = -1;
661 lookup->section_alignment = -1;
662 lookup->load_base = NULL;
663 lookup->update_dot_tree = NULL;
664 lookup->phdrs = NULL;
666 lang_statement_append (&lang_output_section_statement,
667 (lang_statement_union_type *) lookup,
668 (lang_statement_union_type **) &lookup->next);
673 lang_output_section_statement_type *
674 lang_output_section_statement_lookup (const char *const name)
676 return lang_output_section_statement_lookup_1 (name, 0);
679 /* A variant of lang_output_section_find used by place_orphan.
680 Returns the output statement that should precede a new output
681 statement for SEC. If an exact match is found on certain flags,
684 lang_output_section_statement_type *
685 lang_output_section_find_by_flags (const asection *sec,
686 lang_output_section_statement_type **exact)
688 lang_output_section_statement_type *first, *look, *found;
691 /* We know the first statement on this list is *ABS*. May as well
693 first = &lang_output_section_statement.head->output_section_statement;
696 /* First try for an exact match. */
698 for (look = first; look; look = look->next)
701 if (look->bfd_section != NULL)
702 flags = look->bfd_section->flags;
704 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
705 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
714 if (sec->flags & SEC_CODE)
716 /* Try for a rw code section. */
717 for (look = first; look; look = look->next)
720 if (look->bfd_section != NULL)
721 flags = look->bfd_section->flags;
723 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
724 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
730 if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
732 /* .rodata can go after .text, .sdata2 after .rodata. */
733 for (look = first; look; look = look->next)
736 if (look->bfd_section != NULL)
737 flags = look->bfd_section->flags;
739 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
741 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
747 if (sec->flags & SEC_SMALL_DATA)
749 /* .sdata goes after .data, .sbss after .sdata. */
750 for (look = first; look; look = look->next)
753 if (look->bfd_section != NULL)
754 flags = look->bfd_section->flags;
756 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
758 || ((look->flags & SEC_SMALL_DATA)
759 && !(sec->flags & SEC_HAS_CONTENTS)))
765 if (sec->flags & SEC_HAS_CONTENTS)
767 /* .data goes after .rodata. */
768 for (look = first; look; look = look->next)
771 if (look->bfd_section != NULL)
772 flags = look->bfd_section->flags;
774 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
775 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
781 /* .bss goes last. */
782 for (look = first; look; look = look->next)
785 if (look->bfd_section != NULL)
786 flags = look->bfd_section->flags;
788 if (!(flags & SEC_ALLOC))
795 /* Find the last output section before given output statement.
796 Used by place_orphan. */
799 output_prev_sec_find (lang_output_section_statement_type *os)
801 asection *s = (asection *) NULL;
802 lang_output_section_statement_type *lookup;
804 for (lookup = &lang_output_section_statement.head->output_section_statement;
806 lookup = lookup->next)
808 if (lookup->constraint == -1)
813 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
814 s = lookup->bfd_section;
820 lang_output_section_statement_type *
821 lang_insert_orphan (lang_input_statement_type *file,
824 lang_output_section_statement_type *after,
825 struct orphan_save *place,
827 lang_statement_list_type *add_child)
829 lang_statement_list_type *old;
830 lang_statement_list_type add;
832 etree_type *load_base;
833 lang_output_section_statement_type *os;
834 lang_output_section_statement_type **os_tail;
836 /* Start building a list of statements for this section.
837 First save the current statement pointer. */
840 /* If we have found an appropriate place for the output section
841 statements for this orphan, add them to our own private list,
842 inserting them later into the global statement list. */
846 lang_list_init (stat_ptr);
850 if (config.build_constructors)
852 /* If the name of the section is representable in C, then create
853 symbols to mark the start and the end of the section. */
854 for (ps = secname; *ps != '\0'; ps++)
855 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
862 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
863 symname[0] = bfd_get_symbol_leading_char (output_bfd);
864 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
865 e_align = exp_unop (ALIGN_K,
866 exp_intop ((bfd_vma) 1 << s->alignment_power));
867 lang_add_assignment (exp_assop ('=', ".", e_align));
868 lang_add_assignment (exp_assop ('=', symname,
869 exp_nameop (NAME, ".")));
873 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
874 address = exp_intop (0);
877 if (after != NULL && after->load_base != NULL)
879 etree_type *lma_from_vma;
880 lma_from_vma = exp_binop ('-', after->load_base,
881 exp_nameop (ADDR, after->name));
882 load_base = exp_binop ('+', lma_from_vma,
883 exp_nameop (ADDR, secname));
886 os_tail = ((lang_output_section_statement_type **)
887 lang_output_section_statement.tail);
888 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
891 if (add_child == NULL)
892 add_child = &os->children;
893 lang_add_section (add_child, s, os, file);
895 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
897 if (config.build_constructors && *ps == '\0')
901 /* lang_leave_ouput_section_statement resets stat_ptr.
902 Put stat_ptr back where we want it. */
906 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
907 symname[0] = bfd_get_symbol_leading_char (output_bfd);
908 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
909 lang_add_assignment (exp_assop ('=', symname,
910 exp_nameop (NAME, ".")));
913 /* Restore the global list pointer. */
917 if (after != NULL && os->bfd_section != NULL)
919 asection *snew, **pps;
921 snew = os->bfd_section;
923 /* Shuffle the bfd section list to make the output file look
924 neater. This is really only cosmetic. */
925 if (place->section == NULL
926 && after != (&lang_output_section_statement.head
927 ->output_section_statement))
929 asection *bfd_section = after->bfd_section;
931 /* If the output statement hasn't been used to place any input
932 sections (and thus doesn't have an output bfd_section),
933 look for the closest prior output statement having an
935 if (bfd_section == NULL)
936 bfd_section = output_prev_sec_find (after);
938 if (bfd_section != NULL && bfd_section != snew)
939 place->section = &bfd_section->next;
942 if (place->section == NULL)
943 place->section = &output_bfd->sections;
945 /* Unlink the section. */
946 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
948 bfd_section_list_remove (output_bfd, pps);
950 /* Now tack it back on in the right place. */
951 bfd_section_list_insert (output_bfd, place->section, snew);
953 /* Save the end of this list. Further ophans of this type will
954 follow the one we've just added. */
955 place->section = &snew->next;
957 /* The following is non-cosmetic. We try to put the output
958 statements in some sort of reasonable order here, because they
959 determine the final load addresses of the orphan sections.
960 In addition, placing output statements in the wrong order may
961 require extra segments. For instance, given a typical
962 situation of all read-only sections placed in one segment and
963 following that a segment containing all the read-write
964 sections, we wouldn't want to place an orphan read/write
965 section before or amongst the read-only ones. */
966 if (add.head != NULL)
968 lang_output_section_statement_type *newly_added_os;
970 if (place->stmt == NULL)
972 lang_statement_union_type **where;
973 lang_statement_union_type **assign = NULL;
975 /* Look for a suitable place for the new statement list.
976 The idea is to skip over anything that might be inside
977 a SECTIONS {} statement in a script, before we find
978 another output_section_statement. Assignments to "dot"
979 before an output section statement are assumed to
981 for (where = &after->header.next;
983 where = &(*where)->header.next)
985 switch ((*where)->header.type)
987 case lang_assignment_statement_enum:
990 lang_assignment_statement_type *ass;
991 ass = &(*where)->assignment_statement;
992 if (ass->exp->type.node_class != etree_assert
993 && ass->exp->assign.dst[0] == '.'
994 && ass->exp->assign.dst[1] == 0)
998 case lang_wild_statement_enum:
999 case lang_input_section_enum:
1000 case lang_object_symbols_statement_enum:
1001 case lang_fill_statement_enum:
1002 case lang_data_statement_enum:
1003 case lang_reloc_statement_enum:
1004 case lang_padding_statement_enum:
1005 case lang_constructors_statement_enum:
1008 case lang_output_section_statement_enum:
1011 case lang_input_statement_enum:
1012 case lang_address_statement_enum:
1013 case lang_target_statement_enum:
1014 case lang_output_statement_enum:
1015 case lang_group_statement_enum:
1016 case lang_afile_asection_pair_statement_enum:
1025 place->os_tail = &after->next;
1029 /* Put it after the last orphan statement we added. */
1030 *add.tail = *place->stmt;
1031 *place->stmt = add.head;
1034 /* Fix the global list pointer if we happened to tack our
1035 new list at the tail. */
1036 if (*old->tail == add.head)
1037 old->tail = add.tail;
1039 /* Save the end of this list. */
1040 place->stmt = add.tail;
1042 /* Do the same for the list of output section statements. */
1043 newly_added_os = *os_tail;
1045 newly_added_os->next = *place->os_tail;
1046 *place->os_tail = newly_added_os;
1047 place->os_tail = &newly_added_os->next;
1049 /* Fixing the global list pointer here is a little different.
1050 We added to the list in lang_enter_output_section_statement,
1051 trimmed off the new output_section_statment above when
1052 assigning *os_tail = NULL, but possibly added it back in
1053 the same place when assigning *place->os_tail. */
1054 if (*os_tail == NULL)
1055 lang_output_section_statement.tail
1056 = (lang_statement_union_type **) os_tail;
1063 lang_map_flags (flagword flag)
1065 if (flag & SEC_ALLOC)
1068 if (flag & SEC_CODE)
1071 if (flag & SEC_READONLY)
1074 if (flag & SEC_DATA)
1077 if (flag & SEC_LOAD)
1084 lang_memory_region_type *m;
1087 minfo (_("\nMemory Configuration\n\n"));
1088 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1089 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1091 for (m = lang_memory_region_list; m != NULL; m = m->next)
1096 fprintf (config.map_file, "%-16s ", m->name);
1098 sprintf_vma (buf, m->origin);
1099 minfo ("0x%s ", buf);
1107 minfo ("0x%V", m->length);
1108 if (m->flags || m->not_flags)
1116 lang_map_flags (m->flags);
1122 lang_map_flags (m->not_flags);
1129 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1131 if (! command_line.reduce_memory_overheads)
1133 obstack_begin (&map_obstack, 1000);
1134 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1135 bfd_map_over_sections (p, init_map_userdata, 0);
1136 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1138 print_statements ();
1142 init_map_userdata (abfd, sec, data)
1143 bfd *abfd ATTRIBUTE_UNUSED;
1145 void *data ATTRIBUTE_UNUSED;
1147 fat_section_userdata_type *new_data
1148 = ((fat_section_userdata_type *) (stat_alloc
1149 (sizeof (fat_section_userdata_type))));
1151 ASSERT (get_userdata (sec) == NULL);
1152 get_userdata (sec) = new_data;
1153 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1157 sort_def_symbol (hash_entry, info)
1158 struct bfd_link_hash_entry *hash_entry;
1159 void *info ATTRIBUTE_UNUSED;
1161 if (hash_entry->type == bfd_link_hash_defined
1162 || hash_entry->type == bfd_link_hash_defweak)
1164 struct fat_user_section_struct *ud;
1165 struct map_symbol_def *def;
1167 ud = get_userdata (hash_entry->u.def.section);
1170 /* ??? What do we have to do to initialize this beforehand? */
1171 /* The first time we get here is bfd_abs_section... */
1172 init_map_userdata (0, hash_entry->u.def.section, 0);
1173 ud = get_userdata (hash_entry->u.def.section);
1175 else if (!ud->map_symbol_def_tail)
1176 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1178 def = obstack_alloc (&map_obstack, sizeof *def);
1179 def->entry = hash_entry;
1180 *(ud->map_symbol_def_tail) = def;
1181 ud->map_symbol_def_tail = &def->next;
1186 /* Initialize an output section. */
1189 init_os (lang_output_section_statement_type *s)
1191 lean_section_userdata_type *new;
1193 if (s->bfd_section != NULL)
1196 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1197 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1199 new = stat_alloc (SECTION_USERDATA_SIZE);
1200 memset (new, 0, SECTION_USERDATA_SIZE);
1202 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1203 if (s->bfd_section == NULL)
1204 s->bfd_section = bfd_make_section (output_bfd, s->name);
1205 if (s->bfd_section == NULL)
1207 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1208 output_bfd->xvec->name, s->name);
1210 s->bfd_section->output_section = s->bfd_section;
1212 /* We initialize an output sections output offset to minus its own
1213 vma to allow us to output a section through itself. */
1214 s->bfd_section->output_offset = 0;
1215 get_userdata (s->bfd_section) = new;
1217 /* If there is a base address, make sure that any sections it might
1218 mention are initialized. */
1219 if (s->addr_tree != NULL)
1220 exp_init_os (s->addr_tree);
1222 if (s->load_base != NULL)
1223 exp_init_os (s->load_base);
1226 /* Make sure that all output sections mentioned in an expression are
1230 exp_init_os (etree_type *exp)
1232 switch (exp->type.node_class)
1235 exp_init_os (exp->assign.src);
1239 exp_init_os (exp->binary.lhs);
1240 exp_init_os (exp->binary.rhs);
1244 exp_init_os (exp->trinary.cond);
1245 exp_init_os (exp->trinary.lhs);
1246 exp_init_os (exp->trinary.rhs);
1250 exp_init_os (exp->assert_s.child);
1254 exp_init_os (exp->unary.child);
1258 switch (exp->type.node_code)
1264 lang_output_section_statement_type *os;
1266 os = lang_output_section_find (exp->name.name);
1267 if (os != NULL && os->bfd_section == NULL)
1279 section_already_linked (bfd *abfd, asection *sec, void *data)
1281 lang_input_statement_type *entry = data;
1283 /* If we are only reading symbols from this object, then we want to
1284 discard all sections. */
1285 if (entry->just_syms_flag)
1287 bfd_link_just_syms (sec, &link_info);
1291 bfd_section_already_linked (abfd, sec);
1294 /* The wild routines.
1296 These expand statements like *(.text) and foo.o to a list of
1297 explicit actions, like foo.o(.text), bar.o(.text) and
1298 foo.o(.text, .data). */
1300 /* Add SECTION to the output section OUTPUT. Do this by creating a
1301 lang_input_section statement which is placed at PTR. FILE is the
1302 input file which holds SECTION. */
1305 lang_add_section (lang_statement_list_type *ptr,
1307 lang_output_section_statement_type *output,
1308 lang_input_statement_type *file)
1310 flagword flags = section->flags;
1311 bfd_boolean discard;
1313 /* Discard sections marked with SEC_EXCLUDE. */
1314 discard = (flags & SEC_EXCLUDE) != 0;
1316 /* Discard input sections which are assigned to a section named
1317 DISCARD_SECTION_NAME. */
1318 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1321 /* Discard debugging sections if we are stripping debugging
1323 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1324 && (flags & SEC_DEBUGGING) != 0)
1329 if (section->output_section == NULL)
1331 /* This prevents future calls from assigning this section. */
1332 section->output_section = bfd_abs_section_ptr;
1337 if (section->output_section == NULL)
1340 lang_input_section_type *new;
1343 if (output->bfd_section == NULL)
1346 first = ! output->bfd_section->linker_has_input;
1347 output->bfd_section->linker_has_input = 1;
1349 /* Add a section reference to the list. */
1350 new = new_stat (lang_input_section, ptr);
1352 new->section = section;
1354 section->output_section = output->bfd_section;
1356 flags = section->flags;
1358 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1359 to an output section, because we want to be able to include a
1360 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1361 section (I don't know why we want to do this, but we do).
1362 build_link_order in ldwrite.c handles this case by turning
1363 the embedded SEC_NEVER_LOAD section into a fill. */
1365 flags &= ~ SEC_NEVER_LOAD;
1367 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1368 already been processed. One reason to do this is that on pe
1369 format targets, .text$foo sections go into .text and it's odd
1370 to see .text with SEC_LINK_ONCE set. */
1372 if (! link_info.relocatable)
1373 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1375 /* If this is not the first input section, and the SEC_READONLY
1376 flag is not currently set, then don't set it just because the
1377 input section has it set. */
1379 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
1380 flags &= ~ SEC_READONLY;
1382 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1384 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
1385 != (flags & (SEC_MERGE | SEC_STRINGS))
1386 || ((flags & SEC_MERGE)
1387 && output->bfd_section->entsize != section->entsize)))
1389 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1390 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1393 output->bfd_section->flags |= flags;
1395 if (flags & SEC_MERGE)
1396 output->bfd_section->entsize = section->entsize;
1398 /* If SEC_READONLY is not set in the input section, then clear
1399 it from the output section. */
1400 if ((section->flags & SEC_READONLY) == 0)
1401 output->bfd_section->flags &= ~SEC_READONLY;
1403 switch (output->sectype)
1405 case normal_section:
1410 case overlay_section:
1411 output->bfd_section->flags &= ~SEC_ALLOC;
1413 case noload_section:
1414 output->bfd_section->flags &= ~SEC_LOAD;
1415 output->bfd_section->flags |= SEC_NEVER_LOAD;
1419 /* Copy over SEC_SMALL_DATA. */
1420 if (section->flags & SEC_SMALL_DATA)
1421 output->bfd_section->flags |= SEC_SMALL_DATA;
1423 if (section->alignment_power > output->bfd_section->alignment_power)
1424 output->bfd_section->alignment_power = section->alignment_power;
1426 /* If supplied an alignment, then force it. */
1427 if (output->section_alignment != -1)
1428 output->bfd_section->alignment_power = output->section_alignment;
1430 if (section->flags & SEC_BLOCK)
1432 output->bfd_section->flags |= SEC_BLOCK;
1433 /* FIXME: This value should really be obtained from the bfd... */
1434 output->block_value = 128;
1439 /* Compare sections ASEC and BSEC according to SORT. */
1442 compare_section (sort_type sort, asection *asec, asection *bsec)
1451 case by_alignment_name:
1452 ret = (bfd_section_alignment (bsec->owner, bsec)
1453 - bfd_section_alignment (asec->owner, asec));
1459 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1460 bfd_get_section_name (bsec->owner, bsec));
1463 case by_name_alignment:
1464 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1465 bfd_get_section_name (bsec->owner, bsec));
1471 ret = (bfd_section_alignment (bsec->owner, bsec)
1472 - bfd_section_alignment (asec->owner, asec));
1479 /* Handle wildcard sorting. This returns the lang_input_section which
1480 should follow the one we are going to create for SECTION and FILE,
1481 based on the sorting requirements of WILD. It returns NULL if the
1482 new section should just go at the end of the current list. */
1484 static lang_statement_union_type *
1485 wild_sort (lang_wild_statement_type *wild,
1486 struct wildcard_list *sec,
1487 lang_input_statement_type *file,
1490 const char *section_name;
1491 lang_statement_union_type *l;
1493 if (!wild->filenames_sorted
1494 && (sec == NULL || sec->spec.sorted == none))
1497 section_name = bfd_get_section_name (file->the_bfd, section);
1498 for (l = wild->children.head; l != NULL; l = l->header.next)
1500 lang_input_section_type *ls;
1502 if (l->header.type != lang_input_section_enum)
1504 ls = &l->input_section;
1506 /* Sorting by filename takes precedence over sorting by section
1509 if (wild->filenames_sorted)
1511 const char *fn, *ln;
1515 /* The PE support for the .idata section as generated by
1516 dlltool assumes that files will be sorted by the name of
1517 the archive and then the name of the file within the
1520 if (file->the_bfd != NULL
1521 && bfd_my_archive (file->the_bfd) != NULL)
1523 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1528 fn = file->filename;
1532 if (ls->ifile->the_bfd != NULL
1533 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1535 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1540 ln = ls->ifile->filename;
1544 i = strcmp (fn, ln);
1553 fn = file->filename;
1555 ln = ls->ifile->filename;
1557 i = strcmp (fn, ln);
1565 /* Here either the files are not sorted by name, or we are
1566 looking at the sections for this file. */
1568 if (sec != NULL && sec->spec.sorted != none)
1570 if (compare_section (sec->spec.sorted, section,
1579 /* Expand a wild statement for a particular FILE. SECTION may be
1580 NULL, in which case it is a wild card. */
1583 output_section_callback (lang_wild_statement_type *ptr,
1584 struct wildcard_list *sec,
1586 lang_input_statement_type *file,
1589 lang_statement_union_type *before;
1591 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1592 if (unique_section_p (section))
1595 before = wild_sort (ptr, sec, file, section);
1597 /* Here BEFORE points to the lang_input_section which
1598 should follow the one we are about to add. If BEFORE
1599 is NULL, then the section should just go at the end
1600 of the current list. */
1603 lang_add_section (&ptr->children, section,
1604 (lang_output_section_statement_type *) output,
1608 lang_statement_list_type list;
1609 lang_statement_union_type **pp;
1611 lang_list_init (&list);
1612 lang_add_section (&list, section,
1613 (lang_output_section_statement_type *) output,
1616 /* If we are discarding the section, LIST.HEAD will
1618 if (list.head != NULL)
1620 ASSERT (list.head->header.next == NULL);
1622 for (pp = &ptr->children.head;
1624 pp = &(*pp)->header.next)
1625 ASSERT (*pp != NULL);
1627 list.head->header.next = *pp;
1633 /* Check if all sections in a wild statement for a particular FILE
1637 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1638 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1640 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1643 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1644 if (unique_section_p (section))
1647 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1648 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1651 /* This is passed a file name which must have been seen already and
1652 added to the statement tree. We will see if it has been opened
1653 already and had its symbols read. If not then we'll read it. */
1655 static lang_input_statement_type *
1656 lookup_name (const char *name)
1658 lang_input_statement_type *search;
1660 for (search = (lang_input_statement_type *) input_file_chain.head;
1662 search = (lang_input_statement_type *) search->next_real_file)
1664 /* Use the local_sym_name as the name of the file that has
1665 already been loaded as filename might have been transformed
1666 via the search directory lookup mechanism. */
1667 const char * filename = search->local_sym_name;
1669 if (filename == NULL && name == NULL)
1671 if (filename != NULL
1673 && strcmp (filename, name) == 0)
1678 search = new_afile (name, lang_input_file_is_search_file_enum,
1679 default_target, FALSE);
1681 /* If we have already added this file, or this file is not real
1682 (FIXME: can that ever actually happen?) or the name is NULL
1683 (FIXME: can that ever actually happen?) don't add this file. */
1686 || search->filename == NULL)
1689 if (! load_symbols (search, NULL))
1695 /* Save LIST as a list of libraries whose symbols should not be exported. */
1700 struct excluded_lib *next;
1702 static struct excluded_lib *excluded_libs;
1705 add_excluded_libs (const char *list)
1707 const char *p = list, *end;
1711 struct excluded_lib *entry;
1712 end = strpbrk (p, ",:");
1714 end = p + strlen (p);
1715 entry = xmalloc (sizeof (*entry));
1716 entry->next = excluded_libs;
1717 entry->name = xmalloc (end - p + 1);
1718 memcpy (entry->name, p, end - p);
1719 entry->name[end - p] = '\0';
1720 excluded_libs = entry;
1728 check_excluded_libs (bfd *abfd)
1730 struct excluded_lib *lib = excluded_libs;
1734 int len = strlen (lib->name);
1735 const char *filename = lbasename (abfd->filename);
1737 if (strcmp (lib->name, "ALL") == 0)
1739 abfd->no_export = TRUE;
1743 if (strncmp (lib->name, filename, len) == 0
1744 && (filename[len] == '\0'
1745 || (filename[len] == '.' && filename[len + 1] == 'a'
1746 && filename[len + 2] == '\0')))
1748 abfd->no_export = TRUE;
1756 /* Get the symbols for an input file. */
1759 load_symbols (lang_input_statement_type *entry,
1760 lang_statement_list_type *place)
1767 ldfile_open_file (entry);
1769 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1770 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1773 lang_statement_list_type *hold;
1774 bfd_boolean bad_load = TRUE;
1775 bfd_boolean save_ldlang_sysrooted_script;
1777 err = bfd_get_error ();
1779 /* See if the emulation has some special knowledge. */
1780 if (ldemul_unrecognized_file (entry))
1783 if (err == bfd_error_file_ambiguously_recognized)
1787 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1788 einfo (_("%B: matching formats:"), entry->the_bfd);
1789 for (p = matching; *p != NULL; p++)
1793 else if (err != bfd_error_file_not_recognized
1795 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1799 bfd_close (entry->the_bfd);
1800 entry->the_bfd = NULL;
1802 /* Try to interpret the file as a linker script. */
1803 ldfile_open_command_file (entry->filename);
1807 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1808 ldlang_sysrooted_script = entry->sysrooted;
1810 ldfile_assumed_script = TRUE;
1811 parser_input = input_script;
1812 /* We want to use the same -Bdynamic/-Bstatic as the one for
1814 config.dynamic_link = entry->dynamic;
1816 ldfile_assumed_script = FALSE;
1818 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1824 if (ldemul_recognized_file (entry))
1827 /* We don't call ldlang_add_file for an archive. Instead, the
1828 add_symbols entry point will call ldlang_add_file, via the
1829 add_archive_element callback, for each element of the archive
1831 switch (bfd_get_format (entry->the_bfd))
1837 ldlang_add_file (entry);
1838 if (trace_files || trace_file_tries)
1839 info_msg ("%I\n", entry);
1843 check_excluded_libs (entry->the_bfd);
1845 if (entry->whole_archive)
1848 bfd_boolean loaded = TRUE;
1852 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1857 if (! bfd_check_format (member, bfd_object))
1859 einfo (_("%F%B: member %B in archive is not an object\n"),
1860 entry->the_bfd, member);
1864 if (! ((*link_info.callbacks->add_archive_element)
1865 (&link_info, member, "--whole-archive")))
1868 if (! bfd_link_add_symbols (member, &link_info))
1870 einfo (_("%F%B: could not read symbols: %E\n"), member);
1875 entry->loaded = loaded;
1881 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1882 entry->loaded = TRUE;
1884 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1886 return entry->loaded;
1889 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1890 may be NULL, indicating that it is a wildcard. Separate
1891 lang_input_section statements are created for each part of the
1892 expansion; they are added after the wild statement S. OUTPUT is
1893 the output section. */
1896 wild (lang_wild_statement_type *s,
1897 const char *target ATTRIBUTE_UNUSED,
1898 lang_output_section_statement_type *output)
1900 struct wildcard_list *sec;
1902 walk_wild (s, output_section_callback, output);
1904 for (sec = s->section_list; sec != NULL; sec = sec->next)
1906 if (default_common_section != NULL)
1908 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1910 /* Remember the section that common is going to in case we
1911 later get something which doesn't know where to put it. */
1912 default_common_section = output;
1917 /* Return TRUE iff target is the sought target. */
1920 get_target (const bfd_target *target, void *data)
1922 const char *sought = data;
1924 return strcmp (target->name, sought) == 0;
1927 /* Like strcpy() but convert to lower case as well. */
1930 stricpy (char *dest, char *src)
1934 while ((c = *src++) != 0)
1935 *dest++ = TOLOWER (c);
1940 /* Remove the first occurrence of needle (if any) in haystack
1944 strcut (char *haystack, char *needle)
1946 haystack = strstr (haystack, needle);
1952 for (src = haystack + strlen (needle); *src;)
1953 *haystack++ = *src++;
1959 /* Compare two target format name strings.
1960 Return a value indicating how "similar" they are. */
1963 name_compare (char *first, char *second)
1969 copy1 = xmalloc (strlen (first) + 1);
1970 copy2 = xmalloc (strlen (second) + 1);
1972 /* Convert the names to lower case. */
1973 stricpy (copy1, first);
1974 stricpy (copy2, second);
1976 /* Remove size and endian strings from the name. */
1977 strcut (copy1, "big");
1978 strcut (copy1, "little");
1979 strcut (copy2, "big");
1980 strcut (copy2, "little");
1982 /* Return a value based on how many characters match,
1983 starting from the beginning. If both strings are
1984 the same then return 10 * their length. */
1985 for (result = 0; copy1[result] == copy2[result]; result++)
1986 if (copy1[result] == 0)
1998 /* Set by closest_target_match() below. */
1999 static const bfd_target *winner;
2001 /* Scan all the valid bfd targets looking for one that has the endianness
2002 requirement that was specified on the command line, and is the nearest
2003 match to the original output target. */
2006 closest_target_match (const bfd_target *target, void *data)
2008 const bfd_target *original = data;
2010 if (command_line.endian == ENDIAN_BIG
2011 && target->byteorder != BFD_ENDIAN_BIG)
2014 if (command_line.endian == ENDIAN_LITTLE
2015 && target->byteorder != BFD_ENDIAN_LITTLE)
2018 /* Must be the same flavour. */
2019 if (target->flavour != original->flavour)
2022 /* If we have not found a potential winner yet, then record this one. */
2029 /* Oh dear, we now have two potential candidates for a successful match.
2030 Compare their names and choose the better one. */
2031 if (name_compare (target->name, original->name)
2032 > name_compare (winner->name, original->name))
2035 /* Keep on searching until wqe have checked them all. */
2039 /* Return the BFD target format of the first input file. */
2042 get_first_input_target (void)
2044 char *target = NULL;
2046 LANG_FOR_EACH_INPUT_STATEMENT (s)
2048 if (s->header.type == lang_input_statement_enum
2051 ldfile_open_file (s);
2053 if (s->the_bfd != NULL
2054 && bfd_check_format (s->the_bfd, bfd_object))
2056 target = bfd_get_target (s->the_bfd);
2068 lang_get_output_target (void)
2072 /* Has the user told us which output format to use? */
2073 if (output_target != NULL)
2074 return output_target;
2076 /* No - has the current target been set to something other than
2078 if (current_target != default_target)
2079 return current_target;
2081 /* No - can we determine the format of the first input file? */
2082 target = get_first_input_target ();
2086 /* Failed - use the default output target. */
2087 return default_target;
2090 /* Open the output file. */
2093 open_output (const char *name)
2097 output_target = lang_get_output_target ();
2099 /* Has the user requested a particular endianness on the command
2101 if (command_line.endian != ENDIAN_UNSET)
2103 const bfd_target *target;
2104 enum bfd_endian desired_endian;
2106 /* Get the chosen target. */
2107 target = bfd_search_for_target (get_target, (void *) output_target);
2109 /* If the target is not supported, we cannot do anything. */
2112 if (command_line.endian == ENDIAN_BIG)
2113 desired_endian = BFD_ENDIAN_BIG;
2115 desired_endian = BFD_ENDIAN_LITTLE;
2117 /* See if the target has the wrong endianness. This should
2118 not happen if the linker script has provided big and
2119 little endian alternatives, but some scrips don't do
2121 if (target->byteorder != desired_endian)
2123 /* If it does, then see if the target provides
2124 an alternative with the correct endianness. */
2125 if (target->alternative_target != NULL
2126 && (target->alternative_target->byteorder == desired_endian))
2127 output_target = target->alternative_target->name;
2130 /* Try to find a target as similar as possible to
2131 the default target, but which has the desired
2132 endian characteristic. */
2133 bfd_search_for_target (closest_target_match,
2136 /* Oh dear - we could not find any targets that
2137 satisfy our requirements. */
2139 einfo (_("%P: warning: could not find any targets"
2140 " that match endianness requirement\n"));
2142 output_target = winner->name;
2148 output = bfd_openw (name, output_target);
2152 if (bfd_get_error () == bfd_error_invalid_target)
2153 einfo (_("%P%F: target %s not found\n"), output_target);
2155 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2158 delete_output_file_on_failure = TRUE;
2161 output->flags |= D_PAGED;
2164 if (! bfd_set_format (output, bfd_object))
2165 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2166 if (! bfd_set_arch_mach (output,
2167 ldfile_output_architecture,
2168 ldfile_output_machine))
2169 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2171 link_info.hash = bfd_link_hash_table_create (output);
2172 if (link_info.hash == NULL)
2173 einfo (_("%P%F: can not create link hash table: %E\n"));
2175 bfd_set_gp_size (output, g_switch_value);
2180 ldlang_open_output (lang_statement_union_type *statement)
2182 switch (statement->header.type)
2184 case lang_output_statement_enum:
2185 ASSERT (output_bfd == NULL);
2186 output_bfd = open_output (statement->output_statement.name);
2187 ldemul_set_output_arch ();
2188 if (config.magic_demand_paged && !link_info.relocatable)
2189 output_bfd->flags |= D_PAGED;
2191 output_bfd->flags &= ~D_PAGED;
2192 if (config.text_read_only)
2193 output_bfd->flags |= WP_TEXT;
2195 output_bfd->flags &= ~WP_TEXT;
2196 if (link_info.traditional_format)
2197 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2199 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2202 case lang_target_statement_enum:
2203 current_target = statement->target_statement.target;
2210 /* Convert between addresses in bytes and sizes in octets.
2211 For currently supported targets, octets_per_byte is always a power
2212 of two, so we can use shifts. */
2213 #define TO_ADDR(X) ((X) >> opb_shift)
2214 #define TO_SIZE(X) ((X) << opb_shift)
2216 /* Support the above. */
2217 static unsigned int opb_shift = 0;
2222 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2223 ldfile_output_machine);
2226 while ((x & 1) == 0)
2234 /* Open all the input files. */
2237 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2239 for (; s != NULL; s = s->header.next)
2241 switch (s->header.type)
2243 case lang_constructors_statement_enum:
2244 open_input_bfds (constructor_list.head, force);
2246 case lang_output_section_statement_enum:
2247 open_input_bfds (s->output_section_statement.children.head, force);
2249 case lang_wild_statement_enum:
2250 /* Maybe we should load the file's symbols. */
2251 if (s->wild_statement.filename
2252 && ! wildcardp (s->wild_statement.filename))
2253 lookup_name (s->wild_statement.filename);
2254 open_input_bfds (s->wild_statement.children.head, force);
2256 case lang_group_statement_enum:
2258 struct bfd_link_hash_entry *undefs;
2260 /* We must continually search the entries in the group
2261 until no new symbols are added to the list of undefined
2266 undefs = link_info.hash->undefs_tail;
2267 open_input_bfds (s->group_statement.children.head, TRUE);
2269 while (undefs != link_info.hash->undefs_tail);
2272 case lang_target_statement_enum:
2273 current_target = s->target_statement.target;
2275 case lang_input_statement_enum:
2276 if (s->input_statement.real)
2278 lang_statement_list_type add;
2280 s->input_statement.target = current_target;
2282 /* If we are being called from within a group, and this
2283 is an archive which has already been searched, then
2284 force it to be researched unless the whole archive
2285 has been loaded already. */
2287 && !s->input_statement.whole_archive
2288 && s->input_statement.loaded
2289 && bfd_check_format (s->input_statement.the_bfd,
2291 s->input_statement.loaded = FALSE;
2293 lang_list_init (&add);
2295 if (! load_symbols (&s->input_statement, &add))
2296 config.make_executable = FALSE;
2298 if (add.head != NULL)
2300 *add.tail = s->header.next;
2301 s->header.next = add.head;
2311 /* If there are [COMMONS] statements, put a wild one into the bss
2315 lang_reasonable_defaults (void)
2318 lang_output_section_statement_lookup (".text");
2319 lang_output_section_statement_lookup (".data");
2321 default_common_section = lang_output_section_statement_lookup (".bss");
2323 if (!placed_commons)
2325 lang_wild_statement_type *new =
2326 new_stat (lang_wild_statement,
2327 &default_common_section->children);
2329 new->section_name = "COMMON";
2330 new->filename = NULL;
2331 lang_list_init (&new->children);
2336 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2339 lang_track_definedness (const char *name)
2341 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2342 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2345 /* New-function for the definedness hash table. */
2347 static struct bfd_hash_entry *
2348 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2349 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2350 const char *name ATTRIBUTE_UNUSED)
2352 struct lang_definedness_hash_entry *ret
2353 = (struct lang_definedness_hash_entry *) entry;
2356 ret = (struct lang_definedness_hash_entry *)
2357 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2360 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2362 ret->iteration = -1;
2366 /* Return the iteration when the definition of NAME was last updated. A
2367 value of -1 means that the symbol is not defined in the linker script
2368 or the command line, but may be defined in the linker symbol table. */
2371 lang_symbol_definition_iteration (const char *name)
2373 struct lang_definedness_hash_entry *defentry
2374 = (struct lang_definedness_hash_entry *)
2375 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2377 /* We've already created this one on the presence of DEFINED in the
2378 script, so it can't be NULL unless something is borked elsewhere in
2380 if (defentry == NULL)
2383 return defentry->iteration;
2386 /* Update the definedness state of NAME. */
2389 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2391 struct lang_definedness_hash_entry *defentry
2392 = (struct lang_definedness_hash_entry *)
2393 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2395 /* We don't keep track of symbols not tested with DEFINED. */
2396 if (defentry == NULL)
2399 /* If the symbol was already defined, and not from an earlier statement
2400 iteration, don't update the definedness iteration, because that'd
2401 make the symbol seem defined in the linker script at this point, and
2402 it wasn't; it was defined in some object. If we do anyway, DEFINED
2403 would start to yield false before this point and the construct "sym =
2404 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2406 if (h->type != bfd_link_hash_undefined
2407 && h->type != bfd_link_hash_common
2408 && h->type != bfd_link_hash_new
2409 && defentry->iteration == -1)
2412 defentry->iteration = lang_statement_iteration;
2415 /* Add the supplied name to the symbol table as an undefined reference.
2416 This is a two step process as the symbol table doesn't even exist at
2417 the time the ld command line is processed. First we put the name
2418 on a list, then, once the output file has been opened, transfer the
2419 name to the symbol table. */
2421 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2423 #define ldlang_undef_chain_list_head entry_symbol.next
2426 ldlang_add_undef (const char *const name)
2428 ldlang_undef_chain_list_type *new =
2429 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2431 new->next = ldlang_undef_chain_list_head;
2432 ldlang_undef_chain_list_head = new;
2434 new->name = xstrdup (name);
2436 if (output_bfd != NULL)
2437 insert_undefined (new->name);
2440 /* Insert NAME as undefined in the symbol table. */
2443 insert_undefined (const char *name)
2445 struct bfd_link_hash_entry *h;
2447 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2449 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2450 if (h->type == bfd_link_hash_new)
2452 h->type = bfd_link_hash_undefined;
2453 h->u.undef.abfd = NULL;
2454 bfd_link_add_undef (link_info.hash, h);
2458 /* Run through the list of undefineds created above and place them
2459 into the linker hash table as undefined symbols belonging to the
2463 lang_place_undefineds (void)
2465 ldlang_undef_chain_list_type *ptr;
2467 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2468 insert_undefined (ptr->name);
2471 /* Check for all readonly or some readwrite sections. */
2474 check_input_sections
2475 (lang_statement_union_type *s,
2476 lang_output_section_statement_type *output_section_statement)
2478 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2480 switch (s->header.type)
2482 case lang_wild_statement_enum:
2483 walk_wild (&s->wild_statement, check_section_callback,
2484 output_section_statement);
2485 if (! output_section_statement->all_input_readonly)
2488 case lang_constructors_statement_enum:
2489 check_input_sections (constructor_list.head,
2490 output_section_statement);
2491 if (! output_section_statement->all_input_readonly)
2494 case lang_group_statement_enum:
2495 check_input_sections (s->group_statement.children.head,
2496 output_section_statement);
2497 if (! output_section_statement->all_input_readonly)
2506 /* Update wildcard statements if needed. */
2509 update_wild_statements (lang_statement_union_type *s)
2511 struct wildcard_list *sec;
2513 switch (sort_section)
2523 for (; s != NULL; s = s->header.next)
2525 switch (s->header.type)
2530 case lang_wild_statement_enum:
2531 sec = s->wild_statement.section_list;
2534 switch (sec->spec.sorted)
2537 sec->spec.sorted = sort_section;
2540 if (sort_section == by_alignment)
2541 sec->spec.sorted = by_name_alignment;
2544 if (sort_section == by_name)
2545 sec->spec.sorted = by_alignment_name;
2553 case lang_constructors_statement_enum:
2554 update_wild_statements (constructor_list.head);
2557 case lang_output_section_statement_enum:
2558 update_wild_statements
2559 (s->output_section_statement.children.head);
2562 case lang_group_statement_enum:
2563 update_wild_statements (s->group_statement.children.head);
2571 /* Open input files and attach to output sections. */
2574 map_input_to_output_sections
2575 (lang_statement_union_type *s, const char *target,
2576 lang_output_section_statement_type *os)
2578 for (; s != NULL; s = s->header.next)
2580 switch (s->header.type)
2582 case lang_wild_statement_enum:
2583 wild (&s->wild_statement, target, os);
2585 case lang_constructors_statement_enum:
2586 map_input_to_output_sections (constructor_list.head,
2590 case lang_output_section_statement_enum:
2591 if (s->output_section_statement.constraint)
2593 if (s->output_section_statement.constraint == -1)
2595 s->output_section_statement.all_input_readonly = TRUE;
2596 check_input_sections (s->output_section_statement.children.head,
2597 &s->output_section_statement);
2598 if ((s->output_section_statement.all_input_readonly
2599 && s->output_section_statement.constraint == ONLY_IF_RW)
2600 || (!s->output_section_statement.all_input_readonly
2601 && s->output_section_statement.constraint == ONLY_IF_RO))
2603 s->output_section_statement.constraint = -1;
2608 map_input_to_output_sections (s->output_section_statement.children.head,
2610 &s->output_section_statement);
2612 case lang_output_statement_enum:
2614 case lang_target_statement_enum:
2615 target = s->target_statement.target;
2617 case lang_group_statement_enum:
2618 map_input_to_output_sections (s->group_statement.children.head,
2622 case lang_data_statement_enum:
2623 /* Make sure that any sections mentioned in the expression
2625 exp_init_os (s->data_statement.exp);
2626 if (os != NULL && os->bfd_section == NULL)
2628 /* The output section gets contents, and then we inspect for
2629 any flags set in the input script which override any ALLOC. */
2630 os->bfd_section->flags |= SEC_HAS_CONTENTS;
2631 if (!(os->flags & SEC_NEVER_LOAD))
2632 os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2634 case lang_fill_statement_enum:
2635 case lang_input_section_enum:
2636 case lang_object_symbols_statement_enum:
2637 case lang_reloc_statement_enum:
2638 case lang_padding_statement_enum:
2639 case lang_input_statement_enum:
2640 if (os != NULL && os->bfd_section == NULL)
2643 case lang_assignment_statement_enum:
2644 if (os != NULL && os->bfd_section == NULL)
2647 /* Make sure that any sections mentioned in the assignment
2649 exp_init_os (s->assignment_statement.exp);
2651 case lang_afile_asection_pair_statement_enum:
2654 case lang_address_statement_enum:
2655 /* Mark the specified section with the supplied address. */
2657 lang_output_section_statement_type *aos
2658 = (lang_output_section_statement_lookup
2659 (s->address_statement.section_name));
2661 if (aos->bfd_section == NULL)
2663 aos->addr_tree = s->address_statement.address;
2670 /* An output section might have been removed after its statement was
2671 added. For example, ldemul_before_allocation can remove dynamic
2672 sections if they turn out to be not needed. Clean them up here. */
2675 strip_excluded_output_sections (void)
2677 lang_output_section_statement_type *os;
2679 for (os = &lang_output_section_statement.head->output_section_statement;
2685 if (os->constraint == -1)
2687 s = os->bfd_section;
2688 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2692 os->bfd_section = NULL;
2694 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2697 bfd_section_list_remove (output_bfd, p);
2698 output_bfd->section_count--;
2706 print_output_section_statement
2707 (lang_output_section_statement_type *output_section_statement)
2709 asection *section = output_section_statement->bfd_section;
2712 if (output_section_statement != abs_output_section)
2714 minfo ("\n%s", output_section_statement->name);
2716 if (section != NULL)
2718 print_dot = section->vma;
2720 len = strlen (output_section_statement->name);
2721 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2726 while (len < SECTION_NAME_MAP_LENGTH)
2732 minfo ("0x%V %W", section->vma, section->size);
2734 if (output_section_statement->load_base != NULL)
2738 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2739 "load base", lang_final_phase_enum);
2740 minfo (_(" load address 0x%V"), addr);
2747 print_statement_list (output_section_statement->children.head,
2748 output_section_statement);
2752 print_assignment (lang_assignment_statement_type *assignment,
2753 lang_output_section_statement_type *output_section)
2758 etree_value_type result;
2760 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2763 if (assignment->exp->type.node_class == etree_assert)
2766 tree = assignment->exp->assert_s.child;
2770 const char *dst = assignment->exp->assign.dst;
2771 is_dot = dst[0] == '.' && dst[1] == 0;
2772 tree = assignment->exp->assign.src;
2775 result = exp_fold_tree (tree, output_section, lang_final_phase_enum,
2776 print_dot, &print_dot);
2781 value = result.value + result.section->bfd_section->vma;
2783 minfo ("0x%V", value);
2796 exp_print_tree (assignment->exp);
2801 print_input_statement (lang_input_statement_type *statm)
2803 if (statm->filename != NULL)
2805 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2809 /* Print all symbols defined in a particular section. This is called
2810 via bfd_link_hash_traverse, or by print_all_symbols. */
2813 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2815 asection *sec = ptr;
2817 if ((hash_entry->type == bfd_link_hash_defined
2818 || hash_entry->type == bfd_link_hash_defweak)
2819 && sec == hash_entry->u.def.section)
2823 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2826 (hash_entry->u.def.value
2827 + hash_entry->u.def.section->output_offset
2828 + hash_entry->u.def.section->output_section->vma));
2830 minfo (" %T\n", hash_entry->root.string);
2837 print_all_symbols (sec)
2840 struct fat_user_section_struct *ud = get_userdata (sec);
2841 struct map_symbol_def *def;
2846 *ud->map_symbol_def_tail = 0;
2847 for (def = ud->map_symbol_def_head; def; def = def->next)
2848 print_one_symbol (def->entry, sec);
2851 /* Print information about an input section to the map file. */
2854 print_input_section (lang_input_section_type *in)
2856 asection *i = in->section;
2857 bfd_size_type size = i->size;
2866 minfo ("%s", i->name);
2868 len = 1 + strlen (i->name);
2869 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2874 while (len < SECTION_NAME_MAP_LENGTH)
2880 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2881 addr = i->output_section->vma + i->output_offset;
2888 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2890 if (size != i->rawsize && i->rawsize != 0)
2892 len = SECTION_NAME_MAP_LENGTH + 3;
2904 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2907 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2909 if (command_line.reduce_memory_overheads)
2910 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2912 print_all_symbols (i);
2914 print_dot = addr + TO_ADDR (size);
2920 print_fill_statement (lang_fill_statement_type *fill)
2924 fputs (" FILL mask 0x", config.map_file);
2925 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2926 fprintf (config.map_file, "%02x", *p);
2927 fputs ("\n", config.map_file);
2931 print_data_statement (lang_data_statement_type *data)
2939 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2942 addr = data->output_vma;
2943 if (data->output_section != NULL)
2944 addr += data->output_section->vma;
2972 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2974 if (data->exp->type.node_class != etree_value)
2977 exp_print_tree (data->exp);
2982 print_dot = addr + TO_ADDR (size);
2985 /* Print an address statement. These are generated by options like
2989 print_address_statement (lang_address_statement_type *address)
2991 minfo (_("Address of section %s set to "), address->section_name);
2992 exp_print_tree (address->address);
2996 /* Print a reloc statement. */
2999 print_reloc_statement (lang_reloc_statement_type *reloc)
3006 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3009 addr = reloc->output_vma;
3010 if (reloc->output_section != NULL)
3011 addr += reloc->output_section->vma;
3013 size = bfd_get_reloc_size (reloc->howto);
3015 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3017 if (reloc->name != NULL)
3018 minfo ("%s+", reloc->name);
3020 minfo ("%s+", reloc->section->name);
3022 exp_print_tree (reloc->addend_exp);
3026 print_dot = addr + TO_ADDR (size);
3030 print_padding_statement (lang_padding_statement_type *s)
3038 len = sizeof " *fill*" - 1;
3039 while (len < SECTION_NAME_MAP_LENGTH)
3045 addr = s->output_offset;
3046 if (s->output_section != NULL)
3047 addr += s->output_section->vma;
3048 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3050 if (s->fill->size != 0)
3054 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3055 fprintf (config.map_file, "%02x", *p);
3060 print_dot = addr + TO_ADDR (s->size);
3064 print_wild_statement (lang_wild_statement_type *w,
3065 lang_output_section_statement_type *os)
3067 struct wildcard_list *sec;
3071 if (w->filenames_sorted)
3073 if (w->filename != NULL)
3074 minfo ("%s", w->filename);
3077 if (w->filenames_sorted)
3081 for (sec = w->section_list; sec; sec = sec->next)
3083 if (sec->spec.sorted)
3085 if (sec->spec.exclude_name_list != NULL)
3088 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3089 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3090 minfo (" %s", tmp->name);
3093 if (sec->spec.name != NULL)
3094 minfo ("%s", sec->spec.name);
3097 if (sec->spec.sorted)
3106 print_statement_list (w->children.head, os);
3109 /* Print a group statement. */
3112 print_group (lang_group_statement_type *s,
3113 lang_output_section_statement_type *os)
3115 fprintf (config.map_file, "START GROUP\n");
3116 print_statement_list (s->children.head, os);
3117 fprintf (config.map_file, "END GROUP\n");
3120 /* Print the list of statements in S.
3121 This can be called for any statement type. */
3124 print_statement_list (lang_statement_union_type *s,
3125 lang_output_section_statement_type *os)
3129 print_statement (s, os);
3134 /* Print the first statement in statement list S.
3135 This can be called for any statement type. */
3138 print_statement (lang_statement_union_type *s,
3139 lang_output_section_statement_type *os)
3141 switch (s->header.type)
3144 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3147 case lang_constructors_statement_enum:
3148 if (constructor_list.head != NULL)
3150 if (constructors_sorted)
3151 minfo (" SORT (CONSTRUCTORS)\n");
3153 minfo (" CONSTRUCTORS\n");
3154 print_statement_list (constructor_list.head, os);
3157 case lang_wild_statement_enum:
3158 print_wild_statement (&s->wild_statement, os);
3160 case lang_address_statement_enum:
3161 print_address_statement (&s->address_statement);
3163 case lang_object_symbols_statement_enum:
3164 minfo (" CREATE_OBJECT_SYMBOLS\n");
3166 case lang_fill_statement_enum:
3167 print_fill_statement (&s->fill_statement);
3169 case lang_data_statement_enum:
3170 print_data_statement (&s->data_statement);
3172 case lang_reloc_statement_enum:
3173 print_reloc_statement (&s->reloc_statement);
3175 case lang_input_section_enum:
3176 print_input_section (&s->input_section);
3178 case lang_padding_statement_enum:
3179 print_padding_statement (&s->padding_statement);
3181 case lang_output_section_statement_enum:
3182 print_output_section_statement (&s->output_section_statement);
3184 case lang_assignment_statement_enum:
3185 print_assignment (&s->assignment_statement, os);
3187 case lang_target_statement_enum:
3188 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3190 case lang_output_statement_enum:
3191 minfo ("OUTPUT(%s", s->output_statement.name);
3192 if (output_target != NULL)
3193 minfo (" %s", output_target);
3196 case lang_input_statement_enum:
3197 print_input_statement (&s->input_statement);
3199 case lang_group_statement_enum:
3200 print_group (&s->group_statement, os);
3202 case lang_afile_asection_pair_statement_enum:
3209 print_statements (void)
3211 print_statement_list (statement_list.head, abs_output_section);
3214 /* Print the first N statements in statement list S to STDERR.
3215 If N == 0, nothing is printed.
3216 If N < 0, the entire list is printed.
3217 Intended to be called from GDB. */
3220 dprint_statement (lang_statement_union_type *s, int n)
3222 FILE *map_save = config.map_file;
3224 config.map_file = stderr;
3227 print_statement_list (s, abs_output_section);
3230 while (s && --n >= 0)
3232 print_statement (s, abs_output_section);
3237 config.map_file = map_save;
3241 insert_pad (lang_statement_union_type **ptr,
3243 unsigned int alignment_needed,
3244 asection *output_section,
3247 static fill_type zero_fill = { 1, { 0 } };
3248 lang_statement_union_type *pad;
3250 pad = ((lang_statement_union_type *)
3251 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
3252 if (ptr != &statement_list.head
3253 && pad->header.type == lang_padding_statement_enum
3254 && pad->padding_statement.output_section == output_section)
3256 /* Use the existing pad statement. The above test on output
3257 section is probably redundant, but it doesn't hurt to check. */
3261 /* Make a new padding statement, linked into existing chain. */
3262 pad = stat_alloc (sizeof (lang_padding_statement_type));
3263 pad->header.next = *ptr;
3265 pad->header.type = lang_padding_statement_enum;
3266 pad->padding_statement.output_section = output_section;
3269 pad->padding_statement.fill = fill;
3271 pad->padding_statement.output_offset = dot - output_section->vma;
3272 pad->padding_statement.size = alignment_needed;
3273 output_section->size += alignment_needed;
3276 /* Work out how much this section will move the dot point. */
3280 (lang_statement_union_type **this_ptr,
3281 lang_output_section_statement_type *output_section_statement,
3285 lang_input_section_type *is = &((*this_ptr)->input_section);
3286 asection *i = is->section;
3288 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
3290 unsigned int alignment_needed;
3293 /* Align this section first to the input sections requirement,
3294 then to the output section's requirement. If this alignment
3295 is greater than any seen before, then record it too. Perform
3296 the alignment by inserting a magic 'padding' statement. */
3298 if (output_section_statement->subsection_alignment != -1)
3299 i->alignment_power = output_section_statement->subsection_alignment;
3301 o = output_section_statement->bfd_section;
3302 if (o->alignment_power < i->alignment_power)
3303 o->alignment_power = i->alignment_power;
3305 alignment_needed = align_power (dot, i->alignment_power) - dot;
3307 if (alignment_needed != 0)
3309 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
3310 dot += alignment_needed;
3313 /* Remember where in the output section this input section goes. */
3315 i->output_offset = dot - o->vma;
3317 /* Mark how big the output section must be to contain this now. */
3318 dot += TO_ADDR (i->size);
3319 o->size = TO_SIZE (dot - o->vma);
3323 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
3329 #define IGNORE_SECTION(s) \
3330 ((s->flags & SEC_NEVER_LOAD) != 0 \
3331 || (s->flags & SEC_ALLOC) == 0 \
3332 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
3333 && (s->flags & SEC_LOAD) == 0) \
3336 /* Check to see if any allocated sections overlap with other allocated
3337 sections. This can happen if a linker script specifies the output
3338 section addresses of the two sections. */
3341 lang_check_section_addresses (void)
3345 /* Scan all sections in the output list. */
3346 for (s = output_bfd->sections; s != NULL; s = s->next)
3350 /* Ignore sections which are not loaded or which have no contents. */
3351 if (IGNORE_SECTION (s))
3354 /* Once we reach section 's' stop our seach. This prevents two
3355 warning messages from being produced, one for 'section A overlaps
3356 section B' and one for 'section B overlaps section A'. */
3357 for (os = output_bfd->sections; os != s; os = os->next)
3364 /* Only consider loadable sections with real contents. */
3365 if (IGNORE_SECTION (os))
3368 /* We must check the sections' LMA addresses not their
3369 VMA addresses because overlay sections can have
3370 overlapping VMAs but they must have distinct LMAs. */
3371 s_start = bfd_section_lma (output_bfd, s);
3372 os_start = bfd_section_lma (output_bfd, os);
3373 s_end = s_start + TO_ADDR (s->size) - 1;
3374 os_end = os_start + TO_ADDR (os->size) - 1;
3376 /* Look for an overlap. */
3377 if ((s_end < os_start) || (s_start > os_end))
3381 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
3382 s->name, s_start, s_end, os->name, os_start, os_end);
3384 /* Once we have found one overlap for this section,
3385 stop looking for others. */
3391 /* Make sure the new address is within the region. We explicitly permit the
3392 current address to be at the exact end of the region when the address is
3393 non-zero, in case the region is at the end of addressable memory and the
3394 calculation wraps around. */
3397 os_region_check (lang_output_section_statement_type *os,
3398 lang_memory_region_type *region,
3402 if ((region->current < region->origin
3403 || (region->current - region->origin > region->length))
3404 && ((region->current != region->origin + region->length)
3409 einfo (_("%X%P: address 0x%v of %B section %s"
3410 " is not within region %s\n"),
3412 os->bfd_section->owner,
3413 os->bfd_section->name,
3418 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3420 os->bfd_section->owner,
3421 os->bfd_section->name);
3423 /* Reset the region pointer. */
3424 region->current = region->origin;
3428 /* Set the sizes for all the output sections. */
3431 lang_size_sections_1
3432 (lang_statement_union_type *s,
3433 lang_output_section_statement_type *output_section_statement,
3434 lang_statement_union_type **prev,
3438 bfd_boolean check_regions)
3440 /* Size up the sections from their constituent parts. */
3441 for (; s != NULL; s = s->header.next)
3443 switch (s->header.type)
3445 case lang_output_section_statement_enum:
3448 lang_output_section_statement_type *os;
3450 os = &s->output_section_statement;
3451 if (os->bfd_section == NULL)
3452 /* This section was never actually created. */
3455 /* If this is a COFF shared library section, use the size and
3456 address from the input section. FIXME: This is COFF
3457 specific; it would be cleaner if there were some other way
3458 to do this, but nothing simple comes to mind. */
3459 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3463 if (os->children.head == NULL
3464 || os->children.head->header.next != NULL
3465 || (os->children.head->header.type
3466 != lang_input_section_enum))
3467 einfo (_("%P%X: Internal error on COFF shared library"
3468 " section %s\n"), os->name);
3470 input = os->children.head->input_section.section;
3471 bfd_set_section_vma (os->bfd_section->owner,
3473 bfd_section_vma (input->owner, input));
3474 os->bfd_section->size = input->size;
3478 if (bfd_is_abs_section (os->bfd_section))
3480 /* No matter what happens, an abs section starts at zero. */
3481 ASSERT (os->bfd_section->vma == 0);
3485 if (os->addr_tree == NULL)
3487 /* No address specified for this section, get one
3488 from the region specification. */
3489 if (os->region == NULL
3490 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3491 && os->region->name[0] == '*'
3492 && strcmp (os->region->name,
3493 DEFAULT_MEMORY_REGION) == 0))
3495 os->region = lang_memory_default (os->bfd_section);
3498 /* If a loadable section is using the default memory
3499 region, and some non default memory regions were
3500 defined, issue an error message. */
3501 if (!IGNORE_SECTION (os->bfd_section)
3502 && ! link_info.relocatable
3504 && strcmp (os->region->name,
3505 DEFAULT_MEMORY_REGION) == 0
3506 && lang_memory_region_list != NULL
3507 && (strcmp (lang_memory_region_list->name,
3508 DEFAULT_MEMORY_REGION) != 0
3509 || lang_memory_region_list->next != NULL))
3511 /* By default this is an error rather than just a
3512 warning because if we allocate the section to the
3513 default memory region we can end up creating an
3514 excessively large binary, or even seg faulting when
3515 attempting to perform a negative seek. See
3516 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3517 for an example of this. This behaviour can be
3518 overridden by the using the --no-check-sections
3520 if (command_line.check_section_addresses)
3521 einfo (_("%P%F: error: no memory region specified"
3522 " for loadable section `%s'\n"),
3523 bfd_get_section_name (output_bfd,
3526 einfo (_("%P: warning: no memory region specified"
3527 " for loadable section `%s'\n"),
3528 bfd_get_section_name (output_bfd,
3532 dot = os->region->current;
3534 if (os->section_alignment == -1)
3539 dot = align_power (dot,
3540 os->bfd_section->alignment_power);
3542 if (dot != olddot && config.warn_section_align)
3543 einfo (_("%P: warning: changing start of section"
3544 " %s by %u bytes\n"),
3545 os->name, (unsigned int) (dot - olddot));
3553 r = exp_fold_tree (os->addr_tree,
3555 lang_allocating_phase_enum,
3560 einfo (_("%F%S: non constant or forward reference"
3561 " address expression for section %s\n"),
3564 dot = r.value + r.section->bfd_section->vma;
3567 /* The section starts here.
3568 First, align to what the section needs. */
3570 if (os->section_alignment != -1)
3571 dot = align_power (dot, os->section_alignment);
3573 bfd_set_section_vma (0, os->bfd_section, dot);
3575 os->bfd_section->output_offset = 0;
3578 lang_size_sections_1 (os->children.head, os, &os->children.head,
3579 os->fill, dot, relax, check_regions);
3581 /* Put the section within the requested block size, or
3582 align at the block boundary. */
3583 after = ((os->bfd_section->vma
3584 + TO_ADDR (os->bfd_section->size)
3585 + os->block_value - 1)
3586 & - (bfd_vma) os->block_value);
3588 if (bfd_is_abs_section (os->bfd_section))
3589 ASSERT (after == os->bfd_section->vma);
3591 os->bfd_section->size
3592 = TO_SIZE (after - os->bfd_section->vma);
3594 dot = os->bfd_section->vma;
3595 /* .tbss sections effectively have zero size. */
3596 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3597 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3598 || link_info.relocatable)
3599 dot += TO_ADDR (os->bfd_section->size);
3603 if (os->update_dot_tree != 0)
3604 exp_fold_tree (os->update_dot_tree, abs_output_section,
3605 lang_allocating_phase_enum, dot, &dot);
3607 /* Update dot in the region ?
3608 We only do this if the section is going to be allocated,
3609 since unallocated sections do not contribute to the region's
3610 overall size in memory.
3612 If the SEC_NEVER_LOAD bit is not set, it will affect the
3613 addresses of sections after it. We have to update
3615 if (os->region != NULL
3616 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3617 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3619 os->region->current = dot;
3622 /* Make sure the new address is within the region. */
3623 os_region_check (os, os->region, os->addr_tree,
3624 os->bfd_section->vma);
3626 /* If there's no load address specified, use the run
3627 region as the load region. */
3628 if (os->lma_region == NULL && os->load_base == NULL)
3629 os->lma_region = os->region;
3631 if (os->lma_region != NULL && os->lma_region != os->region)
3633 /* Set load_base, which will be handled later. */
3634 os->load_base = exp_intop (os->lma_region->current);
3635 os->lma_region->current +=
3636 TO_ADDR (os->bfd_section->size);
3638 os_region_check (os, os->lma_region, NULL,
3639 os->bfd_section->lma);
3645 case lang_constructors_statement_enum:
3646 dot = lang_size_sections_1 (constructor_list.head,
3647 output_section_statement,
3648 &s->wild_statement.children.head,
3649 fill, dot, relax, check_regions);
3652 case lang_data_statement_enum:
3654 unsigned int size = 0;
3656 s->data_statement.output_vma =
3657 dot - output_section_statement->bfd_section->vma;
3658 s->data_statement.output_section =
3659 output_section_statement->bfd_section;
3661 /* We might refer to provided symbols in the expression, and
3662 need to mark them as needed. */
3663 exp_fold_tree (s->data_statement.exp, abs_output_section,
3664 lang_allocating_phase_enum, dot, &dot);
3666 switch (s->data_statement.type)
3684 if (size < TO_SIZE ((unsigned) 1))
3685 size = TO_SIZE ((unsigned) 1);
3686 dot += TO_ADDR (size);
3687 output_section_statement->bfd_section->size += size;
3691 case lang_reloc_statement_enum:
3695 s->reloc_statement.output_vma =
3696 dot - output_section_statement->bfd_section->vma;
3697 s->reloc_statement.output_section =
3698 output_section_statement->bfd_section;
3699 size = bfd_get_reloc_size (s->reloc_statement.howto);
3700 dot += TO_ADDR (size);
3701 output_section_statement->bfd_section->size += size;
3705 case lang_wild_statement_enum:
3707 dot = lang_size_sections_1 (s->wild_statement.children.head,
3708 output_section_statement,
3709 &s->wild_statement.children.head,
3710 fill, dot, relax, check_regions);
3714 case lang_object_symbols_statement_enum:
3715 link_info.create_object_symbols_section =
3716 output_section_statement->bfd_section;
3718 case lang_output_statement_enum:
3719 case lang_target_statement_enum:
3721 case lang_input_section_enum:
3725 i = (*prev)->input_section.section;
3730 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3731 einfo (_("%P%F: can't relax section: %E\n"));
3735 dot = size_input_section (prev, output_section_statement,
3736 output_section_statement->fill, dot);
3739 case lang_input_statement_enum:
3741 case lang_fill_statement_enum:
3742 s->fill_statement.output_section =
3743 output_section_statement->bfd_section;
3745 fill = s->fill_statement.fill;
3747 case lang_assignment_statement_enum:
3749 bfd_vma newdot = dot;
3751 exp_fold_tree (s->assignment_statement.exp,
3752 output_section_statement,
3753 lang_allocating_phase_enum,
3759 if (output_section_statement == abs_output_section)
3761 /* If we don't have an output section, then just adjust
3762 the default memory address. */
3763 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3764 FALSE)->current = newdot;
3768 /* Insert a pad after this statement. We can't
3769 put the pad before when relaxing, in case the
3770 assignment references dot. */
3771 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3772 output_section_statement->bfd_section, dot);
3774 /* Don't neuter the pad below when relaxing. */
3778 /* If dot is advanced, this implies that the section should
3779 have space allocated to it, unless the user has explicitly
3780 stated that the section should never be loaded. */
3781 if (!(output_section_statement->flags
3782 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3783 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3790 case lang_padding_statement_enum:
3791 /* If this is the first time lang_size_sections is called,
3792 we won't have any padding statements. If this is the
3793 second or later passes when relaxing, we should allow
3794 padding to shrink. If padding is needed on this pass, it
3795 will be added back in. */
3796 s->padding_statement.size = 0;
3798 /* Make sure output_offset is valid. If relaxation shrinks
3799 the section and this pad isn't needed, it's possible to
3800 have output_offset larger than the final size of the
3801 section. bfd_set_section_contents will complain even for
3802 a pad size of zero. */
3803 s->padding_statement.output_offset
3804 = dot - output_section_statement->bfd_section->vma;
3807 case lang_group_statement_enum:
3808 dot = lang_size_sections_1 (s->group_statement.children.head,
3809 output_section_statement,
3810 &s->group_statement.children.head,
3811 fill, dot, relax, check_regions);
3818 /* We can only get here when relaxing is turned on. */
3819 case lang_address_statement_enum:
3822 prev = &s->header.next;
3829 (lang_statement_union_type *s,
3830 lang_output_section_statement_type *output_section_statement,
3831 lang_statement_union_type **prev,
3835 bfd_boolean check_regions)
3839 /* Callers of exp_fold_tree need to increment this. */
3840 lang_statement_iteration++;
3842 exp_data_seg.phase = exp_dataseg_none;
3843 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3844 dot, relax, check_regions);
3845 if (exp_data_seg.phase == exp_dataseg_end_seen
3846 && link_info.relro && exp_data_seg.relro_end)
3848 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3849 to put exp_data_seg.relro on a (common) page boundary. */
3850 bfd_vma old_base, relro_end;
3852 exp_data_seg.phase = exp_dataseg_relro_adjust;
3853 old_base = exp_data_seg.base;
3854 exp_data_seg.base += (-exp_data_seg.relro_end
3855 & (exp_data_seg.pagesize - 1));
3856 /* Compute the expected PT_GNU_RELRO segment end. */
3857 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
3858 & (exp_data_seg.pagesize - 1);
3859 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3860 dot, relax, check_regions);
3861 if (exp_data_seg.relro_end > relro_end)
3863 /* The alignment of sections between DATA_SEGMENT_ALIGN
3864 and DATA_SEGMENT_RELRO_END caused huge padding to be
3865 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
3867 unsigned int max_alignment_power = 0;
3869 /* Find maximum alignment power of sections between
3870 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
3871 for (sec = output_bfd->sections; sec; sec = sec->next)
3872 if (sec->vma >= exp_data_seg.base
3873 && sec->vma < exp_data_seg.relro_end
3874 && sec->alignment_power > max_alignment_power)
3875 max_alignment_power = sec->alignment_power;
3877 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
3879 if (exp_data_seg.base - (1 << max_alignment_power)
3881 exp_data_seg.base += exp_data_seg.pagesize;
3882 exp_data_seg.base -= (1 << max_alignment_power);
3883 result = lang_size_sections_1 (s, output_section_statement,
3884 prev, fill, dot, relax,
3888 link_info.relro_start = exp_data_seg.base;
3889 link_info.relro_end = exp_data_seg.relro_end;
3891 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3893 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3894 a page could be saved in the data segment. */
3895 bfd_vma first, last;
3897 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3898 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3900 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3901 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3902 && first + last <= exp_data_seg.pagesize)
3904 exp_data_seg.phase = exp_dataseg_adjust;
3905 lang_statement_iteration++;
3906 result = lang_size_sections_1 (s, output_section_statement, prev,
3907 fill, dot, relax, check_regions);
3914 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3917 lang_do_assignments_1
3918 (lang_statement_union_type *s,
3919 lang_output_section_statement_type *output_section_statement,
3923 for (; s != NULL; s = s->header.next)
3925 switch (s->header.type)
3927 case lang_constructors_statement_enum:
3928 dot = lang_do_assignments_1 (constructor_list.head,
3929 output_section_statement,
3934 case lang_output_section_statement_enum:
3936 lang_output_section_statement_type *os;
3938 os = &(s->output_section_statement);
3939 if (os->bfd_section != NULL)
3941 dot = os->bfd_section->vma;
3942 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3943 /* .tbss sections effectively have zero size. */
3944 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3945 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3946 || link_info.relocatable)
3947 dot += TO_ADDR (os->bfd_section->size);
3951 /* If nothing has been placed into the output section then
3952 it won't have a bfd_section. */
3953 if (os->bfd_section)
3955 os->bfd_section->lma
3956 = exp_get_abs_int (os->load_base, 0, "load base",
3957 lang_final_phase_enum);
3962 case lang_wild_statement_enum:
3964 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3965 output_section_statement,
3970 case lang_object_symbols_statement_enum:
3971 case lang_output_statement_enum:
3972 case lang_target_statement_enum:
3974 case lang_common_statement_enum:
3977 case lang_data_statement_enum:
3979 etree_value_type value;
3981 value = exp_fold_tree (s->data_statement.exp,
3983 lang_final_phase_enum, dot, &dot);
3985 einfo (_("%F%P: invalid data statement\n"));
3986 s->data_statement.value
3987 = value.value + value.section->bfd_section->vma;
3991 switch (s->data_statement.type)
4009 if (size < TO_SIZE ((unsigned) 1))
4010 size = TO_SIZE ((unsigned) 1);
4011 dot += TO_ADDR (size);
4015 case lang_reloc_statement_enum:
4017 etree_value_type value;
4019 value = exp_fold_tree (s->reloc_statement.addend_exp,
4021 lang_final_phase_enum, dot, &dot);
4022 s->reloc_statement.addend_value = value.value;
4024 einfo (_("%F%P: invalid reloc statement\n"));
4026 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4029 case lang_input_section_enum:
4031 asection *in = s->input_section.section;
4033 if ((in->flags & SEC_EXCLUDE) == 0)
4034 dot += TO_ADDR (in->size);
4038 case lang_input_statement_enum:
4040 case lang_fill_statement_enum:
4041 fill = s->fill_statement.fill;
4043 case lang_assignment_statement_enum:
4045 exp_fold_tree (s->assignment_statement.exp,
4046 output_section_statement,
4047 lang_final_phase_enum,
4053 case lang_padding_statement_enum:
4054 dot += TO_ADDR (s->padding_statement.size);
4057 case lang_group_statement_enum:
4058 dot = lang_do_assignments_1 (s->group_statement.children.head,
4059 output_section_statement,
4067 case lang_address_statement_enum:
4077 (lang_statement_union_type *s,
4078 lang_output_section_statement_type *output_section_statement,
4082 /* Callers of exp_fold_tree need to increment this. */
4083 lang_statement_iteration++;
4084 lang_do_assignments_1 (s, output_section_statement, fill, dot);
4087 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4088 operator .startof. (section_name), it produces an undefined symbol
4089 .startof.section_name. Similarly, when it sees
4090 .sizeof. (section_name), it produces an undefined symbol
4091 .sizeof.section_name. For all the output sections, we look for
4092 such symbols, and set them to the correct value. */
4095 lang_set_startof (void)
4099 if (link_info.relocatable)
4102 for (s = output_bfd->sections; s != NULL; s = s->next)
4104 const char *secname;
4106 struct bfd_link_hash_entry *h;
4108 secname = bfd_get_section_name (output_bfd, s);
4109 buf = xmalloc (10 + strlen (secname));
4111 sprintf (buf, ".startof.%s", secname);
4112 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4113 if (h != NULL && h->type == bfd_link_hash_undefined)
4115 h->type = bfd_link_hash_defined;
4116 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4117 h->u.def.section = bfd_abs_section_ptr;
4120 sprintf (buf, ".sizeof.%s", secname);
4121 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4122 if (h != NULL && h->type == bfd_link_hash_undefined)
4124 h->type = bfd_link_hash_defined;
4125 h->u.def.value = TO_ADDR (s->size);
4126 h->u.def.section = bfd_abs_section_ptr;
4136 struct bfd_link_hash_entry *h;
4139 if (link_info.relocatable || link_info.shared)
4144 if (entry_symbol.name == NULL)
4146 /* No entry has been specified. Look for start, but don't warn
4147 if we don't find it. */
4148 entry_symbol.name = "start";
4152 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4153 FALSE, FALSE, TRUE);
4155 && (h->type == bfd_link_hash_defined
4156 || h->type == bfd_link_hash_defweak)
4157 && h->u.def.section->output_section != NULL)
4161 val = (h->u.def.value
4162 + bfd_get_section_vma (output_bfd,
4163 h->u.def.section->output_section)
4164 + h->u.def.section->output_offset);
4165 if (! bfd_set_start_address (output_bfd, val))
4166 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4173 /* We couldn't find the entry symbol. Try parsing it as a
4175 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4178 if (! bfd_set_start_address (output_bfd, val))
4179 einfo (_("%P%F: can't set start address\n"));
4185 /* Can't find the entry symbol, and it's not a number. Use
4186 the first address in the text section. */
4187 ts = bfd_get_section_by_name (output_bfd, entry_section);
4191 einfo (_("%P: warning: cannot find entry symbol %s;"
4192 " defaulting to %V\n"),
4194 bfd_get_section_vma (output_bfd, ts));
4195 if (! bfd_set_start_address (output_bfd,
4196 bfd_get_section_vma (output_bfd,
4198 einfo (_("%P%F: can't set start address\n"));
4203 einfo (_("%P: warning: cannot find entry symbol %s;"
4204 " not setting start address\n"),
4210 /* Don't bfd_hash_table_free (&lang_definedness_table);
4211 map file output may result in a call of lang_track_definedness. */
4214 /* This is a small function used when we want to ignore errors from
4218 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
4220 /* Don't do anything. */
4223 /* Check that the architecture of all the input files is compatible
4224 with the output file. Also call the backend to let it do any
4225 other checking that is needed. */
4230 lang_statement_union_type *file;
4232 const bfd_arch_info_type *compatible;
4234 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
4236 input_bfd = file->input_statement.the_bfd;
4238 = bfd_arch_get_compatible (input_bfd, output_bfd,
4239 command_line.accept_unknown_input_arch);
4241 /* In general it is not possible to perform a relocatable
4242 link between differing object formats when the input
4243 file has relocations, because the relocations in the
4244 input format may not have equivalent representations in
4245 the output format (and besides BFD does not translate
4246 relocs for other link purposes than a final link). */
4247 if ((link_info.relocatable || link_info.emitrelocations)
4248 && (compatible == NULL
4249 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
4250 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
4252 einfo (_("%P%F: Relocatable linking with relocations from"
4253 " format %s (%B) to format %s (%B) is not supported\n"),
4254 bfd_get_target (input_bfd), input_bfd,
4255 bfd_get_target (output_bfd), output_bfd);
4256 /* einfo with %F exits. */
4259 if (compatible == NULL)
4261 if (command_line.warn_mismatch)
4262 einfo (_("%P: warning: %s architecture of input file `%B'"
4263 " is incompatible with %s output\n"),
4264 bfd_printable_name (input_bfd), input_bfd,
4265 bfd_printable_name (output_bfd));
4267 else if (bfd_count_sections (input_bfd))
4269 /* If the input bfd has no contents, it shouldn't set the
4270 private data of the output bfd. */
4272 bfd_error_handler_type pfn = NULL;
4274 /* If we aren't supposed to warn about mismatched input
4275 files, temporarily set the BFD error handler to a
4276 function which will do nothing. We still want to call
4277 bfd_merge_private_bfd_data, since it may set up
4278 information which is needed in the output file. */
4279 if (! command_line.warn_mismatch)
4280 pfn = bfd_set_error_handler (ignore_bfd_errors);
4281 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
4283 if (command_line.warn_mismatch)
4284 einfo (_("%P%X: failed to merge target specific data"
4285 " of file %B\n"), input_bfd);
4287 if (! command_line.warn_mismatch)
4288 bfd_set_error_handler (pfn);
4293 /* Look through all the global common symbols and attach them to the
4294 correct section. The -sort-common command line switch may be used
4295 to roughly sort the entries by size. */
4300 if (command_line.inhibit_common_definition)
4302 if (link_info.relocatable
4303 && ! command_line.force_common_definition)
4306 if (! config.sort_common)
4307 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
4312 for (power = 4; power >= 0; power--)
4313 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
4317 /* Place one common symbol in the correct section. */
4320 lang_one_common (struct bfd_link_hash_entry *h, void *info)
4322 unsigned int power_of_two;
4326 if (h->type != bfd_link_hash_common)
4330 power_of_two = h->u.c.p->alignment_power;
4332 if (config.sort_common
4333 && power_of_two < (unsigned int) *(int *) info)
4336 section = h->u.c.p->section;
4338 /* Increase the size of the section to align the common sym. */
4339 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
4340 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
4342 /* Adjust the alignment if necessary. */
4343 if (power_of_two > section->alignment_power)
4344 section->alignment_power = power_of_two;
4346 /* Change the symbol from common to defined. */
4347 h->type = bfd_link_hash_defined;
4348 h->u.def.section = section;
4349 h->u.def.value = section->size;
4351 /* Increase the size of the section. */
4352 section->size += size;
4354 /* Make sure the section is allocated in memory, and make sure that
4355 it is no longer a common section. */
4356 section->flags |= SEC_ALLOC;
4357 section->flags &= ~SEC_IS_COMMON;
4359 if (config.map_file != NULL)
4361 static bfd_boolean header_printed;
4366 if (! header_printed)
4368 minfo (_("\nAllocating common symbols\n"));
4369 minfo (_("Common symbol size file\n\n"));
4370 header_printed = TRUE;
4373 name = demangle (h->root.string);
4375 len = strlen (name);
4390 if (size <= 0xffffffff)
4391 sprintf (buf, "%lx", (unsigned long) size);
4393 sprintf_vma (buf, size);
4403 minfo ("%B\n", section->owner);
4409 /* Run through the input files and ensure that every input section has
4410 somewhere to go. If one is found without a destination then create
4411 an input request and place it into the statement tree. */
4414 lang_place_orphans (void)
4416 LANG_FOR_EACH_INPUT_STATEMENT (file)
4420 for (s = file->the_bfd->sections; s != NULL; s = s->next)
4422 if (s->output_section == NULL)
4424 /* This section of the file is not attached, root
4425 around for a sensible place for it to go. */
4427 if (file->just_syms_flag)
4430 if ((s->flags & SEC_EXCLUDE) != 0)
4431 s->output_section = bfd_abs_section_ptr;
4432 else if (strcmp (s->name, "COMMON") == 0)
4434 /* This is a lonely common section which must have
4435 come from an archive. We attach to the section
4436 with the wildcard. */
4437 if (! link_info.relocatable
4438 || command_line.force_common_definition)
4440 if (default_common_section == NULL)
4443 /* This message happens when using the
4444 svr3.ifile linker script, so I have
4446 info_msg (_("%P: no [COMMON] command,"
4447 " defaulting to .bss\n"));
4449 default_common_section =
4450 lang_output_section_statement_lookup (".bss");
4453 lang_add_section (&default_common_section->children, s,
4454 default_common_section, file);
4457 else if (ldemul_place_orphan (file, s))
4461 lang_output_section_statement_type *os;
4463 os = lang_output_section_statement_lookup (s->name);
4464 lang_add_section (&os->children, s, os, file);
4472 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4474 flagword *ptr_flags;
4476 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4482 *ptr_flags |= SEC_ALLOC;
4486 *ptr_flags |= SEC_READONLY;
4490 *ptr_flags |= SEC_DATA;
4494 *ptr_flags |= SEC_CODE;
4499 *ptr_flags |= SEC_LOAD;
4503 einfo (_("%P%F: invalid syntax in flags\n"));
4510 /* Call a function on each input file. This function will be called
4511 on an archive, but not on the elements. */
4514 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4516 lang_input_statement_type *f;
4518 for (f = (lang_input_statement_type *) input_file_chain.head;
4520 f = (lang_input_statement_type *) f->next_real_file)
4524 /* Call a function on each file. The function will be called on all
4525 the elements of an archive which are included in the link, but will
4526 not be called on the archive file itself. */
4529 lang_for_each_file (void (*func) (lang_input_statement_type *))
4531 LANG_FOR_EACH_INPUT_STATEMENT (f)
4538 ldlang_add_file (lang_input_statement_type *entry)
4542 lang_statement_append (&file_chain,
4543 (lang_statement_union_type *) entry,
4546 /* The BFD linker needs to have a list of all input BFDs involved in
4548 ASSERT (entry->the_bfd->link_next == NULL);
4549 ASSERT (entry->the_bfd != output_bfd);
4550 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4552 *pp = entry->the_bfd;
4553 entry->the_bfd->usrdata = entry;
4554 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4556 /* Look through the sections and check for any which should not be
4557 included in the link. We need to do this now, so that we can
4558 notice when the backend linker tries to report multiple
4559 definition errors for symbols which are in sections we aren't
4560 going to link. FIXME: It might be better to entirely ignore
4561 symbols which are defined in sections which are going to be
4562 discarded. This would require modifying the backend linker for
4563 each backend which might set the SEC_LINK_ONCE flag. If we do
4564 this, we should probably handle SEC_EXCLUDE in the same way. */
4566 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4570 lang_add_output (const char *name, int from_script)
4572 /* Make -o on command line override OUTPUT in script. */
4573 if (!had_output_filename || !from_script)
4575 output_filename = name;
4576 had_output_filename = TRUE;
4580 static lang_output_section_statement_type *current_section;
4591 for (l = 0; l < 32; l++)
4593 if (i >= (unsigned int) x)
4601 lang_output_section_statement_type *
4602 lang_enter_output_section_statement (const char *output_section_statement_name,
4603 etree_type *address_exp,
4604 enum section_type sectype,
4606 etree_type *subalign,
4610 lang_output_section_statement_type *os;
4614 lang_output_section_statement_lookup_1 (output_section_statement_name,
4617 /* Add this statement to tree. */
4619 add_statement (lang_output_section_statement_enum,
4620 output_section_statement);
4622 /* Make next things chain into subchain of this. */
4624 if (os->addr_tree == NULL)
4626 os->addr_tree = address_exp;
4628 os->sectype = sectype;
4629 if (sectype != noload_section)
4630 os->flags = SEC_NO_FLAGS;
4632 os->flags = SEC_NEVER_LOAD;
4633 os->block_value = 1;
4634 stat_ptr = &os->children;
4636 os->subsection_alignment =
4637 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4638 os->section_alignment =
4639 topower (exp_get_value_int (align, -1, "section alignment", 0));
4641 os->load_base = ebase;
4648 lang_output_statement_type *new =
4649 new_stat (lang_output_statement, stat_ptr);
4651 new->name = output_filename;
4654 /* Reset the current counters in the regions. */
4657 lang_reset_memory_regions (void)
4659 lang_memory_region_type *p = lang_memory_region_list;
4662 for (p = lang_memory_region_list; p != NULL; p = p->next)
4664 p->old_length = (bfd_size_type) (p->current - p->origin);
4665 p->current = p->origin;
4668 for (o = output_bfd->sections; o != NULL; o = o->next)
4670 /* Save the last size for possible use by bfd_relax_section. */
4671 o->rawsize = o->size;
4676 /* Worker for lang_gc_sections_1. */
4679 gc_section_callback (lang_wild_statement_type *ptr,
4680 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4682 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4683 void *data ATTRIBUTE_UNUSED)
4685 /* If the wild pattern was marked KEEP, the member sections
4686 should be as well. */
4687 if (ptr->keep_sections)
4688 section->flags |= SEC_KEEP;
4691 /* Iterate over sections marking them against GC. */
4694 lang_gc_sections_1 (lang_statement_union_type *s)
4696 for (; s != NULL; s = s->header.next)
4698 switch (s->header.type)
4700 case lang_wild_statement_enum:
4701 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4703 case lang_constructors_statement_enum:
4704 lang_gc_sections_1 (constructor_list.head);
4706 case lang_output_section_statement_enum:
4707 lang_gc_sections_1 (s->output_section_statement.children.head);
4709 case lang_group_statement_enum:
4710 lang_gc_sections_1 (s->group_statement.children.head);
4719 lang_gc_sections (void)
4721 struct bfd_link_hash_entry *h;
4722 ldlang_undef_chain_list_type *ulist;
4724 /* Keep all sections so marked in the link script. */
4726 lang_gc_sections_1 (statement_list.head);
4728 /* Keep all sections containing symbols undefined on the command-line,
4729 and the section containing the entry symbol. */
4731 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4733 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4734 FALSE, FALSE, FALSE);
4737 && (h->type == bfd_link_hash_defined
4738 || h->type == bfd_link_hash_defweak)
4739 && ! bfd_is_abs_section (h->u.def.section))
4741 h->u.def.section->flags |= SEC_KEEP;
4745 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4746 the special case of debug info. (See bfd/stabs.c)
4747 Twiddle the flag here, to simplify later linker code. */
4748 if (link_info.relocatable)
4750 LANG_FOR_EACH_INPUT_STATEMENT (f)
4753 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4754 if ((sec->flags & SEC_DEBUGGING) == 0)
4755 sec->flags &= ~SEC_EXCLUDE;
4759 if (command_line.gc_sections)
4760 bfd_gc_sections (output_bfd, &link_info);
4766 lang_reasonable_defaults ();
4767 current_target = default_target;
4769 /* Open the output file. */
4770 lang_for_each_statement (ldlang_open_output);
4773 ldemul_create_output_section_statements ();
4775 /* Add to the hash table all undefineds on the command line. */
4776 lang_place_undefineds ();
4778 if (!bfd_section_already_linked_table_init ())
4779 einfo (_("%P%F: Failed to create hash table\n"));
4781 /* Create a bfd for each input file. */
4782 current_target = default_target;
4783 open_input_bfds (statement_list.head, FALSE);
4785 link_info.gc_sym_list = &entry_symbol;
4786 if (entry_symbol.name == NULL)
4787 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4789 ldemul_after_open ();
4791 bfd_section_already_linked_table_free ();
4793 /* Make sure that we're not mixing architectures. We call this
4794 after all the input files have been opened, but before we do any
4795 other processing, so that any operations merge_private_bfd_data
4796 does on the output file will be known during the rest of the
4800 /* Handle .exports instead of a version script if we're told to do so. */
4801 if (command_line.version_exports_section)
4802 lang_do_version_exports_section ();
4804 /* Build all sets based on the information gathered from the input
4806 ldctor_build_sets ();
4808 /* Remove unreferenced sections if asked to. */
4809 lang_gc_sections ();
4811 /* Size up the common data. */
4814 /* Update wild statements. */
4815 update_wild_statements (statement_list.head);
4817 /* Run through the contours of the script and attach input sections
4818 to the correct output sections. */
4819 map_input_to_output_sections (statement_list.head, NULL, NULL);
4821 /* Find any sections not attached explicitly and handle them. */
4822 lang_place_orphans ();
4824 if (! link_info.relocatable)
4828 /* Merge SEC_MERGE sections. This has to be done after GC of
4829 sections, so that GCed sections are not merged, but before
4830 assigning dynamic symbols, since removing whole input sections
4832 bfd_merge_sections (output_bfd, &link_info);
4834 /* Look for a text section and set the readonly attribute in it. */
4835 found = bfd_get_section_by_name (output_bfd, ".text");
4839 if (config.text_read_only)
4840 found->flags |= SEC_READONLY;
4842 found->flags &= ~SEC_READONLY;
4846 /* Do anything special before sizing sections. This is where ELF
4847 and other back-ends size dynamic sections. */
4848 ldemul_before_allocation ();
4850 if (!link_info.relocatable)
4851 strip_excluded_output_sections ();
4853 /* We must record the program headers before we try to fix the
4854 section positions, since they will affect SIZEOF_HEADERS. */
4855 lang_record_phdrs ();
4857 /* Size up the sections. */
4858 lang_size_sections (statement_list.head, abs_output_section,
4859 &statement_list.head, 0, 0, NULL,
4860 command_line.relax ? FALSE : TRUE);
4862 /* Now run around and relax if we can. */
4863 if (command_line.relax)
4865 /* Keep relaxing until bfd_relax_section gives up. */
4866 bfd_boolean relax_again;
4870 relax_again = FALSE;
4872 /* Note: pe-dll.c does something like this also. If you find
4873 you need to change this code, you probably need to change
4874 pe-dll.c also. DJ */
4876 /* Do all the assignments with our current guesses as to
4878 lang_do_assignments (statement_list.head, abs_output_section,
4881 /* We must do this after lang_do_assignments, because it uses
4883 lang_reset_memory_regions ();
4885 /* Perform another relax pass - this time we know where the
4886 globals are, so can make a better guess. */
4887 lang_size_sections (statement_list.head, abs_output_section,
4888 &statement_list.head, 0, 0, &relax_again, FALSE);
4890 /* If the normal relax is done and the relax finalize pass
4891 is not performed yet, we perform another relax pass. */
4892 if (!relax_again && link_info.need_relax_finalize)
4894 link_info.need_relax_finalize = FALSE;
4898 while (relax_again);
4900 /* Final extra sizing to report errors. */
4901 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4902 lang_reset_memory_regions ();
4903 lang_size_sections (statement_list.head, abs_output_section,
4904 &statement_list.head, 0, 0, NULL, TRUE);
4907 /* See if anything special should be done now we know how big
4909 ldemul_after_allocation ();
4911 /* Fix any .startof. or .sizeof. symbols. */
4912 lang_set_startof ();
4914 /* Do all the assignments, now that we know the final resting places
4915 of all the symbols. */
4917 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4919 /* Make sure that the section addresses make sense. */
4920 if (! link_info.relocatable
4921 && command_line.check_section_addresses)
4922 lang_check_section_addresses ();
4930 /* EXPORTED TO YACC */
4933 lang_add_wild (struct wildcard_spec *filespec,
4934 struct wildcard_list *section_list,
4935 bfd_boolean keep_sections)
4937 struct wildcard_list *curr, *next;
4938 lang_wild_statement_type *new;
4940 /* Reverse the list as the parser puts it back to front. */
4941 for (curr = section_list, section_list = NULL;
4943 section_list = curr, curr = next)
4945 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4946 placed_commons = TRUE;
4949 curr->next = section_list;
4952 if (filespec != NULL && filespec->name != NULL)
4954 if (strcmp (filespec->name, "*") == 0)
4955 filespec->name = NULL;
4956 else if (! wildcardp (filespec->name))
4957 lang_has_input_file = TRUE;
4960 new = new_stat (lang_wild_statement, stat_ptr);
4961 new->filename = NULL;
4962 new->filenames_sorted = FALSE;
4963 if (filespec != NULL)
4965 new->filename = filespec->name;
4966 new->filenames_sorted = filespec->sorted == by_name;
4968 new->section_list = section_list;
4969 new->keep_sections = keep_sections;
4970 lang_list_init (&new->children);
4974 lang_section_start (const char *name, etree_type *address)
4976 lang_address_statement_type *ad;
4978 ad = new_stat (lang_address_statement, stat_ptr);
4979 ad->section_name = name;
4980 ad->address = address;
4983 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4984 because of a -e argument on the command line, or zero if this is
4985 called by ENTRY in a linker script. Command line arguments take
4989 lang_add_entry (const char *name, bfd_boolean cmdline)
4991 if (entry_symbol.name == NULL
4993 || ! entry_from_cmdline)
4995 entry_symbol.name = name;
4996 entry_from_cmdline = cmdline;
5001 lang_add_target (const char *name)
5003 lang_target_statement_type *new = new_stat (lang_target_statement,
5011 lang_add_map (const char *name)
5018 map_option_f = TRUE;
5026 lang_add_fill (fill_type *fill)
5028 lang_fill_statement_type *new = new_stat (lang_fill_statement,
5035 lang_add_data (int type, union etree_union *exp)
5038 lang_data_statement_type *new = new_stat (lang_data_statement,
5046 /* Create a new reloc statement. RELOC is the BFD relocation type to
5047 generate. HOWTO is the corresponding howto structure (we could
5048 look this up, but the caller has already done so). SECTION is the
5049 section to generate a reloc against, or NAME is the name of the
5050 symbol to generate a reloc against. Exactly one of SECTION and
5051 NAME must be NULL. ADDEND is an expression for the addend. */
5054 lang_add_reloc (bfd_reloc_code_real_type reloc,
5055 reloc_howto_type *howto,
5058 union etree_union *addend)
5060 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5064 p->section = section;
5066 p->addend_exp = addend;
5068 p->addend_value = 0;
5069 p->output_section = NULL;
5073 lang_assignment_statement_type *
5074 lang_add_assignment (etree_type *exp)
5076 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
5084 lang_add_attribute (enum statement_enum attribute)
5086 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
5090 lang_startup (const char *name)
5092 if (startup_file != NULL)
5094 einfo (_("%P%Fmultiple STARTUP files\n"));
5096 first_file->filename = name;
5097 first_file->local_sym_name = name;
5098 first_file->real = TRUE;
5100 startup_file = name;
5104 lang_float (bfd_boolean maybe)
5106 lang_float_flag = maybe;
5110 /* Work out the load- and run-time regions from a script statement, and
5111 store them in *LMA_REGION and *REGION respectively.
5113 MEMSPEC is the name of the run-time region, or the value of
5114 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5115 LMA_MEMSPEC is the name of the load-time region, or null if the
5116 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5117 had an explicit load address.
5119 It is an error to specify both a load region and a load address. */
5122 lang_get_regions (lang_memory_region_type **region,
5123 lang_memory_region_type **lma_region,
5124 const char *memspec,
5125 const char *lma_memspec,
5126 bfd_boolean have_lma,
5127 bfd_boolean have_vma)
5129 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5131 /* If no runtime region or VMA has been specified, but the load region
5132 has been specified, then use the load region for the runtime region
5134 if (lma_memspec != NULL
5136 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5137 *region = *lma_region;
5139 *region = lang_memory_region_lookup (memspec, FALSE);
5141 if (have_lma && lma_memspec != 0)
5142 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5146 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5147 lang_output_section_phdr_list *phdrs,
5148 const char *lma_memspec)
5150 lang_get_regions (¤t_section->region,
5151 ¤t_section->lma_region,
5152 memspec, lma_memspec,
5153 current_section->load_base != NULL,
5154 current_section->addr_tree != NULL);
5155 current_section->fill = fill;
5156 current_section->phdrs = phdrs;
5157 stat_ptr = &statement_list;
5160 /* Create an absolute symbol with the given name with the value of the
5161 address of first byte of the section named.
5163 If the symbol already exists, then do nothing. */
5166 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5168 struct bfd_link_hash_entry *h;
5170 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5172 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5174 if (h->type == bfd_link_hash_new
5175 || h->type == bfd_link_hash_undefined)
5179 h->type = bfd_link_hash_defined;
5181 sec = bfd_get_section_by_name (output_bfd, secname);
5185 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5187 h->u.def.section = bfd_abs_section_ptr;
5191 /* Create an absolute symbol with the given name with the value of the
5192 address of the first byte after the end of the section named.
5194 If the symbol already exists, then do nothing. */
5197 lang_abs_symbol_at_end_of (const char *secname, const char *name)
5199 struct bfd_link_hash_entry *h;
5201 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5203 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5205 if (h->type == bfd_link_hash_new
5206 || h->type == bfd_link_hash_undefined)
5210 h->type = bfd_link_hash_defined;
5212 sec = bfd_get_section_by_name (output_bfd, secname);
5216 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
5217 + TO_ADDR (sec->size));
5219 h->u.def.section = bfd_abs_section_ptr;
5224 lang_statement_append (lang_statement_list_type *list,
5225 lang_statement_union_type *element,
5226 lang_statement_union_type **field)
5228 *(list->tail) = element;
5232 /* Set the output format type. -oformat overrides scripts. */
5235 lang_add_output_format (const char *format,
5240 if (output_target == NULL || !from_script)
5242 if (command_line.endian == ENDIAN_BIG
5245 else if (command_line.endian == ENDIAN_LITTLE
5249 output_target = format;
5253 /* Enter a group. This creates a new lang_group_statement, and sets
5254 stat_ptr to build new statements within the group. */
5257 lang_enter_group (void)
5259 lang_group_statement_type *g;
5261 g = new_stat (lang_group_statement, stat_ptr);
5262 lang_list_init (&g->children);
5263 stat_ptr = &g->children;
5266 /* Leave a group. This just resets stat_ptr to start writing to the
5267 regular list of statements again. Note that this will not work if
5268 groups can occur inside anything else which can adjust stat_ptr,
5269 but currently they can't. */
5272 lang_leave_group (void)
5274 stat_ptr = &statement_list;
5277 /* Add a new program header. This is called for each entry in a PHDRS
5278 command in a linker script. */
5281 lang_new_phdr (const char *name,
5283 bfd_boolean filehdr,
5288 struct lang_phdr *n, **pp;
5290 n = stat_alloc (sizeof (struct lang_phdr));
5293 n->type = exp_get_value_int (type, 0, "program header type",
5294 lang_final_phase_enum);
5295 n->filehdr = filehdr;
5300 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
5305 /* Record the program header information in the output BFD. FIXME: We
5306 should not be calling an ELF specific function here. */
5309 lang_record_phdrs (void)
5313 lang_output_section_phdr_list *last;
5314 struct lang_phdr *l;
5315 lang_output_section_statement_type *os;
5318 secs = xmalloc (alc * sizeof (asection *));
5320 for (l = lang_phdr_list; l != NULL; l = l->next)
5327 for (os = &lang_output_section_statement.head->output_section_statement;
5331 lang_output_section_phdr_list *pl;
5333 if (os->constraint == -1)
5341 if (os->sectype == noload_section
5342 || os->bfd_section == NULL
5343 || (os->bfd_section->flags & SEC_ALLOC) == 0)
5348 if (os->bfd_section == NULL)
5351 for (; pl != NULL; pl = pl->next)
5353 if (strcmp (pl->name, l->name) == 0)
5358 secs = xrealloc (secs, alc * sizeof (asection *));
5360 secs[c] = os->bfd_section;
5367 if (l->flags == NULL)
5370 flags = exp_get_vma (l->flags, 0, "phdr flags",
5371 lang_final_phase_enum);
5376 at = exp_get_vma (l->at, 0, "phdr load address",
5377 lang_final_phase_enum);
5379 if (! bfd_record_phdr (output_bfd, l->type,
5380 l->flags != NULL, flags, l->at != NULL,
5381 at, l->filehdr, l->phdrs, c, secs))
5382 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
5387 /* Make sure all the phdr assignments succeeded. */
5388 for (os = &lang_output_section_statement.head->output_section_statement;
5392 lang_output_section_phdr_list *pl;
5394 if (os->constraint == -1
5395 || os->bfd_section == NULL)
5398 for (pl = os->phdrs;
5401 if (! pl->used && strcmp (pl->name, "NONE") != 0)
5402 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
5403 os->name, pl->name);
5407 /* Record a list of sections which may not be cross referenced. */
5410 lang_add_nocrossref (lang_nocrossref_type *l)
5412 struct lang_nocrossrefs *n;
5414 n = xmalloc (sizeof *n);
5415 n->next = nocrossref_list;
5417 nocrossref_list = n;
5419 /* Set notice_all so that we get informed about all symbols. */
5420 link_info.notice_all = TRUE;
5423 /* Overlay handling. We handle overlays with some static variables. */
5425 /* The overlay virtual address. */
5426 static etree_type *overlay_vma;
5427 /* And subsection alignment. */
5428 static etree_type *overlay_subalign;
5430 /* An expression for the maximum section size seen so far. */
5431 static etree_type *overlay_max;
5433 /* A list of all the sections in this overlay. */
5435 struct overlay_list {
5436 struct overlay_list *next;
5437 lang_output_section_statement_type *os;
5440 static struct overlay_list *overlay_list;
5442 /* Start handling an overlay. */
5445 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5447 /* The grammar should prevent nested overlays from occurring. */
5448 ASSERT (overlay_vma == NULL
5449 && overlay_subalign == NULL
5450 && overlay_max == NULL);
5452 overlay_vma = vma_expr;
5453 overlay_subalign = subalign;
5456 /* Start a section in an overlay. We handle this by calling
5457 lang_enter_output_section_statement with the correct VMA.
5458 lang_leave_overlay sets up the LMA and memory regions. */
5461 lang_enter_overlay_section (const char *name)
5463 struct overlay_list *n;
5466 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5467 0, overlay_subalign, 0, 0);
5469 /* If this is the first section, then base the VMA of future
5470 sections on this one. This will work correctly even if `.' is
5471 used in the addresses. */
5472 if (overlay_list == NULL)
5473 overlay_vma = exp_nameop (ADDR, name);
5475 /* Remember the section. */
5476 n = xmalloc (sizeof *n);
5477 n->os = current_section;
5478 n->next = overlay_list;
5481 size = exp_nameop (SIZEOF, name);
5483 /* Arrange to work out the maximum section end address. */
5484 if (overlay_max == NULL)
5487 overlay_max = exp_binop (MAX_K, overlay_max, size);
5490 /* Finish a section in an overlay. There isn't any special to do
5494 lang_leave_overlay_section (fill_type *fill,
5495 lang_output_section_phdr_list *phdrs)
5502 name = current_section->name;
5504 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5505 region and that no load-time region has been specified. It doesn't
5506 really matter what we say here, since lang_leave_overlay will
5508 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5510 /* Define the magic symbols. */
5512 clean = xmalloc (strlen (name) + 1);
5514 for (s1 = name; *s1 != '\0'; s1++)
5515 if (ISALNUM (*s1) || *s1 == '_')
5519 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5520 sprintf (buf, "__load_start_%s", clean);
5521 lang_add_assignment (exp_assop ('=', buf,
5522 exp_nameop (LOADADDR, name)));
5524 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5525 sprintf (buf, "__load_stop_%s", clean);
5526 lang_add_assignment (exp_assop ('=', buf,
5528 exp_nameop (LOADADDR, name),
5529 exp_nameop (SIZEOF, name))));
5534 /* Finish an overlay. If there are any overlay wide settings, this
5535 looks through all the sections in the overlay and sets them. */
5538 lang_leave_overlay (etree_type *lma_expr,
5541 const char *memspec,
5542 lang_output_section_phdr_list *phdrs,
5543 const char *lma_memspec)
5545 lang_memory_region_type *region;
5546 lang_memory_region_type *lma_region;
5547 struct overlay_list *l;
5548 lang_nocrossref_type *nocrossref;
5550 lang_get_regions (®ion, &lma_region,
5551 memspec, lma_memspec,
5552 lma_expr != NULL, FALSE);
5556 /* After setting the size of the last section, set '.' to end of the
5558 if (overlay_list != NULL)
5559 overlay_list->os->update_dot_tree
5560 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5565 struct overlay_list *next;
5567 if (fill != NULL && l->os->fill == NULL)
5570 l->os->region = region;
5571 l->os->lma_region = lma_region;
5573 /* The first section has the load address specified in the
5574 OVERLAY statement. The rest are worked out from that.
5575 The base address is not needed (and should be null) if
5576 an LMA region was specified. */
5578 l->os->load_base = lma_expr;
5579 else if (lma_region == 0)
5580 l->os->load_base = exp_binop ('+',
5581 exp_nameop (LOADADDR, l->next->os->name),
5582 exp_nameop (SIZEOF, l->next->os->name));
5584 if (phdrs != NULL && l->os->phdrs == NULL)
5585 l->os->phdrs = phdrs;
5589 lang_nocrossref_type *nc;
5591 nc = xmalloc (sizeof *nc);
5592 nc->name = l->os->name;
5593 nc->next = nocrossref;
5602 if (nocrossref != NULL)
5603 lang_add_nocrossref (nocrossref);
5606 overlay_list = NULL;
5610 /* Version handling. This is only useful for ELF. */
5612 /* This global variable holds the version tree that we build. */
5614 struct bfd_elf_version_tree *lang_elf_version_info;
5616 /* If PREV is NULL, return first version pattern matching particular symbol.
5617 If PREV is non-NULL, return first version pattern matching particular
5618 symbol after PREV (previously returned by lang_vers_match). */
5620 static struct bfd_elf_version_expr *
5621 lang_vers_match (struct bfd_elf_version_expr_head *head,
5622 struct bfd_elf_version_expr *prev,
5625 const char *cxx_sym = sym;
5626 const char *java_sym = sym;
5627 struct bfd_elf_version_expr *expr = NULL;
5629 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5631 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5635 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5637 java_sym = cplus_demangle (sym, DMGL_JAVA);
5642 if (head->htab && (prev == NULL || prev->symbol))
5644 struct bfd_elf_version_expr e;
5646 switch (prev ? prev->mask : 0)
5649 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5652 expr = htab_find (head->htab, &e);
5653 while (expr && strcmp (expr->symbol, sym) == 0)
5654 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5660 case BFD_ELF_VERSION_C_TYPE:
5661 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5664 expr = htab_find (head->htab, &e);
5665 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5666 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5672 case BFD_ELF_VERSION_CXX_TYPE:
5673 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5675 e.symbol = java_sym;
5676 expr = htab_find (head->htab, &e);
5677 while (expr && strcmp (expr->symbol, java_sym) == 0)
5678 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5689 /* Finally, try the wildcards. */
5690 if (prev == NULL || prev->symbol)
5691 expr = head->remaining;
5698 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5701 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5703 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5707 if (fnmatch (expr->pattern, s, 0) == 0)
5714 free ((char *) cxx_sym);
5715 if (java_sym != sym)
5716 free ((char *) java_sym);
5720 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5721 return a string pointing to the symbol name. */
5724 realsymbol (const char *pattern)
5727 bfd_boolean changed = FALSE, backslash = FALSE;
5728 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5730 for (p = pattern, s = symbol; *p != '\0'; ++p)
5732 /* It is a glob pattern only if there is no preceding
5734 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5742 /* Remove the preceding backslash. */
5749 backslash = *p == '\\';
5764 /* This is called for each variable name or match expression. */
5766 struct bfd_elf_version_expr *
5767 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5771 struct bfd_elf_version_expr *ret;
5773 ret = xmalloc (sizeof *ret);
5778 ret->symbol = realsymbol (new);
5780 if (lang == NULL || strcasecmp (lang, "C") == 0)
5781 ret->mask = BFD_ELF_VERSION_C_TYPE;
5782 else if (strcasecmp (lang, "C++") == 0)
5783 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5784 else if (strcasecmp (lang, "Java") == 0)
5785 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5788 einfo (_("%X%P: unknown language `%s' in version information\n"),
5790 ret->mask = BFD_ELF_VERSION_C_TYPE;
5793 return ldemul_new_vers_pattern (ret);
5796 /* This is called for each set of variable names and match
5799 struct bfd_elf_version_tree *
5800 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5801 struct bfd_elf_version_expr *locals)
5803 struct bfd_elf_version_tree *ret;
5805 ret = xcalloc (1, sizeof *ret);
5806 ret->globals.list = globals;
5807 ret->locals.list = locals;
5808 ret->match = lang_vers_match;
5809 ret->name_indx = (unsigned int) -1;
5813 /* This static variable keeps track of version indices. */
5815 static int version_index;
5818 version_expr_head_hash (const void *p)
5820 const struct bfd_elf_version_expr *e = p;
5822 return htab_hash_string (e->symbol);
5826 version_expr_head_eq (const void *p1, const void *p2)
5828 const struct bfd_elf_version_expr *e1 = p1;
5829 const struct bfd_elf_version_expr *e2 = p2;
5831 return strcmp (e1->symbol, e2->symbol) == 0;
5835 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5838 struct bfd_elf_version_expr *e, *next;
5839 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5841 for (e = head->list; e; e = e->next)
5845 head->mask |= e->mask;
5850 head->htab = htab_create (count * 2, version_expr_head_hash,
5851 version_expr_head_eq, NULL);
5852 list_loc = &head->list;
5853 remaining_loc = &head->remaining;
5854 for (e = head->list; e; e = next)
5860 remaining_loc = &e->next;
5864 void **loc = htab_find_slot (head->htab, e, INSERT);
5868 struct bfd_elf_version_expr *e1, *last;
5874 if (e1->mask == e->mask)
5882 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5886 /* This is a duplicate. */
5887 /* FIXME: Memory leak. Sometimes pattern is not
5888 xmalloced alone, but in larger chunk of memory. */
5889 /* free (e->symbol); */
5894 e->next = last->next;
5902 list_loc = &e->next;
5906 *remaining_loc = NULL;
5907 *list_loc = head->remaining;
5910 head->remaining = head->list;
5913 /* This is called when we know the name and dependencies of the
5917 lang_register_vers_node (const char *name,
5918 struct bfd_elf_version_tree *version,
5919 struct bfd_elf_version_deps *deps)
5921 struct bfd_elf_version_tree *t, **pp;
5922 struct bfd_elf_version_expr *e1;
5927 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5928 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5930 einfo (_("%X%P: anonymous version tag cannot be combined"
5931 " with other version tags\n"));
5936 /* Make sure this node has a unique name. */
5937 for (t = lang_elf_version_info; t != NULL; t = t->next)
5938 if (strcmp (t->name, name) == 0)
5939 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5941 lang_finalize_version_expr_head (&version->globals);
5942 lang_finalize_version_expr_head (&version->locals);
5944 /* Check the global and local match names, and make sure there
5945 aren't any duplicates. */
5947 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5949 for (t = lang_elf_version_info; t != NULL; t = t->next)
5951 struct bfd_elf_version_expr *e2;
5953 if (t->locals.htab && e1->symbol)
5955 e2 = htab_find (t->locals.htab, e1);
5956 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5958 if (e1->mask == e2->mask)
5959 einfo (_("%X%P: duplicate expression `%s'"
5960 " in version information\n"), e1->symbol);
5964 else if (!e1->symbol)
5965 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5966 if (strcmp (e1->pattern, e2->pattern) == 0
5967 && e1->mask == e2->mask)
5968 einfo (_("%X%P: duplicate expression `%s'"
5969 " in version information\n"), e1->pattern);
5973 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5975 for (t = lang_elf_version_info; t != NULL; t = t->next)
5977 struct bfd_elf_version_expr *e2;
5979 if (t->globals.htab && e1->symbol)
5981 e2 = htab_find (t->globals.htab, e1);
5982 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5984 if (e1->mask == e2->mask)
5985 einfo (_("%X%P: duplicate expression `%s'"
5986 " in version information\n"),
5991 else if (!e1->symbol)
5992 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5993 if (strcmp (e1->pattern, e2->pattern) == 0
5994 && e1->mask == e2->mask)
5995 einfo (_("%X%P: duplicate expression `%s'"
5996 " in version information\n"), e1->pattern);
6000 version->deps = deps;
6001 version->name = name;
6002 if (name[0] != '\0')
6005 version->vernum = version_index;
6008 version->vernum = 0;
6010 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6015 /* This is called when we see a version dependency. */
6017 struct bfd_elf_version_deps *
6018 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6020 struct bfd_elf_version_deps *ret;
6021 struct bfd_elf_version_tree *t;
6023 ret = xmalloc (sizeof *ret);
6026 for (t = lang_elf_version_info; t != NULL; t = t->next)
6028 if (strcmp (t->name, name) == 0)
6030 ret->version_needed = t;
6035 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6041 lang_do_version_exports_section (void)
6043 struct bfd_elf_version_expr *greg = NULL, *lreg;
6045 LANG_FOR_EACH_INPUT_STATEMENT (is)
6047 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6055 contents = xmalloc (len);
6056 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6057 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6060 while (p < contents + len)
6062 greg = lang_new_vers_pattern (greg, p, NULL);
6063 p = strchr (p, '\0') + 1;
6066 /* Do not free the contents, as we used them creating the regex. */
6068 /* Do not include this section in the link. */
6069 sec->flags |= SEC_EXCLUDE;
6072 lreg = lang_new_vers_pattern (NULL, "*", NULL);
6073 lang_register_vers_node (command_line.version_exports_section,
6074 lang_new_vers_node (greg, lreg), NULL);
6078 lang_add_unique (const char *name)
6080 struct unique_sections *ent;
6082 for (ent = unique_section_list; ent; ent = ent->next)
6083 if (strcmp (ent->name, name) == 0)
6086 ent = xmalloc (sizeof *ent);
6087 ent->name = xstrdup (name);
6088 ent->next = unique_section_list;
6089 unique_section_list = ent;