daily update
[binutils.git] / ld / ldlang.c
blobc8aac293f7e23ff1130b0617b154acf05f64d60d
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils.
8 This program 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 3 of the License, or
11 (at your option) any later version.
13 This program 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 this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include <limits.h>
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libiberty.h"
28 #include "safe-ctype.h"
29 #include "obstack.h"
30 #include "bfdlink.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldexp.h"
35 #include "ldlang.h"
36 #include <ldgram.h>
37 #include "ldlex.h"
38 #include "ldmisc.h"
39 #include "ldctor.h"
40 #include "ldfile.h"
41 #include "ldemul.h"
42 #include "fnmatch.h"
43 #include "demangle.h"
44 #include "hashtab.h"
46 #ifndef offsetof
47 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 #endif
50 /* Locals variables. */
51 static struct obstack stat_obstack;
52 static struct obstack map_obstack;
54 #define obstack_chunk_alloc xmalloc
55 #define obstack_chunk_free free
56 static const char *startup_file;
57 static bfd_boolean placed_commons = FALSE;
58 static bfd_boolean stripped_excluded_sections = FALSE;
59 static lang_output_section_statement_type *default_common_section;
60 static bfd_boolean map_option_f;
61 static bfd_vma print_dot;
62 static lang_input_statement_type *first_file;
63 static const char *current_target;
64 static const char *output_target;
65 static lang_statement_list_type statement_list;
66 static struct bfd_hash_table lang_definedness_table;
68 /* Forward declarations. */
69 static void exp_init_os (etree_type *);
70 static void init_map_userdata (bfd *, asection *, void *);
71 static lang_input_statement_type *lookup_name (const char *);
72 static struct bfd_hash_entry *lang_definedness_newfunc
73 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
74 static void insert_undefined (const char *);
75 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
76 static void print_statement (lang_statement_union_type *,
77 lang_output_section_statement_type *);
78 static void print_statement_list (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statements (void);
81 static void print_input_section (asection *);
82 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
83 static void lang_record_phdrs (void);
84 static void lang_do_version_exports_section (void);
85 static void lang_finalize_version_expr_head
86 (struct bfd_elf_version_expr_head *);
88 /* Exported variables. */
89 lang_output_section_statement_type *abs_output_section;
90 lang_statement_list_type lang_output_section_statement;
91 lang_statement_list_type *stat_ptr = &statement_list;
92 lang_statement_list_type file_chain = { NULL, NULL };
93 lang_statement_list_type input_file_chain;
94 struct bfd_sym_chain entry_symbol = { NULL, NULL };
95 static const char *entry_symbol_default = "start";
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_phdr *lang_phdr_list;
103 struct lang_nocrossrefs *nocrossref_list;
104 static struct unique_sections *unique_section_list;
105 static bfd_boolean ldlang_sysrooted_script = FALSE;
107 /* Functions that traverse the linker script and might evaluate
108 DEFINED() need to increment this. */
109 int lang_statement_iteration = 0;
111 etree_type *base; /* Relocation base - or null */
113 /* Return TRUE if the PATTERN argument is a wildcard pattern.
114 Although backslashes are treated specially if a pattern contains
115 wildcards, we do not consider the mere presence of a backslash to
116 be enough to cause the pattern to be treated as a wildcard.
117 That lets us handle DOS filenames more naturally. */
118 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
120 #define new_stat(x, y) \
121 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
123 #define outside_section_address(q) \
124 ((q)->output_offset + (q)->output_section->vma)
126 #define outside_symbol_address(q) \
127 ((q)->value + outside_section_address (q->section))
129 #define SECTION_NAME_MAP_LENGTH (16)
131 void *
132 stat_alloc (size_t size)
134 return obstack_alloc (&stat_obstack, size);
137 bfd_boolean
138 unique_section_p (const asection *sec)
140 struct unique_sections *unam;
141 const char *secnam;
143 if (link_info.relocatable
144 && sec->owner != NULL
145 && bfd_is_group_section (sec->owner, sec))
146 return TRUE;
148 secnam = sec->name;
149 for (unam = unique_section_list; unam; unam = unam->next)
150 if (wildcardp (unam->name)
151 ? fnmatch (unam->name, secnam, 0) == 0
152 : strcmp (unam->name, secnam) == 0)
154 return TRUE;
157 return FALSE;
160 /* Generic traversal routines for finding matching sections. */
162 /* Try processing a section against a wildcard. This just calls
163 the callback unless the filename exclusion list is present
164 and excludes the file. It's hardly ever present so this
165 function is very fast. */
167 static void
168 walk_wild_consider_section (lang_wild_statement_type *ptr,
169 lang_input_statement_type *file,
170 asection *s,
171 struct wildcard_list *sec,
172 callback_t callback,
173 void *data)
175 bfd_boolean skip = FALSE;
176 struct name_list *list_tmp;
178 /* Don't process sections from files which were
179 excluded. */
180 for (list_tmp = sec->spec.exclude_name_list;
181 list_tmp;
182 list_tmp = list_tmp->next)
184 bfd_boolean is_wildcard = wildcardp (list_tmp->name);
185 if (is_wildcard)
186 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
187 else
188 skip = strcmp (list_tmp->name, file->filename) == 0;
190 /* If this file is part of an archive, and the archive is
191 excluded, exclude this file. */
192 if (! skip && file->the_bfd != NULL
193 && file->the_bfd->my_archive != NULL
194 && file->the_bfd->my_archive->filename != NULL)
196 if (is_wildcard)
197 skip = fnmatch (list_tmp->name,
198 file->the_bfd->my_archive->filename,
199 0) == 0;
200 else
201 skip = strcmp (list_tmp->name,
202 file->the_bfd->my_archive->filename) == 0;
205 if (skip)
206 break;
209 if (!skip)
210 (*callback) (ptr, sec, s, file, data);
213 /* Lowest common denominator routine that can handle everything correctly,
214 but slowly. */
216 static void
217 walk_wild_section_general (lang_wild_statement_type *ptr,
218 lang_input_statement_type *file,
219 callback_t callback,
220 void *data)
222 asection *s;
223 struct wildcard_list *sec;
225 for (s = file->the_bfd->sections; s != NULL; s = s->next)
227 sec = ptr->section_list;
228 if (sec == NULL)
229 (*callback) (ptr, sec, s, file, data);
231 while (sec != NULL)
233 bfd_boolean skip = FALSE;
235 if (sec->spec.name != NULL)
237 const char *sname = bfd_get_section_name (file->the_bfd, s);
239 if (wildcardp (sec->spec.name))
240 skip = fnmatch (sec->spec.name, sname, 0) != 0;
241 else
242 skip = strcmp (sec->spec.name, sname) != 0;
245 if (!skip)
246 walk_wild_consider_section (ptr, file, s, sec, callback, data);
248 sec = sec->next;
253 /* Routines to find a single section given its name. If there's more
254 than one section with that name, we report that. */
256 typedef struct
258 asection *found_section;
259 bfd_boolean multiple_sections_found;
260 } section_iterator_callback_data;
262 static bfd_boolean
263 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
265 section_iterator_callback_data *d = data;
267 if (d->found_section != NULL)
269 d->multiple_sections_found = TRUE;
270 return TRUE;
273 d->found_section = s;
274 return FALSE;
277 static asection *
278 find_section (lang_input_statement_type *file,
279 struct wildcard_list *sec,
280 bfd_boolean *multiple_sections_found)
282 section_iterator_callback_data cb_data = { NULL, FALSE };
284 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
285 section_iterator_callback, &cb_data);
286 *multiple_sections_found = cb_data.multiple_sections_found;
287 return cb_data.found_section;
290 /* Code for handling simple wildcards without going through fnmatch,
291 which can be expensive because of charset translations etc. */
293 /* A simple wild is a literal string followed by a single '*',
294 where the literal part is at least 4 characters long. */
296 static bfd_boolean
297 is_simple_wild (const char *name)
299 size_t len = strcspn (name, "*?[");
300 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
303 static bfd_boolean
304 match_simple_wild (const char *pattern, const char *name)
306 /* The first four characters of the pattern are guaranteed valid
307 non-wildcard characters. So we can go faster. */
308 if (pattern[0] != name[0] || pattern[1] != name[1]
309 || pattern[2] != name[2] || pattern[3] != name[3])
310 return FALSE;
312 pattern += 4;
313 name += 4;
314 while (*pattern != '*')
315 if (*name++ != *pattern++)
316 return FALSE;
318 return TRUE;
321 /* Compare sections ASEC and BSEC according to SORT. */
323 static int
324 compare_section (sort_type sort, asection *asec, asection *bsec)
326 int ret;
328 switch (sort)
330 default:
331 abort ();
333 case by_alignment_name:
334 ret = (bfd_section_alignment (bsec->owner, bsec)
335 - bfd_section_alignment (asec->owner, asec));
336 if (ret)
337 break;
338 /* Fall through. */
340 case by_name:
341 ret = strcmp (bfd_get_section_name (asec->owner, asec),
342 bfd_get_section_name (bsec->owner, bsec));
343 break;
345 case by_name_alignment:
346 ret = strcmp (bfd_get_section_name (asec->owner, asec),
347 bfd_get_section_name (bsec->owner, bsec));
348 if (ret)
349 break;
350 /* Fall through. */
352 case by_alignment:
353 ret = (bfd_section_alignment (bsec->owner, bsec)
354 - bfd_section_alignment (asec->owner, asec));
355 break;
358 return ret;
361 /* Build a Binary Search Tree to sort sections, unlike insertion sort
362 used in wild_sort(). BST is considerably faster if the number of
363 of sections are large. */
365 static lang_section_bst_type **
366 wild_sort_fast (lang_wild_statement_type *wild,
367 struct wildcard_list *sec,
368 lang_input_statement_type *file ATTRIBUTE_UNUSED,
369 asection *section)
371 lang_section_bst_type **tree;
373 tree = &wild->tree;
374 if (!wild->filenames_sorted
375 && (sec == NULL || sec->spec.sorted == none))
377 /* Append at the right end of tree. */
378 while (*tree)
379 tree = &((*tree)->right);
380 return tree;
383 while (*tree)
385 /* Find the correct node to append this section. */
386 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
387 tree = &((*tree)->left);
388 else
389 tree = &((*tree)->right);
392 return tree;
395 /* Use wild_sort_fast to build a BST to sort sections. */
397 static void
398 output_section_callback_fast (lang_wild_statement_type *ptr,
399 struct wildcard_list *sec,
400 asection *section,
401 lang_input_statement_type *file,
402 void *output ATTRIBUTE_UNUSED)
404 lang_section_bst_type *node;
405 lang_section_bst_type **tree;
407 if (unique_section_p (section))
408 return;
410 node = xmalloc (sizeof (lang_section_bst_type));
411 node->left = 0;
412 node->right = 0;
413 node->section = section;
415 tree = wild_sort_fast (ptr, sec, file, section);
416 if (tree != NULL)
417 *tree = node;
420 /* Convert a sorted sections' BST back to list form. */
422 static void
423 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
424 lang_section_bst_type *tree,
425 void *output)
427 if (tree->left)
428 output_section_callback_tree_to_list (ptr, tree->left, output);
430 lang_add_section (&ptr->children, tree->section,
431 (lang_output_section_statement_type *) output);
433 if (tree->right)
434 output_section_callback_tree_to_list (ptr, tree->right, output);
436 free (tree);
439 /* Specialized, optimized routines for handling different kinds of
440 wildcards */
442 static void
443 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
444 lang_input_statement_type *file,
445 callback_t callback,
446 void *data)
448 /* We can just do a hash lookup for the section with the right name.
449 But if that lookup discovers more than one section with the name
450 (should be rare), we fall back to the general algorithm because
451 we would otherwise have to sort the sections to make sure they
452 get processed in the bfd's order. */
453 bfd_boolean multiple_sections_found;
454 struct wildcard_list *sec0 = ptr->handler_data[0];
455 asection *s0 = find_section (file, sec0, &multiple_sections_found);
457 if (multiple_sections_found)
458 walk_wild_section_general (ptr, file, callback, data);
459 else if (s0)
460 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
463 static void
464 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
465 lang_input_statement_type *file,
466 callback_t callback,
467 void *data)
469 asection *s;
470 struct wildcard_list *wildsec0 = ptr->handler_data[0];
472 for (s = file->the_bfd->sections; s != NULL; s = s->next)
474 const char *sname = bfd_get_section_name (file->the_bfd, s);
475 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
477 if (!skip)
478 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
482 static void
483 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
484 lang_input_statement_type *file,
485 callback_t callback,
486 void *data)
488 asection *s;
489 struct wildcard_list *sec0 = ptr->handler_data[0];
490 struct wildcard_list *wildsec1 = ptr->handler_data[1];
491 bfd_boolean multiple_sections_found;
492 asection *s0 = find_section (file, sec0, &multiple_sections_found);
494 if (multiple_sections_found)
496 walk_wild_section_general (ptr, file, callback, data);
497 return;
500 /* Note that if the section was not found, s0 is NULL and
501 we'll simply never succeed the s == s0 test below. */
502 for (s = file->the_bfd->sections; s != NULL; s = s->next)
504 /* Recall that in this code path, a section cannot satisfy more
505 than one spec, so if s == s0 then it cannot match
506 wildspec1. */
507 if (s == s0)
508 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
509 else
511 const char *sname = bfd_get_section_name (file->the_bfd, s);
512 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
514 if (!skip)
515 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
516 data);
521 static void
522 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
523 lang_input_statement_type *file,
524 callback_t callback,
525 void *data)
527 asection *s;
528 struct wildcard_list *sec0 = ptr->handler_data[0];
529 struct wildcard_list *wildsec1 = ptr->handler_data[1];
530 struct wildcard_list *wildsec2 = ptr->handler_data[2];
531 bfd_boolean multiple_sections_found;
532 asection *s0 = find_section (file, sec0, &multiple_sections_found);
534 if (multiple_sections_found)
536 walk_wild_section_general (ptr, file, callback, data);
537 return;
540 for (s = file->the_bfd->sections; s != NULL; s = s->next)
542 if (s == s0)
543 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
544 else
546 const char *sname = bfd_get_section_name (file->the_bfd, s);
547 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
549 if (!skip)
550 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
551 else
553 skip = !match_simple_wild (wildsec2->spec.name, sname);
554 if (!skip)
555 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
556 data);
562 static void
563 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
564 lang_input_statement_type *file,
565 callback_t callback,
566 void *data)
568 asection *s;
569 struct wildcard_list *sec0 = ptr->handler_data[0];
570 struct wildcard_list *sec1 = ptr->handler_data[1];
571 struct wildcard_list *wildsec2 = ptr->handler_data[2];
572 struct wildcard_list *wildsec3 = ptr->handler_data[3];
573 bfd_boolean multiple_sections_found;
574 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
576 if (multiple_sections_found)
578 walk_wild_section_general (ptr, file, callback, data);
579 return;
582 s1 = find_section (file, sec1, &multiple_sections_found);
583 if (multiple_sections_found)
585 walk_wild_section_general (ptr, file, callback, data);
586 return;
589 for (s = file->the_bfd->sections; s != NULL; s = s->next)
591 if (s == s0)
592 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
593 else
594 if (s == s1)
595 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
596 else
598 const char *sname = bfd_get_section_name (file->the_bfd, s);
599 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
600 sname);
602 if (!skip)
603 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
604 data);
605 else
607 skip = !match_simple_wild (wildsec3->spec.name, sname);
608 if (!skip)
609 walk_wild_consider_section (ptr, file, s, wildsec3,
610 callback, data);
616 static void
617 walk_wild_section (lang_wild_statement_type *ptr,
618 lang_input_statement_type *file,
619 callback_t callback,
620 void *data)
622 if (file->just_syms_flag)
623 return;
625 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
628 /* Returns TRUE when name1 is a wildcard spec that might match
629 something name2 can match. We're conservative: we return FALSE
630 only if the prefixes of name1 and name2 are different up to the
631 first wildcard character. */
633 static bfd_boolean
634 wild_spec_can_overlap (const char *name1, const char *name2)
636 size_t prefix1_len = strcspn (name1, "?*[");
637 size_t prefix2_len = strcspn (name2, "?*[");
638 size_t min_prefix_len;
640 /* Note that if there is no wildcard character, then we treat the
641 terminating 0 as part of the prefix. Thus ".text" won't match
642 ".text." or ".text.*", for example. */
643 if (name1[prefix1_len] == '\0')
644 prefix1_len++;
645 if (name2[prefix2_len] == '\0')
646 prefix2_len++;
648 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
650 return memcmp (name1, name2, min_prefix_len) == 0;
653 /* Select specialized code to handle various kinds of wildcard
654 statements. */
656 static void
657 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
659 int sec_count = 0;
660 int wild_name_count = 0;
661 struct wildcard_list *sec;
662 int signature;
663 int data_counter;
665 ptr->walk_wild_section_handler = walk_wild_section_general;
666 ptr->handler_data[0] = NULL;
667 ptr->handler_data[1] = NULL;
668 ptr->handler_data[2] = NULL;
669 ptr->handler_data[3] = NULL;
670 ptr->tree = NULL;
672 /* Count how many wildcard_specs there are, and how many of those
673 actually use wildcards in the name. Also, bail out if any of the
674 wildcard names are NULL. (Can this actually happen?
675 walk_wild_section used to test for it.) And bail out if any
676 of the wildcards are more complex than a simple string
677 ending in a single '*'. */
678 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
680 ++sec_count;
681 if (sec->spec.name == NULL)
682 return;
683 if (wildcardp (sec->spec.name))
685 ++wild_name_count;
686 if (!is_simple_wild (sec->spec.name))
687 return;
691 /* The zero-spec case would be easy to optimize but it doesn't
692 happen in practice. Likewise, more than 4 specs doesn't
693 happen in practice. */
694 if (sec_count == 0 || sec_count > 4)
695 return;
697 /* Check that no two specs can match the same section. */
698 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
700 struct wildcard_list *sec2;
701 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
703 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
704 return;
708 signature = (sec_count << 8) + wild_name_count;
709 switch (signature)
711 case 0x0100:
712 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
713 break;
714 case 0x0101:
715 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
716 break;
717 case 0x0201:
718 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
719 break;
720 case 0x0302:
721 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
722 break;
723 case 0x0402:
724 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
725 break;
726 default:
727 return;
730 /* Now fill the data array with pointers to the specs, first the
731 specs with non-wildcard names, then the specs with wildcard
732 names. It's OK to process the specs in different order from the
733 given order, because we've already determined that no section
734 will match more than one spec. */
735 data_counter = 0;
736 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
737 if (!wildcardp (sec->spec.name))
738 ptr->handler_data[data_counter++] = sec;
739 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
740 if (wildcardp (sec->spec.name))
741 ptr->handler_data[data_counter++] = sec;
744 /* Handle a wild statement for a single file F. */
746 static void
747 walk_wild_file (lang_wild_statement_type *s,
748 lang_input_statement_type *f,
749 callback_t callback,
750 void *data)
752 if (f->the_bfd == NULL
753 || ! bfd_check_format (f->the_bfd, bfd_archive))
754 walk_wild_section (s, f, callback, data);
755 else
757 bfd *member;
759 /* This is an archive file. We must map each member of the
760 archive separately. */
761 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
762 while (member != NULL)
764 /* When lookup_name is called, it will call the add_symbols
765 entry point for the archive. For each element of the
766 archive which is included, BFD will call ldlang_add_file,
767 which will set the usrdata field of the member to the
768 lang_input_statement. */
769 if (member->usrdata != NULL)
771 walk_wild_section (s, member->usrdata, callback, data);
774 member = bfd_openr_next_archived_file (f->the_bfd, member);
779 static void
780 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
782 const char *file_spec = s->filename;
784 if (file_spec == NULL)
786 /* Perform the iteration over all files in the list. */
787 LANG_FOR_EACH_INPUT_STATEMENT (f)
789 walk_wild_file (s, f, callback, data);
792 else if (wildcardp (file_spec))
794 LANG_FOR_EACH_INPUT_STATEMENT (f)
796 if (fnmatch (file_spec, f->filename, 0) == 0)
797 walk_wild_file (s, f, callback, data);
800 else
802 lang_input_statement_type *f;
804 /* Perform the iteration over a single file. */
805 f = lookup_name (file_spec);
806 if (f)
807 walk_wild_file (s, f, callback, data);
811 /* lang_for_each_statement walks the parse tree and calls the provided
812 function for each node. */
814 static void
815 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
816 lang_statement_union_type *s)
818 for (; s != NULL; s = s->header.next)
820 func (s);
822 switch (s->header.type)
824 case lang_constructors_statement_enum:
825 lang_for_each_statement_worker (func, constructor_list.head);
826 break;
827 case lang_output_section_statement_enum:
828 lang_for_each_statement_worker
829 (func, s->output_section_statement.children.head);
830 break;
831 case lang_wild_statement_enum:
832 lang_for_each_statement_worker (func,
833 s->wild_statement.children.head);
834 break;
835 case lang_group_statement_enum:
836 lang_for_each_statement_worker (func,
837 s->group_statement.children.head);
838 break;
839 case lang_data_statement_enum:
840 case lang_reloc_statement_enum:
841 case lang_object_symbols_statement_enum:
842 case lang_output_statement_enum:
843 case lang_target_statement_enum:
844 case lang_input_section_enum:
845 case lang_input_statement_enum:
846 case lang_assignment_statement_enum:
847 case lang_padding_statement_enum:
848 case lang_address_statement_enum:
849 case lang_fill_statement_enum:
850 case lang_insert_statement_enum:
851 break;
852 default:
853 FAIL ();
854 break;
859 void
860 lang_for_each_statement (void (*func) (lang_statement_union_type *))
862 lang_for_each_statement_worker (func, statement_list.head);
865 /*----------------------------------------------------------------------*/
867 void
868 lang_list_init (lang_statement_list_type *list)
870 list->head = NULL;
871 list->tail = &list->head;
874 /* Build a new statement node for the parse tree. */
876 static lang_statement_union_type *
877 new_statement (enum statement_enum type,
878 size_t size,
879 lang_statement_list_type *list)
881 lang_statement_union_type *new;
883 new = stat_alloc (size);
884 new->header.type = type;
885 new->header.next = NULL;
886 lang_statement_append (list, new, &new->header.next);
887 return new;
890 /* Build a new input file node for the language. There are several
891 ways in which we treat an input file, eg, we only look at symbols,
892 or prefix it with a -l etc.
894 We can be supplied with requests for input files more than once;
895 they may, for example be split over several lines like foo.o(.text)
896 foo.o(.data) etc, so when asked for a file we check that we haven't
897 got it already so we don't duplicate the bfd. */
899 static lang_input_statement_type *
900 new_afile (const char *name,
901 lang_input_file_enum_type file_type,
902 const char *target,
903 bfd_boolean add_to_list)
905 lang_input_statement_type *p;
907 if (add_to_list)
908 p = new_stat (lang_input_statement, stat_ptr);
909 else
911 p = stat_alloc (sizeof (lang_input_statement_type));
912 p->header.type = lang_input_statement_enum;
913 p->header.next = NULL;
916 lang_has_input_file = TRUE;
917 p->target = target;
918 p->sysrooted = FALSE;
920 if (file_type == lang_input_file_is_l_enum
921 && name[0] == ':' && name[1] != '\0')
923 file_type = lang_input_file_is_search_file_enum;
924 name = name + 1;
927 switch (file_type)
929 case lang_input_file_is_symbols_only_enum:
930 p->filename = name;
931 p->is_archive = FALSE;
932 p->real = TRUE;
933 p->local_sym_name = name;
934 p->just_syms_flag = TRUE;
935 p->search_dirs_flag = FALSE;
936 break;
937 case lang_input_file_is_fake_enum:
938 p->filename = name;
939 p->is_archive = FALSE;
940 p->real = FALSE;
941 p->local_sym_name = name;
942 p->just_syms_flag = FALSE;
943 p->search_dirs_flag = FALSE;
944 break;
945 case lang_input_file_is_l_enum:
946 p->is_archive = TRUE;
947 p->filename = name;
948 p->real = TRUE;
949 p->local_sym_name = concat ("-l", name, (const char *) NULL);
950 p->just_syms_flag = FALSE;
951 p->search_dirs_flag = TRUE;
952 break;
953 case lang_input_file_is_marker_enum:
954 p->filename = name;
955 p->is_archive = FALSE;
956 p->real = FALSE;
957 p->local_sym_name = name;
958 p->just_syms_flag = FALSE;
959 p->search_dirs_flag = TRUE;
960 break;
961 case lang_input_file_is_search_file_enum:
962 p->sysrooted = ldlang_sysrooted_script;
963 p->filename = name;
964 p->is_archive = FALSE;
965 p->real = TRUE;
966 p->local_sym_name = name;
967 p->just_syms_flag = FALSE;
968 p->search_dirs_flag = TRUE;
969 break;
970 case lang_input_file_is_file_enum:
971 p->filename = name;
972 p->is_archive = FALSE;
973 p->real = TRUE;
974 p->local_sym_name = name;
975 p->just_syms_flag = FALSE;
976 p->search_dirs_flag = FALSE;
977 break;
978 default:
979 FAIL ();
981 p->the_bfd = NULL;
982 p->asymbols = NULL;
983 p->next_real_file = NULL;
984 p->next = NULL;
985 p->symbol_count = 0;
986 p->dynamic = config.dynamic_link;
987 p->add_needed = add_needed;
988 p->as_needed = as_needed;
989 p->whole_archive = whole_archive;
990 p->loaded = FALSE;
991 lang_statement_append (&input_file_chain,
992 (lang_statement_union_type *) p,
993 &p->next_real_file);
994 return p;
997 lang_input_statement_type *
998 lang_add_input_file (const char *name,
999 lang_input_file_enum_type file_type,
1000 const char *target)
1002 return new_afile (name, file_type, target, TRUE);
1005 struct out_section_hash_entry
1007 struct bfd_hash_entry root;
1008 lang_statement_union_type s;
1011 /* The hash table. */
1013 static struct bfd_hash_table output_section_statement_table;
1015 /* Support routines for the hash table used by lang_output_section_find,
1016 initialize the table, fill in an entry and remove the table. */
1018 static struct bfd_hash_entry *
1019 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1020 struct bfd_hash_table *table,
1021 const char *string)
1023 lang_output_section_statement_type **nextp;
1024 struct out_section_hash_entry *ret;
1026 if (entry == NULL)
1028 entry = bfd_hash_allocate (table, sizeof (*ret));
1029 if (entry == NULL)
1030 return entry;
1033 entry = bfd_hash_newfunc (entry, table, string);
1034 if (entry == NULL)
1035 return entry;
1037 ret = (struct out_section_hash_entry *) entry;
1038 memset (&ret->s, 0, sizeof (ret->s));
1039 ret->s.header.type = lang_output_section_statement_enum;
1040 ret->s.output_section_statement.subsection_alignment = -1;
1041 ret->s.output_section_statement.section_alignment = -1;
1042 ret->s.output_section_statement.block_value = 1;
1043 lang_list_init (&ret->s.output_section_statement.children);
1044 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1046 /* For every output section statement added to the list, except the
1047 first one, lang_output_section_statement.tail points to the "next"
1048 field of the last element of the list. */
1049 if (lang_output_section_statement.head != NULL)
1050 ret->s.output_section_statement.prev
1051 = ((lang_output_section_statement_type *)
1052 ((char *) lang_output_section_statement.tail
1053 - offsetof (lang_output_section_statement_type, next)));
1055 /* GCC's strict aliasing rules prevent us from just casting the
1056 address, so we store the pointer in a variable and cast that
1057 instead. */
1058 nextp = &ret->s.output_section_statement.next;
1059 lang_statement_append (&lang_output_section_statement,
1060 &ret->s,
1061 (lang_statement_union_type **) nextp);
1062 return &ret->root;
1065 static void
1066 output_section_statement_table_init (void)
1068 if (!bfd_hash_table_init_n (&output_section_statement_table,
1069 output_section_statement_newfunc,
1070 sizeof (struct out_section_hash_entry),
1071 61))
1072 einfo (_("%P%F: can not create hash table: %E\n"));
1075 static void
1076 output_section_statement_table_free (void)
1078 bfd_hash_table_free (&output_section_statement_table);
1081 /* Build enough state so that the parser can build its tree. */
1083 void
1084 lang_init (void)
1086 obstack_begin (&stat_obstack, 1000);
1088 stat_ptr = &statement_list;
1090 output_section_statement_table_init ();
1092 lang_list_init (stat_ptr);
1094 lang_list_init (&input_file_chain);
1095 lang_list_init (&lang_output_section_statement);
1096 lang_list_init (&file_chain);
1097 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1098 NULL);
1099 abs_output_section =
1100 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
1102 abs_output_section->bfd_section = bfd_abs_section_ptr;
1104 /* The value "3" is ad-hoc, somewhat related to the expected number of
1105 DEFINED expressions in a linker script. For most default linker
1106 scripts, there are none. Why a hash table then? Well, it's somewhat
1107 simpler to re-use working machinery than using a linked list in terms
1108 of code-complexity here in ld, besides the initialization which just
1109 looks like other code here. */
1110 if (!bfd_hash_table_init_n (&lang_definedness_table,
1111 lang_definedness_newfunc,
1112 sizeof (struct lang_definedness_hash_entry),
1114 einfo (_("%P%F: can not create hash table: %E\n"));
1117 void
1118 lang_finish (void)
1120 output_section_statement_table_free ();
1123 /*----------------------------------------------------------------------
1124 A region is an area of memory declared with the
1125 MEMORY { name:org=exp, len=exp ... }
1126 syntax.
1128 We maintain a list of all the regions here.
1130 If no regions are specified in the script, then the default is used
1131 which is created when looked up to be the entire data space.
1133 If create is true we are creating a region inside a MEMORY block.
1134 In this case it is probably an error to create a region that has
1135 already been created. If we are not inside a MEMORY block it is
1136 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1137 and so we issue a warning. */
1139 static lang_memory_region_type *lang_memory_region_list;
1140 static lang_memory_region_type **lang_memory_region_list_tail
1141 = &lang_memory_region_list;
1143 lang_memory_region_type *
1144 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1146 lang_memory_region_type *p;
1147 lang_memory_region_type *new;
1149 /* NAME is NULL for LMA memspecs if no region was specified. */
1150 if (name == NULL)
1151 return NULL;
1153 for (p = lang_memory_region_list; p != NULL; p = p->next)
1154 if (strcmp (p->name, name) == 0)
1156 if (create)
1157 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1158 name);
1159 return p;
1162 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1163 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
1165 new = stat_alloc (sizeof (lang_memory_region_type));
1167 new->name = xstrdup (name);
1168 new->next = NULL;
1169 new->origin = 0;
1170 new->length = ~(bfd_size_type) 0;
1171 new->current = 0;
1172 new->last_os = NULL;
1173 new->flags = 0;
1174 new->not_flags = 0;
1175 new->had_full_message = FALSE;
1177 *lang_memory_region_list_tail = new;
1178 lang_memory_region_list_tail = &new->next;
1180 return new;
1183 static lang_memory_region_type *
1184 lang_memory_default (asection *section)
1186 lang_memory_region_type *p;
1188 flagword sec_flags = section->flags;
1190 /* Override SEC_DATA to mean a writable section. */
1191 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1192 sec_flags |= SEC_DATA;
1194 for (p = lang_memory_region_list; p != NULL; p = p->next)
1196 if ((p->flags & sec_flags) != 0
1197 && (p->not_flags & sec_flags) == 0)
1199 return p;
1202 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1205 lang_output_section_statement_type *
1206 lang_output_section_find (const char *const name)
1208 struct out_section_hash_entry *entry;
1209 unsigned long hash;
1211 entry = ((struct out_section_hash_entry *)
1212 bfd_hash_lookup (&output_section_statement_table, name,
1213 FALSE, FALSE));
1214 if (entry == NULL)
1215 return NULL;
1217 hash = entry->root.hash;
1220 if (entry->s.output_section_statement.constraint != -1)
1221 return &entry->s.output_section_statement;
1222 entry = (struct out_section_hash_entry *) entry->root.next;
1224 while (entry != NULL
1225 && entry->root.hash == hash
1226 && strcmp (name, entry->s.output_section_statement.name) == 0);
1228 return NULL;
1231 static lang_output_section_statement_type *
1232 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
1234 struct out_section_hash_entry *entry;
1235 struct out_section_hash_entry *last_ent;
1236 unsigned long hash;
1238 entry = ((struct out_section_hash_entry *)
1239 bfd_hash_lookup (&output_section_statement_table, name,
1240 TRUE, FALSE));
1241 if (entry == NULL)
1243 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1244 return NULL;
1247 if (entry->s.output_section_statement.name != NULL)
1249 /* We have a section of this name, but it might not have the correct
1250 constraint. */
1251 hash = entry->root.hash;
1254 if (entry->s.output_section_statement.constraint != -1
1255 && (constraint == 0
1256 || (constraint == entry->s.output_section_statement.constraint
1257 && constraint != SPECIAL)))
1258 return &entry->s.output_section_statement;
1259 last_ent = entry;
1260 entry = (struct out_section_hash_entry *) entry->root.next;
1262 while (entry != NULL
1263 && entry->root.hash == hash
1264 && strcmp (name, entry->s.output_section_statement.name) == 0);
1266 entry
1267 = ((struct out_section_hash_entry *)
1268 output_section_statement_newfunc (NULL,
1269 &output_section_statement_table,
1270 name));
1271 if (entry == NULL)
1273 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1274 return NULL;
1276 entry->root = last_ent->root;
1277 last_ent->root.next = &entry->root;
1280 entry->s.output_section_statement.name = name;
1281 entry->s.output_section_statement.constraint = constraint;
1282 return &entry->s.output_section_statement;
1285 lang_output_section_statement_type *
1286 lang_output_section_statement_lookup (const char *const name)
1288 return lang_output_section_statement_lookup_1 (name, 0);
1291 /* A variant of lang_output_section_find used by place_orphan.
1292 Returns the output statement that should precede a new output
1293 statement for SEC. If an exact match is found on certain flags,
1294 sets *EXACT too. */
1296 lang_output_section_statement_type *
1297 lang_output_section_find_by_flags (const asection *sec,
1298 lang_output_section_statement_type **exact,
1299 lang_match_sec_type_func match_type)
1301 lang_output_section_statement_type *first, *look, *found;
1302 flagword flags;
1304 /* We know the first statement on this list is *ABS*. May as well
1305 skip it. */
1306 first = &lang_output_section_statement.head->output_section_statement;
1307 first = first->next;
1309 /* First try for an exact match. */
1310 found = NULL;
1311 for (look = first; look; look = look->next)
1313 flags = look->flags;
1314 if (look->bfd_section != NULL)
1316 flags = look->bfd_section->flags;
1317 if (match_type && !match_type (link_info.output_bfd,
1318 look->bfd_section,
1319 sec->owner, sec))
1320 continue;
1322 flags ^= sec->flags;
1323 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1324 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1325 found = look;
1327 if (found != NULL)
1329 if (exact != NULL)
1330 *exact = found;
1331 return found;
1334 if (sec->flags & SEC_CODE)
1336 /* Try for a rw code section. */
1337 for (look = first; look; look = look->next)
1339 flags = look->flags;
1340 if (look->bfd_section != NULL)
1342 flags = look->bfd_section->flags;
1343 if (match_type && !match_type (link_info.output_bfd,
1344 look->bfd_section,
1345 sec->owner, sec))
1346 continue;
1348 flags ^= sec->flags;
1349 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1350 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1351 found = look;
1354 else if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
1356 /* .rodata can go after .text, .sdata2 after .rodata. */
1357 for (look = first; look; look = look->next)
1359 flags = look->flags;
1360 if (look->bfd_section != NULL)
1362 flags = look->bfd_section->flags;
1363 if (match_type && !match_type (link_info.output_bfd,
1364 look->bfd_section,
1365 sec->owner, sec))
1366 continue;
1368 flags ^= sec->flags;
1369 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1370 | SEC_READONLY))
1371 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1372 found = look;
1375 else if (sec->flags & SEC_SMALL_DATA)
1377 /* .sdata goes after .data, .sbss after .sdata. */
1378 for (look = first; look; look = look->next)
1380 flags = look->flags;
1381 if (look->bfd_section != NULL)
1383 flags = look->bfd_section->flags;
1384 if (match_type && !match_type (link_info.output_bfd,
1385 look->bfd_section,
1386 sec->owner, sec))
1387 continue;
1389 flags ^= sec->flags;
1390 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1391 | SEC_THREAD_LOCAL))
1392 || ((look->flags & SEC_SMALL_DATA)
1393 && !(sec->flags & SEC_HAS_CONTENTS)))
1394 found = look;
1397 else if (sec->flags & SEC_HAS_CONTENTS)
1399 /* .data goes after .rodata. */
1400 for (look = first; look; look = look->next)
1402 flags = look->flags;
1403 if (look->bfd_section != NULL)
1405 flags = look->bfd_section->flags;
1406 if (match_type && !match_type (link_info.output_bfd,
1407 look->bfd_section,
1408 sec->owner, sec))
1409 continue;
1411 flags ^= sec->flags;
1412 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1413 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1414 found = look;
1417 else
1419 /* .bss goes last. */
1420 for (look = first; look; look = look->next)
1422 flags = look->flags;
1423 if (look->bfd_section != NULL)
1425 flags = look->bfd_section->flags;
1426 if (match_type && !match_type (link_info.output_bfd,
1427 look->bfd_section,
1428 sec->owner, sec))
1429 continue;
1431 flags ^= sec->flags;
1432 if (!(flags & SEC_ALLOC))
1433 found = look;
1437 if (found || !match_type)
1438 return found;
1440 return lang_output_section_find_by_flags (sec, NULL, NULL);
1443 /* Find the last output section before given output statement.
1444 Used by place_orphan. */
1446 static asection *
1447 output_prev_sec_find (lang_output_section_statement_type *os)
1449 lang_output_section_statement_type *lookup;
1451 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1453 if (lookup->constraint == -1)
1454 continue;
1456 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1457 return lookup->bfd_section;
1460 return NULL;
1463 /* Look for a suitable place for a new output section statement. The
1464 idea is to skip over anything that might be inside a SECTIONS {}
1465 statement in a script, before we find another output section
1466 statement. Assignments to "dot" before an output section statement
1467 are assumed to belong to it. An exception to this rule is made for
1468 the first assignment to dot, otherwise we might put an orphan
1469 before . = . + SIZEOF_HEADERS or similar assignments that set the
1470 initial address. */
1472 static lang_statement_union_type **
1473 insert_os_after (lang_output_section_statement_type *after)
1475 lang_statement_union_type **where;
1476 lang_statement_union_type **assign = NULL;
1477 bfd_boolean ignore_first;
1479 ignore_first
1480 = after == &lang_output_section_statement.head->output_section_statement;
1482 for (where = &after->header.next;
1483 *where != NULL;
1484 where = &(*where)->header.next)
1486 switch ((*where)->header.type)
1488 case lang_assignment_statement_enum:
1489 if (assign == NULL)
1491 lang_assignment_statement_type *ass;
1493 ass = &(*where)->assignment_statement;
1494 if (ass->exp->type.node_class != etree_assert
1495 && ass->exp->assign.dst[0] == '.'
1496 && ass->exp->assign.dst[1] == 0
1497 && !ignore_first)
1498 assign = where;
1500 ignore_first = FALSE;
1501 continue;
1502 case lang_wild_statement_enum:
1503 case lang_input_section_enum:
1504 case lang_object_symbols_statement_enum:
1505 case lang_fill_statement_enum:
1506 case lang_data_statement_enum:
1507 case lang_reloc_statement_enum:
1508 case lang_padding_statement_enum:
1509 case lang_constructors_statement_enum:
1510 assign = NULL;
1511 continue;
1512 case lang_output_section_statement_enum:
1513 if (assign != NULL)
1514 where = assign;
1515 break;
1516 case lang_input_statement_enum:
1517 case lang_address_statement_enum:
1518 case lang_target_statement_enum:
1519 case lang_output_statement_enum:
1520 case lang_group_statement_enum:
1521 case lang_insert_statement_enum:
1522 continue;
1524 break;
1527 return where;
1530 lang_output_section_statement_type *
1531 lang_insert_orphan (asection *s,
1532 const char *secname,
1533 lang_output_section_statement_type *after,
1534 struct orphan_save *place,
1535 etree_type *address,
1536 lang_statement_list_type *add_child)
1538 lang_statement_list_type *old;
1539 lang_statement_list_type add;
1540 const char *ps;
1541 lang_output_section_statement_type *os;
1542 lang_output_section_statement_type **os_tail;
1544 /* Start building a list of statements for this section.
1545 First save the current statement pointer. */
1546 old = stat_ptr;
1548 /* If we have found an appropriate place for the output section
1549 statements for this orphan, add them to our own private list,
1550 inserting them later into the global statement list. */
1551 if (after != NULL)
1553 stat_ptr = &add;
1554 lang_list_init (stat_ptr);
1557 ps = NULL;
1558 if (config.build_constructors)
1560 /* If the name of the section is representable in C, then create
1561 symbols to mark the start and the end of the section. */
1562 for (ps = secname; *ps != '\0'; ps++)
1563 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1564 break;
1565 if (*ps == '\0')
1567 char *symname;
1568 etree_type *e_align;
1570 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1571 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1572 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1573 e_align = exp_unop (ALIGN_K,
1574 exp_intop ((bfd_vma) 1 << s->alignment_power));
1575 lang_add_assignment (exp_assop ('=', ".", e_align));
1576 lang_add_assignment (exp_provide (symname,
1577 exp_nameop (NAME, "."),
1578 FALSE));
1582 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1583 address = exp_intop (0);
1585 os_tail = ((lang_output_section_statement_type **)
1586 lang_output_section_statement.tail);
1587 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1588 NULL, 0);
1590 if (add_child == NULL)
1591 add_child = &os->children;
1592 lang_add_section (add_child, s, os);
1594 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1596 if (config.build_constructors && *ps == '\0')
1598 char *symname;
1600 /* lang_leave_ouput_section_statement resets stat_ptr.
1601 Put stat_ptr back where we want it. */
1602 if (after != NULL)
1603 stat_ptr = &add;
1605 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1606 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1607 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1608 lang_add_assignment (exp_provide (symname,
1609 exp_nameop (NAME, "."),
1610 FALSE));
1613 /* Restore the global list pointer. */
1614 if (after != NULL)
1615 stat_ptr = old;
1617 if (after != NULL && os->bfd_section != NULL)
1619 asection *snew, *as;
1621 snew = os->bfd_section;
1623 /* Shuffle the bfd section list to make the output file look
1624 neater. This is really only cosmetic. */
1625 if (place->section == NULL
1626 && after != (&lang_output_section_statement.head
1627 ->output_section_statement))
1629 asection *bfd_section = after->bfd_section;
1631 /* If the output statement hasn't been used to place any input
1632 sections (and thus doesn't have an output bfd_section),
1633 look for the closest prior output statement having an
1634 output section. */
1635 if (bfd_section == NULL)
1636 bfd_section = output_prev_sec_find (after);
1638 if (bfd_section != NULL && bfd_section != snew)
1639 place->section = &bfd_section->next;
1642 if (place->section == NULL)
1643 place->section = &link_info.output_bfd->sections;
1645 as = *place->section;
1647 if (!as)
1649 /* Put the section at the end of the list. */
1651 /* Unlink the section. */
1652 bfd_section_list_remove (link_info.output_bfd, snew);
1654 /* Now tack it back on in the right place. */
1655 bfd_section_list_append (link_info.output_bfd, snew);
1657 else if (as != snew && as->prev != snew)
1659 /* Unlink the section. */
1660 bfd_section_list_remove (link_info.output_bfd, snew);
1662 /* Now tack it back on in the right place. */
1663 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
1666 /* Save the end of this list. Further ophans of this type will
1667 follow the one we've just added. */
1668 place->section = &snew->next;
1670 /* The following is non-cosmetic. We try to put the output
1671 statements in some sort of reasonable order here, because they
1672 determine the final load addresses of the orphan sections.
1673 In addition, placing output statements in the wrong order may
1674 require extra segments. For instance, given a typical
1675 situation of all read-only sections placed in one segment and
1676 following that a segment containing all the read-write
1677 sections, we wouldn't want to place an orphan read/write
1678 section before or amongst the read-only ones. */
1679 if (add.head != NULL)
1681 lang_output_section_statement_type *newly_added_os;
1683 if (place->stmt == NULL)
1685 lang_statement_union_type **where = insert_os_after (after);
1687 *add.tail = *where;
1688 *where = add.head;
1690 place->os_tail = &after->next;
1692 else
1694 /* Put it after the last orphan statement we added. */
1695 *add.tail = *place->stmt;
1696 *place->stmt = add.head;
1699 /* Fix the global list pointer if we happened to tack our
1700 new list at the tail. */
1701 if (*old->tail == add.head)
1702 old->tail = add.tail;
1704 /* Save the end of this list. */
1705 place->stmt = add.tail;
1707 /* Do the same for the list of output section statements. */
1708 newly_added_os = *os_tail;
1709 *os_tail = NULL;
1710 newly_added_os->prev = (lang_output_section_statement_type *)
1711 ((char *) place->os_tail
1712 - offsetof (lang_output_section_statement_type, next));
1713 newly_added_os->next = *place->os_tail;
1714 if (newly_added_os->next != NULL)
1715 newly_added_os->next->prev = newly_added_os;
1716 *place->os_tail = newly_added_os;
1717 place->os_tail = &newly_added_os->next;
1719 /* Fixing the global list pointer here is a little different.
1720 We added to the list in lang_enter_output_section_statement,
1721 trimmed off the new output_section_statment above when
1722 assigning *os_tail = NULL, but possibly added it back in
1723 the same place when assigning *place->os_tail. */
1724 if (*os_tail == NULL)
1725 lang_output_section_statement.tail
1726 = (lang_statement_union_type **) os_tail;
1729 return os;
1732 static void
1733 lang_map_flags (flagword flag)
1735 if (flag & SEC_ALLOC)
1736 minfo ("a");
1738 if (flag & SEC_CODE)
1739 minfo ("x");
1741 if (flag & SEC_READONLY)
1742 minfo ("r");
1744 if (flag & SEC_DATA)
1745 minfo ("w");
1747 if (flag & SEC_LOAD)
1748 minfo ("l");
1751 void
1752 lang_map (void)
1754 lang_memory_region_type *m;
1755 bfd_boolean dis_header_printed = FALSE;
1756 bfd *p;
1758 LANG_FOR_EACH_INPUT_STATEMENT (file)
1760 asection *s;
1762 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1763 || file->just_syms_flag)
1764 continue;
1766 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1767 if ((s->output_section == NULL
1768 || s->output_section->owner != link_info.output_bfd)
1769 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
1771 if (! dis_header_printed)
1773 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1774 dis_header_printed = TRUE;
1777 print_input_section (s);
1781 minfo (_("\nMemory Configuration\n\n"));
1782 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1783 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1785 for (m = lang_memory_region_list; m != NULL; m = m->next)
1787 char buf[100];
1788 int len;
1790 fprintf (config.map_file, "%-16s ", m->name);
1792 sprintf_vma (buf, m->origin);
1793 minfo ("0x%s ", buf);
1794 len = strlen (buf);
1795 while (len < 16)
1797 print_space ();
1798 ++len;
1801 minfo ("0x%V", m->length);
1802 if (m->flags || m->not_flags)
1804 #ifndef BFD64
1805 minfo (" ");
1806 #endif
1807 if (m->flags)
1809 print_space ();
1810 lang_map_flags (m->flags);
1813 if (m->not_flags)
1815 minfo (" !");
1816 lang_map_flags (m->not_flags);
1820 print_nl ();
1823 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1825 if (! link_info.reduce_memory_overheads)
1827 obstack_begin (&map_obstack, 1000);
1828 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1829 bfd_map_over_sections (p, init_map_userdata, 0);
1830 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1832 lang_statement_iteration ++;
1833 print_statements ();
1836 static void
1837 init_map_userdata (abfd, sec, data)
1838 bfd *abfd ATTRIBUTE_UNUSED;
1839 asection *sec;
1840 void *data ATTRIBUTE_UNUSED;
1842 fat_section_userdata_type *new_data
1843 = ((fat_section_userdata_type *) (stat_alloc
1844 (sizeof (fat_section_userdata_type))));
1846 ASSERT (get_userdata (sec) == NULL);
1847 get_userdata (sec) = new_data;
1848 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1851 static bfd_boolean
1852 sort_def_symbol (hash_entry, info)
1853 struct bfd_link_hash_entry *hash_entry;
1854 void *info ATTRIBUTE_UNUSED;
1856 if (hash_entry->type == bfd_link_hash_defined
1857 || hash_entry->type == bfd_link_hash_defweak)
1859 struct fat_user_section_struct *ud;
1860 struct map_symbol_def *def;
1862 ud = get_userdata (hash_entry->u.def.section);
1863 if (! ud)
1865 /* ??? What do we have to do to initialize this beforehand? */
1866 /* The first time we get here is bfd_abs_section... */
1867 init_map_userdata (0, hash_entry->u.def.section, 0);
1868 ud = get_userdata (hash_entry->u.def.section);
1870 else if (!ud->map_symbol_def_tail)
1871 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1873 def = obstack_alloc (&map_obstack, sizeof *def);
1874 def->entry = hash_entry;
1875 *(ud->map_symbol_def_tail) = def;
1876 ud->map_symbol_def_tail = &def->next;
1878 return TRUE;
1881 /* Initialize an output section. */
1883 static void
1884 init_os (lang_output_section_statement_type *s, asection *isec,
1885 flagword flags)
1887 if (s->bfd_section != NULL)
1888 return;
1890 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1891 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1893 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
1894 if (s->bfd_section == NULL)
1895 s->bfd_section = bfd_make_section_with_flags (link_info.output_bfd,
1896 s->name, flags);
1897 if (s->bfd_section == NULL)
1899 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1900 link_info.output_bfd->xvec->name, s->name);
1902 s->bfd_section->output_section = s->bfd_section;
1903 s->bfd_section->output_offset = 0;
1905 if (!link_info.reduce_memory_overheads)
1907 fat_section_userdata_type *new
1908 = stat_alloc (sizeof (fat_section_userdata_type));
1909 memset (new, 0, sizeof (fat_section_userdata_type));
1910 get_userdata (s->bfd_section) = new;
1913 /* If there is a base address, make sure that any sections it might
1914 mention are initialized. */
1915 if (s->addr_tree != NULL)
1916 exp_init_os (s->addr_tree);
1918 if (s->load_base != NULL)
1919 exp_init_os (s->load_base);
1921 /* If supplied an alignment, set it. */
1922 if (s->section_alignment != -1)
1923 s->bfd_section->alignment_power = s->section_alignment;
1925 if (isec)
1926 bfd_init_private_section_data (isec->owner, isec,
1927 link_info.output_bfd, s->bfd_section,
1928 &link_info);
1931 /* Make sure that all output sections mentioned in an expression are
1932 initialized. */
1934 static void
1935 exp_init_os (etree_type *exp)
1937 switch (exp->type.node_class)
1939 case etree_assign:
1940 case etree_provide:
1941 exp_init_os (exp->assign.src);
1942 break;
1944 case etree_binary:
1945 exp_init_os (exp->binary.lhs);
1946 exp_init_os (exp->binary.rhs);
1947 break;
1949 case etree_trinary:
1950 exp_init_os (exp->trinary.cond);
1951 exp_init_os (exp->trinary.lhs);
1952 exp_init_os (exp->trinary.rhs);
1953 break;
1955 case etree_assert:
1956 exp_init_os (exp->assert_s.child);
1957 break;
1959 case etree_unary:
1960 exp_init_os (exp->unary.child);
1961 break;
1963 case etree_name:
1964 switch (exp->type.node_code)
1966 case ADDR:
1967 case LOADADDR:
1968 case SIZEOF:
1970 lang_output_section_statement_type *os;
1972 os = lang_output_section_find (exp->name.name);
1973 if (os != NULL && os->bfd_section == NULL)
1974 init_os (os, NULL, 0);
1977 break;
1979 default:
1980 break;
1984 static void
1985 section_already_linked (bfd *abfd, asection *sec, void *data)
1987 lang_input_statement_type *entry = data;
1989 /* If we are only reading symbols from this object, then we want to
1990 discard all sections. */
1991 if (entry->just_syms_flag)
1993 bfd_link_just_syms (abfd, sec, &link_info);
1994 return;
1997 if (!(abfd->flags & DYNAMIC))
1998 bfd_section_already_linked (abfd, sec, &link_info);
2001 /* The wild routines.
2003 These expand statements like *(.text) and foo.o to a list of
2004 explicit actions, like foo.o(.text), bar.o(.text) and
2005 foo.o(.text, .data). */
2007 /* Add SECTION to the output section OUTPUT. Do this by creating a
2008 lang_input_section statement which is placed at PTR. FILE is the
2009 input file which holds SECTION. */
2011 void
2012 lang_add_section (lang_statement_list_type *ptr,
2013 asection *section,
2014 lang_output_section_statement_type *output)
2016 flagword flags = section->flags;
2017 bfd_boolean discard;
2019 /* Discard sections marked with SEC_EXCLUDE. */
2020 discard = (flags & SEC_EXCLUDE) != 0;
2022 /* Discard input sections which are assigned to a section named
2023 DISCARD_SECTION_NAME. */
2024 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2025 discard = TRUE;
2027 /* Discard debugging sections if we are stripping debugging
2028 information. */
2029 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2030 && (flags & SEC_DEBUGGING) != 0)
2031 discard = TRUE;
2033 if (discard)
2035 if (section->output_section == NULL)
2037 /* This prevents future calls from assigning this section. */
2038 section->output_section = bfd_abs_section_ptr;
2040 return;
2043 if (section->output_section == NULL)
2045 bfd_boolean first;
2046 lang_input_section_type *new;
2047 flagword flags;
2049 flags = section->flags;
2051 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2052 to an output section, because we want to be able to include a
2053 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2054 section (I don't know why we want to do this, but we do).
2055 build_link_order in ldwrite.c handles this case by turning
2056 the embedded SEC_NEVER_LOAD section into a fill. */
2058 flags &= ~ SEC_NEVER_LOAD;
2060 switch (output->sectype)
2062 case normal_section:
2063 case overlay_section:
2064 break;
2065 case noalloc_section:
2066 flags &= ~SEC_ALLOC;
2067 break;
2068 case noload_section:
2069 flags &= ~SEC_LOAD;
2070 flags |= SEC_NEVER_LOAD;
2071 break;
2074 if (output->bfd_section == NULL)
2075 init_os (output, section, flags);
2077 first = ! output->bfd_section->linker_has_input;
2078 output->bfd_section->linker_has_input = 1;
2080 if (!link_info.relocatable
2081 && !stripped_excluded_sections)
2083 asection *s = output->bfd_section->map_tail.s;
2084 output->bfd_section->map_tail.s = section;
2085 section->map_head.s = NULL;
2086 section->map_tail.s = s;
2087 if (s != NULL)
2088 s->map_head.s = section;
2089 else
2090 output->bfd_section->map_head.s = section;
2093 /* Add a section reference to the list. */
2094 new = new_stat (lang_input_section, ptr);
2096 new->section = section;
2097 section->output_section = output->bfd_section;
2099 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2100 already been processed. One reason to do this is that on pe
2101 format targets, .text$foo sections go into .text and it's odd
2102 to see .text with SEC_LINK_ONCE set. */
2104 if (! link_info.relocatable)
2105 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2107 /* If this is not the first input section, and the SEC_READONLY
2108 flag is not currently set, then don't set it just because the
2109 input section has it set. */
2111 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2112 flags &= ~ SEC_READONLY;
2114 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2115 if (! first
2116 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2117 != (flags & (SEC_MERGE | SEC_STRINGS))
2118 || ((flags & SEC_MERGE)
2119 && output->bfd_section->entsize != section->entsize)))
2121 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2122 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2125 output->bfd_section->flags |= flags;
2127 if (flags & SEC_MERGE)
2128 output->bfd_section->entsize = section->entsize;
2130 /* If SEC_READONLY is not set in the input section, then clear
2131 it from the output section. */
2132 if ((section->flags & SEC_READONLY) == 0)
2133 output->bfd_section->flags &= ~SEC_READONLY;
2135 /* Copy over SEC_SMALL_DATA. */
2136 if (section->flags & SEC_SMALL_DATA)
2137 output->bfd_section->flags |= SEC_SMALL_DATA;
2139 if (section->alignment_power > output->bfd_section->alignment_power)
2140 output->bfd_section->alignment_power = section->alignment_power;
2142 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2143 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2145 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2146 /* FIXME: This value should really be obtained from the bfd... */
2147 output->block_value = 128;
2152 /* Handle wildcard sorting. This returns the lang_input_section which
2153 should follow the one we are going to create for SECTION and FILE,
2154 based on the sorting requirements of WILD. It returns NULL if the
2155 new section should just go at the end of the current list. */
2157 static lang_statement_union_type *
2158 wild_sort (lang_wild_statement_type *wild,
2159 struct wildcard_list *sec,
2160 lang_input_statement_type *file,
2161 asection *section)
2163 const char *section_name;
2164 lang_statement_union_type *l;
2166 if (!wild->filenames_sorted
2167 && (sec == NULL || sec->spec.sorted == none))
2168 return NULL;
2170 section_name = bfd_get_section_name (file->the_bfd, section);
2171 for (l = wild->children.head; l != NULL; l = l->header.next)
2173 lang_input_section_type *ls;
2175 if (l->header.type != lang_input_section_enum)
2176 continue;
2177 ls = &l->input_section;
2179 /* Sorting by filename takes precedence over sorting by section
2180 name. */
2182 if (wild->filenames_sorted)
2184 const char *fn, *ln;
2185 bfd_boolean fa, la;
2186 int i;
2188 /* The PE support for the .idata section as generated by
2189 dlltool assumes that files will be sorted by the name of
2190 the archive and then the name of the file within the
2191 archive. */
2193 if (file->the_bfd != NULL
2194 && bfd_my_archive (file->the_bfd) != NULL)
2196 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2197 fa = TRUE;
2199 else
2201 fn = file->filename;
2202 fa = FALSE;
2205 if (bfd_my_archive (ls->section->owner) != NULL)
2207 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2208 la = TRUE;
2210 else
2212 ln = ls->section->owner->filename;
2213 la = FALSE;
2216 i = strcmp (fn, ln);
2217 if (i > 0)
2218 continue;
2219 else if (i < 0)
2220 break;
2222 if (fa || la)
2224 if (fa)
2225 fn = file->filename;
2226 if (la)
2227 ln = ls->section->owner->filename;
2229 i = strcmp (fn, ln);
2230 if (i > 0)
2231 continue;
2232 else if (i < 0)
2233 break;
2237 /* Here either the files are not sorted by name, or we are
2238 looking at the sections for this file. */
2240 if (sec != NULL && sec->spec.sorted != none)
2241 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2242 break;
2245 return l;
2248 /* Expand a wild statement for a particular FILE. SECTION may be
2249 NULL, in which case it is a wild card. */
2251 static void
2252 output_section_callback (lang_wild_statement_type *ptr,
2253 struct wildcard_list *sec,
2254 asection *section,
2255 lang_input_statement_type *file,
2256 void *output)
2258 lang_statement_union_type *before;
2260 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2261 if (unique_section_p (section))
2262 return;
2264 before = wild_sort (ptr, sec, file, section);
2266 /* Here BEFORE points to the lang_input_section which
2267 should follow the one we are about to add. If BEFORE
2268 is NULL, then the section should just go at the end
2269 of the current list. */
2271 if (before == NULL)
2272 lang_add_section (&ptr->children, section,
2273 (lang_output_section_statement_type *) output);
2274 else
2276 lang_statement_list_type list;
2277 lang_statement_union_type **pp;
2279 lang_list_init (&list);
2280 lang_add_section (&list, section,
2281 (lang_output_section_statement_type *) output);
2283 /* If we are discarding the section, LIST.HEAD will
2284 be NULL. */
2285 if (list.head != NULL)
2287 ASSERT (list.head->header.next == NULL);
2289 for (pp = &ptr->children.head;
2290 *pp != before;
2291 pp = &(*pp)->header.next)
2292 ASSERT (*pp != NULL);
2294 list.head->header.next = *pp;
2295 *pp = list.head;
2300 /* Check if all sections in a wild statement for a particular FILE
2301 are readonly. */
2303 static void
2304 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2305 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2306 asection *section,
2307 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2308 void *data)
2310 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2311 if (unique_section_p (section))
2312 return;
2314 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2315 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2318 /* This is passed a file name which must have been seen already and
2319 added to the statement tree. We will see if it has been opened
2320 already and had its symbols read. If not then we'll read it. */
2322 static lang_input_statement_type *
2323 lookup_name (const char *name)
2325 lang_input_statement_type *search;
2327 for (search = (lang_input_statement_type *) input_file_chain.head;
2328 search != NULL;
2329 search = (lang_input_statement_type *) search->next_real_file)
2331 /* Use the local_sym_name as the name of the file that has
2332 already been loaded as filename might have been transformed
2333 via the search directory lookup mechanism. */
2334 const char *filename = search->local_sym_name;
2336 if (filename != NULL
2337 && strcmp (filename, name) == 0)
2338 break;
2341 if (search == NULL)
2342 search = new_afile (name, lang_input_file_is_search_file_enum,
2343 default_target, FALSE);
2345 /* If we have already added this file, or this file is not real
2346 don't add this file. */
2347 if (search->loaded || !search->real)
2348 return search;
2350 if (! load_symbols (search, NULL))
2351 return NULL;
2353 return search;
2356 /* Save LIST as a list of libraries whose symbols should not be exported. */
2358 struct excluded_lib
2360 char *name;
2361 struct excluded_lib *next;
2363 static struct excluded_lib *excluded_libs;
2365 void
2366 add_excluded_libs (const char *list)
2368 const char *p = list, *end;
2370 while (*p != '\0')
2372 struct excluded_lib *entry;
2373 end = strpbrk (p, ",:");
2374 if (end == NULL)
2375 end = p + strlen (p);
2376 entry = xmalloc (sizeof (*entry));
2377 entry->next = excluded_libs;
2378 entry->name = xmalloc (end - p + 1);
2379 memcpy (entry->name, p, end - p);
2380 entry->name[end - p] = '\0';
2381 excluded_libs = entry;
2382 if (*end == '\0')
2383 break;
2384 p = end + 1;
2388 static void
2389 check_excluded_libs (bfd *abfd)
2391 struct excluded_lib *lib = excluded_libs;
2393 while (lib)
2395 int len = strlen (lib->name);
2396 const char *filename = lbasename (abfd->filename);
2398 if (strcmp (lib->name, "ALL") == 0)
2400 abfd->no_export = TRUE;
2401 return;
2404 if (strncmp (lib->name, filename, len) == 0
2405 && (filename[len] == '\0'
2406 || (filename[len] == '.' && filename[len + 1] == 'a'
2407 && filename[len + 2] == '\0')))
2409 abfd->no_export = TRUE;
2410 return;
2413 lib = lib->next;
2417 /* Get the symbols for an input file. */
2419 bfd_boolean
2420 load_symbols (lang_input_statement_type *entry,
2421 lang_statement_list_type *place)
2423 char **matching;
2425 if (entry->loaded)
2426 return TRUE;
2428 ldfile_open_file (entry);
2430 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2431 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2433 bfd_error_type err;
2434 lang_statement_list_type *hold;
2435 bfd_boolean bad_load = TRUE;
2436 bfd_boolean save_ldlang_sysrooted_script;
2437 bfd_boolean save_as_needed, save_add_needed;
2439 err = bfd_get_error ();
2441 /* See if the emulation has some special knowledge. */
2442 if (ldemul_unrecognized_file (entry))
2443 return TRUE;
2445 if (err == bfd_error_file_ambiguously_recognized)
2447 char **p;
2449 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2450 einfo (_("%B: matching formats:"), entry->the_bfd);
2451 for (p = matching; *p != NULL; p++)
2452 einfo (" %s", *p);
2453 einfo ("%F\n");
2455 else if (err != bfd_error_file_not_recognized
2456 || place == NULL)
2457 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2458 else
2459 bad_load = FALSE;
2461 bfd_close (entry->the_bfd);
2462 entry->the_bfd = NULL;
2464 /* Try to interpret the file as a linker script. */
2465 ldfile_open_command_file (entry->filename);
2467 hold = stat_ptr;
2468 stat_ptr = place;
2469 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2470 ldlang_sysrooted_script = entry->sysrooted;
2471 save_as_needed = as_needed;
2472 as_needed = entry->as_needed;
2473 save_add_needed = add_needed;
2474 add_needed = entry->add_needed;
2476 ldfile_assumed_script = TRUE;
2477 parser_input = input_script;
2478 /* We want to use the same -Bdynamic/-Bstatic as the one for
2479 ENTRY. */
2480 config.dynamic_link = entry->dynamic;
2481 yyparse ();
2482 ldfile_assumed_script = FALSE;
2484 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2485 as_needed = save_as_needed;
2486 add_needed = save_add_needed;
2487 stat_ptr = hold;
2489 return ! bad_load;
2492 if (ldemul_recognized_file (entry))
2493 return TRUE;
2495 /* We don't call ldlang_add_file for an archive. Instead, the
2496 add_symbols entry point will call ldlang_add_file, via the
2497 add_archive_element callback, for each element of the archive
2498 which is used. */
2499 switch (bfd_get_format (entry->the_bfd))
2501 default:
2502 break;
2504 case bfd_object:
2505 ldlang_add_file (entry);
2506 if (trace_files || trace_file_tries)
2507 info_msg ("%I\n", entry);
2508 break;
2510 case bfd_archive:
2511 check_excluded_libs (entry->the_bfd);
2513 if (entry->whole_archive)
2515 bfd *member = NULL;
2516 bfd_boolean loaded = TRUE;
2518 for (;;)
2520 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2522 if (member == NULL)
2523 break;
2525 if (! bfd_check_format (member, bfd_object))
2527 einfo (_("%F%B: member %B in archive is not an object\n"),
2528 entry->the_bfd, member);
2529 loaded = FALSE;
2532 if (! ((*link_info.callbacks->add_archive_element)
2533 (&link_info, member, "--whole-archive")))
2534 abort ();
2536 if (! bfd_link_add_symbols (member, &link_info))
2538 einfo (_("%F%B: could not read symbols: %E\n"), member);
2539 loaded = FALSE;
2543 entry->loaded = loaded;
2544 return loaded;
2546 break;
2549 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2550 entry->loaded = TRUE;
2551 else
2552 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2554 return entry->loaded;
2557 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2558 may be NULL, indicating that it is a wildcard. Separate
2559 lang_input_section statements are created for each part of the
2560 expansion; they are added after the wild statement S. OUTPUT is
2561 the output section. */
2563 static void
2564 wild (lang_wild_statement_type *s,
2565 const char *target ATTRIBUTE_UNUSED,
2566 lang_output_section_statement_type *output)
2568 struct wildcard_list *sec;
2570 if (s->handler_data[0]
2571 && s->handler_data[0]->spec.sorted == by_name
2572 && !s->filenames_sorted)
2574 lang_section_bst_type *tree;
2576 walk_wild (s, output_section_callback_fast, output);
2578 tree = s->tree;
2579 if (tree)
2581 output_section_callback_tree_to_list (s, tree, output);
2582 s->tree = NULL;
2585 else
2586 walk_wild (s, output_section_callback, output);
2588 if (default_common_section == NULL)
2589 for (sec = s->section_list; sec != NULL; sec = sec->next)
2590 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2592 /* Remember the section that common is going to in case we
2593 later get something which doesn't know where to put it. */
2594 default_common_section = output;
2595 break;
2599 /* Return TRUE iff target is the sought target. */
2601 static int
2602 get_target (const bfd_target *target, void *data)
2604 const char *sought = data;
2606 return strcmp (target->name, sought) == 0;
2609 /* Like strcpy() but convert to lower case as well. */
2611 static void
2612 stricpy (char *dest, char *src)
2614 char c;
2616 while ((c = *src++) != 0)
2617 *dest++ = TOLOWER (c);
2619 *dest = 0;
2622 /* Remove the first occurrence of needle (if any) in haystack
2623 from haystack. */
2625 static void
2626 strcut (char *haystack, char *needle)
2628 haystack = strstr (haystack, needle);
2630 if (haystack)
2632 char *src;
2634 for (src = haystack + strlen (needle); *src;)
2635 *haystack++ = *src++;
2637 *haystack = 0;
2641 /* Compare two target format name strings.
2642 Return a value indicating how "similar" they are. */
2644 static int
2645 name_compare (char *first, char *second)
2647 char *copy1;
2648 char *copy2;
2649 int result;
2651 copy1 = xmalloc (strlen (first) + 1);
2652 copy2 = xmalloc (strlen (second) + 1);
2654 /* Convert the names to lower case. */
2655 stricpy (copy1, first);
2656 stricpy (copy2, second);
2658 /* Remove size and endian strings from the name. */
2659 strcut (copy1, "big");
2660 strcut (copy1, "little");
2661 strcut (copy2, "big");
2662 strcut (copy2, "little");
2664 /* Return a value based on how many characters match,
2665 starting from the beginning. If both strings are
2666 the same then return 10 * their length. */
2667 for (result = 0; copy1[result] == copy2[result]; result++)
2668 if (copy1[result] == 0)
2670 result *= 10;
2671 break;
2674 free (copy1);
2675 free (copy2);
2677 return result;
2680 /* Set by closest_target_match() below. */
2681 static const bfd_target *winner;
2683 /* Scan all the valid bfd targets looking for one that has the endianness
2684 requirement that was specified on the command line, and is the nearest
2685 match to the original output target. */
2687 static int
2688 closest_target_match (const bfd_target *target, void *data)
2690 const bfd_target *original = data;
2692 if (command_line.endian == ENDIAN_BIG
2693 && target->byteorder != BFD_ENDIAN_BIG)
2694 return 0;
2696 if (command_line.endian == ENDIAN_LITTLE
2697 && target->byteorder != BFD_ENDIAN_LITTLE)
2698 return 0;
2700 /* Must be the same flavour. */
2701 if (target->flavour != original->flavour)
2702 return 0;
2704 /* Ignore generic big and little endian elf vectors. */
2705 if ( strcmp (target->name, "elf32-big") == 0
2706 || strcmp (target->name, "elf64-big") == 0
2707 || strcmp (target->name, "elf32-little") == 0
2708 || strcmp (target->name, "elf64-little") == 0)
2709 return 0;
2711 /* If we have not found a potential winner yet, then record this one. */
2712 if (winner == NULL)
2714 winner = target;
2715 return 0;
2718 /* Oh dear, we now have two potential candidates for a successful match.
2719 Compare their names and choose the better one. */
2720 if (name_compare (target->name, original->name)
2721 > name_compare (winner->name, original->name))
2722 winner = target;
2724 /* Keep on searching until wqe have checked them all. */
2725 return 0;
2728 /* Return the BFD target format of the first input file. */
2730 static char *
2731 get_first_input_target (void)
2733 char *target = NULL;
2735 LANG_FOR_EACH_INPUT_STATEMENT (s)
2737 if (s->header.type == lang_input_statement_enum
2738 && s->real)
2740 ldfile_open_file (s);
2742 if (s->the_bfd != NULL
2743 && bfd_check_format (s->the_bfd, bfd_object))
2745 target = bfd_get_target (s->the_bfd);
2747 if (target != NULL)
2748 break;
2753 return target;
2756 const char *
2757 lang_get_output_target (void)
2759 const char *target;
2761 /* Has the user told us which output format to use? */
2762 if (output_target != NULL)
2763 return output_target;
2765 /* No - has the current target been set to something other than
2766 the default? */
2767 if (current_target != default_target)
2768 return current_target;
2770 /* No - can we determine the format of the first input file? */
2771 target = get_first_input_target ();
2772 if (target != NULL)
2773 return target;
2775 /* Failed - use the default output target. */
2776 return default_target;
2779 /* Open the output file. */
2781 static void
2782 open_output (const char *name)
2784 output_target = lang_get_output_target ();
2786 /* Has the user requested a particular endianness on the command
2787 line? */
2788 if (command_line.endian != ENDIAN_UNSET)
2790 const bfd_target *target;
2791 enum bfd_endian desired_endian;
2793 /* Get the chosen target. */
2794 target = bfd_search_for_target (get_target, (void *) output_target);
2796 /* If the target is not supported, we cannot do anything. */
2797 if (target != NULL)
2799 if (command_line.endian == ENDIAN_BIG)
2800 desired_endian = BFD_ENDIAN_BIG;
2801 else
2802 desired_endian = BFD_ENDIAN_LITTLE;
2804 /* See if the target has the wrong endianness. This should
2805 not happen if the linker script has provided big and
2806 little endian alternatives, but some scrips don't do
2807 this. */
2808 if (target->byteorder != desired_endian)
2810 /* If it does, then see if the target provides
2811 an alternative with the correct endianness. */
2812 if (target->alternative_target != NULL
2813 && (target->alternative_target->byteorder == desired_endian))
2814 output_target = target->alternative_target->name;
2815 else
2817 /* Try to find a target as similar as possible to
2818 the default target, but which has the desired
2819 endian characteristic. */
2820 bfd_search_for_target (closest_target_match,
2821 (void *) target);
2823 /* Oh dear - we could not find any targets that
2824 satisfy our requirements. */
2825 if (winner == NULL)
2826 einfo (_("%P: warning: could not find any targets"
2827 " that match endianness requirement\n"));
2828 else
2829 output_target = winner->name;
2835 link_info.output_bfd = bfd_openw (name, output_target);
2837 if (link_info.output_bfd == NULL)
2839 if (bfd_get_error () == bfd_error_invalid_target)
2840 einfo (_("%P%F: target %s not found\n"), output_target);
2842 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2845 delete_output_file_on_failure = TRUE;
2847 if (! bfd_set_format (link_info.output_bfd, bfd_object))
2848 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2849 if (! bfd_set_arch_mach (link_info.output_bfd,
2850 ldfile_output_architecture,
2851 ldfile_output_machine))
2852 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2854 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
2855 if (link_info.hash == NULL)
2856 einfo (_("%P%F: can not create hash table: %E\n"));
2858 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
2861 static void
2862 ldlang_open_output (lang_statement_union_type *statement)
2864 switch (statement->header.type)
2866 case lang_output_statement_enum:
2867 ASSERT (link_info.output_bfd == NULL);
2868 open_output (statement->output_statement.name);
2869 ldemul_set_output_arch ();
2870 if (config.magic_demand_paged && !link_info.relocatable)
2871 link_info.output_bfd->flags |= D_PAGED;
2872 else
2873 link_info.output_bfd->flags &= ~D_PAGED;
2874 if (config.text_read_only)
2875 link_info.output_bfd->flags |= WP_TEXT;
2876 else
2877 link_info.output_bfd->flags &= ~WP_TEXT;
2878 if (link_info.traditional_format)
2879 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2880 else
2881 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2882 break;
2884 case lang_target_statement_enum:
2885 current_target = statement->target_statement.target;
2886 break;
2887 default:
2888 break;
2892 /* Convert between addresses in bytes and sizes in octets.
2893 For currently supported targets, octets_per_byte is always a power
2894 of two, so we can use shifts. */
2895 #define TO_ADDR(X) ((X) >> opb_shift)
2896 #define TO_SIZE(X) ((X) << opb_shift)
2898 /* Support the above. */
2899 static unsigned int opb_shift = 0;
2901 static void
2902 init_opb (void)
2904 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2905 ldfile_output_machine);
2906 opb_shift = 0;
2907 if (x > 1)
2908 while ((x & 1) == 0)
2910 x >>= 1;
2911 ++opb_shift;
2913 ASSERT (x == 1);
2916 /* Open all the input files. */
2918 static void
2919 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2921 for (; s != NULL; s = s->header.next)
2923 switch (s->header.type)
2925 case lang_constructors_statement_enum:
2926 open_input_bfds (constructor_list.head, force);
2927 break;
2928 case lang_output_section_statement_enum:
2929 open_input_bfds (s->output_section_statement.children.head, force);
2930 break;
2931 case lang_wild_statement_enum:
2932 /* Maybe we should load the file's symbols. */
2933 if (s->wild_statement.filename
2934 && ! wildcardp (s->wild_statement.filename))
2935 lookup_name (s->wild_statement.filename);
2936 open_input_bfds (s->wild_statement.children.head, force);
2937 break;
2938 case lang_group_statement_enum:
2940 struct bfd_link_hash_entry *undefs;
2942 /* We must continually search the entries in the group
2943 until no new symbols are added to the list of undefined
2944 symbols. */
2948 undefs = link_info.hash->undefs_tail;
2949 open_input_bfds (s->group_statement.children.head, TRUE);
2951 while (undefs != link_info.hash->undefs_tail);
2953 break;
2954 case lang_target_statement_enum:
2955 current_target = s->target_statement.target;
2956 break;
2957 case lang_input_statement_enum:
2958 if (s->input_statement.real)
2960 lang_statement_list_type add;
2962 s->input_statement.target = current_target;
2964 /* If we are being called from within a group, and this
2965 is an archive which has already been searched, then
2966 force it to be researched unless the whole archive
2967 has been loaded already. */
2968 if (force
2969 && !s->input_statement.whole_archive
2970 && s->input_statement.loaded
2971 && bfd_check_format (s->input_statement.the_bfd,
2972 bfd_archive))
2973 s->input_statement.loaded = FALSE;
2975 lang_list_init (&add);
2977 if (! load_symbols (&s->input_statement, &add))
2978 config.make_executable = FALSE;
2980 if (add.head != NULL)
2982 *add.tail = s->header.next;
2983 s->header.next = add.head;
2986 break;
2987 default:
2988 break;
2993 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2995 void
2996 lang_track_definedness (const char *name)
2998 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2999 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
3002 /* New-function for the definedness hash table. */
3004 static struct bfd_hash_entry *
3005 lang_definedness_newfunc (struct bfd_hash_entry *entry,
3006 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
3007 const char *name ATTRIBUTE_UNUSED)
3009 struct lang_definedness_hash_entry *ret
3010 = (struct lang_definedness_hash_entry *) entry;
3012 if (ret == NULL)
3013 ret = (struct lang_definedness_hash_entry *)
3014 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
3016 if (ret == NULL)
3017 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
3019 ret->iteration = -1;
3020 return &ret->root;
3023 /* Return the iteration when the definition of NAME was last updated. A
3024 value of -1 means that the symbol is not defined in the linker script
3025 or the command line, but may be defined in the linker symbol table. */
3028 lang_symbol_definition_iteration (const char *name)
3030 struct lang_definedness_hash_entry *defentry
3031 = (struct lang_definedness_hash_entry *)
3032 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3034 /* We've already created this one on the presence of DEFINED in the
3035 script, so it can't be NULL unless something is borked elsewhere in
3036 the code. */
3037 if (defentry == NULL)
3038 FAIL ();
3040 return defentry->iteration;
3043 /* Update the definedness state of NAME. */
3045 void
3046 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3048 struct lang_definedness_hash_entry *defentry
3049 = (struct lang_definedness_hash_entry *)
3050 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3052 /* We don't keep track of symbols not tested with DEFINED. */
3053 if (defentry == NULL)
3054 return;
3056 /* If the symbol was already defined, and not from an earlier statement
3057 iteration, don't update the definedness iteration, because that'd
3058 make the symbol seem defined in the linker script at this point, and
3059 it wasn't; it was defined in some object. If we do anyway, DEFINED
3060 would start to yield false before this point and the construct "sym =
3061 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3062 in an object. */
3063 if (h->type != bfd_link_hash_undefined
3064 && h->type != bfd_link_hash_common
3065 && h->type != bfd_link_hash_new
3066 && defentry->iteration == -1)
3067 return;
3069 defentry->iteration = lang_statement_iteration;
3072 /* Add the supplied name to the symbol table as an undefined reference.
3073 This is a two step process as the symbol table doesn't even exist at
3074 the time the ld command line is processed. First we put the name
3075 on a list, then, once the output file has been opened, transfer the
3076 name to the symbol table. */
3078 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3080 #define ldlang_undef_chain_list_head entry_symbol.next
3082 void
3083 ldlang_add_undef (const char *const name)
3085 ldlang_undef_chain_list_type *new =
3086 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3088 new->next = ldlang_undef_chain_list_head;
3089 ldlang_undef_chain_list_head = new;
3091 new->name = xstrdup (name);
3093 if (link_info.output_bfd != NULL)
3094 insert_undefined (new->name);
3097 /* Insert NAME as undefined in the symbol table. */
3099 static void
3100 insert_undefined (const char *name)
3102 struct bfd_link_hash_entry *h;
3104 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3105 if (h == NULL)
3106 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3107 if (h->type == bfd_link_hash_new)
3109 h->type = bfd_link_hash_undefined;
3110 h->u.undef.abfd = NULL;
3111 bfd_link_add_undef (link_info.hash, h);
3115 /* Run through the list of undefineds created above and place them
3116 into the linker hash table as undefined symbols belonging to the
3117 script file. */
3119 static void
3120 lang_place_undefineds (void)
3122 ldlang_undef_chain_list_type *ptr;
3124 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3125 insert_undefined (ptr->name);
3128 /* Check for all readonly or some readwrite sections. */
3130 static void
3131 check_input_sections
3132 (lang_statement_union_type *s,
3133 lang_output_section_statement_type *output_section_statement)
3135 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3137 switch (s->header.type)
3139 case lang_wild_statement_enum:
3140 walk_wild (&s->wild_statement, check_section_callback,
3141 output_section_statement);
3142 if (! output_section_statement->all_input_readonly)
3143 return;
3144 break;
3145 case lang_constructors_statement_enum:
3146 check_input_sections (constructor_list.head,
3147 output_section_statement);
3148 if (! output_section_statement->all_input_readonly)
3149 return;
3150 break;
3151 case lang_group_statement_enum:
3152 check_input_sections (s->group_statement.children.head,
3153 output_section_statement);
3154 if (! output_section_statement->all_input_readonly)
3155 return;
3156 break;
3157 default:
3158 break;
3163 /* Update wildcard statements if needed. */
3165 static void
3166 update_wild_statements (lang_statement_union_type *s)
3168 struct wildcard_list *sec;
3170 switch (sort_section)
3172 default:
3173 FAIL ();
3175 case none:
3176 break;
3178 case by_name:
3179 case by_alignment:
3180 for (; s != NULL; s = s->header.next)
3182 switch (s->header.type)
3184 default:
3185 break;
3187 case lang_wild_statement_enum:
3188 sec = s->wild_statement.section_list;
3189 for (sec = s->wild_statement.section_list; sec != NULL;
3190 sec = sec->next)
3192 switch (sec->spec.sorted)
3194 case none:
3195 sec->spec.sorted = sort_section;
3196 break;
3197 case by_name:
3198 if (sort_section == by_alignment)
3199 sec->spec.sorted = by_name_alignment;
3200 break;
3201 case by_alignment:
3202 if (sort_section == by_name)
3203 sec->spec.sorted = by_alignment_name;
3204 break;
3205 default:
3206 break;
3209 break;
3211 case lang_constructors_statement_enum:
3212 update_wild_statements (constructor_list.head);
3213 break;
3215 case lang_output_section_statement_enum:
3216 update_wild_statements
3217 (s->output_section_statement.children.head);
3218 break;
3220 case lang_group_statement_enum:
3221 update_wild_statements (s->group_statement.children.head);
3222 break;
3225 break;
3229 /* Open input files and attach to output sections. */
3231 static void
3232 map_input_to_output_sections
3233 (lang_statement_union_type *s, const char *target,
3234 lang_output_section_statement_type *os)
3236 flagword flags;
3238 for (; s != NULL; s = s->header.next)
3240 switch (s->header.type)
3242 case lang_wild_statement_enum:
3243 wild (&s->wild_statement, target, os);
3244 break;
3245 case lang_constructors_statement_enum:
3246 map_input_to_output_sections (constructor_list.head,
3247 target,
3248 os);
3249 break;
3250 case lang_output_section_statement_enum:
3251 if (s->output_section_statement.constraint)
3253 if (s->output_section_statement.constraint != ONLY_IF_RW
3254 && s->output_section_statement.constraint != ONLY_IF_RO)
3255 break;
3256 s->output_section_statement.all_input_readonly = TRUE;
3257 check_input_sections (s->output_section_statement.children.head,
3258 &s->output_section_statement);
3259 if ((s->output_section_statement.all_input_readonly
3260 && s->output_section_statement.constraint == ONLY_IF_RW)
3261 || (!s->output_section_statement.all_input_readonly
3262 && s->output_section_statement.constraint == ONLY_IF_RO))
3264 s->output_section_statement.constraint = -1;
3265 break;
3269 map_input_to_output_sections (s->output_section_statement.children.head,
3270 target,
3271 &s->output_section_statement);
3272 break;
3273 case lang_output_statement_enum:
3274 break;
3275 case lang_target_statement_enum:
3276 target = s->target_statement.target;
3277 break;
3278 case lang_group_statement_enum:
3279 map_input_to_output_sections (s->group_statement.children.head,
3280 target,
3281 os);
3282 break;
3283 case lang_data_statement_enum:
3284 /* Make sure that any sections mentioned in the expression
3285 are initialized. */
3286 exp_init_os (s->data_statement.exp);
3287 flags = SEC_HAS_CONTENTS;
3288 /* The output section gets contents, and then we inspect for
3289 any flags set in the input script which override any ALLOC. */
3290 if (!(os->flags & SEC_NEVER_LOAD))
3291 flags |= SEC_ALLOC | SEC_LOAD;
3292 if (os->bfd_section == NULL)
3293 init_os (os, NULL, flags);
3294 else
3295 os->bfd_section->flags |= flags;
3296 break;
3297 case lang_input_section_enum:
3298 break;
3299 case lang_fill_statement_enum:
3300 case lang_object_symbols_statement_enum:
3301 case lang_reloc_statement_enum:
3302 case lang_padding_statement_enum:
3303 case lang_input_statement_enum:
3304 if (os != NULL && os->bfd_section == NULL)
3305 init_os (os, NULL, 0);
3306 break;
3307 case lang_assignment_statement_enum:
3308 if (os != NULL && os->bfd_section == NULL)
3309 init_os (os, NULL, 0);
3311 /* Make sure that any sections mentioned in the assignment
3312 are initialized. */
3313 exp_init_os (s->assignment_statement.exp);
3314 break;
3315 case lang_address_statement_enum:
3316 /* Mark the specified section with the supplied address.
3318 If this section was actually a segment marker, then the
3319 directive is ignored if the linker script explicitly
3320 processed the segment marker. Originally, the linker
3321 treated segment directives (like -Ttext on the
3322 command-line) as section directives. We honor the
3323 section directive semantics for backwards compatibilty;
3324 linker scripts that do not specifically check for
3325 SEGMENT_START automatically get the old semantics. */
3326 if (!s->address_statement.segment
3327 || !s->address_statement.segment->used)
3329 lang_output_section_statement_type *aos
3330 = (lang_output_section_statement_lookup
3331 (s->address_statement.section_name));
3333 if (aos->bfd_section == NULL)
3334 init_os (aos, NULL, 0);
3335 aos->addr_tree = s->address_statement.address;
3337 break;
3338 case lang_insert_statement_enum:
3339 break;
3344 /* An insert statement snips out all the linker statements from the
3345 start of the list and places them after the output section
3346 statement specified by the insert. This operation is complicated
3347 by the fact that we keep a doubly linked list of output section
3348 statements as well as the singly linked list of all statements. */
3350 static void
3351 process_insert_statements (void)
3353 lang_statement_union_type **s;
3354 lang_output_section_statement_type *first_os = NULL;
3355 lang_output_section_statement_type *last_os = NULL;
3357 /* "start of list" is actually the statement immediately after
3358 the special abs_section output statement, so that it isn't
3359 reordered. */
3360 s = &lang_output_section_statement.head;
3361 while (*(s = &(*s)->header.next) != NULL)
3363 if ((*s)->header.type == lang_output_section_statement_enum)
3365 /* Keep pointers to the first and last output section
3366 statement in the sequence we may be about to move. */
3367 last_os = &(*s)->output_section_statement;
3368 if (first_os == NULL)
3369 first_os = last_os;
3371 else if ((*s)->header.type == lang_insert_statement_enum)
3373 lang_insert_statement_type *i = &(*s)->insert_statement;
3374 lang_output_section_statement_type *where;
3375 lang_output_section_statement_type *os;
3376 lang_statement_union_type **ptr;
3377 lang_statement_union_type *first;
3379 where = lang_output_section_find (i->where);
3380 if (where != NULL && i->is_before)
3383 where = where->prev;
3384 while (where != NULL && where->constraint == -1);
3386 if (where == NULL)
3388 einfo (_("%X%P: %s not found for insert\n"), i->where);
3389 continue;
3391 /* You can't insert into the list you are moving. */
3392 for (os = first_os; os != NULL; os = os->next)
3393 if (os == where || os == last_os)
3394 break;
3395 if (os == where)
3397 einfo (_("%X%P: %s not found for insert\n"), i->where);
3398 continue;
3401 /* Deal with reordering the output section statement list. */
3402 if (last_os != NULL)
3404 asection *first_sec, *last_sec;
3405 struct lang_output_section_statement_struct **next;
3407 /* Snip out the output sections we are moving. */
3408 first_os->prev->next = last_os->next;
3409 if (last_os->next == NULL)
3411 next = &first_os->prev->next;
3412 lang_output_section_statement.tail
3413 = (lang_statement_union_type **) next;
3415 else
3416 last_os->next->prev = first_os->prev;
3417 /* Add them in at the new position. */
3418 last_os->next = where->next;
3419 if (where->next == NULL)
3421 next = &last_os->next;
3422 lang_output_section_statement.tail
3423 = (lang_statement_union_type **) next;
3425 else
3426 where->next->prev = last_os;
3427 first_os->prev = where;
3428 where->next = first_os;
3430 /* Move the bfd sections in the same way. */
3431 first_sec = NULL;
3432 last_sec = NULL;
3433 for (os = first_os; os != NULL; os = os->next)
3435 if (os->bfd_section != NULL
3436 && os->bfd_section->owner != NULL)
3438 last_sec = os->bfd_section;
3439 if (first_sec == NULL)
3440 first_sec = last_sec;
3442 if (os == last_os)
3443 break;
3445 if (last_sec != NULL)
3447 asection *sec = where->bfd_section;
3448 if (sec == NULL)
3449 sec = output_prev_sec_find (where);
3451 /* The place we want to insert must come after the
3452 sections we are moving. So if we find no
3453 section or if the section is the same as our
3454 last section, then no move is needed. */
3455 if (sec != NULL && sec != last_sec)
3457 /* Trim them off. */
3458 if (first_sec->prev != NULL)
3459 first_sec->prev->next = last_sec->next;
3460 else
3461 link_info.output_bfd->sections = last_sec->next;
3462 if (last_sec->next != NULL)
3463 last_sec->next->prev = first_sec->prev;
3464 else
3465 link_info.output_bfd->section_last = first_sec->prev;
3466 /* Add back. */
3467 last_sec->next = sec->next;
3468 if (sec->next != NULL)
3469 sec->next->prev = last_sec;
3470 else
3471 link_info.output_bfd->section_last = last_sec;
3472 first_sec->prev = sec;
3473 sec->next = first_sec;
3477 first_os = NULL;
3478 last_os = NULL;
3481 ptr = insert_os_after (where);
3482 /* Snip everything after the abs_section output statement we
3483 know is at the start of the list, up to and including
3484 the insert statement we are currently processing. */
3485 first = lang_output_section_statement.head->header.next;
3486 lang_output_section_statement.head->header.next = (*s)->header.next;
3487 /* Add them back where they belong. */
3488 *s = *ptr;
3489 if (*s == NULL)
3490 statement_list.tail = s;
3491 *ptr = first;
3492 s = &lang_output_section_statement.head;
3497 /* An output section might have been removed after its statement was
3498 added. For example, ldemul_before_allocation can remove dynamic
3499 sections if they turn out to be not needed. Clean them up here. */
3501 void
3502 strip_excluded_output_sections (void)
3504 lang_output_section_statement_type *os;
3506 /* Run lang_size_sections (if not already done). */
3507 if (expld.phase != lang_mark_phase_enum)
3509 expld.phase = lang_mark_phase_enum;
3510 expld.dataseg.phase = exp_dataseg_none;
3511 one_lang_size_sections_pass (NULL, FALSE);
3512 lang_reset_memory_regions ();
3515 for (os = &lang_output_section_statement.head->output_section_statement;
3516 os != NULL;
3517 os = os->next)
3519 asection *output_section;
3520 bfd_boolean exclude;
3522 if (os->constraint == -1)
3523 continue;
3525 output_section = os->bfd_section;
3526 if (output_section == NULL)
3527 continue;
3529 exclude = (output_section->rawsize == 0
3530 && (output_section->flags & SEC_KEEP) == 0
3531 && !bfd_section_removed_from_list (link_info.output_bfd,
3532 output_section));
3534 /* Some sections have not yet been sized, notably .gnu.version,
3535 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3536 input sections, so don't drop output sections that have such
3537 input sections unless they are also marked SEC_EXCLUDE. */
3538 if (exclude && output_section->map_head.s != NULL)
3540 asection *s;
3542 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3543 if ((s->flags & SEC_LINKER_CREATED) != 0
3544 && (s->flags & SEC_EXCLUDE) == 0)
3546 exclude = FALSE;
3547 break;
3551 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3552 output_section->map_head.link_order = NULL;
3553 output_section->map_tail.link_order = NULL;
3555 if (exclude)
3557 /* We don't set bfd_section to NULL since bfd_section of the
3558 removed output section statement may still be used. */
3559 if (!os->section_relative_symbol
3560 && !os->update_dot_tree)
3561 os->ignored = TRUE;
3562 output_section->flags |= SEC_EXCLUDE;
3563 bfd_section_list_remove (link_info.output_bfd, output_section);
3564 link_info.output_bfd->section_count--;
3568 /* Stop future calls to lang_add_section from messing with map_head
3569 and map_tail link_order fields. */
3570 stripped_excluded_sections = TRUE;
3573 static void
3574 print_output_section_statement
3575 (lang_output_section_statement_type *output_section_statement)
3577 asection *section = output_section_statement->bfd_section;
3578 int len;
3580 if (output_section_statement != abs_output_section)
3582 minfo ("\n%s", output_section_statement->name);
3584 if (section != NULL)
3586 print_dot = section->vma;
3588 len = strlen (output_section_statement->name);
3589 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3591 print_nl ();
3592 len = 0;
3594 while (len < SECTION_NAME_MAP_LENGTH)
3596 print_space ();
3597 ++len;
3600 minfo ("0x%V %W", section->vma, section->size);
3602 if (section->vma != section->lma)
3603 minfo (_(" load address 0x%V"), section->lma);
3606 print_nl ();
3609 print_statement_list (output_section_statement->children.head,
3610 output_section_statement);
3613 /* Scan for the use of the destination in the right hand side
3614 of an expression. In such cases we will not compute the
3615 correct expression, since the value of DST that is used on
3616 the right hand side will be its final value, not its value
3617 just before this expression is evaluated. */
3619 static bfd_boolean
3620 scan_for_self_assignment (const char * dst, etree_type * rhs)
3622 if (rhs == NULL || dst == NULL)
3623 return FALSE;
3625 switch (rhs->type.node_class)
3627 case etree_binary:
3628 return scan_for_self_assignment (dst, rhs->binary.lhs)
3629 || scan_for_self_assignment (dst, rhs->binary.rhs);
3631 case etree_trinary:
3632 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3633 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3635 case etree_assign:
3636 case etree_provided:
3637 case etree_provide:
3638 if (strcmp (dst, rhs->assign.dst) == 0)
3639 return TRUE;
3640 return scan_for_self_assignment (dst, rhs->assign.src);
3642 case etree_unary:
3643 return scan_for_self_assignment (dst, rhs->unary.child);
3645 case etree_value:
3646 if (rhs->value.str)
3647 return strcmp (dst, rhs->value.str) == 0;
3648 return FALSE;
3650 case etree_name:
3651 if (rhs->name.name)
3652 return strcmp (dst, rhs->name.name) == 0;
3653 return FALSE;
3655 default:
3656 break;
3659 return FALSE;
3663 static void
3664 print_assignment (lang_assignment_statement_type *assignment,
3665 lang_output_section_statement_type *output_section)
3667 unsigned int i;
3668 bfd_boolean is_dot;
3669 bfd_boolean computation_is_valid = TRUE;
3670 etree_type *tree;
3672 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3673 print_space ();
3675 if (assignment->exp->type.node_class == etree_assert)
3677 is_dot = FALSE;
3678 tree = assignment->exp->assert_s.child;
3679 computation_is_valid = TRUE;
3681 else
3683 const char *dst = assignment->exp->assign.dst;
3685 is_dot = (dst[0] == '.' && dst[1] == 0);
3686 tree = assignment->exp->assign.src;
3687 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3690 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3691 if (expld.result.valid_p)
3693 bfd_vma value;
3695 if (computation_is_valid)
3697 value = expld.result.value;
3699 if (expld.result.section)
3700 value += expld.result.section->vma;
3702 minfo ("0x%V", value);
3703 if (is_dot)
3704 print_dot = value;
3706 else
3708 struct bfd_link_hash_entry *h;
3710 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3711 FALSE, FALSE, TRUE);
3712 if (h)
3714 value = h->u.def.value;
3716 if (expld.result.section)
3717 value += expld.result.section->vma;
3719 minfo ("[0x%V]", value);
3721 else
3722 minfo ("[unresolved]");
3725 else
3727 minfo ("*undef* ");
3728 #ifdef BFD64
3729 minfo (" ");
3730 #endif
3733 minfo (" ");
3734 exp_print_tree (assignment->exp);
3735 print_nl ();
3738 static void
3739 print_input_statement (lang_input_statement_type *statm)
3741 if ((statm->filename != NULL)
3742 && ((statm->the_bfd == NULL)
3744 ((statm->the_bfd->flags & BFD_LINKER_CREATED) == 0)))
3745 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3748 /* Print all symbols defined in a particular section. This is called
3749 via bfd_link_hash_traverse, or by print_all_symbols. */
3751 static bfd_boolean
3752 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3754 asection *sec = ptr;
3756 if ((hash_entry->type == bfd_link_hash_defined
3757 || hash_entry->type == bfd_link_hash_defweak)
3758 && sec == hash_entry->u.def.section)
3760 int i;
3762 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3763 print_space ();
3764 minfo ("0x%V ",
3765 (hash_entry->u.def.value
3766 + hash_entry->u.def.section->output_offset
3767 + hash_entry->u.def.section->output_section->vma));
3769 minfo (" %T\n", hash_entry->root.string);
3772 return TRUE;
3775 static void
3776 print_all_symbols (asection *sec)
3778 struct fat_user_section_struct *ud = get_userdata (sec);
3779 struct map_symbol_def *def;
3781 if (!ud)
3782 return;
3784 *ud->map_symbol_def_tail = 0;
3785 for (def = ud->map_symbol_def_head; def; def = def->next)
3786 print_one_symbol (def->entry, sec);
3789 /* Print information about an input section to the map file. */
3791 static void
3792 print_input_section (asection *i)
3794 bfd_size_type size = i->size;
3795 int len;
3796 bfd_vma addr;
3798 init_opb ();
3800 print_space ();
3801 minfo ("%s", i->name);
3803 len = 1 + strlen (i->name);
3804 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3806 print_nl ();
3807 len = 0;
3809 while (len < SECTION_NAME_MAP_LENGTH)
3811 print_space ();
3812 ++len;
3815 if (i->output_section != NULL
3816 && i->output_section->owner == link_info.output_bfd)
3817 addr = i->output_section->vma + i->output_offset;
3818 else
3820 addr = print_dot;
3821 size = 0;
3824 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3826 if (size != i->rawsize && i->rawsize != 0)
3828 len = SECTION_NAME_MAP_LENGTH + 3;
3829 #ifdef BFD64
3830 len += 16;
3831 #else
3832 len += 8;
3833 #endif
3834 while (len > 0)
3836 print_space ();
3837 --len;
3840 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3843 if (i->output_section != NULL
3844 && i->output_section->owner == link_info.output_bfd)
3846 if (link_info.reduce_memory_overheads)
3847 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3848 else
3849 print_all_symbols (i);
3851 print_dot = addr + TO_ADDR (size);
3855 static void
3856 print_fill_statement (lang_fill_statement_type *fill)
3858 size_t size;
3859 unsigned char *p;
3860 fputs (" FILL mask 0x", config.map_file);
3861 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
3862 fprintf (config.map_file, "%02x", *p);
3863 fputs ("\n", config.map_file);
3866 static void
3867 print_data_statement (lang_data_statement_type *data)
3869 int i;
3870 bfd_vma addr;
3871 bfd_size_type size;
3872 const char *name;
3874 init_opb ();
3875 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3876 print_space ();
3878 addr = data->output_offset;
3879 if (data->output_section != NULL)
3880 addr += data->output_section->vma;
3882 switch (data->type)
3884 default:
3885 abort ();
3886 case BYTE:
3887 size = BYTE_SIZE;
3888 name = "BYTE";
3889 break;
3890 case SHORT:
3891 size = SHORT_SIZE;
3892 name = "SHORT";
3893 break;
3894 case LONG:
3895 size = LONG_SIZE;
3896 name = "LONG";
3897 break;
3898 case QUAD:
3899 size = QUAD_SIZE;
3900 name = "QUAD";
3901 break;
3902 case SQUAD:
3903 size = QUAD_SIZE;
3904 name = "SQUAD";
3905 break;
3908 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
3910 if (data->exp->type.node_class != etree_value)
3912 print_space ();
3913 exp_print_tree (data->exp);
3916 print_nl ();
3918 print_dot = addr + TO_ADDR (size);
3921 /* Print an address statement. These are generated by options like
3922 -Ttext. */
3924 static void
3925 print_address_statement (lang_address_statement_type *address)
3927 minfo (_("Address of section %s set to "), address->section_name);
3928 exp_print_tree (address->address);
3929 print_nl ();
3932 /* Print a reloc statement. */
3934 static void
3935 print_reloc_statement (lang_reloc_statement_type *reloc)
3937 int i;
3938 bfd_vma addr;
3939 bfd_size_type size;
3941 init_opb ();
3942 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3943 print_space ();
3945 addr = reloc->output_offset;
3946 if (reloc->output_section != NULL)
3947 addr += reloc->output_section->vma;
3949 size = bfd_get_reloc_size (reloc->howto);
3951 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3953 if (reloc->name != NULL)
3954 minfo ("%s+", reloc->name);
3955 else
3956 minfo ("%s+", reloc->section->name);
3958 exp_print_tree (reloc->addend_exp);
3960 print_nl ();
3962 print_dot = addr + TO_ADDR (size);
3965 static void
3966 print_padding_statement (lang_padding_statement_type *s)
3968 int len;
3969 bfd_vma addr;
3971 init_opb ();
3972 minfo (" *fill*");
3974 len = sizeof " *fill*" - 1;
3975 while (len < SECTION_NAME_MAP_LENGTH)
3977 print_space ();
3978 ++len;
3981 addr = s->output_offset;
3982 if (s->output_section != NULL)
3983 addr += s->output_section->vma;
3984 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3986 if (s->fill->size != 0)
3988 size_t size;
3989 unsigned char *p;
3990 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3991 fprintf (config.map_file, "%02x", *p);
3994 print_nl ();
3996 print_dot = addr + TO_ADDR (s->size);
3999 static void
4000 print_wild_statement (lang_wild_statement_type *w,
4001 lang_output_section_statement_type *os)
4003 struct wildcard_list *sec;
4005 print_space ();
4007 if (w->filenames_sorted)
4008 minfo ("SORT(");
4009 if (w->filename != NULL)
4010 minfo ("%s", w->filename);
4011 else
4012 minfo ("*");
4013 if (w->filenames_sorted)
4014 minfo (")");
4016 minfo ("(");
4017 for (sec = w->section_list; sec; sec = sec->next)
4019 if (sec->spec.sorted)
4020 minfo ("SORT(");
4021 if (sec->spec.exclude_name_list != NULL)
4023 name_list *tmp;
4024 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
4025 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
4026 minfo (" %s", tmp->name);
4027 minfo (") ");
4029 if (sec->spec.name != NULL)
4030 minfo ("%s", sec->spec.name);
4031 else
4032 minfo ("*");
4033 if (sec->spec.sorted)
4034 minfo (")");
4035 if (sec->next)
4036 minfo (" ");
4038 minfo (")");
4040 print_nl ();
4042 print_statement_list (w->children.head, os);
4045 /* Print a group statement. */
4047 static void
4048 print_group (lang_group_statement_type *s,
4049 lang_output_section_statement_type *os)
4051 fprintf (config.map_file, "START GROUP\n");
4052 print_statement_list (s->children.head, os);
4053 fprintf (config.map_file, "END GROUP\n");
4056 /* Print the list of statements in S.
4057 This can be called for any statement type. */
4059 static void
4060 print_statement_list (lang_statement_union_type *s,
4061 lang_output_section_statement_type *os)
4063 while (s != NULL)
4065 print_statement (s, os);
4066 s = s->header.next;
4070 /* Print the first statement in statement list S.
4071 This can be called for any statement type. */
4073 static void
4074 print_statement (lang_statement_union_type *s,
4075 lang_output_section_statement_type *os)
4077 switch (s->header.type)
4079 default:
4080 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4081 FAIL ();
4082 break;
4083 case lang_constructors_statement_enum:
4084 if (constructor_list.head != NULL)
4086 if (constructors_sorted)
4087 minfo (" SORT (CONSTRUCTORS)\n");
4088 else
4089 minfo (" CONSTRUCTORS\n");
4090 print_statement_list (constructor_list.head, os);
4092 break;
4093 case lang_wild_statement_enum:
4094 print_wild_statement (&s->wild_statement, os);
4095 break;
4096 case lang_address_statement_enum:
4097 print_address_statement (&s->address_statement);
4098 break;
4099 case lang_object_symbols_statement_enum:
4100 minfo (" CREATE_OBJECT_SYMBOLS\n");
4101 break;
4102 case lang_fill_statement_enum:
4103 print_fill_statement (&s->fill_statement);
4104 break;
4105 case lang_data_statement_enum:
4106 print_data_statement (&s->data_statement);
4107 break;
4108 case lang_reloc_statement_enum:
4109 print_reloc_statement (&s->reloc_statement);
4110 break;
4111 case lang_input_section_enum:
4112 print_input_section (s->input_section.section);
4113 break;
4114 case lang_padding_statement_enum:
4115 print_padding_statement (&s->padding_statement);
4116 break;
4117 case lang_output_section_statement_enum:
4118 print_output_section_statement (&s->output_section_statement);
4119 break;
4120 case lang_assignment_statement_enum:
4121 print_assignment (&s->assignment_statement, os);
4122 break;
4123 case lang_target_statement_enum:
4124 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4125 break;
4126 case lang_output_statement_enum:
4127 minfo ("OUTPUT(%s", s->output_statement.name);
4128 if (output_target != NULL)
4129 minfo (" %s", output_target);
4130 minfo (")\n");
4131 break;
4132 case lang_input_statement_enum:
4133 print_input_statement (&s->input_statement);
4134 break;
4135 case lang_group_statement_enum:
4136 print_group (&s->group_statement, os);
4137 break;
4138 case lang_insert_statement_enum:
4139 minfo ("INSERT %s %s\n",
4140 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4141 s->insert_statement.where);
4142 break;
4146 static void
4147 print_statements (void)
4149 print_statement_list (statement_list.head, abs_output_section);
4152 /* Print the first N statements in statement list S to STDERR.
4153 If N == 0, nothing is printed.
4154 If N < 0, the entire list is printed.
4155 Intended to be called from GDB. */
4157 void
4158 dprint_statement (lang_statement_union_type *s, int n)
4160 FILE *map_save = config.map_file;
4162 config.map_file = stderr;
4164 if (n < 0)
4165 print_statement_list (s, abs_output_section);
4166 else
4168 while (s && --n >= 0)
4170 print_statement (s, abs_output_section);
4171 s = s->header.next;
4175 config.map_file = map_save;
4178 static void
4179 insert_pad (lang_statement_union_type **ptr,
4180 fill_type *fill,
4181 unsigned int alignment_needed,
4182 asection *output_section,
4183 bfd_vma dot)
4185 static fill_type zero_fill = { 1, { 0 } };
4186 lang_statement_union_type *pad = NULL;
4188 if (ptr != &statement_list.head)
4189 pad = ((lang_statement_union_type *)
4190 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4191 if (pad != NULL
4192 && pad->header.type == lang_padding_statement_enum
4193 && pad->padding_statement.output_section == output_section)
4195 /* Use the existing pad statement. */
4197 else if ((pad = *ptr) != NULL
4198 && pad->header.type == lang_padding_statement_enum
4199 && pad->padding_statement.output_section == output_section)
4201 /* Use the existing pad statement. */
4203 else
4205 /* Make a new padding statement, linked into existing chain. */
4206 pad = stat_alloc (sizeof (lang_padding_statement_type));
4207 pad->header.next = *ptr;
4208 *ptr = pad;
4209 pad->header.type = lang_padding_statement_enum;
4210 pad->padding_statement.output_section = output_section;
4211 if (fill == NULL)
4212 fill = &zero_fill;
4213 pad->padding_statement.fill = fill;
4215 pad->padding_statement.output_offset = dot - output_section->vma;
4216 pad->padding_statement.size = alignment_needed;
4217 output_section->size += alignment_needed;
4220 /* Work out how much this section will move the dot point. */
4222 static bfd_vma
4223 size_input_section
4224 (lang_statement_union_type **this_ptr,
4225 lang_output_section_statement_type *output_section_statement,
4226 fill_type *fill,
4227 bfd_vma dot)
4229 lang_input_section_type *is = &((*this_ptr)->input_section);
4230 asection *i = is->section;
4232 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4233 && (i->flags & SEC_EXCLUDE) == 0)
4235 unsigned int alignment_needed;
4236 asection *o;
4238 /* Align this section first to the input sections requirement,
4239 then to the output section's requirement. If this alignment
4240 is greater than any seen before, then record it too. Perform
4241 the alignment by inserting a magic 'padding' statement. */
4243 if (output_section_statement->subsection_alignment != -1)
4244 i->alignment_power = output_section_statement->subsection_alignment;
4246 o = output_section_statement->bfd_section;
4247 if (o->alignment_power < i->alignment_power)
4248 o->alignment_power = i->alignment_power;
4250 alignment_needed = align_power (dot, i->alignment_power) - dot;
4252 if (alignment_needed != 0)
4254 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4255 dot += alignment_needed;
4258 /* Remember where in the output section this input section goes. */
4260 i->output_offset = dot - o->vma;
4262 /* Mark how big the output section must be to contain this now. */
4263 dot += TO_ADDR (i->size);
4264 o->size = TO_SIZE (dot - o->vma);
4266 else
4268 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4271 return dot;
4274 static int
4275 sort_sections_by_lma (const void *arg1, const void *arg2)
4277 const asection *sec1 = *(const asection **) arg1;
4278 const asection *sec2 = *(const asection **) arg2;
4280 if (bfd_section_lma (sec1->owner, sec1)
4281 < bfd_section_lma (sec2->owner, sec2))
4282 return -1;
4283 else if (bfd_section_lma (sec1->owner, sec1)
4284 > bfd_section_lma (sec2->owner, sec2))
4285 return 1;
4286 else if (sec1->id < sec2->id)
4287 return -1;
4288 else if (sec1->id > sec2->id)
4289 return 1;
4291 return 0;
4294 #define IGNORE_SECTION(s) \
4295 ((s->flags & SEC_NEVER_LOAD) != 0 \
4296 || (s->flags & SEC_ALLOC) == 0 \
4297 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4298 && (s->flags & SEC_LOAD) == 0))
4300 /* Check to see if any allocated sections overlap with other allocated
4301 sections. This can happen if a linker script specifies the output
4302 section addresses of the two sections. Also check whether any memory
4303 region has overflowed. */
4305 static void
4306 lang_check_section_addresses (void)
4308 asection *s, *os;
4309 asection **sections, **spp;
4310 unsigned int count;
4311 bfd_vma s_start;
4312 bfd_vma s_end;
4313 bfd_vma os_start;
4314 bfd_vma os_end;
4315 bfd_size_type amt;
4316 lang_memory_region_type *m;
4318 if (bfd_count_sections (link_info.output_bfd) <= 1)
4319 return;
4321 amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
4322 sections = xmalloc (amt);
4324 /* Scan all sections in the output list. */
4325 count = 0;
4326 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
4328 /* Only consider loadable sections with real contents. */
4329 if (IGNORE_SECTION (s) || s->size == 0)
4330 continue;
4332 sections[count] = s;
4333 count++;
4336 if (count <= 1)
4337 return;
4339 qsort (sections, (size_t) count, sizeof (asection *),
4340 sort_sections_by_lma);
4342 spp = sections;
4343 s = *spp++;
4344 s_start = bfd_section_lma (link_info.output_bfd, s);
4345 s_end = s_start + TO_ADDR (s->size) - 1;
4346 for (count--; count; count--)
4348 /* We must check the sections' LMA addresses not their VMA
4349 addresses because overlay sections can have overlapping VMAs
4350 but they must have distinct LMAs. */
4351 os = s;
4352 os_start = s_start;
4353 os_end = s_end;
4354 s = *spp++;
4355 s_start = bfd_section_lma (link_info.output_bfd, s);
4356 s_end = s_start + TO_ADDR (s->size) - 1;
4358 /* Look for an overlap. */
4359 if (s_end >= os_start && s_start <= os_end)
4360 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4361 s->name, s_start, s_end, os->name, os_start, os_end);
4364 free (sections);
4366 /* If any memory region has overflowed, report by how much.
4367 We do not issue this diagnostic for regions that had sections
4368 explicitly placed outside their bounds; os_region_check's
4369 diagnostics are adequate for that case.
4371 FIXME: It is conceivable that m->current - (m->origin + m->length)
4372 might overflow a 32-bit integer. There is, alas, no way to print
4373 a bfd_vma quantity in decimal. */
4374 for (m = lang_memory_region_list; m; m = m->next)
4375 if (m->had_full_message)
4376 einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
4377 m->name, (long)(m->current - (m->origin + m->length)));
4381 /* Make sure the new address is within the region. We explicitly permit the
4382 current address to be at the exact end of the region when the address is
4383 non-zero, in case the region is at the end of addressable memory and the
4384 calculation wraps around. */
4386 static void
4387 os_region_check (lang_output_section_statement_type *os,
4388 lang_memory_region_type *region,
4389 etree_type *tree,
4390 bfd_vma base)
4392 if ((region->current < region->origin
4393 || (region->current - region->origin > region->length))
4394 && ((region->current != region->origin + region->length)
4395 || base == 0))
4397 if (tree != NULL)
4399 einfo (_("%X%P: address 0x%v of %B section %s"
4400 " is not within region %s\n"),
4401 region->current,
4402 os->bfd_section->owner,
4403 os->bfd_section->name,
4404 region->name);
4406 else if (!region->had_full_message)
4408 region->had_full_message = TRUE;
4410 einfo (_("%X%P: %B section %s will not fit in region %s\n"),
4411 os->bfd_section->owner,
4412 os->bfd_section->name,
4413 region->name);
4418 /* Set the sizes for all the output sections. */
4420 static bfd_vma
4421 lang_size_sections_1
4422 (lang_statement_union_type *s,
4423 lang_output_section_statement_type *output_section_statement,
4424 lang_statement_union_type **prev,
4425 fill_type *fill,
4426 bfd_vma dot,
4427 bfd_boolean *relax,
4428 bfd_boolean check_regions)
4430 /* Size up the sections from their constituent parts. */
4431 for (; s != NULL; s = s->header.next)
4433 switch (s->header.type)
4435 case lang_output_section_statement_enum:
4437 bfd_vma newdot, after;
4438 lang_output_section_statement_type *os;
4439 lang_memory_region_type *r;
4441 os = &s->output_section_statement;
4442 if (os->addr_tree != NULL)
4444 os->processed_vma = FALSE;
4445 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4447 if (expld.result.valid_p)
4448 dot = expld.result.value + expld.result.section->vma;
4449 else if (expld.phase != lang_mark_phase_enum)
4450 einfo (_("%F%S: non constant or forward reference"
4451 " address expression for section %s\n"),
4452 os->name);
4455 if (os->bfd_section == NULL)
4456 /* This section was removed or never actually created. */
4457 break;
4459 /* If this is a COFF shared library section, use the size and
4460 address from the input section. FIXME: This is COFF
4461 specific; it would be cleaner if there were some other way
4462 to do this, but nothing simple comes to mind. */
4463 if (((bfd_get_flavour (link_info.output_bfd)
4464 == bfd_target_ecoff_flavour)
4465 || (bfd_get_flavour (link_info.output_bfd)
4466 == bfd_target_coff_flavour))
4467 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4469 asection *input;
4471 if (os->children.head == NULL
4472 || os->children.head->header.next != NULL
4473 || (os->children.head->header.type
4474 != lang_input_section_enum))
4475 einfo (_("%P%X: Internal error on COFF shared library"
4476 " section %s\n"), os->name);
4478 input = os->children.head->input_section.section;
4479 bfd_set_section_vma (os->bfd_section->owner,
4480 os->bfd_section,
4481 bfd_section_vma (input->owner, input));
4482 os->bfd_section->size = input->size;
4483 break;
4486 newdot = dot;
4487 if (bfd_is_abs_section (os->bfd_section))
4489 /* No matter what happens, an abs section starts at zero. */
4490 ASSERT (os->bfd_section->vma == 0);
4492 else
4494 int align;
4496 if (os->addr_tree == NULL)
4498 /* No address specified for this section, get one
4499 from the region specification. */
4500 if (os->region == NULL
4501 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4502 && os->region->name[0] == '*'
4503 && strcmp (os->region->name,
4504 DEFAULT_MEMORY_REGION) == 0))
4506 os->region = lang_memory_default (os->bfd_section);
4509 /* If a loadable section is using the default memory
4510 region, and some non default memory regions were
4511 defined, issue an error message. */
4512 if (!os->ignored
4513 && !IGNORE_SECTION (os->bfd_section)
4514 && ! link_info.relocatable
4515 && check_regions
4516 && strcmp (os->region->name,
4517 DEFAULT_MEMORY_REGION) == 0
4518 && lang_memory_region_list != NULL
4519 && (strcmp (lang_memory_region_list->name,
4520 DEFAULT_MEMORY_REGION) != 0
4521 || lang_memory_region_list->next != NULL)
4522 && expld.phase != lang_mark_phase_enum)
4524 /* By default this is an error rather than just a
4525 warning because if we allocate the section to the
4526 default memory region we can end up creating an
4527 excessively large binary, or even seg faulting when
4528 attempting to perform a negative seek. See
4529 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4530 for an example of this. This behaviour can be
4531 overridden by the using the --no-check-sections
4532 switch. */
4533 if (command_line.check_section_addresses)
4534 einfo (_("%P%F: error: no memory region specified"
4535 " for loadable section `%s'\n"),
4536 bfd_get_section_name (link_info.output_bfd,
4537 os->bfd_section));
4538 else
4539 einfo (_("%P: warning: no memory region specified"
4540 " for loadable section `%s'\n"),
4541 bfd_get_section_name (link_info.output_bfd,
4542 os->bfd_section));
4545 newdot = os->region->current;
4546 align = os->bfd_section->alignment_power;
4548 else
4549 align = os->section_alignment;
4551 /* Align to what the section needs. */
4552 if (align > 0)
4554 bfd_vma savedot = newdot;
4555 newdot = align_power (newdot, align);
4557 if (newdot != savedot
4558 && (config.warn_section_align
4559 || os->addr_tree != NULL)
4560 && expld.phase != lang_mark_phase_enum)
4561 einfo (_("%P: warning: changing start of section"
4562 " %s by %lu bytes\n"),
4563 os->name, (unsigned long) (newdot - savedot));
4566 bfd_set_section_vma (0, os->bfd_section, newdot);
4568 os->bfd_section->output_offset = 0;
4571 lang_size_sections_1 (os->children.head, os, &os->children.head,
4572 os->fill, newdot, relax, check_regions);
4574 os->processed_vma = TRUE;
4576 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4577 /* Except for some special linker created sections,
4578 no output section should change from zero size
4579 after strip_excluded_output_sections. A non-zero
4580 size on an ignored section indicates that some
4581 input section was not sized early enough. */
4582 ASSERT (os->bfd_section->size == 0);
4583 else
4585 dot = os->bfd_section->vma;
4587 /* Put the section within the requested block size, or
4588 align at the block boundary. */
4589 after = ((dot
4590 + TO_ADDR (os->bfd_section->size)
4591 + os->block_value - 1)
4592 & - (bfd_vma) os->block_value);
4594 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4597 /* Set section lma. */
4598 r = os->region;
4599 if (r == NULL)
4600 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4602 if (os->load_base)
4604 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4605 os->bfd_section->lma = lma;
4607 else if (os->lma_region != NULL)
4609 bfd_vma lma = os->lma_region->current;
4611 if (os->section_alignment != -1)
4612 lma = align_power (lma, os->section_alignment);
4613 os->bfd_section->lma = lma;
4615 else if (r->last_os != NULL
4616 && (os->bfd_section->flags & SEC_ALLOC) != 0)
4618 bfd_vma lma;
4619 asection *last;
4621 last = r->last_os->output_section_statement.bfd_section;
4623 /* A backwards move of dot should be accompanied by
4624 an explicit assignment to the section LMA (ie.
4625 os->load_base set) because backwards moves can
4626 create overlapping LMAs. */
4627 if (dot < last->vma
4628 && os->bfd_section->size != 0
4629 && dot + os->bfd_section->size <= last->vma)
4631 /* If dot moved backwards then leave lma equal to
4632 vma. This is the old default lma, which might
4633 just happen to work when the backwards move is
4634 sufficiently large. Nag if this changes anything,
4635 so people can fix their linker scripts. */
4637 if (last->vma != last->lma)
4638 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4639 os->name);
4641 else
4643 /* If this is an overlay, set the current lma to that
4644 at the end of the previous section. */
4645 if (os->sectype == overlay_section)
4646 lma = last->lma + last->size;
4648 /* Otherwise, keep the same lma to vma relationship
4649 as the previous section. */
4650 else
4651 lma = dot + last->lma - last->vma;
4653 if (os->section_alignment != -1)
4654 lma = align_power (lma, os->section_alignment);
4655 os->bfd_section->lma = lma;
4658 os->processed_lma = TRUE;
4660 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4661 break;
4663 /* Keep track of normal sections using the default
4664 lma region. We use this to set the lma for
4665 following sections. Overlays or other linker
4666 script assignment to lma might mean that the
4667 default lma == vma is incorrect.
4668 To avoid warnings about dot moving backwards when using
4669 -Ttext, don't start tracking sections until we find one
4670 of non-zero size or with lma set differently to vma. */
4671 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4672 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
4673 && (os->bfd_section->flags & SEC_ALLOC) != 0
4674 && (os->bfd_section->size != 0
4675 || (r->last_os == NULL
4676 && os->bfd_section->vma != os->bfd_section->lma)
4677 || (r->last_os != NULL
4678 && dot >= (r->last_os->output_section_statement
4679 .bfd_section->vma)))
4680 && os->lma_region == NULL
4681 && !link_info.relocatable)
4682 r->last_os = s;
4684 /* .tbss sections effectively have zero size. */
4685 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4686 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4687 || link_info.relocatable)
4688 dot += TO_ADDR (os->bfd_section->size);
4690 if (os->update_dot_tree != 0)
4691 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4693 /* Update dot in the region ?
4694 We only do this if the section is going to be allocated,
4695 since unallocated sections do not contribute to the region's
4696 overall size in memory.
4698 If the SEC_NEVER_LOAD bit is not set, it will affect the
4699 addresses of sections after it. We have to update
4700 dot. */
4701 if (os->region != NULL
4702 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4703 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4705 os->region->current = dot;
4707 if (check_regions)
4708 /* Make sure the new address is within the region. */
4709 os_region_check (os, os->region, os->addr_tree,
4710 os->bfd_section->vma);
4712 if (os->lma_region != NULL && os->lma_region != os->region
4713 && (os->bfd_section->flags & SEC_LOAD))
4715 os->lma_region->current
4716 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
4718 if (check_regions)
4719 os_region_check (os, os->lma_region, NULL,
4720 os->bfd_section->lma);
4724 break;
4726 case lang_constructors_statement_enum:
4727 dot = lang_size_sections_1 (constructor_list.head,
4728 output_section_statement,
4729 &s->wild_statement.children.head,
4730 fill, dot, relax, check_regions);
4731 break;
4733 case lang_data_statement_enum:
4735 unsigned int size = 0;
4737 s->data_statement.output_offset =
4738 dot - output_section_statement->bfd_section->vma;
4739 s->data_statement.output_section =
4740 output_section_statement->bfd_section;
4742 /* We might refer to provided symbols in the expression, and
4743 need to mark them as needed. */
4744 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4746 switch (s->data_statement.type)
4748 default:
4749 abort ();
4750 case QUAD:
4751 case SQUAD:
4752 size = QUAD_SIZE;
4753 break;
4754 case LONG:
4755 size = LONG_SIZE;
4756 break;
4757 case SHORT:
4758 size = SHORT_SIZE;
4759 break;
4760 case BYTE:
4761 size = BYTE_SIZE;
4762 break;
4764 if (size < TO_SIZE ((unsigned) 1))
4765 size = TO_SIZE ((unsigned) 1);
4766 dot += TO_ADDR (size);
4767 output_section_statement->bfd_section->size += size;
4769 break;
4771 case lang_reloc_statement_enum:
4773 int size;
4775 s->reloc_statement.output_offset =
4776 dot - output_section_statement->bfd_section->vma;
4777 s->reloc_statement.output_section =
4778 output_section_statement->bfd_section;
4779 size = bfd_get_reloc_size (s->reloc_statement.howto);
4780 dot += TO_ADDR (size);
4781 output_section_statement->bfd_section->size += size;
4783 break;
4785 case lang_wild_statement_enum:
4786 dot = lang_size_sections_1 (s->wild_statement.children.head,
4787 output_section_statement,
4788 &s->wild_statement.children.head,
4789 fill, dot, relax, check_regions);
4790 break;
4792 case lang_object_symbols_statement_enum:
4793 link_info.create_object_symbols_section =
4794 output_section_statement->bfd_section;
4795 break;
4797 case lang_output_statement_enum:
4798 case lang_target_statement_enum:
4799 break;
4801 case lang_input_section_enum:
4803 asection *i;
4805 i = (*prev)->input_section.section;
4806 if (relax)
4808 bfd_boolean again;
4810 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4811 einfo (_("%P%F: can't relax section: %E\n"));
4812 if (again)
4813 *relax = TRUE;
4815 dot = size_input_section (prev, output_section_statement,
4816 output_section_statement->fill, dot);
4818 break;
4820 case lang_input_statement_enum:
4821 break;
4823 case lang_fill_statement_enum:
4824 s->fill_statement.output_section =
4825 output_section_statement->bfd_section;
4827 fill = s->fill_statement.fill;
4828 break;
4830 case lang_assignment_statement_enum:
4832 bfd_vma newdot = dot;
4833 etree_type *tree = s->assignment_statement.exp;
4835 expld.dataseg.relro = exp_dataseg_relro_none;
4837 exp_fold_tree (tree,
4838 output_section_statement->bfd_section,
4839 &newdot);
4841 if (expld.dataseg.relro == exp_dataseg_relro_start)
4843 if (!expld.dataseg.relro_start_stat)
4844 expld.dataseg.relro_start_stat = s;
4845 else
4847 ASSERT (expld.dataseg.relro_start_stat == s);
4850 else if (expld.dataseg.relro == exp_dataseg_relro_end)
4852 if (!expld.dataseg.relro_end_stat)
4853 expld.dataseg.relro_end_stat = s;
4854 else
4856 ASSERT (expld.dataseg.relro_end_stat == s);
4859 expld.dataseg.relro = exp_dataseg_relro_none;
4861 /* This symbol is relative to this section. */
4862 if ((tree->type.node_class == etree_provided
4863 || tree->type.node_class == etree_assign)
4864 && (tree->assign.dst [0] != '.'
4865 || tree->assign.dst [1] != '\0'))
4866 output_section_statement->section_relative_symbol = 1;
4868 if (!output_section_statement->ignored)
4870 if (output_section_statement == abs_output_section)
4872 /* If we don't have an output section, then just adjust
4873 the default memory address. */
4874 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
4875 FALSE)->current = newdot;
4877 else if (newdot != dot)
4879 /* Insert a pad after this statement. We can't
4880 put the pad before when relaxing, in case the
4881 assignment references dot. */
4882 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
4883 output_section_statement->bfd_section, dot);
4885 /* Don't neuter the pad below when relaxing. */
4886 s = s->header.next;
4888 /* If dot is advanced, this implies that the section
4889 should have space allocated to it, unless the
4890 user has explicitly stated that the section
4891 should never be loaded. */
4892 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
4893 output_section_statement->bfd_section->flags |= SEC_ALLOC;
4895 dot = newdot;
4898 break;
4900 case lang_padding_statement_enum:
4901 /* If this is the first time lang_size_sections is called,
4902 we won't have any padding statements. If this is the
4903 second or later passes when relaxing, we should allow
4904 padding to shrink. If padding is needed on this pass, it
4905 will be added back in. */
4906 s->padding_statement.size = 0;
4908 /* Make sure output_offset is valid. If relaxation shrinks
4909 the section and this pad isn't needed, it's possible to
4910 have output_offset larger than the final size of the
4911 section. bfd_set_section_contents will complain even for
4912 a pad size of zero. */
4913 s->padding_statement.output_offset
4914 = dot - output_section_statement->bfd_section->vma;
4915 break;
4917 case lang_group_statement_enum:
4918 dot = lang_size_sections_1 (s->group_statement.children.head,
4919 output_section_statement,
4920 &s->group_statement.children.head,
4921 fill, dot, relax, check_regions);
4922 break;
4924 case lang_insert_statement_enum:
4925 break;
4927 /* We can only get here when relaxing is turned on. */
4928 case lang_address_statement_enum:
4929 break;
4931 default:
4932 FAIL ();
4933 break;
4935 prev = &s->header.next;
4937 return dot;
4940 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
4941 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
4942 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
4943 segments. We are allowed an opportunity to override this decision. */
4945 bfd_boolean
4946 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
4947 bfd * abfd ATTRIBUTE_UNUSED,
4948 asection * current_section,
4949 asection * previous_section,
4950 bfd_boolean new_segment)
4952 lang_output_section_statement_type * cur;
4953 lang_output_section_statement_type * prev;
4955 /* The checks below are only necessary when the BFD library has decided
4956 that the two sections ought to be placed into the same segment. */
4957 if (new_segment)
4958 return TRUE;
4960 /* Paranoia checks. */
4961 if (current_section == NULL || previous_section == NULL)
4962 return new_segment;
4964 /* Find the memory regions associated with the two sections.
4965 We call lang_output_section_find() here rather than scanning the list
4966 of output sections looking for a matching section pointer because if
4967 we have a large number of sections then a hash lookup is faster. */
4968 cur = lang_output_section_find (current_section->name);
4969 prev = lang_output_section_find (previous_section->name);
4971 /* More paranoia. */
4972 if (cur == NULL || prev == NULL)
4973 return new_segment;
4975 /* If the regions are different then force the sections to live in
4976 different segments. See the email thread starting at the following
4977 URL for the reasons why this is necessary:
4978 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
4979 return cur->region != prev->region;
4982 void
4983 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
4985 lang_statement_iteration++;
4986 lang_size_sections_1 (statement_list.head, abs_output_section,
4987 &statement_list.head, 0, 0, relax, check_regions);
4990 void
4991 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
4993 expld.phase = lang_allocating_phase_enum;
4994 expld.dataseg.phase = exp_dataseg_none;
4996 one_lang_size_sections_pass (relax, check_regions);
4997 if (expld.dataseg.phase == exp_dataseg_end_seen
4998 && link_info.relro && expld.dataseg.relro_end)
5000 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5001 to put expld.dataseg.relro on a (common) page boundary. */
5002 bfd_vma old_min_base, relro_end, maxpage;
5004 expld.dataseg.phase = exp_dataseg_relro_adjust;
5005 old_min_base = expld.dataseg.min_base;
5006 maxpage = expld.dataseg.maxpagesize;
5007 expld.dataseg.base += (-expld.dataseg.relro_end
5008 & (expld.dataseg.pagesize - 1));
5009 /* Compute the expected PT_GNU_RELRO segment end. */
5010 relro_end = (expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
5011 & ~(expld.dataseg.pagesize - 1);
5012 if (old_min_base + maxpage < expld.dataseg.base)
5014 expld.dataseg.base -= maxpage;
5015 relro_end -= maxpage;
5017 lang_reset_memory_regions ();
5018 one_lang_size_sections_pass (relax, check_regions);
5019 if (expld.dataseg.relro_end > relro_end)
5021 /* The alignment of sections between DATA_SEGMENT_ALIGN
5022 and DATA_SEGMENT_RELRO_END caused huge padding to be
5023 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
5024 asection *sec;
5025 unsigned int max_alignment_power = 0;
5027 /* Find maximum alignment power of sections between
5028 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5029 for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
5030 if (sec->vma >= expld.dataseg.base
5031 && sec->vma < expld.dataseg.relro_end
5032 && sec->alignment_power > max_alignment_power)
5033 max_alignment_power = sec->alignment_power;
5035 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
5037 if (expld.dataseg.base - (1 << max_alignment_power)
5038 < old_min_base)
5039 expld.dataseg.base += expld.dataseg.pagesize;
5040 expld.dataseg.base -= (1 << max_alignment_power);
5041 lang_reset_memory_regions ();
5042 one_lang_size_sections_pass (relax, check_regions);
5045 link_info.relro_start = expld.dataseg.base;
5046 link_info.relro_end = expld.dataseg.relro_end;
5048 else if (expld.dataseg.phase == exp_dataseg_end_seen)
5050 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5051 a page could be saved in the data segment. */
5052 bfd_vma first, last;
5054 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5055 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
5056 if (first && last
5057 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5058 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5059 && first + last <= expld.dataseg.pagesize)
5061 expld.dataseg.phase = exp_dataseg_adjust;
5062 lang_reset_memory_regions ();
5063 one_lang_size_sections_pass (relax, check_regions);
5067 expld.phase = lang_final_phase_enum;
5070 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5072 static bfd_vma
5073 lang_do_assignments_1 (lang_statement_union_type *s,
5074 lang_output_section_statement_type *current_os,
5075 fill_type *fill,
5076 bfd_vma dot)
5078 for (; s != NULL; s = s->header.next)
5080 switch (s->header.type)
5082 case lang_constructors_statement_enum:
5083 dot = lang_do_assignments_1 (constructor_list.head,
5084 current_os, fill, dot);
5085 break;
5087 case lang_output_section_statement_enum:
5089 lang_output_section_statement_type *os;
5091 os = &(s->output_section_statement);
5092 if (os->bfd_section != NULL && !os->ignored)
5094 dot = os->bfd_section->vma;
5096 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
5098 /* .tbss sections effectively have zero size. */
5099 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5100 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5101 || link_info.relocatable)
5102 dot += TO_ADDR (os->bfd_section->size);
5105 break;
5107 case lang_wild_statement_enum:
5109 dot = lang_do_assignments_1 (s->wild_statement.children.head,
5110 current_os, fill, dot);
5111 break;
5113 case lang_object_symbols_statement_enum:
5114 case lang_output_statement_enum:
5115 case lang_target_statement_enum:
5116 break;
5118 case lang_data_statement_enum:
5119 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5120 if (expld.result.valid_p)
5121 s->data_statement.value = (expld.result.value
5122 + expld.result.section->vma);
5123 else
5124 einfo (_("%F%P: invalid data statement\n"));
5126 unsigned int size;
5127 switch (s->data_statement.type)
5129 default:
5130 abort ();
5131 case QUAD:
5132 case SQUAD:
5133 size = QUAD_SIZE;
5134 break;
5135 case LONG:
5136 size = LONG_SIZE;
5137 break;
5138 case SHORT:
5139 size = SHORT_SIZE;
5140 break;
5141 case BYTE:
5142 size = BYTE_SIZE;
5143 break;
5145 if (size < TO_SIZE ((unsigned) 1))
5146 size = TO_SIZE ((unsigned) 1);
5147 dot += TO_ADDR (size);
5149 break;
5151 case lang_reloc_statement_enum:
5152 exp_fold_tree (s->reloc_statement.addend_exp,
5153 bfd_abs_section_ptr, &dot);
5154 if (expld.result.valid_p)
5155 s->reloc_statement.addend_value = expld.result.value;
5156 else
5157 einfo (_("%F%P: invalid reloc statement\n"));
5158 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
5159 break;
5161 case lang_input_section_enum:
5163 asection *in = s->input_section.section;
5165 if ((in->flags & SEC_EXCLUDE) == 0)
5166 dot += TO_ADDR (in->size);
5168 break;
5170 case lang_input_statement_enum:
5171 break;
5173 case lang_fill_statement_enum:
5174 fill = s->fill_statement.fill;
5175 break;
5177 case lang_assignment_statement_enum:
5178 exp_fold_tree (s->assignment_statement.exp,
5179 current_os->bfd_section,
5180 &dot);
5181 break;
5183 case lang_padding_statement_enum:
5184 dot += TO_ADDR (s->padding_statement.size);
5185 break;
5187 case lang_group_statement_enum:
5188 dot = lang_do_assignments_1 (s->group_statement.children.head,
5189 current_os, fill, dot);
5190 break;
5192 case lang_insert_statement_enum:
5193 break;
5195 case lang_address_statement_enum:
5196 break;
5198 default:
5199 FAIL ();
5200 break;
5203 return dot;
5206 void
5207 lang_do_assignments (void)
5209 lang_statement_iteration++;
5210 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
5213 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5214 operator .startof. (section_name), it produces an undefined symbol
5215 .startof.section_name. Similarly, when it sees
5216 .sizeof. (section_name), it produces an undefined symbol
5217 .sizeof.section_name. For all the output sections, we look for
5218 such symbols, and set them to the correct value. */
5220 static void
5221 lang_set_startof (void)
5223 asection *s;
5225 if (link_info.relocatable)
5226 return;
5228 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5230 const char *secname;
5231 char *buf;
5232 struct bfd_link_hash_entry *h;
5234 secname = bfd_get_section_name (link_info.output_bfd, s);
5235 buf = xmalloc (10 + strlen (secname));
5237 sprintf (buf, ".startof.%s", secname);
5238 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5239 if (h != NULL && h->type == bfd_link_hash_undefined)
5241 h->type = bfd_link_hash_defined;
5242 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s);
5243 h->u.def.section = bfd_abs_section_ptr;
5246 sprintf (buf, ".sizeof.%s", secname);
5247 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5248 if (h != NULL && h->type == bfd_link_hash_undefined)
5250 h->type = bfd_link_hash_defined;
5251 h->u.def.value = TO_ADDR (s->size);
5252 h->u.def.section = bfd_abs_section_ptr;
5255 free (buf);
5259 static void
5260 lang_end (void)
5262 struct bfd_link_hash_entry *h;
5263 bfd_boolean warn;
5265 if ((link_info.relocatable && !link_info.gc_sections)
5266 || link_info.shared)
5267 warn = entry_from_cmdline;
5268 else
5269 warn = TRUE;
5271 /* Force the user to specify a root when generating a relocatable with
5272 --gc-sections. */
5273 if (link_info.gc_sections && link_info.relocatable
5274 && (entry_symbol.name == NULL
5275 && ldlang_undef_chain_list_head == NULL))
5276 einfo (_("%P%F: gc-sections requires either an entry or "
5277 "an undefined symbol\n"));
5279 if (entry_symbol.name == NULL)
5281 /* No entry has been specified. Look for the default entry, but
5282 don't warn if we don't find it. */
5283 entry_symbol.name = entry_symbol_default;
5284 warn = FALSE;
5287 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
5288 FALSE, FALSE, TRUE);
5289 if (h != NULL
5290 && (h->type == bfd_link_hash_defined
5291 || h->type == bfd_link_hash_defweak)
5292 && h->u.def.section->output_section != NULL)
5294 bfd_vma val;
5296 val = (h->u.def.value
5297 + bfd_get_section_vma (link_info.output_bfd,
5298 h->u.def.section->output_section)
5299 + h->u.def.section->output_offset);
5300 if (! bfd_set_start_address (link_info.output_bfd, val))
5301 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
5303 else
5305 bfd_vma val;
5306 const char *send;
5308 /* We couldn't find the entry symbol. Try parsing it as a
5309 number. */
5310 val = bfd_scan_vma (entry_symbol.name, &send, 0);
5311 if (*send == '\0')
5313 if (! bfd_set_start_address (link_info.output_bfd, val))
5314 einfo (_("%P%F: can't set start address\n"));
5316 else
5318 asection *ts;
5320 /* Can't find the entry symbol, and it's not a number. Use
5321 the first address in the text section. */
5322 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
5323 if (ts != NULL)
5325 if (warn)
5326 einfo (_("%P: warning: cannot find entry symbol %s;"
5327 " defaulting to %V\n"),
5328 entry_symbol.name,
5329 bfd_get_section_vma (link_info.output_bfd, ts));
5330 if (!(bfd_set_start_address
5331 (link_info.output_bfd,
5332 bfd_get_section_vma (link_info.output_bfd, ts))))
5333 einfo (_("%P%F: can't set start address\n"));
5335 else
5337 if (warn)
5338 einfo (_("%P: warning: cannot find entry symbol %s;"
5339 " not setting start address\n"),
5340 entry_symbol.name);
5345 /* Don't bfd_hash_table_free (&lang_definedness_table);
5346 map file output may result in a call of lang_track_definedness. */
5349 /* This is a small function used when we want to ignore errors from
5350 BFD. */
5352 static void
5353 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5355 /* Don't do anything. */
5358 /* Check that the architecture of all the input files is compatible
5359 with the output file. Also call the backend to let it do any
5360 other checking that is needed. */
5362 static void
5363 lang_check (void)
5365 lang_statement_union_type *file;
5366 bfd *input_bfd;
5367 const bfd_arch_info_type *compatible;
5369 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5371 input_bfd = file->input_statement.the_bfd;
5372 compatible
5373 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
5374 command_line.accept_unknown_input_arch);
5376 /* In general it is not possible to perform a relocatable
5377 link between differing object formats when the input
5378 file has relocations, because the relocations in the
5379 input format may not have equivalent representations in
5380 the output format (and besides BFD does not translate
5381 relocs for other link purposes than a final link). */
5382 if ((link_info.relocatable || link_info.emitrelocations)
5383 && (compatible == NULL
5384 || (bfd_get_flavour (input_bfd)
5385 != bfd_get_flavour (link_info.output_bfd)))
5386 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5388 einfo (_("%P%F: Relocatable linking with relocations from"
5389 " format %s (%B) to format %s (%B) is not supported\n"),
5390 bfd_get_target (input_bfd), input_bfd,
5391 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
5392 /* einfo with %F exits. */
5395 if (compatible == NULL)
5397 if (command_line.warn_mismatch)
5398 einfo (_("%P%X: %s architecture of input file `%B'"
5399 " is incompatible with %s output\n"),
5400 bfd_printable_name (input_bfd), input_bfd,
5401 bfd_printable_name (link_info.output_bfd));
5403 else if (bfd_count_sections (input_bfd))
5405 /* If the input bfd has no contents, it shouldn't set the
5406 private data of the output bfd. */
5408 bfd_error_handler_type pfn = NULL;
5410 /* If we aren't supposed to warn about mismatched input
5411 files, temporarily set the BFD error handler to a
5412 function which will do nothing. We still want to call
5413 bfd_merge_private_bfd_data, since it may set up
5414 information which is needed in the output file. */
5415 if (! command_line.warn_mismatch)
5416 pfn = bfd_set_error_handler (ignore_bfd_errors);
5417 if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
5419 if (command_line.warn_mismatch)
5420 einfo (_("%P%X: failed to merge target specific data"
5421 " of file %B\n"), input_bfd);
5423 if (! command_line.warn_mismatch)
5424 bfd_set_error_handler (pfn);
5429 /* Look through all the global common symbols and attach them to the
5430 correct section. The -sort-common command line switch may be used
5431 to roughly sort the entries by alignment. */
5433 static void
5434 lang_common (void)
5436 if (command_line.inhibit_common_definition)
5437 return;
5438 if (link_info.relocatable
5439 && ! command_line.force_common_definition)
5440 return;
5442 if (! config.sort_common)
5443 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5444 else
5446 unsigned int power;
5448 if (config.sort_common == sort_descending)
5450 for (power = 4; power > 0; power--)
5451 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5453 power = 0;
5454 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5456 else
5458 for (power = 0; power <= 4; power++)
5459 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5461 power = UINT_MAX;
5462 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5467 /* Place one common symbol in the correct section. */
5469 static bfd_boolean
5470 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5472 unsigned int power_of_two;
5473 bfd_vma size;
5474 asection *section;
5476 if (h->type != bfd_link_hash_common)
5477 return TRUE;
5479 size = h->u.c.size;
5480 power_of_two = h->u.c.p->alignment_power;
5482 if (config.sort_common == sort_descending
5483 && power_of_two < *(unsigned int *) info)
5484 return TRUE;
5485 else if (config.sort_common == sort_ascending
5486 && power_of_two > *(unsigned int *) info)
5487 return TRUE;
5489 section = h->u.c.p->section;
5491 /* Increase the size of the section to align the common sym. */
5492 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
5493 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
5495 /* Adjust the alignment if necessary. */
5496 if (power_of_two > section->alignment_power)
5497 section->alignment_power = power_of_two;
5499 /* Change the symbol from common to defined. */
5500 h->type = bfd_link_hash_defined;
5501 h->u.def.section = section;
5502 h->u.def.value = section->size;
5504 /* Increase the size of the section. */
5505 section->size += size;
5507 /* Make sure the section is allocated in memory, and make sure that
5508 it is no longer a common section. */
5509 section->flags |= SEC_ALLOC;
5510 section->flags &= ~SEC_IS_COMMON;
5512 if (config.map_file != NULL)
5514 static bfd_boolean header_printed;
5515 int len;
5516 char *name;
5517 char buf[50];
5519 if (! header_printed)
5521 minfo (_("\nAllocating common symbols\n"));
5522 minfo (_("Common symbol size file\n\n"));
5523 header_printed = TRUE;
5526 name = bfd_demangle (link_info.output_bfd, h->root.string,
5527 DMGL_ANSI | DMGL_PARAMS);
5528 if (name == NULL)
5530 minfo ("%s", h->root.string);
5531 len = strlen (h->root.string);
5533 else
5535 minfo ("%s", name);
5536 len = strlen (name);
5537 free (name);
5540 if (len >= 19)
5542 print_nl ();
5543 len = 0;
5545 while (len < 20)
5547 print_space ();
5548 ++len;
5551 minfo ("0x");
5552 if (size <= 0xffffffff)
5553 sprintf (buf, "%lx", (unsigned long) size);
5554 else
5555 sprintf_vma (buf, size);
5556 minfo ("%s", buf);
5557 len = strlen (buf);
5559 while (len < 16)
5561 print_space ();
5562 ++len;
5565 minfo ("%B\n", section->owner);
5568 return TRUE;
5571 /* Run through the input files and ensure that every input section has
5572 somewhere to go. If one is found without a destination then create
5573 an input request and place it into the statement tree. */
5575 static void
5576 lang_place_orphans (void)
5578 LANG_FOR_EACH_INPUT_STATEMENT (file)
5580 asection *s;
5582 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5584 if (s->output_section == NULL)
5586 /* This section of the file is not attached, root
5587 around for a sensible place for it to go. */
5589 if (file->just_syms_flag)
5590 bfd_link_just_syms (file->the_bfd, s, &link_info);
5591 else if ((s->flags & SEC_EXCLUDE) != 0)
5592 s->output_section = bfd_abs_section_ptr;
5593 else if (strcmp (s->name, "COMMON") == 0)
5595 /* This is a lonely common section which must have
5596 come from an archive. We attach to the section
5597 with the wildcard. */
5598 if (! link_info.relocatable
5599 || command_line.force_common_definition)
5601 if (default_common_section == NULL)
5603 default_common_section =
5604 lang_output_section_statement_lookup (".bss");
5607 lang_add_section (&default_common_section->children, s,
5608 default_common_section);
5611 else if (ldemul_place_orphan (s))
5613 else
5615 lang_output_section_statement_type *os;
5617 os = lang_output_section_statement_lookup (s->name);
5618 lang_add_section (&os->children, s, os);
5625 void
5626 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5628 flagword *ptr_flags;
5630 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5631 while (*flags)
5633 switch (*flags)
5635 case 'A': case 'a':
5636 *ptr_flags |= SEC_ALLOC;
5637 break;
5639 case 'R': case 'r':
5640 *ptr_flags |= SEC_READONLY;
5641 break;
5643 case 'W': case 'w':
5644 *ptr_flags |= SEC_DATA;
5645 break;
5647 case 'X': case 'x':
5648 *ptr_flags |= SEC_CODE;
5649 break;
5651 case 'L': case 'l':
5652 case 'I': case 'i':
5653 *ptr_flags |= SEC_LOAD;
5654 break;
5656 default:
5657 einfo (_("%P%F: invalid syntax in flags\n"));
5658 break;
5660 flags++;
5664 /* Call a function on each input file. This function will be called
5665 on an archive, but not on the elements. */
5667 void
5668 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5670 lang_input_statement_type *f;
5672 for (f = (lang_input_statement_type *) input_file_chain.head;
5673 f != NULL;
5674 f = (lang_input_statement_type *) f->next_real_file)
5675 func (f);
5678 /* Call a function on each file. The function will be called on all
5679 the elements of an archive which are included in the link, but will
5680 not be called on the archive file itself. */
5682 void
5683 lang_for_each_file (void (*func) (lang_input_statement_type *))
5685 LANG_FOR_EACH_INPUT_STATEMENT (f)
5687 func (f);
5691 void
5692 ldlang_add_file (lang_input_statement_type *entry)
5694 lang_statement_append (&file_chain,
5695 (lang_statement_union_type *) entry,
5696 &entry->next);
5698 /* The BFD linker needs to have a list of all input BFDs involved in
5699 a link. */
5700 ASSERT (entry->the_bfd->link_next == NULL);
5701 ASSERT (entry->the_bfd != link_info.output_bfd);
5703 *link_info.input_bfds_tail = entry->the_bfd;
5704 link_info.input_bfds_tail = &entry->the_bfd->link_next;
5705 entry->the_bfd->usrdata = entry;
5706 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5708 /* Look through the sections and check for any which should not be
5709 included in the link. We need to do this now, so that we can
5710 notice when the backend linker tries to report multiple
5711 definition errors for symbols which are in sections we aren't
5712 going to link. FIXME: It might be better to entirely ignore
5713 symbols which are defined in sections which are going to be
5714 discarded. This would require modifying the backend linker for
5715 each backend which might set the SEC_LINK_ONCE flag. If we do
5716 this, we should probably handle SEC_EXCLUDE in the same way. */
5718 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5721 void
5722 lang_add_output (const char *name, int from_script)
5724 /* Make -o on command line override OUTPUT in script. */
5725 if (!had_output_filename || !from_script)
5727 output_filename = name;
5728 had_output_filename = TRUE;
5732 static lang_output_section_statement_type *current_section;
5734 static int
5735 topower (int x)
5737 unsigned int i = 1;
5738 int l;
5740 if (x < 0)
5741 return -1;
5743 for (l = 0; l < 32; l++)
5745 if (i >= (unsigned int) x)
5746 return l;
5747 i <<= 1;
5750 return 0;
5753 lang_output_section_statement_type *
5754 lang_enter_output_section_statement (const char *output_section_statement_name,
5755 etree_type *address_exp,
5756 enum section_type sectype,
5757 etree_type *align,
5758 etree_type *subalign,
5759 etree_type *ebase,
5760 int constraint)
5762 lang_output_section_statement_type *os;
5764 os = lang_output_section_statement_lookup_1 (output_section_statement_name,
5765 constraint);
5766 current_section = os;
5768 /* Make next things chain into subchain of this. */
5770 if (os->addr_tree == NULL)
5772 os->addr_tree = address_exp;
5774 os->sectype = sectype;
5775 if (sectype != noload_section)
5776 os->flags = SEC_NO_FLAGS;
5777 else
5778 os->flags = SEC_NEVER_LOAD;
5779 os->block_value = 1;
5780 stat_ptr = &os->children;
5782 os->subsection_alignment =
5783 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5784 os->section_alignment =
5785 topower (exp_get_value_int (align, -1, "section alignment"));
5787 os->load_base = ebase;
5788 return os;
5791 void
5792 lang_final (void)
5794 lang_output_statement_type *new;
5796 new = new_stat (lang_output_statement, stat_ptr);
5797 new->name = output_filename;
5800 /* Reset the current counters in the regions. */
5802 void
5803 lang_reset_memory_regions (void)
5805 lang_memory_region_type *p = lang_memory_region_list;
5806 asection *o;
5807 lang_output_section_statement_type *os;
5809 for (p = lang_memory_region_list; p != NULL; p = p->next)
5811 p->current = p->origin;
5812 p->last_os = NULL;
5815 for (os = &lang_output_section_statement.head->output_section_statement;
5816 os != NULL;
5817 os = os->next)
5819 os->processed_vma = FALSE;
5820 os->processed_lma = FALSE;
5823 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
5825 /* Save the last size for possible use by bfd_relax_section. */
5826 o->rawsize = o->size;
5827 o->size = 0;
5831 /* Worker for lang_gc_sections_1. */
5833 static void
5834 gc_section_callback (lang_wild_statement_type *ptr,
5835 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5836 asection *section,
5837 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5838 void *data ATTRIBUTE_UNUSED)
5840 /* If the wild pattern was marked KEEP, the member sections
5841 should be as well. */
5842 if (ptr->keep_sections)
5843 section->flags |= SEC_KEEP;
5846 /* Iterate over sections marking them against GC. */
5848 static void
5849 lang_gc_sections_1 (lang_statement_union_type *s)
5851 for (; s != NULL; s = s->header.next)
5853 switch (s->header.type)
5855 case lang_wild_statement_enum:
5856 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5857 break;
5858 case lang_constructors_statement_enum:
5859 lang_gc_sections_1 (constructor_list.head);
5860 break;
5861 case lang_output_section_statement_enum:
5862 lang_gc_sections_1 (s->output_section_statement.children.head);
5863 break;
5864 case lang_group_statement_enum:
5865 lang_gc_sections_1 (s->group_statement.children.head);
5866 break;
5867 default:
5868 break;
5873 static void
5874 lang_gc_sections (void)
5876 /* Keep all sections so marked in the link script. */
5878 lang_gc_sections_1 (statement_list.head);
5880 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5881 the special case of debug info. (See bfd/stabs.c)
5882 Twiddle the flag here, to simplify later linker code. */
5883 if (link_info.relocatable)
5885 LANG_FOR_EACH_INPUT_STATEMENT (f)
5887 asection *sec;
5888 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
5889 if ((sec->flags & SEC_DEBUGGING) == 0)
5890 sec->flags &= ~SEC_EXCLUDE;
5894 if (link_info.gc_sections)
5895 bfd_gc_sections (link_info.output_bfd, &link_info);
5898 /* Worker for lang_find_relro_sections_1. */
5900 static void
5901 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
5902 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5903 asection *section,
5904 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5905 void *data)
5907 /* Discarded, excluded and ignored sections effectively have zero
5908 size. */
5909 if (section->output_section != NULL
5910 && section->output_section->owner == link_info.output_bfd
5911 && (section->output_section->flags & SEC_EXCLUDE) == 0
5912 && !IGNORE_SECTION (section)
5913 && section->size != 0)
5915 bfd_boolean *has_relro_section = (bfd_boolean *) data;
5916 *has_relro_section = TRUE;
5920 /* Iterate over sections for relro sections. */
5922 static void
5923 lang_find_relro_sections_1 (lang_statement_union_type *s,
5924 bfd_boolean *has_relro_section)
5926 if (*has_relro_section)
5927 return;
5929 for (; s != NULL; s = s->header.next)
5931 if (s == expld.dataseg.relro_end_stat)
5932 break;
5934 switch (s->header.type)
5936 case lang_wild_statement_enum:
5937 walk_wild (&s->wild_statement,
5938 find_relro_section_callback,
5939 has_relro_section);
5940 break;
5941 case lang_constructors_statement_enum:
5942 lang_find_relro_sections_1 (constructor_list.head,
5943 has_relro_section);
5944 break;
5945 case lang_output_section_statement_enum:
5946 lang_find_relro_sections_1 (s->output_section_statement.children.head,
5947 has_relro_section);
5948 break;
5949 case lang_group_statement_enum:
5950 lang_find_relro_sections_1 (s->group_statement.children.head,
5951 has_relro_section);
5952 break;
5953 default:
5954 break;
5959 static void
5960 lang_find_relro_sections (void)
5962 bfd_boolean has_relro_section = FALSE;
5964 /* Check all sections in the link script. */
5966 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
5967 &has_relro_section);
5969 if (!has_relro_section)
5970 link_info.relro = FALSE;
5973 /* Relax all sections until bfd_relax_section gives up. */
5975 static void
5976 relax_sections (void)
5978 /* Keep relaxing until bfd_relax_section gives up. */
5979 bfd_boolean relax_again;
5981 link_info.relax_trip = -1;
5984 relax_again = FALSE;
5985 link_info.relax_trip++;
5987 /* Note: pe-dll.c does something like this also. If you find
5988 you need to change this code, you probably need to change
5989 pe-dll.c also. DJ */
5991 /* Do all the assignments with our current guesses as to
5992 section sizes. */
5993 lang_do_assignments ();
5995 /* We must do this after lang_do_assignments, because it uses
5996 size. */
5997 lang_reset_memory_regions ();
5999 /* Perform another relax pass - this time we know where the
6000 globals are, so can make a better guess. */
6001 lang_size_sections (&relax_again, FALSE);
6003 while (relax_again);
6006 void
6007 lang_process (void)
6009 /* Finalize dynamic list. */
6010 if (link_info.dynamic_list)
6011 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
6013 current_target = default_target;
6015 /* Open the output file. */
6016 lang_for_each_statement (ldlang_open_output);
6017 init_opb ();
6019 ldemul_create_output_section_statements ();
6021 /* Add to the hash table all undefineds on the command line. */
6022 lang_place_undefineds ();
6024 if (!bfd_section_already_linked_table_init ())
6025 einfo (_("%P%F: Failed to create hash table\n"));
6027 /* Create a bfd for each input file. */
6028 current_target = default_target;
6029 open_input_bfds (statement_list.head, FALSE);
6031 link_info.gc_sym_list = &entry_symbol;
6032 if (entry_symbol.name == NULL)
6033 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6035 ldemul_after_open ();
6037 bfd_section_already_linked_table_free ();
6039 /* Make sure that we're not mixing architectures. We call this
6040 after all the input files have been opened, but before we do any
6041 other processing, so that any operations merge_private_bfd_data
6042 does on the output file will be known during the rest of the
6043 link. */
6044 lang_check ();
6046 /* Handle .exports instead of a version script if we're told to do so. */
6047 if (command_line.version_exports_section)
6048 lang_do_version_exports_section ();
6050 /* Build all sets based on the information gathered from the input
6051 files. */
6052 ldctor_build_sets ();
6054 /* Remove unreferenced sections if asked to. */
6055 lang_gc_sections ();
6057 /* Size up the common data. */
6058 lang_common ();
6060 /* Update wild statements. */
6061 update_wild_statements (statement_list.head);
6063 /* Run through the contours of the script and attach input sections
6064 to the correct output sections. */
6065 map_input_to_output_sections (statement_list.head, NULL, NULL);
6067 process_insert_statements ();
6069 /* Find any sections not attached explicitly and handle them. */
6070 lang_place_orphans ();
6072 if (! link_info.relocatable)
6074 asection *found;
6076 /* Merge SEC_MERGE sections. This has to be done after GC of
6077 sections, so that GCed sections are not merged, but before
6078 assigning dynamic symbols, since removing whole input sections
6079 is hard then. */
6080 bfd_merge_sections (link_info.output_bfd, &link_info);
6082 /* Look for a text section and set the readonly attribute in it. */
6083 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
6085 if (found != NULL)
6087 if (config.text_read_only)
6088 found->flags |= SEC_READONLY;
6089 else
6090 found->flags &= ~SEC_READONLY;
6094 /* Do anything special before sizing sections. This is where ELF
6095 and other back-ends size dynamic sections. */
6096 ldemul_before_allocation ();
6098 /* We must record the program headers before we try to fix the
6099 section positions, since they will affect SIZEOF_HEADERS. */
6100 lang_record_phdrs ();
6102 /* Check relro sections. */
6103 if (link_info.relro && ! link_info.relocatable)
6104 lang_find_relro_sections ();
6106 /* Size up the sections. */
6107 lang_size_sections (NULL, !command_line.relax);
6109 /* Now run around and relax if we can. */
6110 if (command_line.relax)
6112 /* We may need more than one relaxation pass. */
6113 int i = link_info.relax_pass;
6115 /* The backend can use it to determine the current pass. */
6116 link_info.relax_pass = 0;
6118 while (i--)
6120 relax_sections ();
6121 link_info.relax_pass++;
6124 /* Final extra sizing to report errors. */
6125 lang_do_assignments ();
6126 lang_reset_memory_regions ();
6127 lang_size_sections (NULL, TRUE);
6130 /* See if anything special should be done now we know how big
6131 everything is. */
6132 ldemul_after_allocation ();
6134 /* Fix any .startof. or .sizeof. symbols. */
6135 lang_set_startof ();
6137 /* Do all the assignments, now that we know the final resting places
6138 of all the symbols. */
6140 lang_do_assignments ();
6142 ldemul_finish ();
6144 /* Make sure that the section addresses make sense. */
6145 if (! link_info.relocatable
6146 && command_line.check_section_addresses)
6147 lang_check_section_addresses ();
6149 lang_end ();
6152 /* EXPORTED TO YACC */
6154 void
6155 lang_add_wild (struct wildcard_spec *filespec,
6156 struct wildcard_list *section_list,
6157 bfd_boolean keep_sections)
6159 struct wildcard_list *curr, *next;
6160 lang_wild_statement_type *new;
6162 /* Reverse the list as the parser puts it back to front. */
6163 for (curr = section_list, section_list = NULL;
6164 curr != NULL;
6165 section_list = curr, curr = next)
6167 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
6168 placed_commons = TRUE;
6170 next = curr->next;
6171 curr->next = section_list;
6174 if (filespec != NULL && filespec->name != NULL)
6176 if (strcmp (filespec->name, "*") == 0)
6177 filespec->name = NULL;
6178 else if (! wildcardp (filespec->name))
6179 lang_has_input_file = TRUE;
6182 new = new_stat (lang_wild_statement, stat_ptr);
6183 new->filename = NULL;
6184 new->filenames_sorted = FALSE;
6185 if (filespec != NULL)
6187 new->filename = filespec->name;
6188 new->filenames_sorted = filespec->sorted == by_name;
6190 new->section_list = section_list;
6191 new->keep_sections = keep_sections;
6192 lang_list_init (&new->children);
6193 analyze_walk_wild_section_handler (new);
6196 void
6197 lang_section_start (const char *name, etree_type *address,
6198 const segment_type *segment)
6200 lang_address_statement_type *ad;
6202 ad = new_stat (lang_address_statement, stat_ptr);
6203 ad->section_name = name;
6204 ad->address = address;
6205 ad->segment = segment;
6208 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6209 because of a -e argument on the command line, or zero if this is
6210 called by ENTRY in a linker script. Command line arguments take
6211 precedence. */
6213 void
6214 lang_add_entry (const char *name, bfd_boolean cmdline)
6216 if (entry_symbol.name == NULL
6217 || cmdline
6218 || ! entry_from_cmdline)
6220 entry_symbol.name = name;
6221 entry_from_cmdline = cmdline;
6225 /* Set the default start symbol to NAME. .em files should use this,
6226 not lang_add_entry, to override the use of "start" if neither the
6227 linker script nor the command line specifies an entry point. NAME
6228 must be permanently allocated. */
6229 void
6230 lang_default_entry (const char *name)
6232 entry_symbol_default = name;
6235 void
6236 lang_add_target (const char *name)
6238 lang_target_statement_type *new;
6240 new = new_stat (lang_target_statement, stat_ptr);
6241 new->target = name;
6244 void
6245 lang_add_map (const char *name)
6247 while (*name)
6249 switch (*name)
6251 case 'F':
6252 map_option_f = TRUE;
6253 break;
6255 name++;
6259 void
6260 lang_add_fill (fill_type *fill)
6262 lang_fill_statement_type *new;
6264 new = new_stat (lang_fill_statement, stat_ptr);
6265 new->fill = fill;
6268 void
6269 lang_add_data (int type, union etree_union *exp)
6271 lang_data_statement_type *new;
6273 new = new_stat (lang_data_statement, stat_ptr);
6274 new->exp = exp;
6275 new->type = type;
6278 /* Create a new reloc statement. RELOC is the BFD relocation type to
6279 generate. HOWTO is the corresponding howto structure (we could
6280 look this up, but the caller has already done so). SECTION is the
6281 section to generate a reloc against, or NAME is the name of the
6282 symbol to generate a reloc against. Exactly one of SECTION and
6283 NAME must be NULL. ADDEND is an expression for the addend. */
6285 void
6286 lang_add_reloc (bfd_reloc_code_real_type reloc,
6287 reloc_howto_type *howto,
6288 asection *section,
6289 const char *name,
6290 union etree_union *addend)
6292 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
6294 p->reloc = reloc;
6295 p->howto = howto;
6296 p->section = section;
6297 p->name = name;
6298 p->addend_exp = addend;
6300 p->addend_value = 0;
6301 p->output_section = NULL;
6302 p->output_offset = 0;
6305 lang_assignment_statement_type *
6306 lang_add_assignment (etree_type *exp)
6308 lang_assignment_statement_type *new;
6310 new = new_stat (lang_assignment_statement, stat_ptr);
6311 new->exp = exp;
6312 return new;
6315 void
6316 lang_add_attribute (enum statement_enum attribute)
6318 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
6321 void
6322 lang_startup (const char *name)
6324 if (startup_file != NULL)
6326 einfo (_("%P%F: multiple STARTUP files\n"));
6328 first_file->filename = name;
6329 first_file->local_sym_name = name;
6330 first_file->real = TRUE;
6332 startup_file = name;
6335 void
6336 lang_float (bfd_boolean maybe)
6338 lang_float_flag = maybe;
6342 /* Work out the load- and run-time regions from a script statement, and
6343 store them in *LMA_REGION and *REGION respectively.
6345 MEMSPEC is the name of the run-time region, or the value of
6346 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6347 LMA_MEMSPEC is the name of the load-time region, or null if the
6348 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6349 had an explicit load address.
6351 It is an error to specify both a load region and a load address. */
6353 static void
6354 lang_get_regions (lang_memory_region_type **region,
6355 lang_memory_region_type **lma_region,
6356 const char *memspec,
6357 const char *lma_memspec,
6358 bfd_boolean have_lma,
6359 bfd_boolean have_vma)
6361 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
6363 /* If no runtime region or VMA has been specified, but the load region
6364 has been specified, then use the load region for the runtime region
6365 as well. */
6366 if (lma_memspec != NULL
6367 && ! have_vma
6368 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
6369 *region = *lma_region;
6370 else
6371 *region = lang_memory_region_lookup (memspec, FALSE);
6373 if (have_lma && lma_memspec != 0)
6374 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6377 void
6378 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6379 lang_output_section_phdr_list *phdrs,
6380 const char *lma_memspec)
6382 lang_get_regions (&current_section->region,
6383 &current_section->lma_region,
6384 memspec, lma_memspec,
6385 current_section->load_base != NULL,
6386 current_section->addr_tree != NULL);
6387 current_section->fill = fill;
6388 current_section->phdrs = phdrs;
6389 stat_ptr = &statement_list;
6392 /* Create an absolute symbol with the given name with the value of the
6393 address of first byte of the section named.
6395 If the symbol already exists, then do nothing. */
6397 void
6398 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
6400 struct bfd_link_hash_entry *h;
6402 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6403 if (h == NULL)
6404 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6406 if (h->type == bfd_link_hash_new
6407 || h->type == bfd_link_hash_undefined)
6409 asection *sec;
6411 h->type = bfd_link_hash_defined;
6413 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6414 if (sec == NULL)
6415 h->u.def.value = 0;
6416 else
6417 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec);
6419 h->u.def.section = bfd_abs_section_ptr;
6423 /* Create an absolute symbol with the given name with the value of the
6424 address of the first byte after the end of the section named.
6426 If the symbol already exists, then do nothing. */
6428 void
6429 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6431 struct bfd_link_hash_entry *h;
6433 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6434 if (h == NULL)
6435 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6437 if (h->type == bfd_link_hash_new
6438 || h->type == bfd_link_hash_undefined)
6440 asection *sec;
6442 h->type = bfd_link_hash_defined;
6444 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6445 if (sec == NULL)
6446 h->u.def.value = 0;
6447 else
6448 h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec)
6449 + TO_ADDR (sec->size));
6451 h->u.def.section = bfd_abs_section_ptr;
6455 void
6456 lang_statement_append (lang_statement_list_type *list,
6457 lang_statement_union_type *element,
6458 lang_statement_union_type **field)
6460 *(list->tail) = element;
6461 list->tail = field;
6464 /* Set the output format type. -oformat overrides scripts. */
6466 void
6467 lang_add_output_format (const char *format,
6468 const char *big,
6469 const char *little,
6470 int from_script)
6472 if (output_target == NULL || !from_script)
6474 if (command_line.endian == ENDIAN_BIG
6475 && big != NULL)
6476 format = big;
6477 else if (command_line.endian == ENDIAN_LITTLE
6478 && little != NULL)
6479 format = little;
6481 output_target = format;
6485 void
6486 lang_add_insert (const char *where, int is_before)
6488 lang_insert_statement_type *new;
6490 new = new_stat (lang_insert_statement, stat_ptr);
6491 new->where = where;
6492 new->is_before = is_before;
6493 saved_script_handle = previous_script_handle;
6496 /* Enter a group. This creates a new lang_group_statement, and sets
6497 stat_ptr to build new statements within the group. */
6499 void
6500 lang_enter_group (void)
6502 lang_group_statement_type *g;
6504 g = new_stat (lang_group_statement, stat_ptr);
6505 lang_list_init (&g->children);
6506 stat_ptr = &g->children;
6509 /* Leave a group. This just resets stat_ptr to start writing to the
6510 regular list of statements again. Note that this will not work if
6511 groups can occur inside anything else which can adjust stat_ptr,
6512 but currently they can't. */
6514 void
6515 lang_leave_group (void)
6517 stat_ptr = &statement_list;
6520 /* Add a new program header. This is called for each entry in a PHDRS
6521 command in a linker script. */
6523 void
6524 lang_new_phdr (const char *name,
6525 etree_type *type,
6526 bfd_boolean filehdr,
6527 bfd_boolean phdrs,
6528 etree_type *at,
6529 etree_type *flags)
6531 struct lang_phdr *n, **pp;
6533 n = stat_alloc (sizeof (struct lang_phdr));
6534 n->next = NULL;
6535 n->name = name;
6536 n->type = exp_get_value_int (type, 0, "program header type");
6537 n->filehdr = filehdr;
6538 n->phdrs = phdrs;
6539 n->at = at;
6540 n->flags = flags;
6542 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6544 *pp = n;
6547 /* Record the program header information in the output BFD. FIXME: We
6548 should not be calling an ELF specific function here. */
6550 static void
6551 lang_record_phdrs (void)
6553 unsigned int alc;
6554 asection **secs;
6555 lang_output_section_phdr_list *last;
6556 struct lang_phdr *l;
6557 lang_output_section_statement_type *os;
6559 alc = 10;
6560 secs = xmalloc (alc * sizeof (asection *));
6561 last = NULL;
6563 for (l = lang_phdr_list; l != NULL; l = l->next)
6565 unsigned int c;
6566 flagword flags;
6567 bfd_vma at;
6569 c = 0;
6570 for (os = &lang_output_section_statement.head->output_section_statement;
6571 os != NULL;
6572 os = os->next)
6574 lang_output_section_phdr_list *pl;
6576 if (os->constraint == -1)
6577 continue;
6579 pl = os->phdrs;
6580 if (pl != NULL)
6581 last = pl;
6582 else
6584 if (os->sectype == noload_section
6585 || os->bfd_section == NULL
6586 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6587 continue;
6589 if (last == NULL)
6591 lang_output_section_statement_type * tmp_os;
6593 /* If we have not run across a section with a program
6594 header assigned to it yet, then scan forwards to find
6595 one. This prevents inconsistencies in the linker's
6596 behaviour when a script has specified just a single
6597 header and there are sections in that script which are
6598 not assigned to it, and which occur before the first
6599 use of that header. See here for more details:
6600 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6601 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
6602 if (tmp_os->phdrs)
6604 last = tmp_os->phdrs;
6605 break;
6607 if (last == NULL)
6608 einfo (_("%F%P: no sections assigned to phdrs\n"));
6610 pl = last;
6613 if (os->bfd_section == NULL)
6614 continue;
6616 for (; pl != NULL; pl = pl->next)
6618 if (strcmp (pl->name, l->name) == 0)
6620 if (c >= alc)
6622 alc *= 2;
6623 secs = xrealloc (secs, alc * sizeof (asection *));
6625 secs[c] = os->bfd_section;
6626 ++c;
6627 pl->used = TRUE;
6632 if (l->flags == NULL)
6633 flags = 0;
6634 else
6635 flags = exp_get_vma (l->flags, 0, "phdr flags");
6637 if (l->at == NULL)
6638 at = 0;
6639 else
6640 at = exp_get_vma (l->at, 0, "phdr load address");
6642 if (! bfd_record_phdr (link_info.output_bfd, l->type,
6643 l->flags != NULL, flags, l->at != NULL,
6644 at, l->filehdr, l->phdrs, c, secs))
6645 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6648 free (secs);
6650 /* Make sure all the phdr assignments succeeded. */
6651 for (os = &lang_output_section_statement.head->output_section_statement;
6652 os != NULL;
6653 os = os->next)
6655 lang_output_section_phdr_list *pl;
6657 if (os->constraint == -1
6658 || os->bfd_section == NULL)
6659 continue;
6661 for (pl = os->phdrs;
6662 pl != NULL;
6663 pl = pl->next)
6664 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6665 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6666 os->name, pl->name);
6670 /* Record a list of sections which may not be cross referenced. */
6672 void
6673 lang_add_nocrossref (lang_nocrossref_type *l)
6675 struct lang_nocrossrefs *n;
6677 n = xmalloc (sizeof *n);
6678 n->next = nocrossref_list;
6679 n->list = l;
6680 nocrossref_list = n;
6682 /* Set notice_all so that we get informed about all symbols. */
6683 link_info.notice_all = TRUE;
6686 /* Overlay handling. We handle overlays with some static variables. */
6688 /* The overlay virtual address. */
6689 static etree_type *overlay_vma;
6690 /* And subsection alignment. */
6691 static etree_type *overlay_subalign;
6693 /* An expression for the maximum section size seen so far. */
6694 static etree_type *overlay_max;
6696 /* A list of all the sections in this overlay. */
6698 struct overlay_list {
6699 struct overlay_list *next;
6700 lang_output_section_statement_type *os;
6703 static struct overlay_list *overlay_list;
6705 /* Start handling an overlay. */
6707 void
6708 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6710 /* The grammar should prevent nested overlays from occurring. */
6711 ASSERT (overlay_vma == NULL
6712 && overlay_subalign == NULL
6713 && overlay_max == NULL);
6715 overlay_vma = vma_expr;
6716 overlay_subalign = subalign;
6719 /* Start a section in an overlay. We handle this by calling
6720 lang_enter_output_section_statement with the correct VMA.
6721 lang_leave_overlay sets up the LMA and memory regions. */
6723 void
6724 lang_enter_overlay_section (const char *name)
6726 struct overlay_list *n;
6727 etree_type *size;
6729 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
6730 0, overlay_subalign, 0, 0);
6732 /* If this is the first section, then base the VMA of future
6733 sections on this one. This will work correctly even if `.' is
6734 used in the addresses. */
6735 if (overlay_list == NULL)
6736 overlay_vma = exp_nameop (ADDR, name);
6738 /* Remember the section. */
6739 n = xmalloc (sizeof *n);
6740 n->os = current_section;
6741 n->next = overlay_list;
6742 overlay_list = n;
6744 size = exp_nameop (SIZEOF, name);
6746 /* Arrange to work out the maximum section end address. */
6747 if (overlay_max == NULL)
6748 overlay_max = size;
6749 else
6750 overlay_max = exp_binop (MAX_K, overlay_max, size);
6753 /* Finish a section in an overlay. There isn't any special to do
6754 here. */
6756 void
6757 lang_leave_overlay_section (fill_type *fill,
6758 lang_output_section_phdr_list *phdrs)
6760 const char *name;
6761 char *clean, *s2;
6762 const char *s1;
6763 char *buf;
6765 name = current_section->name;
6767 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6768 region and that no load-time region has been specified. It doesn't
6769 really matter what we say here, since lang_leave_overlay will
6770 override it. */
6771 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6773 /* Define the magic symbols. */
6775 clean = xmalloc (strlen (name) + 1);
6776 s2 = clean;
6777 for (s1 = name; *s1 != '\0'; s1++)
6778 if (ISALNUM (*s1) || *s1 == '_')
6779 *s2++ = *s1;
6780 *s2 = '\0';
6782 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6783 sprintf (buf, "__load_start_%s", clean);
6784 lang_add_assignment (exp_provide (buf,
6785 exp_nameop (LOADADDR, name),
6786 FALSE));
6788 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6789 sprintf (buf, "__load_stop_%s", clean);
6790 lang_add_assignment (exp_provide (buf,
6791 exp_binop ('+',
6792 exp_nameop (LOADADDR, name),
6793 exp_nameop (SIZEOF, name)),
6794 FALSE));
6796 free (clean);
6799 /* Finish an overlay. If there are any overlay wide settings, this
6800 looks through all the sections in the overlay and sets them. */
6802 void
6803 lang_leave_overlay (etree_type *lma_expr,
6804 int nocrossrefs,
6805 fill_type *fill,
6806 const char *memspec,
6807 lang_output_section_phdr_list *phdrs,
6808 const char *lma_memspec)
6810 lang_memory_region_type *region;
6811 lang_memory_region_type *lma_region;
6812 struct overlay_list *l;
6813 lang_nocrossref_type *nocrossref;
6815 lang_get_regions (&region, &lma_region,
6816 memspec, lma_memspec,
6817 lma_expr != NULL, FALSE);
6819 nocrossref = NULL;
6821 /* After setting the size of the last section, set '.' to end of the
6822 overlay region. */
6823 if (overlay_list != NULL)
6824 overlay_list->os->update_dot_tree
6825 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6827 l = overlay_list;
6828 while (l != NULL)
6830 struct overlay_list *next;
6832 if (fill != NULL && l->os->fill == NULL)
6833 l->os->fill = fill;
6835 l->os->region = region;
6836 l->os->lma_region = lma_region;
6838 /* The first section has the load address specified in the
6839 OVERLAY statement. The rest are worked out from that.
6840 The base address is not needed (and should be null) if
6841 an LMA region was specified. */
6842 if (l->next == 0)
6844 l->os->load_base = lma_expr;
6845 l->os->sectype = normal_section;
6847 if (phdrs != NULL && l->os->phdrs == NULL)
6848 l->os->phdrs = phdrs;
6850 if (nocrossrefs)
6852 lang_nocrossref_type *nc;
6854 nc = xmalloc (sizeof *nc);
6855 nc->name = l->os->name;
6856 nc->next = nocrossref;
6857 nocrossref = nc;
6860 next = l->next;
6861 free (l);
6862 l = next;
6865 if (nocrossref != NULL)
6866 lang_add_nocrossref (nocrossref);
6868 overlay_vma = NULL;
6869 overlay_list = NULL;
6870 overlay_max = NULL;
6873 /* Version handling. This is only useful for ELF. */
6875 /* This global variable holds the version tree that we build. */
6877 struct bfd_elf_version_tree *lang_elf_version_info;
6879 /* If PREV is NULL, return first version pattern matching particular symbol.
6880 If PREV is non-NULL, return first version pattern matching particular
6881 symbol after PREV (previously returned by lang_vers_match). */
6883 static struct bfd_elf_version_expr *
6884 lang_vers_match (struct bfd_elf_version_expr_head *head,
6885 struct bfd_elf_version_expr *prev,
6886 const char *sym)
6888 const char *cxx_sym = sym;
6889 const char *java_sym = sym;
6890 struct bfd_elf_version_expr *expr = NULL;
6892 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6894 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
6895 if (!cxx_sym)
6896 cxx_sym = sym;
6898 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6900 java_sym = cplus_demangle (sym, DMGL_JAVA);
6901 if (!java_sym)
6902 java_sym = sym;
6905 if (head->htab && (prev == NULL || prev->symbol))
6907 struct bfd_elf_version_expr e;
6909 switch (prev ? prev->mask : 0)
6911 case 0:
6912 if (head->mask & BFD_ELF_VERSION_C_TYPE)
6914 e.symbol = sym;
6915 expr = htab_find (head->htab, &e);
6916 while (expr && strcmp (expr->symbol, sym) == 0)
6917 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
6918 goto out_ret;
6919 else
6920 expr = expr->next;
6922 /* Fallthrough */
6923 case BFD_ELF_VERSION_C_TYPE:
6924 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
6926 e.symbol = cxx_sym;
6927 expr = htab_find (head->htab, &e);
6928 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
6929 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6930 goto out_ret;
6931 else
6932 expr = expr->next;
6934 /* Fallthrough */
6935 case BFD_ELF_VERSION_CXX_TYPE:
6936 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
6938 e.symbol = java_sym;
6939 expr = htab_find (head->htab, &e);
6940 while (expr && strcmp (expr->symbol, java_sym) == 0)
6941 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6942 goto out_ret;
6943 else
6944 expr = expr->next;
6946 /* Fallthrough */
6947 default:
6948 break;
6952 /* Finally, try the wildcards. */
6953 if (prev == NULL || prev->symbol)
6954 expr = head->remaining;
6955 else
6956 expr = prev->next;
6957 for (; expr; expr = expr->next)
6959 const char *s;
6961 if (!expr->pattern)
6962 continue;
6964 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
6965 break;
6967 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
6968 s = java_sym;
6969 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
6970 s = cxx_sym;
6971 else
6972 s = sym;
6973 if (fnmatch (expr->pattern, s, 0) == 0)
6974 break;
6977 out_ret:
6978 if (cxx_sym != sym)
6979 free ((char *) cxx_sym);
6980 if (java_sym != sym)
6981 free ((char *) java_sym);
6982 return expr;
6985 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6986 return a string pointing to the symbol name. */
6988 static const char *
6989 realsymbol (const char *pattern)
6991 const char *p;
6992 bfd_boolean changed = FALSE, backslash = FALSE;
6993 char *s, *symbol = xmalloc (strlen (pattern) + 1);
6995 for (p = pattern, s = symbol; *p != '\0'; ++p)
6997 /* It is a glob pattern only if there is no preceding
6998 backslash. */
6999 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
7001 free (symbol);
7002 return NULL;
7005 if (backslash)
7007 /* Remove the preceding backslash. */
7008 *(s - 1) = *p;
7009 changed = TRUE;
7011 else
7012 *s++ = *p;
7014 backslash = *p == '\\';
7017 if (changed)
7019 *s = '\0';
7020 return symbol;
7022 else
7024 free (symbol);
7025 return pattern;
7029 /* This is called for each variable name or match expression. NEW is
7030 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7031 pattern to be matched against symbol names. */
7033 struct bfd_elf_version_expr *
7034 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7035 const char *new,
7036 const char *lang,
7037 bfd_boolean literal_p)
7039 struct bfd_elf_version_expr *ret;
7041 ret = xmalloc (sizeof *ret);
7042 ret->next = orig;
7043 ret->pattern = literal_p ? NULL : new;
7044 ret->symver = 0;
7045 ret->script = 0;
7046 ret->symbol = literal_p ? new : realsymbol (new);
7048 if (lang == NULL || strcasecmp (lang, "C") == 0)
7049 ret->mask = BFD_ELF_VERSION_C_TYPE;
7050 else if (strcasecmp (lang, "C++") == 0)
7051 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
7052 else if (strcasecmp (lang, "Java") == 0)
7053 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
7054 else
7056 einfo (_("%X%P: unknown language `%s' in version information\n"),
7057 lang);
7058 ret->mask = BFD_ELF_VERSION_C_TYPE;
7061 return ldemul_new_vers_pattern (ret);
7064 /* This is called for each set of variable names and match
7065 expressions. */
7067 struct bfd_elf_version_tree *
7068 lang_new_vers_node (struct bfd_elf_version_expr *globals,
7069 struct bfd_elf_version_expr *locals)
7071 struct bfd_elf_version_tree *ret;
7073 ret = xcalloc (1, sizeof *ret);
7074 ret->globals.list = globals;
7075 ret->locals.list = locals;
7076 ret->match = lang_vers_match;
7077 ret->name_indx = (unsigned int) -1;
7078 return ret;
7081 /* This static variable keeps track of version indices. */
7083 static int version_index;
7085 static hashval_t
7086 version_expr_head_hash (const void *p)
7088 const struct bfd_elf_version_expr *e = p;
7090 return htab_hash_string (e->symbol);
7093 static int
7094 version_expr_head_eq (const void *p1, const void *p2)
7096 const struct bfd_elf_version_expr *e1 = p1;
7097 const struct bfd_elf_version_expr *e2 = p2;
7099 return strcmp (e1->symbol, e2->symbol) == 0;
7102 static void
7103 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7105 size_t count = 0;
7106 struct bfd_elf_version_expr *e, *next;
7107 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7109 for (e = head->list; e; e = e->next)
7111 if (e->symbol)
7112 count++;
7113 head->mask |= e->mask;
7116 if (count)
7118 head->htab = htab_create (count * 2, version_expr_head_hash,
7119 version_expr_head_eq, NULL);
7120 list_loc = &head->list;
7121 remaining_loc = &head->remaining;
7122 for (e = head->list; e; e = next)
7124 next = e->next;
7125 if (!e->symbol)
7127 *remaining_loc = e;
7128 remaining_loc = &e->next;
7130 else
7132 void **loc = htab_find_slot (head->htab, e, INSERT);
7134 if (*loc)
7136 struct bfd_elf_version_expr *e1, *last;
7138 e1 = *loc;
7139 last = NULL;
7142 if (e1->mask == e->mask)
7144 last = NULL;
7145 break;
7147 last = e1;
7148 e1 = e1->next;
7150 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
7152 if (last == NULL)
7154 /* This is a duplicate. */
7155 /* FIXME: Memory leak. Sometimes pattern is not
7156 xmalloced alone, but in larger chunk of memory. */
7157 /* free (e->symbol); */
7158 free (e);
7160 else
7162 e->next = last->next;
7163 last->next = e;
7166 else
7168 *loc = e;
7169 *list_loc = e;
7170 list_loc = &e->next;
7174 *remaining_loc = NULL;
7175 *list_loc = head->remaining;
7177 else
7178 head->remaining = head->list;
7181 /* This is called when we know the name and dependencies of the
7182 version. */
7184 void
7185 lang_register_vers_node (const char *name,
7186 struct bfd_elf_version_tree *version,
7187 struct bfd_elf_version_deps *deps)
7189 struct bfd_elf_version_tree *t, **pp;
7190 struct bfd_elf_version_expr *e1;
7192 if (name == NULL)
7193 name = "";
7195 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7196 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7198 einfo (_("%X%P: anonymous version tag cannot be combined"
7199 " with other version tags\n"));
7200 free (version);
7201 return;
7204 /* Make sure this node has a unique name. */
7205 for (t = lang_elf_version_info; t != NULL; t = t->next)
7206 if (strcmp (t->name, name) == 0)
7207 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7209 lang_finalize_version_expr_head (&version->globals);
7210 lang_finalize_version_expr_head (&version->locals);
7212 /* Check the global and local match names, and make sure there
7213 aren't any duplicates. */
7215 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
7217 for (t = lang_elf_version_info; t != NULL; t = t->next)
7219 struct bfd_elf_version_expr *e2;
7221 if (t->locals.htab && e1->symbol)
7223 e2 = htab_find (t->locals.htab, e1);
7224 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
7226 if (e1->mask == e2->mask)
7227 einfo (_("%X%P: duplicate expression `%s'"
7228 " in version information\n"), e1->symbol);
7229 e2 = e2->next;
7232 else if (!e1->symbol)
7233 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
7234 if (strcmp (e1->pattern, e2->pattern) == 0
7235 && e1->mask == e2->mask)
7236 einfo (_("%X%P: duplicate expression `%s'"
7237 " in version information\n"), e1->pattern);
7241 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
7243 for (t = lang_elf_version_info; t != NULL; t = t->next)
7245 struct bfd_elf_version_expr *e2;
7247 if (t->globals.htab && e1->symbol)
7249 e2 = htab_find (t->globals.htab, e1);
7250 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
7252 if (e1->mask == e2->mask)
7253 einfo (_("%X%P: duplicate expression `%s'"
7254 " in version information\n"),
7255 e1->symbol);
7256 e2 = e2->next;
7259 else if (!e1->symbol)
7260 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
7261 if (strcmp (e1->pattern, e2->pattern) == 0
7262 && e1->mask == e2->mask)
7263 einfo (_("%X%P: duplicate expression `%s'"
7264 " in version information\n"), e1->pattern);
7268 version->deps = deps;
7269 version->name = name;
7270 if (name[0] != '\0')
7272 ++version_index;
7273 version->vernum = version_index;
7275 else
7276 version->vernum = 0;
7278 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7280 *pp = version;
7283 /* This is called when we see a version dependency. */
7285 struct bfd_elf_version_deps *
7286 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
7288 struct bfd_elf_version_deps *ret;
7289 struct bfd_elf_version_tree *t;
7291 ret = xmalloc (sizeof *ret);
7292 ret->next = list;
7294 for (t = lang_elf_version_info; t != NULL; t = t->next)
7296 if (strcmp (t->name, name) == 0)
7298 ret->version_needed = t;
7299 return ret;
7303 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7305 return ret;
7308 static void
7309 lang_do_version_exports_section (void)
7311 struct bfd_elf_version_expr *greg = NULL, *lreg;
7313 LANG_FOR_EACH_INPUT_STATEMENT (is)
7315 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7316 char *contents, *p;
7317 bfd_size_type len;
7319 if (sec == NULL)
7320 continue;
7322 len = sec->size;
7323 contents = xmalloc (len);
7324 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
7325 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
7327 p = contents;
7328 while (p < contents + len)
7330 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
7331 p = strchr (p, '\0') + 1;
7334 /* Do not free the contents, as we used them creating the regex. */
7336 /* Do not include this section in the link. */
7337 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
7340 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
7341 lang_register_vers_node (command_line.version_exports_section,
7342 lang_new_vers_node (greg, lreg), NULL);
7345 void
7346 lang_add_unique (const char *name)
7348 struct unique_sections *ent;
7350 for (ent = unique_section_list; ent; ent = ent->next)
7351 if (strcmp (ent->name, name) == 0)
7352 return;
7354 ent = xmalloc (sizeof *ent);
7355 ent->name = xstrdup (name);
7356 ent->next = unique_section_list;
7357 unique_section_list = ent;
7360 /* Append the list of dynamic symbols to the existing one. */
7362 void
7363 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7365 if (link_info.dynamic_list)
7367 struct bfd_elf_version_expr *tail;
7368 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7370 tail->next = link_info.dynamic_list->head.list;
7371 link_info.dynamic_list->head.list = dynamic;
7373 else
7375 struct bfd_elf_dynamic_list *d;
7377 d = xcalloc (1, sizeof *d);
7378 d->head.list = dynamic;
7379 d->match = lang_vers_match;
7380 link_info.dynamic_list = d;
7384 /* Append the list of C++ typeinfo dynamic symbols to the existing
7385 one. */
7387 void
7388 lang_append_dynamic_list_cpp_typeinfo (void)
7390 const char * symbols [] =
7392 "typeinfo name for*",
7393 "typeinfo for*"
7395 struct bfd_elf_version_expr *dynamic = NULL;
7396 unsigned int i;
7398 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7399 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7400 FALSE);
7402 lang_append_dynamic_list (dynamic);
7405 /* Append the list of C++ operator new and delete dynamic symbols to the
7406 existing one. */
7408 void
7409 lang_append_dynamic_list_cpp_new (void)
7411 const char * symbols [] =
7413 "operator new*",
7414 "operator delete*"
7416 struct bfd_elf_version_expr *dynamic = NULL;
7417 unsigned int i;
7419 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7420 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7421 FALSE);
7423 lang_append_dynamic_list (dynamic);