Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / binutils-2.17 / ld / ldlang.c
blob099a4ccb5077365b965df1609b51522bc82ff1b9
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006
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)
11 any later version.
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, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libiberty.h"
26 #include "safe-ctype.h"
27 #include "obstack.h"
28 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldmisc.h"
37 #include "ldctor.h"
38 #include "ldfile.h"
39 #include "ldemul.h"
40 #include "fnmatch.h"
41 #include "demangle.h"
42 #include "hashtab.h"
44 #ifndef offsetof
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
46 #endif
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 bfd_boolean stripped_excluded_sections = FALSE;
58 static lang_output_section_statement_type *default_common_section;
59 static bfd_boolean map_option_f;
60 static bfd_vma print_dot;
61 static lang_input_statement_type *first_file;
62 static const char *current_target;
63 static const char *output_target;
64 static lang_statement_list_type statement_list;
65 static struct lang_phdr *lang_phdr_list;
66 static struct bfd_hash_table lang_definedness_table;
68 /* Forward declarations. */
69 static void exp_init_os (etree_type *);
70 static void init_map_userdata (bfd *, asection *, void *);
71 static lang_input_statement_type *lookup_name (const char *);
72 static bfd_boolean load_symbols (lang_input_statement_type *,
73 lang_statement_list_type *);
74 static struct bfd_hash_entry *lang_definedness_newfunc
75 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
76 static void insert_undefined (const char *);
77 static void print_all_symbols (asection *);
78 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
79 static void print_statement (lang_statement_union_type *,
80 lang_output_section_statement_type *);
81 static void print_statement_list (lang_statement_union_type *,
82 lang_output_section_statement_type *);
83 static void print_statements (void);
84 static void print_input_section (asection *);
85 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
86 static void lang_record_phdrs (void);
87 static void lang_do_version_exports_section (void);
89 /* Exported variables. */
90 lang_output_section_statement_type *abs_output_section;
91 lang_statement_list_type lang_output_section_statement;
92 lang_statement_list_type *stat_ptr = &statement_list;
93 lang_statement_list_type file_chain = { NULL, NULL };
94 struct bfd_sym_chain entry_symbol = { NULL, NULL };
95 static const char *entry_symbol_default = "start";
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 static struct unique_sections *unique_section_list;
104 static bfd_boolean ldlang_sysrooted_script = FALSE;
106 /* Functions that traverse the linker script and might evaluate
107 DEFINED() need to increment this. */
108 int lang_statement_iteration = 0;
110 etree_type *base; /* Relocation base - or null */
112 /* Return TRUE if the PATTERN argument is a wildcard pattern.
113 Although backslashes are treated specially if a pattern contains
114 wildcards, we do not consider the mere presence of a backslash to
115 be enough to cause the pattern to be treated as a wildcard.
116 That lets us handle DOS filenames more naturally. */
117 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
119 #define new_stat(x, y) \
120 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
122 #define outside_section_address(q) \
123 ((q)->output_offset + (q)->output_section->vma)
125 #define outside_symbol_address(q) \
126 ((q)->value + outside_section_address (q->section))
128 #define SECTION_NAME_MAP_LENGTH (16)
130 void *
131 stat_alloc (size_t size)
133 return obstack_alloc (&stat_obstack, size);
136 bfd_boolean
137 unique_section_p (const asection *sec)
139 struct unique_sections *unam;
140 const char *secnam;
142 if (link_info.relocatable
143 && sec->owner != NULL
144 && bfd_is_group_section (sec->owner, sec))
145 return TRUE;
147 secnam = sec->name;
148 for (unam = unique_section_list; unam; unam = unam->next)
149 if (wildcardp (unam->name)
150 ? fnmatch (unam->name, secnam, 0) == 0
151 : strcmp (unam->name, secnam) == 0)
153 return TRUE;
156 return FALSE;
159 /* Generic traversal routines for finding matching sections. */
161 /* Try processing a section against a wildcard. This just calls
162 the callback unless the filename exclusion list is present
163 and excludes the file. It's hardly ever present so this
164 function is very fast. */
166 static void
167 walk_wild_consider_section (lang_wild_statement_type *ptr,
168 lang_input_statement_type *file,
169 asection *s,
170 struct wildcard_list *sec,
171 callback_t callback,
172 void *data)
174 bfd_boolean skip = FALSE;
175 struct name_list *list_tmp;
177 /* Don't process sections from files which were
178 excluded. */
179 for (list_tmp = sec->spec.exclude_name_list;
180 list_tmp;
181 list_tmp = list_tmp->next)
183 bfd_boolean is_wildcard = wildcardp (list_tmp->name);
184 if (is_wildcard)
185 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
186 else
187 skip = strcmp (list_tmp->name, file->filename) == 0;
189 /* If this file is part of an archive, and the archive is
190 excluded, exclude this file. */
191 if (! skip && file->the_bfd != NULL
192 && file->the_bfd->my_archive != NULL
193 && file->the_bfd->my_archive->filename != NULL)
195 if (is_wildcard)
196 skip = fnmatch (list_tmp->name,
197 file->the_bfd->my_archive->filename,
198 0) == 0;
199 else
200 skip = strcmp (list_tmp->name,
201 file->the_bfd->my_archive->filename) == 0;
204 if (skip)
205 break;
208 if (!skip)
209 (*callback) (ptr, sec, s, file, data);
212 /* Lowest common denominator routine that can handle everything correctly,
213 but slowly. */
215 static void
216 walk_wild_section_general (lang_wild_statement_type *ptr,
217 lang_input_statement_type *file,
218 callback_t callback,
219 void *data)
221 asection *s;
222 struct wildcard_list *sec;
224 for (s = file->the_bfd->sections; s != NULL; s = s->next)
226 sec = ptr->section_list;
227 if (sec == NULL)
228 (*callback) (ptr, sec, s, file, data);
230 while (sec != NULL)
232 bfd_boolean skip = FALSE;
234 if (sec->spec.name != NULL)
236 const char *sname = bfd_get_section_name (file->the_bfd, s);
238 if (wildcardp (sec->spec.name))
239 skip = fnmatch (sec->spec.name, sname, 0) != 0;
240 else
241 skip = strcmp (sec->spec.name, sname) != 0;
244 if (!skip)
245 walk_wild_consider_section (ptr, file, s, sec, callback, data);
247 sec = sec->next;
252 /* Routines to find a single section given its name. If there's more
253 than one section with that name, we report that. */
255 typedef struct
257 asection *found_section;
258 bfd_boolean multiple_sections_found;
259 } section_iterator_callback_data;
261 static bfd_boolean
262 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
264 section_iterator_callback_data *d = data;
266 if (d->found_section != NULL)
268 d->multiple_sections_found = TRUE;
269 return TRUE;
272 d->found_section = s;
273 return FALSE;
276 static asection *
277 find_section (lang_input_statement_type *file,
278 struct wildcard_list *sec,
279 bfd_boolean *multiple_sections_found)
281 section_iterator_callback_data cb_data = { NULL, FALSE };
283 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
284 section_iterator_callback, &cb_data);
285 *multiple_sections_found = cb_data.multiple_sections_found;
286 return cb_data.found_section;
289 /* Code for handling simple wildcards without going through fnmatch,
290 which can be expensive because of charset translations etc. */
292 /* A simple wild is a literal string followed by a single '*',
293 where the literal part is at least 4 characters long. */
295 static bfd_boolean
296 is_simple_wild (const char *name)
298 size_t len = strcspn (name, "*?[");
299 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
302 static bfd_boolean
303 match_simple_wild (const char *pattern, const char *name)
305 /* The first four characters of the pattern are guaranteed valid
306 non-wildcard characters. So we can go faster. */
307 if (pattern[0] != name[0] || pattern[1] != name[1]
308 || pattern[2] != name[2] || pattern[3] != name[3])
309 return FALSE;
311 pattern += 4;
312 name += 4;
313 while (*pattern != '*')
314 if (*name++ != *pattern++)
315 return FALSE;
317 return TRUE;
320 /* Specialized, optimized routines for handling different kinds of
321 wildcards */
323 static void
324 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
325 lang_input_statement_type *file,
326 callback_t callback,
327 void *data)
329 /* We can just do a hash lookup for the section with the right name.
330 But if that lookup discovers more than one section with the name
331 (should be rare), we fall back to the general algorithm because
332 we would otherwise have to sort the sections to make sure they
333 get processed in the bfd's order. */
334 bfd_boolean multiple_sections_found;
335 struct wildcard_list *sec0 = ptr->handler_data[0];
336 asection *s0 = find_section (file, sec0, &multiple_sections_found);
338 if (multiple_sections_found)
339 walk_wild_section_general (ptr, file, callback, data);
340 else if (s0)
341 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
344 static void
345 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
346 lang_input_statement_type *file,
347 callback_t callback,
348 void *data)
350 asection *s;
351 struct wildcard_list *wildsec0 = ptr->handler_data[0];
353 for (s = file->the_bfd->sections; s != NULL; s = s->next)
355 const char *sname = bfd_get_section_name (file->the_bfd, s);
356 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
358 if (!skip)
359 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
363 static void
364 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
365 lang_input_statement_type *file,
366 callback_t callback,
367 void *data)
369 asection *s;
370 struct wildcard_list *sec0 = ptr->handler_data[0];
371 struct wildcard_list *wildsec1 = ptr->handler_data[1];
372 bfd_boolean multiple_sections_found;
373 asection *s0 = find_section (file, sec0, &multiple_sections_found);
375 if (multiple_sections_found)
377 walk_wild_section_general (ptr, file, callback, data);
378 return;
381 /* Note that if the section was not found, s0 is NULL and
382 we'll simply never succeed the s == s0 test below. */
383 for (s = file->the_bfd->sections; s != NULL; s = s->next)
385 /* Recall that in this code path, a section cannot satisfy more
386 than one spec, so if s == s0 then it cannot match
387 wildspec1. */
388 if (s == s0)
389 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
390 else
392 const char *sname = bfd_get_section_name (file->the_bfd, s);
393 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
395 if (!skip)
396 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
397 data);
402 static void
403 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
404 lang_input_statement_type *file,
405 callback_t callback,
406 void *data)
408 asection *s;
409 struct wildcard_list *sec0 = ptr->handler_data[0];
410 struct wildcard_list *wildsec1 = ptr->handler_data[1];
411 struct wildcard_list *wildsec2 = ptr->handler_data[2];
412 bfd_boolean multiple_sections_found;
413 asection *s0 = find_section (file, sec0, &multiple_sections_found);
415 if (multiple_sections_found)
417 walk_wild_section_general (ptr, file, callback, data);
418 return;
421 for (s = file->the_bfd->sections; s != NULL; s = s->next)
423 if (s == s0)
424 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
425 else
427 const char *sname = bfd_get_section_name (file->the_bfd, s);
428 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
430 if (!skip)
431 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
432 else
434 skip = !match_simple_wild (wildsec2->spec.name, sname);
435 if (!skip)
436 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
437 data);
443 static void
444 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
445 lang_input_statement_type *file,
446 callback_t callback,
447 void *data)
449 asection *s;
450 struct wildcard_list *sec0 = ptr->handler_data[0];
451 struct wildcard_list *sec1 = ptr->handler_data[1];
452 struct wildcard_list *wildsec2 = ptr->handler_data[2];
453 struct wildcard_list *wildsec3 = ptr->handler_data[3];
454 bfd_boolean multiple_sections_found;
455 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
457 if (multiple_sections_found)
459 walk_wild_section_general (ptr, file, callback, data);
460 return;
463 s1 = find_section (file, sec1, &multiple_sections_found);
464 if (multiple_sections_found)
466 walk_wild_section_general (ptr, file, callback, data);
467 return;
470 for (s = file->the_bfd->sections; s != NULL; s = s->next)
472 if (s == s0)
473 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
474 else
475 if (s == s1)
476 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
477 else
479 const char *sname = bfd_get_section_name (file->the_bfd, s);
480 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
481 sname);
483 if (!skip)
484 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
485 data);
486 else
488 skip = !match_simple_wild (wildsec3->spec.name, sname);
489 if (!skip)
490 walk_wild_consider_section (ptr, file, s, wildsec3,
491 callback, data);
497 static void
498 walk_wild_section (lang_wild_statement_type *ptr,
499 lang_input_statement_type *file,
500 callback_t callback,
501 void *data)
503 if (file->just_syms_flag)
504 return;
506 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
509 /* Returns TRUE when name1 is a wildcard spec that might match
510 something name2 can match. We're conservative: we return FALSE
511 only if the prefixes of name1 and name2 are different up to the
512 first wildcard character. */
514 static bfd_boolean
515 wild_spec_can_overlap (const char *name1, const char *name2)
517 size_t prefix1_len = strcspn (name1, "?*[");
518 size_t prefix2_len = strcspn (name2, "?*[");
519 size_t min_prefix_len;
521 /* Note that if there is no wildcard character, then we treat the
522 terminating 0 as part of the prefix. Thus ".text" won't match
523 ".text." or ".text.*", for example. */
524 if (name1[prefix1_len] == '\0')
525 prefix1_len++;
526 if (name2[prefix2_len] == '\0')
527 prefix2_len++;
529 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
531 return memcmp (name1, name2, min_prefix_len) == 0;
534 /* Select specialized code to handle various kinds of wildcard
535 statements. */
537 static void
538 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
540 int sec_count = 0;
541 int wild_name_count = 0;
542 struct wildcard_list *sec;
543 int signature;
544 int data_counter;
546 ptr->walk_wild_section_handler = walk_wild_section_general;
548 /* Count how many wildcard_specs there are, and how many of those
549 actually use wildcards in the name. Also, bail out if any of the
550 wildcard names are NULL. (Can this actually happen?
551 walk_wild_section used to test for it.) And bail out if any
552 of the wildcards are more complex than a simple string
553 ending in a single '*'. */
554 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
556 ++sec_count;
557 if (sec->spec.name == NULL)
558 return;
559 if (wildcardp (sec->spec.name))
561 ++wild_name_count;
562 if (!is_simple_wild (sec->spec.name))
563 return;
567 /* The zero-spec case would be easy to optimize but it doesn't
568 happen in practice. Likewise, more than 4 specs doesn't
569 happen in practice. */
570 if (sec_count == 0 || sec_count > 4)
571 return;
573 /* Check that no two specs can match the same section. */
574 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
576 struct wildcard_list *sec2;
577 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
579 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
580 return;
584 signature = (sec_count << 8) + wild_name_count;
585 switch (signature)
587 case 0x0100:
588 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
589 break;
590 case 0x0101:
591 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
592 break;
593 case 0x0201:
594 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
595 break;
596 case 0x0302:
597 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
598 break;
599 case 0x0402:
600 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
601 break;
602 default:
603 return;
606 /* Now fill the data array with pointers to the specs, first the
607 specs with non-wildcard names, then the specs with wildcard
608 names. It's OK to process the specs in different order from the
609 given order, because we've already determined that no section
610 will match more than one spec. */
611 data_counter = 0;
612 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
613 if (!wildcardp (sec->spec.name))
614 ptr->handler_data[data_counter++] = sec;
615 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
616 if (wildcardp (sec->spec.name))
617 ptr->handler_data[data_counter++] = sec;
620 /* Handle a wild statement for a single file F. */
622 static void
623 walk_wild_file (lang_wild_statement_type *s,
624 lang_input_statement_type *f,
625 callback_t callback,
626 void *data)
628 if (f->the_bfd == NULL
629 || ! bfd_check_format (f->the_bfd, bfd_archive))
630 walk_wild_section (s, f, callback, data);
631 else
633 bfd *member;
635 /* This is an archive file. We must map each member of the
636 archive separately. */
637 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
638 while (member != NULL)
640 /* When lookup_name is called, it will call the add_symbols
641 entry point for the archive. For each element of the
642 archive which is included, BFD will call ldlang_add_file,
643 which will set the usrdata field of the member to the
644 lang_input_statement. */
645 if (member->usrdata != NULL)
647 walk_wild_section (s, member->usrdata, callback, data);
650 member = bfd_openr_next_archived_file (f->the_bfd, member);
655 static void
656 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
658 const char *file_spec = s->filename;
660 if (file_spec == NULL)
662 /* Perform the iteration over all files in the list. */
663 LANG_FOR_EACH_INPUT_STATEMENT (f)
665 walk_wild_file (s, f, callback, data);
668 else if (wildcardp (file_spec))
670 LANG_FOR_EACH_INPUT_STATEMENT (f)
672 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
673 walk_wild_file (s, f, callback, data);
676 else
678 lang_input_statement_type *f;
680 /* Perform the iteration over a single file. */
681 f = lookup_name (file_spec);
682 if (f)
683 walk_wild_file (s, f, callback, data);
687 /* lang_for_each_statement walks the parse tree and calls the provided
688 function for each node. */
690 static void
691 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
692 lang_statement_union_type *s)
694 for (; s != NULL; s = s->header.next)
696 func (s);
698 switch (s->header.type)
700 case lang_constructors_statement_enum:
701 lang_for_each_statement_worker (func, constructor_list.head);
702 break;
703 case lang_output_section_statement_enum:
704 lang_for_each_statement_worker
705 (func, s->output_section_statement.children.head);
706 break;
707 case lang_wild_statement_enum:
708 lang_for_each_statement_worker (func,
709 s->wild_statement.children.head);
710 break;
711 case lang_group_statement_enum:
712 lang_for_each_statement_worker (func,
713 s->group_statement.children.head);
714 break;
715 case lang_data_statement_enum:
716 case lang_reloc_statement_enum:
717 case lang_object_symbols_statement_enum:
718 case lang_output_statement_enum:
719 case lang_target_statement_enum:
720 case lang_input_section_enum:
721 case lang_input_statement_enum:
722 case lang_assignment_statement_enum:
723 case lang_padding_statement_enum:
724 case lang_address_statement_enum:
725 case lang_fill_statement_enum:
726 break;
727 default:
728 FAIL ();
729 break;
734 void
735 lang_for_each_statement (void (*func) (lang_statement_union_type *))
737 lang_for_each_statement_worker (func, statement_list.head);
740 /*----------------------------------------------------------------------*/
742 void
743 lang_list_init (lang_statement_list_type *list)
745 list->head = NULL;
746 list->tail = &list->head;
749 /* Build a new statement node for the parse tree. */
751 static lang_statement_union_type *
752 new_statement (enum statement_enum type,
753 size_t size,
754 lang_statement_list_type *list)
756 lang_statement_union_type *new;
758 new = stat_alloc (size);
759 new->header.type = type;
760 new->header.next = NULL;
761 lang_statement_append (list, new, &new->header.next);
762 return new;
765 /* Build a new input file node for the language. There are several
766 ways in which we treat an input file, eg, we only look at symbols,
767 or prefix it with a -l etc.
769 We can be supplied with requests for input files more than once;
770 they may, for example be split over several lines like foo.o(.text)
771 foo.o(.data) etc, so when asked for a file we check that we haven't
772 got it already so we don't duplicate the bfd. */
774 static lang_input_statement_type *
775 new_afile (const char *name,
776 lang_input_file_enum_type file_type,
777 const char *target,
778 bfd_boolean add_to_list)
780 lang_input_statement_type *p;
782 if (add_to_list)
783 p = new_stat (lang_input_statement, stat_ptr);
784 else
786 p = stat_alloc (sizeof (lang_input_statement_type));
787 p->header.type = lang_input_statement_enum;
788 p->header.next = NULL;
791 lang_has_input_file = TRUE;
792 p->target = target;
793 p->sysrooted = FALSE;
794 switch (file_type)
796 case lang_input_file_is_symbols_only_enum:
797 p->filename = name;
798 p->is_archive = FALSE;
799 p->real = TRUE;
800 p->local_sym_name = name;
801 p->just_syms_flag = TRUE;
802 p->search_dirs_flag = FALSE;
803 break;
804 case lang_input_file_is_fake_enum:
805 p->filename = name;
806 p->is_archive = FALSE;
807 p->real = FALSE;
808 p->local_sym_name = name;
809 p->just_syms_flag = FALSE;
810 p->search_dirs_flag = FALSE;
811 break;
812 case lang_input_file_is_l_enum:
813 p->is_archive = TRUE;
814 p->filename = name;
815 p->real = TRUE;
816 p->local_sym_name = concat ("-l", name, NULL);
817 p->just_syms_flag = FALSE;
818 p->search_dirs_flag = TRUE;
819 break;
820 case lang_input_file_is_marker_enum:
821 p->filename = name;
822 p->is_archive = FALSE;
823 p->real = FALSE;
824 p->local_sym_name = name;
825 p->just_syms_flag = FALSE;
826 p->search_dirs_flag = TRUE;
827 break;
828 case lang_input_file_is_search_file_enum:
829 p->sysrooted = ldlang_sysrooted_script;
830 p->filename = name;
831 p->is_archive = FALSE;
832 p->real = TRUE;
833 p->local_sym_name = name;
834 p->just_syms_flag = FALSE;
835 p->search_dirs_flag = TRUE;
836 break;
837 case lang_input_file_is_file_enum:
838 p->filename = name;
839 p->is_archive = FALSE;
840 p->real = TRUE;
841 p->local_sym_name = name;
842 p->just_syms_flag = FALSE;
843 p->search_dirs_flag = FALSE;
844 break;
845 default:
846 FAIL ();
848 p->the_bfd = NULL;
849 p->asymbols = NULL;
850 p->next_real_file = NULL;
851 p->next = NULL;
852 p->symbol_count = 0;
853 p->dynamic = config.dynamic_link;
854 p->add_needed = add_needed;
855 p->as_needed = as_needed;
856 p->whole_archive = whole_archive;
857 p->loaded = FALSE;
858 lang_statement_append (&input_file_chain,
859 (lang_statement_union_type *) p,
860 &p->next_real_file);
861 return p;
864 lang_input_statement_type *
865 lang_add_input_file (const char *name,
866 lang_input_file_enum_type file_type,
867 const char *target)
869 lang_has_input_file = TRUE;
870 return new_afile (name, file_type, target, TRUE);
873 struct out_section_hash_entry
875 struct bfd_hash_entry root;
876 lang_statement_union_type s;
879 /* The hash table. */
881 static struct bfd_hash_table output_section_statement_table;
883 /* Support routines for the hash table used by lang_output_section_find,
884 initialize the table, fill in an entry and remove the table. */
886 static struct bfd_hash_entry *
887 output_section_statement_newfunc (struct bfd_hash_entry *entry,
888 struct bfd_hash_table *table,
889 const char *string)
891 lang_output_section_statement_type **nextp;
892 struct out_section_hash_entry *ret;
894 if (entry == NULL)
896 entry = bfd_hash_allocate (table, sizeof (*ret));
897 if (entry == NULL)
898 return entry;
901 entry = bfd_hash_newfunc (entry, table, string);
902 if (entry == NULL)
903 return entry;
905 ret = (struct out_section_hash_entry *) entry;
906 memset (&ret->s, 0, sizeof (ret->s));
907 ret->s.header.type = lang_output_section_statement_enum;
908 ret->s.output_section_statement.subsection_alignment = -1;
909 ret->s.output_section_statement.section_alignment = -1;
910 ret->s.output_section_statement.block_value = 1;
911 lang_list_init (&ret->s.output_section_statement.children);
912 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
914 /* For every output section statement added to the list, except the
915 first one, lang_output_section_statement.tail points to the "next"
916 field of the last element of the list. */
917 if (lang_output_section_statement.head != NULL)
918 ret->s.output_section_statement.prev
919 = ((lang_output_section_statement_type *)
920 ((char *) lang_output_section_statement.tail
921 - offsetof (lang_output_section_statement_type, next)));
923 /* GCC's strict aliasing rules prevent us from just casting the
924 address, so we store the pointer in a variable and cast that
925 instead. */
926 nextp = &ret->s.output_section_statement.next;
927 lang_statement_append (&lang_output_section_statement,
928 &ret->s,
929 (lang_statement_union_type **) nextp);
930 return &ret->root;
933 static void
934 output_section_statement_table_init (void)
936 if (!bfd_hash_table_init_n (&output_section_statement_table,
937 output_section_statement_newfunc,
938 sizeof (struct out_section_hash_entry),
939 61))
940 einfo (_("%P%F: can not create hash table: %E\n"));
943 static void
944 output_section_statement_table_free (void)
946 bfd_hash_table_free (&output_section_statement_table);
949 /* Build enough state so that the parser can build its tree. */
951 void
952 lang_init (void)
954 obstack_begin (&stat_obstack, 1000);
956 stat_ptr = &statement_list;
958 output_section_statement_table_init ();
960 lang_list_init (stat_ptr);
962 lang_list_init (&input_file_chain);
963 lang_list_init (&lang_output_section_statement);
964 lang_list_init (&file_chain);
965 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
966 NULL);
967 abs_output_section =
968 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
970 abs_output_section->bfd_section = bfd_abs_section_ptr;
972 /* The value "3" is ad-hoc, somewhat related to the expected number of
973 DEFINED expressions in a linker script. For most default linker
974 scripts, there are none. Why a hash table then? Well, it's somewhat
975 simpler to re-use working machinery than using a linked list in terms
976 of code-complexity here in ld, besides the initialization which just
977 looks like other code here. */
978 if (!bfd_hash_table_init_n (&lang_definedness_table,
979 lang_definedness_newfunc,
980 sizeof (struct lang_definedness_hash_entry),
982 einfo (_("%P%F: can not create hash table: %E\n"));
985 void
986 lang_finish (void)
988 output_section_statement_table_free ();
991 /*----------------------------------------------------------------------
992 A region is an area of memory declared with the
993 MEMORY { name:org=exp, len=exp ... }
994 syntax.
996 We maintain a list of all the regions here.
998 If no regions are specified in the script, then the default is used
999 which is created when looked up to be the entire data space.
1001 If create is true we are creating a region inside a MEMORY block.
1002 In this case it is probably an error to create a region that has
1003 already been created. If we are not inside a MEMORY block it is
1004 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1005 and so we issue a warning. */
1007 static lang_memory_region_type *lang_memory_region_list;
1008 static lang_memory_region_type **lang_memory_region_list_tail
1009 = &lang_memory_region_list;
1011 lang_memory_region_type *
1012 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1014 lang_memory_region_type *p;
1015 lang_memory_region_type *new;
1017 /* NAME is NULL for LMA memspecs if no region was specified. */
1018 if (name == NULL)
1019 return NULL;
1021 for (p = lang_memory_region_list; p != NULL; p = p->next)
1022 if (strcmp (p->name, name) == 0)
1024 if (create)
1025 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1026 name);
1027 return p;
1030 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1031 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
1033 new = stat_alloc (sizeof (lang_memory_region_type));
1035 new->name = xstrdup (name);
1036 new->next = NULL;
1038 *lang_memory_region_list_tail = new;
1039 lang_memory_region_list_tail = &new->next;
1040 new->origin = 0;
1041 new->flags = 0;
1042 new->not_flags = 0;
1043 new->length = ~(bfd_size_type) 0;
1044 new->current = 0;
1045 new->had_full_message = FALSE;
1047 return new;
1050 static lang_memory_region_type *
1051 lang_memory_default (asection *section)
1053 lang_memory_region_type *p;
1055 flagword sec_flags = section->flags;
1057 /* Override SEC_DATA to mean a writable section. */
1058 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1059 sec_flags |= SEC_DATA;
1061 for (p = lang_memory_region_list; p != NULL; p = p->next)
1063 if ((p->flags & sec_flags) != 0
1064 && (p->not_flags & sec_flags) == 0)
1066 return p;
1069 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1072 lang_output_section_statement_type *
1073 lang_output_section_find (const char *const name)
1075 struct out_section_hash_entry *entry;
1076 unsigned long hash;
1078 entry = ((struct out_section_hash_entry *)
1079 bfd_hash_lookup (&output_section_statement_table, name,
1080 FALSE, FALSE));
1081 if (entry == NULL)
1082 return NULL;
1084 hash = entry->root.hash;
1087 if (entry->s.output_section_statement.constraint != -1)
1088 return &entry->s.output_section_statement;
1089 entry = (struct out_section_hash_entry *) entry->root.next;
1091 while (entry != NULL
1092 && entry->root.hash == hash
1093 && strcmp (name, entry->s.output_section_statement.name) == 0);
1095 return NULL;
1098 static lang_output_section_statement_type *
1099 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
1101 struct out_section_hash_entry *entry;
1102 struct out_section_hash_entry *last_ent;
1103 unsigned long hash;
1105 entry = ((struct out_section_hash_entry *)
1106 bfd_hash_lookup (&output_section_statement_table, name,
1107 TRUE, FALSE));
1108 if (entry == NULL)
1110 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1111 return NULL;
1114 if (entry->s.output_section_statement.name != NULL)
1116 /* We have a section of this name, but it might not have the correct
1117 constraint. */
1118 hash = entry->root.hash;
1121 if (entry->s.output_section_statement.constraint != -1
1122 && (constraint == 0
1123 || (constraint == entry->s.output_section_statement.constraint
1124 && constraint != SPECIAL)))
1125 return &entry->s.output_section_statement;
1126 last_ent = entry;
1127 entry = (struct out_section_hash_entry *) entry->root.next;
1129 while (entry != NULL
1130 && entry->root.hash == hash
1131 && strcmp (name, entry->s.output_section_statement.name) == 0);
1133 entry
1134 = ((struct out_section_hash_entry *)
1135 output_section_statement_newfunc (NULL,
1136 &output_section_statement_table,
1137 name));
1138 if (entry == NULL)
1140 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1141 return NULL;
1143 entry->root = last_ent->root;
1144 last_ent->root.next = &entry->root;
1147 entry->s.output_section_statement.name = name;
1148 entry->s.output_section_statement.constraint = constraint;
1149 return &entry->s.output_section_statement;
1152 lang_output_section_statement_type *
1153 lang_output_section_statement_lookup (const char *const name)
1155 return lang_output_section_statement_lookup_1 (name, 0);
1158 /* A variant of lang_output_section_find used by place_orphan.
1159 Returns the output statement that should precede a new output
1160 statement for SEC. If an exact match is found on certain flags,
1161 sets *EXACT too. */
1163 lang_output_section_statement_type *
1164 lang_output_section_find_by_flags (const asection *sec,
1165 lang_output_section_statement_type **exact,
1166 lang_match_sec_type_func match_type)
1168 lang_output_section_statement_type *first, *look, *found;
1169 flagword flags;
1171 /* We know the first statement on this list is *ABS*. May as well
1172 skip it. */
1173 first = &lang_output_section_statement.head->output_section_statement;
1174 first = first->next;
1176 /* First try for an exact match. */
1177 found = NULL;
1178 for (look = first; look; look = look->next)
1180 flags = look->flags;
1181 if (look->bfd_section != NULL)
1183 flags = look->bfd_section->flags;
1184 if (match_type && !match_type (output_bfd, look->bfd_section,
1185 sec->owner, sec))
1186 continue;
1188 flags ^= sec->flags;
1189 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1190 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1191 found = look;
1193 if (found != NULL)
1195 if (exact != NULL)
1196 *exact = found;
1197 return found;
1200 if (sec->flags & SEC_CODE)
1202 /* Try for a rw code section. */
1203 for (look = first; look; look = look->next)
1205 flags = look->flags;
1206 if (look->bfd_section != NULL)
1208 flags = look->bfd_section->flags;
1209 if (match_type && !match_type (output_bfd, look->bfd_section,
1210 sec->owner, sec))
1211 continue;
1213 flags ^= sec->flags;
1214 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1215 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1216 found = look;
1219 else if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
1221 /* .rodata can go after .text, .sdata2 after .rodata. */
1222 for (look = first; look; look = look->next)
1224 flags = look->flags;
1225 if (look->bfd_section != NULL)
1227 flags = look->bfd_section->flags;
1228 if (match_type && !match_type (output_bfd, look->bfd_section,
1229 sec->owner, sec))
1230 continue;
1232 flags ^= sec->flags;
1233 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1234 | SEC_READONLY))
1235 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1236 found = look;
1239 else if (sec->flags & SEC_SMALL_DATA)
1241 /* .sdata goes after .data, .sbss after .sdata. */
1242 for (look = first; look; look = look->next)
1244 flags = look->flags;
1245 if (look->bfd_section != NULL)
1247 flags = look->bfd_section->flags;
1248 if (match_type && !match_type (output_bfd, look->bfd_section,
1249 sec->owner, sec))
1250 continue;
1252 flags ^= sec->flags;
1253 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1254 | SEC_THREAD_LOCAL))
1255 || ((look->flags & SEC_SMALL_DATA)
1256 && !(sec->flags & SEC_HAS_CONTENTS)))
1257 found = look;
1260 else if (sec->flags & SEC_HAS_CONTENTS)
1262 /* .data goes after .rodata. */
1263 for (look = first; look; look = look->next)
1265 flags = look->flags;
1266 if (look->bfd_section != NULL)
1268 flags = look->bfd_section->flags;
1269 if (match_type && !match_type (output_bfd, look->bfd_section,
1270 sec->owner, sec))
1271 continue;
1273 flags ^= sec->flags;
1274 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1275 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1276 found = look;
1279 else
1281 /* .bss goes last. */
1282 for (look = first; look; look = look->next)
1284 flags = look->flags;
1285 if (look->bfd_section != NULL)
1287 flags = look->bfd_section->flags;
1288 if (match_type && !match_type (output_bfd, look->bfd_section,
1289 sec->owner, sec))
1290 continue;
1292 flags ^= sec->flags;
1293 if (!(flags & SEC_ALLOC))
1294 found = look;
1298 if (found || !match_type)
1299 return found;
1301 return lang_output_section_find_by_flags (sec, NULL, NULL);
1304 /* Find the last output section before given output statement.
1305 Used by place_orphan. */
1307 static asection *
1308 output_prev_sec_find (lang_output_section_statement_type *os)
1310 lang_output_section_statement_type *lookup;
1312 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1314 if (lookup->constraint == -1)
1315 continue;
1317 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1318 return lookup->bfd_section;
1321 return NULL;
1324 lang_output_section_statement_type *
1325 lang_insert_orphan (asection *s,
1326 const char *secname,
1327 lang_output_section_statement_type *after,
1328 struct orphan_save *place,
1329 etree_type *address,
1330 lang_statement_list_type *add_child)
1332 lang_statement_list_type *old;
1333 lang_statement_list_type add;
1334 const char *ps;
1335 etree_type *load_base;
1336 lang_output_section_statement_type *os;
1337 lang_output_section_statement_type **os_tail;
1339 /* Start building a list of statements for this section.
1340 First save the current statement pointer. */
1341 old = stat_ptr;
1343 /* If we have found an appropriate place for the output section
1344 statements for this orphan, add them to our own private list,
1345 inserting them later into the global statement list. */
1346 if (after != NULL)
1348 stat_ptr = &add;
1349 lang_list_init (stat_ptr);
1352 ps = NULL;
1353 if (config.build_constructors)
1355 /* If the name of the section is representable in C, then create
1356 symbols to mark the start and the end of the section. */
1357 for (ps = secname; *ps != '\0'; ps++)
1358 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1359 break;
1360 if (*ps == '\0')
1362 char *symname;
1363 etree_type *e_align;
1365 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1366 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1367 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1368 e_align = exp_unop (ALIGN_K,
1369 exp_intop ((bfd_vma) 1 << s->alignment_power));
1370 lang_add_assignment (exp_assop ('=', ".", e_align));
1371 lang_add_assignment (exp_assop ('=', symname,
1372 exp_nameop (NAME, ".")));
1376 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1377 address = exp_intop (0);
1379 load_base = NULL;
1380 if (after != NULL && after->load_base != NULL)
1382 etree_type *lma_from_vma;
1383 lma_from_vma = exp_binop ('-', after->load_base,
1384 exp_nameop (ADDR, after->name));
1385 load_base = exp_binop ('+', lma_from_vma,
1386 exp_nameop (ADDR, secname));
1389 os_tail = ((lang_output_section_statement_type **)
1390 lang_output_section_statement.tail);
1391 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1392 load_base, 0);
1394 if (add_child == NULL)
1395 add_child = &os->children;
1396 lang_add_section (add_child, s, os);
1398 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1400 if (config.build_constructors && *ps == '\0')
1402 char *symname;
1404 /* lang_leave_ouput_section_statement resets stat_ptr.
1405 Put stat_ptr back where we want it. */
1406 if (after != NULL)
1407 stat_ptr = &add;
1409 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1410 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1411 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1412 lang_add_assignment (exp_assop ('=', symname,
1413 exp_nameop (NAME, ".")));
1416 /* Restore the global list pointer. */
1417 if (after != NULL)
1418 stat_ptr = old;
1420 if (after != NULL && os->bfd_section != NULL)
1422 asection *snew, *as;
1424 snew = os->bfd_section;
1426 /* Shuffle the bfd section list to make the output file look
1427 neater. This is really only cosmetic. */
1428 if (place->section == NULL
1429 && after != (&lang_output_section_statement.head
1430 ->output_section_statement))
1432 asection *bfd_section = after->bfd_section;
1434 /* If the output statement hasn't been used to place any input
1435 sections (and thus doesn't have an output bfd_section),
1436 look for the closest prior output statement having an
1437 output section. */
1438 if (bfd_section == NULL)
1439 bfd_section = output_prev_sec_find (after);
1441 if (bfd_section != NULL && bfd_section != snew)
1442 place->section = &bfd_section->next;
1445 if (place->section == NULL)
1446 place->section = &output_bfd->sections;
1448 as = *place->section;
1449 if (as != snew && as->prev != snew)
1451 /* Unlink the section. */
1452 bfd_section_list_remove (output_bfd, snew);
1454 /* Now tack it back on in the right place. */
1455 bfd_section_list_insert_before (output_bfd, as, snew);
1458 /* Save the end of this list. Further ophans of this type will
1459 follow the one we've just added. */
1460 place->section = &snew->next;
1462 /* The following is non-cosmetic. We try to put the output
1463 statements in some sort of reasonable order here, because they
1464 determine the final load addresses of the orphan sections.
1465 In addition, placing output statements in the wrong order may
1466 require extra segments. For instance, given a typical
1467 situation of all read-only sections placed in one segment and
1468 following that a segment containing all the read-write
1469 sections, we wouldn't want to place an orphan read/write
1470 section before or amongst the read-only ones. */
1471 if (add.head != NULL)
1473 lang_output_section_statement_type *newly_added_os;
1475 if (place->stmt == NULL)
1477 lang_statement_union_type **where;
1478 lang_statement_union_type **assign = NULL;
1479 bfd_boolean ignore_first;
1481 /* Look for a suitable place for the new statement list.
1482 The idea is to skip over anything that might be inside
1483 a SECTIONS {} statement in a script, before we find
1484 another output_section_statement. Assignments to "dot"
1485 before an output section statement are assumed to
1486 belong to it. An exception to this rule is made for
1487 the first assignment to dot, otherwise we might put an
1488 orphan before . = . + SIZEOF_HEADERS or similar
1489 assignments that set the initial address. */
1491 ignore_first = after == (&lang_output_section_statement.head
1492 ->output_section_statement);
1493 for (where = &after->header.next;
1494 *where != NULL;
1495 where = &(*where)->header.next)
1497 switch ((*where)->header.type)
1499 case lang_assignment_statement_enum:
1500 if (assign == NULL)
1502 lang_assignment_statement_type *ass;
1503 ass = &(*where)->assignment_statement;
1504 if (ass->exp->type.node_class != etree_assert
1505 && ass->exp->assign.dst[0] == '.'
1506 && ass->exp->assign.dst[1] == 0
1507 && !ignore_first)
1508 assign = where;
1510 ignore_first = FALSE;
1511 continue;
1512 case lang_wild_statement_enum:
1513 case lang_input_section_enum:
1514 case lang_object_symbols_statement_enum:
1515 case lang_fill_statement_enum:
1516 case lang_data_statement_enum:
1517 case lang_reloc_statement_enum:
1518 case lang_padding_statement_enum:
1519 case lang_constructors_statement_enum:
1520 assign = NULL;
1521 continue;
1522 case lang_output_section_statement_enum:
1523 if (assign != NULL)
1524 where = assign;
1525 case lang_input_statement_enum:
1526 case lang_address_statement_enum:
1527 case lang_target_statement_enum:
1528 case lang_output_statement_enum:
1529 case lang_group_statement_enum:
1530 case lang_afile_asection_pair_statement_enum:
1531 break;
1533 break;
1536 *add.tail = *where;
1537 *where = add.head;
1539 place->os_tail = &after->next;
1541 else
1543 /* Put it after the last orphan statement we added. */
1544 *add.tail = *place->stmt;
1545 *place->stmt = add.head;
1548 /* Fix the global list pointer if we happened to tack our
1549 new list at the tail. */
1550 if (*old->tail == add.head)
1551 old->tail = add.tail;
1553 /* Save the end of this list. */
1554 place->stmt = add.tail;
1556 /* Do the same for the list of output section statements. */
1557 newly_added_os = *os_tail;
1558 *os_tail = NULL;
1559 newly_added_os->prev = (lang_output_section_statement_type *)
1560 ((char *) place->os_tail
1561 - offsetof (lang_output_section_statement_type, next));
1562 newly_added_os->next = *place->os_tail;
1563 if (newly_added_os->next != NULL)
1564 newly_added_os->next->prev = newly_added_os;
1565 *place->os_tail = newly_added_os;
1566 place->os_tail = &newly_added_os->next;
1568 /* Fixing the global list pointer here is a little different.
1569 We added to the list in lang_enter_output_section_statement,
1570 trimmed off the new output_section_statment above when
1571 assigning *os_tail = NULL, but possibly added it back in
1572 the same place when assigning *place->os_tail. */
1573 if (*os_tail == NULL)
1574 lang_output_section_statement.tail
1575 = (lang_statement_union_type **) os_tail;
1578 return os;
1581 static void
1582 lang_map_flags (flagword flag)
1584 if (flag & SEC_ALLOC)
1585 minfo ("a");
1587 if (flag & SEC_CODE)
1588 minfo ("x");
1590 if (flag & SEC_READONLY)
1591 minfo ("r");
1593 if (flag & SEC_DATA)
1594 minfo ("w");
1596 if (flag & SEC_LOAD)
1597 minfo ("l");
1600 void
1601 lang_map (void)
1603 lang_memory_region_type *m;
1604 bfd_boolean dis_header_printed = FALSE;
1605 bfd *p;
1607 LANG_FOR_EACH_INPUT_STATEMENT (file)
1609 asection *s;
1611 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1612 || file->just_syms_flag)
1613 continue;
1615 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1616 if (s->output_section == NULL
1617 || s->output_section->owner != output_bfd)
1619 if (! dis_header_printed)
1621 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1622 dis_header_printed = TRUE;
1625 print_input_section (s);
1629 minfo (_("\nMemory Configuration\n\n"));
1630 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1631 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1633 for (m = lang_memory_region_list; m != NULL; m = m->next)
1635 char buf[100];
1636 int len;
1638 fprintf (config.map_file, "%-16s ", m->name);
1640 sprintf_vma (buf, m->origin);
1641 minfo ("0x%s ", buf);
1642 len = strlen (buf);
1643 while (len < 16)
1645 print_space ();
1646 ++len;
1649 minfo ("0x%V", m->length);
1650 if (m->flags || m->not_flags)
1652 #ifndef BFD64
1653 minfo (" ");
1654 #endif
1655 if (m->flags)
1657 print_space ();
1658 lang_map_flags (m->flags);
1661 if (m->not_flags)
1663 minfo (" !");
1664 lang_map_flags (m->not_flags);
1668 print_nl ();
1671 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1673 if (! command_line.reduce_memory_overheads)
1675 obstack_begin (&map_obstack, 1000);
1676 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1677 bfd_map_over_sections (p, init_map_userdata, 0);
1678 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1680 print_statements ();
1683 static void
1684 init_map_userdata (abfd, sec, data)
1685 bfd *abfd ATTRIBUTE_UNUSED;
1686 asection *sec;
1687 void *data ATTRIBUTE_UNUSED;
1689 fat_section_userdata_type *new_data
1690 = ((fat_section_userdata_type *) (stat_alloc
1691 (sizeof (fat_section_userdata_type))));
1693 ASSERT (get_userdata (sec) == NULL);
1694 get_userdata (sec) = new_data;
1695 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1698 static bfd_boolean
1699 sort_def_symbol (hash_entry, info)
1700 struct bfd_link_hash_entry *hash_entry;
1701 void *info ATTRIBUTE_UNUSED;
1703 if (hash_entry->type == bfd_link_hash_defined
1704 || hash_entry->type == bfd_link_hash_defweak)
1706 struct fat_user_section_struct *ud;
1707 struct map_symbol_def *def;
1709 ud = get_userdata (hash_entry->u.def.section);
1710 if (! ud)
1712 /* ??? What do we have to do to initialize this beforehand? */
1713 /* The first time we get here is bfd_abs_section... */
1714 init_map_userdata (0, hash_entry->u.def.section, 0);
1715 ud = get_userdata (hash_entry->u.def.section);
1717 else if (!ud->map_symbol_def_tail)
1718 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1720 def = obstack_alloc (&map_obstack, sizeof *def);
1721 def->entry = hash_entry;
1722 *(ud->map_symbol_def_tail) = def;
1723 ud->map_symbol_def_tail = &def->next;
1725 return TRUE;
1728 /* Initialize an output section. */
1730 static void
1731 init_os (lang_output_section_statement_type *s, asection *isec)
1733 if (s->bfd_section != NULL)
1734 return;
1736 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1737 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1739 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1740 if (s->bfd_section == NULL)
1741 s->bfd_section = bfd_make_section (output_bfd, s->name);
1742 if (s->bfd_section == NULL)
1744 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1745 output_bfd->xvec->name, s->name);
1747 s->bfd_section->output_section = s->bfd_section;
1748 s->bfd_section->output_offset = 0;
1749 if (!command_line.reduce_memory_overheads)
1751 fat_section_userdata_type *new
1752 = stat_alloc (sizeof (fat_section_userdata_type));
1753 memset (new, 0, sizeof (fat_section_userdata_type));
1754 get_userdata (s->bfd_section) = new;
1758 /* If there is a base address, make sure that any sections it might
1759 mention are initialized. */
1760 if (s->addr_tree != NULL)
1761 exp_init_os (s->addr_tree);
1763 if (s->load_base != NULL)
1764 exp_init_os (s->load_base);
1766 /* If supplied an alignment, set it. */
1767 if (s->section_alignment != -1)
1768 s->bfd_section->alignment_power = s->section_alignment;
1770 if (isec)
1771 bfd_init_private_section_data (isec->owner, isec,
1772 output_bfd, s->bfd_section,
1773 &link_info);
1776 /* Make sure that all output sections mentioned in an expression are
1777 initialized. */
1779 static void
1780 exp_init_os (etree_type *exp)
1782 switch (exp->type.node_class)
1784 case etree_assign:
1785 case etree_provide:
1786 exp_init_os (exp->assign.src);
1787 break;
1789 case etree_binary:
1790 exp_init_os (exp->binary.lhs);
1791 exp_init_os (exp->binary.rhs);
1792 break;
1794 case etree_trinary:
1795 exp_init_os (exp->trinary.cond);
1796 exp_init_os (exp->trinary.lhs);
1797 exp_init_os (exp->trinary.rhs);
1798 break;
1800 case etree_assert:
1801 exp_init_os (exp->assert_s.child);
1802 break;
1804 case etree_unary:
1805 exp_init_os (exp->unary.child);
1806 break;
1808 case etree_name:
1809 switch (exp->type.node_code)
1811 case ADDR:
1812 case LOADADDR:
1813 case SIZEOF:
1815 lang_output_section_statement_type *os;
1817 os = lang_output_section_find (exp->name.name);
1818 if (os != NULL && os->bfd_section == NULL)
1819 init_os (os, NULL);
1822 break;
1824 default:
1825 break;
1829 static void
1830 section_already_linked (bfd *abfd, asection *sec, void *data)
1832 lang_input_statement_type *entry = data;
1834 /* If we are only reading symbols from this object, then we want to
1835 discard all sections. */
1836 if (entry->just_syms_flag)
1838 bfd_link_just_syms (abfd, sec, &link_info);
1839 return;
1842 if (!(abfd->flags & DYNAMIC))
1843 bfd_section_already_linked (abfd, sec);
1846 /* The wild routines.
1848 These expand statements like *(.text) and foo.o to a list of
1849 explicit actions, like foo.o(.text), bar.o(.text) and
1850 foo.o(.text, .data). */
1852 /* Add SECTION to the output section OUTPUT. Do this by creating a
1853 lang_input_section statement which is placed at PTR. FILE is the
1854 input file which holds SECTION. */
1856 void
1857 lang_add_section (lang_statement_list_type *ptr,
1858 asection *section,
1859 lang_output_section_statement_type *output)
1861 flagword flags = section->flags;
1862 bfd_boolean discard;
1864 /* Discard sections marked with SEC_EXCLUDE. */
1865 discard = (flags & SEC_EXCLUDE) != 0;
1867 /* Discard input sections which are assigned to a section named
1868 DISCARD_SECTION_NAME. */
1869 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1870 discard = TRUE;
1872 /* Discard debugging sections if we are stripping debugging
1873 information. */
1874 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1875 && (flags & SEC_DEBUGGING) != 0)
1876 discard = TRUE;
1878 if (discard)
1880 if (section->output_section == NULL)
1882 /* This prevents future calls from assigning this section. */
1883 section->output_section = bfd_abs_section_ptr;
1885 return;
1888 if (section->output_section == NULL)
1890 bfd_boolean first;
1891 lang_input_section_type *new;
1892 flagword flags;
1894 if (output->bfd_section == NULL)
1895 init_os (output, section);
1897 first = ! output->bfd_section->linker_has_input;
1898 output->bfd_section->linker_has_input = 1;
1900 if (!link_info.relocatable
1901 && !stripped_excluded_sections)
1903 asection *s = output->bfd_section->map_tail.s;
1904 output->bfd_section->map_tail.s = section;
1905 section->map_head.s = NULL;
1906 section->map_tail.s = s;
1907 if (s != NULL)
1908 s->map_head.s = section;
1909 else
1910 output->bfd_section->map_head.s = section;
1913 /* Add a section reference to the list. */
1914 new = new_stat (lang_input_section, ptr);
1916 new->section = section;
1917 section->output_section = output->bfd_section;
1919 flags = section->flags;
1921 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1922 to an output section, because we want to be able to include a
1923 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1924 section (I don't know why we want to do this, but we do).
1925 build_link_order in ldwrite.c handles this case by turning
1926 the embedded SEC_NEVER_LOAD section into a fill. */
1928 flags &= ~ SEC_NEVER_LOAD;
1930 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1931 already been processed. One reason to do this is that on pe
1932 format targets, .text$foo sections go into .text and it's odd
1933 to see .text with SEC_LINK_ONCE set. */
1935 if (! link_info.relocatable)
1936 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1938 /* If this is not the first input section, and the SEC_READONLY
1939 flag is not currently set, then don't set it just because the
1940 input section has it set. */
1942 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
1943 flags &= ~ SEC_READONLY;
1945 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1946 if (! first
1947 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
1948 != (flags & (SEC_MERGE | SEC_STRINGS))
1949 || ((flags & SEC_MERGE)
1950 && output->bfd_section->entsize != section->entsize)))
1952 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1953 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1956 output->bfd_section->flags |= flags;
1958 if (flags & SEC_MERGE)
1959 output->bfd_section->entsize = section->entsize;
1961 /* If SEC_READONLY is not set in the input section, then clear
1962 it from the output section. */
1963 if ((section->flags & SEC_READONLY) == 0)
1964 output->bfd_section->flags &= ~SEC_READONLY;
1966 switch (output->sectype)
1968 case normal_section:
1969 break;
1970 case dsect_section:
1971 case copy_section:
1972 case info_section:
1973 case overlay_section:
1974 output->bfd_section->flags &= ~SEC_ALLOC;
1975 break;
1976 case noload_section:
1977 output->bfd_section->flags &= ~SEC_LOAD;
1978 output->bfd_section->flags |= SEC_NEVER_LOAD;
1979 break;
1982 /* Copy over SEC_SMALL_DATA. */
1983 if (section->flags & SEC_SMALL_DATA)
1984 output->bfd_section->flags |= SEC_SMALL_DATA;
1986 if (section->alignment_power > output->bfd_section->alignment_power)
1987 output->bfd_section->alignment_power = section->alignment_power;
1989 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
1990 && (section->flags & SEC_TIC54X_BLOCK) != 0)
1992 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
1993 /* FIXME: This value should really be obtained from the bfd... */
1994 output->block_value = 128;
1999 /* Compare sections ASEC and BSEC according to SORT. */
2001 static int
2002 compare_section (sort_type sort, asection *asec, asection *bsec)
2004 int ret;
2006 switch (sort)
2008 default:
2009 abort ();
2011 case by_alignment_name:
2012 ret = (bfd_section_alignment (bsec->owner, bsec)
2013 - bfd_section_alignment (asec->owner, asec));
2014 if (ret)
2015 break;
2016 /* Fall through. */
2018 case by_name:
2019 ret = strcmp (bfd_get_section_name (asec->owner, asec),
2020 bfd_get_section_name (bsec->owner, bsec));
2021 break;
2023 case by_name_alignment:
2024 ret = strcmp (bfd_get_section_name (asec->owner, asec),
2025 bfd_get_section_name (bsec->owner, bsec));
2026 if (ret)
2027 break;
2028 /* Fall through. */
2030 case by_alignment:
2031 ret = (bfd_section_alignment (bsec->owner, bsec)
2032 - bfd_section_alignment (asec->owner, asec));
2033 break;
2036 return ret;
2039 /* Handle wildcard sorting. This returns the lang_input_section which
2040 should follow the one we are going to create for SECTION and FILE,
2041 based on the sorting requirements of WILD. It returns NULL if the
2042 new section should just go at the end of the current list. */
2044 static lang_statement_union_type *
2045 wild_sort (lang_wild_statement_type *wild,
2046 struct wildcard_list *sec,
2047 lang_input_statement_type *file,
2048 asection *section)
2050 const char *section_name;
2051 lang_statement_union_type *l;
2053 if (!wild->filenames_sorted
2054 && (sec == NULL || sec->spec.sorted == none))
2055 return NULL;
2057 section_name = bfd_get_section_name (file->the_bfd, section);
2058 for (l = wild->children.head; l != NULL; l = l->header.next)
2060 lang_input_section_type *ls;
2062 if (l->header.type != lang_input_section_enum)
2063 continue;
2064 ls = &l->input_section;
2066 /* Sorting by filename takes precedence over sorting by section
2067 name. */
2069 if (wild->filenames_sorted)
2071 const char *fn, *ln;
2072 bfd_boolean fa, la;
2073 int i;
2075 /* The PE support for the .idata section as generated by
2076 dlltool assumes that files will be sorted by the name of
2077 the archive and then the name of the file within the
2078 archive. */
2080 if (file->the_bfd != NULL
2081 && bfd_my_archive (file->the_bfd) != NULL)
2083 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2084 fa = TRUE;
2086 else
2088 fn = file->filename;
2089 fa = FALSE;
2092 if (bfd_my_archive (ls->section->owner) != NULL)
2094 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2095 la = TRUE;
2097 else
2099 ln = ls->section->owner->filename;
2100 la = FALSE;
2103 i = strcmp (fn, ln);
2104 if (i > 0)
2105 continue;
2106 else if (i < 0)
2107 break;
2109 if (fa || la)
2111 if (fa)
2112 fn = file->filename;
2113 if (la)
2114 ln = ls->section->owner->filename;
2116 i = strcmp (fn, ln);
2117 if (i > 0)
2118 continue;
2119 else if (i < 0)
2120 break;
2124 /* Here either the files are not sorted by name, or we are
2125 looking at the sections for this file. */
2127 if (sec != NULL && sec->spec.sorted != none)
2128 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2129 break;
2132 return l;
2135 /* Expand a wild statement for a particular FILE. SECTION may be
2136 NULL, in which case it is a wild card. */
2138 static void
2139 output_section_callback (lang_wild_statement_type *ptr,
2140 struct wildcard_list *sec,
2141 asection *section,
2142 lang_input_statement_type *file,
2143 void *output)
2145 lang_statement_union_type *before;
2147 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2148 if (unique_section_p (section))
2149 return;
2151 before = wild_sort (ptr, sec, file, section);
2153 /* Here BEFORE points to the lang_input_section which
2154 should follow the one we are about to add. If BEFORE
2155 is NULL, then the section should just go at the end
2156 of the current list. */
2158 if (before == NULL)
2159 lang_add_section (&ptr->children, section,
2160 (lang_output_section_statement_type *) output);
2161 else
2163 lang_statement_list_type list;
2164 lang_statement_union_type **pp;
2166 lang_list_init (&list);
2167 lang_add_section (&list, section,
2168 (lang_output_section_statement_type *) output);
2170 /* If we are discarding the section, LIST.HEAD will
2171 be NULL. */
2172 if (list.head != NULL)
2174 ASSERT (list.head->header.next == NULL);
2176 for (pp = &ptr->children.head;
2177 *pp != before;
2178 pp = &(*pp)->header.next)
2179 ASSERT (*pp != NULL);
2181 list.head->header.next = *pp;
2182 *pp = list.head;
2187 /* Check if all sections in a wild statement for a particular FILE
2188 are readonly. */
2190 static void
2191 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2192 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2193 asection *section,
2194 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2195 void *data)
2197 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2198 if (unique_section_p (section))
2199 return;
2201 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2202 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2205 /* This is passed a file name which must have been seen already and
2206 added to the statement tree. We will see if it has been opened
2207 already and had its symbols read. If not then we'll read it. */
2209 static lang_input_statement_type *
2210 lookup_name (const char *name)
2212 lang_input_statement_type *search;
2214 for (search = (lang_input_statement_type *) input_file_chain.head;
2215 search != NULL;
2216 search = (lang_input_statement_type *) search->next_real_file)
2218 /* Use the local_sym_name as the name of the file that has
2219 already been loaded as filename might have been transformed
2220 via the search directory lookup mechanism. */
2221 const char * filename = search->local_sym_name;
2223 if (filename == NULL && name == NULL)
2224 return search;
2225 if (filename != NULL
2226 && name != NULL
2227 && strcmp (filename, name) == 0)
2228 break;
2231 if (search == NULL)
2232 search = new_afile (name, lang_input_file_is_search_file_enum,
2233 default_target, FALSE);
2235 /* If we have already added this file, or this file is not real
2236 (FIXME: can that ever actually happen?) or the name is NULL
2237 (FIXME: can that ever actually happen?) don't add this file. */
2238 if (search->loaded
2239 || ! search->real
2240 || search->filename == NULL)
2241 return search;
2243 if (! load_symbols (search, NULL))
2244 return NULL;
2246 return search;
2249 /* Save LIST as a list of libraries whose symbols should not be exported. */
2251 struct excluded_lib
2253 char *name;
2254 struct excluded_lib *next;
2256 static struct excluded_lib *excluded_libs;
2258 void
2259 add_excluded_libs (const char *list)
2261 const char *p = list, *end;
2263 while (*p != '\0')
2265 struct excluded_lib *entry;
2266 end = strpbrk (p, ",:");
2267 if (end == NULL)
2268 end = p + strlen (p);
2269 entry = xmalloc (sizeof (*entry));
2270 entry->next = excluded_libs;
2271 entry->name = xmalloc (end - p + 1);
2272 memcpy (entry->name, p, end - p);
2273 entry->name[end - p] = '\0';
2274 excluded_libs = entry;
2275 if (*end == '\0')
2276 break;
2277 p = end + 1;
2281 static void
2282 check_excluded_libs (bfd *abfd)
2284 struct excluded_lib *lib = excluded_libs;
2286 while (lib)
2288 int len = strlen (lib->name);
2289 const char *filename = lbasename (abfd->filename);
2291 if (strcmp (lib->name, "ALL") == 0)
2293 abfd->no_export = TRUE;
2294 return;
2297 if (strncmp (lib->name, filename, len) == 0
2298 && (filename[len] == '\0'
2299 || (filename[len] == '.' && filename[len + 1] == 'a'
2300 && filename[len + 2] == '\0')))
2302 abfd->no_export = TRUE;
2303 return;
2306 lib = lib->next;
2310 /* Get the symbols for an input file. */
2312 static bfd_boolean
2313 load_symbols (lang_input_statement_type *entry,
2314 lang_statement_list_type *place)
2316 char **matching;
2318 if (entry->loaded)
2319 return TRUE;
2321 ldfile_open_file (entry);
2323 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2324 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2326 bfd_error_type err;
2327 lang_statement_list_type *hold;
2328 bfd_boolean bad_load = TRUE;
2329 bfd_boolean save_ldlang_sysrooted_script;
2330 bfd_boolean save_as_needed, save_add_needed;
2332 err = bfd_get_error ();
2334 /* See if the emulation has some special knowledge. */
2335 if (ldemul_unrecognized_file (entry))
2336 return TRUE;
2338 if (err == bfd_error_file_ambiguously_recognized)
2340 char **p;
2342 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2343 einfo (_("%B: matching formats:"), entry->the_bfd);
2344 for (p = matching; *p != NULL; p++)
2345 einfo (" %s", *p);
2346 einfo ("%F\n");
2348 else if (err != bfd_error_file_not_recognized
2349 || place == NULL)
2350 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2351 else
2352 bad_load = FALSE;
2354 bfd_close (entry->the_bfd);
2355 entry->the_bfd = NULL;
2357 /* Try to interpret the file as a linker script. */
2358 ldfile_open_command_file (entry->filename);
2360 hold = stat_ptr;
2361 stat_ptr = place;
2362 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2363 ldlang_sysrooted_script = entry->sysrooted;
2364 save_as_needed = as_needed;
2365 as_needed = entry->as_needed;
2366 save_add_needed = add_needed;
2367 add_needed = entry->add_needed;
2369 ldfile_assumed_script = TRUE;
2370 parser_input = input_script;
2371 /* We want to use the same -Bdynamic/-Bstatic as the one for
2372 ENTRY. */
2373 config.dynamic_link = entry->dynamic;
2374 yyparse ();
2375 ldfile_assumed_script = FALSE;
2377 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2378 as_needed = save_as_needed;
2379 add_needed = save_add_needed;
2380 stat_ptr = hold;
2382 return ! bad_load;
2385 if (ldemul_recognized_file (entry))
2386 return TRUE;
2388 /* We don't call ldlang_add_file for an archive. Instead, the
2389 add_symbols entry point will call ldlang_add_file, via the
2390 add_archive_element callback, for each element of the archive
2391 which is used. */
2392 switch (bfd_get_format (entry->the_bfd))
2394 default:
2395 break;
2397 case bfd_object:
2398 ldlang_add_file (entry);
2399 if (trace_files || trace_file_tries)
2400 info_msg ("%I\n", entry);
2401 break;
2403 case bfd_archive:
2404 check_excluded_libs (entry->the_bfd);
2406 if (entry->whole_archive)
2408 bfd *member = NULL;
2409 bfd_boolean loaded = TRUE;
2411 for (;;)
2413 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2415 if (member == NULL)
2416 break;
2418 if (! bfd_check_format (member, bfd_object))
2420 einfo (_("%F%B: member %B in archive is not an object\n"),
2421 entry->the_bfd, member);
2422 loaded = FALSE;
2425 if (! ((*link_info.callbacks->add_archive_element)
2426 (&link_info, member, "--whole-archive")))
2427 abort ();
2429 if (! bfd_link_add_symbols (member, &link_info))
2431 einfo (_("%F%B: could not read symbols: %E\n"), member);
2432 loaded = FALSE;
2436 entry->loaded = loaded;
2437 return loaded;
2439 break;
2442 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2443 entry->loaded = TRUE;
2444 else
2445 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2447 return entry->loaded;
2450 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2451 may be NULL, indicating that it is a wildcard. Separate
2452 lang_input_section statements are created for each part of the
2453 expansion; they are added after the wild statement S. OUTPUT is
2454 the output section. */
2456 static void
2457 wild (lang_wild_statement_type *s,
2458 const char *target ATTRIBUTE_UNUSED,
2459 lang_output_section_statement_type *output)
2461 struct wildcard_list *sec;
2463 walk_wild (s, output_section_callback, output);
2465 for (sec = s->section_list; sec != NULL; sec = sec->next)
2467 if (default_common_section != NULL)
2468 break;
2469 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2471 /* Remember the section that common is going to in case we
2472 later get something which doesn't know where to put it. */
2473 default_common_section = output;
2478 /* Return TRUE iff target is the sought target. */
2480 static int
2481 get_target (const bfd_target *target, void *data)
2483 const char *sought = data;
2485 return strcmp (target->name, sought) == 0;
2488 /* Like strcpy() but convert to lower case as well. */
2490 static void
2491 stricpy (char *dest, char *src)
2493 char c;
2495 while ((c = *src++) != 0)
2496 *dest++ = TOLOWER (c);
2498 *dest = 0;
2501 /* Remove the first occurrence of needle (if any) in haystack
2502 from haystack. */
2504 static void
2505 strcut (char *haystack, char *needle)
2507 haystack = strstr (haystack, needle);
2509 if (haystack)
2511 char *src;
2513 for (src = haystack + strlen (needle); *src;)
2514 *haystack++ = *src++;
2516 *haystack = 0;
2520 /* Compare two target format name strings.
2521 Return a value indicating how "similar" they are. */
2523 static int
2524 name_compare (char *first, char *second)
2526 char *copy1;
2527 char *copy2;
2528 int result;
2530 copy1 = xmalloc (strlen (first) + 1);
2531 copy2 = xmalloc (strlen (second) + 1);
2533 /* Convert the names to lower case. */
2534 stricpy (copy1, first);
2535 stricpy (copy2, second);
2537 /* Remove size and endian strings from the name. */
2538 strcut (copy1, "big");
2539 strcut (copy1, "little");
2540 strcut (copy2, "big");
2541 strcut (copy2, "little");
2543 /* Return a value based on how many characters match,
2544 starting from the beginning. If both strings are
2545 the same then return 10 * their length. */
2546 for (result = 0; copy1[result] == copy2[result]; result++)
2547 if (copy1[result] == 0)
2549 result *= 10;
2550 break;
2553 free (copy1);
2554 free (copy2);
2556 return result;
2559 /* Set by closest_target_match() below. */
2560 static const bfd_target *winner;
2562 /* Scan all the valid bfd targets looking for one that has the endianness
2563 requirement that was specified on the command line, and is the nearest
2564 match to the original output target. */
2566 static int
2567 closest_target_match (const bfd_target *target, void *data)
2569 const bfd_target *original = data;
2571 if (command_line.endian == ENDIAN_BIG
2572 && target->byteorder != BFD_ENDIAN_BIG)
2573 return 0;
2575 if (command_line.endian == ENDIAN_LITTLE
2576 && target->byteorder != BFD_ENDIAN_LITTLE)
2577 return 0;
2579 /* Must be the same flavour. */
2580 if (target->flavour != original->flavour)
2581 return 0;
2583 /* If we have not found a potential winner yet, then record this one. */
2584 if (winner == NULL)
2586 winner = target;
2587 return 0;
2590 /* Oh dear, we now have two potential candidates for a successful match.
2591 Compare their names and choose the better one. */
2592 if (name_compare (target->name, original->name)
2593 > name_compare (winner->name, original->name))
2594 winner = target;
2596 /* Keep on searching until wqe have checked them all. */
2597 return 0;
2600 /* Return the BFD target format of the first input file. */
2602 static char *
2603 get_first_input_target (void)
2605 char *target = NULL;
2607 LANG_FOR_EACH_INPUT_STATEMENT (s)
2609 if (s->header.type == lang_input_statement_enum
2610 && s->real)
2612 ldfile_open_file (s);
2614 if (s->the_bfd != NULL
2615 && bfd_check_format (s->the_bfd, bfd_object))
2617 target = bfd_get_target (s->the_bfd);
2619 if (target != NULL)
2620 break;
2625 return target;
2628 const char *
2629 lang_get_output_target (void)
2631 const char *target;
2633 /* Has the user told us which output format to use? */
2634 if (output_target != NULL)
2635 return output_target;
2637 /* No - has the current target been set to something other than
2638 the default? */
2639 if (current_target != default_target)
2640 return current_target;
2642 /* No - can we determine the format of the first input file? */
2643 target = get_first_input_target ();
2644 if (target != NULL)
2645 return target;
2647 /* Failed - use the default output target. */
2648 return default_target;
2651 /* Open the output file. */
2653 static bfd *
2654 open_output (const char *name)
2656 bfd *output;
2658 output_target = lang_get_output_target ();
2660 /* Has the user requested a particular endianness on the command
2661 line? */
2662 if (command_line.endian != ENDIAN_UNSET)
2664 const bfd_target *target;
2665 enum bfd_endian desired_endian;
2667 /* Get the chosen target. */
2668 target = bfd_search_for_target (get_target, (void *) output_target);
2670 /* If the target is not supported, we cannot do anything. */
2671 if (target != NULL)
2673 if (command_line.endian == ENDIAN_BIG)
2674 desired_endian = BFD_ENDIAN_BIG;
2675 else
2676 desired_endian = BFD_ENDIAN_LITTLE;
2678 /* See if the target has the wrong endianness. This should
2679 not happen if the linker script has provided big and
2680 little endian alternatives, but some scrips don't do
2681 this. */
2682 if (target->byteorder != desired_endian)
2684 /* If it does, then see if the target provides
2685 an alternative with the correct endianness. */
2686 if (target->alternative_target != NULL
2687 && (target->alternative_target->byteorder == desired_endian))
2688 output_target = target->alternative_target->name;
2689 else
2691 /* Try to find a target as similar as possible to
2692 the default target, but which has the desired
2693 endian characteristic. */
2694 bfd_search_for_target (closest_target_match,
2695 (void *) target);
2697 /* Oh dear - we could not find any targets that
2698 satisfy our requirements. */
2699 if (winner == NULL)
2700 einfo (_("%P: warning: could not find any targets"
2701 " that match endianness requirement\n"));
2702 else
2703 output_target = winner->name;
2709 output = bfd_openw (name, output_target);
2711 if (output == NULL)
2713 if (bfd_get_error () == bfd_error_invalid_target)
2714 einfo (_("%P%F: target %s not found\n"), output_target);
2716 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2719 delete_output_file_on_failure = TRUE;
2721 if (! bfd_set_format (output, bfd_object))
2722 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2723 if (! bfd_set_arch_mach (output,
2724 ldfile_output_architecture,
2725 ldfile_output_machine))
2726 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2728 link_info.hash = bfd_link_hash_table_create (output);
2729 if (link_info.hash == NULL)
2730 einfo (_("%P%F: can not create hash table: %E\n"));
2732 bfd_set_gp_size (output, g_switch_value);
2733 return output;
2736 static void
2737 ldlang_open_output (lang_statement_union_type *statement)
2739 switch (statement->header.type)
2741 case lang_output_statement_enum:
2742 ASSERT (output_bfd == NULL);
2743 output_bfd = open_output (statement->output_statement.name);
2744 ldemul_set_output_arch ();
2745 if (config.magic_demand_paged && !link_info.relocatable)
2746 output_bfd->flags |= D_PAGED;
2747 else
2748 output_bfd->flags &= ~D_PAGED;
2749 if (config.text_read_only)
2750 output_bfd->flags |= WP_TEXT;
2751 else
2752 output_bfd->flags &= ~WP_TEXT;
2753 if (link_info.traditional_format)
2754 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2755 else
2756 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2757 break;
2759 case lang_target_statement_enum:
2760 current_target = statement->target_statement.target;
2761 break;
2762 default:
2763 break;
2767 /* Convert between addresses in bytes and sizes in octets.
2768 For currently supported targets, octets_per_byte is always a power
2769 of two, so we can use shifts. */
2770 #define TO_ADDR(X) ((X) >> opb_shift)
2771 #define TO_SIZE(X) ((X) << opb_shift)
2773 /* Support the above. */
2774 static unsigned int opb_shift = 0;
2776 static void
2777 init_opb (void)
2779 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2780 ldfile_output_machine);
2781 opb_shift = 0;
2782 if (x > 1)
2783 while ((x & 1) == 0)
2785 x >>= 1;
2786 ++opb_shift;
2788 ASSERT (x == 1);
2791 /* Open all the input files. */
2793 static void
2794 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2796 for (; s != NULL; s = s->header.next)
2798 switch (s->header.type)
2800 case lang_constructors_statement_enum:
2801 open_input_bfds (constructor_list.head, force);
2802 break;
2803 case lang_output_section_statement_enum:
2804 open_input_bfds (s->output_section_statement.children.head, force);
2805 break;
2806 case lang_wild_statement_enum:
2807 /* Maybe we should load the file's symbols. */
2808 if (s->wild_statement.filename
2809 && ! wildcardp (s->wild_statement.filename))
2810 lookup_name (s->wild_statement.filename);
2811 open_input_bfds (s->wild_statement.children.head, force);
2812 break;
2813 case lang_group_statement_enum:
2815 struct bfd_link_hash_entry *undefs;
2817 /* We must continually search the entries in the group
2818 until no new symbols are added to the list of undefined
2819 symbols. */
2823 undefs = link_info.hash->undefs_tail;
2824 open_input_bfds (s->group_statement.children.head, TRUE);
2826 while (undefs != link_info.hash->undefs_tail);
2828 break;
2829 case lang_target_statement_enum:
2830 current_target = s->target_statement.target;
2831 break;
2832 case lang_input_statement_enum:
2833 if (s->input_statement.real)
2835 lang_statement_list_type add;
2837 s->input_statement.target = current_target;
2839 /* If we are being called from within a group, and this
2840 is an archive which has already been searched, then
2841 force it to be researched unless the whole archive
2842 has been loaded already. */
2843 if (force
2844 && !s->input_statement.whole_archive
2845 && s->input_statement.loaded
2846 && bfd_check_format (s->input_statement.the_bfd,
2847 bfd_archive))
2848 s->input_statement.loaded = FALSE;
2850 lang_list_init (&add);
2852 if (! load_symbols (&s->input_statement, &add))
2853 config.make_executable = FALSE;
2855 if (add.head != NULL)
2857 *add.tail = s->header.next;
2858 s->header.next = add.head;
2861 break;
2862 default:
2863 break;
2868 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2870 void
2871 lang_track_definedness (const char *name)
2873 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2874 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2877 /* New-function for the definedness hash table. */
2879 static struct bfd_hash_entry *
2880 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2881 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2882 const char *name ATTRIBUTE_UNUSED)
2884 struct lang_definedness_hash_entry *ret
2885 = (struct lang_definedness_hash_entry *) entry;
2887 if (ret == NULL)
2888 ret = (struct lang_definedness_hash_entry *)
2889 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2891 if (ret == NULL)
2892 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2894 ret->iteration = -1;
2895 return &ret->root;
2898 /* Return the iteration when the definition of NAME was last updated. A
2899 value of -1 means that the symbol is not defined in the linker script
2900 or the command line, but may be defined in the linker symbol table. */
2903 lang_symbol_definition_iteration (const char *name)
2905 struct lang_definedness_hash_entry *defentry
2906 = (struct lang_definedness_hash_entry *)
2907 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2909 /* We've already created this one on the presence of DEFINED in the
2910 script, so it can't be NULL unless something is borked elsewhere in
2911 the code. */
2912 if (defentry == NULL)
2913 FAIL ();
2915 return defentry->iteration;
2918 /* Update the definedness state of NAME. */
2920 void
2921 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2923 struct lang_definedness_hash_entry *defentry
2924 = (struct lang_definedness_hash_entry *)
2925 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2927 /* We don't keep track of symbols not tested with DEFINED. */
2928 if (defentry == NULL)
2929 return;
2931 /* If the symbol was already defined, and not from an earlier statement
2932 iteration, don't update the definedness iteration, because that'd
2933 make the symbol seem defined in the linker script at this point, and
2934 it wasn't; it was defined in some object. If we do anyway, DEFINED
2935 would start to yield false before this point and the construct "sym =
2936 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2937 in an object. */
2938 if (h->type != bfd_link_hash_undefined
2939 && h->type != bfd_link_hash_common
2940 && h->type != bfd_link_hash_new
2941 && defentry->iteration == -1)
2942 return;
2944 defentry->iteration = lang_statement_iteration;
2947 /* Add the supplied name to the symbol table as an undefined reference.
2948 This is a two step process as the symbol table doesn't even exist at
2949 the time the ld command line is processed. First we put the name
2950 on a list, then, once the output file has been opened, transfer the
2951 name to the symbol table. */
2953 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2955 #define ldlang_undef_chain_list_head entry_symbol.next
2957 void
2958 ldlang_add_undef (const char *const name)
2960 ldlang_undef_chain_list_type *new =
2961 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2963 new->next = ldlang_undef_chain_list_head;
2964 ldlang_undef_chain_list_head = new;
2966 new->name = xstrdup (name);
2968 if (output_bfd != NULL)
2969 insert_undefined (new->name);
2972 /* Insert NAME as undefined in the symbol table. */
2974 static void
2975 insert_undefined (const char *name)
2977 struct bfd_link_hash_entry *h;
2979 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2980 if (h == NULL)
2981 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2982 if (h->type == bfd_link_hash_new)
2984 h->type = bfd_link_hash_undefined;
2985 h->u.undef.abfd = NULL;
2986 bfd_link_add_undef (link_info.hash, h);
2990 /* Run through the list of undefineds created above and place them
2991 into the linker hash table as undefined symbols belonging to the
2992 script file. */
2994 static void
2995 lang_place_undefineds (void)
2997 ldlang_undef_chain_list_type *ptr;
2999 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3000 insert_undefined (ptr->name);
3003 /* Check for all readonly or some readwrite sections. */
3005 static void
3006 check_input_sections
3007 (lang_statement_union_type *s,
3008 lang_output_section_statement_type *output_section_statement)
3010 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3012 switch (s->header.type)
3014 case lang_wild_statement_enum:
3015 walk_wild (&s->wild_statement, check_section_callback,
3016 output_section_statement);
3017 if (! output_section_statement->all_input_readonly)
3018 return;
3019 break;
3020 case lang_constructors_statement_enum:
3021 check_input_sections (constructor_list.head,
3022 output_section_statement);
3023 if (! output_section_statement->all_input_readonly)
3024 return;
3025 break;
3026 case lang_group_statement_enum:
3027 check_input_sections (s->group_statement.children.head,
3028 output_section_statement);
3029 if (! output_section_statement->all_input_readonly)
3030 return;
3031 break;
3032 default:
3033 break;
3038 /* Update wildcard statements if needed. */
3040 static void
3041 update_wild_statements (lang_statement_union_type *s)
3043 struct wildcard_list *sec;
3045 switch (sort_section)
3047 default:
3048 FAIL ();
3050 case none:
3051 break;
3053 case by_name:
3054 case by_alignment:
3055 for (; s != NULL; s = s->header.next)
3057 switch (s->header.type)
3059 default:
3060 break;
3062 case lang_wild_statement_enum:
3063 sec = s->wild_statement.section_list;
3064 if (sec != NULL)
3066 switch (sec->spec.sorted)
3068 case none:
3069 sec->spec.sorted = sort_section;
3070 break;
3071 case by_name:
3072 if (sort_section == by_alignment)
3073 sec->spec.sorted = by_name_alignment;
3074 break;
3075 case by_alignment:
3076 if (sort_section == by_name)
3077 sec->spec.sorted = by_alignment_name;
3078 break;
3079 default:
3080 break;
3083 break;
3085 case lang_constructors_statement_enum:
3086 update_wild_statements (constructor_list.head);
3087 break;
3089 case lang_output_section_statement_enum:
3090 update_wild_statements
3091 (s->output_section_statement.children.head);
3092 break;
3094 case lang_group_statement_enum:
3095 update_wild_statements (s->group_statement.children.head);
3096 break;
3099 break;
3103 /* Open input files and attach to output sections. */
3105 static void
3106 map_input_to_output_sections
3107 (lang_statement_union_type *s, const char *target,
3108 lang_output_section_statement_type *os)
3110 for (; s != NULL; s = s->header.next)
3112 switch (s->header.type)
3114 case lang_wild_statement_enum:
3115 wild (&s->wild_statement, target, os);
3116 break;
3117 case lang_constructors_statement_enum:
3118 map_input_to_output_sections (constructor_list.head,
3119 target,
3120 os);
3121 break;
3122 case lang_output_section_statement_enum:
3123 if (s->output_section_statement.constraint)
3125 if (s->output_section_statement.constraint != ONLY_IF_RW
3126 && s->output_section_statement.constraint != ONLY_IF_RO)
3127 break;
3128 s->output_section_statement.all_input_readonly = TRUE;
3129 check_input_sections (s->output_section_statement.children.head,
3130 &s->output_section_statement);
3131 if ((s->output_section_statement.all_input_readonly
3132 && s->output_section_statement.constraint == ONLY_IF_RW)
3133 || (!s->output_section_statement.all_input_readonly
3134 && s->output_section_statement.constraint == ONLY_IF_RO))
3136 s->output_section_statement.constraint = -1;
3137 break;
3141 map_input_to_output_sections (s->output_section_statement.children.head,
3142 target,
3143 &s->output_section_statement);
3144 break;
3145 case lang_output_statement_enum:
3146 break;
3147 case lang_target_statement_enum:
3148 target = s->target_statement.target;
3149 break;
3150 case lang_group_statement_enum:
3151 map_input_to_output_sections (s->group_statement.children.head,
3152 target,
3153 os);
3154 break;
3155 case lang_data_statement_enum:
3156 /* Make sure that any sections mentioned in the expression
3157 are initialized. */
3158 exp_init_os (s->data_statement.exp);
3159 if (os != NULL && os->bfd_section == NULL)
3160 init_os (os, NULL);
3161 /* The output section gets contents, and then we inspect for
3162 any flags set in the input script which override any ALLOC. */
3163 os->bfd_section->flags |= SEC_HAS_CONTENTS;
3164 if (!(os->flags & SEC_NEVER_LOAD))
3165 os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
3166 break;
3167 case lang_fill_statement_enum:
3168 case lang_input_section_enum:
3169 case lang_object_symbols_statement_enum:
3170 case lang_reloc_statement_enum:
3171 case lang_padding_statement_enum:
3172 case lang_input_statement_enum:
3173 if (os != NULL && os->bfd_section == NULL)
3174 init_os (os, NULL);
3175 break;
3176 case lang_assignment_statement_enum:
3177 if (os != NULL && os->bfd_section == NULL)
3178 init_os (os, NULL);
3180 /* Make sure that any sections mentioned in the assignment
3181 are initialized. */
3182 exp_init_os (s->assignment_statement.exp);
3183 break;
3184 case lang_afile_asection_pair_statement_enum:
3185 FAIL ();
3186 break;
3187 case lang_address_statement_enum:
3188 /* Mark the specified section with the supplied address.
3190 If this section was actually a segment marker, then the
3191 directive is ignored if the linker script explicitly
3192 processed the segment marker. Originally, the linker
3193 treated segment directives (like -Ttext on the
3194 command-line) as section directives. We honor the
3195 section directive semantics for backwards compatibilty;
3196 linker scripts that do not specifically check for
3197 SEGMENT_START automatically get the old semantics. */
3198 if (!s->address_statement.segment
3199 || !s->address_statement.segment->used)
3201 lang_output_section_statement_type *aos
3202 = (lang_output_section_statement_lookup
3203 (s->address_statement.section_name));
3205 if (aos->bfd_section == NULL)
3206 init_os (aos, NULL);
3207 aos->addr_tree = s->address_statement.address;
3209 break;
3214 /* An output section might have been removed after its statement was
3215 added. For example, ldemul_before_allocation can remove dynamic
3216 sections if they turn out to be not needed. Clean them up here. */
3218 void
3219 strip_excluded_output_sections (void)
3221 lang_output_section_statement_type *os;
3223 /* Run lang_size_sections (if not already done). */
3224 if (expld.phase != lang_mark_phase_enum)
3226 expld.phase = lang_mark_phase_enum;
3227 expld.dataseg.phase = exp_dataseg_none;
3228 one_lang_size_sections_pass (NULL, FALSE);
3229 lang_reset_memory_regions ();
3232 for (os = &lang_output_section_statement.head->output_section_statement;
3233 os != NULL;
3234 os = os->next)
3236 asection *output_section;
3237 bfd_boolean exclude;
3239 if (os->constraint == -1)
3240 continue;
3242 output_section = os->bfd_section;
3243 if (output_section == NULL)
3244 continue;
3246 exclude = (output_section->rawsize == 0
3247 && (output_section->flags & SEC_KEEP) == 0
3248 && !bfd_section_removed_from_list (output_bfd,
3249 output_section));
3251 /* Some sections have not yet been sized, notably .gnu.version,
3252 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3253 input sections, so don't drop output sections that have such
3254 input sections unless they are also marked SEC_EXCLUDE. */
3255 if (exclude && output_section->map_head.s != NULL)
3257 asection *s;
3259 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3260 if ((s->flags & SEC_LINKER_CREATED) != 0
3261 && (s->flags & SEC_EXCLUDE) == 0)
3263 exclude = FALSE;
3264 break;
3268 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3269 output_section->map_head.link_order = NULL;
3270 output_section->map_tail.link_order = NULL;
3272 if (exclude)
3274 /* We don't set bfd_section to NULL since bfd_section of the
3275 removed output section statement may still be used. */
3276 os->ignored = TRUE;
3277 output_section->flags |= SEC_EXCLUDE;
3278 bfd_section_list_remove (output_bfd, output_section);
3279 output_bfd->section_count--;
3283 /* Stop future calls to lang_add_section from messing with map_head
3284 and map_tail link_order fields. */
3285 stripped_excluded_sections = TRUE;
3288 static void
3289 print_output_section_statement
3290 (lang_output_section_statement_type *output_section_statement)
3292 asection *section = output_section_statement->bfd_section;
3293 int len;
3295 if (output_section_statement != abs_output_section)
3297 minfo ("\n%s", output_section_statement->name);
3299 if (section != NULL)
3301 print_dot = section->vma;
3303 len = strlen (output_section_statement->name);
3304 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3306 print_nl ();
3307 len = 0;
3309 while (len < SECTION_NAME_MAP_LENGTH)
3311 print_space ();
3312 ++len;
3315 minfo ("0x%V %W", section->vma, section->size);
3317 if (output_section_statement->load_base != NULL)
3319 bfd_vma addr;
3321 addr = exp_get_abs_int (output_section_statement->load_base, 0,
3322 "load base");
3323 minfo (_(" load address 0x%V"), addr);
3327 print_nl ();
3330 print_statement_list (output_section_statement->children.head,
3331 output_section_statement);
3334 /* Scan for the use of the destination in the right hand side
3335 of an expression. In such cases we will not compute the
3336 correct expression, since the value of DST that is used on
3337 the right hand side will be its final value, not its value
3338 just before this expression is evaluated. */
3340 static bfd_boolean
3341 scan_for_self_assignment (const char * dst, etree_type * rhs)
3343 if (rhs == NULL || dst == NULL)
3344 return FALSE;
3346 switch (rhs->type.node_class)
3348 case etree_binary:
3349 return scan_for_self_assignment (dst, rhs->binary.lhs)
3350 || scan_for_self_assignment (dst, rhs->binary.rhs);
3352 case etree_trinary:
3353 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3354 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3356 case etree_assign:
3357 case etree_provided:
3358 case etree_provide:
3359 if (strcmp (dst, rhs->assign.dst) == 0)
3360 return TRUE;
3361 return scan_for_self_assignment (dst, rhs->assign.src);
3363 case etree_unary:
3364 return scan_for_self_assignment (dst, rhs->unary.child);
3366 case etree_value:
3367 if (rhs->value.str)
3368 return strcmp (dst, rhs->value.str) == 0;
3369 return FALSE;
3371 case etree_name:
3372 if (rhs->name.name)
3373 return strcmp (dst, rhs->name.name) == 0;
3374 return FALSE;
3376 default:
3377 break;
3380 return FALSE;
3384 static void
3385 print_assignment (lang_assignment_statement_type *assignment,
3386 lang_output_section_statement_type *output_section)
3388 unsigned int i;
3389 bfd_boolean is_dot;
3390 bfd_boolean computation_is_valid = TRUE;
3391 etree_type *tree;
3393 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3394 print_space ();
3396 if (assignment->exp->type.node_class == etree_assert)
3398 is_dot = FALSE;
3399 tree = assignment->exp->assert_s.child;
3400 computation_is_valid = TRUE;
3402 else
3404 const char *dst = assignment->exp->assign.dst;
3406 is_dot = (dst[0] == '.' && dst[1] == 0);
3407 tree = assignment->exp->assign.src;
3408 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3411 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3412 if (expld.result.valid_p)
3414 bfd_vma value;
3416 if (computation_is_valid)
3418 value = expld.result.value;
3420 if (expld.result.section)
3421 value += expld.result.section->vma;
3423 minfo ("0x%V", value);
3424 if (is_dot)
3425 print_dot = value;
3427 else
3429 struct bfd_link_hash_entry *h;
3431 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3432 FALSE, FALSE, TRUE);
3433 if (h)
3435 value = h->u.def.value;
3437 if (expld.result.section)
3438 value += expld.result.section->vma;
3440 minfo ("[0x%V]", value);
3442 else
3443 minfo ("[unresolved]");
3446 else
3448 minfo ("*undef* ");
3449 #ifdef BFD64
3450 minfo (" ");
3451 #endif
3454 minfo (" ");
3455 exp_print_tree (assignment->exp);
3456 print_nl ();
3459 static void
3460 print_input_statement (lang_input_statement_type *statm)
3462 if (statm->filename != NULL)
3464 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3468 /* Print all symbols defined in a particular section. This is called
3469 via bfd_link_hash_traverse, or by print_all_symbols. */
3471 static bfd_boolean
3472 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3474 asection *sec = ptr;
3476 if ((hash_entry->type == bfd_link_hash_defined
3477 || hash_entry->type == bfd_link_hash_defweak)
3478 && sec == hash_entry->u.def.section)
3480 int i;
3482 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3483 print_space ();
3484 minfo ("0x%V ",
3485 (hash_entry->u.def.value
3486 + hash_entry->u.def.section->output_offset
3487 + hash_entry->u.def.section->output_section->vma));
3489 minfo (" %T\n", hash_entry->root.string);
3492 return TRUE;
3495 static void
3496 print_all_symbols (sec)
3497 asection *sec;
3499 struct fat_user_section_struct *ud = get_userdata (sec);
3500 struct map_symbol_def *def;
3502 if (!ud)
3503 return;
3505 *ud->map_symbol_def_tail = 0;
3506 for (def = ud->map_symbol_def_head; def; def = def->next)
3507 print_one_symbol (def->entry, sec);
3510 /* Print information about an input section to the map file. */
3512 static void
3513 print_input_section (asection *i)
3515 bfd_size_type size = i->size;
3517 init_opb ();
3520 int len;
3521 bfd_vma addr;
3523 print_space ();
3524 minfo ("%s", i->name);
3526 len = 1 + strlen (i->name);
3527 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3529 print_nl ();
3530 len = 0;
3532 while (len < SECTION_NAME_MAP_LENGTH)
3534 print_space ();
3535 ++len;
3538 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3539 addr = i->output_section->vma + i->output_offset;
3540 else
3542 addr = print_dot;
3543 size = 0;
3546 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3548 if (size != i->rawsize && i->rawsize != 0)
3550 len = SECTION_NAME_MAP_LENGTH + 3;
3551 #ifdef BFD64
3552 len += 16;
3553 #else
3554 len += 8;
3555 #endif
3556 while (len > 0)
3558 print_space ();
3559 --len;
3562 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3565 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3567 if (command_line.reduce_memory_overheads)
3568 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3569 else
3570 print_all_symbols (i);
3572 print_dot = addr + TO_ADDR (size);
3577 static void
3578 print_fill_statement (lang_fill_statement_type *fill)
3580 size_t size;
3581 unsigned char *p;
3582 fputs (" FILL mask 0x", config.map_file);
3583 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3584 fprintf (config.map_file, "%02x", *p);
3585 fputs ("\n", config.map_file);
3588 static void
3589 print_data_statement (lang_data_statement_type *data)
3591 int i;
3592 bfd_vma addr;
3593 bfd_size_type size;
3594 const char *name;
3596 init_opb ();
3597 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3598 print_space ();
3600 addr = data->output_offset;
3601 if (data->output_section != NULL)
3602 addr += data->output_section->vma;
3604 switch (data->type)
3606 default:
3607 abort ();
3608 case BYTE:
3609 size = BYTE_SIZE;
3610 name = "BYTE";
3611 break;
3612 case SHORT:
3613 size = SHORT_SIZE;
3614 name = "SHORT";
3615 break;
3616 case LONG:
3617 size = LONG_SIZE;
3618 name = "LONG";
3619 break;
3620 case QUAD:
3621 size = QUAD_SIZE;
3622 name = "QUAD";
3623 break;
3624 case SQUAD:
3625 size = QUAD_SIZE;
3626 name = "SQUAD";
3627 break;
3630 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3632 if (data->exp->type.node_class != etree_value)
3634 print_space ();
3635 exp_print_tree (data->exp);
3638 print_nl ();
3640 print_dot = addr + TO_ADDR (size);
3643 /* Print an address statement. These are generated by options like
3644 -Ttext. */
3646 static void
3647 print_address_statement (lang_address_statement_type *address)
3649 minfo (_("Address of section %s set to "), address->section_name);
3650 exp_print_tree (address->address);
3651 print_nl ();
3654 /* Print a reloc statement. */
3656 static void
3657 print_reloc_statement (lang_reloc_statement_type *reloc)
3659 int i;
3660 bfd_vma addr;
3661 bfd_size_type size;
3663 init_opb ();
3664 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3665 print_space ();
3667 addr = reloc->output_offset;
3668 if (reloc->output_section != NULL)
3669 addr += reloc->output_section->vma;
3671 size = bfd_get_reloc_size (reloc->howto);
3673 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3675 if (reloc->name != NULL)
3676 minfo ("%s+", reloc->name);
3677 else
3678 minfo ("%s+", reloc->section->name);
3680 exp_print_tree (reloc->addend_exp);
3682 print_nl ();
3684 print_dot = addr + TO_ADDR (size);
3687 static void
3688 print_padding_statement (lang_padding_statement_type *s)
3690 int len;
3691 bfd_vma addr;
3693 init_opb ();
3694 minfo (" *fill*");
3696 len = sizeof " *fill*" - 1;
3697 while (len < SECTION_NAME_MAP_LENGTH)
3699 print_space ();
3700 ++len;
3703 addr = s->output_offset;
3704 if (s->output_section != NULL)
3705 addr += s->output_section->vma;
3706 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3708 if (s->fill->size != 0)
3710 size_t size;
3711 unsigned char *p;
3712 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3713 fprintf (config.map_file, "%02x", *p);
3716 print_nl ();
3718 print_dot = addr + TO_ADDR (s->size);
3721 static void
3722 print_wild_statement (lang_wild_statement_type *w,
3723 lang_output_section_statement_type *os)
3725 struct wildcard_list *sec;
3727 print_space ();
3729 if (w->filenames_sorted)
3730 minfo ("SORT(");
3731 if (w->filename != NULL)
3732 minfo ("%s", w->filename);
3733 else
3734 minfo ("*");
3735 if (w->filenames_sorted)
3736 minfo (")");
3738 minfo ("(");
3739 for (sec = w->section_list; sec; sec = sec->next)
3741 if (sec->spec.sorted)
3742 minfo ("SORT(");
3743 if (sec->spec.exclude_name_list != NULL)
3745 name_list *tmp;
3746 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3747 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3748 minfo (" %s", tmp->name);
3749 minfo (") ");
3751 if (sec->spec.name != NULL)
3752 minfo ("%s", sec->spec.name);
3753 else
3754 minfo ("*");
3755 if (sec->spec.sorted)
3756 minfo (")");
3757 if (sec->next)
3758 minfo (" ");
3760 minfo (")");
3762 print_nl ();
3764 print_statement_list (w->children.head, os);
3767 /* Print a group statement. */
3769 static void
3770 print_group (lang_group_statement_type *s,
3771 lang_output_section_statement_type *os)
3773 fprintf (config.map_file, "START GROUP\n");
3774 print_statement_list (s->children.head, os);
3775 fprintf (config.map_file, "END GROUP\n");
3778 /* Print the list of statements in S.
3779 This can be called for any statement type. */
3781 static void
3782 print_statement_list (lang_statement_union_type *s,
3783 lang_output_section_statement_type *os)
3785 while (s != NULL)
3787 print_statement (s, os);
3788 s = s->header.next;
3792 /* Print the first statement in statement list S.
3793 This can be called for any statement type. */
3795 static void
3796 print_statement (lang_statement_union_type *s,
3797 lang_output_section_statement_type *os)
3799 switch (s->header.type)
3801 default:
3802 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3803 FAIL ();
3804 break;
3805 case lang_constructors_statement_enum:
3806 if (constructor_list.head != NULL)
3808 if (constructors_sorted)
3809 minfo (" SORT (CONSTRUCTORS)\n");
3810 else
3811 minfo (" CONSTRUCTORS\n");
3812 print_statement_list (constructor_list.head, os);
3814 break;
3815 case lang_wild_statement_enum:
3816 print_wild_statement (&s->wild_statement, os);
3817 break;
3818 case lang_address_statement_enum:
3819 print_address_statement (&s->address_statement);
3820 break;
3821 case lang_object_symbols_statement_enum:
3822 minfo (" CREATE_OBJECT_SYMBOLS\n");
3823 break;
3824 case lang_fill_statement_enum:
3825 print_fill_statement (&s->fill_statement);
3826 break;
3827 case lang_data_statement_enum:
3828 print_data_statement (&s->data_statement);
3829 break;
3830 case lang_reloc_statement_enum:
3831 print_reloc_statement (&s->reloc_statement);
3832 break;
3833 case lang_input_section_enum:
3834 print_input_section (s->input_section.section);
3835 break;
3836 case lang_padding_statement_enum:
3837 print_padding_statement (&s->padding_statement);
3838 break;
3839 case lang_output_section_statement_enum:
3840 print_output_section_statement (&s->output_section_statement);
3841 break;
3842 case lang_assignment_statement_enum:
3843 print_assignment (&s->assignment_statement, os);
3844 break;
3845 case lang_target_statement_enum:
3846 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3847 break;
3848 case lang_output_statement_enum:
3849 minfo ("OUTPUT(%s", s->output_statement.name);
3850 if (output_target != NULL)
3851 minfo (" %s", output_target);
3852 minfo (")\n");
3853 break;
3854 case lang_input_statement_enum:
3855 print_input_statement (&s->input_statement);
3856 break;
3857 case lang_group_statement_enum:
3858 print_group (&s->group_statement, os);
3859 break;
3860 case lang_afile_asection_pair_statement_enum:
3861 FAIL ();
3862 break;
3866 static void
3867 print_statements (void)
3869 print_statement_list (statement_list.head, abs_output_section);
3872 /* Print the first N statements in statement list S to STDERR.
3873 If N == 0, nothing is printed.
3874 If N < 0, the entire list is printed.
3875 Intended to be called from GDB. */
3877 void
3878 dprint_statement (lang_statement_union_type *s, int n)
3880 FILE *map_save = config.map_file;
3882 config.map_file = stderr;
3884 if (n < 0)
3885 print_statement_list (s, abs_output_section);
3886 else
3888 while (s && --n >= 0)
3890 print_statement (s, abs_output_section);
3891 s = s->header.next;
3895 config.map_file = map_save;
3898 static void
3899 insert_pad (lang_statement_union_type **ptr,
3900 fill_type *fill,
3901 unsigned int alignment_needed,
3902 asection *output_section,
3903 bfd_vma dot)
3905 static fill_type zero_fill = { 1, { 0 } };
3906 lang_statement_union_type *pad = NULL;
3908 if (ptr != &statement_list.head)
3909 pad = ((lang_statement_union_type *)
3910 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
3911 if (pad != NULL
3912 && pad->header.type == lang_padding_statement_enum
3913 && pad->padding_statement.output_section == output_section)
3915 /* Use the existing pad statement. */
3917 else if ((pad = *ptr) != NULL
3918 && pad->header.type == lang_padding_statement_enum
3919 && pad->padding_statement.output_section == output_section)
3921 /* Use the existing pad statement. */
3923 else
3925 /* Make a new padding statement, linked into existing chain. */
3926 pad = stat_alloc (sizeof (lang_padding_statement_type));
3927 pad->header.next = *ptr;
3928 *ptr = pad;
3929 pad->header.type = lang_padding_statement_enum;
3930 pad->padding_statement.output_section = output_section;
3931 if (fill == NULL)
3932 fill = &zero_fill;
3933 pad->padding_statement.fill = fill;
3935 pad->padding_statement.output_offset = dot - output_section->vma;
3936 pad->padding_statement.size = alignment_needed;
3937 output_section->size += alignment_needed;
3940 /* Work out how much this section will move the dot point. */
3942 static bfd_vma
3943 size_input_section
3944 (lang_statement_union_type **this_ptr,
3945 lang_output_section_statement_type *output_section_statement,
3946 fill_type *fill,
3947 bfd_vma dot)
3949 lang_input_section_type *is = &((*this_ptr)->input_section);
3950 asection *i = is->section;
3952 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
3953 && (i->flags & SEC_EXCLUDE) == 0)
3955 unsigned int alignment_needed;
3956 asection *o;
3958 /* Align this section first to the input sections requirement,
3959 then to the output section's requirement. If this alignment
3960 is greater than any seen before, then record it too. Perform
3961 the alignment by inserting a magic 'padding' statement. */
3963 if (output_section_statement->subsection_alignment != -1)
3964 i->alignment_power = output_section_statement->subsection_alignment;
3966 o = output_section_statement->bfd_section;
3967 if (o->alignment_power < i->alignment_power)
3968 o->alignment_power = i->alignment_power;
3970 alignment_needed = align_power (dot, i->alignment_power) - dot;
3972 if (alignment_needed != 0)
3974 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
3975 dot += alignment_needed;
3978 /* Remember where in the output section this input section goes. */
3980 i->output_offset = dot - o->vma;
3982 /* Mark how big the output section must be to contain this now. */
3983 dot += TO_ADDR (i->size);
3984 o->size = TO_SIZE (dot - o->vma);
3986 else
3988 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
3991 return dot;
3994 static int
3995 sort_sections_by_lma (const void *arg1, const void *arg2)
3997 const asection *sec1 = *(const asection **) arg1;
3998 const asection *sec2 = *(const asection **) arg2;
4000 if (bfd_section_lma (sec1->owner, sec1)
4001 < bfd_section_lma (sec2->owner, sec2))
4002 return -1;
4003 else if (bfd_section_lma (sec1->owner, sec1)
4004 > bfd_section_lma (sec2->owner, sec2))
4005 return 1;
4007 return 0;
4010 #define IGNORE_SECTION(s) \
4011 ((s->flags & SEC_NEVER_LOAD) != 0 \
4012 || (s->flags & SEC_ALLOC) == 0 \
4013 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4014 && (s->flags & SEC_LOAD) == 0))
4016 /* Check to see if any allocated sections overlap with other allocated
4017 sections. This can happen if a linker script specifies the output
4018 section addresses of the two sections. */
4020 static void
4021 lang_check_section_addresses (void)
4023 asection *s, *os;
4024 asection **sections, **spp;
4025 unsigned int count;
4026 bfd_vma s_start;
4027 bfd_vma s_end;
4028 bfd_vma os_start;
4029 bfd_vma os_end;
4030 bfd_size_type amt;
4032 if (bfd_count_sections (output_bfd) <= 1)
4033 return;
4035 amt = bfd_count_sections (output_bfd) * sizeof (asection *);
4036 sections = xmalloc (amt);
4038 /* Scan all sections in the output list. */
4039 count = 0;
4040 for (s = output_bfd->sections; s != NULL; s = s->next)
4042 /* Only consider loadable sections with real contents. */
4043 if (IGNORE_SECTION (s) || s->size == 0)
4044 continue;
4046 sections[count] = s;
4047 count++;
4050 if (count <= 1)
4051 return;
4053 qsort (sections, (size_t) count, sizeof (asection *),
4054 sort_sections_by_lma);
4056 spp = sections;
4057 s = *spp++;
4058 s_start = bfd_section_lma (output_bfd, s);
4059 s_end = s_start + TO_ADDR (s->size) - 1;
4060 for (count--; count; count--)
4062 /* We must check the sections' LMA addresses not their VMA
4063 addresses because overlay sections can have overlapping VMAs
4064 but they must have distinct LMAs. */
4065 os = s;
4066 os_start = s_start;
4067 os_end = s_end;
4068 s = *spp++;
4069 s_start = bfd_section_lma (output_bfd, s);
4070 s_end = s_start + TO_ADDR (s->size) - 1;
4072 /* Look for an overlap. */
4073 if (s_end >= os_start && s_start <= os_end)
4074 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4075 s->name, s_start, s_end, os->name, os_start, os_end);
4078 free (sections);
4081 /* Make sure the new address is within the region. We explicitly permit the
4082 current address to be at the exact end of the region when the address is
4083 non-zero, in case the region is at the end of addressable memory and the
4084 calculation wraps around. */
4086 static void
4087 os_region_check (lang_output_section_statement_type *os,
4088 lang_memory_region_type *region,
4089 etree_type *tree,
4090 bfd_vma base)
4092 if ((region->current < region->origin
4093 || (region->current - region->origin > region->length))
4094 && ((region->current != region->origin + region->length)
4095 || base == 0))
4097 if (tree != NULL)
4099 einfo (_("%X%P: address 0x%v of %B section %s"
4100 " is not within region %s\n"),
4101 region->current,
4102 os->bfd_section->owner,
4103 os->bfd_section->name,
4104 region->name);
4106 else
4108 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4109 region->name,
4110 os->bfd_section->owner,
4111 os->bfd_section->name);
4113 /* Reset the region pointer. */
4114 region->current = region->origin;
4118 /* Set the sizes for all the output sections. */
4120 static bfd_vma
4121 lang_size_sections_1
4122 (lang_statement_union_type *s,
4123 lang_output_section_statement_type *output_section_statement,
4124 lang_statement_union_type **prev,
4125 fill_type *fill,
4126 bfd_vma dot,
4127 bfd_boolean *relax,
4128 bfd_boolean check_regions)
4130 /* Size up the sections from their constituent parts. */
4131 for (; s != NULL; s = s->header.next)
4133 switch (s->header.type)
4135 case lang_output_section_statement_enum:
4137 bfd_vma newdot, after;
4138 lang_output_section_statement_type *os;
4140 os = &s->output_section_statement;
4141 if (os->addr_tree != NULL)
4143 os->processed = FALSE;
4144 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4146 if (!expld.result.valid_p
4147 && expld.phase != lang_mark_phase_enum)
4148 einfo (_("%F%S: non constant or forward reference"
4149 " address expression for section %s\n"),
4150 os->name);
4152 dot = expld.result.value + expld.result.section->vma;
4155 if (os->bfd_section == NULL)
4156 /* This section was removed or never actually created. */
4157 break;
4159 /* If this is a COFF shared library section, use the size and
4160 address from the input section. FIXME: This is COFF
4161 specific; it would be cleaner if there were some other way
4162 to do this, but nothing simple comes to mind. */
4163 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
4164 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
4165 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4167 asection *input;
4169 if (os->children.head == NULL
4170 || os->children.head->header.next != NULL
4171 || (os->children.head->header.type
4172 != lang_input_section_enum))
4173 einfo (_("%P%X: Internal error on COFF shared library"
4174 " section %s\n"), os->name);
4176 input = os->children.head->input_section.section;
4177 bfd_set_section_vma (os->bfd_section->owner,
4178 os->bfd_section,
4179 bfd_section_vma (input->owner, input));
4180 os->bfd_section->size = input->size;
4181 break;
4184 newdot = dot;
4185 if (bfd_is_abs_section (os->bfd_section))
4187 /* No matter what happens, an abs section starts at zero. */
4188 ASSERT (os->bfd_section->vma == 0);
4190 else
4192 int align;
4194 if (os->addr_tree == NULL)
4196 /* No address specified for this section, get one
4197 from the region specification. */
4198 if (os->region == NULL
4199 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4200 && os->region->name[0] == '*'
4201 && strcmp (os->region->name,
4202 DEFAULT_MEMORY_REGION) == 0))
4204 os->region = lang_memory_default (os->bfd_section);
4207 /* If a loadable section is using the default memory
4208 region, and some non default memory regions were
4209 defined, issue an error message. */
4210 if (!IGNORE_SECTION (os->bfd_section)
4211 && ! link_info.relocatable
4212 && check_regions
4213 && strcmp (os->region->name,
4214 DEFAULT_MEMORY_REGION) == 0
4215 && lang_memory_region_list != NULL
4216 && (strcmp (lang_memory_region_list->name,
4217 DEFAULT_MEMORY_REGION) != 0
4218 || lang_memory_region_list->next != NULL)
4219 && expld.phase != lang_mark_phase_enum)
4221 /* By default this is an error rather than just a
4222 warning because if we allocate the section to the
4223 default memory region we can end up creating an
4224 excessively large binary, or even seg faulting when
4225 attempting to perform a negative seek. See
4226 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4227 for an example of this. This behaviour can be
4228 overridden by the using the --no-check-sections
4229 switch. */
4230 if (command_line.check_section_addresses)
4231 einfo (_("%P%F: error: no memory region specified"
4232 " for loadable section `%s'\n"),
4233 bfd_get_section_name (output_bfd,
4234 os->bfd_section));
4235 else
4236 einfo (_("%P: warning: no memory region specified"
4237 " for loadable section `%s'\n"),
4238 bfd_get_section_name (output_bfd,
4239 os->bfd_section));
4242 newdot = os->region->current;
4243 align = os->bfd_section->alignment_power;
4245 else
4246 align = os->section_alignment;
4248 /* Align to what the section needs. */
4249 if (align > 0)
4251 bfd_vma savedot = newdot;
4252 newdot = align_power (newdot, align);
4254 if (newdot != savedot
4255 && (config.warn_section_align
4256 || os->addr_tree != NULL)
4257 && expld.phase != lang_mark_phase_enum)
4258 einfo (_("%P: warning: changing start of section"
4259 " %s by %lu bytes\n"),
4260 os->name, (unsigned long) (newdot - savedot));
4263 bfd_set_section_vma (0, os->bfd_section, newdot);
4265 os->bfd_section->output_offset = 0;
4268 lang_size_sections_1 (os->children.head, os, &os->children.head,
4269 os->fill, newdot, relax, check_regions);
4271 os->processed = TRUE;
4273 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4275 ASSERT (os->bfd_section->size == 0);
4276 break;
4279 dot = os->bfd_section->vma;
4281 /* Put the section within the requested block size, or
4282 align at the block boundary. */
4283 after = ((dot
4284 + TO_ADDR (os->bfd_section->size)
4285 + os->block_value - 1)
4286 & - (bfd_vma) os->block_value);
4288 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4290 /* .tbss sections effectively have zero size. */
4291 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4292 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4293 || link_info.relocatable)
4294 dot += TO_ADDR (os->bfd_section->size);
4296 if (os->update_dot_tree != 0)
4297 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4299 /* Update dot in the region ?
4300 We only do this if the section is going to be allocated,
4301 since unallocated sections do not contribute to the region's
4302 overall size in memory.
4304 If the SEC_NEVER_LOAD bit is not set, it will affect the
4305 addresses of sections after it. We have to update
4306 dot. */
4307 if (os->region != NULL
4308 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4309 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4311 os->region->current = dot;
4313 if (check_regions)
4314 /* Make sure the new address is within the region. */
4315 os_region_check (os, os->region, os->addr_tree,
4316 os->bfd_section->vma);
4318 /* If there's no load address specified, use the run
4319 region as the load region. */
4320 if (os->lma_region == NULL && os->load_base == NULL)
4321 os->lma_region = os->region;
4323 if (os->lma_region != NULL && os->lma_region != os->region)
4325 /* Set load_base, which will be handled later. */
4326 os->load_base = exp_intop (os->lma_region->current);
4327 os->lma_region->current +=
4328 TO_ADDR (os->bfd_section->size);
4329 if (check_regions)
4330 os_region_check (os, os->lma_region, NULL,
4331 os->bfd_section->lma);
4335 break;
4337 case lang_constructors_statement_enum:
4338 dot = lang_size_sections_1 (constructor_list.head,
4339 output_section_statement,
4340 &s->wild_statement.children.head,
4341 fill, dot, relax, check_regions);
4342 break;
4344 case lang_data_statement_enum:
4346 unsigned int size = 0;
4348 s->data_statement.output_offset =
4349 dot - output_section_statement->bfd_section->vma;
4350 s->data_statement.output_section =
4351 output_section_statement->bfd_section;
4353 /* We might refer to provided symbols in the expression, and
4354 need to mark them as needed. */
4355 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4357 switch (s->data_statement.type)
4359 default:
4360 abort ();
4361 case QUAD:
4362 case SQUAD:
4363 size = QUAD_SIZE;
4364 break;
4365 case LONG:
4366 size = LONG_SIZE;
4367 break;
4368 case SHORT:
4369 size = SHORT_SIZE;
4370 break;
4371 case BYTE:
4372 size = BYTE_SIZE;
4373 break;
4375 if (size < TO_SIZE ((unsigned) 1))
4376 size = TO_SIZE ((unsigned) 1);
4377 dot += TO_ADDR (size);
4378 output_section_statement->bfd_section->size += size;
4380 break;
4382 case lang_reloc_statement_enum:
4384 int size;
4386 s->reloc_statement.output_offset =
4387 dot - output_section_statement->bfd_section->vma;
4388 s->reloc_statement.output_section =
4389 output_section_statement->bfd_section;
4390 size = bfd_get_reloc_size (s->reloc_statement.howto);
4391 dot += TO_ADDR (size);
4392 output_section_statement->bfd_section->size += size;
4394 break;
4396 case lang_wild_statement_enum:
4397 dot = lang_size_sections_1 (s->wild_statement.children.head,
4398 output_section_statement,
4399 &s->wild_statement.children.head,
4400 fill, dot, relax, check_regions);
4401 break;
4403 case lang_object_symbols_statement_enum:
4404 link_info.create_object_symbols_section =
4405 output_section_statement->bfd_section;
4406 break;
4408 case lang_output_statement_enum:
4409 case lang_target_statement_enum:
4410 break;
4412 case lang_input_section_enum:
4414 asection *i;
4416 i = (*prev)->input_section.section;
4417 if (relax)
4419 bfd_boolean again;
4421 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4422 einfo (_("%P%F: can't relax section: %E\n"));
4423 if (again)
4424 *relax = TRUE;
4426 dot = size_input_section (prev, output_section_statement,
4427 output_section_statement->fill, dot);
4429 break;
4431 case lang_input_statement_enum:
4432 break;
4434 case lang_fill_statement_enum:
4435 s->fill_statement.output_section =
4436 output_section_statement->bfd_section;
4438 fill = s->fill_statement.fill;
4439 break;
4441 case lang_assignment_statement_enum:
4443 bfd_vma newdot = dot;
4445 exp_fold_tree (s->assignment_statement.exp,
4446 output_section_statement->bfd_section,
4447 &newdot);
4449 if (newdot != dot && !output_section_statement->ignored)
4451 if (output_section_statement == abs_output_section)
4453 /* If we don't have an output section, then just adjust
4454 the default memory address. */
4455 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4456 FALSE)->current = newdot;
4458 else
4460 /* Insert a pad after this statement. We can't
4461 put the pad before when relaxing, in case the
4462 assignment references dot. */
4463 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4464 output_section_statement->bfd_section, dot);
4466 /* Don't neuter the pad below when relaxing. */
4467 s = s->header.next;
4469 /* If dot is advanced, this implies that the section
4470 should have space allocated to it, unless the
4471 user has explicitly stated that the section
4472 should never be loaded. */
4473 if (!(output_section_statement->flags
4474 & (SEC_NEVER_LOAD | SEC_ALLOC)))
4475 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4477 dot = newdot;
4480 break;
4482 case lang_padding_statement_enum:
4483 /* If this is the first time lang_size_sections is called,
4484 we won't have any padding statements. If this is the
4485 second or later passes when relaxing, we should allow
4486 padding to shrink. If padding is needed on this pass, it
4487 will be added back in. */
4488 s->padding_statement.size = 0;
4490 /* Make sure output_offset is valid. If relaxation shrinks
4491 the section and this pad isn't needed, it's possible to
4492 have output_offset larger than the final size of the
4493 section. bfd_set_section_contents will complain even for
4494 a pad size of zero. */
4495 s->padding_statement.output_offset
4496 = dot - output_section_statement->bfd_section->vma;
4497 break;
4499 case lang_group_statement_enum:
4500 dot = lang_size_sections_1 (s->group_statement.children.head,
4501 output_section_statement,
4502 &s->group_statement.children.head,
4503 fill, dot, relax, check_regions);
4504 break;
4506 default:
4507 FAIL ();
4508 break;
4510 /* We can only get here when relaxing is turned on. */
4511 case lang_address_statement_enum:
4512 break;
4514 prev = &s->header.next;
4516 return dot;
4519 void
4520 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
4522 lang_statement_iteration++;
4523 lang_size_sections_1 (statement_list.head, abs_output_section,
4524 &statement_list.head, 0, 0, relax, check_regions);
4527 void
4528 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4530 expld.phase = lang_allocating_phase_enum;
4531 expld.dataseg.phase = exp_dataseg_none;
4533 one_lang_size_sections_pass (relax, check_regions);
4534 if (expld.dataseg.phase == exp_dataseg_end_seen
4535 && link_info.relro && expld.dataseg.relro_end)
4537 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4538 to put expld.dataseg.relro on a (common) page boundary. */
4539 bfd_vma old_min_base, relro_end, maxpage;
4541 expld.dataseg.phase = exp_dataseg_relro_adjust;
4542 old_min_base = expld.dataseg.min_base;
4543 maxpage = expld.dataseg.maxpagesize;
4544 expld.dataseg.base += (-expld.dataseg.relro_end
4545 & (expld.dataseg.pagesize - 1));
4546 /* Compute the expected PT_GNU_RELRO segment end. */
4547 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
4548 & ~(expld.dataseg.pagesize - 1);
4549 if (old_min_base + maxpage < expld.dataseg.base)
4551 expld.dataseg.base -= maxpage;
4552 relro_end -= maxpage;
4554 one_lang_size_sections_pass (relax, check_regions);
4555 if (expld.dataseg.relro_end > relro_end)
4557 /* The alignment of sections between DATA_SEGMENT_ALIGN
4558 and DATA_SEGMENT_RELRO_END caused huge padding to be
4559 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4560 asection *sec;
4561 unsigned int max_alignment_power = 0;
4563 /* Find maximum alignment power of sections between
4564 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4565 for (sec = output_bfd->sections; sec; sec = sec->next)
4566 if (sec->vma >= expld.dataseg.base
4567 && sec->vma < expld.dataseg.relro_end
4568 && sec->alignment_power > max_alignment_power)
4569 max_alignment_power = sec->alignment_power;
4571 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
4573 if (expld.dataseg.base - (1 << max_alignment_power)
4574 < old_min_base)
4575 expld.dataseg.base += expld.dataseg.pagesize;
4576 expld.dataseg.base -= (1 << max_alignment_power);
4577 one_lang_size_sections_pass (relax, check_regions);
4580 link_info.relro_start = expld.dataseg.base;
4581 link_info.relro_end = expld.dataseg.relro_end;
4583 else if (expld.dataseg.phase == exp_dataseg_end_seen)
4585 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4586 a page could be saved in the data segment. */
4587 bfd_vma first, last;
4589 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
4590 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
4591 if (first && last
4592 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
4593 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
4594 && first + last <= expld.dataseg.pagesize)
4596 expld.dataseg.phase = exp_dataseg_adjust;
4597 one_lang_size_sections_pass (relax, check_regions);
4601 expld.phase = lang_final_phase_enum;
4604 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4606 static bfd_vma
4607 lang_do_assignments_1
4608 (lang_statement_union_type *s,
4609 lang_output_section_statement_type *output_section_statement,
4610 fill_type *fill,
4611 bfd_vma dot)
4613 for (; s != NULL; s = s->header.next)
4615 switch (s->header.type)
4617 case lang_constructors_statement_enum:
4618 dot = lang_do_assignments_1 (constructor_list.head,
4619 output_section_statement,
4620 fill,
4621 dot);
4622 break;
4624 case lang_output_section_statement_enum:
4626 lang_output_section_statement_type *os;
4628 os = &(s->output_section_statement);
4629 if (os->bfd_section != NULL && !os->ignored)
4631 dot = os->bfd_section->vma;
4632 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
4633 /* .tbss sections effectively have zero size. */
4634 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4635 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4636 || link_info.relocatable)
4637 dot += TO_ADDR (os->bfd_section->size);
4639 if (os->load_base)
4641 /* If nothing has been placed into the output section then
4642 it won't have a bfd_section. */
4643 if (os->bfd_section && !os->ignored)
4645 os->bfd_section->lma
4646 = exp_get_abs_int (os->load_base, 0, "load base");
4650 break;
4652 case lang_wild_statement_enum:
4654 dot = lang_do_assignments_1 (s->wild_statement.children.head,
4655 output_section_statement,
4656 fill, dot);
4657 break;
4659 case lang_object_symbols_statement_enum:
4660 case lang_output_statement_enum:
4661 case lang_target_statement_enum:
4662 break;
4664 case lang_data_statement_enum:
4665 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4666 if (expld.result.valid_p)
4667 s->data_statement.value = (expld.result.value
4668 + expld.result.section->vma);
4669 else
4670 einfo (_("%F%P: invalid data statement\n"));
4672 unsigned int size;
4673 switch (s->data_statement.type)
4675 default:
4676 abort ();
4677 case QUAD:
4678 case SQUAD:
4679 size = QUAD_SIZE;
4680 break;
4681 case LONG:
4682 size = LONG_SIZE;
4683 break;
4684 case SHORT:
4685 size = SHORT_SIZE;
4686 break;
4687 case BYTE:
4688 size = BYTE_SIZE;
4689 break;
4691 if (size < TO_SIZE ((unsigned) 1))
4692 size = TO_SIZE ((unsigned) 1);
4693 dot += TO_ADDR (size);
4695 break;
4697 case lang_reloc_statement_enum:
4698 exp_fold_tree (s->reloc_statement.addend_exp,
4699 bfd_abs_section_ptr, &dot);
4700 if (expld.result.valid_p)
4701 s->reloc_statement.addend_value = expld.result.value;
4702 else
4703 einfo (_("%F%P: invalid reloc statement\n"));
4704 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4705 break;
4707 case lang_input_section_enum:
4709 asection *in = s->input_section.section;
4711 if ((in->flags & SEC_EXCLUDE) == 0)
4712 dot += TO_ADDR (in->size);
4714 break;
4716 case lang_input_statement_enum:
4717 break;
4719 case lang_fill_statement_enum:
4720 fill = s->fill_statement.fill;
4721 break;
4723 case lang_assignment_statement_enum:
4724 exp_fold_tree (s->assignment_statement.exp,
4725 output_section_statement->bfd_section,
4726 &dot);
4727 break;
4729 case lang_padding_statement_enum:
4730 dot += TO_ADDR (s->padding_statement.size);
4731 break;
4733 case lang_group_statement_enum:
4734 dot = lang_do_assignments_1 (s->group_statement.children.head,
4735 output_section_statement,
4736 fill, dot);
4737 break;
4739 default:
4740 FAIL ();
4741 break;
4743 case lang_address_statement_enum:
4744 break;
4747 return dot;
4750 void
4751 lang_do_assignments (void)
4753 lang_statement_iteration++;
4754 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
4757 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4758 operator .startof. (section_name), it produces an undefined symbol
4759 .startof.section_name. Similarly, when it sees
4760 .sizeof. (section_name), it produces an undefined symbol
4761 .sizeof.section_name. For all the output sections, we look for
4762 such symbols, and set them to the correct value. */
4764 static void
4765 lang_set_startof (void)
4767 asection *s;
4769 if (link_info.relocatable)
4770 return;
4772 for (s = output_bfd->sections; s != NULL; s = s->next)
4774 const char *secname;
4775 char *buf;
4776 struct bfd_link_hash_entry *h;
4778 secname = bfd_get_section_name (output_bfd, s);
4779 buf = xmalloc (10 + strlen (secname));
4781 sprintf (buf, ".startof.%s", secname);
4782 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4783 if (h != NULL && h->type == bfd_link_hash_undefined)
4785 h->type = bfd_link_hash_defined;
4786 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4787 h->u.def.section = bfd_abs_section_ptr;
4790 sprintf (buf, ".sizeof.%s", secname);
4791 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4792 if (h != NULL && h->type == bfd_link_hash_undefined)
4794 h->type = bfd_link_hash_defined;
4795 h->u.def.value = TO_ADDR (s->size);
4796 h->u.def.section = bfd_abs_section_ptr;
4799 free (buf);
4803 static void
4804 lang_end (void)
4806 struct bfd_link_hash_entry *h;
4807 bfd_boolean warn;
4809 if (link_info.relocatable || link_info.shared)
4810 warn = FALSE;
4811 else
4812 warn = TRUE;
4814 if (entry_symbol.name == NULL)
4816 /* No entry has been specified. Look for the default entry, but
4817 don't warn if we don't find it. */
4818 entry_symbol.name = entry_symbol_default;
4819 warn = FALSE;
4822 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4823 FALSE, FALSE, TRUE);
4824 if (h != NULL
4825 && (h->type == bfd_link_hash_defined
4826 || h->type == bfd_link_hash_defweak)
4827 && h->u.def.section->output_section != NULL)
4829 bfd_vma val;
4831 val = (h->u.def.value
4832 + bfd_get_section_vma (output_bfd,
4833 h->u.def.section->output_section)
4834 + h->u.def.section->output_offset);
4835 if (! bfd_set_start_address (output_bfd, val))
4836 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4838 else
4840 bfd_vma val;
4841 const char *send;
4843 /* We couldn't find the entry symbol. Try parsing it as a
4844 number. */
4845 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4846 if (*send == '\0')
4848 if (! bfd_set_start_address (output_bfd, val))
4849 einfo (_("%P%F: can't set start address\n"));
4851 else
4853 asection *ts;
4855 /* Can't find the entry symbol, and it's not a number. Use
4856 the first address in the text section. */
4857 ts = bfd_get_section_by_name (output_bfd, entry_section);
4858 if (ts != NULL)
4860 if (warn)
4861 einfo (_("%P: warning: cannot find entry symbol %s;"
4862 " defaulting to %V\n"),
4863 entry_symbol.name,
4864 bfd_get_section_vma (output_bfd, ts));
4865 if (! bfd_set_start_address (output_bfd,
4866 bfd_get_section_vma (output_bfd,
4867 ts)))
4868 einfo (_("%P%F: can't set start address\n"));
4870 else
4872 if (warn)
4873 einfo (_("%P: warning: cannot find entry symbol %s;"
4874 " not setting start address\n"),
4875 entry_symbol.name);
4880 /* Don't bfd_hash_table_free (&lang_definedness_table);
4881 map file output may result in a call of lang_track_definedness. */
4884 /* This is a small function used when we want to ignore errors from
4885 BFD. */
4887 static void
4888 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
4890 /* Don't do anything. */
4893 /* Check that the architecture of all the input files is compatible
4894 with the output file. Also call the backend to let it do any
4895 other checking that is needed. */
4897 static void
4898 lang_check (void)
4900 lang_statement_union_type *file;
4901 bfd *input_bfd;
4902 const bfd_arch_info_type *compatible;
4904 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
4906 input_bfd = file->input_statement.the_bfd;
4907 compatible
4908 = bfd_arch_get_compatible (input_bfd, output_bfd,
4909 command_line.accept_unknown_input_arch);
4911 /* In general it is not possible to perform a relocatable
4912 link between differing object formats when the input
4913 file has relocations, because the relocations in the
4914 input format may not have equivalent representations in
4915 the output format (and besides BFD does not translate
4916 relocs for other link purposes than a final link). */
4917 if ((link_info.relocatable || link_info.emitrelocations)
4918 && (compatible == NULL
4919 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
4920 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
4922 einfo (_("%P%F: Relocatable linking with relocations from"
4923 " format %s (%B) to format %s (%B) is not supported\n"),
4924 bfd_get_target (input_bfd), input_bfd,
4925 bfd_get_target (output_bfd), output_bfd);
4926 /* einfo with %F exits. */
4929 if (compatible == NULL)
4931 if (command_line.warn_mismatch)
4932 einfo (_("%P: warning: %s architecture of input file `%B'"
4933 " is incompatible with %s output\n"),
4934 bfd_printable_name (input_bfd), input_bfd,
4935 bfd_printable_name (output_bfd));
4937 else if (bfd_count_sections (input_bfd))
4939 /* If the input bfd has no contents, it shouldn't set the
4940 private data of the output bfd. */
4942 bfd_error_handler_type pfn = NULL;
4944 /* If we aren't supposed to warn about mismatched input
4945 files, temporarily set the BFD error handler to a
4946 function which will do nothing. We still want to call
4947 bfd_merge_private_bfd_data, since it may set up
4948 information which is needed in the output file. */
4949 if (! command_line.warn_mismatch)
4950 pfn = bfd_set_error_handler (ignore_bfd_errors);
4951 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
4953 if (command_line.warn_mismatch)
4954 einfo (_("%P%X: failed to merge target specific data"
4955 " of file %B\n"), input_bfd);
4957 if (! command_line.warn_mismatch)
4958 bfd_set_error_handler (pfn);
4963 /* Look through all the global common symbols and attach them to the
4964 correct section. The -sort-common command line switch may be used
4965 to roughly sort the entries by size. */
4967 static void
4968 lang_common (void)
4970 if (command_line.inhibit_common_definition)
4971 return;
4972 if (link_info.relocatable
4973 && ! command_line.force_common_definition)
4974 return;
4976 if (! config.sort_common)
4977 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
4978 else
4980 int power;
4982 for (power = 4; power >= 0; power--)
4983 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
4987 /* Place one common symbol in the correct section. */
4989 static bfd_boolean
4990 lang_one_common (struct bfd_link_hash_entry *h, void *info)
4992 unsigned int power_of_two;
4993 bfd_vma size;
4994 asection *section;
4996 if (h->type != bfd_link_hash_common)
4997 return TRUE;
4999 size = h->u.c.size;
5000 power_of_two = h->u.c.p->alignment_power;
5002 if (config.sort_common
5003 && power_of_two < (unsigned int) *(int *) info)
5004 return TRUE;
5006 section = h->u.c.p->section;
5008 /* Increase the size of the section to align the common sym. */
5009 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5010 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
5012 /* Adjust the alignment if necessary. */
5013 if (power_of_two > section->alignment_power)
5014 section->alignment_power = power_of_two;
5016 /* Change the symbol from common to defined. */
5017 h->type = bfd_link_hash_defined;
5018 h->u.def.section = section;
5019 h->u.def.value = section->size;
5021 /* Increase the size of the section. */
5022 section->size += size;
5024 /* Make sure the section is allocated in memory, and make sure that
5025 it is no longer a common section. */
5026 section->flags |= SEC_ALLOC;
5027 section->flags &= ~SEC_IS_COMMON;
5029 if (config.map_file != NULL)
5031 static bfd_boolean header_printed;
5032 int len;
5033 char *name;
5034 char buf[50];
5036 if (! header_printed)
5038 minfo (_("\nAllocating common symbols\n"));
5039 minfo (_("Common symbol size file\n\n"));
5040 header_printed = TRUE;
5043 name = demangle (h->root.string);
5044 minfo ("%s", name);
5045 len = strlen (name);
5046 free (name);
5048 if (len >= 19)
5050 print_nl ();
5051 len = 0;
5053 while (len < 20)
5055 print_space ();
5056 ++len;
5059 minfo ("0x");
5060 if (size <= 0xffffffff)
5061 sprintf (buf, "%lx", (unsigned long) size);
5062 else
5063 sprintf_vma (buf, size);
5064 minfo ("%s", buf);
5065 len = strlen (buf);
5067 while (len < 16)
5069 print_space ();
5070 ++len;
5073 minfo ("%B\n", section->owner);
5076 return TRUE;
5079 /* Run through the input files and ensure that every input section has
5080 somewhere to go. If one is found without a destination then create
5081 an input request and place it into the statement tree. */
5083 static void
5084 lang_place_orphans (void)
5086 LANG_FOR_EACH_INPUT_STATEMENT (file)
5088 asection *s;
5090 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5092 if (s->output_section == NULL)
5094 /* This section of the file is not attached, root
5095 around for a sensible place for it to go. */
5097 if (file->just_syms_flag)
5098 bfd_link_just_syms (file->the_bfd, s, &link_info);
5099 else if ((s->flags & SEC_EXCLUDE) != 0)
5100 s->output_section = bfd_abs_section_ptr;
5101 else if (strcmp (s->name, "COMMON") == 0)
5103 /* This is a lonely common section which must have
5104 come from an archive. We attach to the section
5105 with the wildcard. */
5106 if (! link_info.relocatable
5107 || command_line.force_common_definition)
5109 if (default_common_section == NULL)
5111 default_common_section =
5112 lang_output_section_statement_lookup (".bss");
5115 lang_add_section (&default_common_section->children, s,
5116 default_common_section);
5119 else if (ldemul_place_orphan (s))
5121 else
5123 lang_output_section_statement_type *os;
5125 os = lang_output_section_statement_lookup (s->name);
5126 lang_add_section (&os->children, s, os);
5133 void
5134 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5136 flagword *ptr_flags;
5138 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5139 while (*flags)
5141 switch (*flags)
5143 case 'A': case 'a':
5144 *ptr_flags |= SEC_ALLOC;
5145 break;
5147 case 'R': case 'r':
5148 *ptr_flags |= SEC_READONLY;
5149 break;
5151 case 'W': case 'w':
5152 *ptr_flags |= SEC_DATA;
5153 break;
5155 case 'X': case 'x':
5156 *ptr_flags |= SEC_CODE;
5157 break;
5159 case 'L': case 'l':
5160 case 'I': case 'i':
5161 *ptr_flags |= SEC_LOAD;
5162 break;
5164 default:
5165 einfo (_("%P%F: invalid syntax in flags\n"));
5166 break;
5168 flags++;
5172 /* Call a function on each input file. This function will be called
5173 on an archive, but not on the elements. */
5175 void
5176 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5178 lang_input_statement_type *f;
5180 for (f = (lang_input_statement_type *) input_file_chain.head;
5181 f != NULL;
5182 f = (lang_input_statement_type *) f->next_real_file)
5183 func (f);
5186 /* Call a function on each file. The function will be called on all
5187 the elements of an archive which are included in the link, but will
5188 not be called on the archive file itself. */
5190 void
5191 lang_for_each_file (void (*func) (lang_input_statement_type *))
5193 LANG_FOR_EACH_INPUT_STATEMENT (f)
5195 func (f);
5199 void
5200 ldlang_add_file (lang_input_statement_type *entry)
5202 bfd **pp;
5204 lang_statement_append (&file_chain,
5205 (lang_statement_union_type *) entry,
5206 &entry->next);
5208 /* The BFD linker needs to have a list of all input BFDs involved in
5209 a link. */
5210 ASSERT (entry->the_bfd->link_next == NULL);
5211 ASSERT (entry->the_bfd != output_bfd);
5212 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
5214 *pp = entry->the_bfd;
5215 entry->the_bfd->usrdata = entry;
5216 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5218 /* Look through the sections and check for any which should not be
5219 included in the link. We need to do this now, so that we can
5220 notice when the backend linker tries to report multiple
5221 definition errors for symbols which are in sections we aren't
5222 going to link. FIXME: It might be better to entirely ignore
5223 symbols which are defined in sections which are going to be
5224 discarded. This would require modifying the backend linker for
5225 each backend which might set the SEC_LINK_ONCE flag. If we do
5226 this, we should probably handle SEC_EXCLUDE in the same way. */
5228 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5231 void
5232 lang_add_output (const char *name, int from_script)
5234 /* Make -o on command line override OUTPUT in script. */
5235 if (!had_output_filename || !from_script)
5237 output_filename = name;
5238 had_output_filename = TRUE;
5242 static lang_output_section_statement_type *current_section;
5244 static int
5245 topower (int x)
5247 unsigned int i = 1;
5248 int l;
5250 if (x < 0)
5251 return -1;
5253 for (l = 0; l < 32; l++)
5255 if (i >= (unsigned int) x)
5256 return l;
5257 i <<= 1;
5260 return 0;
5263 lang_output_section_statement_type *
5264 lang_enter_output_section_statement (const char *output_section_statement_name,
5265 etree_type *address_exp,
5266 enum section_type sectype,
5267 etree_type *align,
5268 etree_type *subalign,
5269 etree_type *ebase,
5270 int constraint)
5272 lang_output_section_statement_type *os;
5274 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5275 constraint);
5276 current_section = os;
5278 /* Make next things chain into subchain of this. */
5280 if (os->addr_tree == NULL)
5282 os->addr_tree = address_exp;
5284 os->sectype = sectype;
5285 if (sectype != noload_section)
5286 os->flags = SEC_NO_FLAGS;
5287 else
5288 os->flags = SEC_NEVER_LOAD;
5289 os->block_value = 1;
5290 stat_ptr = &os->children;
5292 os->subsection_alignment =
5293 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5294 os->section_alignment =
5295 topower (exp_get_value_int (align, -1, "section alignment"));
5297 os->load_base = ebase;
5298 return os;
5301 void
5302 lang_final (void)
5304 lang_output_statement_type *new;
5306 new = new_stat (lang_output_statement, stat_ptr);
5307 new->name = output_filename;
5310 /* Reset the current counters in the regions. */
5312 void
5313 lang_reset_memory_regions (void)
5315 lang_memory_region_type *p = lang_memory_region_list;
5316 asection *o;
5317 lang_output_section_statement_type *os;
5319 for (p = lang_memory_region_list; p != NULL; p = p->next)
5321 p->old_length = (bfd_size_type) (p->current - p->origin);
5322 p->current = p->origin;
5325 for (os = &lang_output_section_statement.head->output_section_statement;
5326 os != NULL;
5327 os = os->next)
5328 os->processed = FALSE;
5330 for (o = output_bfd->sections; o != NULL; o = o->next)
5332 /* Save the last size for possible use by bfd_relax_section. */
5333 o->rawsize = o->size;
5334 o->size = 0;
5338 /* Worker for lang_gc_sections_1. */
5340 static void
5341 gc_section_callback (lang_wild_statement_type *ptr,
5342 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5343 asection *section,
5344 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5345 void *data ATTRIBUTE_UNUSED)
5347 /* If the wild pattern was marked KEEP, the member sections
5348 should be as well. */
5349 if (ptr->keep_sections)
5350 section->flags |= SEC_KEEP;
5353 /* Iterate over sections marking them against GC. */
5355 static void
5356 lang_gc_sections_1 (lang_statement_union_type *s)
5358 for (; s != NULL; s = s->header.next)
5360 switch (s->header.type)
5362 case lang_wild_statement_enum:
5363 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5364 break;
5365 case lang_constructors_statement_enum:
5366 lang_gc_sections_1 (constructor_list.head);
5367 break;
5368 case lang_output_section_statement_enum:
5369 lang_gc_sections_1 (s->output_section_statement.children.head);
5370 break;
5371 case lang_group_statement_enum:
5372 lang_gc_sections_1 (s->group_statement.children.head);
5373 break;
5374 default:
5375 break;
5380 static void
5381 lang_gc_sections (void)
5383 struct bfd_link_hash_entry *h;
5384 ldlang_undef_chain_list_type *ulist;
5386 /* Keep all sections so marked in the link script. */
5388 lang_gc_sections_1 (statement_list.head);
5390 /* Keep all sections containing symbols undefined on the command-line,
5391 and the section containing the entry symbol. */
5393 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
5395 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
5396 FALSE, FALSE, FALSE);
5398 if (h != NULL
5399 && (h->type == bfd_link_hash_defined
5400 || h->type == bfd_link_hash_defweak)
5401 && ! bfd_is_abs_section (h->u.def.section))
5403 h->u.def.section->flags |= SEC_KEEP;
5407 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5408 the special case of debug info. (See bfd/stabs.c)
5409 Twiddle the flag here, to simplify later linker code. */
5410 if (link_info.relocatable)
5412 LANG_FOR_EACH_INPUT_STATEMENT (f)
5414 asection *sec;
5415 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5416 if ((sec->flags & SEC_DEBUGGING) == 0)
5417 sec->flags &= ~SEC_EXCLUDE;
5421 if (link_info.gc_sections)
5422 bfd_gc_sections (output_bfd, &link_info);
5425 /* Relax all sections until bfd_relax_section gives up. */
5427 static void
5428 relax_sections (void)
5430 /* Keep relaxing until bfd_relax_section gives up. */
5431 bfd_boolean relax_again;
5435 relax_again = FALSE;
5437 /* Note: pe-dll.c does something like this also. If you find
5438 you need to change this code, you probably need to change
5439 pe-dll.c also. DJ */
5441 /* Do all the assignments with our current guesses as to
5442 section sizes. */
5443 lang_do_assignments ();
5445 /* We must do this after lang_do_assignments, because it uses
5446 size. */
5447 lang_reset_memory_regions ();
5449 /* Perform another relax pass - this time we know where the
5450 globals are, so can make a better guess. */
5451 lang_size_sections (&relax_again, FALSE);
5453 while (relax_again);
5456 void
5457 lang_process (void)
5459 current_target = default_target;
5461 /* Open the output file. */
5462 lang_for_each_statement (ldlang_open_output);
5463 init_opb ();
5465 ldemul_create_output_section_statements ();
5467 /* Add to the hash table all undefineds on the command line. */
5468 lang_place_undefineds ();
5470 if (!bfd_section_already_linked_table_init ())
5471 einfo (_("%P%F: Failed to create hash table\n"));
5473 /* Create a bfd for each input file. */
5474 current_target = default_target;
5475 open_input_bfds (statement_list.head, FALSE);
5477 link_info.gc_sym_list = &entry_symbol;
5478 if (entry_symbol.name == NULL)
5479 link_info.gc_sym_list = ldlang_undef_chain_list_head;
5481 ldemul_after_open ();
5483 bfd_section_already_linked_table_free ();
5485 /* Make sure that we're not mixing architectures. We call this
5486 after all the input files have been opened, but before we do any
5487 other processing, so that any operations merge_private_bfd_data
5488 does on the output file will be known during the rest of the
5489 link. */
5490 lang_check ();
5492 /* Handle .exports instead of a version script if we're told to do so. */
5493 if (command_line.version_exports_section)
5494 lang_do_version_exports_section ();
5496 /* Build all sets based on the information gathered from the input
5497 files. */
5498 ldctor_build_sets ();
5500 /* Remove unreferenced sections if asked to. */
5501 lang_gc_sections ();
5503 /* Size up the common data. */
5504 lang_common ();
5506 /* Update wild statements. */
5507 update_wild_statements (statement_list.head);
5509 /* Run through the contours of the script and attach input sections
5510 to the correct output sections. */
5511 map_input_to_output_sections (statement_list.head, NULL, NULL);
5513 /* Find any sections not attached explicitly and handle them. */
5514 lang_place_orphans ();
5516 if (! link_info.relocatable)
5518 asection *found;
5520 /* Merge SEC_MERGE sections. This has to be done after GC of
5521 sections, so that GCed sections are not merged, but before
5522 assigning dynamic symbols, since removing whole input sections
5523 is hard then. */
5524 bfd_merge_sections (output_bfd, &link_info);
5526 /* Look for a text section and set the readonly attribute in it. */
5527 found = bfd_get_section_by_name (output_bfd, ".text");
5529 if (found != NULL)
5531 if (config.text_read_only)
5532 found->flags |= SEC_READONLY;
5533 else
5534 found->flags &= ~SEC_READONLY;
5538 /* Do anything special before sizing sections. This is where ELF
5539 and other back-ends size dynamic sections. */
5540 ldemul_before_allocation ();
5542 /* We must record the program headers before we try to fix the
5543 section positions, since they will affect SIZEOF_HEADERS. */
5544 lang_record_phdrs ();
5546 /* Size up the sections. */
5547 lang_size_sections (NULL, !command_line.relax);
5549 /* Now run around and relax if we can. */
5550 if (command_line.relax)
5552 /* We may need more than one relaxation pass. */
5553 int i = link_info.relax_pass;
5555 /* The backend can use it to determine the current pass. */
5556 link_info.relax_pass = 0;
5558 while (i--)
5560 relax_sections ();
5561 link_info.relax_pass++;
5564 /* Final extra sizing to report errors. */
5565 lang_do_assignments ();
5566 lang_reset_memory_regions ();
5567 lang_size_sections (NULL, TRUE);
5570 /* See if anything special should be done now we know how big
5571 everything is. */
5572 ldemul_after_allocation ();
5574 /* Fix any .startof. or .sizeof. symbols. */
5575 lang_set_startof ();
5577 /* Do all the assignments, now that we know the final resting places
5578 of all the symbols. */
5580 lang_do_assignments ();
5582 /* Make sure that the section addresses make sense. */
5583 if (! link_info.relocatable
5584 && command_line.check_section_addresses)
5585 lang_check_section_addresses ();
5587 /* Final stuffs. */
5588 ldemul_finish ();
5589 lang_end ();
5592 /* EXPORTED TO YACC */
5594 void
5595 lang_add_wild (struct wildcard_spec *filespec,
5596 struct wildcard_list *section_list,
5597 bfd_boolean keep_sections)
5599 struct wildcard_list *curr, *next;
5600 lang_wild_statement_type *new;
5602 /* Reverse the list as the parser puts it back to front. */
5603 for (curr = section_list, section_list = NULL;
5604 curr != NULL;
5605 section_list = curr, curr = next)
5607 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
5608 placed_commons = TRUE;
5610 next = curr->next;
5611 curr->next = section_list;
5614 if (filespec != NULL && filespec->name != NULL)
5616 if (strcmp (filespec->name, "*") == 0)
5617 filespec->name = NULL;
5618 else if (! wildcardp (filespec->name))
5619 lang_has_input_file = TRUE;
5622 new = new_stat (lang_wild_statement, stat_ptr);
5623 new->filename = NULL;
5624 new->filenames_sorted = FALSE;
5625 if (filespec != NULL)
5627 new->filename = filespec->name;
5628 new->filenames_sorted = filespec->sorted == by_name;
5630 new->section_list = section_list;
5631 new->keep_sections = keep_sections;
5632 lang_list_init (&new->children);
5633 analyze_walk_wild_section_handler (new);
5636 void
5637 lang_section_start (const char *name, etree_type *address,
5638 const segment_type *segment)
5640 lang_address_statement_type *ad;
5642 ad = new_stat (lang_address_statement, stat_ptr);
5643 ad->section_name = name;
5644 ad->address = address;
5645 ad->segment = segment;
5648 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5649 because of a -e argument on the command line, or zero if this is
5650 called by ENTRY in a linker script. Command line arguments take
5651 precedence. */
5653 void
5654 lang_add_entry (const char *name, bfd_boolean cmdline)
5656 if (entry_symbol.name == NULL
5657 || cmdline
5658 || ! entry_from_cmdline)
5660 entry_symbol.name = name;
5661 entry_from_cmdline = cmdline;
5665 /* Set the default start symbol to NAME. .em files should use this,
5666 not lang_add_entry, to override the use of "start" if neither the
5667 linker script nor the command line specifies an entry point. NAME
5668 must be permanently allocated. */
5669 void
5670 lang_default_entry (const char *name)
5672 entry_symbol_default = name;
5675 void
5676 lang_add_target (const char *name)
5678 lang_target_statement_type *new;
5680 new = new_stat (lang_target_statement, stat_ptr);
5681 new->target = name;
5684 void
5685 lang_add_map (const char *name)
5687 while (*name)
5689 switch (*name)
5691 case 'F':
5692 map_option_f = TRUE;
5693 break;
5695 name++;
5699 void
5700 lang_add_fill (fill_type *fill)
5702 lang_fill_statement_type *new;
5704 new = new_stat (lang_fill_statement, stat_ptr);
5705 new->fill = fill;
5708 void
5709 lang_add_data (int type, union etree_union *exp)
5711 lang_data_statement_type *new;
5713 new = new_stat (lang_data_statement, stat_ptr);
5714 new->exp = exp;
5715 new->type = type;
5718 /* Create a new reloc statement. RELOC is the BFD relocation type to
5719 generate. HOWTO is the corresponding howto structure (we could
5720 look this up, but the caller has already done so). SECTION is the
5721 section to generate a reloc against, or NAME is the name of the
5722 symbol to generate a reloc against. Exactly one of SECTION and
5723 NAME must be NULL. ADDEND is an expression for the addend. */
5725 void
5726 lang_add_reloc (bfd_reloc_code_real_type reloc,
5727 reloc_howto_type *howto,
5728 asection *section,
5729 const char *name,
5730 union etree_union *addend)
5732 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5734 p->reloc = reloc;
5735 p->howto = howto;
5736 p->section = section;
5737 p->name = name;
5738 p->addend_exp = addend;
5740 p->addend_value = 0;
5741 p->output_section = NULL;
5742 p->output_offset = 0;
5745 lang_assignment_statement_type *
5746 lang_add_assignment (etree_type *exp)
5748 lang_assignment_statement_type *new;
5750 new = new_stat (lang_assignment_statement, stat_ptr);
5751 new->exp = exp;
5752 return new;
5755 void
5756 lang_add_attribute (enum statement_enum attribute)
5758 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
5761 void
5762 lang_startup (const char *name)
5764 if (startup_file != NULL)
5766 einfo (_("%P%F: multiple STARTUP files\n"));
5768 first_file->filename = name;
5769 first_file->local_sym_name = name;
5770 first_file->real = TRUE;
5772 startup_file = name;
5775 void
5776 lang_float (bfd_boolean maybe)
5778 lang_float_flag = maybe;
5782 /* Work out the load- and run-time regions from a script statement, and
5783 store them in *LMA_REGION and *REGION respectively.
5785 MEMSPEC is the name of the run-time region, or the value of
5786 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5787 LMA_MEMSPEC is the name of the load-time region, or null if the
5788 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5789 had an explicit load address.
5791 It is an error to specify both a load region and a load address. */
5793 static void
5794 lang_get_regions (lang_memory_region_type **region,
5795 lang_memory_region_type **lma_region,
5796 const char *memspec,
5797 const char *lma_memspec,
5798 bfd_boolean have_lma,
5799 bfd_boolean have_vma)
5801 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5803 /* If no runtime region or VMA has been specified, but the load region
5804 has been specified, then use the load region for the runtime region
5805 as well. */
5806 if (lma_memspec != NULL
5807 && ! have_vma
5808 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5809 *region = *lma_region;
5810 else
5811 *region = lang_memory_region_lookup (memspec, FALSE);
5813 if (have_lma && lma_memspec != 0)
5814 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5817 void
5818 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5819 lang_output_section_phdr_list *phdrs,
5820 const char *lma_memspec)
5822 lang_get_regions (&current_section->region,
5823 &current_section->lma_region,
5824 memspec, lma_memspec,
5825 current_section->load_base != NULL,
5826 current_section->addr_tree != NULL);
5827 current_section->fill = fill;
5828 current_section->phdrs = phdrs;
5829 stat_ptr = &statement_list;
5832 /* Create an absolute symbol with the given name with the value of the
5833 address of first byte of the section named.
5835 If the symbol already exists, then do nothing. */
5837 void
5838 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5840 struct bfd_link_hash_entry *h;
5842 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5843 if (h == NULL)
5844 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5846 if (h->type == bfd_link_hash_new
5847 || h->type == bfd_link_hash_undefined)
5849 asection *sec;
5851 h->type = bfd_link_hash_defined;
5853 sec = bfd_get_section_by_name (output_bfd, secname);
5854 if (sec == NULL)
5855 h->u.def.value = 0;
5856 else
5857 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5859 h->u.def.section = bfd_abs_section_ptr;
5863 /* Create an absolute symbol with the given name with the value of the
5864 address of the first byte after the end of the section named.
5866 If the symbol already exists, then do nothing. */
5868 void
5869 lang_abs_symbol_at_end_of (const char *secname, const char *name)
5871 struct bfd_link_hash_entry *h;
5873 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5874 if (h == NULL)
5875 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5877 if (h->type == bfd_link_hash_new
5878 || h->type == bfd_link_hash_undefined)
5880 asection *sec;
5882 h->type = bfd_link_hash_defined;
5884 sec = bfd_get_section_by_name (output_bfd, secname);
5885 if (sec == NULL)
5886 h->u.def.value = 0;
5887 else
5888 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
5889 + TO_ADDR (sec->size));
5891 h->u.def.section = bfd_abs_section_ptr;
5895 void
5896 lang_statement_append (lang_statement_list_type *list,
5897 lang_statement_union_type *element,
5898 lang_statement_union_type **field)
5900 *(list->tail) = element;
5901 list->tail = field;
5904 /* Set the output format type. -oformat overrides scripts. */
5906 void
5907 lang_add_output_format (const char *format,
5908 const char *big,
5909 const char *little,
5910 int from_script)
5912 if (output_target == NULL || !from_script)
5914 if (command_line.endian == ENDIAN_BIG
5915 && big != NULL)
5916 format = big;
5917 else if (command_line.endian == ENDIAN_LITTLE
5918 && little != NULL)
5919 format = little;
5921 output_target = format;
5925 /* Enter a group. This creates a new lang_group_statement, and sets
5926 stat_ptr to build new statements within the group. */
5928 void
5929 lang_enter_group (void)
5931 lang_group_statement_type *g;
5933 g = new_stat (lang_group_statement, stat_ptr);
5934 lang_list_init (&g->children);
5935 stat_ptr = &g->children;
5938 /* Leave a group. This just resets stat_ptr to start writing to the
5939 regular list of statements again. Note that this will not work if
5940 groups can occur inside anything else which can adjust stat_ptr,
5941 but currently they can't. */
5943 void
5944 lang_leave_group (void)
5946 stat_ptr = &statement_list;
5949 /* Add a new program header. This is called for each entry in a PHDRS
5950 command in a linker script. */
5952 void
5953 lang_new_phdr (const char *name,
5954 etree_type *type,
5955 bfd_boolean filehdr,
5956 bfd_boolean phdrs,
5957 etree_type *at,
5958 etree_type *flags)
5960 struct lang_phdr *n, **pp;
5962 n = stat_alloc (sizeof (struct lang_phdr));
5963 n->next = NULL;
5964 n->name = name;
5965 n->type = exp_get_value_int (type, 0, "program header type");
5966 n->filehdr = filehdr;
5967 n->phdrs = phdrs;
5968 n->at = at;
5969 n->flags = flags;
5971 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
5973 *pp = n;
5976 /* Record the program header information in the output BFD. FIXME: We
5977 should not be calling an ELF specific function here. */
5979 static void
5980 lang_record_phdrs (void)
5982 unsigned int alc;
5983 asection **secs;
5984 lang_output_section_phdr_list *last;
5985 struct lang_phdr *l;
5986 lang_output_section_statement_type *os;
5988 alc = 10;
5989 secs = xmalloc (alc * sizeof (asection *));
5990 last = NULL;
5991 for (l = lang_phdr_list; l != NULL; l = l->next)
5993 unsigned int c;
5994 flagword flags;
5995 bfd_vma at;
5997 c = 0;
5998 for (os = &lang_output_section_statement.head->output_section_statement;
5999 os != NULL;
6000 os = os->next)
6002 lang_output_section_phdr_list *pl;
6004 if (os->constraint == -1)
6005 continue;
6007 pl = os->phdrs;
6008 if (pl != NULL)
6009 last = pl;
6010 else
6012 if (os->sectype == noload_section
6013 || os->bfd_section == NULL
6014 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6015 continue;
6016 pl = last;
6019 if (os->bfd_section == NULL)
6020 continue;
6022 for (; pl != NULL; pl = pl->next)
6024 if (strcmp (pl->name, l->name) == 0)
6026 if (c >= alc)
6028 alc *= 2;
6029 secs = xrealloc (secs, alc * sizeof (asection *));
6031 secs[c] = os->bfd_section;
6032 ++c;
6033 pl->used = TRUE;
6038 if (l->flags == NULL)
6039 flags = 0;
6040 else
6041 flags = exp_get_vma (l->flags, 0, "phdr flags");
6043 if (l->at == NULL)
6044 at = 0;
6045 else
6046 at = exp_get_vma (l->at, 0, "phdr load address");
6048 if (! bfd_record_phdr (output_bfd, l->type,
6049 l->flags != NULL, flags, l->at != NULL,
6050 at, l->filehdr, l->phdrs, c, secs))
6051 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6054 free (secs);
6056 /* Make sure all the phdr assignments succeeded. */
6057 for (os = &lang_output_section_statement.head->output_section_statement;
6058 os != NULL;
6059 os = os->next)
6061 lang_output_section_phdr_list *pl;
6063 if (os->constraint == -1
6064 || os->bfd_section == NULL)
6065 continue;
6067 for (pl = os->phdrs;
6068 pl != NULL;
6069 pl = pl->next)
6070 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6071 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6072 os->name, pl->name);
6076 /* Record a list of sections which may not be cross referenced. */
6078 void
6079 lang_add_nocrossref (lang_nocrossref_type *l)
6081 struct lang_nocrossrefs *n;
6083 n = xmalloc (sizeof *n);
6084 n->next = nocrossref_list;
6085 n->list = l;
6086 nocrossref_list = n;
6088 /* Set notice_all so that we get informed about all symbols. */
6089 link_info.notice_all = TRUE;
6092 /* Overlay handling. We handle overlays with some static variables. */
6094 /* The overlay virtual address. */
6095 static etree_type *overlay_vma;
6096 /* And subsection alignment. */
6097 static etree_type *overlay_subalign;
6099 /* An expression for the maximum section size seen so far. */
6100 static etree_type *overlay_max;
6102 /* A list of all the sections in this overlay. */
6104 struct overlay_list {
6105 struct overlay_list *next;
6106 lang_output_section_statement_type *os;
6109 static struct overlay_list *overlay_list;
6111 /* Start handling an overlay. */
6113 void
6114 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6116 /* The grammar should prevent nested overlays from occurring. */
6117 ASSERT (overlay_vma == NULL
6118 && overlay_subalign == NULL
6119 && overlay_max == NULL);
6121 overlay_vma = vma_expr;
6122 overlay_subalign = subalign;
6125 /* Start a section in an overlay. We handle this by calling
6126 lang_enter_output_section_statement with the correct VMA.
6127 lang_leave_overlay sets up the LMA and memory regions. */
6129 void
6130 lang_enter_overlay_section (const char *name)
6132 struct overlay_list *n;
6133 etree_type *size;
6135 lang_enter_output_section_statement (name, overlay_vma, normal_section,
6136 0, overlay_subalign, 0, 0);
6138 /* If this is the first section, then base the VMA of future
6139 sections on this one. This will work correctly even if `.' is
6140 used in the addresses. */
6141 if (overlay_list == NULL)
6142 overlay_vma = exp_nameop (ADDR, name);
6144 /* Remember the section. */
6145 n = xmalloc (sizeof *n);
6146 n->os = current_section;
6147 n->next = overlay_list;
6148 overlay_list = n;
6150 size = exp_nameop (SIZEOF, name);
6152 /* Arrange to work out the maximum section end address. */
6153 if (overlay_max == NULL)
6154 overlay_max = size;
6155 else
6156 overlay_max = exp_binop (MAX_K, overlay_max, size);
6159 /* Finish a section in an overlay. There isn't any special to do
6160 here. */
6162 void
6163 lang_leave_overlay_section (fill_type *fill,
6164 lang_output_section_phdr_list *phdrs)
6166 const char *name;
6167 char *clean, *s2;
6168 const char *s1;
6169 char *buf;
6171 name = current_section->name;
6173 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6174 region and that no load-time region has been specified. It doesn't
6175 really matter what we say here, since lang_leave_overlay will
6176 override it. */
6177 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6179 /* Define the magic symbols. */
6181 clean = xmalloc (strlen (name) + 1);
6182 s2 = clean;
6183 for (s1 = name; *s1 != '\0'; s1++)
6184 if (ISALNUM (*s1) || *s1 == '_')
6185 *s2++ = *s1;
6186 *s2 = '\0';
6188 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6189 sprintf (buf, "__load_start_%s", clean);
6190 lang_add_assignment (exp_assop ('=', buf,
6191 exp_nameop (LOADADDR, name)));
6193 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6194 sprintf (buf, "__load_stop_%s", clean);
6195 lang_add_assignment (exp_assop ('=', buf,
6196 exp_binop ('+',
6197 exp_nameop (LOADADDR, name),
6198 exp_nameop (SIZEOF, name))));
6200 free (clean);
6203 /* Finish an overlay. If there are any overlay wide settings, this
6204 looks through all the sections in the overlay and sets them. */
6206 void
6207 lang_leave_overlay (etree_type *lma_expr,
6208 int nocrossrefs,
6209 fill_type *fill,
6210 const char *memspec,
6211 lang_output_section_phdr_list *phdrs,
6212 const char *lma_memspec)
6214 lang_memory_region_type *region;
6215 lang_memory_region_type *lma_region;
6216 struct overlay_list *l;
6217 lang_nocrossref_type *nocrossref;
6219 lang_get_regions (&region, &lma_region,
6220 memspec, lma_memspec,
6221 lma_expr != NULL, FALSE);
6223 nocrossref = NULL;
6225 /* After setting the size of the last section, set '.' to end of the
6226 overlay region. */
6227 if (overlay_list != NULL)
6228 overlay_list->os->update_dot_tree
6229 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6231 l = overlay_list;
6232 while (l != NULL)
6234 struct overlay_list *next;
6236 if (fill != NULL && l->os->fill == NULL)
6237 l->os->fill = fill;
6239 l->os->region = region;
6240 l->os->lma_region = lma_region;
6242 /* The first section has the load address specified in the
6243 OVERLAY statement. The rest are worked out from that.
6244 The base address is not needed (and should be null) if
6245 an LMA region was specified. */
6246 if (l->next == 0)
6247 l->os->load_base = lma_expr;
6248 else if (lma_region == 0)
6249 l->os->load_base = exp_binop ('+',
6250 exp_nameop (LOADADDR, l->next->os->name),
6251 exp_nameop (SIZEOF, l->next->os->name));
6253 if (phdrs != NULL && l->os->phdrs == NULL)
6254 l->os->phdrs = phdrs;
6256 if (nocrossrefs)
6258 lang_nocrossref_type *nc;
6260 nc = xmalloc (sizeof *nc);
6261 nc->name = l->os->name;
6262 nc->next = nocrossref;
6263 nocrossref = nc;
6266 next = l->next;
6267 free (l);
6268 l = next;
6271 if (nocrossref != NULL)
6272 lang_add_nocrossref (nocrossref);
6274 overlay_vma = NULL;
6275 overlay_list = NULL;
6276 overlay_max = NULL;
6279 /* Version handling. This is only useful for ELF. */
6281 /* This global variable holds the version tree that we build. */
6283 struct bfd_elf_version_tree *lang_elf_version_info;
6285 /* If PREV is NULL, return first version pattern matching particular symbol.
6286 If PREV is non-NULL, return first version pattern matching particular
6287 symbol after PREV (previously returned by lang_vers_match). */
6289 static struct bfd_elf_version_expr *
6290 lang_vers_match (struct bfd_elf_version_expr_head *head,
6291 struct bfd_elf_version_expr *prev,
6292 const char *sym)
6294 const char *cxx_sym = sym;
6295 const char *java_sym = sym;
6296 struct bfd_elf_version_expr *expr = NULL;
6298 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6300 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
6301 if (!cxx_sym)
6302 cxx_sym = sym;
6304 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6306 java_sym = cplus_demangle (sym, DMGL_JAVA);
6307 if (!java_sym)
6308 java_sym = sym;
6311 if (head->htab && (prev == NULL || prev->symbol))
6313 struct bfd_elf_version_expr e;
6315 switch (prev ? prev->mask : 0)
6317 case 0:
6318 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6320 e.symbol = sym;
6321 expr = htab_find (head->htab, &e);
6322 while (expr && strcmp (expr->symbol, sym) == 0)
6323 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6324 goto out_ret;
6325 else
6326 expr = expr->next;
6328 /* Fallthrough */
6329 case BFD_ELF_VERSION_C_TYPE:
6330 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6332 e.symbol = cxx_sym;
6333 expr = htab_find (head->htab, &e);
6334 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6335 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6336 goto out_ret;
6337 else
6338 expr = expr->next;
6340 /* Fallthrough */
6341 case BFD_ELF_VERSION_CXX_TYPE:
6342 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6344 e.symbol = java_sym;
6345 expr = htab_find (head->htab, &e);
6346 while (expr && strcmp (expr->symbol, java_sym) == 0)
6347 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6348 goto out_ret;
6349 else
6350 expr = expr->next;
6352 /* Fallthrough */
6353 default:
6354 break;
6358 /* Finally, try the wildcards. */
6359 if (prev == NULL || prev->symbol)
6360 expr = head->remaining;
6361 else
6362 expr = prev->next;
6363 for (; expr; expr = expr->next)
6365 const char *s;
6367 if (!expr->pattern)
6368 continue;
6370 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6371 break;
6373 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6374 s = java_sym;
6375 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6376 s = cxx_sym;
6377 else
6378 s = sym;
6379 if (fnmatch (expr->pattern, s, 0) == 0)
6380 break;
6383 out_ret:
6384 if (cxx_sym != sym)
6385 free ((char *) cxx_sym);
6386 if (java_sym != sym)
6387 free ((char *) java_sym);
6388 return expr;
6391 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6392 return a string pointing to the symbol name. */
6394 static const char *
6395 realsymbol (const char *pattern)
6397 const char *p;
6398 bfd_boolean changed = FALSE, backslash = FALSE;
6399 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6401 for (p = pattern, s = symbol; *p != '\0'; ++p)
6403 /* It is a glob pattern only if there is no preceding
6404 backslash. */
6405 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
6407 free (symbol);
6408 return NULL;
6411 if (backslash)
6413 /* Remove the preceding backslash. */
6414 *(s - 1) = *p;
6415 changed = TRUE;
6417 else
6418 *s++ = *p;
6420 backslash = *p == '\\';
6423 if (changed)
6425 *s = '\0';
6426 return symbol;
6428 else
6430 free (symbol);
6431 return pattern;
6435 /* This is called for each variable name or match expression. NEW is
6436 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6437 pattern to be matched against symbol names. */
6439 struct bfd_elf_version_expr *
6440 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
6441 const char *new,
6442 const char *lang,
6443 bfd_boolean literal_p)
6445 struct bfd_elf_version_expr *ret;
6447 ret = xmalloc (sizeof *ret);
6448 ret->next = orig;
6449 ret->pattern = literal_p ? NULL : new;
6450 ret->symver = 0;
6451 ret->script = 0;
6452 ret->symbol = literal_p ? new : realsymbol (new);
6454 if (lang == NULL || strcasecmp (lang, "C") == 0)
6455 ret->mask = BFD_ELF_VERSION_C_TYPE;
6456 else if (strcasecmp (lang, "C++") == 0)
6457 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
6458 else if (strcasecmp (lang, "Java") == 0)
6459 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
6460 else
6462 einfo (_("%X%P: unknown language `%s' in version information\n"),
6463 lang);
6464 ret->mask = BFD_ELF_VERSION_C_TYPE;
6467 return ldemul_new_vers_pattern (ret);
6470 /* This is called for each set of variable names and match
6471 expressions. */
6473 struct bfd_elf_version_tree *
6474 lang_new_vers_node (struct bfd_elf_version_expr *globals,
6475 struct bfd_elf_version_expr *locals)
6477 struct bfd_elf_version_tree *ret;
6479 ret = xcalloc (1, sizeof *ret);
6480 ret->globals.list = globals;
6481 ret->locals.list = locals;
6482 ret->match = lang_vers_match;
6483 ret->name_indx = (unsigned int) -1;
6484 return ret;
6487 /* This static variable keeps track of version indices. */
6489 static int version_index;
6491 static hashval_t
6492 version_expr_head_hash (const void *p)
6494 const struct bfd_elf_version_expr *e = p;
6496 return htab_hash_string (e->symbol);
6499 static int
6500 version_expr_head_eq (const void *p1, const void *p2)
6502 const struct bfd_elf_version_expr *e1 = p1;
6503 const struct bfd_elf_version_expr *e2 = p2;
6505 return strcmp (e1->symbol, e2->symbol) == 0;
6508 static void
6509 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
6511 size_t count = 0;
6512 struct bfd_elf_version_expr *e, *next;
6513 struct bfd_elf_version_expr **list_loc, **remaining_loc;
6515 for (e = head->list; e; e = e->next)
6517 if (e->symbol)
6518 count++;
6519 head->mask |= e->mask;
6522 if (count)
6524 head->htab = htab_create (count * 2, version_expr_head_hash,
6525 version_expr_head_eq, NULL);
6526 list_loc = &head->list;
6527 remaining_loc = &head->remaining;
6528 for (e = head->list; e; e = next)
6530 next = e->next;
6531 if (!e->symbol)
6533 *remaining_loc = e;
6534 remaining_loc = &e->next;
6536 else
6538 void **loc = htab_find_slot (head->htab, e, INSERT);
6540 if (*loc)
6542 struct bfd_elf_version_expr *e1, *last;
6544 e1 = *loc;
6545 last = NULL;
6548 if (e1->mask == e->mask)
6550 last = NULL;
6551 break;
6553 last = e1;
6554 e1 = e1->next;
6556 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
6558 if (last == NULL)
6560 /* This is a duplicate. */
6561 /* FIXME: Memory leak. Sometimes pattern is not
6562 xmalloced alone, but in larger chunk of memory. */
6563 /* free (e->symbol); */
6564 free (e);
6566 else
6568 e->next = last->next;
6569 last->next = e;
6572 else
6574 *loc = e;
6575 *list_loc = e;
6576 list_loc = &e->next;
6580 *remaining_loc = NULL;
6581 *list_loc = head->remaining;
6583 else
6584 head->remaining = head->list;
6587 /* This is called when we know the name and dependencies of the
6588 version. */
6590 void
6591 lang_register_vers_node (const char *name,
6592 struct bfd_elf_version_tree *version,
6593 struct bfd_elf_version_deps *deps)
6595 struct bfd_elf_version_tree *t, **pp;
6596 struct bfd_elf_version_expr *e1;
6598 if (name == NULL)
6599 name = "";
6601 if ((name[0] == '\0' && lang_elf_version_info != NULL)
6602 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
6604 einfo (_("%X%P: anonymous version tag cannot be combined"
6605 " with other version tags\n"));
6606 free (version);
6607 return;
6610 /* Make sure this node has a unique name. */
6611 for (t = lang_elf_version_info; t != NULL; t = t->next)
6612 if (strcmp (t->name, name) == 0)
6613 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
6615 lang_finalize_version_expr_head (&version->globals);
6616 lang_finalize_version_expr_head (&version->locals);
6618 /* Check the global and local match names, and make sure there
6619 aren't any duplicates. */
6621 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
6623 for (t = lang_elf_version_info; t != NULL; t = t->next)
6625 struct bfd_elf_version_expr *e2;
6627 if (t->locals.htab && e1->symbol)
6629 e2 = htab_find (t->locals.htab, e1);
6630 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6632 if (e1->mask == e2->mask)
6633 einfo (_("%X%P: duplicate expression `%s'"
6634 " in version information\n"), e1->symbol);
6635 e2 = e2->next;
6638 else if (!e1->symbol)
6639 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
6640 if (strcmp (e1->pattern, e2->pattern) == 0
6641 && e1->mask == e2->mask)
6642 einfo (_("%X%P: duplicate expression `%s'"
6643 " in version information\n"), e1->pattern);
6647 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
6649 for (t = lang_elf_version_info; t != NULL; t = t->next)
6651 struct bfd_elf_version_expr *e2;
6653 if (t->globals.htab && e1->symbol)
6655 e2 = htab_find (t->globals.htab, e1);
6656 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6658 if (e1->mask == e2->mask)
6659 einfo (_("%X%P: duplicate expression `%s'"
6660 " in version information\n"),
6661 e1->symbol);
6662 e2 = e2->next;
6665 else if (!e1->symbol)
6666 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6667 if (strcmp (e1->pattern, e2->pattern) == 0
6668 && e1->mask == e2->mask)
6669 einfo (_("%X%P: duplicate expression `%s'"
6670 " in version information\n"), e1->pattern);
6674 version->deps = deps;
6675 version->name = name;
6676 if (name[0] != '\0')
6678 ++version_index;
6679 version->vernum = version_index;
6681 else
6682 version->vernum = 0;
6684 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6686 *pp = version;
6689 /* This is called when we see a version dependency. */
6691 struct bfd_elf_version_deps *
6692 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6694 struct bfd_elf_version_deps *ret;
6695 struct bfd_elf_version_tree *t;
6697 ret = xmalloc (sizeof *ret);
6698 ret->next = list;
6700 for (t = lang_elf_version_info; t != NULL; t = t->next)
6702 if (strcmp (t->name, name) == 0)
6704 ret->version_needed = t;
6705 return ret;
6709 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6711 return ret;
6714 static void
6715 lang_do_version_exports_section (void)
6717 struct bfd_elf_version_expr *greg = NULL, *lreg;
6719 LANG_FOR_EACH_INPUT_STATEMENT (is)
6721 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6722 char *contents, *p;
6723 bfd_size_type len;
6725 if (sec == NULL)
6726 continue;
6728 len = sec->size;
6729 contents = xmalloc (len);
6730 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6731 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6733 p = contents;
6734 while (p < contents + len)
6736 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
6737 p = strchr (p, '\0') + 1;
6740 /* Do not free the contents, as we used them creating the regex. */
6742 /* Do not include this section in the link. */
6743 sec->flags |= SEC_EXCLUDE;
6746 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
6747 lang_register_vers_node (command_line.version_exports_section,
6748 lang_new_vers_node (greg, lreg), NULL);
6751 void
6752 lang_add_unique (const char *name)
6754 struct unique_sections *ent;
6756 for (ent = unique_section_list; ent; ent = ent->next)
6757 if (strcmp (ent->name, name) == 0)
6758 return;
6760 ent = xmalloc (sizeof *ent);
6761 ent->name = xstrdup (name);
6762 ent->next = unique_section_list;
6763 unique_section_list = ent;