* readelf.c (dump_relocations): Handle MIPS-specific
[binutils.git] / ld / ldlang.c
blob73d405365dd683d4a1744941bf272e887a9158ea
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 /* Binary search tree structure to
49 efficiently sort sections by name. */
50 typedef struct lang_section_bst
52 asection *section;
53 struct lang_section_bst *left;
54 struct lang_section_bst *right;
55 } lang_section_bst_type;
57 /* Locals variables. */
58 static struct obstack stat_obstack;
59 static struct obstack map_obstack;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
63 static const char *startup_file;
64 static lang_statement_list_type input_file_chain;
65 static bfd_boolean placed_commons = FALSE;
66 static bfd_boolean stripped_excluded_sections = FALSE;
67 static lang_output_section_statement_type *default_common_section;
68 static bfd_boolean map_option_f;
69 static bfd_vma print_dot;
70 static lang_input_statement_type *first_file;
71 static const char *current_target;
72 static const char *output_target;
73 static lang_statement_list_type statement_list;
74 static struct bfd_hash_table lang_definedness_table;
76 /* Forward declarations. */
77 static void exp_init_os (etree_type *);
78 static void init_map_userdata (bfd *, asection *, void *);
79 static lang_input_statement_type *lookup_name (const char *);
80 static bfd_boolean load_symbols (lang_input_statement_type *,
81 lang_statement_list_type *);
82 static struct bfd_hash_entry *lang_definedness_newfunc
83 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
84 static void insert_undefined (const char *);
85 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
86 static void print_statement (lang_statement_union_type *,
87 lang_output_section_statement_type *);
88 static void print_statement_list (lang_statement_union_type *,
89 lang_output_section_statement_type *);
90 static void print_statements (void);
91 static void print_input_section (asection *);
92 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
93 static void lang_record_phdrs (void);
94 static void lang_do_version_exports_section (void);
96 /* Exported variables. */
97 lang_output_section_statement_type *abs_output_section;
98 lang_statement_list_type lang_output_section_statement;
99 lang_statement_list_type *stat_ptr = &statement_list;
100 lang_statement_list_type file_chain = { NULL, NULL };
101 struct bfd_sym_chain entry_symbol = { NULL, NULL };
102 static const char *entry_symbol_default = "start";
103 const char *entry_section = ".text";
104 bfd_boolean entry_from_cmdline;
105 bfd_boolean lang_has_input_file = FALSE;
106 bfd_boolean had_output_filename = FALSE;
107 bfd_boolean lang_float_flag = FALSE;
108 bfd_boolean delete_output_file_on_failure = FALSE;
109 struct lang_phdr *lang_phdr_list;
110 struct lang_nocrossrefs *nocrossref_list;
111 static struct unique_sections *unique_section_list;
112 static bfd_boolean ldlang_sysrooted_script = FALSE;
114 /* Functions that traverse the linker script and might evaluate
115 DEFINED() need to increment this. */
116 int lang_statement_iteration = 0;
118 etree_type *base; /* Relocation base - or null */
120 /* Return TRUE if the PATTERN argument is a wildcard pattern.
121 Although backslashes are treated specially if a pattern contains
122 wildcards, we do not consider the mere presence of a backslash to
123 be enough to cause the pattern to be treated as a wildcard.
124 That lets us handle DOS filenames more naturally. */
125 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
127 #define new_stat(x, y) \
128 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
130 #define outside_section_address(q) \
131 ((q)->output_offset + (q)->output_section->vma)
133 #define outside_symbol_address(q) \
134 ((q)->value + outside_section_address (q->section))
136 #define SECTION_NAME_MAP_LENGTH (16)
138 void *
139 stat_alloc (size_t size)
141 return obstack_alloc (&stat_obstack, size);
144 bfd_boolean
145 unique_section_p (const asection *sec)
147 struct unique_sections *unam;
148 const char *secnam;
150 if (link_info.relocatable
151 && sec->owner != NULL
152 && bfd_is_group_section (sec->owner, sec))
153 return TRUE;
155 secnam = sec->name;
156 for (unam = unique_section_list; unam; unam = unam->next)
157 if (wildcardp (unam->name)
158 ? fnmatch (unam->name, secnam, 0) == 0
159 : strcmp (unam->name, secnam) == 0)
161 return TRUE;
164 return FALSE;
167 /* Generic traversal routines for finding matching sections. */
169 /* Try processing a section against a wildcard. This just calls
170 the callback unless the filename exclusion list is present
171 and excludes the file. It's hardly ever present so this
172 function is very fast. */
174 static void
175 walk_wild_consider_section (lang_wild_statement_type *ptr,
176 lang_input_statement_type *file,
177 asection *s,
178 struct wildcard_list *sec,
179 callback_t callback,
180 void *data)
182 bfd_boolean skip = FALSE;
183 struct name_list *list_tmp;
185 /* Don't process sections from files which were
186 excluded. */
187 for (list_tmp = sec->spec.exclude_name_list;
188 list_tmp;
189 list_tmp = list_tmp->next)
191 bfd_boolean is_wildcard = wildcardp (list_tmp->name);
192 if (is_wildcard)
193 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
194 else
195 skip = strcmp (list_tmp->name, file->filename) == 0;
197 /* If this file is part of an archive, and the archive is
198 excluded, exclude this file. */
199 if (! skip && file->the_bfd != NULL
200 && file->the_bfd->my_archive != NULL
201 && file->the_bfd->my_archive->filename != NULL)
203 if (is_wildcard)
204 skip = fnmatch (list_tmp->name,
205 file->the_bfd->my_archive->filename,
206 0) == 0;
207 else
208 skip = strcmp (list_tmp->name,
209 file->the_bfd->my_archive->filename) == 0;
212 if (skip)
213 break;
216 if (!skip)
217 (*callback) (ptr, sec, s, file, data);
220 /* Lowest common denominator routine that can handle everything correctly,
221 but slowly. */
223 static void
224 walk_wild_section_general (lang_wild_statement_type *ptr,
225 lang_input_statement_type *file,
226 callback_t callback,
227 void *data)
229 asection *s;
230 struct wildcard_list *sec;
232 for (s = file->the_bfd->sections; s != NULL; s = s->next)
234 sec = ptr->section_list;
235 if (sec == NULL)
236 (*callback) (ptr, sec, s, file, data);
238 while (sec != NULL)
240 bfd_boolean skip = FALSE;
242 if (sec->spec.name != NULL)
244 const char *sname = bfd_get_section_name (file->the_bfd, s);
246 if (wildcardp (sec->spec.name))
247 skip = fnmatch (sec->spec.name, sname, 0) != 0;
248 else
249 skip = strcmp (sec->spec.name, sname) != 0;
252 if (!skip)
253 walk_wild_consider_section (ptr, file, s, sec, callback, data);
255 sec = sec->next;
260 /* Routines to find a single section given its name. If there's more
261 than one section with that name, we report that. */
263 typedef struct
265 asection *found_section;
266 bfd_boolean multiple_sections_found;
267 } section_iterator_callback_data;
269 static bfd_boolean
270 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
272 section_iterator_callback_data *d = data;
274 if (d->found_section != NULL)
276 d->multiple_sections_found = TRUE;
277 return TRUE;
280 d->found_section = s;
281 return FALSE;
284 static asection *
285 find_section (lang_input_statement_type *file,
286 struct wildcard_list *sec,
287 bfd_boolean *multiple_sections_found)
289 section_iterator_callback_data cb_data = { NULL, FALSE };
291 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
292 section_iterator_callback, &cb_data);
293 *multiple_sections_found = cb_data.multiple_sections_found;
294 return cb_data.found_section;
297 /* Code for handling simple wildcards without going through fnmatch,
298 which can be expensive because of charset translations etc. */
300 /* A simple wild is a literal string followed by a single '*',
301 where the literal part is at least 4 characters long. */
303 static bfd_boolean
304 is_simple_wild (const char *name)
306 size_t len = strcspn (name, "*?[");
307 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
310 static bfd_boolean
311 match_simple_wild (const char *pattern, const char *name)
313 /* The first four characters of the pattern are guaranteed valid
314 non-wildcard characters. So we can go faster. */
315 if (pattern[0] != name[0] || pattern[1] != name[1]
316 || pattern[2] != name[2] || pattern[3] != name[3])
317 return FALSE;
319 pattern += 4;
320 name += 4;
321 while (*pattern != '*')
322 if (*name++ != *pattern++)
323 return FALSE;
325 return TRUE;
328 /* Compare sections ASEC and BSEC according to SORT. */
330 static int
331 compare_section (sort_type sort, asection *asec, asection *bsec)
333 int ret;
335 switch (sort)
337 default:
338 abort ();
340 case by_alignment_name:
341 ret = (bfd_section_alignment (bsec->owner, bsec)
342 - bfd_section_alignment (asec->owner, asec));
343 if (ret)
344 break;
345 /* Fall through. */
347 case by_name:
348 ret = strcmp (bfd_get_section_name (asec->owner, asec),
349 bfd_get_section_name (bsec->owner, bsec));
350 break;
352 case by_name_alignment:
353 ret = strcmp (bfd_get_section_name (asec->owner, asec),
354 bfd_get_section_name (bsec->owner, bsec));
355 if (ret)
356 break;
357 /* Fall through. */
359 case by_alignment:
360 ret = (bfd_section_alignment (bsec->owner, bsec)
361 - bfd_section_alignment (asec->owner, asec));
362 break;
365 return ret;
368 /* Build a Binary Search Tree to sort sections, unlike insertion sort
369 used in wild_sort(). BST is considerably faster if the number of
370 of sections are large. */
372 static lang_section_bst_type **
373 wild_sort_fast (lang_wild_statement_type *wild,
374 struct wildcard_list *sec,
375 lang_input_statement_type *file ATTRIBUTE_UNUSED,
376 asection *section)
378 lang_section_bst_type **tree;
380 tree = (lang_section_bst_type **) &wild->handler_data[1];
381 if (!wild->filenames_sorted
382 && (sec == NULL || sec->spec.sorted == none))
384 /* Append at the right end of tree. */
385 while (*tree)
386 tree = &((*tree)->right);
387 return tree;
390 while (*tree)
392 /* Find the correct node to append this section. */
393 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
394 tree = &((*tree)->left);
395 else
396 tree = &((*tree)->right);
399 return tree;
402 /* Use wild_sort_fast to build a BST to sort sections. */
404 static void
405 output_section_callback_fast (lang_wild_statement_type *ptr,
406 struct wildcard_list *sec,
407 asection *section,
408 lang_input_statement_type *file,
409 void *output ATTRIBUTE_UNUSED)
411 lang_section_bst_type *node;
412 lang_section_bst_type **tree;
414 if (unique_section_p (section))
415 return;
417 node = xmalloc (sizeof (lang_section_bst_type));
418 node->left = 0;
419 node->right = 0;
420 node->section = section;
422 tree = wild_sort_fast (ptr, sec, file, section);
423 if (tree != NULL)
424 *tree = node;
427 /* Convert a sorted sections' BST back to list form. */
429 static void
430 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
431 lang_section_bst_type *tree,
432 void *output)
434 if (tree->left)
435 output_section_callback_tree_to_list (ptr, tree->left, output);
437 lang_add_section (&ptr->children, tree->section,
438 (lang_output_section_statement_type *) output);
440 if (tree->right)
441 output_section_callback_tree_to_list (ptr, tree->right, output);
443 free (tree);
446 /* Specialized, optimized routines for handling different kinds of
447 wildcards */
449 static void
450 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
451 lang_input_statement_type *file,
452 callback_t callback,
453 void *data)
455 /* We can just do a hash lookup for the section with the right name.
456 But if that lookup discovers more than one section with the name
457 (should be rare), we fall back to the general algorithm because
458 we would otherwise have to sort the sections to make sure they
459 get processed in the bfd's order. */
460 bfd_boolean multiple_sections_found;
461 struct wildcard_list *sec0 = ptr->handler_data[0];
462 asection *s0 = find_section (file, sec0, &multiple_sections_found);
464 if (multiple_sections_found)
465 walk_wild_section_general (ptr, file, callback, data);
466 else if (s0)
467 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
470 static void
471 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
472 lang_input_statement_type *file,
473 callback_t callback,
474 void *data)
476 asection *s;
477 struct wildcard_list *wildsec0 = ptr->handler_data[0];
479 for (s = file->the_bfd->sections; s != NULL; s = s->next)
481 const char *sname = bfd_get_section_name (file->the_bfd, s);
482 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
484 if (!skip)
485 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
489 static void
490 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
491 lang_input_statement_type *file,
492 callback_t callback,
493 void *data)
495 asection *s;
496 struct wildcard_list *sec0 = ptr->handler_data[0];
497 struct wildcard_list *wildsec1 = ptr->handler_data[1];
498 bfd_boolean multiple_sections_found;
499 asection *s0 = find_section (file, sec0, &multiple_sections_found);
501 if (multiple_sections_found)
503 walk_wild_section_general (ptr, file, callback, data);
504 return;
507 /* Note that if the section was not found, s0 is NULL and
508 we'll simply never succeed the s == s0 test below. */
509 for (s = file->the_bfd->sections; s != NULL; s = s->next)
511 /* Recall that in this code path, a section cannot satisfy more
512 than one spec, so if s == s0 then it cannot match
513 wildspec1. */
514 if (s == s0)
515 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
516 else
518 const char *sname = bfd_get_section_name (file->the_bfd, s);
519 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
521 if (!skip)
522 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
523 data);
528 static void
529 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
530 lang_input_statement_type *file,
531 callback_t callback,
532 void *data)
534 asection *s;
535 struct wildcard_list *sec0 = ptr->handler_data[0];
536 struct wildcard_list *wildsec1 = ptr->handler_data[1];
537 struct wildcard_list *wildsec2 = ptr->handler_data[2];
538 bfd_boolean multiple_sections_found;
539 asection *s0 = find_section (file, sec0, &multiple_sections_found);
541 if (multiple_sections_found)
543 walk_wild_section_general (ptr, file, callback, data);
544 return;
547 for (s = file->the_bfd->sections; s != NULL; s = s->next)
549 if (s == s0)
550 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
551 else
553 const char *sname = bfd_get_section_name (file->the_bfd, s);
554 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
556 if (!skip)
557 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
558 else
560 skip = !match_simple_wild (wildsec2->spec.name, sname);
561 if (!skip)
562 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
563 data);
569 static void
570 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
571 lang_input_statement_type *file,
572 callback_t callback,
573 void *data)
575 asection *s;
576 struct wildcard_list *sec0 = ptr->handler_data[0];
577 struct wildcard_list *sec1 = ptr->handler_data[1];
578 struct wildcard_list *wildsec2 = ptr->handler_data[2];
579 struct wildcard_list *wildsec3 = ptr->handler_data[3];
580 bfd_boolean multiple_sections_found;
581 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
583 if (multiple_sections_found)
585 walk_wild_section_general (ptr, file, callback, data);
586 return;
589 s1 = find_section (file, sec1, &multiple_sections_found);
590 if (multiple_sections_found)
592 walk_wild_section_general (ptr, file, callback, data);
593 return;
596 for (s = file->the_bfd->sections; s != NULL; s = s->next)
598 if (s == s0)
599 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
600 else
601 if (s == s1)
602 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
603 else
605 const char *sname = bfd_get_section_name (file->the_bfd, s);
606 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
607 sname);
609 if (!skip)
610 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
611 data);
612 else
614 skip = !match_simple_wild (wildsec3->spec.name, sname);
615 if (!skip)
616 walk_wild_consider_section (ptr, file, s, wildsec3,
617 callback, data);
623 static void
624 walk_wild_section (lang_wild_statement_type *ptr,
625 lang_input_statement_type *file,
626 callback_t callback,
627 void *data)
629 if (file->just_syms_flag)
630 return;
632 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
635 /* Returns TRUE when name1 is a wildcard spec that might match
636 something name2 can match. We're conservative: we return FALSE
637 only if the prefixes of name1 and name2 are different up to the
638 first wildcard character. */
640 static bfd_boolean
641 wild_spec_can_overlap (const char *name1, const char *name2)
643 size_t prefix1_len = strcspn (name1, "?*[");
644 size_t prefix2_len = strcspn (name2, "?*[");
645 size_t min_prefix_len;
647 /* Note that if there is no wildcard character, then we treat the
648 terminating 0 as part of the prefix. Thus ".text" won't match
649 ".text." or ".text.*", for example. */
650 if (name1[prefix1_len] == '\0')
651 prefix1_len++;
652 if (name2[prefix2_len] == '\0')
653 prefix2_len++;
655 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
657 return memcmp (name1, name2, min_prefix_len) == 0;
660 /* Select specialized code to handle various kinds of wildcard
661 statements. */
663 static void
664 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
666 int sec_count = 0;
667 int wild_name_count = 0;
668 struct wildcard_list *sec;
669 int signature;
670 int data_counter;
672 ptr->walk_wild_section_handler = walk_wild_section_general;
673 ptr->handler_data[0] = NULL;
674 ptr->handler_data[1] = NULL;
675 ptr->handler_data[2] = NULL;
676 ptr->handler_data[3] = NULL;
678 /* Count how many wildcard_specs there are, and how many of those
679 actually use wildcards in the name. Also, bail out if any of the
680 wildcard names are NULL. (Can this actually happen?
681 walk_wild_section used to test for it.) And bail out if any
682 of the wildcards are more complex than a simple string
683 ending in a single '*'. */
684 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
686 ++sec_count;
687 if (sec->spec.name == NULL)
688 return;
689 if (wildcardp (sec->spec.name))
691 ++wild_name_count;
692 if (!is_simple_wild (sec->spec.name))
693 return;
697 /* The zero-spec case would be easy to optimize but it doesn't
698 happen in practice. Likewise, more than 4 specs doesn't
699 happen in practice. */
700 if (sec_count == 0 || sec_count > 4)
701 return;
703 /* Check that no two specs can match the same section. */
704 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
706 struct wildcard_list *sec2;
707 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
709 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
710 return;
714 signature = (sec_count << 8) + wild_name_count;
715 switch (signature)
717 case 0x0100:
718 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
719 break;
720 case 0x0101:
721 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
722 break;
723 case 0x0201:
724 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
725 break;
726 case 0x0302:
727 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
728 break;
729 case 0x0402:
730 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
731 break;
732 default:
733 return;
736 /* Now fill the data array with pointers to the specs, first the
737 specs with non-wildcard names, then the specs with wildcard
738 names. It's OK to process the specs in different order from the
739 given order, because we've already determined that no section
740 will match more than one spec. */
741 data_counter = 0;
742 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
743 if (!wildcardp (sec->spec.name))
744 ptr->handler_data[data_counter++] = sec;
745 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
746 if (wildcardp (sec->spec.name))
747 ptr->handler_data[data_counter++] = sec;
750 /* Handle a wild statement for a single file F. */
752 static void
753 walk_wild_file (lang_wild_statement_type *s,
754 lang_input_statement_type *f,
755 callback_t callback,
756 void *data)
758 if (f->the_bfd == NULL
759 || ! bfd_check_format (f->the_bfd, bfd_archive))
760 walk_wild_section (s, f, callback, data);
761 else
763 bfd *member;
765 /* This is an archive file. We must map each member of the
766 archive separately. */
767 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
768 while (member != NULL)
770 /* When lookup_name is called, it will call the add_symbols
771 entry point for the archive. For each element of the
772 archive which is included, BFD will call ldlang_add_file,
773 which will set the usrdata field of the member to the
774 lang_input_statement. */
775 if (member->usrdata != NULL)
777 walk_wild_section (s, member->usrdata, callback, data);
780 member = bfd_openr_next_archived_file (f->the_bfd, member);
785 static void
786 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
788 const char *file_spec = s->filename;
790 if (file_spec == NULL)
792 /* Perform the iteration over all files in the list. */
793 LANG_FOR_EACH_INPUT_STATEMENT (f)
795 walk_wild_file (s, f, callback, data);
798 else if (wildcardp (file_spec))
800 LANG_FOR_EACH_INPUT_STATEMENT (f)
802 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
803 walk_wild_file (s, f, callback, data);
806 else
808 lang_input_statement_type *f;
810 /* Perform the iteration over a single file. */
811 f = lookup_name (file_spec);
812 if (f)
813 walk_wild_file (s, f, callback, data);
817 /* lang_for_each_statement walks the parse tree and calls the provided
818 function for each node. */
820 static void
821 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
822 lang_statement_union_type *s)
824 for (; s != NULL; s = s->header.next)
826 func (s);
828 switch (s->header.type)
830 case lang_constructors_statement_enum:
831 lang_for_each_statement_worker (func, constructor_list.head);
832 break;
833 case lang_output_section_statement_enum:
834 lang_for_each_statement_worker
835 (func, s->output_section_statement.children.head);
836 break;
837 case lang_wild_statement_enum:
838 lang_for_each_statement_worker (func,
839 s->wild_statement.children.head);
840 break;
841 case lang_group_statement_enum:
842 lang_for_each_statement_worker (func,
843 s->group_statement.children.head);
844 break;
845 case lang_data_statement_enum:
846 case lang_reloc_statement_enum:
847 case lang_object_symbols_statement_enum:
848 case lang_output_statement_enum:
849 case lang_target_statement_enum:
850 case lang_input_section_enum:
851 case lang_input_statement_enum:
852 case lang_assignment_statement_enum:
853 case lang_padding_statement_enum:
854 case lang_address_statement_enum:
855 case lang_fill_statement_enum:
856 break;
857 default:
858 FAIL ();
859 break;
864 void
865 lang_for_each_statement (void (*func) (lang_statement_union_type *))
867 lang_for_each_statement_worker (func, statement_list.head);
870 /*----------------------------------------------------------------------*/
872 void
873 lang_list_init (lang_statement_list_type *list)
875 list->head = NULL;
876 list->tail = &list->head;
879 /* Build a new statement node for the parse tree. */
881 static lang_statement_union_type *
882 new_statement (enum statement_enum type,
883 size_t size,
884 lang_statement_list_type *list)
886 lang_statement_union_type *new;
888 new = stat_alloc (size);
889 new->header.type = type;
890 new->header.next = NULL;
891 lang_statement_append (list, new, &new->header.next);
892 return new;
895 /* Build a new input file node for the language. There are several
896 ways in which we treat an input file, eg, we only look at symbols,
897 or prefix it with a -l etc.
899 We can be supplied with requests for input files more than once;
900 they may, for example be split over several lines like foo.o(.text)
901 foo.o(.data) etc, so when asked for a file we check that we haven't
902 got it already so we don't duplicate the bfd. */
904 static lang_input_statement_type *
905 new_afile (const char *name,
906 lang_input_file_enum_type file_type,
907 const char *target,
908 bfd_boolean add_to_list)
910 lang_input_statement_type *p;
912 if (add_to_list)
913 p = new_stat (lang_input_statement, stat_ptr);
914 else
916 p = stat_alloc (sizeof (lang_input_statement_type));
917 p->header.type = lang_input_statement_enum;
918 p->header.next = NULL;
921 lang_has_input_file = TRUE;
922 p->target = target;
923 p->sysrooted = FALSE;
924 switch (file_type)
926 case lang_input_file_is_symbols_only_enum:
927 p->filename = name;
928 p->is_archive = FALSE;
929 p->real = TRUE;
930 p->local_sym_name = name;
931 p->just_syms_flag = TRUE;
932 p->search_dirs_flag = FALSE;
933 break;
934 case lang_input_file_is_fake_enum:
935 p->filename = name;
936 p->is_archive = FALSE;
937 p->real = FALSE;
938 p->local_sym_name = name;
939 p->just_syms_flag = FALSE;
940 p->search_dirs_flag = FALSE;
941 break;
942 case lang_input_file_is_l_enum:
943 p->is_archive = TRUE;
944 p->filename = name;
945 p->real = TRUE;
946 p->local_sym_name = concat ("-l", name, NULL);
947 p->just_syms_flag = FALSE;
948 p->search_dirs_flag = TRUE;
949 break;
950 case lang_input_file_is_marker_enum:
951 p->filename = name;
952 p->is_archive = FALSE;
953 p->real = FALSE;
954 p->local_sym_name = name;
955 p->just_syms_flag = FALSE;
956 p->search_dirs_flag = TRUE;
957 break;
958 case lang_input_file_is_search_file_enum:
959 p->sysrooted = ldlang_sysrooted_script;
960 p->filename = name;
961 p->is_archive = FALSE;
962 p->real = TRUE;
963 p->local_sym_name = name;
964 p->just_syms_flag = FALSE;
965 p->search_dirs_flag = TRUE;
966 break;
967 case lang_input_file_is_file_enum:
968 p->filename = name;
969 p->is_archive = FALSE;
970 p->real = TRUE;
971 p->local_sym_name = name;
972 p->just_syms_flag = FALSE;
973 p->search_dirs_flag = FALSE;
974 break;
975 default:
976 FAIL ();
978 p->the_bfd = NULL;
979 p->asymbols = NULL;
980 p->next_real_file = NULL;
981 p->next = NULL;
982 p->symbol_count = 0;
983 p->dynamic = config.dynamic_link;
984 p->add_needed = add_needed;
985 p->as_needed = as_needed;
986 p->whole_archive = whole_archive;
987 p->loaded = FALSE;
988 lang_statement_append (&input_file_chain,
989 (lang_statement_union_type *) p,
990 &p->next_real_file);
991 return p;
994 lang_input_statement_type *
995 lang_add_input_file (const char *name,
996 lang_input_file_enum_type file_type,
997 const char *target)
999 lang_has_input_file = TRUE;
1000 return new_afile (name, file_type, target, TRUE);
1003 struct out_section_hash_entry
1005 struct bfd_hash_entry root;
1006 lang_statement_union_type s;
1009 /* The hash table. */
1011 static struct bfd_hash_table output_section_statement_table;
1013 /* Support routines for the hash table used by lang_output_section_find,
1014 initialize the table, fill in an entry and remove the table. */
1016 static struct bfd_hash_entry *
1017 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1018 struct bfd_hash_table *table,
1019 const char *string)
1021 lang_output_section_statement_type **nextp;
1022 struct out_section_hash_entry *ret;
1024 if (entry == NULL)
1026 entry = bfd_hash_allocate (table, sizeof (*ret));
1027 if (entry == NULL)
1028 return entry;
1031 entry = bfd_hash_newfunc (entry, table, string);
1032 if (entry == NULL)
1033 return entry;
1035 ret = (struct out_section_hash_entry *) entry;
1036 memset (&ret->s, 0, sizeof (ret->s));
1037 ret->s.header.type = lang_output_section_statement_enum;
1038 ret->s.output_section_statement.subsection_alignment = -1;
1039 ret->s.output_section_statement.section_alignment = -1;
1040 ret->s.output_section_statement.block_value = 1;
1041 lang_list_init (&ret->s.output_section_statement.children);
1042 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1044 /* For every output section statement added to the list, except the
1045 first one, lang_output_section_statement.tail points to the "next"
1046 field of the last element of the list. */
1047 if (lang_output_section_statement.head != NULL)
1048 ret->s.output_section_statement.prev
1049 = ((lang_output_section_statement_type *)
1050 ((char *) lang_output_section_statement.tail
1051 - offsetof (lang_output_section_statement_type, next)));
1053 /* GCC's strict aliasing rules prevent us from just casting the
1054 address, so we store the pointer in a variable and cast that
1055 instead. */
1056 nextp = &ret->s.output_section_statement.next;
1057 lang_statement_append (&lang_output_section_statement,
1058 &ret->s,
1059 (lang_statement_union_type **) nextp);
1060 return &ret->root;
1063 static void
1064 output_section_statement_table_init (void)
1066 if (!bfd_hash_table_init_n (&output_section_statement_table,
1067 output_section_statement_newfunc,
1068 sizeof (struct out_section_hash_entry),
1069 61))
1070 einfo (_("%P%F: can not create hash table: %E\n"));
1073 static void
1074 output_section_statement_table_free (void)
1076 bfd_hash_table_free (&output_section_statement_table);
1079 /* Build enough state so that the parser can build its tree. */
1081 void
1082 lang_init (void)
1084 obstack_begin (&stat_obstack, 1000);
1086 stat_ptr = &statement_list;
1088 output_section_statement_table_init ();
1090 lang_list_init (stat_ptr);
1092 lang_list_init (&input_file_chain);
1093 lang_list_init (&lang_output_section_statement);
1094 lang_list_init (&file_chain);
1095 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1096 NULL);
1097 abs_output_section =
1098 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
1100 abs_output_section->bfd_section = bfd_abs_section_ptr;
1102 /* The value "3" is ad-hoc, somewhat related to the expected number of
1103 DEFINED expressions in a linker script. For most default linker
1104 scripts, there are none. Why a hash table then? Well, it's somewhat
1105 simpler to re-use working machinery than using a linked list in terms
1106 of code-complexity here in ld, besides the initialization which just
1107 looks like other code here. */
1108 if (!bfd_hash_table_init_n (&lang_definedness_table,
1109 lang_definedness_newfunc,
1110 sizeof (struct lang_definedness_hash_entry),
1112 einfo (_("%P%F: can not create hash table: %E\n"));
1115 void
1116 lang_finish (void)
1118 output_section_statement_table_free ();
1121 /*----------------------------------------------------------------------
1122 A region is an area of memory declared with the
1123 MEMORY { name:org=exp, len=exp ... }
1124 syntax.
1126 We maintain a list of all the regions here.
1128 If no regions are specified in the script, then the default is used
1129 which is created when looked up to be the entire data space.
1131 If create is true we are creating a region inside a MEMORY block.
1132 In this case it is probably an error to create a region that has
1133 already been created. If we are not inside a MEMORY block it is
1134 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1135 and so we issue a warning. */
1137 static lang_memory_region_type *lang_memory_region_list;
1138 static lang_memory_region_type **lang_memory_region_list_tail
1139 = &lang_memory_region_list;
1141 lang_memory_region_type *
1142 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1144 lang_memory_region_type *p;
1145 lang_memory_region_type *new;
1147 /* NAME is NULL for LMA memspecs if no region was specified. */
1148 if (name == NULL)
1149 return NULL;
1151 for (p = lang_memory_region_list; p != NULL; p = p->next)
1152 if (strcmp (p->name, name) == 0)
1154 if (create)
1155 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1156 name);
1157 return p;
1160 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1161 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
1163 new = stat_alloc (sizeof (lang_memory_region_type));
1165 new->name = xstrdup (name);
1166 new->next = NULL;
1167 new->origin = 0;
1168 new->length = ~(bfd_size_type) 0;
1169 new->current = 0;
1170 new->last_os = NULL;
1171 new->flags = 0;
1172 new->not_flags = 0;
1173 new->had_full_message = FALSE;
1175 *lang_memory_region_list_tail = new;
1176 lang_memory_region_list_tail = &new->next;
1178 return new;
1181 static lang_memory_region_type *
1182 lang_memory_default (asection *section)
1184 lang_memory_region_type *p;
1186 flagword sec_flags = section->flags;
1188 /* Override SEC_DATA to mean a writable section. */
1189 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1190 sec_flags |= SEC_DATA;
1192 for (p = lang_memory_region_list; p != NULL; p = p->next)
1194 if ((p->flags & sec_flags) != 0
1195 && (p->not_flags & sec_flags) == 0)
1197 return p;
1200 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1203 lang_output_section_statement_type *
1204 lang_output_section_find (const char *const name)
1206 struct out_section_hash_entry *entry;
1207 unsigned long hash;
1209 entry = ((struct out_section_hash_entry *)
1210 bfd_hash_lookup (&output_section_statement_table, name,
1211 FALSE, FALSE));
1212 if (entry == NULL)
1213 return NULL;
1215 hash = entry->root.hash;
1218 if (entry->s.output_section_statement.constraint != -1)
1219 return &entry->s.output_section_statement;
1220 entry = (struct out_section_hash_entry *) entry->root.next;
1222 while (entry != NULL
1223 && entry->root.hash == hash
1224 && strcmp (name, entry->s.output_section_statement.name) == 0);
1226 return NULL;
1229 static lang_output_section_statement_type *
1230 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
1232 struct out_section_hash_entry *entry;
1233 struct out_section_hash_entry *last_ent;
1234 unsigned long hash;
1236 entry = ((struct out_section_hash_entry *)
1237 bfd_hash_lookup (&output_section_statement_table, name,
1238 TRUE, FALSE));
1239 if (entry == NULL)
1241 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1242 return NULL;
1245 if (entry->s.output_section_statement.name != NULL)
1247 /* We have a section of this name, but it might not have the correct
1248 constraint. */
1249 hash = entry->root.hash;
1252 if (entry->s.output_section_statement.constraint != -1
1253 && (constraint == 0
1254 || (constraint == entry->s.output_section_statement.constraint
1255 && constraint != SPECIAL)))
1256 return &entry->s.output_section_statement;
1257 last_ent = entry;
1258 entry = (struct out_section_hash_entry *) entry->root.next;
1260 while (entry != NULL
1261 && entry->root.hash == hash
1262 && strcmp (name, entry->s.output_section_statement.name) == 0);
1264 entry
1265 = ((struct out_section_hash_entry *)
1266 output_section_statement_newfunc (NULL,
1267 &output_section_statement_table,
1268 name));
1269 if (entry == NULL)
1271 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1272 return NULL;
1274 entry->root = last_ent->root;
1275 last_ent->root.next = &entry->root;
1278 entry->s.output_section_statement.name = name;
1279 entry->s.output_section_statement.constraint = constraint;
1280 return &entry->s.output_section_statement;
1283 lang_output_section_statement_type *
1284 lang_output_section_statement_lookup (const char *const name)
1286 return lang_output_section_statement_lookup_1 (name, 0);
1289 /* A variant of lang_output_section_find used by place_orphan.
1290 Returns the output statement that should precede a new output
1291 statement for SEC. If an exact match is found on certain flags,
1292 sets *EXACT too. */
1294 lang_output_section_statement_type *
1295 lang_output_section_find_by_flags (const asection *sec,
1296 lang_output_section_statement_type **exact,
1297 lang_match_sec_type_func match_type)
1299 lang_output_section_statement_type *first, *look, *found;
1300 flagword flags;
1302 /* We know the first statement on this list is *ABS*. May as well
1303 skip it. */
1304 first = &lang_output_section_statement.head->output_section_statement;
1305 first = first->next;
1307 /* First try for an exact match. */
1308 found = NULL;
1309 for (look = first; look; look = look->next)
1311 flags = look->flags;
1312 if (look->bfd_section != NULL)
1314 flags = look->bfd_section->flags;
1315 if (match_type && !match_type (output_bfd, look->bfd_section,
1316 sec->owner, sec))
1317 continue;
1319 flags ^= sec->flags;
1320 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1321 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1322 found = look;
1324 if (found != NULL)
1326 if (exact != NULL)
1327 *exact = found;
1328 return found;
1331 if (sec->flags & SEC_CODE)
1333 /* Try for a rw code section. */
1334 for (look = first; look; look = look->next)
1336 flags = look->flags;
1337 if (look->bfd_section != NULL)
1339 flags = look->bfd_section->flags;
1340 if (match_type && !match_type (output_bfd, look->bfd_section,
1341 sec->owner, sec))
1342 continue;
1344 flags ^= sec->flags;
1345 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1346 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1347 found = look;
1350 else if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
1352 /* .rodata can go after .text, .sdata2 after .rodata. */
1353 for (look = first; look; look = look->next)
1355 flags = look->flags;
1356 if (look->bfd_section != NULL)
1358 flags = look->bfd_section->flags;
1359 if (match_type && !match_type (output_bfd, look->bfd_section,
1360 sec->owner, sec))
1361 continue;
1363 flags ^= sec->flags;
1364 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1365 | SEC_READONLY))
1366 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1367 found = look;
1370 else if (sec->flags & SEC_SMALL_DATA)
1372 /* .sdata goes after .data, .sbss after .sdata. */
1373 for (look = first; look; look = look->next)
1375 flags = look->flags;
1376 if (look->bfd_section != NULL)
1378 flags = look->bfd_section->flags;
1379 if (match_type && !match_type (output_bfd, look->bfd_section,
1380 sec->owner, sec))
1381 continue;
1383 flags ^= sec->flags;
1384 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1385 | SEC_THREAD_LOCAL))
1386 || ((look->flags & SEC_SMALL_DATA)
1387 && !(sec->flags & SEC_HAS_CONTENTS)))
1388 found = look;
1391 else if (sec->flags & SEC_HAS_CONTENTS)
1393 /* .data goes after .rodata. */
1394 for (look = first; look; look = look->next)
1396 flags = look->flags;
1397 if (look->bfd_section != NULL)
1399 flags = look->bfd_section->flags;
1400 if (match_type && !match_type (output_bfd, look->bfd_section,
1401 sec->owner, sec))
1402 continue;
1404 flags ^= sec->flags;
1405 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1406 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1407 found = look;
1410 else
1412 /* .bss goes last. */
1413 for (look = first; look; look = look->next)
1415 flags = look->flags;
1416 if (look->bfd_section != NULL)
1418 flags = look->bfd_section->flags;
1419 if (match_type && !match_type (output_bfd, look->bfd_section,
1420 sec->owner, sec))
1421 continue;
1423 flags ^= sec->flags;
1424 if (!(flags & SEC_ALLOC))
1425 found = look;
1429 if (found || !match_type)
1430 return found;
1432 return lang_output_section_find_by_flags (sec, NULL, NULL);
1435 /* Find the last output section before given output statement.
1436 Used by place_orphan. */
1438 static asection *
1439 output_prev_sec_find (lang_output_section_statement_type *os)
1441 lang_output_section_statement_type *lookup;
1443 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1445 if (lookup->constraint == -1)
1446 continue;
1448 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1449 return lookup->bfd_section;
1452 return NULL;
1455 lang_output_section_statement_type *
1456 lang_insert_orphan (asection *s,
1457 const char *secname,
1458 lang_output_section_statement_type *after,
1459 struct orphan_save *place,
1460 etree_type *address,
1461 lang_statement_list_type *add_child)
1463 lang_statement_list_type *old;
1464 lang_statement_list_type add;
1465 const char *ps;
1466 lang_output_section_statement_type *os;
1467 lang_output_section_statement_type **os_tail;
1469 /* Start building a list of statements for this section.
1470 First save the current statement pointer. */
1471 old = stat_ptr;
1473 /* If we have found an appropriate place for the output section
1474 statements for this orphan, add them to our own private list,
1475 inserting them later into the global statement list. */
1476 if (after != NULL)
1478 stat_ptr = &add;
1479 lang_list_init (stat_ptr);
1482 ps = NULL;
1483 if (config.build_constructors)
1485 /* If the name of the section is representable in C, then create
1486 symbols to mark the start and the end of the section. */
1487 for (ps = secname; *ps != '\0'; ps++)
1488 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1489 break;
1490 if (*ps == '\0')
1492 char *symname;
1493 etree_type *e_align;
1495 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1496 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1497 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1498 e_align = exp_unop (ALIGN_K,
1499 exp_intop ((bfd_vma) 1 << s->alignment_power));
1500 lang_add_assignment (exp_assop ('=', ".", e_align));
1501 lang_add_assignment (exp_assop ('=', symname,
1502 exp_nameop (NAME, ".")));
1506 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1507 address = exp_intop (0);
1509 os_tail = ((lang_output_section_statement_type **)
1510 lang_output_section_statement.tail);
1511 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1512 NULL, 0);
1514 if (add_child == NULL)
1515 add_child = &os->children;
1516 lang_add_section (add_child, s, os);
1518 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1520 if (config.build_constructors && *ps == '\0')
1522 char *symname;
1524 /* lang_leave_ouput_section_statement resets stat_ptr.
1525 Put stat_ptr back where we want it. */
1526 if (after != NULL)
1527 stat_ptr = &add;
1529 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1530 symname[0] = bfd_get_symbol_leading_char (output_bfd);
1531 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1532 lang_add_assignment (exp_assop ('=', symname,
1533 exp_nameop (NAME, ".")));
1536 /* Restore the global list pointer. */
1537 if (after != NULL)
1538 stat_ptr = old;
1540 if (after != NULL && os->bfd_section != NULL)
1542 asection *snew, *as;
1544 snew = os->bfd_section;
1546 /* Shuffle the bfd section list to make the output file look
1547 neater. This is really only cosmetic. */
1548 if (place->section == NULL
1549 && after != (&lang_output_section_statement.head
1550 ->output_section_statement))
1552 asection *bfd_section = after->bfd_section;
1554 /* If the output statement hasn't been used to place any input
1555 sections (and thus doesn't have an output bfd_section),
1556 look for the closest prior output statement having an
1557 output section. */
1558 if (bfd_section == NULL)
1559 bfd_section = output_prev_sec_find (after);
1561 if (bfd_section != NULL && bfd_section != snew)
1562 place->section = &bfd_section->next;
1565 if (place->section == NULL)
1566 place->section = &output_bfd->sections;
1568 as = *place->section;
1570 if (!as)
1572 /* Put the section at the end of the list. */
1574 /* Unlink the section. */
1575 bfd_section_list_remove (output_bfd, snew);
1577 /* Now tack it back on in the right place. */
1578 bfd_section_list_append (output_bfd, snew);
1580 else if (as != snew && as->prev != snew)
1582 /* Unlink the section. */
1583 bfd_section_list_remove (output_bfd, snew);
1585 /* Now tack it back on in the right place. */
1586 bfd_section_list_insert_before (output_bfd, as, snew);
1589 /* Save the end of this list. Further ophans of this type will
1590 follow the one we've just added. */
1591 place->section = &snew->next;
1593 /* The following is non-cosmetic. We try to put the output
1594 statements in some sort of reasonable order here, because they
1595 determine the final load addresses of the orphan sections.
1596 In addition, placing output statements in the wrong order may
1597 require extra segments. For instance, given a typical
1598 situation of all read-only sections placed in one segment and
1599 following that a segment containing all the read-write
1600 sections, we wouldn't want to place an orphan read/write
1601 section before or amongst the read-only ones. */
1602 if (add.head != NULL)
1604 lang_output_section_statement_type *newly_added_os;
1606 if (place->stmt == NULL)
1608 lang_statement_union_type **where;
1609 lang_statement_union_type **assign = NULL;
1610 bfd_boolean ignore_first;
1612 /* Look for a suitable place for the new statement list.
1613 The idea is to skip over anything that might be inside
1614 a SECTIONS {} statement in a script, before we find
1615 another output_section_statement. Assignments to "dot"
1616 before an output section statement are assumed to
1617 belong to it. An exception to this rule is made for
1618 the first assignment to dot, otherwise we might put an
1619 orphan before . = . + SIZEOF_HEADERS or similar
1620 assignments that set the initial address. */
1622 ignore_first = after == (&lang_output_section_statement.head
1623 ->output_section_statement);
1624 for (where = &after->header.next;
1625 *where != NULL;
1626 where = &(*where)->header.next)
1628 switch ((*where)->header.type)
1630 case lang_assignment_statement_enum:
1631 if (assign == NULL)
1633 lang_assignment_statement_type *ass;
1634 ass = &(*where)->assignment_statement;
1635 if (ass->exp->type.node_class != etree_assert
1636 && ass->exp->assign.dst[0] == '.'
1637 && ass->exp->assign.dst[1] == 0
1638 && !ignore_first)
1639 assign = where;
1641 ignore_first = FALSE;
1642 continue;
1643 case lang_wild_statement_enum:
1644 case lang_input_section_enum:
1645 case lang_object_symbols_statement_enum:
1646 case lang_fill_statement_enum:
1647 case lang_data_statement_enum:
1648 case lang_reloc_statement_enum:
1649 case lang_padding_statement_enum:
1650 case lang_constructors_statement_enum:
1651 assign = NULL;
1652 continue;
1653 case lang_output_section_statement_enum:
1654 if (assign != NULL)
1655 where = assign;
1656 case lang_input_statement_enum:
1657 case lang_address_statement_enum:
1658 case lang_target_statement_enum:
1659 case lang_output_statement_enum:
1660 case lang_group_statement_enum:
1661 case lang_afile_asection_pair_statement_enum:
1662 break;
1664 break;
1667 *add.tail = *where;
1668 *where = add.head;
1670 place->os_tail = &after->next;
1672 else
1674 /* Put it after the last orphan statement we added. */
1675 *add.tail = *place->stmt;
1676 *place->stmt = add.head;
1679 /* Fix the global list pointer if we happened to tack our
1680 new list at the tail. */
1681 if (*old->tail == add.head)
1682 old->tail = add.tail;
1684 /* Save the end of this list. */
1685 place->stmt = add.tail;
1687 /* Do the same for the list of output section statements. */
1688 newly_added_os = *os_tail;
1689 *os_tail = NULL;
1690 newly_added_os->prev = (lang_output_section_statement_type *)
1691 ((char *) place->os_tail
1692 - offsetof (lang_output_section_statement_type, next));
1693 newly_added_os->next = *place->os_tail;
1694 if (newly_added_os->next != NULL)
1695 newly_added_os->next->prev = newly_added_os;
1696 *place->os_tail = newly_added_os;
1697 place->os_tail = &newly_added_os->next;
1699 /* Fixing the global list pointer here is a little different.
1700 We added to the list in lang_enter_output_section_statement,
1701 trimmed off the new output_section_statment above when
1702 assigning *os_tail = NULL, but possibly added it back in
1703 the same place when assigning *place->os_tail. */
1704 if (*os_tail == NULL)
1705 lang_output_section_statement.tail
1706 = (lang_statement_union_type **) os_tail;
1709 return os;
1712 static void
1713 lang_map_flags (flagword flag)
1715 if (flag & SEC_ALLOC)
1716 minfo ("a");
1718 if (flag & SEC_CODE)
1719 minfo ("x");
1721 if (flag & SEC_READONLY)
1722 minfo ("r");
1724 if (flag & SEC_DATA)
1725 minfo ("w");
1727 if (flag & SEC_LOAD)
1728 minfo ("l");
1731 void
1732 lang_map (void)
1734 lang_memory_region_type *m;
1735 bfd_boolean dis_header_printed = FALSE;
1736 bfd *p;
1738 LANG_FOR_EACH_INPUT_STATEMENT (file)
1740 asection *s;
1742 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1743 || file->just_syms_flag)
1744 continue;
1746 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1747 if (s->output_section == NULL
1748 || s->output_section->owner != output_bfd)
1750 if (! dis_header_printed)
1752 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1753 dis_header_printed = TRUE;
1756 print_input_section (s);
1760 minfo (_("\nMemory Configuration\n\n"));
1761 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1762 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1764 for (m = lang_memory_region_list; m != NULL; m = m->next)
1766 char buf[100];
1767 int len;
1769 fprintf (config.map_file, "%-16s ", m->name);
1771 sprintf_vma (buf, m->origin);
1772 minfo ("0x%s ", buf);
1773 len = strlen (buf);
1774 while (len < 16)
1776 print_space ();
1777 ++len;
1780 minfo ("0x%V", m->length);
1781 if (m->flags || m->not_flags)
1783 #ifndef BFD64
1784 minfo (" ");
1785 #endif
1786 if (m->flags)
1788 print_space ();
1789 lang_map_flags (m->flags);
1792 if (m->not_flags)
1794 minfo (" !");
1795 lang_map_flags (m->not_flags);
1799 print_nl ();
1802 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1804 if (! command_line.reduce_memory_overheads)
1806 obstack_begin (&map_obstack, 1000);
1807 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1808 bfd_map_over_sections (p, init_map_userdata, 0);
1809 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1811 print_statements ();
1814 static void
1815 init_map_userdata (abfd, sec, data)
1816 bfd *abfd ATTRIBUTE_UNUSED;
1817 asection *sec;
1818 void *data ATTRIBUTE_UNUSED;
1820 fat_section_userdata_type *new_data
1821 = ((fat_section_userdata_type *) (stat_alloc
1822 (sizeof (fat_section_userdata_type))));
1824 ASSERT (get_userdata (sec) == NULL);
1825 get_userdata (sec) = new_data;
1826 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1829 static bfd_boolean
1830 sort_def_symbol (hash_entry, info)
1831 struct bfd_link_hash_entry *hash_entry;
1832 void *info ATTRIBUTE_UNUSED;
1834 if (hash_entry->type == bfd_link_hash_defined
1835 || hash_entry->type == bfd_link_hash_defweak)
1837 struct fat_user_section_struct *ud;
1838 struct map_symbol_def *def;
1840 ud = get_userdata (hash_entry->u.def.section);
1841 if (! ud)
1843 /* ??? What do we have to do to initialize this beforehand? */
1844 /* The first time we get here is bfd_abs_section... */
1845 init_map_userdata (0, hash_entry->u.def.section, 0);
1846 ud = get_userdata (hash_entry->u.def.section);
1848 else if (!ud->map_symbol_def_tail)
1849 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1851 def = obstack_alloc (&map_obstack, sizeof *def);
1852 def->entry = hash_entry;
1853 *(ud->map_symbol_def_tail) = def;
1854 ud->map_symbol_def_tail = &def->next;
1856 return TRUE;
1859 /* Initialize an output section. */
1861 static void
1862 init_os (lang_output_section_statement_type *s, asection *isec,
1863 flagword flags)
1865 if (s->bfd_section != NULL)
1866 return;
1868 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1869 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1871 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1872 if (s->bfd_section == NULL)
1873 s->bfd_section = bfd_make_section_with_flags (output_bfd, s->name,
1874 flags);
1875 if (s->bfd_section == NULL)
1877 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1878 output_bfd->xvec->name, s->name);
1880 s->bfd_section->output_section = s->bfd_section;
1881 s->bfd_section->output_offset = 0;
1883 if (!command_line.reduce_memory_overheads)
1885 fat_section_userdata_type *new
1886 = stat_alloc (sizeof (fat_section_userdata_type));
1887 memset (new, 0, sizeof (fat_section_userdata_type));
1888 get_userdata (s->bfd_section) = new;
1891 /* If there is a base address, make sure that any sections it might
1892 mention are initialized. */
1893 if (s->addr_tree != NULL)
1894 exp_init_os (s->addr_tree);
1896 if (s->load_base != NULL)
1897 exp_init_os (s->load_base);
1899 /* If supplied an alignment, set it. */
1900 if (s->section_alignment != -1)
1901 s->bfd_section->alignment_power = s->section_alignment;
1903 if (isec)
1904 bfd_init_private_section_data (isec->owner, isec,
1905 output_bfd, s->bfd_section,
1906 &link_info);
1909 /* Make sure that all output sections mentioned in an expression are
1910 initialized. */
1912 static void
1913 exp_init_os (etree_type *exp)
1915 switch (exp->type.node_class)
1917 case etree_assign:
1918 case etree_provide:
1919 exp_init_os (exp->assign.src);
1920 break;
1922 case etree_binary:
1923 exp_init_os (exp->binary.lhs);
1924 exp_init_os (exp->binary.rhs);
1925 break;
1927 case etree_trinary:
1928 exp_init_os (exp->trinary.cond);
1929 exp_init_os (exp->trinary.lhs);
1930 exp_init_os (exp->trinary.rhs);
1931 break;
1933 case etree_assert:
1934 exp_init_os (exp->assert_s.child);
1935 break;
1937 case etree_unary:
1938 exp_init_os (exp->unary.child);
1939 break;
1941 case etree_name:
1942 switch (exp->type.node_code)
1944 case ADDR:
1945 case LOADADDR:
1946 case SIZEOF:
1948 lang_output_section_statement_type *os;
1950 os = lang_output_section_find (exp->name.name);
1951 if (os != NULL && os->bfd_section == NULL)
1952 init_os (os, NULL, 0);
1955 break;
1957 default:
1958 break;
1962 static void
1963 section_already_linked (bfd *abfd, asection *sec, void *data)
1965 lang_input_statement_type *entry = data;
1967 /* If we are only reading symbols from this object, then we want to
1968 discard all sections. */
1969 if (entry->just_syms_flag)
1971 bfd_link_just_syms (abfd, sec, &link_info);
1972 return;
1975 if (!(abfd->flags & DYNAMIC))
1976 bfd_section_already_linked (abfd, sec);
1979 /* The wild routines.
1981 These expand statements like *(.text) and foo.o to a list of
1982 explicit actions, like foo.o(.text), bar.o(.text) and
1983 foo.o(.text, .data). */
1985 /* Add SECTION to the output section OUTPUT. Do this by creating a
1986 lang_input_section statement which is placed at PTR. FILE is the
1987 input file which holds SECTION. */
1989 void
1990 lang_add_section (lang_statement_list_type *ptr,
1991 asection *section,
1992 lang_output_section_statement_type *output)
1994 flagword flags = section->flags;
1995 bfd_boolean discard;
1997 /* Discard sections marked with SEC_EXCLUDE. */
1998 discard = (flags & SEC_EXCLUDE) != 0;
2000 /* Discard input sections which are assigned to a section named
2001 DISCARD_SECTION_NAME. */
2002 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2003 discard = TRUE;
2005 /* Discard debugging sections if we are stripping debugging
2006 information. */
2007 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2008 && (flags & SEC_DEBUGGING) != 0)
2009 discard = TRUE;
2011 if (discard)
2013 if (section->output_section == NULL)
2015 /* This prevents future calls from assigning this section. */
2016 section->output_section = bfd_abs_section_ptr;
2018 return;
2021 if (section->output_section == NULL)
2023 bfd_boolean first;
2024 lang_input_section_type *new;
2025 flagword flags;
2027 flags = section->flags;
2029 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2030 to an output section, because we want to be able to include a
2031 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2032 section (I don't know why we want to do this, but we do).
2033 build_link_order in ldwrite.c handles this case by turning
2034 the embedded SEC_NEVER_LOAD section into a fill. */
2036 flags &= ~ SEC_NEVER_LOAD;
2038 switch (output->sectype)
2040 case normal_section:
2041 break;
2042 case noalloc_section:
2043 flags &= ~SEC_ALLOC;
2044 break;
2045 case noload_section:
2046 flags &= ~SEC_LOAD;
2047 flags |= SEC_NEVER_LOAD;
2048 break;
2051 if (output->bfd_section == NULL)
2052 init_os (output, section, flags);
2054 first = ! output->bfd_section->linker_has_input;
2055 output->bfd_section->linker_has_input = 1;
2057 if (!link_info.relocatable
2058 && !stripped_excluded_sections)
2060 asection *s = output->bfd_section->map_tail.s;
2061 output->bfd_section->map_tail.s = section;
2062 section->map_head.s = NULL;
2063 section->map_tail.s = s;
2064 if (s != NULL)
2065 s->map_head.s = section;
2066 else
2067 output->bfd_section->map_head.s = section;
2070 /* Add a section reference to the list. */
2071 new = new_stat (lang_input_section, ptr);
2073 new->section = section;
2074 section->output_section = output->bfd_section;
2076 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2077 already been processed. One reason to do this is that on pe
2078 format targets, .text$foo sections go into .text and it's odd
2079 to see .text with SEC_LINK_ONCE set. */
2081 if (! link_info.relocatable)
2082 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2084 /* If this is not the first input section, and the SEC_READONLY
2085 flag is not currently set, then don't set it just because the
2086 input section has it set. */
2088 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2089 flags &= ~ SEC_READONLY;
2091 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2092 if (! first
2093 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2094 != (flags & (SEC_MERGE | SEC_STRINGS))
2095 || ((flags & SEC_MERGE)
2096 && output->bfd_section->entsize != section->entsize)))
2098 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2099 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2102 output->bfd_section->flags |= flags;
2104 if (flags & SEC_MERGE)
2105 output->bfd_section->entsize = section->entsize;
2107 /* If SEC_READONLY is not set in the input section, then clear
2108 it from the output section. */
2109 if ((section->flags & SEC_READONLY) == 0)
2110 output->bfd_section->flags &= ~SEC_READONLY;
2112 /* Copy over SEC_SMALL_DATA. */
2113 if (section->flags & SEC_SMALL_DATA)
2114 output->bfd_section->flags |= SEC_SMALL_DATA;
2116 if (section->alignment_power > output->bfd_section->alignment_power)
2117 output->bfd_section->alignment_power = section->alignment_power;
2119 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2120 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2122 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2123 /* FIXME: This value should really be obtained from the bfd... */
2124 output->block_value = 128;
2129 /* Handle wildcard sorting. This returns the lang_input_section which
2130 should follow the one we are going to create for SECTION and FILE,
2131 based on the sorting requirements of WILD. It returns NULL if the
2132 new section should just go at the end of the current list. */
2134 static lang_statement_union_type *
2135 wild_sort (lang_wild_statement_type *wild,
2136 struct wildcard_list *sec,
2137 lang_input_statement_type *file,
2138 asection *section)
2140 const char *section_name;
2141 lang_statement_union_type *l;
2143 if (!wild->filenames_sorted
2144 && (sec == NULL || sec->spec.sorted == none))
2145 return NULL;
2147 section_name = bfd_get_section_name (file->the_bfd, section);
2148 for (l = wild->children.head; l != NULL; l = l->header.next)
2150 lang_input_section_type *ls;
2152 if (l->header.type != lang_input_section_enum)
2153 continue;
2154 ls = &l->input_section;
2156 /* Sorting by filename takes precedence over sorting by section
2157 name. */
2159 if (wild->filenames_sorted)
2161 const char *fn, *ln;
2162 bfd_boolean fa, la;
2163 int i;
2165 /* The PE support for the .idata section as generated by
2166 dlltool assumes that files will be sorted by the name of
2167 the archive and then the name of the file within the
2168 archive. */
2170 if (file->the_bfd != NULL
2171 && bfd_my_archive (file->the_bfd) != NULL)
2173 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2174 fa = TRUE;
2176 else
2178 fn = file->filename;
2179 fa = FALSE;
2182 if (bfd_my_archive (ls->section->owner) != NULL)
2184 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2185 la = TRUE;
2187 else
2189 ln = ls->section->owner->filename;
2190 la = FALSE;
2193 i = strcmp (fn, ln);
2194 if (i > 0)
2195 continue;
2196 else if (i < 0)
2197 break;
2199 if (fa || la)
2201 if (fa)
2202 fn = file->filename;
2203 if (la)
2204 ln = ls->section->owner->filename;
2206 i = strcmp (fn, ln);
2207 if (i > 0)
2208 continue;
2209 else if (i < 0)
2210 break;
2214 /* Here either the files are not sorted by name, or we are
2215 looking at the sections for this file. */
2217 if (sec != NULL && sec->spec.sorted != none)
2218 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2219 break;
2222 return l;
2225 /* Expand a wild statement for a particular FILE. SECTION may be
2226 NULL, in which case it is a wild card. */
2228 static void
2229 output_section_callback (lang_wild_statement_type *ptr,
2230 struct wildcard_list *sec,
2231 asection *section,
2232 lang_input_statement_type *file,
2233 void *output)
2235 lang_statement_union_type *before;
2237 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2238 if (unique_section_p (section))
2239 return;
2241 before = wild_sort (ptr, sec, file, section);
2243 /* Here BEFORE points to the lang_input_section which
2244 should follow the one we are about to add. If BEFORE
2245 is NULL, then the section should just go at the end
2246 of the current list. */
2248 if (before == NULL)
2249 lang_add_section (&ptr->children, section,
2250 (lang_output_section_statement_type *) output);
2251 else
2253 lang_statement_list_type list;
2254 lang_statement_union_type **pp;
2256 lang_list_init (&list);
2257 lang_add_section (&list, section,
2258 (lang_output_section_statement_type *) output);
2260 /* If we are discarding the section, LIST.HEAD will
2261 be NULL. */
2262 if (list.head != NULL)
2264 ASSERT (list.head->header.next == NULL);
2266 for (pp = &ptr->children.head;
2267 *pp != before;
2268 pp = &(*pp)->header.next)
2269 ASSERT (*pp != NULL);
2271 list.head->header.next = *pp;
2272 *pp = list.head;
2277 /* Check if all sections in a wild statement for a particular FILE
2278 are readonly. */
2280 static void
2281 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2282 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2283 asection *section,
2284 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2285 void *data)
2287 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2288 if (unique_section_p (section))
2289 return;
2291 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2292 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2295 /* This is passed a file name which must have been seen already and
2296 added to the statement tree. We will see if it has been opened
2297 already and had its symbols read. If not then we'll read it. */
2299 static lang_input_statement_type *
2300 lookup_name (const char *name)
2302 lang_input_statement_type *search;
2304 for (search = (lang_input_statement_type *) input_file_chain.head;
2305 search != NULL;
2306 search = (lang_input_statement_type *) search->next_real_file)
2308 /* Use the local_sym_name as the name of the file that has
2309 already been loaded as filename might have been transformed
2310 via the search directory lookup mechanism. */
2311 const char *filename = search->local_sym_name;
2313 if (filename != NULL
2314 && strcmp (filename, name) == 0)
2315 break;
2318 if (search == NULL)
2319 search = new_afile (name, lang_input_file_is_search_file_enum,
2320 default_target, FALSE);
2322 /* If we have already added this file, or this file is not real
2323 don't add this file. */
2324 if (search->loaded || !search->real)
2325 return search;
2327 if (! load_symbols (search, NULL))
2328 return NULL;
2330 return search;
2333 /* Save LIST as a list of libraries whose symbols should not be exported. */
2335 struct excluded_lib
2337 char *name;
2338 struct excluded_lib *next;
2340 static struct excluded_lib *excluded_libs;
2342 void
2343 add_excluded_libs (const char *list)
2345 const char *p = list, *end;
2347 while (*p != '\0')
2349 struct excluded_lib *entry;
2350 end = strpbrk (p, ",:");
2351 if (end == NULL)
2352 end = p + strlen (p);
2353 entry = xmalloc (sizeof (*entry));
2354 entry->next = excluded_libs;
2355 entry->name = xmalloc (end - p + 1);
2356 memcpy (entry->name, p, end - p);
2357 entry->name[end - p] = '\0';
2358 excluded_libs = entry;
2359 if (*end == '\0')
2360 break;
2361 p = end + 1;
2365 static void
2366 check_excluded_libs (bfd *abfd)
2368 struct excluded_lib *lib = excluded_libs;
2370 while (lib)
2372 int len = strlen (lib->name);
2373 const char *filename = lbasename (abfd->filename);
2375 if (strcmp (lib->name, "ALL") == 0)
2377 abfd->no_export = TRUE;
2378 return;
2381 if (strncmp (lib->name, filename, len) == 0
2382 && (filename[len] == '\0'
2383 || (filename[len] == '.' && filename[len + 1] == 'a'
2384 && filename[len + 2] == '\0')))
2386 abfd->no_export = TRUE;
2387 return;
2390 lib = lib->next;
2394 /* Get the symbols for an input file. */
2396 static bfd_boolean
2397 load_symbols (lang_input_statement_type *entry,
2398 lang_statement_list_type *place)
2400 char **matching;
2402 if (entry->loaded)
2403 return TRUE;
2405 ldfile_open_file (entry);
2407 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2408 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2410 bfd_error_type err;
2411 lang_statement_list_type *hold;
2412 bfd_boolean bad_load = TRUE;
2413 bfd_boolean save_ldlang_sysrooted_script;
2414 bfd_boolean save_as_needed, save_add_needed;
2416 err = bfd_get_error ();
2418 /* See if the emulation has some special knowledge. */
2419 if (ldemul_unrecognized_file (entry))
2420 return TRUE;
2422 if (err == bfd_error_file_ambiguously_recognized)
2424 char **p;
2426 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2427 einfo (_("%B: matching formats:"), entry->the_bfd);
2428 for (p = matching; *p != NULL; p++)
2429 einfo (" %s", *p);
2430 einfo ("%F\n");
2432 else if (err != bfd_error_file_not_recognized
2433 || place == NULL)
2434 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2435 else
2436 bad_load = FALSE;
2438 bfd_close (entry->the_bfd);
2439 entry->the_bfd = NULL;
2441 /* Try to interpret the file as a linker script. */
2442 ldfile_open_command_file (entry->filename);
2444 hold = stat_ptr;
2445 stat_ptr = place;
2446 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2447 ldlang_sysrooted_script = entry->sysrooted;
2448 save_as_needed = as_needed;
2449 as_needed = entry->as_needed;
2450 save_add_needed = add_needed;
2451 add_needed = entry->add_needed;
2453 ldfile_assumed_script = TRUE;
2454 parser_input = input_script;
2455 /* We want to use the same -Bdynamic/-Bstatic as the one for
2456 ENTRY. */
2457 config.dynamic_link = entry->dynamic;
2458 yyparse ();
2459 ldfile_assumed_script = FALSE;
2461 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2462 as_needed = save_as_needed;
2463 add_needed = save_add_needed;
2464 stat_ptr = hold;
2466 return ! bad_load;
2469 if (ldemul_recognized_file (entry))
2470 return TRUE;
2472 /* We don't call ldlang_add_file for an archive. Instead, the
2473 add_symbols entry point will call ldlang_add_file, via the
2474 add_archive_element callback, for each element of the archive
2475 which is used. */
2476 switch (bfd_get_format (entry->the_bfd))
2478 default:
2479 break;
2481 case bfd_object:
2482 ldlang_add_file (entry);
2483 if (trace_files || trace_file_tries)
2484 info_msg ("%I\n", entry);
2485 break;
2487 case bfd_archive:
2488 check_excluded_libs (entry->the_bfd);
2490 if (entry->whole_archive)
2492 bfd *member = NULL;
2493 bfd_boolean loaded = TRUE;
2495 for (;;)
2497 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2499 if (member == NULL)
2500 break;
2502 if (! bfd_check_format (member, bfd_object))
2504 einfo (_("%F%B: member %B in archive is not an object\n"),
2505 entry->the_bfd, member);
2506 loaded = FALSE;
2509 if (! ((*link_info.callbacks->add_archive_element)
2510 (&link_info, member, "--whole-archive")))
2511 abort ();
2513 if (! bfd_link_add_symbols (member, &link_info))
2515 einfo (_("%F%B: could not read symbols: %E\n"), member);
2516 loaded = FALSE;
2520 entry->loaded = loaded;
2521 return loaded;
2523 break;
2526 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2527 entry->loaded = TRUE;
2528 else
2529 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2531 return entry->loaded;
2534 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2535 may be NULL, indicating that it is a wildcard. Separate
2536 lang_input_section statements are created for each part of the
2537 expansion; they are added after the wild statement S. OUTPUT is
2538 the output section. */
2540 static void
2541 wild (lang_wild_statement_type *s,
2542 const char *target ATTRIBUTE_UNUSED,
2543 lang_output_section_statement_type *output)
2545 struct wildcard_list *sec;
2547 if (s->handler_data[0]
2548 && s->handler_data[0]->spec.sorted == by_name
2549 && !s->filenames_sorted)
2551 lang_section_bst_type *tree;
2553 walk_wild (s, output_section_callback_fast, output);
2555 tree = (lang_section_bst_type *) s->handler_data[1];
2556 if (tree)
2557 output_section_callback_tree_to_list (s, tree, output);
2558 s->handler_data[1] = NULL;
2560 else
2561 walk_wild (s, output_section_callback, output);
2563 if (default_common_section == NULL)
2564 for (sec = s->section_list; sec != NULL; sec = sec->next)
2565 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2567 /* Remember the section that common is going to in case we
2568 later get something which doesn't know where to put it. */
2569 default_common_section = output;
2570 break;
2574 /* Return TRUE iff target is the sought target. */
2576 static int
2577 get_target (const bfd_target *target, void *data)
2579 const char *sought = data;
2581 return strcmp (target->name, sought) == 0;
2584 /* Like strcpy() but convert to lower case as well. */
2586 static void
2587 stricpy (char *dest, char *src)
2589 char c;
2591 while ((c = *src++) != 0)
2592 *dest++ = TOLOWER (c);
2594 *dest = 0;
2597 /* Remove the first occurrence of needle (if any) in haystack
2598 from haystack. */
2600 static void
2601 strcut (char *haystack, char *needle)
2603 haystack = strstr (haystack, needle);
2605 if (haystack)
2607 char *src;
2609 for (src = haystack + strlen (needle); *src;)
2610 *haystack++ = *src++;
2612 *haystack = 0;
2616 /* Compare two target format name strings.
2617 Return a value indicating how "similar" they are. */
2619 static int
2620 name_compare (char *first, char *second)
2622 char *copy1;
2623 char *copy2;
2624 int result;
2626 copy1 = xmalloc (strlen (first) + 1);
2627 copy2 = xmalloc (strlen (second) + 1);
2629 /* Convert the names to lower case. */
2630 stricpy (copy1, first);
2631 stricpy (copy2, second);
2633 /* Remove size and endian strings from the name. */
2634 strcut (copy1, "big");
2635 strcut (copy1, "little");
2636 strcut (copy2, "big");
2637 strcut (copy2, "little");
2639 /* Return a value based on how many characters match,
2640 starting from the beginning. If both strings are
2641 the same then return 10 * their length. */
2642 for (result = 0; copy1[result] == copy2[result]; result++)
2643 if (copy1[result] == 0)
2645 result *= 10;
2646 break;
2649 free (copy1);
2650 free (copy2);
2652 return result;
2655 /* Set by closest_target_match() below. */
2656 static const bfd_target *winner;
2658 /* Scan all the valid bfd targets looking for one that has the endianness
2659 requirement that was specified on the command line, and is the nearest
2660 match to the original output target. */
2662 static int
2663 closest_target_match (const bfd_target *target, void *data)
2665 const bfd_target *original = data;
2667 if (command_line.endian == ENDIAN_BIG
2668 && target->byteorder != BFD_ENDIAN_BIG)
2669 return 0;
2671 if (command_line.endian == ENDIAN_LITTLE
2672 && target->byteorder != BFD_ENDIAN_LITTLE)
2673 return 0;
2675 /* Must be the same flavour. */
2676 if (target->flavour != original->flavour)
2677 return 0;
2679 /* If we have not found a potential winner yet, then record this one. */
2680 if (winner == NULL)
2682 winner = target;
2683 return 0;
2686 /* Oh dear, we now have two potential candidates for a successful match.
2687 Compare their names and choose the better one. */
2688 if (name_compare (target->name, original->name)
2689 > name_compare (winner->name, original->name))
2690 winner = target;
2692 /* Keep on searching until wqe have checked them all. */
2693 return 0;
2696 /* Return the BFD target format of the first input file. */
2698 static char *
2699 get_first_input_target (void)
2701 char *target = NULL;
2703 LANG_FOR_EACH_INPUT_STATEMENT (s)
2705 if (s->header.type == lang_input_statement_enum
2706 && s->real)
2708 ldfile_open_file (s);
2710 if (s->the_bfd != NULL
2711 && bfd_check_format (s->the_bfd, bfd_object))
2713 target = bfd_get_target (s->the_bfd);
2715 if (target != NULL)
2716 break;
2721 return target;
2724 const char *
2725 lang_get_output_target (void)
2727 const char *target;
2729 /* Has the user told us which output format to use? */
2730 if (output_target != NULL)
2731 return output_target;
2733 /* No - has the current target been set to something other than
2734 the default? */
2735 if (current_target != default_target)
2736 return current_target;
2738 /* No - can we determine the format of the first input file? */
2739 target = get_first_input_target ();
2740 if (target != NULL)
2741 return target;
2743 /* Failed - use the default output target. */
2744 return default_target;
2747 /* Open the output file. */
2749 static bfd *
2750 open_output (const char *name)
2752 bfd *output;
2754 output_target = lang_get_output_target ();
2756 /* Has the user requested a particular endianness on the command
2757 line? */
2758 if (command_line.endian != ENDIAN_UNSET)
2760 const bfd_target *target;
2761 enum bfd_endian desired_endian;
2763 /* Get the chosen target. */
2764 target = bfd_search_for_target (get_target, (void *) output_target);
2766 /* If the target is not supported, we cannot do anything. */
2767 if (target != NULL)
2769 if (command_line.endian == ENDIAN_BIG)
2770 desired_endian = BFD_ENDIAN_BIG;
2771 else
2772 desired_endian = BFD_ENDIAN_LITTLE;
2774 /* See if the target has the wrong endianness. This should
2775 not happen if the linker script has provided big and
2776 little endian alternatives, but some scrips don't do
2777 this. */
2778 if (target->byteorder != desired_endian)
2780 /* If it does, then see if the target provides
2781 an alternative with the correct endianness. */
2782 if (target->alternative_target != NULL
2783 && (target->alternative_target->byteorder == desired_endian))
2784 output_target = target->alternative_target->name;
2785 else
2787 /* Try to find a target as similar as possible to
2788 the default target, but which has the desired
2789 endian characteristic. */
2790 bfd_search_for_target (closest_target_match,
2791 (void *) target);
2793 /* Oh dear - we could not find any targets that
2794 satisfy our requirements. */
2795 if (winner == NULL)
2796 einfo (_("%P: warning: could not find any targets"
2797 " that match endianness requirement\n"));
2798 else
2799 output_target = winner->name;
2805 output = bfd_openw (name, output_target);
2807 if (output == NULL)
2809 if (bfd_get_error () == bfd_error_invalid_target)
2810 einfo (_("%P%F: target %s not found\n"), output_target);
2812 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2815 delete_output_file_on_failure = TRUE;
2817 if (! bfd_set_format (output, bfd_object))
2818 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2819 if (! bfd_set_arch_mach (output,
2820 ldfile_output_architecture,
2821 ldfile_output_machine))
2822 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2824 link_info.hash = bfd_link_hash_table_create (output);
2825 if (link_info.hash == NULL)
2826 einfo (_("%P%F: can not create hash table: %E\n"));
2828 bfd_set_gp_size (output, g_switch_value);
2829 return output;
2832 static void
2833 ldlang_open_output (lang_statement_union_type *statement)
2835 switch (statement->header.type)
2837 case lang_output_statement_enum:
2838 ASSERT (output_bfd == NULL);
2839 output_bfd = open_output (statement->output_statement.name);
2840 ldemul_set_output_arch ();
2841 if (config.magic_demand_paged && !link_info.relocatable)
2842 output_bfd->flags |= D_PAGED;
2843 else
2844 output_bfd->flags &= ~D_PAGED;
2845 if (config.text_read_only)
2846 output_bfd->flags |= WP_TEXT;
2847 else
2848 output_bfd->flags &= ~WP_TEXT;
2849 if (link_info.traditional_format)
2850 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2851 else
2852 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2853 break;
2855 case lang_target_statement_enum:
2856 current_target = statement->target_statement.target;
2857 break;
2858 default:
2859 break;
2863 /* Convert between addresses in bytes and sizes in octets.
2864 For currently supported targets, octets_per_byte is always a power
2865 of two, so we can use shifts. */
2866 #define TO_ADDR(X) ((X) >> opb_shift)
2867 #define TO_SIZE(X) ((X) << opb_shift)
2869 /* Support the above. */
2870 static unsigned int opb_shift = 0;
2872 static void
2873 init_opb (void)
2875 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2876 ldfile_output_machine);
2877 opb_shift = 0;
2878 if (x > 1)
2879 while ((x & 1) == 0)
2881 x >>= 1;
2882 ++opb_shift;
2884 ASSERT (x == 1);
2887 /* Open all the input files. */
2889 static void
2890 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2892 for (; s != NULL; s = s->header.next)
2894 switch (s->header.type)
2896 case lang_constructors_statement_enum:
2897 open_input_bfds (constructor_list.head, force);
2898 break;
2899 case lang_output_section_statement_enum:
2900 open_input_bfds (s->output_section_statement.children.head, force);
2901 break;
2902 case lang_wild_statement_enum:
2903 /* Maybe we should load the file's symbols. */
2904 if (s->wild_statement.filename
2905 && ! wildcardp (s->wild_statement.filename))
2906 lookup_name (s->wild_statement.filename);
2907 open_input_bfds (s->wild_statement.children.head, force);
2908 break;
2909 case lang_group_statement_enum:
2911 struct bfd_link_hash_entry *undefs;
2913 /* We must continually search the entries in the group
2914 until no new symbols are added to the list of undefined
2915 symbols. */
2919 undefs = link_info.hash->undefs_tail;
2920 open_input_bfds (s->group_statement.children.head, TRUE);
2922 while (undefs != link_info.hash->undefs_tail);
2924 break;
2925 case lang_target_statement_enum:
2926 current_target = s->target_statement.target;
2927 break;
2928 case lang_input_statement_enum:
2929 if (s->input_statement.real)
2931 lang_statement_list_type add;
2933 s->input_statement.target = current_target;
2935 /* If we are being called from within a group, and this
2936 is an archive which has already been searched, then
2937 force it to be researched unless the whole archive
2938 has been loaded already. */
2939 if (force
2940 && !s->input_statement.whole_archive
2941 && s->input_statement.loaded
2942 && bfd_check_format (s->input_statement.the_bfd,
2943 bfd_archive))
2944 s->input_statement.loaded = FALSE;
2946 lang_list_init (&add);
2948 if (! load_symbols (&s->input_statement, &add))
2949 config.make_executable = FALSE;
2951 if (add.head != NULL)
2953 *add.tail = s->header.next;
2954 s->header.next = add.head;
2957 break;
2958 default:
2959 break;
2964 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2966 void
2967 lang_track_definedness (const char *name)
2969 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2970 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2973 /* New-function for the definedness hash table. */
2975 static struct bfd_hash_entry *
2976 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2977 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2978 const char *name ATTRIBUTE_UNUSED)
2980 struct lang_definedness_hash_entry *ret
2981 = (struct lang_definedness_hash_entry *) entry;
2983 if (ret == NULL)
2984 ret = (struct lang_definedness_hash_entry *)
2985 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2987 if (ret == NULL)
2988 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2990 ret->iteration = -1;
2991 return &ret->root;
2994 /* Return the iteration when the definition of NAME was last updated. A
2995 value of -1 means that the symbol is not defined in the linker script
2996 or the command line, but may be defined in the linker symbol table. */
2999 lang_symbol_definition_iteration (const char *name)
3001 struct lang_definedness_hash_entry *defentry
3002 = (struct lang_definedness_hash_entry *)
3003 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3005 /* We've already created this one on the presence of DEFINED in the
3006 script, so it can't be NULL unless something is borked elsewhere in
3007 the code. */
3008 if (defentry == NULL)
3009 FAIL ();
3011 return defentry->iteration;
3014 /* Update the definedness state of NAME. */
3016 void
3017 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3019 struct lang_definedness_hash_entry *defentry
3020 = (struct lang_definedness_hash_entry *)
3021 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3023 /* We don't keep track of symbols not tested with DEFINED. */
3024 if (defentry == NULL)
3025 return;
3027 /* If the symbol was already defined, and not from an earlier statement
3028 iteration, don't update the definedness iteration, because that'd
3029 make the symbol seem defined in the linker script at this point, and
3030 it wasn't; it was defined in some object. If we do anyway, DEFINED
3031 would start to yield false before this point and the construct "sym =
3032 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3033 in an object. */
3034 if (h->type != bfd_link_hash_undefined
3035 && h->type != bfd_link_hash_common
3036 && h->type != bfd_link_hash_new
3037 && defentry->iteration == -1)
3038 return;
3040 defentry->iteration = lang_statement_iteration;
3043 /* Add the supplied name to the symbol table as an undefined reference.
3044 This is a two step process as the symbol table doesn't even exist at
3045 the time the ld command line is processed. First we put the name
3046 on a list, then, once the output file has been opened, transfer the
3047 name to the symbol table. */
3049 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3051 #define ldlang_undef_chain_list_head entry_symbol.next
3053 void
3054 ldlang_add_undef (const char *const name)
3056 ldlang_undef_chain_list_type *new =
3057 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3059 new->next = ldlang_undef_chain_list_head;
3060 ldlang_undef_chain_list_head = new;
3062 new->name = xstrdup (name);
3064 if (output_bfd != NULL)
3065 insert_undefined (new->name);
3068 /* Insert NAME as undefined in the symbol table. */
3070 static void
3071 insert_undefined (const char *name)
3073 struct bfd_link_hash_entry *h;
3075 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3076 if (h == NULL)
3077 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3078 if (h->type == bfd_link_hash_new)
3080 h->type = bfd_link_hash_undefined;
3081 h->u.undef.abfd = NULL;
3082 bfd_link_add_undef (link_info.hash, h);
3086 /* Run through the list of undefineds created above and place them
3087 into the linker hash table as undefined symbols belonging to the
3088 script file. */
3090 static void
3091 lang_place_undefineds (void)
3093 ldlang_undef_chain_list_type *ptr;
3095 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3096 insert_undefined (ptr->name);
3099 /* Check for all readonly or some readwrite sections. */
3101 static void
3102 check_input_sections
3103 (lang_statement_union_type *s,
3104 lang_output_section_statement_type *output_section_statement)
3106 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3108 switch (s->header.type)
3110 case lang_wild_statement_enum:
3111 walk_wild (&s->wild_statement, check_section_callback,
3112 output_section_statement);
3113 if (! output_section_statement->all_input_readonly)
3114 return;
3115 break;
3116 case lang_constructors_statement_enum:
3117 check_input_sections (constructor_list.head,
3118 output_section_statement);
3119 if (! output_section_statement->all_input_readonly)
3120 return;
3121 break;
3122 case lang_group_statement_enum:
3123 check_input_sections (s->group_statement.children.head,
3124 output_section_statement);
3125 if (! output_section_statement->all_input_readonly)
3126 return;
3127 break;
3128 default:
3129 break;
3134 /* Update wildcard statements if needed. */
3136 static void
3137 update_wild_statements (lang_statement_union_type *s)
3139 struct wildcard_list *sec;
3141 switch (sort_section)
3143 default:
3144 FAIL ();
3146 case none:
3147 break;
3149 case by_name:
3150 case by_alignment:
3151 for (; s != NULL; s = s->header.next)
3153 switch (s->header.type)
3155 default:
3156 break;
3158 case lang_wild_statement_enum:
3159 sec = s->wild_statement.section_list;
3160 if (sec != NULL)
3162 switch (sec->spec.sorted)
3164 case none:
3165 sec->spec.sorted = sort_section;
3166 break;
3167 case by_name:
3168 if (sort_section == by_alignment)
3169 sec->spec.sorted = by_name_alignment;
3170 break;
3171 case by_alignment:
3172 if (sort_section == by_name)
3173 sec->spec.sorted = by_alignment_name;
3174 break;
3175 default:
3176 break;
3179 break;
3181 case lang_constructors_statement_enum:
3182 update_wild_statements (constructor_list.head);
3183 break;
3185 case lang_output_section_statement_enum:
3186 update_wild_statements
3187 (s->output_section_statement.children.head);
3188 break;
3190 case lang_group_statement_enum:
3191 update_wild_statements (s->group_statement.children.head);
3192 break;
3195 break;
3199 /* Open input files and attach to output sections. */
3201 static void
3202 map_input_to_output_sections
3203 (lang_statement_union_type *s, const char *target,
3204 lang_output_section_statement_type *os)
3206 flagword flags;
3208 for (; s != NULL; s = s->header.next)
3210 switch (s->header.type)
3212 case lang_wild_statement_enum:
3213 wild (&s->wild_statement, target, os);
3214 break;
3215 case lang_constructors_statement_enum:
3216 map_input_to_output_sections (constructor_list.head,
3217 target,
3218 os);
3219 break;
3220 case lang_output_section_statement_enum:
3221 if (s->output_section_statement.constraint)
3223 if (s->output_section_statement.constraint != ONLY_IF_RW
3224 && s->output_section_statement.constraint != ONLY_IF_RO)
3225 break;
3226 s->output_section_statement.all_input_readonly = TRUE;
3227 check_input_sections (s->output_section_statement.children.head,
3228 &s->output_section_statement);
3229 if ((s->output_section_statement.all_input_readonly
3230 && s->output_section_statement.constraint == ONLY_IF_RW)
3231 || (!s->output_section_statement.all_input_readonly
3232 && s->output_section_statement.constraint == ONLY_IF_RO))
3234 s->output_section_statement.constraint = -1;
3235 break;
3239 map_input_to_output_sections (s->output_section_statement.children.head,
3240 target,
3241 &s->output_section_statement);
3242 break;
3243 case lang_output_statement_enum:
3244 break;
3245 case lang_target_statement_enum:
3246 target = s->target_statement.target;
3247 break;
3248 case lang_group_statement_enum:
3249 map_input_to_output_sections (s->group_statement.children.head,
3250 target,
3251 os);
3252 break;
3253 case lang_data_statement_enum:
3254 /* Make sure that any sections mentioned in the expression
3255 are initialized. */
3256 exp_init_os (s->data_statement.exp);
3257 flags = SEC_HAS_CONTENTS;
3258 /* The output section gets contents, and then we inspect for
3259 any flags set in the input script which override any ALLOC. */
3260 if (!(os->flags & SEC_NEVER_LOAD))
3261 flags |= SEC_ALLOC | SEC_LOAD;
3262 if (os->bfd_section == NULL)
3263 init_os (os, NULL, flags);
3264 else
3265 os->bfd_section->flags |= flags;
3266 break;
3267 case lang_input_section_enum:
3268 break;
3269 case lang_fill_statement_enum:
3270 case lang_object_symbols_statement_enum:
3271 case lang_reloc_statement_enum:
3272 case lang_padding_statement_enum:
3273 case lang_input_statement_enum:
3274 if (os != NULL && os->bfd_section == NULL)
3275 init_os (os, NULL, 0);
3276 break;
3277 case lang_assignment_statement_enum:
3278 if (os != NULL && os->bfd_section == NULL)
3279 init_os (os, NULL, 0);
3281 /* Make sure that any sections mentioned in the assignment
3282 are initialized. */
3283 exp_init_os (s->assignment_statement.exp);
3284 break;
3285 case lang_afile_asection_pair_statement_enum:
3286 FAIL ();
3287 break;
3288 case lang_address_statement_enum:
3289 /* Mark the specified section with the supplied address.
3291 If this section was actually a segment marker, then the
3292 directive is ignored if the linker script explicitly
3293 processed the segment marker. Originally, the linker
3294 treated segment directives (like -Ttext on the
3295 command-line) as section directives. We honor the
3296 section directive semantics for backwards compatibilty;
3297 linker scripts that do not specifically check for
3298 SEGMENT_START automatically get the old semantics. */
3299 if (!s->address_statement.segment
3300 || !s->address_statement.segment->used)
3302 lang_output_section_statement_type *aos
3303 = (lang_output_section_statement_lookup
3304 (s->address_statement.section_name));
3306 if (aos->bfd_section == NULL)
3307 init_os (aos, NULL, 0);
3308 aos->addr_tree = s->address_statement.address;
3310 break;
3315 /* An output section might have been removed after its statement was
3316 added. For example, ldemul_before_allocation can remove dynamic
3317 sections if they turn out to be not needed. Clean them up here. */
3319 void
3320 strip_excluded_output_sections (void)
3322 lang_output_section_statement_type *os;
3324 /* Run lang_size_sections (if not already done). */
3325 if (expld.phase != lang_mark_phase_enum)
3327 expld.phase = lang_mark_phase_enum;
3328 expld.dataseg.phase = exp_dataseg_none;
3329 one_lang_size_sections_pass (NULL, FALSE);
3330 lang_reset_memory_regions ();
3333 for (os = &lang_output_section_statement.head->output_section_statement;
3334 os != NULL;
3335 os = os->next)
3337 asection *output_section;
3338 bfd_boolean exclude;
3340 if (os->constraint == -1)
3341 continue;
3343 output_section = os->bfd_section;
3344 if (output_section == NULL)
3345 continue;
3347 exclude = (output_section->rawsize == 0
3348 && (output_section->flags & SEC_KEEP) == 0
3349 && !bfd_section_removed_from_list (output_bfd,
3350 output_section));
3352 /* Some sections have not yet been sized, notably .gnu.version,
3353 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3354 input sections, so don't drop output sections that have such
3355 input sections unless they are also marked SEC_EXCLUDE. */
3356 if (exclude && output_section->map_head.s != NULL)
3358 asection *s;
3360 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3361 if ((s->flags & SEC_LINKER_CREATED) != 0
3362 && (s->flags & SEC_EXCLUDE) == 0)
3364 exclude = FALSE;
3365 break;
3369 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3370 output_section->map_head.link_order = NULL;
3371 output_section->map_tail.link_order = NULL;
3373 if (exclude)
3375 /* We don't set bfd_section to NULL since bfd_section of the
3376 removed output section statement may still be used. */
3377 os->ignored = TRUE;
3378 output_section->flags |= SEC_EXCLUDE;
3379 bfd_section_list_remove (output_bfd, output_section);
3380 output_bfd->section_count--;
3384 /* Stop future calls to lang_add_section from messing with map_head
3385 and map_tail link_order fields. */
3386 stripped_excluded_sections = TRUE;
3389 static void
3390 print_output_section_statement
3391 (lang_output_section_statement_type *output_section_statement)
3393 asection *section = output_section_statement->bfd_section;
3394 int len;
3396 if (output_section_statement != abs_output_section)
3398 minfo ("\n%s", output_section_statement->name);
3400 if (section != NULL)
3402 print_dot = section->vma;
3404 len = strlen (output_section_statement->name);
3405 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3407 print_nl ();
3408 len = 0;
3410 while (len < SECTION_NAME_MAP_LENGTH)
3412 print_space ();
3413 ++len;
3416 minfo ("0x%V %W", section->vma, section->size);
3418 if (output_section_statement->load_base != NULL)
3420 bfd_vma addr;
3422 addr = exp_get_abs_int (output_section_statement->load_base, 0,
3423 "load base");
3424 minfo (_(" load address 0x%V"), addr);
3428 print_nl ();
3431 print_statement_list (output_section_statement->children.head,
3432 output_section_statement);
3435 /* Scan for the use of the destination in the right hand side
3436 of an expression. In such cases we will not compute the
3437 correct expression, since the value of DST that is used on
3438 the right hand side will be its final value, not its value
3439 just before this expression is evaluated. */
3441 static bfd_boolean
3442 scan_for_self_assignment (const char * dst, etree_type * rhs)
3444 if (rhs == NULL || dst == NULL)
3445 return FALSE;
3447 switch (rhs->type.node_class)
3449 case etree_binary:
3450 return scan_for_self_assignment (dst, rhs->binary.lhs)
3451 || scan_for_self_assignment (dst, rhs->binary.rhs);
3453 case etree_trinary:
3454 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3455 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3457 case etree_assign:
3458 case etree_provided:
3459 case etree_provide:
3460 if (strcmp (dst, rhs->assign.dst) == 0)
3461 return TRUE;
3462 return scan_for_self_assignment (dst, rhs->assign.src);
3464 case etree_unary:
3465 return scan_for_self_assignment (dst, rhs->unary.child);
3467 case etree_value:
3468 if (rhs->value.str)
3469 return strcmp (dst, rhs->value.str) == 0;
3470 return FALSE;
3472 case etree_name:
3473 if (rhs->name.name)
3474 return strcmp (dst, rhs->name.name) == 0;
3475 return FALSE;
3477 default:
3478 break;
3481 return FALSE;
3485 static void
3486 print_assignment (lang_assignment_statement_type *assignment,
3487 lang_output_section_statement_type *output_section)
3489 unsigned int i;
3490 bfd_boolean is_dot;
3491 bfd_boolean computation_is_valid = TRUE;
3492 etree_type *tree;
3494 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3495 print_space ();
3497 if (assignment->exp->type.node_class == etree_assert)
3499 is_dot = FALSE;
3500 tree = assignment->exp->assert_s.child;
3501 computation_is_valid = TRUE;
3503 else
3505 const char *dst = assignment->exp->assign.dst;
3507 is_dot = (dst[0] == '.' && dst[1] == 0);
3508 tree = assignment->exp->assign.src;
3509 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3512 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3513 if (expld.result.valid_p)
3515 bfd_vma value;
3517 if (computation_is_valid)
3519 value = expld.result.value;
3521 if (expld.result.section)
3522 value += expld.result.section->vma;
3524 minfo ("0x%V", value);
3525 if (is_dot)
3526 print_dot = value;
3528 else
3530 struct bfd_link_hash_entry *h;
3532 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3533 FALSE, FALSE, TRUE);
3534 if (h)
3536 value = h->u.def.value;
3538 if (expld.result.section)
3539 value += expld.result.section->vma;
3541 minfo ("[0x%V]", value);
3543 else
3544 minfo ("[unresolved]");
3547 else
3549 minfo ("*undef* ");
3550 #ifdef BFD64
3551 minfo (" ");
3552 #endif
3555 minfo (" ");
3556 exp_print_tree (assignment->exp);
3557 print_nl ();
3560 static void
3561 print_input_statement (lang_input_statement_type *statm)
3563 if (statm->filename != NULL)
3565 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3569 /* Print all symbols defined in a particular section. This is called
3570 via bfd_link_hash_traverse, or by print_all_symbols. */
3572 static bfd_boolean
3573 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3575 asection *sec = ptr;
3577 if ((hash_entry->type == bfd_link_hash_defined
3578 || hash_entry->type == bfd_link_hash_defweak)
3579 && sec == hash_entry->u.def.section)
3581 int i;
3583 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3584 print_space ();
3585 minfo ("0x%V ",
3586 (hash_entry->u.def.value
3587 + hash_entry->u.def.section->output_offset
3588 + hash_entry->u.def.section->output_section->vma));
3590 minfo (" %T\n", hash_entry->root.string);
3593 return TRUE;
3596 static void
3597 print_all_symbols (asection *sec)
3599 struct fat_user_section_struct *ud = get_userdata (sec);
3600 struct map_symbol_def *def;
3602 if (!ud)
3603 return;
3605 *ud->map_symbol_def_tail = 0;
3606 for (def = ud->map_symbol_def_head; def; def = def->next)
3607 print_one_symbol (def->entry, sec);
3610 /* Print information about an input section to the map file. */
3612 static void
3613 print_input_section (asection *i)
3615 bfd_size_type size = i->size;
3616 int len;
3617 bfd_vma addr;
3619 init_opb ();
3621 print_space ();
3622 minfo ("%s", i->name);
3624 len = 1 + strlen (i->name);
3625 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3627 print_nl ();
3628 len = 0;
3630 while (len < SECTION_NAME_MAP_LENGTH)
3632 print_space ();
3633 ++len;
3636 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3637 addr = i->output_section->vma + i->output_offset;
3638 else
3640 addr = print_dot;
3641 size = 0;
3644 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3646 if (size != i->rawsize && i->rawsize != 0)
3648 len = SECTION_NAME_MAP_LENGTH + 3;
3649 #ifdef BFD64
3650 len += 16;
3651 #else
3652 len += 8;
3653 #endif
3654 while (len > 0)
3656 print_space ();
3657 --len;
3660 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3663 if (i->output_section != NULL && i->output_section->owner == output_bfd)
3665 if (command_line.reduce_memory_overheads)
3666 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3667 else
3668 print_all_symbols (i);
3670 print_dot = addr + TO_ADDR (size);
3674 static void
3675 print_fill_statement (lang_fill_statement_type *fill)
3677 size_t size;
3678 unsigned char *p;
3679 fputs (" FILL mask 0x", config.map_file);
3680 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3681 fprintf (config.map_file, "%02x", *p);
3682 fputs ("\n", config.map_file);
3685 static void
3686 print_data_statement (lang_data_statement_type *data)
3688 int i;
3689 bfd_vma addr;
3690 bfd_size_type size;
3691 const char *name;
3693 init_opb ();
3694 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3695 print_space ();
3697 addr = data->output_offset;
3698 if (data->output_section != NULL)
3699 addr += data->output_section->vma;
3701 switch (data->type)
3703 default:
3704 abort ();
3705 case BYTE:
3706 size = BYTE_SIZE;
3707 name = "BYTE";
3708 break;
3709 case SHORT:
3710 size = SHORT_SIZE;
3711 name = "SHORT";
3712 break;
3713 case LONG:
3714 size = LONG_SIZE;
3715 name = "LONG";
3716 break;
3717 case QUAD:
3718 size = QUAD_SIZE;
3719 name = "QUAD";
3720 break;
3721 case SQUAD:
3722 size = QUAD_SIZE;
3723 name = "SQUAD";
3724 break;
3727 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3729 if (data->exp->type.node_class != etree_value)
3731 print_space ();
3732 exp_print_tree (data->exp);
3735 print_nl ();
3737 print_dot = addr + TO_ADDR (size);
3740 /* Print an address statement. These are generated by options like
3741 -Ttext. */
3743 static void
3744 print_address_statement (lang_address_statement_type *address)
3746 minfo (_("Address of section %s set to "), address->section_name);
3747 exp_print_tree (address->address);
3748 print_nl ();
3751 /* Print a reloc statement. */
3753 static void
3754 print_reloc_statement (lang_reloc_statement_type *reloc)
3756 int i;
3757 bfd_vma addr;
3758 bfd_size_type size;
3760 init_opb ();
3761 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3762 print_space ();
3764 addr = reloc->output_offset;
3765 if (reloc->output_section != NULL)
3766 addr += reloc->output_section->vma;
3768 size = bfd_get_reloc_size (reloc->howto);
3770 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3772 if (reloc->name != NULL)
3773 minfo ("%s+", reloc->name);
3774 else
3775 minfo ("%s+", reloc->section->name);
3777 exp_print_tree (reloc->addend_exp);
3779 print_nl ();
3781 print_dot = addr + TO_ADDR (size);
3784 static void
3785 print_padding_statement (lang_padding_statement_type *s)
3787 int len;
3788 bfd_vma addr;
3790 init_opb ();
3791 minfo (" *fill*");
3793 len = sizeof " *fill*" - 1;
3794 while (len < SECTION_NAME_MAP_LENGTH)
3796 print_space ();
3797 ++len;
3800 addr = s->output_offset;
3801 if (s->output_section != NULL)
3802 addr += s->output_section->vma;
3803 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3805 if (s->fill->size != 0)
3807 size_t size;
3808 unsigned char *p;
3809 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3810 fprintf (config.map_file, "%02x", *p);
3813 print_nl ();
3815 print_dot = addr + TO_ADDR (s->size);
3818 static void
3819 print_wild_statement (lang_wild_statement_type *w,
3820 lang_output_section_statement_type *os)
3822 struct wildcard_list *sec;
3824 print_space ();
3826 if (w->filenames_sorted)
3827 minfo ("SORT(");
3828 if (w->filename != NULL)
3829 minfo ("%s", w->filename);
3830 else
3831 minfo ("*");
3832 if (w->filenames_sorted)
3833 minfo (")");
3835 minfo ("(");
3836 for (sec = w->section_list; sec; sec = sec->next)
3838 if (sec->spec.sorted)
3839 minfo ("SORT(");
3840 if (sec->spec.exclude_name_list != NULL)
3842 name_list *tmp;
3843 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3844 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3845 minfo (" %s", tmp->name);
3846 minfo (") ");
3848 if (sec->spec.name != NULL)
3849 minfo ("%s", sec->spec.name);
3850 else
3851 minfo ("*");
3852 if (sec->spec.sorted)
3853 minfo (")");
3854 if (sec->next)
3855 minfo (" ");
3857 minfo (")");
3859 print_nl ();
3861 print_statement_list (w->children.head, os);
3864 /* Print a group statement. */
3866 static void
3867 print_group (lang_group_statement_type *s,
3868 lang_output_section_statement_type *os)
3870 fprintf (config.map_file, "START GROUP\n");
3871 print_statement_list (s->children.head, os);
3872 fprintf (config.map_file, "END GROUP\n");
3875 /* Print the list of statements in S.
3876 This can be called for any statement type. */
3878 static void
3879 print_statement_list (lang_statement_union_type *s,
3880 lang_output_section_statement_type *os)
3882 while (s != NULL)
3884 print_statement (s, os);
3885 s = s->header.next;
3889 /* Print the first statement in statement list S.
3890 This can be called for any statement type. */
3892 static void
3893 print_statement (lang_statement_union_type *s,
3894 lang_output_section_statement_type *os)
3896 switch (s->header.type)
3898 default:
3899 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3900 FAIL ();
3901 break;
3902 case lang_constructors_statement_enum:
3903 if (constructor_list.head != NULL)
3905 if (constructors_sorted)
3906 minfo (" SORT (CONSTRUCTORS)\n");
3907 else
3908 minfo (" CONSTRUCTORS\n");
3909 print_statement_list (constructor_list.head, os);
3911 break;
3912 case lang_wild_statement_enum:
3913 print_wild_statement (&s->wild_statement, os);
3914 break;
3915 case lang_address_statement_enum:
3916 print_address_statement (&s->address_statement);
3917 break;
3918 case lang_object_symbols_statement_enum:
3919 minfo (" CREATE_OBJECT_SYMBOLS\n");
3920 break;
3921 case lang_fill_statement_enum:
3922 print_fill_statement (&s->fill_statement);
3923 break;
3924 case lang_data_statement_enum:
3925 print_data_statement (&s->data_statement);
3926 break;
3927 case lang_reloc_statement_enum:
3928 print_reloc_statement (&s->reloc_statement);
3929 break;
3930 case lang_input_section_enum:
3931 print_input_section (s->input_section.section);
3932 break;
3933 case lang_padding_statement_enum:
3934 print_padding_statement (&s->padding_statement);
3935 break;
3936 case lang_output_section_statement_enum:
3937 print_output_section_statement (&s->output_section_statement);
3938 break;
3939 case lang_assignment_statement_enum:
3940 print_assignment (&s->assignment_statement, os);
3941 break;
3942 case lang_target_statement_enum:
3943 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3944 break;
3945 case lang_output_statement_enum:
3946 minfo ("OUTPUT(%s", s->output_statement.name);
3947 if (output_target != NULL)
3948 minfo (" %s", output_target);
3949 minfo (")\n");
3950 break;
3951 case lang_input_statement_enum:
3952 print_input_statement (&s->input_statement);
3953 break;
3954 case lang_group_statement_enum:
3955 print_group (&s->group_statement, os);
3956 break;
3957 case lang_afile_asection_pair_statement_enum:
3958 FAIL ();
3959 break;
3963 static void
3964 print_statements (void)
3966 print_statement_list (statement_list.head, abs_output_section);
3969 /* Print the first N statements in statement list S to STDERR.
3970 If N == 0, nothing is printed.
3971 If N < 0, the entire list is printed.
3972 Intended to be called from GDB. */
3974 void
3975 dprint_statement (lang_statement_union_type *s, int n)
3977 FILE *map_save = config.map_file;
3979 config.map_file = stderr;
3981 if (n < 0)
3982 print_statement_list (s, abs_output_section);
3983 else
3985 while (s && --n >= 0)
3987 print_statement (s, abs_output_section);
3988 s = s->header.next;
3992 config.map_file = map_save;
3995 static void
3996 insert_pad (lang_statement_union_type **ptr,
3997 fill_type *fill,
3998 unsigned int alignment_needed,
3999 asection *output_section,
4000 bfd_vma dot)
4002 static fill_type zero_fill = { 1, { 0 } };
4003 lang_statement_union_type *pad = NULL;
4005 if (ptr != &statement_list.head)
4006 pad = ((lang_statement_union_type *)
4007 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4008 if (pad != NULL
4009 && pad->header.type == lang_padding_statement_enum
4010 && pad->padding_statement.output_section == output_section)
4012 /* Use the existing pad statement. */
4014 else if ((pad = *ptr) != NULL
4015 && pad->header.type == lang_padding_statement_enum
4016 && pad->padding_statement.output_section == output_section)
4018 /* Use the existing pad statement. */
4020 else
4022 /* Make a new padding statement, linked into existing chain. */
4023 pad = stat_alloc (sizeof (lang_padding_statement_type));
4024 pad->header.next = *ptr;
4025 *ptr = pad;
4026 pad->header.type = lang_padding_statement_enum;
4027 pad->padding_statement.output_section = output_section;
4028 if (fill == NULL)
4029 fill = &zero_fill;
4030 pad->padding_statement.fill = fill;
4032 pad->padding_statement.output_offset = dot - output_section->vma;
4033 pad->padding_statement.size = alignment_needed;
4034 output_section->size += alignment_needed;
4037 /* Work out how much this section will move the dot point. */
4039 static bfd_vma
4040 size_input_section
4041 (lang_statement_union_type **this_ptr,
4042 lang_output_section_statement_type *output_section_statement,
4043 fill_type *fill,
4044 bfd_vma dot)
4046 lang_input_section_type *is = &((*this_ptr)->input_section);
4047 asection *i = is->section;
4049 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4050 && (i->flags & SEC_EXCLUDE) == 0)
4052 unsigned int alignment_needed;
4053 asection *o;
4055 /* Align this section first to the input sections requirement,
4056 then to the output section's requirement. If this alignment
4057 is greater than any seen before, then record it too. Perform
4058 the alignment by inserting a magic 'padding' statement. */
4060 if (output_section_statement->subsection_alignment != -1)
4061 i->alignment_power = output_section_statement->subsection_alignment;
4063 o = output_section_statement->bfd_section;
4064 if (o->alignment_power < i->alignment_power)
4065 o->alignment_power = i->alignment_power;
4067 alignment_needed = align_power (dot, i->alignment_power) - dot;
4069 if (alignment_needed != 0)
4071 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4072 dot += alignment_needed;
4075 /* Remember where in the output section this input section goes. */
4077 i->output_offset = dot - o->vma;
4079 /* Mark how big the output section must be to contain this now. */
4080 dot += TO_ADDR (i->size);
4081 o->size = TO_SIZE (dot - o->vma);
4083 else
4085 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4088 return dot;
4091 static int
4092 sort_sections_by_lma (const void *arg1, const void *arg2)
4094 const asection *sec1 = *(const asection **) arg1;
4095 const asection *sec2 = *(const asection **) arg2;
4097 if (bfd_section_lma (sec1->owner, sec1)
4098 < bfd_section_lma (sec2->owner, sec2))
4099 return -1;
4100 else if (bfd_section_lma (sec1->owner, sec1)
4101 > bfd_section_lma (sec2->owner, sec2))
4102 return 1;
4104 return 0;
4107 #define IGNORE_SECTION(s) \
4108 ((s->flags & SEC_NEVER_LOAD) != 0 \
4109 || (s->flags & SEC_ALLOC) == 0 \
4110 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4111 && (s->flags & SEC_LOAD) == 0))
4113 /* Check to see if any allocated sections overlap with other allocated
4114 sections. This can happen if a linker script specifies the output
4115 section addresses of the two sections. */
4117 static void
4118 lang_check_section_addresses (void)
4120 asection *s, *os;
4121 asection **sections, **spp;
4122 unsigned int count;
4123 bfd_vma s_start;
4124 bfd_vma s_end;
4125 bfd_vma os_start;
4126 bfd_vma os_end;
4127 bfd_size_type amt;
4129 if (bfd_count_sections (output_bfd) <= 1)
4130 return;
4132 amt = bfd_count_sections (output_bfd) * sizeof (asection *);
4133 sections = xmalloc (amt);
4135 /* Scan all sections in the output list. */
4136 count = 0;
4137 for (s = output_bfd->sections; s != NULL; s = s->next)
4139 /* Only consider loadable sections with real contents. */
4140 if (IGNORE_SECTION (s) || s->size == 0)
4141 continue;
4143 sections[count] = s;
4144 count++;
4147 if (count <= 1)
4148 return;
4150 qsort (sections, (size_t) count, sizeof (asection *),
4151 sort_sections_by_lma);
4153 spp = sections;
4154 s = *spp++;
4155 s_start = bfd_section_lma (output_bfd, s);
4156 s_end = s_start + TO_ADDR (s->size) - 1;
4157 for (count--; count; count--)
4159 /* We must check the sections' LMA addresses not their VMA
4160 addresses because overlay sections can have overlapping VMAs
4161 but they must have distinct LMAs. */
4162 os = s;
4163 os_start = s_start;
4164 os_end = s_end;
4165 s = *spp++;
4166 s_start = bfd_section_lma (output_bfd, s);
4167 s_end = s_start + TO_ADDR (s->size) - 1;
4169 /* Look for an overlap. */
4170 if (s_end >= os_start && s_start <= os_end)
4171 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4172 s->name, s_start, s_end, os->name, os_start, os_end);
4175 free (sections);
4178 /* Make sure the new address is within the region. We explicitly permit the
4179 current address to be at the exact end of the region when the address is
4180 non-zero, in case the region is at the end of addressable memory and the
4181 calculation wraps around. */
4183 static void
4184 os_region_check (lang_output_section_statement_type *os,
4185 lang_memory_region_type *region,
4186 etree_type *tree,
4187 bfd_vma base)
4189 if ((region->current < region->origin
4190 || (region->current - region->origin > region->length))
4191 && ((region->current != region->origin + region->length)
4192 || base == 0))
4194 if (tree != NULL)
4196 einfo (_("%X%P: address 0x%v of %B section %s"
4197 " is not within region %s\n"),
4198 region->current,
4199 os->bfd_section->owner,
4200 os->bfd_section->name,
4201 region->name);
4203 else
4205 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4206 region->name,
4207 os->bfd_section->owner,
4208 os->bfd_section->name);
4210 /* Reset the region pointer. */
4211 region->current = region->origin;
4215 /* Set the sizes for all the output sections. */
4217 static bfd_vma
4218 lang_size_sections_1
4219 (lang_statement_union_type *s,
4220 lang_output_section_statement_type *output_section_statement,
4221 lang_statement_union_type **prev,
4222 fill_type *fill,
4223 bfd_vma dot,
4224 bfd_boolean *relax,
4225 bfd_boolean check_regions)
4227 /* Size up the sections from their constituent parts. */
4228 for (; s != NULL; s = s->header.next)
4230 switch (s->header.type)
4232 case lang_output_section_statement_enum:
4234 bfd_vma newdot, after;
4235 lang_output_section_statement_type *os;
4237 os = &s->output_section_statement;
4238 if (os->addr_tree != NULL)
4240 os->processed = FALSE;
4241 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4243 if (!expld.result.valid_p
4244 && expld.phase != lang_mark_phase_enum)
4245 einfo (_("%F%S: non constant or forward reference"
4246 " address expression for section %s\n"),
4247 os->name);
4249 dot = expld.result.value + expld.result.section->vma;
4252 if (os->bfd_section == NULL)
4253 /* This section was removed or never actually created. */
4254 break;
4256 /* If this is a COFF shared library section, use the size and
4257 address from the input section. FIXME: This is COFF
4258 specific; it would be cleaner if there were some other way
4259 to do this, but nothing simple comes to mind. */
4260 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
4261 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
4262 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4264 asection *input;
4266 if (os->children.head == NULL
4267 || os->children.head->header.next != NULL
4268 || (os->children.head->header.type
4269 != lang_input_section_enum))
4270 einfo (_("%P%X: Internal error on COFF shared library"
4271 " section %s\n"), os->name);
4273 input = os->children.head->input_section.section;
4274 bfd_set_section_vma (os->bfd_section->owner,
4275 os->bfd_section,
4276 bfd_section_vma (input->owner, input));
4277 os->bfd_section->size = input->size;
4278 break;
4281 newdot = dot;
4282 if (bfd_is_abs_section (os->bfd_section))
4284 /* No matter what happens, an abs section starts at zero. */
4285 ASSERT (os->bfd_section->vma == 0);
4287 else
4289 int align;
4291 if (os->addr_tree == NULL)
4293 /* No address specified for this section, get one
4294 from the region specification. */
4295 if (os->region == NULL
4296 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4297 && os->region->name[0] == '*'
4298 && strcmp (os->region->name,
4299 DEFAULT_MEMORY_REGION) == 0))
4301 os->region = lang_memory_default (os->bfd_section);
4304 /* If a loadable section is using the default memory
4305 region, and some non default memory regions were
4306 defined, issue an error message. */
4307 if (!os->ignored
4308 && !IGNORE_SECTION (os->bfd_section)
4309 && ! link_info.relocatable
4310 && check_regions
4311 && strcmp (os->region->name,
4312 DEFAULT_MEMORY_REGION) == 0
4313 && lang_memory_region_list != NULL
4314 && (strcmp (lang_memory_region_list->name,
4315 DEFAULT_MEMORY_REGION) != 0
4316 || lang_memory_region_list->next != NULL)
4317 && expld.phase != lang_mark_phase_enum)
4319 /* By default this is an error rather than just a
4320 warning because if we allocate the section to the
4321 default memory region we can end up creating an
4322 excessively large binary, or even seg faulting when
4323 attempting to perform a negative seek. See
4324 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4325 for an example of this. This behaviour can be
4326 overridden by the using the --no-check-sections
4327 switch. */
4328 if (command_line.check_section_addresses)
4329 einfo (_("%P%F: error: no memory region specified"
4330 " for loadable section `%s'\n"),
4331 bfd_get_section_name (output_bfd,
4332 os->bfd_section));
4333 else
4334 einfo (_("%P: warning: no memory region specified"
4335 " for loadable section `%s'\n"),
4336 bfd_get_section_name (output_bfd,
4337 os->bfd_section));
4340 newdot = os->region->current;
4341 align = os->bfd_section->alignment_power;
4343 else
4344 align = os->section_alignment;
4346 /* Align to what the section needs. */
4347 if (align > 0)
4349 bfd_vma savedot = newdot;
4350 newdot = align_power (newdot, align);
4352 if (newdot != savedot
4353 && (config.warn_section_align
4354 || os->addr_tree != NULL)
4355 && expld.phase != lang_mark_phase_enum)
4356 einfo (_("%P: warning: changing start of section"
4357 " %s by %lu bytes\n"),
4358 os->name, (unsigned long) (newdot - savedot));
4361 bfd_set_section_vma (0, os->bfd_section, newdot);
4363 os->bfd_section->output_offset = 0;
4366 lang_size_sections_1 (os->children.head, os, &os->children.head,
4367 os->fill, newdot, relax, check_regions);
4369 os->processed = TRUE;
4371 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4373 ASSERT (os->bfd_section->size == 0);
4374 break;
4377 dot = os->bfd_section->vma;
4379 /* Put the section within the requested block size, or
4380 align at the block boundary. */
4381 after = ((dot
4382 + TO_ADDR (os->bfd_section->size)
4383 + os->block_value - 1)
4384 & - (bfd_vma) os->block_value);
4386 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4388 /* .tbss sections effectively have zero size. */
4389 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4390 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4391 || link_info.relocatable)
4392 dot += TO_ADDR (os->bfd_section->size);
4394 if (os->update_dot_tree != 0)
4395 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4397 /* Update dot in the region ?
4398 We only do this if the section is going to be allocated,
4399 since unallocated sections do not contribute to the region's
4400 overall size in memory.
4402 If the SEC_NEVER_LOAD bit is not set, it will affect the
4403 addresses of sections after it. We have to update
4404 dot. */
4405 if (os->region != NULL
4406 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4407 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4409 os->region->current = dot;
4411 if (check_regions)
4412 /* Make sure the new address is within the region. */
4413 os_region_check (os, os->region, os->addr_tree,
4414 os->bfd_section->vma);
4416 if (os->lma_region != NULL && os->lma_region != os->region)
4418 /* Set load_base, which will be handled later. */
4419 os->load_base = exp_intop (os->lma_region->current);
4420 os->lma_region->current +=
4421 TO_ADDR (os->bfd_section->size);
4423 if (check_regions)
4424 os_region_check (os, os->lma_region, NULL,
4425 os->lma_region->current);
4429 break;
4431 case lang_constructors_statement_enum:
4432 dot = lang_size_sections_1 (constructor_list.head,
4433 output_section_statement,
4434 &s->wild_statement.children.head,
4435 fill, dot, relax, check_regions);
4436 break;
4438 case lang_data_statement_enum:
4440 unsigned int size = 0;
4442 s->data_statement.output_offset =
4443 dot - output_section_statement->bfd_section->vma;
4444 s->data_statement.output_section =
4445 output_section_statement->bfd_section;
4447 /* We might refer to provided symbols in the expression, and
4448 need to mark them as needed. */
4449 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4451 switch (s->data_statement.type)
4453 default:
4454 abort ();
4455 case QUAD:
4456 case SQUAD:
4457 size = QUAD_SIZE;
4458 break;
4459 case LONG:
4460 size = LONG_SIZE;
4461 break;
4462 case SHORT:
4463 size = SHORT_SIZE;
4464 break;
4465 case BYTE:
4466 size = BYTE_SIZE;
4467 break;
4469 if (size < TO_SIZE ((unsigned) 1))
4470 size = TO_SIZE ((unsigned) 1);
4471 dot += TO_ADDR (size);
4472 output_section_statement->bfd_section->size += size;
4474 break;
4476 case lang_reloc_statement_enum:
4478 int size;
4480 s->reloc_statement.output_offset =
4481 dot - output_section_statement->bfd_section->vma;
4482 s->reloc_statement.output_section =
4483 output_section_statement->bfd_section;
4484 size = bfd_get_reloc_size (s->reloc_statement.howto);
4485 dot += TO_ADDR (size);
4486 output_section_statement->bfd_section->size += size;
4488 break;
4490 case lang_wild_statement_enum:
4491 dot = lang_size_sections_1 (s->wild_statement.children.head,
4492 output_section_statement,
4493 &s->wild_statement.children.head,
4494 fill, dot, relax, check_regions);
4495 break;
4497 case lang_object_symbols_statement_enum:
4498 link_info.create_object_symbols_section =
4499 output_section_statement->bfd_section;
4500 break;
4502 case lang_output_statement_enum:
4503 case lang_target_statement_enum:
4504 break;
4506 case lang_input_section_enum:
4508 asection *i;
4510 i = (*prev)->input_section.section;
4511 if (relax)
4513 bfd_boolean again;
4515 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4516 einfo (_("%P%F: can't relax section: %E\n"));
4517 if (again)
4518 *relax = TRUE;
4520 dot = size_input_section (prev, output_section_statement,
4521 output_section_statement->fill, dot);
4523 break;
4525 case lang_input_statement_enum:
4526 break;
4528 case lang_fill_statement_enum:
4529 s->fill_statement.output_section =
4530 output_section_statement->bfd_section;
4532 fill = s->fill_statement.fill;
4533 break;
4535 case lang_assignment_statement_enum:
4537 bfd_vma newdot = dot;
4539 exp_fold_tree (s->assignment_statement.exp,
4540 output_section_statement->bfd_section,
4541 &newdot);
4543 if (newdot != dot && !output_section_statement->ignored)
4545 if (output_section_statement == abs_output_section)
4547 /* If we don't have an output section, then just adjust
4548 the default memory address. */
4549 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4550 FALSE)->current = newdot;
4552 else
4554 /* Insert a pad after this statement. We can't
4555 put the pad before when relaxing, in case the
4556 assignment references dot. */
4557 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4558 output_section_statement->bfd_section, dot);
4560 /* Don't neuter the pad below when relaxing. */
4561 s = s->header.next;
4563 /* If dot is advanced, this implies that the section
4564 should have space allocated to it, unless the
4565 user has explicitly stated that the section
4566 should never be loaded. */
4567 if (!(output_section_statement->flags
4568 & (SEC_NEVER_LOAD | SEC_ALLOC)))
4569 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4571 dot = newdot;
4574 break;
4576 case lang_padding_statement_enum:
4577 /* If this is the first time lang_size_sections is called,
4578 we won't have any padding statements. If this is the
4579 second or later passes when relaxing, we should allow
4580 padding to shrink. If padding is needed on this pass, it
4581 will be added back in. */
4582 s->padding_statement.size = 0;
4584 /* Make sure output_offset is valid. If relaxation shrinks
4585 the section and this pad isn't needed, it's possible to
4586 have output_offset larger than the final size of the
4587 section. bfd_set_section_contents will complain even for
4588 a pad size of zero. */
4589 s->padding_statement.output_offset
4590 = dot - output_section_statement->bfd_section->vma;
4591 break;
4593 case lang_group_statement_enum:
4594 dot = lang_size_sections_1 (s->group_statement.children.head,
4595 output_section_statement,
4596 &s->group_statement.children.head,
4597 fill, dot, relax, check_regions);
4598 break;
4600 default:
4601 FAIL ();
4602 break;
4604 /* We can only get here when relaxing is turned on. */
4605 case lang_address_statement_enum:
4606 break;
4608 prev = &s->header.next;
4610 return dot;
4613 void
4614 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
4616 lang_statement_iteration++;
4617 lang_size_sections_1 (statement_list.head, abs_output_section,
4618 &statement_list.head, 0, 0, relax, check_regions);
4621 void
4622 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4624 expld.phase = lang_allocating_phase_enum;
4625 expld.dataseg.phase = exp_dataseg_none;
4627 one_lang_size_sections_pass (relax, check_regions);
4628 if (expld.dataseg.phase == exp_dataseg_end_seen
4629 && link_info.relro && expld.dataseg.relro_end)
4631 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4632 to put expld.dataseg.relro on a (common) page boundary. */
4633 bfd_vma old_min_base, relro_end, maxpage;
4635 expld.dataseg.phase = exp_dataseg_relro_adjust;
4636 old_min_base = expld.dataseg.min_base;
4637 maxpage = expld.dataseg.maxpagesize;
4638 expld.dataseg.base += (-expld.dataseg.relro_end
4639 & (expld.dataseg.pagesize - 1));
4640 /* Compute the expected PT_GNU_RELRO segment end. */
4641 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
4642 & ~(expld.dataseg.pagesize - 1);
4643 if (old_min_base + maxpage < expld.dataseg.base)
4645 expld.dataseg.base -= maxpage;
4646 relro_end -= maxpage;
4648 lang_reset_memory_regions ();
4649 one_lang_size_sections_pass (relax, check_regions);
4650 if (expld.dataseg.relro_end > relro_end)
4652 /* The alignment of sections between DATA_SEGMENT_ALIGN
4653 and DATA_SEGMENT_RELRO_END caused huge padding to be
4654 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4655 asection *sec;
4656 unsigned int max_alignment_power = 0;
4658 /* Find maximum alignment power of sections between
4659 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4660 for (sec = output_bfd->sections; sec; sec = sec->next)
4661 if (sec->vma >= expld.dataseg.base
4662 && sec->vma < expld.dataseg.relro_end
4663 && sec->alignment_power > max_alignment_power)
4664 max_alignment_power = sec->alignment_power;
4666 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
4668 if (expld.dataseg.base - (1 << max_alignment_power)
4669 < old_min_base)
4670 expld.dataseg.base += expld.dataseg.pagesize;
4671 expld.dataseg.base -= (1 << max_alignment_power);
4672 lang_reset_memory_regions ();
4673 one_lang_size_sections_pass (relax, check_regions);
4676 link_info.relro_start = expld.dataseg.base;
4677 link_info.relro_end = expld.dataseg.relro_end;
4679 else if (expld.dataseg.phase == exp_dataseg_end_seen)
4681 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4682 a page could be saved in the data segment. */
4683 bfd_vma first, last;
4685 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
4686 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
4687 if (first && last
4688 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
4689 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
4690 && first + last <= expld.dataseg.pagesize)
4692 expld.dataseg.phase = exp_dataseg_adjust;
4693 lang_reset_memory_regions ();
4694 one_lang_size_sections_pass (relax, check_regions);
4698 expld.phase = lang_final_phase_enum;
4701 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4703 static bfd_vma
4704 lang_do_assignments_1 (lang_statement_union_type *s,
4705 lang_output_section_statement_type *current_os,
4706 fill_type *fill,
4707 bfd_vma dot)
4709 for (; s != NULL; s = s->header.next)
4711 switch (s->header.type)
4713 case lang_constructors_statement_enum:
4714 dot = lang_do_assignments_1 (constructor_list.head,
4715 current_os, fill, dot);
4716 break;
4718 case lang_output_section_statement_enum:
4720 lang_output_section_statement_type *os;
4722 os = &(s->output_section_statement);
4723 if (os->bfd_section != NULL && !os->ignored)
4725 lang_memory_region_type *r;
4727 dot = os->bfd_section->vma;
4728 r = os->region;
4729 if (r == NULL)
4730 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4732 if (os->load_base)
4733 os->bfd_section->lma
4734 = exp_get_abs_int (os->load_base, 0, "load base");
4735 else if (r->last_os != NULL)
4737 asection *last;
4738 bfd_vma lma;
4740 last = r->last_os->output_section_statement.bfd_section;
4742 /* If the current vma overlaps the previous section,
4743 then set the current lma to that at the end of
4744 the previous section. The previous section was
4745 probably an overlay. */
4746 if ((dot >= last->vma
4747 && dot < last->vma + last->size)
4748 || (last->vma >= dot
4749 && last->vma < dot + os->bfd_section->size))
4750 lma = last->lma + last->size;
4752 /* Otherwise, keep the same lma to vma relationship
4753 as the previous section. */
4754 else
4755 lma = dot + last->lma - last->vma;
4757 if (os->section_alignment != -1)
4758 lma = align_power (lma, os->section_alignment);
4759 os->bfd_section->lma = lma;
4762 lang_do_assignments_1 (os->children.head,
4763 os, os->fill, dot);
4765 /* .tbss sections effectively have zero size. */
4766 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4767 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4768 || link_info.relocatable)
4770 dot += TO_ADDR (os->bfd_section->size);
4772 /* Keep track of normal sections using the default
4773 lma region. We use this to set the lma for
4774 following sections. Overlays or other linker
4775 script assignment to lma might mean that the
4776 default lma == vma is incorrect. */
4777 if (!link_info.relocatable
4778 && os->lma_region == NULL)
4779 r->last_os = s;
4783 break;
4785 case lang_wild_statement_enum:
4787 dot = lang_do_assignments_1 (s->wild_statement.children.head,
4788 current_os, fill, dot);
4789 break;
4791 case lang_object_symbols_statement_enum:
4792 case lang_output_statement_enum:
4793 case lang_target_statement_enum:
4794 break;
4796 case lang_data_statement_enum:
4797 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4798 if (expld.result.valid_p)
4799 s->data_statement.value = (expld.result.value
4800 + expld.result.section->vma);
4801 else
4802 einfo (_("%F%P: invalid data statement\n"));
4804 unsigned int size;
4805 switch (s->data_statement.type)
4807 default:
4808 abort ();
4809 case QUAD:
4810 case SQUAD:
4811 size = QUAD_SIZE;
4812 break;
4813 case LONG:
4814 size = LONG_SIZE;
4815 break;
4816 case SHORT:
4817 size = SHORT_SIZE;
4818 break;
4819 case BYTE:
4820 size = BYTE_SIZE;
4821 break;
4823 if (size < TO_SIZE ((unsigned) 1))
4824 size = TO_SIZE ((unsigned) 1);
4825 dot += TO_ADDR (size);
4827 break;
4829 case lang_reloc_statement_enum:
4830 exp_fold_tree (s->reloc_statement.addend_exp,
4831 bfd_abs_section_ptr, &dot);
4832 if (expld.result.valid_p)
4833 s->reloc_statement.addend_value = expld.result.value;
4834 else
4835 einfo (_("%F%P: invalid reloc statement\n"));
4836 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4837 break;
4839 case lang_input_section_enum:
4841 asection *in = s->input_section.section;
4843 if ((in->flags & SEC_EXCLUDE) == 0)
4844 dot += TO_ADDR (in->size);
4846 break;
4848 case lang_input_statement_enum:
4849 break;
4851 case lang_fill_statement_enum:
4852 fill = s->fill_statement.fill;
4853 break;
4855 case lang_assignment_statement_enum:
4856 exp_fold_tree (s->assignment_statement.exp,
4857 current_os->bfd_section,
4858 &dot);
4859 break;
4861 case lang_padding_statement_enum:
4862 dot += TO_ADDR (s->padding_statement.size);
4863 break;
4865 case lang_group_statement_enum:
4866 dot = lang_do_assignments_1 (s->group_statement.children.head,
4867 current_os, fill, dot);
4868 break;
4870 default:
4871 FAIL ();
4872 break;
4874 case lang_address_statement_enum:
4875 break;
4878 return dot;
4881 void
4882 lang_do_assignments (void)
4884 lang_statement_iteration++;
4885 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
4888 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4889 operator .startof. (section_name), it produces an undefined symbol
4890 .startof.section_name. Similarly, when it sees
4891 .sizeof. (section_name), it produces an undefined symbol
4892 .sizeof.section_name. For all the output sections, we look for
4893 such symbols, and set them to the correct value. */
4895 static void
4896 lang_set_startof (void)
4898 asection *s;
4900 if (link_info.relocatable)
4901 return;
4903 for (s = output_bfd->sections; s != NULL; s = s->next)
4905 const char *secname;
4906 char *buf;
4907 struct bfd_link_hash_entry *h;
4909 secname = bfd_get_section_name (output_bfd, s);
4910 buf = xmalloc (10 + strlen (secname));
4912 sprintf (buf, ".startof.%s", secname);
4913 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4914 if (h != NULL && h->type == bfd_link_hash_undefined)
4916 h->type = bfd_link_hash_defined;
4917 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4918 h->u.def.section = bfd_abs_section_ptr;
4921 sprintf (buf, ".sizeof.%s", secname);
4922 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4923 if (h != NULL && h->type == bfd_link_hash_undefined)
4925 h->type = bfd_link_hash_defined;
4926 h->u.def.value = TO_ADDR (s->size);
4927 h->u.def.section = bfd_abs_section_ptr;
4930 free (buf);
4934 static void
4935 lang_end (void)
4937 struct bfd_link_hash_entry *h;
4938 bfd_boolean warn;
4940 if (link_info.relocatable || link_info.shared)
4941 warn = FALSE;
4942 else
4943 warn = TRUE;
4945 if (entry_symbol.name == NULL)
4947 /* No entry has been specified. Look for the default entry, but
4948 don't warn if we don't find it. */
4949 entry_symbol.name = entry_symbol_default;
4950 warn = FALSE;
4953 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4954 FALSE, FALSE, TRUE);
4955 if (h != NULL
4956 && (h->type == bfd_link_hash_defined
4957 || h->type == bfd_link_hash_defweak)
4958 && h->u.def.section->output_section != NULL)
4960 bfd_vma val;
4962 val = (h->u.def.value
4963 + bfd_get_section_vma (output_bfd,
4964 h->u.def.section->output_section)
4965 + h->u.def.section->output_offset);
4966 if (! bfd_set_start_address (output_bfd, val))
4967 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4969 else
4971 bfd_vma val;
4972 const char *send;
4974 /* We couldn't find the entry symbol. Try parsing it as a
4975 number. */
4976 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4977 if (*send == '\0')
4979 if (! bfd_set_start_address (output_bfd, val))
4980 einfo (_("%P%F: can't set start address\n"));
4982 else
4984 asection *ts;
4986 /* Can't find the entry symbol, and it's not a number. Use
4987 the first address in the text section. */
4988 ts = bfd_get_section_by_name (output_bfd, entry_section);
4989 if (ts != NULL)
4991 if (warn)
4992 einfo (_("%P: warning: cannot find entry symbol %s;"
4993 " defaulting to %V\n"),
4994 entry_symbol.name,
4995 bfd_get_section_vma (output_bfd, ts));
4996 if (! bfd_set_start_address (output_bfd,
4997 bfd_get_section_vma (output_bfd,
4998 ts)))
4999 einfo (_("%P%F: can't set start address\n"));
5001 else
5003 if (warn)
5004 einfo (_("%P: warning: cannot find entry symbol %s;"
5005 " not setting start address\n"),
5006 entry_symbol.name);
5011 /* Don't bfd_hash_table_free (&lang_definedness_table);
5012 map file output may result in a call of lang_track_definedness. */
5015 /* This is a small function used when we want to ignore errors from
5016 BFD. */
5018 static void
5019 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5021 /* Don't do anything. */
5024 /* Check that the architecture of all the input files is compatible
5025 with the output file. Also call the backend to let it do any
5026 other checking that is needed. */
5028 static void
5029 lang_check (void)
5031 lang_statement_union_type *file;
5032 bfd *input_bfd;
5033 const bfd_arch_info_type *compatible;
5035 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5037 input_bfd = file->input_statement.the_bfd;
5038 compatible
5039 = bfd_arch_get_compatible (input_bfd, output_bfd,
5040 command_line.accept_unknown_input_arch);
5042 /* In general it is not possible to perform a relocatable
5043 link between differing object formats when the input
5044 file has relocations, because the relocations in the
5045 input format may not have equivalent representations in
5046 the output format (and besides BFD does not translate
5047 relocs for other link purposes than a final link). */
5048 if ((link_info.relocatable || link_info.emitrelocations)
5049 && (compatible == NULL
5050 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
5051 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5053 einfo (_("%P%F: Relocatable linking with relocations from"
5054 " format %s (%B) to format %s (%B) is not supported\n"),
5055 bfd_get_target (input_bfd), input_bfd,
5056 bfd_get_target (output_bfd), output_bfd);
5057 /* einfo with %F exits. */
5060 if (compatible == NULL)
5062 if (command_line.warn_mismatch)
5063 einfo (_("%P: warning: %s architecture of input file `%B'"
5064 " is incompatible with %s output\n"),
5065 bfd_printable_name (input_bfd), input_bfd,
5066 bfd_printable_name (output_bfd));
5068 else if (bfd_count_sections (input_bfd))
5070 /* If the input bfd has no contents, it shouldn't set the
5071 private data of the output bfd. */
5073 bfd_error_handler_type pfn = NULL;
5075 /* If we aren't supposed to warn about mismatched input
5076 files, temporarily set the BFD error handler to a
5077 function which will do nothing. We still want to call
5078 bfd_merge_private_bfd_data, since it may set up
5079 information which is needed in the output file. */
5080 if (! command_line.warn_mismatch)
5081 pfn = bfd_set_error_handler (ignore_bfd_errors);
5082 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
5084 if (command_line.warn_mismatch)
5085 einfo (_("%P%X: failed to merge target specific data"
5086 " of file %B\n"), input_bfd);
5088 if (! command_line.warn_mismatch)
5089 bfd_set_error_handler (pfn);
5094 /* Look through all the global common symbols and attach them to the
5095 correct section. The -sort-common command line switch may be used
5096 to roughly sort the entries by size. */
5098 static void
5099 lang_common (void)
5101 if (command_line.inhibit_common_definition)
5102 return;
5103 if (link_info.relocatable
5104 && ! command_line.force_common_definition)
5105 return;
5107 if (! config.sort_common)
5108 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5109 else
5111 int power;
5113 for (power = 4; power >= 0; power--)
5114 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5118 /* Place one common symbol in the correct section. */
5120 static bfd_boolean
5121 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5123 unsigned int power_of_two;
5124 bfd_vma size;
5125 asection *section;
5127 if (h->type != bfd_link_hash_common)
5128 return TRUE;
5130 size = h->u.c.size;
5131 power_of_two = h->u.c.p->alignment_power;
5133 if (config.sort_common
5134 && power_of_two < (unsigned int) *(int *) info)
5135 return TRUE;
5137 section = h->u.c.p->section;
5139 /* Increase the size of the section to align the common sym. */
5140 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5141 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
5143 /* Adjust the alignment if necessary. */
5144 if (power_of_two > section->alignment_power)
5145 section->alignment_power = power_of_two;
5147 /* Change the symbol from common to defined. */
5148 h->type = bfd_link_hash_defined;
5149 h->u.def.section = section;
5150 h->u.def.value = section->size;
5152 /* Increase the size of the section. */
5153 section->size += size;
5155 /* Make sure the section is allocated in memory, and make sure that
5156 it is no longer a common section. */
5157 section->flags |= SEC_ALLOC;
5158 section->flags &= ~SEC_IS_COMMON;
5160 if (config.map_file != NULL)
5162 static bfd_boolean header_printed;
5163 int len;
5164 char *name;
5165 char buf[50];
5167 if (! header_printed)
5169 minfo (_("\nAllocating common symbols\n"));
5170 minfo (_("Common symbol size file\n\n"));
5171 header_printed = TRUE;
5174 name = demangle (h->root.string);
5175 minfo ("%s", name);
5176 len = strlen (name);
5177 free (name);
5179 if (len >= 19)
5181 print_nl ();
5182 len = 0;
5184 while (len < 20)
5186 print_space ();
5187 ++len;
5190 minfo ("0x");
5191 if (size <= 0xffffffff)
5192 sprintf (buf, "%lx", (unsigned long) size);
5193 else
5194 sprintf_vma (buf, size);
5195 minfo ("%s", buf);
5196 len = strlen (buf);
5198 while (len < 16)
5200 print_space ();
5201 ++len;
5204 minfo ("%B\n", section->owner);
5207 return TRUE;
5210 /* Run through the input files and ensure that every input section has
5211 somewhere to go. If one is found without a destination then create
5212 an input request and place it into the statement tree. */
5214 static void
5215 lang_place_orphans (void)
5217 LANG_FOR_EACH_INPUT_STATEMENT (file)
5219 asection *s;
5221 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5223 if (s->output_section == NULL)
5225 /* This section of the file is not attached, root
5226 around for a sensible place for it to go. */
5228 if (file->just_syms_flag)
5229 bfd_link_just_syms (file->the_bfd, s, &link_info);
5230 else if ((s->flags & SEC_EXCLUDE) != 0)
5231 s->output_section = bfd_abs_section_ptr;
5232 else if (strcmp (s->name, "COMMON") == 0)
5234 /* This is a lonely common section which must have
5235 come from an archive. We attach to the section
5236 with the wildcard. */
5237 if (! link_info.relocatable
5238 || command_line.force_common_definition)
5240 if (default_common_section == NULL)
5242 default_common_section =
5243 lang_output_section_statement_lookup (".bss");
5246 lang_add_section (&default_common_section->children, s,
5247 default_common_section);
5250 else if (ldemul_place_orphan (s))
5252 else
5254 lang_output_section_statement_type *os;
5256 os = lang_output_section_statement_lookup (s->name);
5257 lang_add_section (&os->children, s, os);
5264 void
5265 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5267 flagword *ptr_flags;
5269 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5270 while (*flags)
5272 switch (*flags)
5274 case 'A': case 'a':
5275 *ptr_flags |= SEC_ALLOC;
5276 break;
5278 case 'R': case 'r':
5279 *ptr_flags |= SEC_READONLY;
5280 break;
5282 case 'W': case 'w':
5283 *ptr_flags |= SEC_DATA;
5284 break;
5286 case 'X': case 'x':
5287 *ptr_flags |= SEC_CODE;
5288 break;
5290 case 'L': case 'l':
5291 case 'I': case 'i':
5292 *ptr_flags |= SEC_LOAD;
5293 break;
5295 default:
5296 einfo (_("%P%F: invalid syntax in flags\n"));
5297 break;
5299 flags++;
5303 /* Call a function on each input file. This function will be called
5304 on an archive, but not on the elements. */
5306 void
5307 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5309 lang_input_statement_type *f;
5311 for (f = (lang_input_statement_type *) input_file_chain.head;
5312 f != NULL;
5313 f = (lang_input_statement_type *) f->next_real_file)
5314 func (f);
5317 /* Call a function on each file. The function will be called on all
5318 the elements of an archive which are included in the link, but will
5319 not be called on the archive file itself. */
5321 void
5322 lang_for_each_file (void (*func) (lang_input_statement_type *))
5324 LANG_FOR_EACH_INPUT_STATEMENT (f)
5326 func (f);
5330 void
5331 ldlang_add_file (lang_input_statement_type *entry)
5333 bfd **pp;
5335 lang_statement_append (&file_chain,
5336 (lang_statement_union_type *) entry,
5337 &entry->next);
5339 /* The BFD linker needs to have a list of all input BFDs involved in
5340 a link. */
5341 ASSERT (entry->the_bfd->link_next == NULL);
5342 ASSERT (entry->the_bfd != output_bfd);
5343 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
5345 *pp = entry->the_bfd;
5346 entry->the_bfd->usrdata = entry;
5347 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5349 /* Look through the sections and check for any which should not be
5350 included in the link. We need to do this now, so that we can
5351 notice when the backend linker tries to report multiple
5352 definition errors for symbols which are in sections we aren't
5353 going to link. FIXME: It might be better to entirely ignore
5354 symbols which are defined in sections which are going to be
5355 discarded. This would require modifying the backend linker for
5356 each backend which might set the SEC_LINK_ONCE flag. If we do
5357 this, we should probably handle SEC_EXCLUDE in the same way. */
5359 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5362 void
5363 lang_add_output (const char *name, int from_script)
5365 /* Make -o on command line override OUTPUT in script. */
5366 if (!had_output_filename || !from_script)
5368 output_filename = name;
5369 had_output_filename = TRUE;
5373 static lang_output_section_statement_type *current_section;
5375 static int
5376 topower (int x)
5378 unsigned int i = 1;
5379 int l;
5381 if (x < 0)
5382 return -1;
5384 for (l = 0; l < 32; l++)
5386 if (i >= (unsigned int) x)
5387 return l;
5388 i <<= 1;
5391 return 0;
5394 lang_output_section_statement_type *
5395 lang_enter_output_section_statement (const char *output_section_statement_name,
5396 etree_type *address_exp,
5397 enum section_type sectype,
5398 etree_type *align,
5399 etree_type *subalign,
5400 etree_type *ebase,
5401 int constraint)
5403 lang_output_section_statement_type *os;
5405 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5406 constraint);
5407 current_section = os;
5409 /* Make next things chain into subchain of this. */
5411 if (os->addr_tree == NULL)
5413 os->addr_tree = address_exp;
5415 os->sectype = sectype;
5416 if (sectype != noload_section)
5417 os->flags = SEC_NO_FLAGS;
5418 else
5419 os->flags = SEC_NEVER_LOAD;
5420 os->block_value = 1;
5421 stat_ptr = &os->children;
5423 os->subsection_alignment =
5424 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5425 os->section_alignment =
5426 topower (exp_get_value_int (align, -1, "section alignment"));
5428 os->load_base = ebase;
5429 return os;
5432 void
5433 lang_final (void)
5435 lang_output_statement_type *new;
5437 new = new_stat (lang_output_statement, stat_ptr);
5438 new->name = output_filename;
5441 /* Reset the current counters in the regions. */
5443 void
5444 lang_reset_memory_regions (void)
5446 lang_memory_region_type *p = lang_memory_region_list;
5447 asection *o;
5448 lang_output_section_statement_type *os;
5450 for (p = lang_memory_region_list; p != NULL; p = p->next)
5452 p->current = p->origin;
5453 p->last_os = NULL;
5456 for (os = &lang_output_section_statement.head->output_section_statement;
5457 os != NULL;
5458 os = os->next)
5459 os->processed = FALSE;
5461 for (o = output_bfd->sections; o != NULL; o = o->next)
5463 /* Save the last size for possible use by bfd_relax_section. */
5464 o->rawsize = o->size;
5465 o->size = 0;
5469 /* Worker for lang_gc_sections_1. */
5471 static void
5472 gc_section_callback (lang_wild_statement_type *ptr,
5473 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5474 asection *section,
5475 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5476 void *data ATTRIBUTE_UNUSED)
5478 /* If the wild pattern was marked KEEP, the member sections
5479 should be as well. */
5480 if (ptr->keep_sections)
5481 section->flags |= SEC_KEEP;
5484 /* Iterate over sections marking them against GC. */
5486 static void
5487 lang_gc_sections_1 (lang_statement_union_type *s)
5489 for (; s != NULL; s = s->header.next)
5491 switch (s->header.type)
5493 case lang_wild_statement_enum:
5494 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5495 break;
5496 case lang_constructors_statement_enum:
5497 lang_gc_sections_1 (constructor_list.head);
5498 break;
5499 case lang_output_section_statement_enum:
5500 lang_gc_sections_1 (s->output_section_statement.children.head);
5501 break;
5502 case lang_group_statement_enum:
5503 lang_gc_sections_1 (s->group_statement.children.head);
5504 break;
5505 default:
5506 break;
5511 static void
5512 lang_gc_sections (void)
5514 struct bfd_link_hash_entry *h;
5515 ldlang_undef_chain_list_type *ulist;
5517 /* Keep all sections so marked in the link script. */
5519 lang_gc_sections_1 (statement_list.head);
5521 /* Keep all sections containing symbols undefined on the command-line,
5522 and the section containing the entry symbol. */
5524 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
5526 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
5527 FALSE, FALSE, FALSE);
5529 if (h != NULL
5530 && (h->type == bfd_link_hash_defined
5531 || h->type == bfd_link_hash_defweak)
5532 && ! bfd_is_abs_section (h->u.def.section))
5534 h->u.def.section->flags |= SEC_KEEP;
5538 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5539 the special case of debug info. (See bfd/stabs.c)
5540 Twiddle the flag here, to simplify later linker code. */
5541 if (link_info.relocatable)
5543 LANG_FOR_EACH_INPUT_STATEMENT (f)
5545 asection *sec;
5546 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5547 if ((sec->flags & SEC_DEBUGGING) == 0)
5548 sec->flags &= ~SEC_EXCLUDE;
5552 if (link_info.gc_sections)
5553 bfd_gc_sections (output_bfd, &link_info);
5556 /* Relax all sections until bfd_relax_section gives up. */
5558 static void
5559 relax_sections (void)
5561 /* Keep relaxing until bfd_relax_section gives up. */
5562 bfd_boolean relax_again;
5566 relax_again = FALSE;
5568 /* Note: pe-dll.c does something like this also. If you find
5569 you need to change this code, you probably need to change
5570 pe-dll.c also. DJ */
5572 /* Do all the assignments with our current guesses as to
5573 section sizes. */
5574 lang_do_assignments ();
5576 /* We must do this after lang_do_assignments, because it uses
5577 size. */
5578 lang_reset_memory_regions ();
5580 /* Perform another relax pass - this time we know where the
5581 globals are, so can make a better guess. */
5582 lang_size_sections (&relax_again, FALSE);
5584 while (relax_again);
5587 void
5588 lang_process (void)
5590 current_target = default_target;
5592 /* Open the output file. */
5593 lang_for_each_statement (ldlang_open_output);
5594 init_opb ();
5596 ldemul_create_output_section_statements ();
5598 /* Add to the hash table all undefineds on the command line. */
5599 lang_place_undefineds ();
5601 if (!bfd_section_already_linked_table_init ())
5602 einfo (_("%P%F: Failed to create hash table\n"));
5604 /* Create a bfd for each input file. */
5605 current_target = default_target;
5606 open_input_bfds (statement_list.head, FALSE);
5608 link_info.gc_sym_list = &entry_symbol;
5609 if (entry_symbol.name == NULL)
5610 link_info.gc_sym_list = ldlang_undef_chain_list_head;
5612 ldemul_after_open ();
5614 bfd_section_already_linked_table_free ();
5616 /* Make sure that we're not mixing architectures. We call this
5617 after all the input files have been opened, but before we do any
5618 other processing, so that any operations merge_private_bfd_data
5619 does on the output file will be known during the rest of the
5620 link. */
5621 lang_check ();
5623 /* Handle .exports instead of a version script if we're told to do so. */
5624 if (command_line.version_exports_section)
5625 lang_do_version_exports_section ();
5627 /* Build all sets based on the information gathered from the input
5628 files. */
5629 ldctor_build_sets ();
5631 /* Remove unreferenced sections if asked to. */
5632 lang_gc_sections ();
5634 /* Size up the common data. */
5635 lang_common ();
5637 /* Update wild statements. */
5638 update_wild_statements (statement_list.head);
5640 /* Run through the contours of the script and attach input sections
5641 to the correct output sections. */
5642 map_input_to_output_sections (statement_list.head, NULL, NULL);
5644 /* Find any sections not attached explicitly and handle them. */
5645 lang_place_orphans ();
5647 if (! link_info.relocatable)
5649 asection *found;
5651 /* Merge SEC_MERGE sections. This has to be done after GC of
5652 sections, so that GCed sections are not merged, but before
5653 assigning dynamic symbols, since removing whole input sections
5654 is hard then. */
5655 bfd_merge_sections (output_bfd, &link_info);
5657 /* Look for a text section and set the readonly attribute in it. */
5658 found = bfd_get_section_by_name (output_bfd, ".text");
5660 if (found != NULL)
5662 if (config.text_read_only)
5663 found->flags |= SEC_READONLY;
5664 else
5665 found->flags &= ~SEC_READONLY;
5669 /* Do anything special before sizing sections. This is where ELF
5670 and other back-ends size dynamic sections. */
5671 ldemul_before_allocation ();
5673 /* We must record the program headers before we try to fix the
5674 section positions, since they will affect SIZEOF_HEADERS. */
5675 lang_record_phdrs ();
5677 /* Size up the sections. */
5678 lang_size_sections (NULL, !command_line.relax);
5680 /* Now run around and relax if we can. */
5681 if (command_line.relax)
5683 /* We may need more than one relaxation pass. */
5684 int i = link_info.relax_pass;
5686 /* The backend can use it to determine the current pass. */
5687 link_info.relax_pass = 0;
5689 while (i--)
5691 relax_sections ();
5692 link_info.relax_pass++;
5695 /* Final extra sizing to report errors. */
5696 lang_do_assignments ();
5697 lang_reset_memory_regions ();
5698 lang_size_sections (NULL, TRUE);
5701 /* See if anything special should be done now we know how big
5702 everything is. */
5703 ldemul_after_allocation ();
5705 /* Fix any .startof. or .sizeof. symbols. */
5706 lang_set_startof ();
5708 /* Do all the assignments, now that we know the final resting places
5709 of all the symbols. */
5711 lang_do_assignments ();
5713 ldemul_finish ();
5715 /* Make sure that the section addresses make sense. */
5716 if (! link_info.relocatable
5717 && command_line.check_section_addresses)
5718 lang_check_section_addresses ();
5720 lang_end ();
5723 /* EXPORTED TO YACC */
5725 void
5726 lang_add_wild (struct wildcard_spec *filespec,
5727 struct wildcard_list *section_list,
5728 bfd_boolean keep_sections)
5730 struct wildcard_list *curr, *next;
5731 lang_wild_statement_type *new;
5733 /* Reverse the list as the parser puts it back to front. */
5734 for (curr = section_list, section_list = NULL;
5735 curr != NULL;
5736 section_list = curr, curr = next)
5738 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
5739 placed_commons = TRUE;
5741 next = curr->next;
5742 curr->next = section_list;
5745 if (filespec != NULL && filespec->name != NULL)
5747 if (strcmp (filespec->name, "*") == 0)
5748 filespec->name = NULL;
5749 else if (! wildcardp (filespec->name))
5750 lang_has_input_file = TRUE;
5753 new = new_stat (lang_wild_statement, stat_ptr);
5754 new->filename = NULL;
5755 new->filenames_sorted = FALSE;
5756 if (filespec != NULL)
5758 new->filename = filespec->name;
5759 new->filenames_sorted = filespec->sorted == by_name;
5761 new->section_list = section_list;
5762 new->keep_sections = keep_sections;
5763 lang_list_init (&new->children);
5764 analyze_walk_wild_section_handler (new);
5767 void
5768 lang_section_start (const char *name, etree_type *address,
5769 const segment_type *segment)
5771 lang_address_statement_type *ad;
5773 ad = new_stat (lang_address_statement, stat_ptr);
5774 ad->section_name = name;
5775 ad->address = address;
5776 ad->segment = segment;
5779 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5780 because of a -e argument on the command line, or zero if this is
5781 called by ENTRY in a linker script. Command line arguments take
5782 precedence. */
5784 void
5785 lang_add_entry (const char *name, bfd_boolean cmdline)
5787 if (entry_symbol.name == NULL
5788 || cmdline
5789 || ! entry_from_cmdline)
5791 entry_symbol.name = name;
5792 entry_from_cmdline = cmdline;
5796 /* Set the default start symbol to NAME. .em files should use this,
5797 not lang_add_entry, to override the use of "start" if neither the
5798 linker script nor the command line specifies an entry point. NAME
5799 must be permanently allocated. */
5800 void
5801 lang_default_entry (const char *name)
5803 entry_symbol_default = name;
5806 void
5807 lang_add_target (const char *name)
5809 lang_target_statement_type *new;
5811 new = new_stat (lang_target_statement, stat_ptr);
5812 new->target = name;
5815 void
5816 lang_add_map (const char *name)
5818 while (*name)
5820 switch (*name)
5822 case 'F':
5823 map_option_f = TRUE;
5824 break;
5826 name++;
5830 void
5831 lang_add_fill (fill_type *fill)
5833 lang_fill_statement_type *new;
5835 new = new_stat (lang_fill_statement, stat_ptr);
5836 new->fill = fill;
5839 void
5840 lang_add_data (int type, union etree_union *exp)
5842 lang_data_statement_type *new;
5844 new = new_stat (lang_data_statement, stat_ptr);
5845 new->exp = exp;
5846 new->type = type;
5849 /* Create a new reloc statement. RELOC is the BFD relocation type to
5850 generate. HOWTO is the corresponding howto structure (we could
5851 look this up, but the caller has already done so). SECTION is the
5852 section to generate a reloc against, or NAME is the name of the
5853 symbol to generate a reloc against. Exactly one of SECTION and
5854 NAME must be NULL. ADDEND is an expression for the addend. */
5856 void
5857 lang_add_reloc (bfd_reloc_code_real_type reloc,
5858 reloc_howto_type *howto,
5859 asection *section,
5860 const char *name,
5861 union etree_union *addend)
5863 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5865 p->reloc = reloc;
5866 p->howto = howto;
5867 p->section = section;
5868 p->name = name;
5869 p->addend_exp = addend;
5871 p->addend_value = 0;
5872 p->output_section = NULL;
5873 p->output_offset = 0;
5876 lang_assignment_statement_type *
5877 lang_add_assignment (etree_type *exp)
5879 lang_assignment_statement_type *new;
5881 new = new_stat (lang_assignment_statement, stat_ptr);
5882 new->exp = exp;
5883 return new;
5886 void
5887 lang_add_attribute (enum statement_enum attribute)
5889 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
5892 void
5893 lang_startup (const char *name)
5895 if (startup_file != NULL)
5897 einfo (_("%P%F: multiple STARTUP files\n"));
5899 first_file->filename = name;
5900 first_file->local_sym_name = name;
5901 first_file->real = TRUE;
5903 startup_file = name;
5906 void
5907 lang_float (bfd_boolean maybe)
5909 lang_float_flag = maybe;
5913 /* Work out the load- and run-time regions from a script statement, and
5914 store them in *LMA_REGION and *REGION respectively.
5916 MEMSPEC is the name of the run-time region, or the value of
5917 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5918 LMA_MEMSPEC is the name of the load-time region, or null if the
5919 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5920 had an explicit load address.
5922 It is an error to specify both a load region and a load address. */
5924 static void
5925 lang_get_regions (lang_memory_region_type **region,
5926 lang_memory_region_type **lma_region,
5927 const char *memspec,
5928 const char *lma_memspec,
5929 bfd_boolean have_lma,
5930 bfd_boolean have_vma)
5932 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5934 /* If no runtime region or VMA has been specified, but the load region
5935 has been specified, then use the load region for the runtime region
5936 as well. */
5937 if (lma_memspec != NULL
5938 && ! have_vma
5939 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5940 *region = *lma_region;
5941 else
5942 *region = lang_memory_region_lookup (memspec, FALSE);
5944 if (have_lma && lma_memspec != 0)
5945 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5948 void
5949 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5950 lang_output_section_phdr_list *phdrs,
5951 const char *lma_memspec)
5953 lang_get_regions (&current_section->region,
5954 &current_section->lma_region,
5955 memspec, lma_memspec,
5956 current_section->load_base != NULL,
5957 current_section->addr_tree != NULL);
5958 current_section->fill = fill;
5959 current_section->phdrs = phdrs;
5960 stat_ptr = &statement_list;
5963 /* Create an absolute symbol with the given name with the value of the
5964 address of first byte of the section named.
5966 If the symbol already exists, then do nothing. */
5968 void
5969 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5971 struct bfd_link_hash_entry *h;
5973 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5974 if (h == NULL)
5975 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5977 if (h->type == bfd_link_hash_new
5978 || h->type == bfd_link_hash_undefined)
5980 asection *sec;
5982 h->type = bfd_link_hash_defined;
5984 sec = bfd_get_section_by_name (output_bfd, secname);
5985 if (sec == NULL)
5986 h->u.def.value = 0;
5987 else
5988 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5990 h->u.def.section = bfd_abs_section_ptr;
5994 /* Create an absolute symbol with the given name with the value of the
5995 address of the first byte after the end of the section named.
5997 If the symbol already exists, then do nothing. */
5999 void
6000 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6002 struct bfd_link_hash_entry *h;
6004 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6005 if (h == NULL)
6006 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6008 if (h->type == bfd_link_hash_new
6009 || h->type == bfd_link_hash_undefined)
6011 asection *sec;
6013 h->type = bfd_link_hash_defined;
6015 sec = bfd_get_section_by_name (output_bfd, secname);
6016 if (sec == NULL)
6017 h->u.def.value = 0;
6018 else
6019 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
6020 + TO_ADDR (sec->size));
6022 h->u.def.section = bfd_abs_section_ptr;
6026 void
6027 lang_statement_append (lang_statement_list_type *list,
6028 lang_statement_union_type *element,
6029 lang_statement_union_type **field)
6031 *(list->tail) = element;
6032 list->tail = field;
6035 /* Set the output format type. -oformat overrides scripts. */
6037 void
6038 lang_add_output_format (const char *format,
6039 const char *big,
6040 const char *little,
6041 int from_script)
6043 if (output_target == NULL || !from_script)
6045 if (command_line.endian == ENDIAN_BIG
6046 && big != NULL)
6047 format = big;
6048 else if (command_line.endian == ENDIAN_LITTLE
6049 && little != NULL)
6050 format = little;
6052 output_target = format;
6056 /* Enter a group. This creates a new lang_group_statement, and sets
6057 stat_ptr to build new statements within the group. */
6059 void
6060 lang_enter_group (void)
6062 lang_group_statement_type *g;
6064 g = new_stat (lang_group_statement, stat_ptr);
6065 lang_list_init (&g->children);
6066 stat_ptr = &g->children;
6069 /* Leave a group. This just resets stat_ptr to start writing to the
6070 regular list of statements again. Note that this will not work if
6071 groups can occur inside anything else which can adjust stat_ptr,
6072 but currently they can't. */
6074 void
6075 lang_leave_group (void)
6077 stat_ptr = &statement_list;
6080 /* Add a new program header. This is called for each entry in a PHDRS
6081 command in a linker script. */
6083 void
6084 lang_new_phdr (const char *name,
6085 etree_type *type,
6086 bfd_boolean filehdr,
6087 bfd_boolean phdrs,
6088 etree_type *at,
6089 etree_type *flags)
6091 struct lang_phdr *n, **pp;
6093 n = stat_alloc (sizeof (struct lang_phdr));
6094 n->next = NULL;
6095 n->name = name;
6096 n->type = exp_get_value_int (type, 0, "program header type");
6097 n->filehdr = filehdr;
6098 n->phdrs = phdrs;
6099 n->at = at;
6100 n->flags = flags;
6102 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6104 *pp = n;
6107 /* Record the program header information in the output BFD. FIXME: We
6108 should not be calling an ELF specific function here. */
6110 static void
6111 lang_record_phdrs (void)
6113 unsigned int alc;
6114 asection **secs;
6115 lang_output_section_phdr_list *last;
6116 struct lang_phdr *l;
6117 lang_output_section_statement_type *os;
6119 alc = 10;
6120 secs = xmalloc (alc * sizeof (asection *));
6121 last = NULL;
6122 for (l = lang_phdr_list; l != NULL; l = l->next)
6124 unsigned int c;
6125 flagword flags;
6126 bfd_vma at;
6128 c = 0;
6129 for (os = &lang_output_section_statement.head->output_section_statement;
6130 os != NULL;
6131 os = os->next)
6133 lang_output_section_phdr_list *pl;
6135 if (os->constraint == -1)
6136 continue;
6138 pl = os->phdrs;
6139 if (pl != NULL)
6140 last = pl;
6141 else
6143 if (os->sectype == noload_section
6144 || os->bfd_section == NULL
6145 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6146 continue;
6147 pl = last;
6150 if (os->bfd_section == NULL)
6151 continue;
6153 for (; pl != NULL; pl = pl->next)
6155 if (strcmp (pl->name, l->name) == 0)
6157 if (c >= alc)
6159 alc *= 2;
6160 secs = xrealloc (secs, alc * sizeof (asection *));
6162 secs[c] = os->bfd_section;
6163 ++c;
6164 pl->used = TRUE;
6169 if (l->flags == NULL)
6170 flags = 0;
6171 else
6172 flags = exp_get_vma (l->flags, 0, "phdr flags");
6174 if (l->at == NULL)
6175 at = 0;
6176 else
6177 at = exp_get_vma (l->at, 0, "phdr load address");
6179 if (! bfd_record_phdr (output_bfd, l->type,
6180 l->flags != NULL, flags, l->at != NULL,
6181 at, l->filehdr, l->phdrs, c, secs))
6182 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6185 free (secs);
6187 /* Make sure all the phdr assignments succeeded. */
6188 for (os = &lang_output_section_statement.head->output_section_statement;
6189 os != NULL;
6190 os = os->next)
6192 lang_output_section_phdr_list *pl;
6194 if (os->constraint == -1
6195 || os->bfd_section == NULL)
6196 continue;
6198 for (pl = os->phdrs;
6199 pl != NULL;
6200 pl = pl->next)
6201 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6202 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6203 os->name, pl->name);
6207 /* Record a list of sections which may not be cross referenced. */
6209 void
6210 lang_add_nocrossref (lang_nocrossref_type *l)
6212 struct lang_nocrossrefs *n;
6214 n = xmalloc (sizeof *n);
6215 n->next = nocrossref_list;
6216 n->list = l;
6217 nocrossref_list = n;
6219 /* Set notice_all so that we get informed about all symbols. */
6220 link_info.notice_all = TRUE;
6223 /* Overlay handling. We handle overlays with some static variables. */
6225 /* The overlay virtual address. */
6226 static etree_type *overlay_vma;
6227 /* And subsection alignment. */
6228 static etree_type *overlay_subalign;
6230 /* An expression for the maximum section size seen so far. */
6231 static etree_type *overlay_max;
6233 /* A list of all the sections in this overlay. */
6235 struct overlay_list {
6236 struct overlay_list *next;
6237 lang_output_section_statement_type *os;
6240 static struct overlay_list *overlay_list;
6242 /* Start handling an overlay. */
6244 void
6245 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6247 /* The grammar should prevent nested overlays from occurring. */
6248 ASSERT (overlay_vma == NULL
6249 && overlay_subalign == NULL
6250 && overlay_max == NULL);
6252 overlay_vma = vma_expr;
6253 overlay_subalign = subalign;
6256 /* Start a section in an overlay. We handle this by calling
6257 lang_enter_output_section_statement with the correct VMA.
6258 lang_leave_overlay sets up the LMA and memory regions. */
6260 void
6261 lang_enter_overlay_section (const char *name)
6263 struct overlay_list *n;
6264 etree_type *size;
6266 lang_enter_output_section_statement (name, overlay_vma, normal_section,
6267 0, overlay_subalign, 0, 0);
6269 /* If this is the first section, then base the VMA of future
6270 sections on this one. This will work correctly even if `.' is
6271 used in the addresses. */
6272 if (overlay_list == NULL)
6273 overlay_vma = exp_nameop (ADDR, name);
6275 /* Remember the section. */
6276 n = xmalloc (sizeof *n);
6277 n->os = current_section;
6278 n->next = overlay_list;
6279 overlay_list = n;
6281 size = exp_nameop (SIZEOF, name);
6283 /* Arrange to work out the maximum section end address. */
6284 if (overlay_max == NULL)
6285 overlay_max = size;
6286 else
6287 overlay_max = exp_binop (MAX_K, overlay_max, size);
6290 /* Finish a section in an overlay. There isn't any special to do
6291 here. */
6293 void
6294 lang_leave_overlay_section (fill_type *fill,
6295 lang_output_section_phdr_list *phdrs)
6297 const char *name;
6298 char *clean, *s2;
6299 const char *s1;
6300 char *buf;
6302 name = current_section->name;
6304 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6305 region and that no load-time region has been specified. It doesn't
6306 really matter what we say here, since lang_leave_overlay will
6307 override it. */
6308 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6310 /* Define the magic symbols. */
6312 clean = xmalloc (strlen (name) + 1);
6313 s2 = clean;
6314 for (s1 = name; *s1 != '\0'; s1++)
6315 if (ISALNUM (*s1) || *s1 == '_')
6316 *s2++ = *s1;
6317 *s2 = '\0';
6319 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6320 sprintf (buf, "__load_start_%s", clean);
6321 lang_add_assignment (exp_assop ('=', buf,
6322 exp_nameop (LOADADDR, name)));
6324 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6325 sprintf (buf, "__load_stop_%s", clean);
6326 lang_add_assignment (exp_assop ('=', buf,
6327 exp_binop ('+',
6328 exp_nameop (LOADADDR, name),
6329 exp_nameop (SIZEOF, name))));
6331 free (clean);
6334 /* Finish an overlay. If there are any overlay wide settings, this
6335 looks through all the sections in the overlay and sets them. */
6337 void
6338 lang_leave_overlay (etree_type *lma_expr,
6339 int nocrossrefs,
6340 fill_type *fill,
6341 const char *memspec,
6342 lang_output_section_phdr_list *phdrs,
6343 const char *lma_memspec)
6345 lang_memory_region_type *region;
6346 lang_memory_region_type *lma_region;
6347 struct overlay_list *l;
6348 lang_nocrossref_type *nocrossref;
6350 lang_get_regions (&region, &lma_region,
6351 memspec, lma_memspec,
6352 lma_expr != NULL, FALSE);
6354 nocrossref = NULL;
6356 /* After setting the size of the last section, set '.' to end of the
6357 overlay region. */
6358 if (overlay_list != NULL)
6359 overlay_list->os->update_dot_tree
6360 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6362 l = overlay_list;
6363 while (l != NULL)
6365 struct overlay_list *next;
6367 if (fill != NULL && l->os->fill == NULL)
6368 l->os->fill = fill;
6370 l->os->region = region;
6371 l->os->lma_region = lma_region;
6373 /* The first section has the load address specified in the
6374 OVERLAY statement. The rest are worked out from that.
6375 The base address is not needed (and should be null) if
6376 an LMA region was specified. */
6377 if (l->next == 0)
6378 l->os->load_base = lma_expr;
6380 if (phdrs != NULL && l->os->phdrs == NULL)
6381 l->os->phdrs = phdrs;
6383 if (nocrossrefs)
6385 lang_nocrossref_type *nc;
6387 nc = xmalloc (sizeof *nc);
6388 nc->name = l->os->name;
6389 nc->next = nocrossref;
6390 nocrossref = nc;
6393 next = l->next;
6394 free (l);
6395 l = next;
6398 if (nocrossref != NULL)
6399 lang_add_nocrossref (nocrossref);
6401 overlay_vma = NULL;
6402 overlay_list = NULL;
6403 overlay_max = NULL;
6406 /* Version handling. This is only useful for ELF. */
6408 /* This global variable holds the version tree that we build. */
6410 struct bfd_elf_version_tree *lang_elf_version_info;
6412 /* If PREV is NULL, return first version pattern matching particular symbol.
6413 If PREV is non-NULL, return first version pattern matching particular
6414 symbol after PREV (previously returned by lang_vers_match). */
6416 static struct bfd_elf_version_expr *
6417 lang_vers_match (struct bfd_elf_version_expr_head *head,
6418 struct bfd_elf_version_expr *prev,
6419 const char *sym)
6421 const char *cxx_sym = sym;
6422 const char *java_sym = sym;
6423 struct bfd_elf_version_expr *expr = NULL;
6425 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6427 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
6428 if (!cxx_sym)
6429 cxx_sym = sym;
6431 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6433 java_sym = cplus_demangle (sym, DMGL_JAVA);
6434 if (!java_sym)
6435 java_sym = sym;
6438 if (head->htab && (prev == NULL || prev->symbol))
6440 struct bfd_elf_version_expr e;
6442 switch (prev ? prev->mask : 0)
6444 case 0:
6445 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6447 e.symbol = sym;
6448 expr = htab_find (head->htab, &e);
6449 while (expr && strcmp (expr->symbol, sym) == 0)
6450 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6451 goto out_ret;
6452 else
6453 expr = expr->next;
6455 /* Fallthrough */
6456 case BFD_ELF_VERSION_C_TYPE:
6457 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6459 e.symbol = cxx_sym;
6460 expr = htab_find (head->htab, &e);
6461 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6462 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6463 goto out_ret;
6464 else
6465 expr = expr->next;
6467 /* Fallthrough */
6468 case BFD_ELF_VERSION_CXX_TYPE:
6469 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6471 e.symbol = java_sym;
6472 expr = htab_find (head->htab, &e);
6473 while (expr && strcmp (expr->symbol, java_sym) == 0)
6474 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6475 goto out_ret;
6476 else
6477 expr = expr->next;
6479 /* Fallthrough */
6480 default:
6481 break;
6485 /* Finally, try the wildcards. */
6486 if (prev == NULL || prev->symbol)
6487 expr = head->remaining;
6488 else
6489 expr = prev->next;
6490 for (; expr; expr = expr->next)
6492 const char *s;
6494 if (!expr->pattern)
6495 continue;
6497 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6498 break;
6500 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6501 s = java_sym;
6502 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6503 s = cxx_sym;
6504 else
6505 s = sym;
6506 if (fnmatch (expr->pattern, s, 0) == 0)
6507 break;
6510 out_ret:
6511 if (cxx_sym != sym)
6512 free ((char *) cxx_sym);
6513 if (java_sym != sym)
6514 free ((char *) java_sym);
6515 return expr;
6518 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6519 return a string pointing to the symbol name. */
6521 static const char *
6522 realsymbol (const char *pattern)
6524 const char *p;
6525 bfd_boolean changed = FALSE, backslash = FALSE;
6526 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6528 for (p = pattern, s = symbol; *p != '\0'; ++p)
6530 /* It is a glob pattern only if there is no preceding
6531 backslash. */
6532 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
6534 free (symbol);
6535 return NULL;
6538 if (backslash)
6540 /* Remove the preceding backslash. */
6541 *(s - 1) = *p;
6542 changed = TRUE;
6544 else
6545 *s++ = *p;
6547 backslash = *p == '\\';
6550 if (changed)
6552 *s = '\0';
6553 return symbol;
6555 else
6557 free (symbol);
6558 return pattern;
6562 /* This is called for each variable name or match expression. NEW is
6563 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6564 pattern to be matched against symbol names. */
6566 struct bfd_elf_version_expr *
6567 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
6568 const char *new,
6569 const char *lang,
6570 bfd_boolean literal_p)
6572 struct bfd_elf_version_expr *ret;
6574 ret = xmalloc (sizeof *ret);
6575 ret->next = orig;
6576 ret->pattern = literal_p ? NULL : new;
6577 ret->symver = 0;
6578 ret->script = 0;
6579 ret->symbol = literal_p ? new : realsymbol (new);
6581 if (lang == NULL || strcasecmp (lang, "C") == 0)
6582 ret->mask = BFD_ELF_VERSION_C_TYPE;
6583 else if (strcasecmp (lang, "C++") == 0)
6584 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
6585 else if (strcasecmp (lang, "Java") == 0)
6586 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
6587 else
6589 einfo (_("%X%P: unknown language `%s' in version information\n"),
6590 lang);
6591 ret->mask = BFD_ELF_VERSION_C_TYPE;
6594 return ldemul_new_vers_pattern (ret);
6597 /* This is called for each set of variable names and match
6598 expressions. */
6600 struct bfd_elf_version_tree *
6601 lang_new_vers_node (struct bfd_elf_version_expr *globals,
6602 struct bfd_elf_version_expr *locals)
6604 struct bfd_elf_version_tree *ret;
6606 ret = xcalloc (1, sizeof *ret);
6607 ret->globals.list = globals;
6608 ret->locals.list = locals;
6609 ret->match = lang_vers_match;
6610 ret->name_indx = (unsigned int) -1;
6611 return ret;
6614 /* This static variable keeps track of version indices. */
6616 static int version_index;
6618 static hashval_t
6619 version_expr_head_hash (const void *p)
6621 const struct bfd_elf_version_expr *e = p;
6623 return htab_hash_string (e->symbol);
6626 static int
6627 version_expr_head_eq (const void *p1, const void *p2)
6629 const struct bfd_elf_version_expr *e1 = p1;
6630 const struct bfd_elf_version_expr *e2 = p2;
6632 return strcmp (e1->symbol, e2->symbol) == 0;
6635 static void
6636 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
6638 size_t count = 0;
6639 struct bfd_elf_version_expr *e, *next;
6640 struct bfd_elf_version_expr **list_loc, **remaining_loc;
6642 for (e = head->list; e; e = e->next)
6644 if (e->symbol)
6645 count++;
6646 head->mask |= e->mask;
6649 if (count)
6651 head->htab = htab_create (count * 2, version_expr_head_hash,
6652 version_expr_head_eq, NULL);
6653 list_loc = &head->list;
6654 remaining_loc = &head->remaining;
6655 for (e = head->list; e; e = next)
6657 next = e->next;
6658 if (!e->symbol)
6660 *remaining_loc = e;
6661 remaining_loc = &e->next;
6663 else
6665 void **loc = htab_find_slot (head->htab, e, INSERT);
6667 if (*loc)
6669 struct bfd_elf_version_expr *e1, *last;
6671 e1 = *loc;
6672 last = NULL;
6675 if (e1->mask == e->mask)
6677 last = NULL;
6678 break;
6680 last = e1;
6681 e1 = e1->next;
6683 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
6685 if (last == NULL)
6687 /* This is a duplicate. */
6688 /* FIXME: Memory leak. Sometimes pattern is not
6689 xmalloced alone, but in larger chunk of memory. */
6690 /* free (e->symbol); */
6691 free (e);
6693 else
6695 e->next = last->next;
6696 last->next = e;
6699 else
6701 *loc = e;
6702 *list_loc = e;
6703 list_loc = &e->next;
6707 *remaining_loc = NULL;
6708 *list_loc = head->remaining;
6710 else
6711 head->remaining = head->list;
6714 /* This is called when we know the name and dependencies of the
6715 version. */
6717 void
6718 lang_register_vers_node (const char *name,
6719 struct bfd_elf_version_tree *version,
6720 struct bfd_elf_version_deps *deps)
6722 struct bfd_elf_version_tree *t, **pp;
6723 struct bfd_elf_version_expr *e1;
6725 if (name == NULL)
6726 name = "";
6728 if ((name[0] == '\0' && lang_elf_version_info != NULL)
6729 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
6731 einfo (_("%X%P: anonymous version tag cannot be combined"
6732 " with other version tags\n"));
6733 free (version);
6734 return;
6737 /* Make sure this node has a unique name. */
6738 for (t = lang_elf_version_info; t != NULL; t = t->next)
6739 if (strcmp (t->name, name) == 0)
6740 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
6742 lang_finalize_version_expr_head (&version->globals);
6743 lang_finalize_version_expr_head (&version->locals);
6745 /* Check the global and local match names, and make sure there
6746 aren't any duplicates. */
6748 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
6750 for (t = lang_elf_version_info; t != NULL; t = t->next)
6752 struct bfd_elf_version_expr *e2;
6754 if (t->locals.htab && e1->symbol)
6756 e2 = htab_find (t->locals.htab, e1);
6757 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6759 if (e1->mask == e2->mask)
6760 einfo (_("%X%P: duplicate expression `%s'"
6761 " in version information\n"), e1->symbol);
6762 e2 = e2->next;
6765 else if (!e1->symbol)
6766 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
6767 if (strcmp (e1->pattern, e2->pattern) == 0
6768 && e1->mask == e2->mask)
6769 einfo (_("%X%P: duplicate expression `%s'"
6770 " in version information\n"), e1->pattern);
6774 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
6776 for (t = lang_elf_version_info; t != NULL; t = t->next)
6778 struct bfd_elf_version_expr *e2;
6780 if (t->globals.htab && e1->symbol)
6782 e2 = htab_find (t->globals.htab, e1);
6783 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6785 if (e1->mask == e2->mask)
6786 einfo (_("%X%P: duplicate expression `%s'"
6787 " in version information\n"),
6788 e1->symbol);
6789 e2 = e2->next;
6792 else if (!e1->symbol)
6793 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6794 if (strcmp (e1->pattern, e2->pattern) == 0
6795 && e1->mask == e2->mask)
6796 einfo (_("%X%P: duplicate expression `%s'"
6797 " in version information\n"), e1->pattern);
6801 version->deps = deps;
6802 version->name = name;
6803 if (name[0] != '\0')
6805 ++version_index;
6806 version->vernum = version_index;
6808 else
6809 version->vernum = 0;
6811 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6813 *pp = version;
6816 /* This is called when we see a version dependency. */
6818 struct bfd_elf_version_deps *
6819 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6821 struct bfd_elf_version_deps *ret;
6822 struct bfd_elf_version_tree *t;
6824 ret = xmalloc (sizeof *ret);
6825 ret->next = list;
6827 for (t = lang_elf_version_info; t != NULL; t = t->next)
6829 if (strcmp (t->name, name) == 0)
6831 ret->version_needed = t;
6832 return ret;
6836 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6838 return ret;
6841 static void
6842 lang_do_version_exports_section (void)
6844 struct bfd_elf_version_expr *greg = NULL, *lreg;
6846 LANG_FOR_EACH_INPUT_STATEMENT (is)
6848 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6849 char *contents, *p;
6850 bfd_size_type len;
6852 if (sec == NULL)
6853 continue;
6855 len = sec->size;
6856 contents = xmalloc (len);
6857 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6858 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6860 p = contents;
6861 while (p < contents + len)
6863 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
6864 p = strchr (p, '\0') + 1;
6867 /* Do not free the contents, as we used them creating the regex. */
6869 /* Do not include this section in the link. */
6870 sec->flags |= SEC_EXCLUDE;
6873 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
6874 lang_register_vers_node (command_line.version_exports_section,
6875 lang_new_vers_node (greg, lreg), NULL);
6878 void
6879 lang_add_unique (const char *name)
6881 struct unique_sections *ent;
6883 for (ent = unique_section_list; ent; ent = ent->next)
6884 if (strcmp (ent->name, name) == 0)
6885 return;
6887 ent = xmalloc (sizeof *ent);
6888 ent->name = xstrdup (name);
6889 ent->next = unique_section_list;
6890 unique_section_list = ent;