* elf32-spu.c (mark_overlay_section): Move .init and .fini
[binutils.git] / ld / ldlang.c
blob65e092bb1928b672284fbf34f7634fbbd2a7ad93
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, 2009
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 "sysdep.h"
24 #include "bfd.h"
25 #include "libiberty.h"
26 #include "safe-ctype.h"
27 #include "obstack.h"
28 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldmisc.h"
37 #include "ldctor.h"
38 #include "ldfile.h"
39 #include "ldemul.h"
40 #include "fnmatch.h"
41 #include "demangle.h"
42 #include "hashtab.h"
44 #ifndef offsetof
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
46 #endif
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file;
55 static bfd_boolean placed_commons = FALSE;
56 static bfd_boolean stripped_excluded_sections = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct bfd_hash_table lang_definedness_table;
65 static lang_statement_list_type *stat_save[10];
66 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
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 static int
138 name_match (const char *pattern, const char *name)
140 if (wildcardp (pattern))
141 return fnmatch (pattern, name, 0);
142 return strcmp (pattern, name);
145 /* If PATTERN is of the form archive:file, return a pointer to the
146 separator. If not, return NULL. */
148 static char *
149 archive_path (const char *pattern)
151 char *p = NULL;
153 if (link_info.path_separator == 0)
154 return p;
156 p = strchr (pattern, link_info.path_separator);
157 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
158 if (p == NULL || link_info.path_separator != ':')
159 return p;
161 /* Assume a match on the second char is part of drive specifier,
162 as in "c:\silly.dos". */
163 if (p == pattern + 1 && ISALPHA (*pattern))
164 p = strchr (p + 1, link_info.path_separator);
165 #endif
166 return p;
169 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
170 return whether F matches FILE_SPEC. */
172 static bfd_boolean
173 input_statement_is_archive_path (const char *file_spec, char *sep,
174 lang_input_statement_type *f)
176 bfd_boolean match = FALSE;
178 if ((*(sep + 1) == 0
179 || name_match (sep + 1, f->filename) == 0)
180 && ((sep != file_spec)
181 == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
183 match = TRUE;
185 if (sep != file_spec)
187 const char *aname = f->the_bfd->my_archive->filename;
188 *sep = 0;
189 match = name_match (file_spec, aname) == 0;
190 *sep = link_info.path_separator;
193 return match;
196 static bfd_boolean
197 unique_section_p (const asection *sec)
199 struct unique_sections *unam;
200 const char *secnam;
202 if (link_info.relocatable
203 && sec->owner != NULL
204 && bfd_is_group_section (sec->owner, sec))
205 return TRUE;
207 secnam = sec->name;
208 for (unam = unique_section_list; unam; unam = unam->next)
209 if (name_match (unam->name, secnam) == 0)
210 return TRUE;
212 return FALSE;
215 /* Generic traversal routines for finding matching sections. */
217 /* Try processing a section against a wildcard. This just calls
218 the callback unless the filename exclusion list is present
219 and excludes the file. It's hardly ever present so this
220 function is very fast. */
222 static void
223 walk_wild_consider_section (lang_wild_statement_type *ptr,
224 lang_input_statement_type *file,
225 asection *s,
226 struct wildcard_list *sec,
227 callback_t callback,
228 void *data)
230 struct name_list *list_tmp;
232 /* Don't process sections from files which were excluded. */
233 for (list_tmp = sec->spec.exclude_name_list;
234 list_tmp;
235 list_tmp = list_tmp->next)
237 char *p = archive_path (list_tmp->name);
239 if (p != NULL)
241 if (input_statement_is_archive_path (list_tmp->name, p, file))
242 return;
245 else if (name_match (list_tmp->name, file->filename) == 0)
246 return;
248 /* FIXME: Perhaps remove the following at some stage? Matching
249 unadorned archives like this was never documented and has
250 been superceded by the archive:path syntax. */
251 else if (file->the_bfd != NULL
252 && file->the_bfd->my_archive != NULL
253 && name_match (list_tmp->name,
254 file->the_bfd->my_archive->filename) == 0)
255 return;
258 (*callback) (ptr, sec, s, file, data);
261 /* Lowest common denominator routine that can handle everything correctly,
262 but slowly. */
264 static void
265 walk_wild_section_general (lang_wild_statement_type *ptr,
266 lang_input_statement_type *file,
267 callback_t callback,
268 void *data)
270 asection *s;
271 struct wildcard_list *sec;
273 for (s = file->the_bfd->sections; s != NULL; s = s->next)
275 sec = ptr->section_list;
276 if (sec == NULL)
277 (*callback) (ptr, sec, s, file, data);
279 while (sec != NULL)
281 bfd_boolean skip = FALSE;
283 if (sec->spec.name != NULL)
285 const char *sname = bfd_get_section_name (file->the_bfd, s);
287 skip = name_match (sec->spec.name, sname) != 0;
290 if (!skip)
291 walk_wild_consider_section (ptr, file, s, sec, callback, data);
293 sec = sec->next;
298 /* Routines to find a single section given its name. If there's more
299 than one section with that name, we report that. */
301 typedef struct
303 asection *found_section;
304 bfd_boolean multiple_sections_found;
305 } section_iterator_callback_data;
307 static bfd_boolean
308 section_iterator_callback (bfd *bfd ATTRIBUTE_UNUSED, asection *s, void *data)
310 section_iterator_callback_data *d = data;
312 if (d->found_section != NULL)
314 d->multiple_sections_found = TRUE;
315 return TRUE;
318 d->found_section = s;
319 return FALSE;
322 static asection *
323 find_section (lang_input_statement_type *file,
324 struct wildcard_list *sec,
325 bfd_boolean *multiple_sections_found)
327 section_iterator_callback_data cb_data = { NULL, FALSE };
329 bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
330 section_iterator_callback, &cb_data);
331 *multiple_sections_found = cb_data.multiple_sections_found;
332 return cb_data.found_section;
335 /* Code for handling simple wildcards without going through fnmatch,
336 which can be expensive because of charset translations etc. */
338 /* A simple wild is a literal string followed by a single '*',
339 where the literal part is at least 4 characters long. */
341 static bfd_boolean
342 is_simple_wild (const char *name)
344 size_t len = strcspn (name, "*?[");
345 return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
348 static bfd_boolean
349 match_simple_wild (const char *pattern, const char *name)
351 /* The first four characters of the pattern are guaranteed valid
352 non-wildcard characters. So we can go faster. */
353 if (pattern[0] != name[0] || pattern[1] != name[1]
354 || pattern[2] != name[2] || pattern[3] != name[3])
355 return FALSE;
357 pattern += 4;
358 name += 4;
359 while (*pattern != '*')
360 if (*name++ != *pattern++)
361 return FALSE;
363 return TRUE;
366 /* Compare sections ASEC and BSEC according to SORT. */
368 static int
369 compare_section (sort_type sort, asection *asec, asection *bsec)
371 int ret;
373 switch (sort)
375 default:
376 abort ();
378 case by_alignment_name:
379 ret = (bfd_section_alignment (bsec->owner, bsec)
380 - bfd_section_alignment (asec->owner, asec));
381 if (ret)
382 break;
383 /* Fall through. */
385 case by_name:
386 ret = strcmp (bfd_get_section_name (asec->owner, asec),
387 bfd_get_section_name (bsec->owner, bsec));
388 break;
390 case by_name_alignment:
391 ret = strcmp (bfd_get_section_name (asec->owner, asec),
392 bfd_get_section_name (bsec->owner, bsec));
393 if (ret)
394 break;
395 /* Fall through. */
397 case by_alignment:
398 ret = (bfd_section_alignment (bsec->owner, bsec)
399 - bfd_section_alignment (asec->owner, asec));
400 break;
403 return ret;
406 /* Build a Binary Search Tree to sort sections, unlike insertion sort
407 used in wild_sort(). BST is considerably faster if the number of
408 of sections are large. */
410 static lang_section_bst_type **
411 wild_sort_fast (lang_wild_statement_type *wild,
412 struct wildcard_list *sec,
413 lang_input_statement_type *file ATTRIBUTE_UNUSED,
414 asection *section)
416 lang_section_bst_type **tree;
418 tree = &wild->tree;
419 if (!wild->filenames_sorted
420 && (sec == NULL || sec->spec.sorted == none))
422 /* Append at the right end of tree. */
423 while (*tree)
424 tree = &((*tree)->right);
425 return tree;
428 while (*tree)
430 /* Find the correct node to append this section. */
431 if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
432 tree = &((*tree)->left);
433 else
434 tree = &((*tree)->right);
437 return tree;
440 /* Use wild_sort_fast to build a BST to sort sections. */
442 static void
443 output_section_callback_fast (lang_wild_statement_type *ptr,
444 struct wildcard_list *sec,
445 asection *section,
446 lang_input_statement_type *file,
447 void *output ATTRIBUTE_UNUSED)
449 lang_section_bst_type *node;
450 lang_section_bst_type **tree;
452 if (unique_section_p (section))
453 return;
455 node = xmalloc (sizeof (lang_section_bst_type));
456 node->left = 0;
457 node->right = 0;
458 node->section = section;
460 tree = wild_sort_fast (ptr, sec, file, section);
461 if (tree != NULL)
462 *tree = node;
465 /* Convert a sorted sections' BST back to list form. */
467 static void
468 output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
469 lang_section_bst_type *tree,
470 void *output)
472 if (tree->left)
473 output_section_callback_tree_to_list (ptr, tree->left, output);
475 lang_add_section (&ptr->children, tree->section,
476 (lang_output_section_statement_type *) output);
478 if (tree->right)
479 output_section_callback_tree_to_list (ptr, tree->right, output);
481 free (tree);
484 /* Specialized, optimized routines for handling different kinds of
485 wildcards */
487 static void
488 walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
489 lang_input_statement_type *file,
490 callback_t callback,
491 void *data)
493 /* We can just do a hash lookup for the section with the right name.
494 But if that lookup discovers more than one section with the name
495 (should be rare), we fall back to the general algorithm because
496 we would otherwise have to sort the sections to make sure they
497 get processed in the bfd's order. */
498 bfd_boolean multiple_sections_found;
499 struct wildcard_list *sec0 = ptr->handler_data[0];
500 asection *s0 = find_section (file, sec0, &multiple_sections_found);
502 if (multiple_sections_found)
503 walk_wild_section_general (ptr, file, callback, data);
504 else if (s0)
505 walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
508 static void
509 walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
510 lang_input_statement_type *file,
511 callback_t callback,
512 void *data)
514 asection *s;
515 struct wildcard_list *wildsec0 = ptr->handler_data[0];
517 for (s = file->the_bfd->sections; s != NULL; s = s->next)
519 const char *sname = bfd_get_section_name (file->the_bfd, s);
520 bfd_boolean skip = !match_simple_wild (wildsec0->spec.name, sname);
522 if (!skip)
523 walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
527 static void
528 walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
529 lang_input_statement_type *file,
530 callback_t callback,
531 void *data)
533 asection *s;
534 struct wildcard_list *sec0 = ptr->handler_data[0];
535 struct wildcard_list *wildsec1 = ptr->handler_data[1];
536 bfd_boolean multiple_sections_found;
537 asection *s0 = find_section (file, sec0, &multiple_sections_found);
539 if (multiple_sections_found)
541 walk_wild_section_general (ptr, file, callback, data);
542 return;
545 /* Note that if the section was not found, s0 is NULL and
546 we'll simply never succeed the s == s0 test below. */
547 for (s = file->the_bfd->sections; s != NULL; s = s->next)
549 /* Recall that in this code path, a section cannot satisfy more
550 than one spec, so if s == s0 then it cannot match
551 wildspec1. */
552 if (s == s0)
553 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
554 else
556 const char *sname = bfd_get_section_name (file->the_bfd, s);
557 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
559 if (!skip)
560 walk_wild_consider_section (ptr, file, s, wildsec1, callback,
561 data);
566 static void
567 walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
568 lang_input_statement_type *file,
569 callback_t callback,
570 void *data)
572 asection *s;
573 struct wildcard_list *sec0 = ptr->handler_data[0];
574 struct wildcard_list *wildsec1 = ptr->handler_data[1];
575 struct wildcard_list *wildsec2 = ptr->handler_data[2];
576 bfd_boolean multiple_sections_found;
577 asection *s0 = find_section (file, sec0, &multiple_sections_found);
579 if (multiple_sections_found)
581 walk_wild_section_general (ptr, file, callback, data);
582 return;
585 for (s = file->the_bfd->sections; s != NULL; s = s->next)
587 if (s == s0)
588 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
589 else
591 const char *sname = bfd_get_section_name (file->the_bfd, s);
592 bfd_boolean skip = !match_simple_wild (wildsec1->spec.name, sname);
594 if (!skip)
595 walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
596 else
598 skip = !match_simple_wild (wildsec2->spec.name, sname);
599 if (!skip)
600 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
601 data);
607 static void
608 walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
609 lang_input_statement_type *file,
610 callback_t callback,
611 void *data)
613 asection *s;
614 struct wildcard_list *sec0 = ptr->handler_data[0];
615 struct wildcard_list *sec1 = ptr->handler_data[1];
616 struct wildcard_list *wildsec2 = ptr->handler_data[2];
617 struct wildcard_list *wildsec3 = ptr->handler_data[3];
618 bfd_boolean multiple_sections_found;
619 asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
621 if (multiple_sections_found)
623 walk_wild_section_general (ptr, file, callback, data);
624 return;
627 s1 = find_section (file, sec1, &multiple_sections_found);
628 if (multiple_sections_found)
630 walk_wild_section_general (ptr, file, callback, data);
631 return;
634 for (s = file->the_bfd->sections; s != NULL; s = s->next)
636 if (s == s0)
637 walk_wild_consider_section (ptr, file, s, sec0, callback, data);
638 else
639 if (s == s1)
640 walk_wild_consider_section (ptr, file, s, sec1, callback, data);
641 else
643 const char *sname = bfd_get_section_name (file->the_bfd, s);
644 bfd_boolean skip = !match_simple_wild (wildsec2->spec.name,
645 sname);
647 if (!skip)
648 walk_wild_consider_section (ptr, file, s, wildsec2, callback,
649 data);
650 else
652 skip = !match_simple_wild (wildsec3->spec.name, sname);
653 if (!skip)
654 walk_wild_consider_section (ptr, file, s, wildsec3,
655 callback, data);
661 static void
662 walk_wild_section (lang_wild_statement_type *ptr,
663 lang_input_statement_type *file,
664 callback_t callback,
665 void *data)
667 if (file->just_syms_flag)
668 return;
670 (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
673 /* Returns TRUE when name1 is a wildcard spec that might match
674 something name2 can match. We're conservative: we return FALSE
675 only if the prefixes of name1 and name2 are different up to the
676 first wildcard character. */
678 static bfd_boolean
679 wild_spec_can_overlap (const char *name1, const char *name2)
681 size_t prefix1_len = strcspn (name1, "?*[");
682 size_t prefix2_len = strcspn (name2, "?*[");
683 size_t min_prefix_len;
685 /* Note that if there is no wildcard character, then we treat the
686 terminating 0 as part of the prefix. Thus ".text" won't match
687 ".text." or ".text.*", for example. */
688 if (name1[prefix1_len] == '\0')
689 prefix1_len++;
690 if (name2[prefix2_len] == '\0')
691 prefix2_len++;
693 min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
695 return memcmp (name1, name2, min_prefix_len) == 0;
698 /* Select specialized code to handle various kinds of wildcard
699 statements. */
701 static void
702 analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
704 int sec_count = 0;
705 int wild_name_count = 0;
706 struct wildcard_list *sec;
707 int signature;
708 int data_counter;
710 ptr->walk_wild_section_handler = walk_wild_section_general;
711 ptr->handler_data[0] = NULL;
712 ptr->handler_data[1] = NULL;
713 ptr->handler_data[2] = NULL;
714 ptr->handler_data[3] = NULL;
715 ptr->tree = NULL;
717 /* Count how many wildcard_specs there are, and how many of those
718 actually use wildcards in the name. Also, bail out if any of the
719 wildcard names are NULL. (Can this actually happen?
720 walk_wild_section used to test for it.) And bail out if any
721 of the wildcards are more complex than a simple string
722 ending in a single '*'. */
723 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
725 ++sec_count;
726 if (sec->spec.name == NULL)
727 return;
728 if (wildcardp (sec->spec.name))
730 ++wild_name_count;
731 if (!is_simple_wild (sec->spec.name))
732 return;
736 /* The zero-spec case would be easy to optimize but it doesn't
737 happen in practice. Likewise, more than 4 specs doesn't
738 happen in practice. */
739 if (sec_count == 0 || sec_count > 4)
740 return;
742 /* Check that no two specs can match the same section. */
743 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
745 struct wildcard_list *sec2;
746 for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
748 if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
749 return;
753 signature = (sec_count << 8) + wild_name_count;
754 switch (signature)
756 case 0x0100:
757 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
758 break;
759 case 0x0101:
760 ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
761 break;
762 case 0x0201:
763 ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
764 break;
765 case 0x0302:
766 ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
767 break;
768 case 0x0402:
769 ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
770 break;
771 default:
772 return;
775 /* Now fill the data array with pointers to the specs, first the
776 specs with non-wildcard names, then the specs with wildcard
777 names. It's OK to process the specs in different order from the
778 given order, because we've already determined that no section
779 will match more than one spec. */
780 data_counter = 0;
781 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
782 if (!wildcardp (sec->spec.name))
783 ptr->handler_data[data_counter++] = sec;
784 for (sec = ptr->section_list; sec != NULL; sec = sec->next)
785 if (wildcardp (sec->spec.name))
786 ptr->handler_data[data_counter++] = sec;
789 /* Handle a wild statement for a single file F. */
791 static void
792 walk_wild_file (lang_wild_statement_type *s,
793 lang_input_statement_type *f,
794 callback_t callback,
795 void *data)
797 if (f->the_bfd == NULL
798 || ! bfd_check_format (f->the_bfd, bfd_archive))
799 walk_wild_section (s, f, callback, data);
800 else
802 bfd *member;
804 /* This is an archive file. We must map each member of the
805 archive separately. */
806 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
807 while (member != NULL)
809 /* When lookup_name is called, it will call the add_symbols
810 entry point for the archive. For each element of the
811 archive which is included, BFD will call ldlang_add_file,
812 which will set the usrdata field of the member to the
813 lang_input_statement. */
814 if (member->usrdata != NULL)
816 walk_wild_section (s, member->usrdata, callback, data);
819 member = bfd_openr_next_archived_file (f->the_bfd, member);
824 static void
825 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
827 const char *file_spec = s->filename;
828 char *p;
830 if (file_spec == NULL)
832 /* Perform the iteration over all files in the list. */
833 LANG_FOR_EACH_INPUT_STATEMENT (f)
835 walk_wild_file (s, f, callback, data);
838 else if ((p = archive_path (file_spec)) != NULL)
840 LANG_FOR_EACH_INPUT_STATEMENT (f)
842 if (input_statement_is_archive_path (file_spec, p, f))
843 walk_wild_file (s, f, callback, data);
846 else if (wildcardp (file_spec))
848 LANG_FOR_EACH_INPUT_STATEMENT (f)
850 if (fnmatch (file_spec, f->filename, 0) == 0)
851 walk_wild_file (s, f, callback, data);
854 else
856 lang_input_statement_type *f;
858 /* Perform the iteration over a single file. */
859 f = lookup_name (file_spec);
860 if (f)
861 walk_wild_file (s, f, callback, data);
865 /* lang_for_each_statement walks the parse tree and calls the provided
866 function for each node. */
868 static void
869 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
870 lang_statement_union_type *s)
872 for (; s != NULL; s = s->header.next)
874 func (s);
876 switch (s->header.type)
878 case lang_constructors_statement_enum:
879 lang_for_each_statement_worker (func, constructor_list.head);
880 break;
881 case lang_output_section_statement_enum:
882 lang_for_each_statement_worker
883 (func, s->output_section_statement.children.head);
884 break;
885 case lang_wild_statement_enum:
886 lang_for_each_statement_worker (func,
887 s->wild_statement.children.head);
888 break;
889 case lang_group_statement_enum:
890 lang_for_each_statement_worker (func,
891 s->group_statement.children.head);
892 break;
893 case lang_data_statement_enum:
894 case lang_reloc_statement_enum:
895 case lang_object_symbols_statement_enum:
896 case lang_output_statement_enum:
897 case lang_target_statement_enum:
898 case lang_input_section_enum:
899 case lang_input_statement_enum:
900 case lang_assignment_statement_enum:
901 case lang_padding_statement_enum:
902 case lang_address_statement_enum:
903 case lang_fill_statement_enum:
904 case lang_insert_statement_enum:
905 break;
906 default:
907 FAIL ();
908 break;
913 void
914 lang_for_each_statement (void (*func) (lang_statement_union_type *))
916 lang_for_each_statement_worker (func, statement_list.head);
919 /*----------------------------------------------------------------------*/
921 void
922 lang_list_init (lang_statement_list_type *list)
924 list->head = NULL;
925 list->tail = &list->head;
928 void
929 push_stat_ptr (lang_statement_list_type *new_ptr)
931 if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
932 abort ();
933 *stat_save_ptr++ = stat_ptr;
934 stat_ptr = new_ptr;
937 void
938 pop_stat_ptr (void)
940 if (stat_save_ptr <= stat_save)
941 abort ();
942 stat_ptr = *--stat_save_ptr;
945 /* Build a new statement node for the parse tree. */
947 static lang_statement_union_type *
948 new_statement (enum statement_enum type,
949 size_t size,
950 lang_statement_list_type *list)
952 lang_statement_union_type *new;
954 new = stat_alloc (size);
955 new->header.type = type;
956 new->header.next = NULL;
957 lang_statement_append (list, new, &new->header.next);
958 return new;
961 /* Build a new input file node for the language. There are several
962 ways in which we treat an input file, eg, we only look at symbols,
963 or prefix it with a -l etc.
965 We can be supplied with requests for input files more than once;
966 they may, for example be split over several lines like foo.o(.text)
967 foo.o(.data) etc, so when asked for a file we check that we haven't
968 got it already so we don't duplicate the bfd. */
970 static lang_input_statement_type *
971 new_afile (const char *name,
972 lang_input_file_enum_type file_type,
973 const char *target,
974 bfd_boolean add_to_list)
976 lang_input_statement_type *p;
978 if (add_to_list)
979 p = new_stat (lang_input_statement, stat_ptr);
980 else
982 p = stat_alloc (sizeof (lang_input_statement_type));
983 p->header.type = lang_input_statement_enum;
984 p->header.next = NULL;
987 lang_has_input_file = TRUE;
988 p->target = target;
989 p->sysrooted = FALSE;
991 if (file_type == lang_input_file_is_l_enum
992 && name[0] == ':' && name[1] != '\0')
994 file_type = lang_input_file_is_search_file_enum;
995 name = name + 1;
998 switch (file_type)
1000 case lang_input_file_is_symbols_only_enum:
1001 p->filename = name;
1002 p->is_archive = FALSE;
1003 p->real = TRUE;
1004 p->local_sym_name = name;
1005 p->just_syms_flag = TRUE;
1006 p->search_dirs_flag = FALSE;
1007 break;
1008 case lang_input_file_is_fake_enum:
1009 p->filename = name;
1010 p->is_archive = FALSE;
1011 p->real = FALSE;
1012 p->local_sym_name = name;
1013 p->just_syms_flag = FALSE;
1014 p->search_dirs_flag = FALSE;
1015 break;
1016 case lang_input_file_is_l_enum:
1017 p->is_archive = TRUE;
1018 p->filename = name;
1019 p->real = TRUE;
1020 p->local_sym_name = concat ("-l", name, (const char *) NULL);
1021 p->just_syms_flag = FALSE;
1022 p->search_dirs_flag = TRUE;
1023 break;
1024 case lang_input_file_is_marker_enum:
1025 p->filename = name;
1026 p->is_archive = FALSE;
1027 p->real = FALSE;
1028 p->local_sym_name = name;
1029 p->just_syms_flag = FALSE;
1030 p->search_dirs_flag = TRUE;
1031 break;
1032 case lang_input_file_is_search_file_enum:
1033 p->sysrooted = ldlang_sysrooted_script;
1034 p->filename = name;
1035 p->is_archive = FALSE;
1036 p->real = TRUE;
1037 p->local_sym_name = name;
1038 p->just_syms_flag = FALSE;
1039 p->search_dirs_flag = TRUE;
1040 break;
1041 case lang_input_file_is_file_enum:
1042 p->filename = name;
1043 p->is_archive = FALSE;
1044 p->real = TRUE;
1045 p->local_sym_name = name;
1046 p->just_syms_flag = FALSE;
1047 p->search_dirs_flag = FALSE;
1048 break;
1049 default:
1050 FAIL ();
1052 p->the_bfd = NULL;
1053 p->next_real_file = NULL;
1054 p->next = NULL;
1055 p->dynamic = config.dynamic_link;
1056 p->add_needed = add_needed;
1057 p->as_needed = as_needed;
1058 p->whole_archive = whole_archive;
1059 p->loaded = FALSE;
1060 lang_statement_append (&input_file_chain,
1061 (lang_statement_union_type *) p,
1062 &p->next_real_file);
1063 return p;
1066 lang_input_statement_type *
1067 lang_add_input_file (const char *name,
1068 lang_input_file_enum_type file_type,
1069 const char *target)
1071 return new_afile (name, file_type, target, TRUE);
1074 struct out_section_hash_entry
1076 struct bfd_hash_entry root;
1077 lang_statement_union_type s;
1080 /* The hash table. */
1082 static struct bfd_hash_table output_section_statement_table;
1084 /* Support routines for the hash table used by lang_output_section_find,
1085 initialize the table, fill in an entry and remove the table. */
1087 static struct bfd_hash_entry *
1088 output_section_statement_newfunc (struct bfd_hash_entry *entry,
1089 struct bfd_hash_table *table,
1090 const char *string)
1092 lang_output_section_statement_type **nextp;
1093 struct out_section_hash_entry *ret;
1095 if (entry == NULL)
1097 entry = bfd_hash_allocate (table, sizeof (*ret));
1098 if (entry == NULL)
1099 return entry;
1102 entry = bfd_hash_newfunc (entry, table, string);
1103 if (entry == NULL)
1104 return entry;
1106 ret = (struct out_section_hash_entry *) entry;
1107 memset (&ret->s, 0, sizeof (ret->s));
1108 ret->s.header.type = lang_output_section_statement_enum;
1109 ret->s.output_section_statement.subsection_alignment = -1;
1110 ret->s.output_section_statement.section_alignment = -1;
1111 ret->s.output_section_statement.block_value = 1;
1112 lang_list_init (&ret->s.output_section_statement.children);
1113 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
1115 /* For every output section statement added to the list, except the
1116 first one, lang_output_section_statement.tail points to the "next"
1117 field of the last element of the list. */
1118 if (lang_output_section_statement.head != NULL)
1119 ret->s.output_section_statement.prev
1120 = ((lang_output_section_statement_type *)
1121 ((char *) lang_output_section_statement.tail
1122 - offsetof (lang_output_section_statement_type, next)));
1124 /* GCC's strict aliasing rules prevent us from just casting the
1125 address, so we store the pointer in a variable and cast that
1126 instead. */
1127 nextp = &ret->s.output_section_statement.next;
1128 lang_statement_append (&lang_output_section_statement,
1129 &ret->s,
1130 (lang_statement_union_type **) nextp);
1131 return &ret->root;
1134 static void
1135 output_section_statement_table_init (void)
1137 if (!bfd_hash_table_init_n (&output_section_statement_table,
1138 output_section_statement_newfunc,
1139 sizeof (struct out_section_hash_entry),
1140 61))
1141 einfo (_("%P%F: can not create hash table: %E\n"));
1144 static void
1145 output_section_statement_table_free (void)
1147 bfd_hash_table_free (&output_section_statement_table);
1150 /* Build enough state so that the parser can build its tree. */
1152 void
1153 lang_init (void)
1155 obstack_begin (&stat_obstack, 1000);
1157 stat_ptr = &statement_list;
1159 output_section_statement_table_init ();
1161 lang_list_init (stat_ptr);
1163 lang_list_init (&input_file_chain);
1164 lang_list_init (&lang_output_section_statement);
1165 lang_list_init (&file_chain);
1166 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
1167 NULL);
1168 abs_output_section =
1169 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, TRUE);
1171 abs_output_section->bfd_section = bfd_abs_section_ptr;
1173 /* The value "3" is ad-hoc, somewhat related to the expected number of
1174 DEFINED expressions in a linker script. For most default linker
1175 scripts, there are none. Why a hash table then? Well, it's somewhat
1176 simpler to re-use working machinery than using a linked list in terms
1177 of code-complexity here in ld, besides the initialization which just
1178 looks like other code here. */
1179 if (!bfd_hash_table_init_n (&lang_definedness_table,
1180 lang_definedness_newfunc,
1181 sizeof (struct lang_definedness_hash_entry),
1183 einfo (_("%P%F: can not create hash table: %E\n"));
1186 void
1187 lang_finish (void)
1189 output_section_statement_table_free ();
1192 /*----------------------------------------------------------------------
1193 A region is an area of memory declared with the
1194 MEMORY { name:org=exp, len=exp ... }
1195 syntax.
1197 We maintain a list of all the regions here.
1199 If no regions are specified in the script, then the default is used
1200 which is created when looked up to be the entire data space.
1202 If create is true we are creating a region inside a MEMORY block.
1203 In this case it is probably an error to create a region that has
1204 already been created. If we are not inside a MEMORY block it is
1205 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1206 and so we issue a warning.
1208 Each region has at least one name. The first name is either
1209 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1210 alias names to an existing region within a script with
1211 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1212 region. */
1214 static lang_memory_region_type *lang_memory_region_list;
1215 static lang_memory_region_type **lang_memory_region_list_tail
1216 = &lang_memory_region_list;
1218 lang_memory_region_type *
1219 lang_memory_region_lookup (const char *const name, bfd_boolean create)
1221 lang_memory_region_name *n;
1222 lang_memory_region_type *r;
1223 lang_memory_region_type *new;
1225 /* NAME is NULL for LMA memspecs if no region was specified. */
1226 if (name == NULL)
1227 return NULL;
1229 for (r = lang_memory_region_list; r != NULL; r = r->next)
1230 for (n = &r->name_list; n != NULL; n = n->next)
1231 if (strcmp (n->name, name) == 0)
1233 if (create)
1234 einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
1235 name);
1236 return r;
1239 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
1240 einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name);
1242 new = stat_alloc (sizeof (lang_memory_region_type));
1244 new->name_list.name = xstrdup (name);
1245 new->name_list.next = NULL;
1246 new->next = NULL;
1247 new->origin = 0;
1248 new->length = ~(bfd_size_type) 0;
1249 new->current = 0;
1250 new->last_os = NULL;
1251 new->flags = 0;
1252 new->not_flags = 0;
1253 new->had_full_message = FALSE;
1255 *lang_memory_region_list_tail = new;
1256 lang_memory_region_list_tail = &new->next;
1258 return new;
1261 void
1262 lang_memory_region_alias (const char * alias, const char * region_name)
1264 lang_memory_region_name * n;
1265 lang_memory_region_type * r;
1266 lang_memory_region_type * region;
1268 /* The default region must be unique. This ensures that it is not necessary
1269 to iterate through the name list if someone wants the check if a region is
1270 the default memory region. */
1271 if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
1272 || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
1273 einfo (_("%F%P:%S: error: alias for default memory region\n"));
1275 /* Look for the target region and check if the alias is not already
1276 in use. */
1277 region = NULL;
1278 for (r = lang_memory_region_list; r != NULL; r = r->next)
1279 for (n = &r->name_list; n != NULL; n = n->next)
1281 if (region == NULL && strcmp (n->name, region_name) == 0)
1282 region = r;
1283 if (strcmp (n->name, alias) == 0)
1284 einfo (_("%F%P:%S: error: redefinition of memory region "
1285 "alias `%s'\n"),
1286 alias);
1289 /* Check if the target region exists. */
1290 if (region == NULL)
1291 einfo (_("%F%P:%S: error: memory region `%s' "
1292 "for alias `%s' does not exist\n"),
1293 region_name,
1294 alias);
1296 /* Add alias to region name list. */
1297 n = stat_alloc (sizeof (lang_memory_region_name));
1298 n->name = xstrdup (alias);
1299 n->next = region->name_list.next;
1300 region->name_list.next = n;
1303 static lang_memory_region_type *
1304 lang_memory_default (asection * section)
1306 lang_memory_region_type *p;
1308 flagword sec_flags = section->flags;
1310 /* Override SEC_DATA to mean a writable section. */
1311 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
1312 sec_flags |= SEC_DATA;
1314 for (p = lang_memory_region_list; p != NULL; p = p->next)
1316 if ((p->flags & sec_flags) != 0
1317 && (p->not_flags & sec_flags) == 0)
1319 return p;
1322 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
1325 lang_output_section_statement_type *
1326 lang_output_section_statement_lookup (const char *const name,
1327 int constraint,
1328 bfd_boolean create)
1330 struct out_section_hash_entry *entry;
1332 entry = ((struct out_section_hash_entry *)
1333 bfd_hash_lookup (&output_section_statement_table, name,
1334 create, FALSE));
1335 if (entry == NULL)
1337 if (create)
1338 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1339 return NULL;
1342 if (entry->s.output_section_statement.name != NULL)
1344 /* We have a section of this name, but it might not have the correct
1345 constraint. */
1346 struct out_section_hash_entry *last_ent;
1347 unsigned long hash = entry->root.hash;
1349 if (create && constraint == SPECIAL)
1350 /* Not traversing to the end reverses the order of the second
1351 and subsequent SPECIAL sections in the hash table chain,
1352 but that shouldn't matter. */
1353 last_ent = entry;
1354 else
1357 if (entry->s.output_section_statement.constraint >= 0
1358 && (constraint == 0
1359 || (constraint
1360 == entry->s.output_section_statement.constraint)))
1361 return &entry->s.output_section_statement;
1362 last_ent = entry;
1363 entry = (struct out_section_hash_entry *) entry->root.next;
1365 while (entry != NULL
1366 && entry->root.hash == hash
1367 && strcmp (name, entry->s.output_section_statement.name) == 0);
1369 if (!create)
1370 return NULL;
1372 entry
1373 = ((struct out_section_hash_entry *)
1374 output_section_statement_newfunc (NULL,
1375 &output_section_statement_table,
1376 name));
1377 if (entry == NULL)
1379 einfo (_("%P%F: failed creating section `%s': %E\n"), name);
1380 return NULL;
1382 entry->root = last_ent->root;
1383 last_ent->root.next = &entry->root;
1386 entry->s.output_section_statement.name = name;
1387 entry->s.output_section_statement.constraint = constraint;
1388 return &entry->s.output_section_statement;
1391 /* A variant of lang_output_section_find used by place_orphan.
1392 Returns the output statement that should precede a new output
1393 statement for SEC. If an exact match is found on certain flags,
1394 sets *EXACT too. */
1396 lang_output_section_statement_type *
1397 lang_output_section_find_by_flags (const asection *sec,
1398 lang_output_section_statement_type **exact,
1399 lang_match_sec_type_func match_type)
1401 lang_output_section_statement_type *first, *look, *found;
1402 flagword flags;
1404 /* We know the first statement on this list is *ABS*. May as well
1405 skip it. */
1406 first = &lang_output_section_statement.head->output_section_statement;
1407 first = first->next;
1409 /* First try for an exact match. */
1410 found = NULL;
1411 for (look = first; look; look = look->next)
1413 flags = look->flags;
1414 if (look->bfd_section != NULL)
1416 flags = look->bfd_section->flags;
1417 if (match_type && !match_type (link_info.output_bfd,
1418 look->bfd_section,
1419 sec->owner, sec))
1420 continue;
1422 flags ^= sec->flags;
1423 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
1424 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1425 found = look;
1427 if (found != NULL)
1429 if (exact != NULL)
1430 *exact = found;
1431 return found;
1434 if ((sec->flags & SEC_CODE) != 0
1435 && (sec->flags & SEC_ALLOC) != 0)
1437 /* Try for a rw code section. */
1438 for (look = first; look; look = look->next)
1440 flags = look->flags;
1441 if (look->bfd_section != NULL)
1443 flags = look->bfd_section->flags;
1444 if (match_type && !match_type (link_info.output_bfd,
1445 look->bfd_section,
1446 sec->owner, sec))
1447 continue;
1449 flags ^= sec->flags;
1450 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1451 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1452 found = look;
1455 else if ((sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL)) != 0
1456 && (sec->flags & SEC_ALLOC) != 0)
1458 /* .rodata can go after .text, .sdata2 after .rodata. */
1459 for (look = first; look; look = look->next)
1461 flags = look->flags;
1462 if (look->bfd_section != NULL)
1464 flags = look->bfd_section->flags;
1465 if (match_type && !match_type (link_info.output_bfd,
1466 look->bfd_section,
1467 sec->owner, sec))
1468 continue;
1470 flags ^= sec->flags;
1471 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1472 | SEC_READONLY))
1473 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1474 found = look;
1477 else if ((sec->flags & SEC_SMALL_DATA) != 0
1478 && (sec->flags & SEC_ALLOC) != 0)
1480 /* .sdata goes after .data, .sbss after .sdata. */
1481 for (look = first; look; look = look->next)
1483 flags = look->flags;
1484 if (look->bfd_section != NULL)
1486 flags = look->bfd_section->flags;
1487 if (match_type && !match_type (link_info.output_bfd,
1488 look->bfd_section,
1489 sec->owner, sec))
1490 continue;
1492 flags ^= sec->flags;
1493 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1494 | SEC_THREAD_LOCAL))
1495 || ((look->flags & SEC_SMALL_DATA)
1496 && !(sec->flags & SEC_HAS_CONTENTS)))
1497 found = look;
1500 else if ((sec->flags & SEC_HAS_CONTENTS) != 0
1501 && (sec->flags & SEC_ALLOC) != 0)
1503 /* .data goes after .rodata. */
1504 for (look = first; look; look = look->next)
1506 flags = look->flags;
1507 if (look->bfd_section != NULL)
1509 flags = look->bfd_section->flags;
1510 if (match_type && !match_type (link_info.output_bfd,
1511 look->bfd_section,
1512 sec->owner, sec))
1513 continue;
1515 flags ^= sec->flags;
1516 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
1517 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
1518 found = look;
1521 else if ((sec->flags & SEC_ALLOC) != 0)
1523 /* .bss goes after any other alloc section. */
1524 for (look = first; look; look = look->next)
1526 flags = look->flags;
1527 if (look->bfd_section != NULL)
1529 flags = look->bfd_section->flags;
1530 if (match_type && !match_type (link_info.output_bfd,
1531 look->bfd_section,
1532 sec->owner, sec))
1533 continue;
1535 flags ^= sec->flags;
1536 if (!(flags & SEC_ALLOC))
1537 found = look;
1540 else
1542 /* non-alloc go last. */
1543 for (look = first; look; look = look->next)
1545 flags = look->flags;
1546 if (look->bfd_section != NULL)
1547 flags = look->bfd_section->flags;
1548 flags ^= sec->flags;
1549 if (!(flags & SEC_DEBUGGING))
1550 found = look;
1552 return found;
1555 if (found || !match_type)
1556 return found;
1558 return lang_output_section_find_by_flags (sec, NULL, NULL);
1561 /* Find the last output section before given output statement.
1562 Used by place_orphan. */
1564 static asection *
1565 output_prev_sec_find (lang_output_section_statement_type *os)
1567 lang_output_section_statement_type *lookup;
1569 for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
1571 if (lookup->constraint < 0)
1572 continue;
1574 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1575 return lookup->bfd_section;
1578 return NULL;
1581 /* Look for a suitable place for a new output section statement. The
1582 idea is to skip over anything that might be inside a SECTIONS {}
1583 statement in a script, before we find another output section
1584 statement. Assignments to "dot" before an output section statement
1585 are assumed to belong to it. An exception to this rule is made for
1586 the first assignment to dot, otherwise we might put an orphan
1587 before . = . + SIZEOF_HEADERS or similar assignments that set the
1588 initial address. */
1590 static lang_statement_union_type **
1591 insert_os_after (lang_output_section_statement_type *after)
1593 lang_statement_union_type **where;
1594 lang_statement_union_type **assign = NULL;
1595 bfd_boolean ignore_first;
1597 ignore_first
1598 = after == &lang_output_section_statement.head->output_section_statement;
1600 for (where = &after->header.next;
1601 *where != NULL;
1602 where = &(*where)->header.next)
1604 switch ((*where)->header.type)
1606 case lang_assignment_statement_enum:
1607 if (assign == NULL)
1609 lang_assignment_statement_type *ass;
1611 ass = &(*where)->assignment_statement;
1612 if (ass->exp->type.node_class != etree_assert
1613 && ass->exp->assign.dst[0] == '.'
1614 && ass->exp->assign.dst[1] == 0
1615 && !ignore_first)
1616 assign = where;
1618 ignore_first = FALSE;
1619 continue;
1620 case lang_wild_statement_enum:
1621 case lang_input_section_enum:
1622 case lang_object_symbols_statement_enum:
1623 case lang_fill_statement_enum:
1624 case lang_data_statement_enum:
1625 case lang_reloc_statement_enum:
1626 case lang_padding_statement_enum:
1627 case lang_constructors_statement_enum:
1628 assign = NULL;
1629 continue;
1630 case lang_output_section_statement_enum:
1631 if (assign != NULL)
1632 where = assign;
1633 break;
1634 case lang_input_statement_enum:
1635 case lang_address_statement_enum:
1636 case lang_target_statement_enum:
1637 case lang_output_statement_enum:
1638 case lang_group_statement_enum:
1639 case lang_insert_statement_enum:
1640 continue;
1642 break;
1645 return where;
1648 lang_output_section_statement_type *
1649 lang_insert_orphan (asection *s,
1650 const char *secname,
1651 int constraint,
1652 lang_output_section_statement_type *after,
1653 struct orphan_save *place,
1654 etree_type *address,
1655 lang_statement_list_type *add_child)
1657 lang_statement_list_type add;
1658 const char *ps;
1659 lang_output_section_statement_type *os;
1660 lang_output_section_statement_type **os_tail;
1662 /* If we have found an appropriate place for the output section
1663 statements for this orphan, add them to our own private list,
1664 inserting them later into the global statement list. */
1665 if (after != NULL)
1667 lang_list_init (&add);
1668 push_stat_ptr (&add);
1671 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1672 address = exp_intop (0);
1674 os_tail = ((lang_output_section_statement_type **)
1675 lang_output_section_statement.tail);
1676 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
1677 NULL, constraint);
1679 ps = NULL;
1680 if (config.build_constructors && *os_tail == os)
1682 /* If the name of the section is representable in C, then create
1683 symbols to mark the start and the end of the section. */
1684 for (ps = secname; *ps != '\0'; ps++)
1685 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
1686 break;
1687 if (*ps == '\0')
1689 char *symname;
1690 etree_type *e_align;
1692 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
1693 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1694 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
1695 e_align = exp_unop (ALIGN_K,
1696 exp_intop ((bfd_vma) 1 << s->alignment_power));
1697 lang_add_assignment (exp_assop ('=', ".", e_align));
1698 lang_add_assignment (exp_provide (symname,
1699 exp_unop (ABSOLUTE,
1700 exp_nameop (NAME, ".")),
1701 FALSE));
1705 if (add_child == NULL)
1706 add_child = &os->children;
1707 lang_add_section (add_child, s, os);
1709 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
1711 if (ps != NULL && *ps == '\0')
1713 char *symname;
1715 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
1716 symname[0] = bfd_get_symbol_leading_char (link_info.output_bfd);
1717 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
1718 lang_add_assignment (exp_provide (symname,
1719 exp_nameop (NAME, "."),
1720 FALSE));
1723 /* Restore the global list pointer. */
1724 if (after != NULL)
1725 pop_stat_ptr ();
1727 if (after != NULL && os->bfd_section != NULL)
1729 asection *snew, *as;
1731 snew = os->bfd_section;
1733 /* Shuffle the bfd section list to make the output file look
1734 neater. This is really only cosmetic. */
1735 if (place->section == NULL
1736 && after != (&lang_output_section_statement.head
1737 ->output_section_statement))
1739 asection *bfd_section = after->bfd_section;
1741 /* If the output statement hasn't been used to place any input
1742 sections (and thus doesn't have an output bfd_section),
1743 look for the closest prior output statement having an
1744 output section. */
1745 if (bfd_section == NULL)
1746 bfd_section = output_prev_sec_find (after);
1748 if (bfd_section != NULL && bfd_section != snew)
1749 place->section = &bfd_section->next;
1752 if (place->section == NULL)
1753 place->section = &link_info.output_bfd->sections;
1755 as = *place->section;
1757 if (!as)
1759 /* Put the section at the end of the list. */
1761 /* Unlink the section. */
1762 bfd_section_list_remove (link_info.output_bfd, snew);
1764 /* Now tack it back on in the right place. */
1765 bfd_section_list_append (link_info.output_bfd, snew);
1767 else if (as != snew && as->prev != snew)
1769 /* Unlink the section. */
1770 bfd_section_list_remove (link_info.output_bfd, snew);
1772 /* Now tack it back on in the right place. */
1773 bfd_section_list_insert_before (link_info.output_bfd, as, snew);
1776 /* Save the end of this list. Further ophans of this type will
1777 follow the one we've just added. */
1778 place->section = &snew->next;
1780 /* The following is non-cosmetic. We try to put the output
1781 statements in some sort of reasonable order here, because they
1782 determine the final load addresses of the orphan sections.
1783 In addition, placing output statements in the wrong order may
1784 require extra segments. For instance, given a typical
1785 situation of all read-only sections placed in one segment and
1786 following that a segment containing all the read-write
1787 sections, we wouldn't want to place an orphan read/write
1788 section before or amongst the read-only ones. */
1789 if (add.head != NULL)
1791 lang_output_section_statement_type *newly_added_os;
1793 if (place->stmt == NULL)
1795 lang_statement_union_type **where = insert_os_after (after);
1797 *add.tail = *where;
1798 *where = add.head;
1800 place->os_tail = &after->next;
1802 else
1804 /* Put it after the last orphan statement we added. */
1805 *add.tail = *place->stmt;
1806 *place->stmt = add.head;
1809 /* Fix the global list pointer if we happened to tack our
1810 new list at the tail. */
1811 if (*stat_ptr->tail == add.head)
1812 stat_ptr->tail = add.tail;
1814 /* Save the end of this list. */
1815 place->stmt = add.tail;
1817 /* Do the same for the list of output section statements. */
1818 newly_added_os = *os_tail;
1819 *os_tail = NULL;
1820 newly_added_os->prev = (lang_output_section_statement_type *)
1821 ((char *) place->os_tail
1822 - offsetof (lang_output_section_statement_type, next));
1823 newly_added_os->next = *place->os_tail;
1824 if (newly_added_os->next != NULL)
1825 newly_added_os->next->prev = newly_added_os;
1826 *place->os_tail = newly_added_os;
1827 place->os_tail = &newly_added_os->next;
1829 /* Fixing the global list pointer here is a little different.
1830 We added to the list in lang_enter_output_section_statement,
1831 trimmed off the new output_section_statment above when
1832 assigning *os_tail = NULL, but possibly added it back in
1833 the same place when assigning *place->os_tail. */
1834 if (*os_tail == NULL)
1835 lang_output_section_statement.tail
1836 = (lang_statement_union_type **) os_tail;
1839 return os;
1842 static void
1843 lang_map_flags (flagword flag)
1845 if (flag & SEC_ALLOC)
1846 minfo ("a");
1848 if (flag & SEC_CODE)
1849 minfo ("x");
1851 if (flag & SEC_READONLY)
1852 minfo ("r");
1854 if (flag & SEC_DATA)
1855 minfo ("w");
1857 if (flag & SEC_LOAD)
1858 minfo ("l");
1861 void
1862 lang_map (void)
1864 lang_memory_region_type *m;
1865 bfd_boolean dis_header_printed = FALSE;
1866 bfd *p;
1868 LANG_FOR_EACH_INPUT_STATEMENT (file)
1870 asection *s;
1872 if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
1873 || file->just_syms_flag)
1874 continue;
1876 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1877 if ((s->output_section == NULL
1878 || s->output_section->owner != link_info.output_bfd)
1879 && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
1881 if (! dis_header_printed)
1883 fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
1884 dis_header_printed = TRUE;
1887 print_input_section (s);
1891 minfo (_("\nMemory Configuration\n\n"));
1892 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1893 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1895 for (m = lang_memory_region_list; m != NULL; m = m->next)
1897 char buf[100];
1898 int len;
1900 fprintf (config.map_file, "%-16s ", m->name_list.name);
1902 sprintf_vma (buf, m->origin);
1903 minfo ("0x%s ", buf);
1904 len = strlen (buf);
1905 while (len < 16)
1907 print_space ();
1908 ++len;
1911 minfo ("0x%V", m->length);
1912 if (m->flags || m->not_flags)
1914 #ifndef BFD64
1915 minfo (" ");
1916 #endif
1917 if (m->flags)
1919 print_space ();
1920 lang_map_flags (m->flags);
1923 if (m->not_flags)
1925 minfo (" !");
1926 lang_map_flags (m->not_flags);
1930 print_nl ();
1933 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1935 if (! link_info.reduce_memory_overheads)
1937 obstack_begin (&map_obstack, 1000);
1938 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1939 bfd_map_over_sections (p, init_map_userdata, 0);
1940 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1942 lang_statement_iteration ++;
1943 print_statements ();
1946 static void
1947 init_map_userdata (bfd *abfd ATTRIBUTE_UNUSED,
1948 asection *sec,
1949 void *data ATTRIBUTE_UNUSED)
1951 fat_section_userdata_type *new_data
1952 = ((fat_section_userdata_type *) (stat_alloc
1953 (sizeof (fat_section_userdata_type))));
1955 ASSERT (get_userdata (sec) == NULL);
1956 get_userdata (sec) = new_data;
1957 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1960 static bfd_boolean
1961 sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
1962 void *info ATTRIBUTE_UNUSED)
1964 if (hash_entry->type == bfd_link_hash_defined
1965 || hash_entry->type == bfd_link_hash_defweak)
1967 struct fat_user_section_struct *ud;
1968 struct map_symbol_def *def;
1970 ud = get_userdata (hash_entry->u.def.section);
1971 if (! ud)
1973 /* ??? What do we have to do to initialize this beforehand? */
1974 /* The first time we get here is bfd_abs_section... */
1975 init_map_userdata (0, hash_entry->u.def.section, 0);
1976 ud = get_userdata (hash_entry->u.def.section);
1978 else if (!ud->map_symbol_def_tail)
1979 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1981 def = obstack_alloc (&map_obstack, sizeof *def);
1982 def->entry = hash_entry;
1983 *(ud->map_symbol_def_tail) = def;
1984 ud->map_symbol_def_tail = &def->next;
1986 return TRUE;
1989 /* Initialize an output section. */
1991 static void
1992 init_os (lang_output_section_statement_type *s, asection *isec,
1993 flagword flags)
1995 if (s->bfd_section != NULL)
1996 return;
1998 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1999 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
2001 if (s->constraint != SPECIAL)
2002 s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
2003 if (s->bfd_section == NULL)
2004 s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
2005 s->name, flags);
2006 if (s->bfd_section == NULL)
2008 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
2009 link_info.output_bfd->xvec->name, s->name);
2011 s->bfd_section->output_section = s->bfd_section;
2012 s->bfd_section->output_offset = 0;
2014 if (!link_info.reduce_memory_overheads)
2016 fat_section_userdata_type *new
2017 = stat_alloc (sizeof (fat_section_userdata_type));
2018 memset (new, 0, sizeof (fat_section_userdata_type));
2019 get_userdata (s->bfd_section) = new;
2022 /* If there is a base address, make sure that any sections it might
2023 mention are initialized. */
2024 if (s->addr_tree != NULL)
2025 exp_init_os (s->addr_tree);
2027 if (s->load_base != NULL)
2028 exp_init_os (s->load_base);
2030 /* If supplied an alignment, set it. */
2031 if (s->section_alignment != -1)
2032 s->bfd_section->alignment_power = s->section_alignment;
2034 if (isec)
2035 bfd_init_private_section_data (isec->owner, isec,
2036 link_info.output_bfd, s->bfd_section,
2037 &link_info);
2040 /* Make sure that all output sections mentioned in an expression are
2041 initialized. */
2043 static void
2044 exp_init_os (etree_type *exp)
2046 switch (exp->type.node_class)
2048 case etree_assign:
2049 case etree_provide:
2050 exp_init_os (exp->assign.src);
2051 break;
2053 case etree_binary:
2054 exp_init_os (exp->binary.lhs);
2055 exp_init_os (exp->binary.rhs);
2056 break;
2058 case etree_trinary:
2059 exp_init_os (exp->trinary.cond);
2060 exp_init_os (exp->trinary.lhs);
2061 exp_init_os (exp->trinary.rhs);
2062 break;
2064 case etree_assert:
2065 exp_init_os (exp->assert_s.child);
2066 break;
2068 case etree_unary:
2069 exp_init_os (exp->unary.child);
2070 break;
2072 case etree_name:
2073 switch (exp->type.node_code)
2075 case ADDR:
2076 case LOADADDR:
2077 case SIZEOF:
2079 lang_output_section_statement_type *os;
2081 os = lang_output_section_find (exp->name.name);
2082 if (os != NULL && os->bfd_section == NULL)
2083 init_os (os, NULL, 0);
2086 break;
2088 default:
2089 break;
2093 static void
2094 section_already_linked (bfd *abfd, asection *sec, void *data)
2096 lang_input_statement_type *entry = data;
2098 /* If we are only reading symbols from this object, then we want to
2099 discard all sections. */
2100 if (entry->just_syms_flag)
2102 bfd_link_just_syms (abfd, sec, &link_info);
2103 return;
2106 if (!(abfd->flags & DYNAMIC))
2107 bfd_section_already_linked (abfd, sec, &link_info);
2110 /* The wild routines.
2112 These expand statements like *(.text) and foo.o to a list of
2113 explicit actions, like foo.o(.text), bar.o(.text) and
2114 foo.o(.text, .data). */
2116 /* Add SECTION to the output section OUTPUT. Do this by creating a
2117 lang_input_section statement which is placed at PTR. FILE is the
2118 input file which holds SECTION. */
2120 void
2121 lang_add_section (lang_statement_list_type *ptr,
2122 asection *section,
2123 lang_output_section_statement_type *output)
2125 flagword flags = section->flags;
2126 bfd_boolean discard;
2128 /* Discard sections marked with SEC_EXCLUDE. */
2129 discard = (flags & SEC_EXCLUDE) != 0;
2131 /* Discard input sections which are assigned to a section named
2132 DISCARD_SECTION_NAME. */
2133 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
2134 discard = TRUE;
2136 /* Discard debugging sections if we are stripping debugging
2137 information. */
2138 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
2139 && (flags & SEC_DEBUGGING) != 0)
2140 discard = TRUE;
2142 if (discard)
2144 if (section->output_section == NULL)
2146 /* This prevents future calls from assigning this section. */
2147 section->output_section = bfd_abs_section_ptr;
2149 return;
2152 if (section->output_section == NULL)
2154 bfd_boolean first;
2155 lang_input_section_type *new;
2156 flagword flags;
2158 flags = section->flags;
2160 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2161 to an output section, because we want to be able to include a
2162 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2163 section (I don't know why we want to do this, but we do).
2164 build_link_order in ldwrite.c handles this case by turning
2165 the embedded SEC_NEVER_LOAD section into a fill. */
2167 flags &= ~ SEC_NEVER_LOAD;
2169 switch (output->sectype)
2171 case normal_section:
2172 case overlay_section:
2173 break;
2174 case noalloc_section:
2175 flags &= ~SEC_ALLOC;
2176 break;
2177 case noload_section:
2178 flags &= ~SEC_LOAD;
2179 flags |= SEC_NEVER_LOAD;
2180 break;
2183 if (output->bfd_section == NULL)
2184 init_os (output, section, flags);
2186 first = ! output->bfd_section->linker_has_input;
2187 output->bfd_section->linker_has_input = 1;
2189 if (!link_info.relocatable
2190 && !stripped_excluded_sections)
2192 asection *s = output->bfd_section->map_tail.s;
2193 output->bfd_section->map_tail.s = section;
2194 section->map_head.s = NULL;
2195 section->map_tail.s = s;
2196 if (s != NULL)
2197 s->map_head.s = section;
2198 else
2199 output->bfd_section->map_head.s = section;
2202 /* Add a section reference to the list. */
2203 new = new_stat (lang_input_section, ptr);
2205 new->section = section;
2206 section->output_section = output->bfd_section;
2208 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2209 already been processed. One reason to do this is that on pe
2210 format targets, .text$foo sections go into .text and it's odd
2211 to see .text with SEC_LINK_ONCE set. */
2213 if (! link_info.relocatable)
2214 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
2216 /* If this is not the first input section, and the SEC_READONLY
2217 flag is not currently set, then don't set it just because the
2218 input section has it set. */
2220 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
2221 flags &= ~ SEC_READONLY;
2223 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2224 if (! first
2225 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
2226 != (flags & (SEC_MERGE | SEC_STRINGS))
2227 || ((flags & SEC_MERGE)
2228 && output->bfd_section->entsize != section->entsize)))
2230 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
2231 flags &= ~ (SEC_MERGE | SEC_STRINGS);
2234 output->bfd_section->flags |= flags;
2236 if (flags & SEC_MERGE)
2237 output->bfd_section->entsize = section->entsize;
2239 /* If SEC_READONLY is not set in the input section, then clear
2240 it from the output section. */
2241 if ((section->flags & SEC_READONLY) == 0)
2242 output->bfd_section->flags &= ~SEC_READONLY;
2244 /* Copy over SEC_SMALL_DATA. */
2245 if (section->flags & SEC_SMALL_DATA)
2246 output->bfd_section->flags |= SEC_SMALL_DATA;
2248 if (section->alignment_power > output->bfd_section->alignment_power)
2249 output->bfd_section->alignment_power = section->alignment_power;
2251 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
2252 && (section->flags & SEC_TIC54X_BLOCK) != 0)
2254 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
2255 /* FIXME: This value should really be obtained from the bfd... */
2256 output->block_value = 128;
2261 /* Handle wildcard sorting. This returns the lang_input_section which
2262 should follow the one we are going to create for SECTION and FILE,
2263 based on the sorting requirements of WILD. It returns NULL if the
2264 new section should just go at the end of the current list. */
2266 static lang_statement_union_type *
2267 wild_sort (lang_wild_statement_type *wild,
2268 struct wildcard_list *sec,
2269 lang_input_statement_type *file,
2270 asection *section)
2272 const char *section_name;
2273 lang_statement_union_type *l;
2275 if (!wild->filenames_sorted
2276 && (sec == NULL || sec->spec.sorted == none))
2277 return NULL;
2279 section_name = bfd_get_section_name (file->the_bfd, section);
2280 for (l = wild->children.head; l != NULL; l = l->header.next)
2282 lang_input_section_type *ls;
2284 if (l->header.type != lang_input_section_enum)
2285 continue;
2286 ls = &l->input_section;
2288 /* Sorting by filename takes precedence over sorting by section
2289 name. */
2291 if (wild->filenames_sorted)
2293 const char *fn, *ln;
2294 bfd_boolean fa, la;
2295 int i;
2297 /* The PE support for the .idata section as generated by
2298 dlltool assumes that files will be sorted by the name of
2299 the archive and then the name of the file within the
2300 archive. */
2302 if (file->the_bfd != NULL
2303 && bfd_my_archive (file->the_bfd) != NULL)
2305 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
2306 fa = TRUE;
2308 else
2310 fn = file->filename;
2311 fa = FALSE;
2314 if (bfd_my_archive (ls->section->owner) != NULL)
2316 ln = bfd_get_filename (bfd_my_archive (ls->section->owner));
2317 la = TRUE;
2319 else
2321 ln = ls->section->owner->filename;
2322 la = FALSE;
2325 i = strcmp (fn, ln);
2326 if (i > 0)
2327 continue;
2328 else if (i < 0)
2329 break;
2331 if (fa || la)
2333 if (fa)
2334 fn = file->filename;
2335 if (la)
2336 ln = ls->section->owner->filename;
2338 i = strcmp (fn, ln);
2339 if (i > 0)
2340 continue;
2341 else if (i < 0)
2342 break;
2346 /* Here either the files are not sorted by name, or we are
2347 looking at the sections for this file. */
2349 if (sec != NULL && sec->spec.sorted != none)
2350 if (compare_section (sec->spec.sorted, section, ls->section) < 0)
2351 break;
2354 return l;
2357 /* Expand a wild statement for a particular FILE. SECTION may be
2358 NULL, in which case it is a wild card. */
2360 static void
2361 output_section_callback (lang_wild_statement_type *ptr,
2362 struct wildcard_list *sec,
2363 asection *section,
2364 lang_input_statement_type *file,
2365 void *output)
2367 lang_statement_union_type *before;
2369 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2370 if (unique_section_p (section))
2371 return;
2373 before = wild_sort (ptr, sec, file, section);
2375 /* Here BEFORE points to the lang_input_section which
2376 should follow the one we are about to add. If BEFORE
2377 is NULL, then the section should just go at the end
2378 of the current list. */
2380 if (before == NULL)
2381 lang_add_section (&ptr->children, section,
2382 (lang_output_section_statement_type *) output);
2383 else
2385 lang_statement_list_type list;
2386 lang_statement_union_type **pp;
2388 lang_list_init (&list);
2389 lang_add_section (&list, section,
2390 (lang_output_section_statement_type *) output);
2392 /* If we are discarding the section, LIST.HEAD will
2393 be NULL. */
2394 if (list.head != NULL)
2396 ASSERT (list.head->header.next == NULL);
2398 for (pp = &ptr->children.head;
2399 *pp != before;
2400 pp = &(*pp)->header.next)
2401 ASSERT (*pp != NULL);
2403 list.head->header.next = *pp;
2404 *pp = list.head;
2409 /* Check if all sections in a wild statement for a particular FILE
2410 are readonly. */
2412 static void
2413 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
2414 struct wildcard_list *sec ATTRIBUTE_UNUSED,
2415 asection *section,
2416 lang_input_statement_type *file ATTRIBUTE_UNUSED,
2417 void *data)
2419 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2420 if (unique_section_p (section))
2421 return;
2423 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
2424 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
2427 /* This is passed a file name which must have been seen already and
2428 added to the statement tree. We will see if it has been opened
2429 already and had its symbols read. If not then we'll read it. */
2431 static lang_input_statement_type *
2432 lookup_name (const char *name)
2434 lang_input_statement_type *search;
2436 for (search = (lang_input_statement_type *) input_file_chain.head;
2437 search != NULL;
2438 search = (lang_input_statement_type *) search->next_real_file)
2440 /* Use the local_sym_name as the name of the file that has
2441 already been loaded as filename might have been transformed
2442 via the search directory lookup mechanism. */
2443 const char *filename = search->local_sym_name;
2445 if (filename != NULL
2446 && strcmp (filename, name) == 0)
2447 break;
2450 if (search == NULL)
2451 search = new_afile (name, lang_input_file_is_search_file_enum,
2452 default_target, FALSE);
2454 /* If we have already added this file, or this file is not real
2455 don't add this file. */
2456 if (search->loaded || !search->real)
2457 return search;
2459 if (! load_symbols (search, NULL))
2460 return NULL;
2462 return search;
2465 /* Save LIST as a list of libraries whose symbols should not be exported. */
2467 struct excluded_lib
2469 char *name;
2470 struct excluded_lib *next;
2472 static struct excluded_lib *excluded_libs;
2474 void
2475 add_excluded_libs (const char *list)
2477 const char *p = list, *end;
2479 while (*p != '\0')
2481 struct excluded_lib *entry;
2482 end = strpbrk (p, ",:");
2483 if (end == NULL)
2484 end = p + strlen (p);
2485 entry = xmalloc (sizeof (*entry));
2486 entry->next = excluded_libs;
2487 entry->name = xmalloc (end - p + 1);
2488 memcpy (entry->name, p, end - p);
2489 entry->name[end - p] = '\0';
2490 excluded_libs = entry;
2491 if (*end == '\0')
2492 break;
2493 p = end + 1;
2497 static void
2498 check_excluded_libs (bfd *abfd)
2500 struct excluded_lib *lib = excluded_libs;
2502 while (lib)
2504 int len = strlen (lib->name);
2505 const char *filename = lbasename (abfd->filename);
2507 if (strcmp (lib->name, "ALL") == 0)
2509 abfd->no_export = TRUE;
2510 return;
2513 if (strncmp (lib->name, filename, len) == 0
2514 && (filename[len] == '\0'
2515 || (filename[len] == '.' && filename[len + 1] == 'a'
2516 && filename[len + 2] == '\0')))
2518 abfd->no_export = TRUE;
2519 return;
2522 lib = lib->next;
2526 /* Get the symbols for an input file. */
2528 bfd_boolean
2529 load_symbols (lang_input_statement_type *entry,
2530 lang_statement_list_type *place)
2532 char **matching;
2534 if (entry->loaded)
2535 return TRUE;
2537 ldfile_open_file (entry);
2539 if (! bfd_check_format (entry->the_bfd, bfd_archive)
2540 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
2542 bfd_error_type err;
2543 bfd_boolean save_ldlang_sysrooted_script;
2544 bfd_boolean save_as_needed, save_add_needed;
2546 err = bfd_get_error ();
2548 /* See if the emulation has some special knowledge. */
2549 if (ldemul_unrecognized_file (entry))
2550 return TRUE;
2552 if (err == bfd_error_file_ambiguously_recognized)
2554 char **p;
2556 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
2557 einfo (_("%B: matching formats:"), entry->the_bfd);
2558 for (p = matching; *p != NULL; p++)
2559 einfo (" %s", *p);
2560 einfo ("%F\n");
2562 else if (err != bfd_error_file_not_recognized
2563 || place == NULL)
2564 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
2566 bfd_close (entry->the_bfd);
2567 entry->the_bfd = NULL;
2569 /* Try to interpret the file as a linker script. */
2570 ldfile_open_command_file (entry->filename);
2572 push_stat_ptr (place);
2573 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
2574 ldlang_sysrooted_script = entry->sysrooted;
2575 save_as_needed = as_needed;
2576 as_needed = entry->as_needed;
2577 save_add_needed = add_needed;
2578 add_needed = entry->add_needed;
2580 ldfile_assumed_script = TRUE;
2581 parser_input = input_script;
2582 /* We want to use the same -Bdynamic/-Bstatic as the one for
2583 ENTRY. */
2584 config.dynamic_link = entry->dynamic;
2585 yyparse ();
2586 ldfile_assumed_script = FALSE;
2588 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
2589 as_needed = save_as_needed;
2590 add_needed = save_add_needed;
2591 pop_stat_ptr ();
2593 return TRUE;
2596 if (ldemul_recognized_file (entry))
2597 return TRUE;
2599 /* We don't call ldlang_add_file for an archive. Instead, the
2600 add_symbols entry point will call ldlang_add_file, via the
2601 add_archive_element callback, for each element of the archive
2602 which is used. */
2603 switch (bfd_get_format (entry->the_bfd))
2605 default:
2606 break;
2608 case bfd_object:
2609 ldlang_add_file (entry);
2610 if (trace_files || trace_file_tries)
2611 info_msg ("%I\n", entry);
2612 break;
2614 case bfd_archive:
2615 check_excluded_libs (entry->the_bfd);
2617 if (entry->whole_archive)
2619 bfd *member = NULL;
2620 bfd_boolean loaded = TRUE;
2622 for (;;)
2624 member = bfd_openr_next_archived_file (entry->the_bfd, member);
2626 if (member == NULL)
2627 break;
2629 if (! bfd_check_format (member, bfd_object))
2631 einfo (_("%F%B: member %B in archive is not an object\n"),
2632 entry->the_bfd, member);
2633 loaded = FALSE;
2636 if (! ((*link_info.callbacks->add_archive_element)
2637 (&link_info, member, "--whole-archive")))
2638 abort ();
2640 if (! bfd_link_add_symbols (member, &link_info))
2642 einfo (_("%F%B: could not read symbols: %E\n"), member);
2643 loaded = FALSE;
2647 entry->loaded = loaded;
2648 return loaded;
2650 break;
2653 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
2654 entry->loaded = TRUE;
2655 else
2656 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
2658 return entry->loaded;
2661 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2662 may be NULL, indicating that it is a wildcard. Separate
2663 lang_input_section statements are created for each part of the
2664 expansion; they are added after the wild statement S. OUTPUT is
2665 the output section. */
2667 static void
2668 wild (lang_wild_statement_type *s,
2669 const char *target ATTRIBUTE_UNUSED,
2670 lang_output_section_statement_type *output)
2672 struct wildcard_list *sec;
2674 if (s->handler_data[0]
2675 && s->handler_data[0]->spec.sorted == by_name
2676 && !s->filenames_sorted)
2678 lang_section_bst_type *tree;
2680 walk_wild (s, output_section_callback_fast, output);
2682 tree = s->tree;
2683 if (tree)
2685 output_section_callback_tree_to_list (s, tree, output);
2686 s->tree = NULL;
2689 else
2690 walk_wild (s, output_section_callback, output);
2692 if (default_common_section == NULL)
2693 for (sec = s->section_list; sec != NULL; sec = sec->next)
2694 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
2696 /* Remember the section that common is going to in case we
2697 later get something which doesn't know where to put it. */
2698 default_common_section = output;
2699 break;
2703 /* Return TRUE iff target is the sought target. */
2705 static int
2706 get_target (const bfd_target *target, void *data)
2708 const char *sought = data;
2710 return strcmp (target->name, sought) == 0;
2713 /* Like strcpy() but convert to lower case as well. */
2715 static void
2716 stricpy (char *dest, char *src)
2718 char c;
2720 while ((c = *src++) != 0)
2721 *dest++ = TOLOWER (c);
2723 *dest = 0;
2726 /* Remove the first occurrence of needle (if any) in haystack
2727 from haystack. */
2729 static void
2730 strcut (char *haystack, char *needle)
2732 haystack = strstr (haystack, needle);
2734 if (haystack)
2736 char *src;
2738 for (src = haystack + strlen (needle); *src;)
2739 *haystack++ = *src++;
2741 *haystack = 0;
2745 /* Compare two target format name strings.
2746 Return a value indicating how "similar" they are. */
2748 static int
2749 name_compare (char *first, char *second)
2751 char *copy1;
2752 char *copy2;
2753 int result;
2755 copy1 = xmalloc (strlen (first) + 1);
2756 copy2 = xmalloc (strlen (second) + 1);
2758 /* Convert the names to lower case. */
2759 stricpy (copy1, first);
2760 stricpy (copy2, second);
2762 /* Remove size and endian strings from the name. */
2763 strcut (copy1, "big");
2764 strcut (copy1, "little");
2765 strcut (copy2, "big");
2766 strcut (copy2, "little");
2768 /* Return a value based on how many characters match,
2769 starting from the beginning. If both strings are
2770 the same then return 10 * their length. */
2771 for (result = 0; copy1[result] == copy2[result]; result++)
2772 if (copy1[result] == 0)
2774 result *= 10;
2775 break;
2778 free (copy1);
2779 free (copy2);
2781 return result;
2784 /* Set by closest_target_match() below. */
2785 static const bfd_target *winner;
2787 /* Scan all the valid bfd targets looking for one that has the endianness
2788 requirement that was specified on the command line, and is the nearest
2789 match to the original output target. */
2791 static int
2792 closest_target_match (const bfd_target *target, void *data)
2794 const bfd_target *original = data;
2796 if (command_line.endian == ENDIAN_BIG
2797 && target->byteorder != BFD_ENDIAN_BIG)
2798 return 0;
2800 if (command_line.endian == ENDIAN_LITTLE
2801 && target->byteorder != BFD_ENDIAN_LITTLE)
2802 return 0;
2804 /* Must be the same flavour. */
2805 if (target->flavour != original->flavour)
2806 return 0;
2808 /* Ignore generic big and little endian elf vectors. */
2809 if (strcmp (target->name, "elf32-big") == 0
2810 || strcmp (target->name, "elf64-big") == 0
2811 || strcmp (target->name, "elf32-little") == 0
2812 || strcmp (target->name, "elf64-little") == 0)
2813 return 0;
2815 /* If we have not found a potential winner yet, then record this one. */
2816 if (winner == NULL)
2818 winner = target;
2819 return 0;
2822 /* Oh dear, we now have two potential candidates for a successful match.
2823 Compare their names and choose the better one. */
2824 if (name_compare (target->name, original->name)
2825 > name_compare (winner->name, original->name))
2826 winner = target;
2828 /* Keep on searching until wqe have checked them all. */
2829 return 0;
2832 /* Return the BFD target format of the first input file. */
2834 static char *
2835 get_first_input_target (void)
2837 char *target = NULL;
2839 LANG_FOR_EACH_INPUT_STATEMENT (s)
2841 if (s->header.type == lang_input_statement_enum
2842 && s->real)
2844 ldfile_open_file (s);
2846 if (s->the_bfd != NULL
2847 && bfd_check_format (s->the_bfd, bfd_object))
2849 target = bfd_get_target (s->the_bfd);
2851 if (target != NULL)
2852 break;
2857 return target;
2860 const char *
2861 lang_get_output_target (void)
2863 const char *target;
2865 /* Has the user told us which output format to use? */
2866 if (output_target != NULL)
2867 return output_target;
2869 /* No - has the current target been set to something other than
2870 the default? */
2871 if (current_target != default_target)
2872 return current_target;
2874 /* No - can we determine the format of the first input file? */
2875 target = get_first_input_target ();
2876 if (target != NULL)
2877 return target;
2879 /* Failed - use the default output target. */
2880 return default_target;
2883 /* Open the output file. */
2885 static void
2886 open_output (const char *name)
2888 output_target = lang_get_output_target ();
2890 /* Has the user requested a particular endianness on the command
2891 line? */
2892 if (command_line.endian != ENDIAN_UNSET)
2894 const bfd_target *target;
2895 enum bfd_endian desired_endian;
2897 /* Get the chosen target. */
2898 target = bfd_search_for_target (get_target, (void *) output_target);
2900 /* If the target is not supported, we cannot do anything. */
2901 if (target != NULL)
2903 if (command_line.endian == ENDIAN_BIG)
2904 desired_endian = BFD_ENDIAN_BIG;
2905 else
2906 desired_endian = BFD_ENDIAN_LITTLE;
2908 /* See if the target has the wrong endianness. This should
2909 not happen if the linker script has provided big and
2910 little endian alternatives, but some scrips don't do
2911 this. */
2912 if (target->byteorder != desired_endian)
2914 /* If it does, then see if the target provides
2915 an alternative with the correct endianness. */
2916 if (target->alternative_target != NULL
2917 && (target->alternative_target->byteorder == desired_endian))
2918 output_target = target->alternative_target->name;
2919 else
2921 /* Try to find a target as similar as possible to
2922 the default target, but which has the desired
2923 endian characteristic. */
2924 bfd_search_for_target (closest_target_match,
2925 (void *) target);
2927 /* Oh dear - we could not find any targets that
2928 satisfy our requirements. */
2929 if (winner == NULL)
2930 einfo (_("%P: warning: could not find any targets"
2931 " that match endianness requirement\n"));
2932 else
2933 output_target = winner->name;
2939 link_info.output_bfd = bfd_openw (name, output_target);
2941 if (link_info.output_bfd == NULL)
2943 if (bfd_get_error () == bfd_error_invalid_target)
2944 einfo (_("%P%F: target %s not found\n"), output_target);
2946 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2949 delete_output_file_on_failure = TRUE;
2951 if (! bfd_set_format (link_info.output_bfd, bfd_object))
2952 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2953 if (! bfd_set_arch_mach (link_info.output_bfd,
2954 ldfile_output_architecture,
2955 ldfile_output_machine))
2956 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2958 link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
2959 if (link_info.hash == NULL)
2960 einfo (_("%P%F: can not create hash table: %E\n"));
2962 bfd_set_gp_size (link_info.output_bfd, g_switch_value);
2965 static void
2966 ldlang_open_output (lang_statement_union_type *statement)
2968 switch (statement->header.type)
2970 case lang_output_statement_enum:
2971 ASSERT (link_info.output_bfd == NULL);
2972 open_output (statement->output_statement.name);
2973 ldemul_set_output_arch ();
2974 if (config.magic_demand_paged && !link_info.relocatable)
2975 link_info.output_bfd->flags |= D_PAGED;
2976 else
2977 link_info.output_bfd->flags &= ~D_PAGED;
2978 if (config.text_read_only)
2979 link_info.output_bfd->flags |= WP_TEXT;
2980 else
2981 link_info.output_bfd->flags &= ~WP_TEXT;
2982 if (link_info.traditional_format)
2983 link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2984 else
2985 link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2986 break;
2988 case lang_target_statement_enum:
2989 current_target = statement->target_statement.target;
2990 break;
2991 default:
2992 break;
2996 /* Convert between addresses in bytes and sizes in octets.
2997 For currently supported targets, octets_per_byte is always a power
2998 of two, so we can use shifts. */
2999 #define TO_ADDR(X) ((X) >> opb_shift)
3000 #define TO_SIZE(X) ((X) << opb_shift)
3002 /* Support the above. */
3003 static unsigned int opb_shift = 0;
3005 static void
3006 init_opb (void)
3008 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3009 ldfile_output_machine);
3010 opb_shift = 0;
3011 if (x > 1)
3012 while ((x & 1) == 0)
3014 x >>= 1;
3015 ++opb_shift;
3017 ASSERT (x == 1);
3020 /* Open all the input files. */
3022 static void
3023 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
3025 for (; s != NULL; s = s->header.next)
3027 switch (s->header.type)
3029 case lang_constructors_statement_enum:
3030 open_input_bfds (constructor_list.head, force);
3031 break;
3032 case lang_output_section_statement_enum:
3033 open_input_bfds (s->output_section_statement.children.head, force);
3034 break;
3035 case lang_wild_statement_enum:
3036 /* Maybe we should load the file's symbols. */
3037 if (s->wild_statement.filename
3038 && !wildcardp (s->wild_statement.filename)
3039 && !archive_path (s->wild_statement.filename))
3040 lookup_name (s->wild_statement.filename);
3041 open_input_bfds (s->wild_statement.children.head, force);
3042 break;
3043 case lang_group_statement_enum:
3045 struct bfd_link_hash_entry *undefs;
3047 /* We must continually search the entries in the group
3048 until no new symbols are added to the list of undefined
3049 symbols. */
3053 undefs = link_info.hash->undefs_tail;
3054 open_input_bfds (s->group_statement.children.head, TRUE);
3056 while (undefs != link_info.hash->undefs_tail);
3058 break;
3059 case lang_target_statement_enum:
3060 current_target = s->target_statement.target;
3061 break;
3062 case lang_input_statement_enum:
3063 if (s->input_statement.real)
3065 lang_statement_union_type **os_tail;
3066 lang_statement_list_type add;
3068 s->input_statement.target = current_target;
3070 /* If we are being called from within a group, and this
3071 is an archive which has already been searched, then
3072 force it to be researched unless the whole archive
3073 has been loaded already. */
3074 if (force
3075 && !s->input_statement.whole_archive
3076 && s->input_statement.loaded
3077 && bfd_check_format (s->input_statement.the_bfd,
3078 bfd_archive))
3079 s->input_statement.loaded = FALSE;
3081 os_tail = lang_output_section_statement.tail;
3082 lang_list_init (&add);
3084 if (! load_symbols (&s->input_statement, &add))
3085 config.make_executable = FALSE;
3087 if (add.head != NULL)
3089 /* If this was a script with output sections then
3090 tack any added statements on to the end of the
3091 list. This avoids having to reorder the output
3092 section statement list. Very likely the user
3093 forgot -T, and whatever we do here will not meet
3094 naive user expectations. */
3095 if (os_tail != lang_output_section_statement.tail)
3097 einfo (_("%P: warning: %s contains output sections;"
3098 " did you forget -T?\n"),
3099 s->input_statement.filename);
3100 *stat_ptr->tail = add.head;
3101 stat_ptr->tail = add.tail;
3103 else
3105 *add.tail = s->header.next;
3106 s->header.next = add.head;
3110 break;
3111 default:
3112 break;
3117 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
3119 void
3120 lang_track_definedness (const char *name)
3122 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
3123 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
3126 /* New-function for the definedness hash table. */
3128 static struct bfd_hash_entry *
3129 lang_definedness_newfunc (struct bfd_hash_entry *entry,
3130 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
3131 const char *name ATTRIBUTE_UNUSED)
3133 struct lang_definedness_hash_entry *ret
3134 = (struct lang_definedness_hash_entry *) entry;
3136 if (ret == NULL)
3137 ret = (struct lang_definedness_hash_entry *)
3138 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
3140 if (ret == NULL)
3141 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
3143 ret->iteration = -1;
3144 return &ret->root;
3147 /* Return the iteration when the definition of NAME was last updated. A
3148 value of -1 means that the symbol is not defined in the linker script
3149 or the command line, but may be defined in the linker symbol table. */
3152 lang_symbol_definition_iteration (const char *name)
3154 struct lang_definedness_hash_entry *defentry
3155 = (struct lang_definedness_hash_entry *)
3156 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3158 /* We've already created this one on the presence of DEFINED in the
3159 script, so it can't be NULL unless something is borked elsewhere in
3160 the code. */
3161 if (defentry == NULL)
3162 FAIL ();
3164 return defentry->iteration;
3167 /* Update the definedness state of NAME. */
3169 void
3170 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
3172 struct lang_definedness_hash_entry *defentry
3173 = (struct lang_definedness_hash_entry *)
3174 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
3176 /* We don't keep track of symbols not tested with DEFINED. */
3177 if (defentry == NULL)
3178 return;
3180 /* If the symbol was already defined, and not from an earlier statement
3181 iteration, don't update the definedness iteration, because that'd
3182 make the symbol seem defined in the linker script at this point, and
3183 it wasn't; it was defined in some object. If we do anyway, DEFINED
3184 would start to yield false before this point and the construct "sym =
3185 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3186 in an object. */
3187 if (h->type != bfd_link_hash_undefined
3188 && h->type != bfd_link_hash_common
3189 && h->type != bfd_link_hash_new
3190 && defentry->iteration == -1)
3191 return;
3193 defentry->iteration = lang_statement_iteration;
3196 /* Add the supplied name to the symbol table as an undefined reference.
3197 This is a two step process as the symbol table doesn't even exist at
3198 the time the ld command line is processed. First we put the name
3199 on a list, then, once the output file has been opened, transfer the
3200 name to the symbol table. */
3202 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
3204 #define ldlang_undef_chain_list_head entry_symbol.next
3206 void
3207 ldlang_add_undef (const char *const name)
3209 ldlang_undef_chain_list_type *new =
3210 stat_alloc (sizeof (ldlang_undef_chain_list_type));
3212 new->next = ldlang_undef_chain_list_head;
3213 ldlang_undef_chain_list_head = new;
3215 new->name = xstrdup (name);
3217 if (link_info.output_bfd != NULL)
3218 insert_undefined (new->name);
3221 /* Insert NAME as undefined in the symbol table. */
3223 static void
3224 insert_undefined (const char *name)
3226 struct bfd_link_hash_entry *h;
3228 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
3229 if (h == NULL)
3230 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3231 if (h->type == bfd_link_hash_new)
3233 h->type = bfd_link_hash_undefined;
3234 h->u.undef.abfd = NULL;
3235 bfd_link_add_undef (link_info.hash, h);
3239 /* Run through the list of undefineds created above and place them
3240 into the linker hash table as undefined symbols belonging to the
3241 script file. */
3243 static void
3244 lang_place_undefineds (void)
3246 ldlang_undef_chain_list_type *ptr;
3248 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
3249 insert_undefined (ptr->name);
3252 /* Check for all readonly or some readwrite sections. */
3254 static void
3255 check_input_sections
3256 (lang_statement_union_type *s,
3257 lang_output_section_statement_type *output_section_statement)
3259 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
3261 switch (s->header.type)
3263 case lang_wild_statement_enum:
3264 walk_wild (&s->wild_statement, check_section_callback,
3265 output_section_statement);
3266 if (! output_section_statement->all_input_readonly)
3267 return;
3268 break;
3269 case lang_constructors_statement_enum:
3270 check_input_sections (constructor_list.head,
3271 output_section_statement);
3272 if (! output_section_statement->all_input_readonly)
3273 return;
3274 break;
3275 case lang_group_statement_enum:
3276 check_input_sections (s->group_statement.children.head,
3277 output_section_statement);
3278 if (! output_section_statement->all_input_readonly)
3279 return;
3280 break;
3281 default:
3282 break;
3287 /* Update wildcard statements if needed. */
3289 static void
3290 update_wild_statements (lang_statement_union_type *s)
3292 struct wildcard_list *sec;
3294 switch (sort_section)
3296 default:
3297 FAIL ();
3299 case none:
3300 break;
3302 case by_name:
3303 case by_alignment:
3304 for (; s != NULL; s = s->header.next)
3306 switch (s->header.type)
3308 default:
3309 break;
3311 case lang_wild_statement_enum:
3312 sec = s->wild_statement.section_list;
3313 for (sec = s->wild_statement.section_list; sec != NULL;
3314 sec = sec->next)
3316 switch (sec->spec.sorted)
3318 case none:
3319 sec->spec.sorted = sort_section;
3320 break;
3321 case by_name:
3322 if (sort_section == by_alignment)
3323 sec->spec.sorted = by_name_alignment;
3324 break;
3325 case by_alignment:
3326 if (sort_section == by_name)
3327 sec->spec.sorted = by_alignment_name;
3328 break;
3329 default:
3330 break;
3333 break;
3335 case lang_constructors_statement_enum:
3336 update_wild_statements (constructor_list.head);
3337 break;
3339 case lang_output_section_statement_enum:
3340 update_wild_statements
3341 (s->output_section_statement.children.head);
3342 break;
3344 case lang_group_statement_enum:
3345 update_wild_statements (s->group_statement.children.head);
3346 break;
3349 break;
3353 /* Open input files and attach to output sections. */
3355 static void
3356 map_input_to_output_sections
3357 (lang_statement_union_type *s, const char *target,
3358 lang_output_section_statement_type *os)
3360 flagword flags;
3362 for (; s != NULL; s = s->header.next)
3364 switch (s->header.type)
3366 case lang_wild_statement_enum:
3367 wild (&s->wild_statement, target, os);
3368 break;
3369 case lang_constructors_statement_enum:
3370 map_input_to_output_sections (constructor_list.head,
3371 target,
3372 os);
3373 break;
3374 case lang_output_section_statement_enum:
3375 if (s->output_section_statement.constraint)
3377 if (s->output_section_statement.constraint != ONLY_IF_RW
3378 && s->output_section_statement.constraint != ONLY_IF_RO)
3379 break;
3380 s->output_section_statement.all_input_readonly = TRUE;
3381 check_input_sections (s->output_section_statement.children.head,
3382 &s->output_section_statement);
3383 if ((s->output_section_statement.all_input_readonly
3384 && s->output_section_statement.constraint == ONLY_IF_RW)
3385 || (!s->output_section_statement.all_input_readonly
3386 && s->output_section_statement.constraint == ONLY_IF_RO))
3388 s->output_section_statement.constraint = -1;
3389 break;
3393 map_input_to_output_sections (s->output_section_statement.children.head,
3394 target,
3395 &s->output_section_statement);
3396 break;
3397 case lang_output_statement_enum:
3398 break;
3399 case lang_target_statement_enum:
3400 target = s->target_statement.target;
3401 break;
3402 case lang_group_statement_enum:
3403 map_input_to_output_sections (s->group_statement.children.head,
3404 target,
3405 os);
3406 break;
3407 case lang_data_statement_enum:
3408 /* Make sure that any sections mentioned in the expression
3409 are initialized. */
3410 exp_init_os (s->data_statement.exp);
3411 flags = SEC_HAS_CONTENTS;
3412 /* The output section gets contents, and then we inspect for
3413 any flags set in the input script which override any ALLOC. */
3414 if (!(os->flags & SEC_NEVER_LOAD))
3415 flags |= SEC_ALLOC | SEC_LOAD;
3416 if (os->bfd_section == NULL)
3417 init_os (os, NULL, flags);
3418 else
3419 os->bfd_section->flags |= flags;
3420 break;
3421 case lang_input_section_enum:
3422 break;
3423 case lang_fill_statement_enum:
3424 case lang_object_symbols_statement_enum:
3425 case lang_reloc_statement_enum:
3426 case lang_padding_statement_enum:
3427 case lang_input_statement_enum:
3428 if (os != NULL && os->bfd_section == NULL)
3429 init_os (os, NULL, 0);
3430 break;
3431 case lang_assignment_statement_enum:
3432 if (os != NULL && os->bfd_section == NULL)
3433 init_os (os, NULL, 0);
3435 /* Make sure that any sections mentioned in the assignment
3436 are initialized. */
3437 exp_init_os (s->assignment_statement.exp);
3438 break;
3439 case lang_address_statement_enum:
3440 /* Mark the specified section with the supplied address.
3441 If this section was actually a segment marker, then the
3442 directive is ignored if the linker script explicitly
3443 processed the segment marker. Originally, the linker
3444 treated segment directives (like -Ttext on the
3445 command-line) as section directives. We honor the
3446 section directive semantics for backwards compatibilty;
3447 linker scripts that do not specifically check for
3448 SEGMENT_START automatically get the old semantics. */
3449 if (!s->address_statement.segment
3450 || !s->address_statement.segment->used)
3452 lang_output_section_statement_type *aos
3453 = (lang_output_section_statement_lookup
3454 (s->address_statement.section_name, 0, TRUE));
3456 if (aos->bfd_section == NULL)
3457 init_os (aos, NULL, 0);
3458 aos->addr_tree = s->address_statement.address;
3460 break;
3461 case lang_insert_statement_enum:
3462 break;
3467 /* An insert statement snips out all the linker statements from the
3468 start of the list and places them after the output section
3469 statement specified by the insert. This operation is complicated
3470 by the fact that we keep a doubly linked list of output section
3471 statements as well as the singly linked list of all statements. */
3473 static void
3474 process_insert_statements (void)
3476 lang_statement_union_type **s;
3477 lang_output_section_statement_type *first_os = NULL;
3478 lang_output_section_statement_type *last_os = NULL;
3479 lang_output_section_statement_type *os;
3481 /* "start of list" is actually the statement immediately after
3482 the special abs_section output statement, so that it isn't
3483 reordered. */
3484 s = &lang_output_section_statement.head;
3485 while (*(s = &(*s)->header.next) != NULL)
3487 if ((*s)->header.type == lang_output_section_statement_enum)
3489 /* Keep pointers to the first and last output section
3490 statement in the sequence we may be about to move. */
3491 os = &(*s)->output_section_statement;
3493 ASSERT (last_os == NULL || last_os->next == os);
3494 last_os = os;
3496 /* Set constraint negative so that lang_output_section_find
3497 won't match this output section statement. At this
3498 stage in linking constraint has values in the range
3499 [-1, ONLY_IN_RW]. */
3500 last_os->constraint = -2 - last_os->constraint;
3501 if (first_os == NULL)
3502 first_os = last_os;
3504 else if ((*s)->header.type == lang_insert_statement_enum)
3506 lang_insert_statement_type *i = &(*s)->insert_statement;
3507 lang_output_section_statement_type *where;
3508 lang_statement_union_type **ptr;
3509 lang_statement_union_type *first;
3511 where = lang_output_section_find (i->where);
3512 if (where != NULL && i->is_before)
3515 where = where->prev;
3516 while (where != NULL && where->constraint < 0);
3518 if (where == NULL)
3520 einfo (_("%F%P: %s not found for insert\n"), i->where);
3521 return;
3524 /* Deal with reordering the output section statement list. */
3525 if (last_os != NULL)
3527 asection *first_sec, *last_sec;
3528 struct lang_output_section_statement_struct **next;
3530 /* Snip out the output sections we are moving. */
3531 first_os->prev->next = last_os->next;
3532 if (last_os->next == NULL)
3534 next = &first_os->prev->next;
3535 lang_output_section_statement.tail
3536 = (lang_statement_union_type **) next;
3538 else
3539 last_os->next->prev = first_os->prev;
3540 /* Add them in at the new position. */
3541 last_os->next = where->next;
3542 if (where->next == NULL)
3544 next = &last_os->next;
3545 lang_output_section_statement.tail
3546 = (lang_statement_union_type **) next;
3548 else
3549 where->next->prev = last_os;
3550 first_os->prev = where;
3551 where->next = first_os;
3553 /* Move the bfd sections in the same way. */
3554 first_sec = NULL;
3555 last_sec = NULL;
3556 for (os = first_os; os != NULL; os = os->next)
3558 os->constraint = -2 - os->constraint;
3559 if (os->bfd_section != NULL
3560 && os->bfd_section->owner != NULL)
3562 last_sec = os->bfd_section;
3563 if (first_sec == NULL)
3564 first_sec = last_sec;
3566 if (os == last_os)
3567 break;
3569 if (last_sec != NULL)
3571 asection *sec = where->bfd_section;
3572 if (sec == NULL)
3573 sec = output_prev_sec_find (where);
3575 /* The place we want to insert must come after the
3576 sections we are moving. So if we find no
3577 section or if the section is the same as our
3578 last section, then no move is needed. */
3579 if (sec != NULL && sec != last_sec)
3581 /* Trim them off. */
3582 if (first_sec->prev != NULL)
3583 first_sec->prev->next = last_sec->next;
3584 else
3585 link_info.output_bfd->sections = last_sec->next;
3586 if (last_sec->next != NULL)
3587 last_sec->next->prev = first_sec->prev;
3588 else
3589 link_info.output_bfd->section_last = first_sec->prev;
3590 /* Add back. */
3591 last_sec->next = sec->next;
3592 if (sec->next != NULL)
3593 sec->next->prev = last_sec;
3594 else
3595 link_info.output_bfd->section_last = last_sec;
3596 first_sec->prev = sec;
3597 sec->next = first_sec;
3601 first_os = NULL;
3602 last_os = NULL;
3605 ptr = insert_os_after (where);
3606 /* Snip everything after the abs_section output statement we
3607 know is at the start of the list, up to and including
3608 the insert statement we are currently processing. */
3609 first = lang_output_section_statement.head->header.next;
3610 lang_output_section_statement.head->header.next = (*s)->header.next;
3611 /* Add them back where they belong. */
3612 *s = *ptr;
3613 if (*s == NULL)
3614 statement_list.tail = s;
3615 *ptr = first;
3616 s = &lang_output_section_statement.head;
3620 /* Undo constraint twiddling. */
3621 for (os = first_os; os != NULL; os = os->next)
3623 os->constraint = -2 - os->constraint;
3624 if (os == last_os)
3625 break;
3629 /* An output section might have been removed after its statement was
3630 added. For example, ldemul_before_allocation can remove dynamic
3631 sections if they turn out to be not needed. Clean them up here. */
3633 void
3634 strip_excluded_output_sections (void)
3636 lang_output_section_statement_type *os;
3638 /* Run lang_size_sections (if not already done). */
3639 if (expld.phase != lang_mark_phase_enum)
3641 expld.phase = lang_mark_phase_enum;
3642 expld.dataseg.phase = exp_dataseg_none;
3643 one_lang_size_sections_pass (NULL, FALSE);
3644 lang_reset_memory_regions ();
3647 for (os = &lang_output_section_statement.head->output_section_statement;
3648 os != NULL;
3649 os = os->next)
3651 asection *output_section;
3652 bfd_boolean exclude;
3654 if (os->constraint < 0)
3655 continue;
3657 output_section = os->bfd_section;
3658 if (output_section == NULL)
3659 continue;
3661 exclude = (output_section->rawsize == 0
3662 && (output_section->flags & SEC_KEEP) == 0
3663 && !bfd_section_removed_from_list (link_info.output_bfd,
3664 output_section));
3666 /* Some sections have not yet been sized, notably .gnu.version,
3667 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3668 input sections, so don't drop output sections that have such
3669 input sections unless they are also marked SEC_EXCLUDE. */
3670 if (exclude && output_section->map_head.s != NULL)
3672 asection *s;
3674 for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
3675 if ((s->flags & SEC_LINKER_CREATED) != 0
3676 && (s->flags & SEC_EXCLUDE) == 0)
3678 exclude = FALSE;
3679 break;
3683 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3684 output_section->map_head.link_order = NULL;
3685 output_section->map_tail.link_order = NULL;
3687 if (exclude)
3689 /* We don't set bfd_section to NULL since bfd_section of the
3690 removed output section statement may still be used. */
3691 if (!os->section_relative_symbol
3692 && !os->update_dot_tree)
3693 os->ignored = TRUE;
3694 output_section->flags |= SEC_EXCLUDE;
3695 bfd_section_list_remove (link_info.output_bfd, output_section);
3696 link_info.output_bfd->section_count--;
3700 /* Stop future calls to lang_add_section from messing with map_head
3701 and map_tail link_order fields. */
3702 stripped_excluded_sections = TRUE;
3705 static void
3706 print_output_section_statement
3707 (lang_output_section_statement_type *output_section_statement)
3709 asection *section = output_section_statement->bfd_section;
3710 int len;
3712 if (output_section_statement != abs_output_section)
3714 minfo ("\n%s", output_section_statement->name);
3716 if (section != NULL)
3718 print_dot = section->vma;
3720 len = strlen (output_section_statement->name);
3721 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3723 print_nl ();
3724 len = 0;
3726 while (len < SECTION_NAME_MAP_LENGTH)
3728 print_space ();
3729 ++len;
3732 minfo ("0x%V %W", section->vma, section->size);
3734 if (section->vma != section->lma)
3735 minfo (_(" load address 0x%V"), section->lma);
3737 if (output_section_statement->update_dot_tree != NULL)
3738 exp_fold_tree (output_section_statement->update_dot_tree,
3739 bfd_abs_section_ptr, &print_dot);
3742 print_nl ();
3745 print_statement_list (output_section_statement->children.head,
3746 output_section_statement);
3749 /* Scan for the use of the destination in the right hand side
3750 of an expression. In such cases we will not compute the
3751 correct expression, since the value of DST that is used on
3752 the right hand side will be its final value, not its value
3753 just before this expression is evaluated. */
3755 static bfd_boolean
3756 scan_for_self_assignment (const char * dst, etree_type * rhs)
3758 if (rhs == NULL || dst == NULL)
3759 return FALSE;
3761 switch (rhs->type.node_class)
3763 case etree_binary:
3764 return scan_for_self_assignment (dst, rhs->binary.lhs)
3765 || scan_for_self_assignment (dst, rhs->binary.rhs);
3767 case etree_trinary:
3768 return scan_for_self_assignment (dst, rhs->trinary.lhs)
3769 || scan_for_self_assignment (dst, rhs->trinary.rhs);
3771 case etree_assign:
3772 case etree_provided:
3773 case etree_provide:
3774 if (strcmp (dst, rhs->assign.dst) == 0)
3775 return TRUE;
3776 return scan_for_self_assignment (dst, rhs->assign.src);
3778 case etree_unary:
3779 return scan_for_self_assignment (dst, rhs->unary.child);
3781 case etree_value:
3782 if (rhs->value.str)
3783 return strcmp (dst, rhs->value.str) == 0;
3784 return FALSE;
3786 case etree_name:
3787 if (rhs->name.name)
3788 return strcmp (dst, rhs->name.name) == 0;
3789 return FALSE;
3791 default:
3792 break;
3795 return FALSE;
3799 static void
3800 print_assignment (lang_assignment_statement_type *assignment,
3801 lang_output_section_statement_type *output_section)
3803 unsigned int i;
3804 bfd_boolean is_dot;
3805 bfd_boolean computation_is_valid = TRUE;
3806 etree_type *tree;
3808 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3809 print_space ();
3811 if (assignment->exp->type.node_class == etree_assert)
3813 is_dot = FALSE;
3814 tree = assignment->exp->assert_s.child;
3815 computation_is_valid = TRUE;
3817 else
3819 const char *dst = assignment->exp->assign.dst;
3821 is_dot = (dst[0] == '.' && dst[1] == 0);
3822 tree = assignment->exp->assign.src;
3823 computation_is_valid = is_dot || (scan_for_self_assignment (dst, tree) == FALSE);
3826 exp_fold_tree (tree, output_section->bfd_section, &print_dot);
3827 if (expld.result.valid_p)
3829 bfd_vma value;
3831 if (computation_is_valid)
3833 value = expld.result.value;
3835 if (expld.result.section)
3836 value += expld.result.section->vma;
3838 minfo ("0x%V", value);
3839 if (is_dot)
3840 print_dot = value;
3842 else
3844 struct bfd_link_hash_entry *h;
3846 h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
3847 FALSE, FALSE, TRUE);
3848 if (h)
3850 value = h->u.def.value;
3852 if (expld.result.section)
3853 value += expld.result.section->vma;
3855 minfo ("[0x%V]", value);
3857 else
3858 minfo ("[unresolved]");
3861 else
3863 minfo ("*undef* ");
3864 #ifdef BFD64
3865 minfo (" ");
3866 #endif
3869 minfo (" ");
3870 exp_print_tree (assignment->exp);
3871 print_nl ();
3874 static void
3875 print_input_statement (lang_input_statement_type *statm)
3877 if (statm->filename != NULL
3878 && (statm->the_bfd == NULL
3879 || (statm->the_bfd->flags & BFD_LINKER_CREATED) == 0))
3880 fprintf (config.map_file, "LOAD %s\n", statm->filename);
3883 /* Print all symbols defined in a particular section. This is called
3884 via bfd_link_hash_traverse, or by print_all_symbols. */
3886 static bfd_boolean
3887 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
3889 asection *sec = ptr;
3891 if ((hash_entry->type == bfd_link_hash_defined
3892 || hash_entry->type == bfd_link_hash_defweak)
3893 && sec == hash_entry->u.def.section)
3895 int i;
3897 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3898 print_space ();
3899 minfo ("0x%V ",
3900 (hash_entry->u.def.value
3901 + hash_entry->u.def.section->output_offset
3902 + hash_entry->u.def.section->output_section->vma));
3904 minfo (" %T\n", hash_entry->root.string);
3907 return TRUE;
3910 static void
3911 print_all_symbols (asection *sec)
3913 struct fat_user_section_struct *ud = get_userdata (sec);
3914 struct map_symbol_def *def;
3916 if (!ud)
3917 return;
3919 *ud->map_symbol_def_tail = 0;
3920 for (def = ud->map_symbol_def_head; def; def = def->next)
3921 print_one_symbol (def->entry, sec);
3924 /* Print information about an input section to the map file. */
3926 static void
3927 print_input_section (asection *i)
3929 bfd_size_type size = i->size;
3930 int len;
3931 bfd_vma addr;
3933 init_opb ();
3935 print_space ();
3936 minfo ("%s", i->name);
3938 len = 1 + strlen (i->name);
3939 if (len >= SECTION_NAME_MAP_LENGTH - 1)
3941 print_nl ();
3942 len = 0;
3944 while (len < SECTION_NAME_MAP_LENGTH)
3946 print_space ();
3947 ++len;
3950 if (i->output_section != NULL
3951 && i->output_section->owner == link_info.output_bfd)
3952 addr = i->output_section->vma + i->output_offset;
3953 else
3955 addr = print_dot;
3956 size = 0;
3959 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
3961 if (size != i->rawsize && i->rawsize != 0)
3963 len = SECTION_NAME_MAP_LENGTH + 3;
3964 #ifdef BFD64
3965 len += 16;
3966 #else
3967 len += 8;
3968 #endif
3969 while (len > 0)
3971 print_space ();
3972 --len;
3975 minfo (_("%W (size before relaxing)\n"), i->rawsize);
3978 if (i->output_section != NULL
3979 && i->output_section->owner == link_info.output_bfd)
3981 if (link_info.reduce_memory_overheads)
3982 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
3983 else
3984 print_all_symbols (i);
3986 /* Update print_dot, but make sure that we do not move it
3987 backwards - this could happen if we have overlays and a
3988 later overlay is shorter than an earier one. */
3989 if (addr + TO_ADDR (size) > print_dot)
3990 print_dot = addr + TO_ADDR (size);
3994 static void
3995 print_fill_statement (lang_fill_statement_type *fill)
3997 size_t size;
3998 unsigned char *p;
3999 fputs (" FILL mask 0x", config.map_file);
4000 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
4001 fprintf (config.map_file, "%02x", *p);
4002 fputs ("\n", config.map_file);
4005 static void
4006 print_data_statement (lang_data_statement_type *data)
4008 int i;
4009 bfd_vma addr;
4010 bfd_size_type size;
4011 const char *name;
4013 init_opb ();
4014 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4015 print_space ();
4017 addr = data->output_offset;
4018 if (data->output_section != NULL)
4019 addr += data->output_section->vma;
4021 switch (data->type)
4023 default:
4024 abort ();
4025 case BYTE:
4026 size = BYTE_SIZE;
4027 name = "BYTE";
4028 break;
4029 case SHORT:
4030 size = SHORT_SIZE;
4031 name = "SHORT";
4032 break;
4033 case LONG:
4034 size = LONG_SIZE;
4035 name = "LONG";
4036 break;
4037 case QUAD:
4038 size = QUAD_SIZE;
4039 name = "QUAD";
4040 break;
4041 case SQUAD:
4042 size = QUAD_SIZE;
4043 name = "SQUAD";
4044 break;
4047 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
4049 if (data->exp->type.node_class != etree_value)
4051 print_space ();
4052 exp_print_tree (data->exp);
4055 print_nl ();
4057 print_dot = addr + TO_ADDR (size);
4060 /* Print an address statement. These are generated by options like
4061 -Ttext. */
4063 static void
4064 print_address_statement (lang_address_statement_type *address)
4066 minfo (_("Address of section %s set to "), address->section_name);
4067 exp_print_tree (address->address);
4068 print_nl ();
4071 /* Print a reloc statement. */
4073 static void
4074 print_reloc_statement (lang_reloc_statement_type *reloc)
4076 int i;
4077 bfd_vma addr;
4078 bfd_size_type size;
4080 init_opb ();
4081 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
4082 print_space ();
4084 addr = reloc->output_offset;
4085 if (reloc->output_section != NULL)
4086 addr += reloc->output_section->vma;
4088 size = bfd_get_reloc_size (reloc->howto);
4090 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
4092 if (reloc->name != NULL)
4093 minfo ("%s+", reloc->name);
4094 else
4095 minfo ("%s+", reloc->section->name);
4097 exp_print_tree (reloc->addend_exp);
4099 print_nl ();
4101 print_dot = addr + TO_ADDR (size);
4104 static void
4105 print_padding_statement (lang_padding_statement_type *s)
4107 int len;
4108 bfd_vma addr;
4110 init_opb ();
4111 minfo (" *fill*");
4113 len = sizeof " *fill*" - 1;
4114 while (len < SECTION_NAME_MAP_LENGTH)
4116 print_space ();
4117 ++len;
4120 addr = s->output_offset;
4121 if (s->output_section != NULL)
4122 addr += s->output_section->vma;
4123 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
4125 if (s->fill->size != 0)
4127 size_t size;
4128 unsigned char *p;
4129 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
4130 fprintf (config.map_file, "%02x", *p);
4133 print_nl ();
4135 print_dot = addr + TO_ADDR (s->size);
4138 static void
4139 print_wild_statement (lang_wild_statement_type *w,
4140 lang_output_section_statement_type *os)
4142 struct wildcard_list *sec;
4144 print_space ();
4146 if (w->filenames_sorted)
4147 minfo ("SORT(");
4148 if (w->filename != NULL)
4149 minfo ("%s", w->filename);
4150 else
4151 minfo ("*");
4152 if (w->filenames_sorted)
4153 minfo (")");
4155 minfo ("(");
4156 for (sec = w->section_list; sec; sec = sec->next)
4158 if (sec->spec.sorted)
4159 minfo ("SORT(");
4160 if (sec->spec.exclude_name_list != NULL)
4162 name_list *tmp;
4163 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
4164 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
4165 minfo (" %s", tmp->name);
4166 minfo (") ");
4168 if (sec->spec.name != NULL)
4169 minfo ("%s", sec->spec.name);
4170 else
4171 minfo ("*");
4172 if (sec->spec.sorted)
4173 minfo (")");
4174 if (sec->next)
4175 minfo (" ");
4177 minfo (")");
4179 print_nl ();
4181 print_statement_list (w->children.head, os);
4184 /* Print a group statement. */
4186 static void
4187 print_group (lang_group_statement_type *s,
4188 lang_output_section_statement_type *os)
4190 fprintf (config.map_file, "START GROUP\n");
4191 print_statement_list (s->children.head, os);
4192 fprintf (config.map_file, "END GROUP\n");
4195 /* Print the list of statements in S.
4196 This can be called for any statement type. */
4198 static void
4199 print_statement_list (lang_statement_union_type *s,
4200 lang_output_section_statement_type *os)
4202 while (s != NULL)
4204 print_statement (s, os);
4205 s = s->header.next;
4209 /* Print the first statement in statement list S.
4210 This can be called for any statement type. */
4212 static void
4213 print_statement (lang_statement_union_type *s,
4214 lang_output_section_statement_type *os)
4216 switch (s->header.type)
4218 default:
4219 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
4220 FAIL ();
4221 break;
4222 case lang_constructors_statement_enum:
4223 if (constructor_list.head != NULL)
4225 if (constructors_sorted)
4226 minfo (" SORT (CONSTRUCTORS)\n");
4227 else
4228 minfo (" CONSTRUCTORS\n");
4229 print_statement_list (constructor_list.head, os);
4231 break;
4232 case lang_wild_statement_enum:
4233 print_wild_statement (&s->wild_statement, os);
4234 break;
4235 case lang_address_statement_enum:
4236 print_address_statement (&s->address_statement);
4237 break;
4238 case lang_object_symbols_statement_enum:
4239 minfo (" CREATE_OBJECT_SYMBOLS\n");
4240 break;
4241 case lang_fill_statement_enum:
4242 print_fill_statement (&s->fill_statement);
4243 break;
4244 case lang_data_statement_enum:
4245 print_data_statement (&s->data_statement);
4246 break;
4247 case lang_reloc_statement_enum:
4248 print_reloc_statement (&s->reloc_statement);
4249 break;
4250 case lang_input_section_enum:
4251 print_input_section (s->input_section.section);
4252 break;
4253 case lang_padding_statement_enum:
4254 print_padding_statement (&s->padding_statement);
4255 break;
4256 case lang_output_section_statement_enum:
4257 print_output_section_statement (&s->output_section_statement);
4258 break;
4259 case lang_assignment_statement_enum:
4260 print_assignment (&s->assignment_statement, os);
4261 break;
4262 case lang_target_statement_enum:
4263 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
4264 break;
4265 case lang_output_statement_enum:
4266 minfo ("OUTPUT(%s", s->output_statement.name);
4267 if (output_target != NULL)
4268 minfo (" %s", output_target);
4269 minfo (")\n");
4270 break;
4271 case lang_input_statement_enum:
4272 print_input_statement (&s->input_statement);
4273 break;
4274 case lang_group_statement_enum:
4275 print_group (&s->group_statement, os);
4276 break;
4277 case lang_insert_statement_enum:
4278 minfo ("INSERT %s %s\n",
4279 s->insert_statement.is_before ? "BEFORE" : "AFTER",
4280 s->insert_statement.where);
4281 break;
4285 static void
4286 print_statements (void)
4288 print_statement_list (statement_list.head, abs_output_section);
4291 /* Print the first N statements in statement list S to STDERR.
4292 If N == 0, nothing is printed.
4293 If N < 0, the entire list is printed.
4294 Intended to be called from GDB. */
4296 void
4297 dprint_statement (lang_statement_union_type *s, int n)
4299 FILE *map_save = config.map_file;
4301 config.map_file = stderr;
4303 if (n < 0)
4304 print_statement_list (s, abs_output_section);
4305 else
4307 while (s && --n >= 0)
4309 print_statement (s, abs_output_section);
4310 s = s->header.next;
4314 config.map_file = map_save;
4317 static void
4318 insert_pad (lang_statement_union_type **ptr,
4319 fill_type *fill,
4320 unsigned int alignment_needed,
4321 asection *output_section,
4322 bfd_vma dot)
4324 static fill_type zero_fill = { 1, { 0 } };
4325 lang_statement_union_type *pad = NULL;
4327 if (ptr != &statement_list.head)
4328 pad = ((lang_statement_union_type *)
4329 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
4330 if (pad != NULL
4331 && pad->header.type == lang_padding_statement_enum
4332 && pad->padding_statement.output_section == output_section)
4334 /* Use the existing pad statement. */
4336 else if ((pad = *ptr) != NULL
4337 && pad->header.type == lang_padding_statement_enum
4338 && pad->padding_statement.output_section == output_section)
4340 /* Use the existing pad statement. */
4342 else
4344 /* Make a new padding statement, linked into existing chain. */
4345 pad = stat_alloc (sizeof (lang_padding_statement_type));
4346 pad->header.next = *ptr;
4347 *ptr = pad;
4348 pad->header.type = lang_padding_statement_enum;
4349 pad->padding_statement.output_section = output_section;
4350 if (fill == NULL)
4351 fill = &zero_fill;
4352 pad->padding_statement.fill = fill;
4354 pad->padding_statement.output_offset = dot - output_section->vma;
4355 pad->padding_statement.size = alignment_needed;
4356 output_section->size += alignment_needed;
4359 /* Work out how much this section will move the dot point. */
4361 static bfd_vma
4362 size_input_section
4363 (lang_statement_union_type **this_ptr,
4364 lang_output_section_statement_type *output_section_statement,
4365 fill_type *fill,
4366 bfd_vma dot)
4368 lang_input_section_type *is = &((*this_ptr)->input_section);
4369 asection *i = is->section;
4371 if (!((lang_input_statement_type *) i->owner->usrdata)->just_syms_flag
4372 && (i->flags & SEC_EXCLUDE) == 0)
4374 unsigned int alignment_needed;
4375 asection *o;
4377 /* Align this section first to the input sections requirement,
4378 then to the output section's requirement. If this alignment
4379 is greater than any seen before, then record it too. Perform
4380 the alignment by inserting a magic 'padding' statement. */
4382 if (output_section_statement->subsection_alignment != -1)
4383 i->alignment_power = output_section_statement->subsection_alignment;
4385 o = output_section_statement->bfd_section;
4386 if (o->alignment_power < i->alignment_power)
4387 o->alignment_power = i->alignment_power;
4389 alignment_needed = align_power (dot, i->alignment_power) - dot;
4391 if (alignment_needed != 0)
4393 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
4394 dot += alignment_needed;
4397 /* Remember where in the output section this input section goes. */
4399 i->output_offset = dot - o->vma;
4401 /* Mark how big the output section must be to contain this now. */
4402 dot += TO_ADDR (i->size);
4403 o->size = TO_SIZE (dot - o->vma);
4405 else
4407 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
4410 return dot;
4413 static int
4414 sort_sections_by_lma (const void *arg1, const void *arg2)
4416 const asection *sec1 = *(const asection **) arg1;
4417 const asection *sec2 = *(const asection **) arg2;
4419 if (bfd_section_lma (sec1->owner, sec1)
4420 < bfd_section_lma (sec2->owner, sec2))
4421 return -1;
4422 else if (bfd_section_lma (sec1->owner, sec1)
4423 > bfd_section_lma (sec2->owner, sec2))
4424 return 1;
4425 else if (sec1->id < sec2->id)
4426 return -1;
4427 else if (sec1->id > sec2->id)
4428 return 1;
4430 return 0;
4433 #define IGNORE_SECTION(s) \
4434 ((s->flags & SEC_NEVER_LOAD) != 0 \
4435 || (s->flags & SEC_ALLOC) == 0 \
4436 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4437 && (s->flags & SEC_LOAD) == 0))
4439 /* Check to see if any allocated sections overlap with other allocated
4440 sections. This can happen if a linker script specifies the output
4441 section addresses of the two sections. Also check whether any memory
4442 region has overflowed. */
4444 static void
4445 lang_check_section_addresses (void)
4447 asection *s, *os;
4448 asection **sections, **spp;
4449 unsigned int count;
4450 bfd_vma s_start;
4451 bfd_vma s_end;
4452 bfd_vma os_start;
4453 bfd_vma os_end;
4454 bfd_size_type amt;
4455 lang_memory_region_type *m;
4457 if (bfd_count_sections (link_info.output_bfd) <= 1)
4458 return;
4460 amt = bfd_count_sections (link_info.output_bfd) * sizeof (asection *);
4461 sections = xmalloc (amt);
4463 /* Scan all sections in the output list. */
4464 count = 0;
4465 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
4467 /* Only consider loadable sections with real contents. */
4468 if (IGNORE_SECTION (s) || s->size == 0)
4469 continue;
4471 sections[count] = s;
4472 count++;
4475 if (count <= 1)
4476 return;
4478 qsort (sections, (size_t) count, sizeof (asection *),
4479 sort_sections_by_lma);
4481 spp = sections;
4482 s = *spp++;
4483 s_start = bfd_section_lma (link_info.output_bfd, s);
4484 s_end = s_start + TO_ADDR (s->size) - 1;
4485 for (count--; count; count--)
4487 /* We must check the sections' LMA addresses not their VMA
4488 addresses because overlay sections can have overlapping VMAs
4489 but they must have distinct LMAs. */
4490 os = s;
4491 os_start = s_start;
4492 os_end = s_end;
4493 s = *spp++;
4494 s_start = bfd_section_lma (link_info.output_bfd, s);
4495 s_end = s_start + TO_ADDR (s->size) - 1;
4497 /* Look for an overlap. */
4498 if (s_end >= os_start && s_start <= os_end)
4499 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4500 s->name, s_start, s_end, os->name, os_start, os_end);
4503 free (sections);
4505 /* If any memory region has overflowed, report by how much.
4506 We do not issue this diagnostic for regions that had sections
4507 explicitly placed outside their bounds; os_region_check's
4508 diagnostics are adequate for that case.
4510 FIXME: It is conceivable that m->current - (m->origin + m->length)
4511 might overflow a 32-bit integer. There is, alas, no way to print
4512 a bfd_vma quantity in decimal. */
4513 for (m = lang_memory_region_list; m; m = m->next)
4514 if (m->had_full_message)
4515 einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
4516 m->name_list.name, (long)(m->current - (m->origin + m->length)));
4520 /* Make sure the new address is within the region. We explicitly permit the
4521 current address to be at the exact end of the region when the address is
4522 non-zero, in case the region is at the end of addressable memory and the
4523 calculation wraps around. */
4525 static void
4526 os_region_check (lang_output_section_statement_type *os,
4527 lang_memory_region_type *region,
4528 etree_type *tree,
4529 bfd_vma base)
4531 if ((region->current < region->origin
4532 || (region->current - region->origin > region->length))
4533 && ((region->current != region->origin + region->length)
4534 || base == 0))
4536 if (tree != NULL)
4538 einfo (_("%X%P: address 0x%v of %B section `%s'"
4539 " is not within region `%s'\n"),
4540 region->current,
4541 os->bfd_section->owner,
4542 os->bfd_section->name,
4543 region->name_list.name);
4545 else if (!region->had_full_message)
4547 region->had_full_message = TRUE;
4549 einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
4550 os->bfd_section->owner,
4551 os->bfd_section->name,
4552 region->name_list.name);
4557 /* Set the sizes for all the output sections. */
4559 static bfd_vma
4560 lang_size_sections_1
4561 (lang_statement_union_type *s,
4562 lang_output_section_statement_type *output_section_statement,
4563 lang_statement_union_type **prev,
4564 fill_type *fill,
4565 bfd_vma dot,
4566 bfd_boolean *relax,
4567 bfd_boolean check_regions)
4569 /* Size up the sections from their constituent parts. */
4570 for (; s != NULL; s = s->header.next)
4572 switch (s->header.type)
4574 case lang_output_section_statement_enum:
4576 bfd_vma newdot, after;
4577 lang_output_section_statement_type *os;
4578 lang_memory_region_type *r;
4580 os = &s->output_section_statement;
4581 if (os->addr_tree != NULL)
4583 os->processed_vma = FALSE;
4584 exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
4586 if (expld.result.valid_p)
4587 dot = expld.result.value + expld.result.section->vma;
4588 else if (expld.phase != lang_mark_phase_enum)
4589 einfo (_("%F%S: non constant or forward reference"
4590 " address expression for section %s\n"),
4591 os->name);
4594 if (os->bfd_section == NULL)
4595 /* This section was removed or never actually created. */
4596 break;
4598 /* If this is a COFF shared library section, use the size and
4599 address from the input section. FIXME: This is COFF
4600 specific; it would be cleaner if there were some other way
4601 to do this, but nothing simple comes to mind. */
4602 if (((bfd_get_flavour (link_info.output_bfd)
4603 == bfd_target_ecoff_flavour)
4604 || (bfd_get_flavour (link_info.output_bfd)
4605 == bfd_target_coff_flavour))
4606 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
4608 asection *input;
4610 if (os->children.head == NULL
4611 || os->children.head->header.next != NULL
4612 || (os->children.head->header.type
4613 != lang_input_section_enum))
4614 einfo (_("%P%X: Internal error on COFF shared library"
4615 " section %s\n"), os->name);
4617 input = os->children.head->input_section.section;
4618 bfd_set_section_vma (os->bfd_section->owner,
4619 os->bfd_section,
4620 bfd_section_vma (input->owner, input));
4621 os->bfd_section->size = input->size;
4622 break;
4625 newdot = dot;
4626 if (bfd_is_abs_section (os->bfd_section))
4628 /* No matter what happens, an abs section starts at zero. */
4629 ASSERT (os->bfd_section->vma == 0);
4631 else
4633 int align;
4635 if (os->addr_tree == NULL)
4637 /* No address specified for this section, get one
4638 from the region specification. */
4639 if (os->region == NULL
4640 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
4641 && os->region->name_list.name[0] == '*'
4642 && strcmp (os->region->name_list.name,
4643 DEFAULT_MEMORY_REGION) == 0))
4645 os->region = lang_memory_default (os->bfd_section);
4648 /* If a loadable section is using the default memory
4649 region, and some non default memory regions were
4650 defined, issue an error message. */
4651 if (!os->ignored
4652 && !IGNORE_SECTION (os->bfd_section)
4653 && ! link_info.relocatable
4654 && check_regions
4655 && strcmp (os->region->name_list.name,
4656 DEFAULT_MEMORY_REGION) == 0
4657 && lang_memory_region_list != NULL
4658 && (strcmp (lang_memory_region_list->name_list.name,
4659 DEFAULT_MEMORY_REGION) != 0
4660 || lang_memory_region_list->next != NULL)
4661 && expld.phase != lang_mark_phase_enum)
4663 /* By default this is an error rather than just a
4664 warning because if we allocate the section to the
4665 default memory region we can end up creating an
4666 excessively large binary, or even seg faulting when
4667 attempting to perform a negative seek. See
4668 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4669 for an example of this. This behaviour can be
4670 overridden by the using the --no-check-sections
4671 switch. */
4672 if (command_line.check_section_addresses)
4673 einfo (_("%P%F: error: no memory region specified"
4674 " for loadable section `%s'\n"),
4675 bfd_get_section_name (link_info.output_bfd,
4676 os->bfd_section));
4677 else
4678 einfo (_("%P: warning: no memory region specified"
4679 " for loadable section `%s'\n"),
4680 bfd_get_section_name (link_info.output_bfd,
4681 os->bfd_section));
4684 newdot = os->region->current;
4685 align = os->bfd_section->alignment_power;
4687 else
4688 align = os->section_alignment;
4690 /* Align to what the section needs. */
4691 if (align > 0)
4693 bfd_vma savedot = newdot;
4694 newdot = align_power (newdot, align);
4696 if (newdot != savedot
4697 && (config.warn_section_align
4698 || os->addr_tree != NULL)
4699 && expld.phase != lang_mark_phase_enum)
4700 einfo (_("%P: warning: changing start of section"
4701 " %s by %lu bytes\n"),
4702 os->name, (unsigned long) (newdot - savedot));
4705 /* PR 6945: Do not update the vma's of output sections
4706 when performing a relocatable link on COFF objects. */
4707 if (! link_info.relocatable
4708 || (bfd_get_flavour (link_info.output_bfd)
4709 != bfd_target_coff_flavour))
4710 bfd_set_section_vma (0, os->bfd_section, newdot);
4712 os->bfd_section->output_offset = 0;
4715 lang_size_sections_1 (os->children.head, os, &os->children.head,
4716 os->fill, newdot, relax, check_regions);
4718 os->processed_vma = TRUE;
4720 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4721 /* Except for some special linker created sections,
4722 no output section should change from zero size
4723 after strip_excluded_output_sections. A non-zero
4724 size on an ignored section indicates that some
4725 input section was not sized early enough. */
4726 ASSERT (os->bfd_section->size == 0);
4727 else
4729 dot = os->bfd_section->vma;
4731 /* Put the section within the requested block size, or
4732 align at the block boundary. */
4733 after = ((dot
4734 + TO_ADDR (os->bfd_section->size)
4735 + os->block_value - 1)
4736 & - (bfd_vma) os->block_value);
4738 os->bfd_section->size = TO_SIZE (after - os->bfd_section->vma);
4741 /* Set section lma. */
4742 r = os->region;
4743 if (r == NULL)
4744 r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
4746 if (os->load_base)
4748 bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
4749 os->bfd_section->lma = lma;
4751 else if (os->lma_region != NULL)
4753 bfd_vma lma = os->lma_region->current;
4755 if (os->section_alignment != -1)
4756 lma = align_power (lma, os->section_alignment);
4757 os->bfd_section->lma = lma;
4759 else if (r->last_os != NULL
4760 && (os->bfd_section->flags & SEC_ALLOC) != 0)
4762 bfd_vma lma;
4763 asection *last;
4765 last = r->last_os->output_section_statement.bfd_section;
4767 /* A backwards move of dot should be accompanied by
4768 an explicit assignment to the section LMA (ie.
4769 os->load_base set) because backwards moves can
4770 create overlapping LMAs. */
4771 if (dot < last->vma
4772 && os->bfd_section->size != 0
4773 && dot + os->bfd_section->size <= last->vma)
4775 /* If dot moved backwards then leave lma equal to
4776 vma. This is the old default lma, which might
4777 just happen to work when the backwards move is
4778 sufficiently large. Nag if this changes anything,
4779 so people can fix their linker scripts. */
4781 if (last->vma != last->lma)
4782 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4783 os->name);
4785 else
4787 /* If this is an overlay, set the current lma to that
4788 at the end of the previous section. */
4789 if (os->sectype == overlay_section)
4790 lma = last->lma + last->size;
4792 /* Otherwise, keep the same lma to vma relationship
4793 as the previous section. */
4794 else
4795 lma = dot + last->lma - last->vma;
4797 if (os->section_alignment != -1)
4798 lma = align_power (lma, os->section_alignment);
4799 os->bfd_section->lma = lma;
4802 os->processed_lma = TRUE;
4804 if (bfd_is_abs_section (os->bfd_section) || os->ignored)
4805 break;
4807 /* Keep track of normal sections using the default
4808 lma region. We use this to set the lma for
4809 following sections. Overlays or other linker
4810 script assignment to lma might mean that the
4811 default lma == vma is incorrect.
4812 To avoid warnings about dot moving backwards when using
4813 -Ttext, don't start tracking sections until we find one
4814 of non-zero size or with lma set differently to vma. */
4815 if (((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4816 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0)
4817 && (os->bfd_section->flags & SEC_ALLOC) != 0
4818 && (os->bfd_section->size != 0
4819 || (r->last_os == NULL
4820 && os->bfd_section->vma != os->bfd_section->lma)
4821 || (r->last_os != NULL
4822 && dot >= (r->last_os->output_section_statement
4823 .bfd_section->vma)))
4824 && os->lma_region == NULL
4825 && !link_info.relocatable)
4826 r->last_os = s;
4828 /* .tbss sections effectively have zero size. */
4829 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
4830 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
4831 || link_info.relocatable)
4832 dot += TO_ADDR (os->bfd_section->size);
4834 if (os->update_dot_tree != 0)
4835 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
4837 /* Update dot in the region ?
4838 We only do this if the section is going to be allocated,
4839 since unallocated sections do not contribute to the region's
4840 overall size in memory.
4842 If the SEC_NEVER_LOAD bit is not set, it will affect the
4843 addresses of sections after it. We have to update
4844 dot. */
4845 if (os->region != NULL
4846 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
4847 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
4849 os->region->current = dot;
4851 if (check_regions)
4852 /* Make sure the new address is within the region. */
4853 os_region_check (os, os->region, os->addr_tree,
4854 os->bfd_section->vma);
4856 if (os->lma_region != NULL && os->lma_region != os->region
4857 && (os->bfd_section->flags & SEC_LOAD))
4859 os->lma_region->current
4860 = os->bfd_section->lma + TO_ADDR (os->bfd_section->size);
4862 if (check_regions)
4863 os_region_check (os, os->lma_region, NULL,
4864 os->bfd_section->lma);
4868 break;
4870 case lang_constructors_statement_enum:
4871 dot = lang_size_sections_1 (constructor_list.head,
4872 output_section_statement,
4873 &s->wild_statement.children.head,
4874 fill, dot, relax, check_regions);
4875 break;
4877 case lang_data_statement_enum:
4879 unsigned int size = 0;
4881 s->data_statement.output_offset =
4882 dot - output_section_statement->bfd_section->vma;
4883 s->data_statement.output_section =
4884 output_section_statement->bfd_section;
4886 /* We might refer to provided symbols in the expression, and
4887 need to mark them as needed. */
4888 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
4890 switch (s->data_statement.type)
4892 default:
4893 abort ();
4894 case QUAD:
4895 case SQUAD:
4896 size = QUAD_SIZE;
4897 break;
4898 case LONG:
4899 size = LONG_SIZE;
4900 break;
4901 case SHORT:
4902 size = SHORT_SIZE;
4903 break;
4904 case BYTE:
4905 size = BYTE_SIZE;
4906 break;
4908 if (size < TO_SIZE ((unsigned) 1))
4909 size = TO_SIZE ((unsigned) 1);
4910 dot += TO_ADDR (size);
4911 output_section_statement->bfd_section->size += size;
4913 break;
4915 case lang_reloc_statement_enum:
4917 int size;
4919 s->reloc_statement.output_offset =
4920 dot - output_section_statement->bfd_section->vma;
4921 s->reloc_statement.output_section =
4922 output_section_statement->bfd_section;
4923 size = bfd_get_reloc_size (s->reloc_statement.howto);
4924 dot += TO_ADDR (size);
4925 output_section_statement->bfd_section->size += size;
4927 break;
4929 case lang_wild_statement_enum:
4930 dot = lang_size_sections_1 (s->wild_statement.children.head,
4931 output_section_statement,
4932 &s->wild_statement.children.head,
4933 fill, dot, relax, check_regions);
4934 break;
4936 case lang_object_symbols_statement_enum:
4937 link_info.create_object_symbols_section =
4938 output_section_statement->bfd_section;
4939 break;
4941 case lang_output_statement_enum:
4942 case lang_target_statement_enum:
4943 break;
4945 case lang_input_section_enum:
4947 asection *i;
4949 i = (*prev)->input_section.section;
4950 if (relax)
4952 bfd_boolean again;
4954 if (! bfd_relax_section (i->owner, i, &link_info, &again))
4955 einfo (_("%P%F: can't relax section: %E\n"));
4956 if (again)
4957 *relax = TRUE;
4959 dot = size_input_section (prev, output_section_statement,
4960 output_section_statement->fill, dot);
4962 break;
4964 case lang_input_statement_enum:
4965 break;
4967 case lang_fill_statement_enum:
4968 s->fill_statement.output_section =
4969 output_section_statement->bfd_section;
4971 fill = s->fill_statement.fill;
4972 break;
4974 case lang_assignment_statement_enum:
4976 bfd_vma newdot = dot;
4977 etree_type *tree = s->assignment_statement.exp;
4979 expld.dataseg.relro = exp_dataseg_relro_none;
4981 exp_fold_tree (tree,
4982 output_section_statement->bfd_section,
4983 &newdot);
4985 if (expld.dataseg.relro == exp_dataseg_relro_start)
4987 if (!expld.dataseg.relro_start_stat)
4988 expld.dataseg.relro_start_stat = s;
4989 else
4991 ASSERT (expld.dataseg.relro_start_stat == s);
4994 else if (expld.dataseg.relro == exp_dataseg_relro_end)
4996 if (!expld.dataseg.relro_end_stat)
4997 expld.dataseg.relro_end_stat = s;
4998 else
5000 ASSERT (expld.dataseg.relro_end_stat == s);
5003 expld.dataseg.relro = exp_dataseg_relro_none;
5005 /* This symbol is relative to this section. */
5006 if ((tree->type.node_class == etree_provided
5007 || tree->type.node_class == etree_assign)
5008 && (tree->assign.dst [0] != '.'
5009 || tree->assign.dst [1] != '\0'))
5010 output_section_statement->section_relative_symbol = 1;
5012 if (!output_section_statement->ignored)
5014 if (output_section_statement == abs_output_section)
5016 /* If we don't have an output section, then just adjust
5017 the default memory address. */
5018 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
5019 FALSE)->current = newdot;
5021 else if (newdot != dot)
5023 /* Insert a pad after this statement. We can't
5024 put the pad before when relaxing, in case the
5025 assignment references dot. */
5026 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
5027 output_section_statement->bfd_section, dot);
5029 /* Don't neuter the pad below when relaxing. */
5030 s = s->header.next;
5032 /* If dot is advanced, this implies that the section
5033 should have space allocated to it, unless the
5034 user has explicitly stated that the section
5035 should never be loaded. */
5036 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
5037 output_section_statement->bfd_section->flags |= SEC_ALLOC;
5039 dot = newdot;
5042 break;
5044 case lang_padding_statement_enum:
5045 /* If this is the first time lang_size_sections is called,
5046 we won't have any padding statements. If this is the
5047 second or later passes when relaxing, we should allow
5048 padding to shrink. If padding is needed on this pass, it
5049 will be added back in. */
5050 s->padding_statement.size = 0;
5052 /* Make sure output_offset is valid. If relaxation shrinks
5053 the section and this pad isn't needed, it's possible to
5054 have output_offset larger than the final size of the
5055 section. bfd_set_section_contents will complain even for
5056 a pad size of zero. */
5057 s->padding_statement.output_offset
5058 = dot - output_section_statement->bfd_section->vma;
5059 break;
5061 case lang_group_statement_enum:
5062 dot = lang_size_sections_1 (s->group_statement.children.head,
5063 output_section_statement,
5064 &s->group_statement.children.head,
5065 fill, dot, relax, check_regions);
5066 break;
5068 case lang_insert_statement_enum:
5069 break;
5071 /* We can only get here when relaxing is turned on. */
5072 case lang_address_statement_enum:
5073 break;
5075 default:
5076 FAIL ();
5077 break;
5079 prev = &s->header.next;
5081 return dot;
5084 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5085 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5086 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5087 segments. We are allowed an opportunity to override this decision. */
5089 bfd_boolean
5090 ldlang_override_segment_assignment (struct bfd_link_info * info ATTRIBUTE_UNUSED,
5091 bfd * abfd ATTRIBUTE_UNUSED,
5092 asection * current_section,
5093 asection * previous_section,
5094 bfd_boolean new_segment)
5096 lang_output_section_statement_type * cur;
5097 lang_output_section_statement_type * prev;
5099 /* The checks below are only necessary when the BFD library has decided
5100 that the two sections ought to be placed into the same segment. */
5101 if (new_segment)
5102 return TRUE;
5104 /* Paranoia checks. */
5105 if (current_section == NULL || previous_section == NULL)
5106 return new_segment;
5108 /* Find the memory regions associated with the two sections.
5109 We call lang_output_section_find() here rather than scanning the list
5110 of output sections looking for a matching section pointer because if
5111 we have a large number of sections then a hash lookup is faster. */
5112 cur = lang_output_section_find (current_section->name);
5113 prev = lang_output_section_find (previous_section->name);
5115 /* More paranoia. */
5116 if (cur == NULL || prev == NULL)
5117 return new_segment;
5119 /* If the regions are different then force the sections to live in
5120 different segments. See the email thread starting at the following
5121 URL for the reasons why this is necessary:
5122 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5123 return cur->region != prev->region;
5126 void
5127 one_lang_size_sections_pass (bfd_boolean *relax, bfd_boolean check_regions)
5129 lang_statement_iteration++;
5130 lang_size_sections_1 (statement_list.head, abs_output_section,
5131 &statement_list.head, 0, 0, relax, check_regions);
5134 void
5135 lang_size_sections (bfd_boolean *relax, bfd_boolean check_regions)
5137 expld.phase = lang_allocating_phase_enum;
5138 expld.dataseg.phase = exp_dataseg_none;
5140 one_lang_size_sections_pass (relax, check_regions);
5141 if (expld.dataseg.phase == exp_dataseg_end_seen
5142 && link_info.relro && expld.dataseg.relro_end)
5144 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5145 to put expld.dataseg.relro on a (common) page boundary. */
5146 bfd_vma min_base, old_base, relro_end, maxpage;
5148 expld.dataseg.phase = exp_dataseg_relro_adjust;
5149 maxpage = expld.dataseg.maxpagesize;
5150 /* MIN_BASE is the absolute minimum address we are allowed to start the
5151 read-write segment (byte before will be mapped read-only). */
5152 min_base = (expld.dataseg.min_base + maxpage - 1) & ~(maxpage - 1);
5153 /* OLD_BASE is the address for a feasible minimum address which will
5154 still not cause a data overlap inside MAXPAGE causing file offset skip
5155 by MAXPAGE. */
5156 old_base = expld.dataseg.base;
5157 expld.dataseg.base += (-expld.dataseg.relro_end
5158 & (expld.dataseg.pagesize - 1));
5159 /* Compute the expected PT_GNU_RELRO segment end. */
5160 relro_end = ((expld.dataseg.relro_end + expld.dataseg.pagesize - 1)
5161 & ~(expld.dataseg.pagesize - 1));
5162 if (min_base + maxpage < expld.dataseg.base)
5164 expld.dataseg.base -= maxpage;
5165 relro_end -= maxpage;
5167 lang_reset_memory_regions ();
5168 one_lang_size_sections_pass (relax, check_regions);
5169 if (expld.dataseg.relro_end > relro_end)
5171 /* The alignment of sections between DATA_SEGMENT_ALIGN
5172 and DATA_SEGMENT_RELRO_END caused huge padding to be
5173 inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so
5174 that the section alignments will fit in. */
5175 asection *sec;
5176 unsigned int max_alignment_power = 0;
5178 /* Find maximum alignment power of sections between
5179 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5180 for (sec = link_info.output_bfd->sections; sec; sec = sec->next)
5181 if (sec->vma >= expld.dataseg.base
5182 && sec->vma < expld.dataseg.relro_end
5183 && sec->alignment_power > max_alignment_power)
5184 max_alignment_power = sec->alignment_power;
5186 if (((bfd_vma) 1 << max_alignment_power) < expld.dataseg.pagesize)
5188 if (expld.dataseg.base - (1 << max_alignment_power) < old_base)
5189 expld.dataseg.base += expld.dataseg.pagesize;
5190 expld.dataseg.base -= (1 << max_alignment_power);
5191 lang_reset_memory_regions ();
5192 one_lang_size_sections_pass (relax, check_regions);
5195 link_info.relro_start = expld.dataseg.base;
5196 link_info.relro_end = expld.dataseg.relro_end;
5198 else if (expld.dataseg.phase == exp_dataseg_end_seen)
5200 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5201 a page could be saved in the data segment. */
5202 bfd_vma first, last;
5204 first = -expld.dataseg.base & (expld.dataseg.pagesize - 1);
5205 last = expld.dataseg.end & (expld.dataseg.pagesize - 1);
5206 if (first && last
5207 && ((expld.dataseg.base & ~(expld.dataseg.pagesize - 1))
5208 != (expld.dataseg.end & ~(expld.dataseg.pagesize - 1)))
5209 && first + last <= expld.dataseg.pagesize)
5211 expld.dataseg.phase = exp_dataseg_adjust;
5212 lang_reset_memory_regions ();
5213 one_lang_size_sections_pass (relax, check_regions);
5217 expld.phase = lang_final_phase_enum;
5220 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5222 static bfd_vma
5223 lang_do_assignments_1 (lang_statement_union_type *s,
5224 lang_output_section_statement_type *current_os,
5225 fill_type *fill,
5226 bfd_vma dot)
5228 for (; s != NULL; s = s->header.next)
5230 switch (s->header.type)
5232 case lang_constructors_statement_enum:
5233 dot = lang_do_assignments_1 (constructor_list.head,
5234 current_os, fill, dot);
5235 break;
5237 case lang_output_section_statement_enum:
5239 lang_output_section_statement_type *os;
5241 os = &(s->output_section_statement);
5242 if (os->bfd_section != NULL && !os->ignored)
5244 dot = os->bfd_section->vma;
5246 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
5248 /* .tbss sections effectively have zero size. */
5249 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
5250 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
5251 || link_info.relocatable)
5252 dot += TO_ADDR (os->bfd_section->size);
5254 if (os->update_dot_tree != NULL)
5255 exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
5258 break;
5260 case lang_wild_statement_enum:
5262 dot = lang_do_assignments_1 (s->wild_statement.children.head,
5263 current_os, fill, dot);
5264 break;
5266 case lang_object_symbols_statement_enum:
5267 case lang_output_statement_enum:
5268 case lang_target_statement_enum:
5269 break;
5271 case lang_data_statement_enum:
5272 exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
5273 if (expld.result.valid_p)
5274 s->data_statement.value = (expld.result.value
5275 + expld.result.section->vma);
5276 else
5277 einfo (_("%F%P: invalid data statement\n"));
5279 unsigned int size;
5280 switch (s->data_statement.type)
5282 default:
5283 abort ();
5284 case QUAD:
5285 case SQUAD:
5286 size = QUAD_SIZE;
5287 break;
5288 case LONG:
5289 size = LONG_SIZE;
5290 break;
5291 case SHORT:
5292 size = SHORT_SIZE;
5293 break;
5294 case BYTE:
5295 size = BYTE_SIZE;
5296 break;
5298 if (size < TO_SIZE ((unsigned) 1))
5299 size = TO_SIZE ((unsigned) 1);
5300 dot += TO_ADDR (size);
5302 break;
5304 case lang_reloc_statement_enum:
5305 exp_fold_tree (s->reloc_statement.addend_exp,
5306 bfd_abs_section_ptr, &dot);
5307 if (expld.result.valid_p)
5308 s->reloc_statement.addend_value = expld.result.value;
5309 else
5310 einfo (_("%F%P: invalid reloc statement\n"));
5311 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
5312 break;
5314 case lang_input_section_enum:
5316 asection *in = s->input_section.section;
5318 if ((in->flags & SEC_EXCLUDE) == 0)
5319 dot += TO_ADDR (in->size);
5321 break;
5323 case lang_input_statement_enum:
5324 break;
5326 case lang_fill_statement_enum:
5327 fill = s->fill_statement.fill;
5328 break;
5330 case lang_assignment_statement_enum:
5331 exp_fold_tree (s->assignment_statement.exp,
5332 current_os->bfd_section,
5333 &dot);
5334 break;
5336 case lang_padding_statement_enum:
5337 dot += TO_ADDR (s->padding_statement.size);
5338 break;
5340 case lang_group_statement_enum:
5341 dot = lang_do_assignments_1 (s->group_statement.children.head,
5342 current_os, fill, dot);
5343 break;
5345 case lang_insert_statement_enum:
5346 break;
5348 case lang_address_statement_enum:
5349 break;
5351 default:
5352 FAIL ();
5353 break;
5356 return dot;
5359 void
5360 lang_do_assignments (void)
5362 lang_statement_iteration++;
5363 lang_do_assignments_1 (statement_list.head, abs_output_section, NULL, 0);
5366 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5367 operator .startof. (section_name), it produces an undefined symbol
5368 .startof.section_name. Similarly, when it sees
5369 .sizeof. (section_name), it produces an undefined symbol
5370 .sizeof.section_name. For all the output sections, we look for
5371 such symbols, and set them to the correct value. */
5373 static void
5374 lang_set_startof (void)
5376 asection *s;
5378 if (link_info.relocatable)
5379 return;
5381 for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
5383 const char *secname;
5384 char *buf;
5385 struct bfd_link_hash_entry *h;
5387 secname = bfd_get_section_name (link_info.output_bfd, s);
5388 buf = xmalloc (10 + strlen (secname));
5390 sprintf (buf, ".startof.%s", secname);
5391 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5392 if (h != NULL && h->type == bfd_link_hash_undefined)
5394 h->type = bfd_link_hash_defined;
5395 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, s);
5396 h->u.def.section = bfd_abs_section_ptr;
5399 sprintf (buf, ".sizeof.%s", secname);
5400 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
5401 if (h != NULL && h->type == bfd_link_hash_undefined)
5403 h->type = bfd_link_hash_defined;
5404 h->u.def.value = TO_ADDR (s->size);
5405 h->u.def.section = bfd_abs_section_ptr;
5408 free (buf);
5412 static void
5413 lang_end (void)
5415 struct bfd_link_hash_entry *h;
5416 bfd_boolean warn;
5418 if ((link_info.relocatable && !link_info.gc_sections)
5419 || (link_info.shared && !link_info.executable))
5420 warn = entry_from_cmdline;
5421 else
5422 warn = TRUE;
5424 /* Force the user to specify a root when generating a relocatable with
5425 --gc-sections. */
5426 if (link_info.gc_sections && link_info.relocatable
5427 && (entry_symbol.name == NULL
5428 && ldlang_undef_chain_list_head == NULL))
5429 einfo (_("%P%F: gc-sections requires either an entry or "
5430 "an undefined symbol\n"));
5432 if (entry_symbol.name == NULL)
5434 /* No entry has been specified. Look for the default entry, but
5435 don't warn if we don't find it. */
5436 entry_symbol.name = entry_symbol_default;
5437 warn = FALSE;
5440 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
5441 FALSE, FALSE, TRUE);
5442 if (h != NULL
5443 && (h->type == bfd_link_hash_defined
5444 || h->type == bfd_link_hash_defweak)
5445 && h->u.def.section->output_section != NULL)
5447 bfd_vma val;
5449 val = (h->u.def.value
5450 + bfd_get_section_vma (link_info.output_bfd,
5451 h->u.def.section->output_section)
5452 + h->u.def.section->output_offset);
5453 if (! bfd_set_start_address (link_info.output_bfd, val))
5454 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
5456 else
5458 bfd_vma val;
5459 const char *send;
5461 /* We couldn't find the entry symbol. Try parsing it as a
5462 number. */
5463 val = bfd_scan_vma (entry_symbol.name, &send, 0);
5464 if (*send == '\0')
5466 if (! bfd_set_start_address (link_info.output_bfd, val))
5467 einfo (_("%P%F: can't set start address\n"));
5469 else
5471 asection *ts;
5473 /* Can't find the entry symbol, and it's not a number. Use
5474 the first address in the text section. */
5475 ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
5476 if (ts != NULL)
5478 if (warn)
5479 einfo (_("%P: warning: cannot find entry symbol %s;"
5480 " defaulting to %V\n"),
5481 entry_symbol.name,
5482 bfd_get_section_vma (link_info.output_bfd, ts));
5483 if (!(bfd_set_start_address
5484 (link_info.output_bfd,
5485 bfd_get_section_vma (link_info.output_bfd, ts))))
5486 einfo (_("%P%F: can't set start address\n"));
5488 else
5490 if (warn)
5491 einfo (_("%P: warning: cannot find entry symbol %s;"
5492 " not setting start address\n"),
5493 entry_symbol.name);
5498 /* Don't bfd_hash_table_free (&lang_definedness_table);
5499 map file output may result in a call of lang_track_definedness. */
5502 /* This is a small function used when we want to ignore errors from
5503 BFD. */
5505 static void
5506 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
5508 /* Don't do anything. */
5511 /* Check that the architecture of all the input files is compatible
5512 with the output file. Also call the backend to let it do any
5513 other checking that is needed. */
5515 static void
5516 lang_check (void)
5518 lang_statement_union_type *file;
5519 bfd *input_bfd;
5520 const bfd_arch_info_type *compatible;
5522 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
5524 input_bfd = file->input_statement.the_bfd;
5525 compatible
5526 = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
5527 command_line.accept_unknown_input_arch);
5529 /* In general it is not possible to perform a relocatable
5530 link between differing object formats when the input
5531 file has relocations, because the relocations in the
5532 input format may not have equivalent representations in
5533 the output format (and besides BFD does not translate
5534 relocs for other link purposes than a final link). */
5535 if ((link_info.relocatable || link_info.emitrelocations)
5536 && (compatible == NULL
5537 || (bfd_get_flavour (input_bfd)
5538 != bfd_get_flavour (link_info.output_bfd)))
5539 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
5541 einfo (_("%P%F: Relocatable linking with relocations from"
5542 " format %s (%B) to format %s (%B) is not supported\n"),
5543 bfd_get_target (input_bfd), input_bfd,
5544 bfd_get_target (link_info.output_bfd), link_info.output_bfd);
5545 /* einfo with %F exits. */
5548 if (compatible == NULL)
5550 if (command_line.warn_mismatch)
5551 einfo (_("%P%X: %s architecture of input file `%B'"
5552 " is incompatible with %s output\n"),
5553 bfd_printable_name (input_bfd), input_bfd,
5554 bfd_printable_name (link_info.output_bfd));
5556 else if (bfd_count_sections (input_bfd))
5558 /* If the input bfd has no contents, it shouldn't set the
5559 private data of the output bfd. */
5561 bfd_error_handler_type pfn = NULL;
5563 /* If we aren't supposed to warn about mismatched input
5564 files, temporarily set the BFD error handler to a
5565 function which will do nothing. We still want to call
5566 bfd_merge_private_bfd_data, since it may set up
5567 information which is needed in the output file. */
5568 if (! command_line.warn_mismatch)
5569 pfn = bfd_set_error_handler (ignore_bfd_errors);
5570 if (! bfd_merge_private_bfd_data (input_bfd, link_info.output_bfd))
5572 if (command_line.warn_mismatch)
5573 einfo (_("%P%X: failed to merge target specific data"
5574 " of file %B\n"), input_bfd);
5576 if (! command_line.warn_mismatch)
5577 bfd_set_error_handler (pfn);
5582 /* Look through all the global common symbols and attach them to the
5583 correct section. The -sort-common command line switch may be used
5584 to roughly sort the entries by alignment. */
5586 static void
5587 lang_common (void)
5589 if (command_line.inhibit_common_definition)
5590 return;
5591 if (link_info.relocatable
5592 && ! command_line.force_common_definition)
5593 return;
5595 if (! config.sort_common)
5596 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
5597 else
5599 unsigned int power;
5601 if (config.sort_common == sort_descending)
5603 for (power = 4; power > 0; power--)
5604 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5606 power = 0;
5607 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5609 else
5611 for (power = 0; power <= 4; power++)
5612 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5614 power = UINT_MAX;
5615 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
5620 /* Place one common symbol in the correct section. */
5622 static bfd_boolean
5623 lang_one_common (struct bfd_link_hash_entry *h, void *info)
5625 unsigned int power_of_two;
5626 bfd_vma size;
5627 asection *section;
5629 if (h->type != bfd_link_hash_common)
5630 return TRUE;
5632 size = h->u.c.size;
5633 power_of_two = h->u.c.p->alignment_power;
5635 if (config.sort_common == sort_descending
5636 && power_of_two < *(unsigned int *) info)
5637 return TRUE;
5638 else if (config.sort_common == sort_ascending
5639 && power_of_two > *(unsigned int *) info)
5640 return TRUE;
5642 section = h->u.c.p->section;
5643 if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
5644 einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
5645 h->root.string);
5647 if (config.map_file != NULL)
5649 static bfd_boolean header_printed;
5650 int len;
5651 char *name;
5652 char buf[50];
5654 if (! header_printed)
5656 minfo (_("\nAllocating common symbols\n"));
5657 minfo (_("Common symbol size file\n\n"));
5658 header_printed = TRUE;
5661 name = bfd_demangle (link_info.output_bfd, h->root.string,
5662 DMGL_ANSI | DMGL_PARAMS);
5663 if (name == NULL)
5665 minfo ("%s", h->root.string);
5666 len = strlen (h->root.string);
5668 else
5670 minfo ("%s", name);
5671 len = strlen (name);
5672 free (name);
5675 if (len >= 19)
5677 print_nl ();
5678 len = 0;
5680 while (len < 20)
5682 print_space ();
5683 ++len;
5686 minfo ("0x");
5687 if (size <= 0xffffffff)
5688 sprintf (buf, "%lx", (unsigned long) size);
5689 else
5690 sprintf_vma (buf, size);
5691 minfo ("%s", buf);
5692 len = strlen (buf);
5694 while (len < 16)
5696 print_space ();
5697 ++len;
5700 minfo ("%B\n", section->owner);
5703 return TRUE;
5706 /* Run through the input files and ensure that every input section has
5707 somewhere to go. If one is found without a destination then create
5708 an input request and place it into the statement tree. */
5710 static void
5711 lang_place_orphans (void)
5713 LANG_FOR_EACH_INPUT_STATEMENT (file)
5715 asection *s;
5717 for (s = file->the_bfd->sections; s != NULL; s = s->next)
5719 if (s->output_section == NULL)
5721 /* This section of the file is not attached, root
5722 around for a sensible place for it to go. */
5724 if (file->just_syms_flag)
5725 bfd_link_just_syms (file->the_bfd, s, &link_info);
5726 else if ((s->flags & SEC_EXCLUDE) != 0)
5727 s->output_section = bfd_abs_section_ptr;
5728 else if (strcmp (s->name, "COMMON") == 0)
5730 /* This is a lonely common section which must have
5731 come from an archive. We attach to the section
5732 with the wildcard. */
5733 if (! link_info.relocatable
5734 || command_line.force_common_definition)
5736 if (default_common_section == NULL)
5737 default_common_section
5738 = lang_output_section_statement_lookup (".bss", 0,
5739 TRUE);
5740 lang_add_section (&default_common_section->children, s,
5741 default_common_section);
5744 else
5746 const char *name = s->name;
5747 int constraint = 0;
5749 if (config.unique_orphan_sections || unique_section_p (s))
5750 constraint = SPECIAL;
5752 if (!ldemul_place_orphan (s, name, constraint))
5754 lang_output_section_statement_type *os;
5755 os = lang_output_section_statement_lookup (name,
5756 constraint,
5757 TRUE);
5758 lang_add_section (&os->children, s, os);
5766 void
5767 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
5769 flagword *ptr_flags;
5771 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
5772 while (*flags)
5774 switch (*flags)
5776 case 'A': case 'a':
5777 *ptr_flags |= SEC_ALLOC;
5778 break;
5780 case 'R': case 'r':
5781 *ptr_flags |= SEC_READONLY;
5782 break;
5784 case 'W': case 'w':
5785 *ptr_flags |= SEC_DATA;
5786 break;
5788 case 'X': case 'x':
5789 *ptr_flags |= SEC_CODE;
5790 break;
5792 case 'L': case 'l':
5793 case 'I': case 'i':
5794 *ptr_flags |= SEC_LOAD;
5795 break;
5797 default:
5798 einfo (_("%P%F: invalid syntax in flags\n"));
5799 break;
5801 flags++;
5805 /* Call a function on each input file. This function will be called
5806 on an archive, but not on the elements. */
5808 void
5809 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
5811 lang_input_statement_type *f;
5813 for (f = (lang_input_statement_type *) input_file_chain.head;
5814 f != NULL;
5815 f = (lang_input_statement_type *) f->next_real_file)
5816 func (f);
5819 /* Call a function on each file. The function will be called on all
5820 the elements of an archive which are included in the link, but will
5821 not be called on the archive file itself. */
5823 void
5824 lang_for_each_file (void (*func) (lang_input_statement_type *))
5826 LANG_FOR_EACH_INPUT_STATEMENT (f)
5828 func (f);
5832 void
5833 ldlang_add_file (lang_input_statement_type *entry)
5835 lang_statement_append (&file_chain,
5836 (lang_statement_union_type *) entry,
5837 &entry->next);
5839 /* The BFD linker needs to have a list of all input BFDs involved in
5840 a link. */
5841 ASSERT (entry->the_bfd->link_next == NULL);
5842 ASSERT (entry->the_bfd != link_info.output_bfd);
5844 *link_info.input_bfds_tail = entry->the_bfd;
5845 link_info.input_bfds_tail = &entry->the_bfd->link_next;
5846 entry->the_bfd->usrdata = entry;
5847 bfd_set_gp_size (entry->the_bfd, g_switch_value);
5849 /* Look through the sections and check for any which should not be
5850 included in the link. We need to do this now, so that we can
5851 notice when the backend linker tries to report multiple
5852 definition errors for symbols which are in sections we aren't
5853 going to link. FIXME: It might be better to entirely ignore
5854 symbols which are defined in sections which are going to be
5855 discarded. This would require modifying the backend linker for
5856 each backend which might set the SEC_LINK_ONCE flag. If we do
5857 this, we should probably handle SEC_EXCLUDE in the same way. */
5859 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
5862 void
5863 lang_add_output (const char *name, int from_script)
5865 /* Make -o on command line override OUTPUT in script. */
5866 if (!had_output_filename || !from_script)
5868 output_filename = name;
5869 had_output_filename = TRUE;
5873 static lang_output_section_statement_type *current_section;
5875 static int
5876 topower (int x)
5878 unsigned int i = 1;
5879 int l;
5881 if (x < 0)
5882 return -1;
5884 for (l = 0; l < 32; l++)
5886 if (i >= (unsigned int) x)
5887 return l;
5888 i <<= 1;
5891 return 0;
5894 lang_output_section_statement_type *
5895 lang_enter_output_section_statement (const char *output_section_statement_name,
5896 etree_type *address_exp,
5897 enum section_type sectype,
5898 etree_type *align,
5899 etree_type *subalign,
5900 etree_type *ebase,
5901 int constraint)
5903 lang_output_section_statement_type *os;
5905 os = lang_output_section_statement_lookup (output_section_statement_name,
5906 constraint, TRUE);
5907 current_section = os;
5909 if (os->addr_tree == NULL)
5911 os->addr_tree = address_exp;
5913 os->sectype = sectype;
5914 if (sectype != noload_section)
5915 os->flags = SEC_NO_FLAGS;
5916 else
5917 os->flags = SEC_NEVER_LOAD;
5918 os->block_value = 1;
5920 /* Make next things chain into subchain of this. */
5921 push_stat_ptr (&os->children);
5923 os->subsection_alignment =
5924 topower (exp_get_value_int (subalign, -1, "subsection alignment"));
5925 os->section_alignment =
5926 topower (exp_get_value_int (align, -1, "section alignment"));
5928 os->load_base = ebase;
5929 return os;
5932 void
5933 lang_final (void)
5935 lang_output_statement_type *new;
5937 new = new_stat (lang_output_statement, stat_ptr);
5938 new->name = output_filename;
5941 /* Reset the current counters in the regions. */
5943 void
5944 lang_reset_memory_regions (void)
5946 lang_memory_region_type *p = lang_memory_region_list;
5947 asection *o;
5948 lang_output_section_statement_type *os;
5950 for (p = lang_memory_region_list; p != NULL; p = p->next)
5952 p->current = p->origin;
5953 p->last_os = NULL;
5956 for (os = &lang_output_section_statement.head->output_section_statement;
5957 os != NULL;
5958 os = os->next)
5960 os->processed_vma = FALSE;
5961 os->processed_lma = FALSE;
5964 for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
5966 /* Save the last size for possible use by bfd_relax_section. */
5967 o->rawsize = o->size;
5968 o->size = 0;
5972 /* Worker for lang_gc_sections_1. */
5974 static void
5975 gc_section_callback (lang_wild_statement_type *ptr,
5976 struct wildcard_list *sec ATTRIBUTE_UNUSED,
5977 asection *section,
5978 lang_input_statement_type *file ATTRIBUTE_UNUSED,
5979 void *data ATTRIBUTE_UNUSED)
5981 /* If the wild pattern was marked KEEP, the member sections
5982 should be as well. */
5983 if (ptr->keep_sections)
5984 section->flags |= SEC_KEEP;
5987 /* Iterate over sections marking them against GC. */
5989 static void
5990 lang_gc_sections_1 (lang_statement_union_type *s)
5992 for (; s != NULL; s = s->header.next)
5994 switch (s->header.type)
5996 case lang_wild_statement_enum:
5997 walk_wild (&s->wild_statement, gc_section_callback, NULL);
5998 break;
5999 case lang_constructors_statement_enum:
6000 lang_gc_sections_1 (constructor_list.head);
6001 break;
6002 case lang_output_section_statement_enum:
6003 lang_gc_sections_1 (s->output_section_statement.children.head);
6004 break;
6005 case lang_group_statement_enum:
6006 lang_gc_sections_1 (s->group_statement.children.head);
6007 break;
6008 default:
6009 break;
6014 static void
6015 lang_gc_sections (void)
6017 /* Keep all sections so marked in the link script. */
6019 lang_gc_sections_1 (statement_list.head);
6021 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
6022 the special case of debug info. (See bfd/stabs.c)
6023 Twiddle the flag here, to simplify later linker code. */
6024 if (link_info.relocatable)
6026 LANG_FOR_EACH_INPUT_STATEMENT (f)
6028 asection *sec;
6029 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
6030 if ((sec->flags & SEC_DEBUGGING) == 0)
6031 sec->flags &= ~SEC_EXCLUDE;
6035 if (link_info.gc_sections)
6036 bfd_gc_sections (link_info.output_bfd, &link_info);
6039 /* Worker for lang_find_relro_sections_1. */
6041 static void
6042 find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
6043 struct wildcard_list *sec ATTRIBUTE_UNUSED,
6044 asection *section,
6045 lang_input_statement_type *file ATTRIBUTE_UNUSED,
6046 void *data)
6048 /* Discarded, excluded and ignored sections effectively have zero
6049 size. */
6050 if (section->output_section != NULL
6051 && section->output_section->owner == link_info.output_bfd
6052 && (section->output_section->flags & SEC_EXCLUDE) == 0
6053 && !IGNORE_SECTION (section)
6054 && section->size != 0)
6056 bfd_boolean *has_relro_section = (bfd_boolean *) data;
6057 *has_relro_section = TRUE;
6061 /* Iterate over sections for relro sections. */
6063 static void
6064 lang_find_relro_sections_1 (lang_statement_union_type *s,
6065 bfd_boolean *has_relro_section)
6067 if (*has_relro_section)
6068 return;
6070 for (; s != NULL; s = s->header.next)
6072 if (s == expld.dataseg.relro_end_stat)
6073 break;
6075 switch (s->header.type)
6077 case lang_wild_statement_enum:
6078 walk_wild (&s->wild_statement,
6079 find_relro_section_callback,
6080 has_relro_section);
6081 break;
6082 case lang_constructors_statement_enum:
6083 lang_find_relro_sections_1 (constructor_list.head,
6084 has_relro_section);
6085 break;
6086 case lang_output_section_statement_enum:
6087 lang_find_relro_sections_1 (s->output_section_statement.children.head,
6088 has_relro_section);
6089 break;
6090 case lang_group_statement_enum:
6091 lang_find_relro_sections_1 (s->group_statement.children.head,
6092 has_relro_section);
6093 break;
6094 default:
6095 break;
6100 static void
6101 lang_find_relro_sections (void)
6103 bfd_boolean has_relro_section = FALSE;
6105 /* Check all sections in the link script. */
6107 lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
6108 &has_relro_section);
6110 if (!has_relro_section)
6111 link_info.relro = FALSE;
6114 /* Relax all sections until bfd_relax_section gives up. */
6116 static void
6117 relax_sections (void)
6119 /* Keep relaxing until bfd_relax_section gives up. */
6120 bfd_boolean relax_again;
6122 link_info.relax_trip = -1;
6125 relax_again = FALSE;
6126 link_info.relax_trip++;
6128 /* Note: pe-dll.c does something like this also. If you find
6129 you need to change this code, you probably need to change
6130 pe-dll.c also. DJ */
6132 /* Do all the assignments with our current guesses as to
6133 section sizes. */
6134 lang_do_assignments ();
6136 /* We must do this after lang_do_assignments, because it uses
6137 size. */
6138 lang_reset_memory_regions ();
6140 /* Perform another relax pass - this time we know where the
6141 globals are, so can make a better guess. */
6142 lang_size_sections (&relax_again, FALSE);
6144 while (relax_again);
6147 void
6148 lang_process (void)
6150 /* Finalize dynamic list. */
6151 if (link_info.dynamic_list)
6152 lang_finalize_version_expr_head (&link_info.dynamic_list->head);
6154 current_target = default_target;
6156 /* Open the output file. */
6157 lang_for_each_statement (ldlang_open_output);
6158 init_opb ();
6160 ldemul_create_output_section_statements ();
6162 /* Add to the hash table all undefineds on the command line. */
6163 lang_place_undefineds ();
6165 if (!bfd_section_already_linked_table_init ())
6166 einfo (_("%P%F: Failed to create hash table\n"));
6168 /* Create a bfd for each input file. */
6169 current_target = default_target;
6170 open_input_bfds (statement_list.head, FALSE);
6172 link_info.gc_sym_list = &entry_symbol;
6173 if (entry_symbol.name == NULL)
6174 link_info.gc_sym_list = ldlang_undef_chain_list_head;
6176 ldemul_after_open ();
6178 bfd_section_already_linked_table_free ();
6180 /* Make sure that we're not mixing architectures. We call this
6181 after all the input files have been opened, but before we do any
6182 other processing, so that any operations merge_private_bfd_data
6183 does on the output file will be known during the rest of the
6184 link. */
6185 lang_check ();
6187 /* Handle .exports instead of a version script if we're told to do so. */
6188 if (command_line.version_exports_section)
6189 lang_do_version_exports_section ();
6191 /* Build all sets based on the information gathered from the input
6192 files. */
6193 ldctor_build_sets ();
6195 /* Remove unreferenced sections if asked to. */
6196 lang_gc_sections ();
6198 /* Size up the common data. */
6199 lang_common ();
6201 /* Update wild statements. */
6202 update_wild_statements (statement_list.head);
6204 /* Run through the contours of the script and attach input sections
6205 to the correct output sections. */
6206 map_input_to_output_sections (statement_list.head, NULL, NULL);
6208 process_insert_statements ();
6210 /* Find any sections not attached explicitly and handle them. */
6211 lang_place_orphans ();
6213 if (! link_info.relocatable)
6215 asection *found;
6217 /* Merge SEC_MERGE sections. This has to be done after GC of
6218 sections, so that GCed sections are not merged, but before
6219 assigning dynamic symbols, since removing whole input sections
6220 is hard then. */
6221 bfd_merge_sections (link_info.output_bfd, &link_info);
6223 /* Look for a text section and set the readonly attribute in it. */
6224 found = bfd_get_section_by_name (link_info.output_bfd, ".text");
6226 if (found != NULL)
6228 if (config.text_read_only)
6229 found->flags |= SEC_READONLY;
6230 else
6231 found->flags &= ~SEC_READONLY;
6235 /* Do anything special before sizing sections. This is where ELF
6236 and other back-ends size dynamic sections. */
6237 ldemul_before_allocation ();
6239 /* We must record the program headers before we try to fix the
6240 section positions, since they will affect SIZEOF_HEADERS. */
6241 lang_record_phdrs ();
6243 /* Check relro sections. */
6244 if (link_info.relro && ! link_info.relocatable)
6245 lang_find_relro_sections ();
6247 /* Size up the sections. */
6248 lang_size_sections (NULL, !command_line.relax);
6250 /* Now run around and relax if we can. */
6251 if (command_line.relax)
6253 /* We may need more than one relaxation pass. */
6254 int i = link_info.relax_pass;
6256 /* The backend can use it to determine the current pass. */
6257 link_info.relax_pass = 0;
6259 while (i--)
6261 relax_sections ();
6262 link_info.relax_pass++;
6265 /* Final extra sizing to report errors. */
6266 lang_do_assignments ();
6267 lang_reset_memory_regions ();
6268 lang_size_sections (NULL, TRUE);
6271 /* See if anything special should be done now we know how big
6272 everything is. */
6273 ldemul_after_allocation ();
6275 /* Fix any .startof. or .sizeof. symbols. */
6276 lang_set_startof ();
6278 /* Do all the assignments, now that we know the final resting places
6279 of all the symbols. */
6281 lang_do_assignments ();
6283 ldemul_finish ();
6285 /* Make sure that the section addresses make sense. */
6286 if (command_line.check_section_addresses)
6287 lang_check_section_addresses ();
6289 lang_end ();
6292 /* EXPORTED TO YACC */
6294 void
6295 lang_add_wild (struct wildcard_spec *filespec,
6296 struct wildcard_list *section_list,
6297 bfd_boolean keep_sections)
6299 struct wildcard_list *curr, *next;
6300 lang_wild_statement_type *new;
6302 /* Reverse the list as the parser puts it back to front. */
6303 for (curr = section_list, section_list = NULL;
6304 curr != NULL;
6305 section_list = curr, curr = next)
6307 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
6308 placed_commons = TRUE;
6310 next = curr->next;
6311 curr->next = section_list;
6314 if (filespec != NULL && filespec->name != NULL)
6316 if (strcmp (filespec->name, "*") == 0)
6317 filespec->name = NULL;
6318 else if (! wildcardp (filespec->name))
6319 lang_has_input_file = TRUE;
6322 new = new_stat (lang_wild_statement, stat_ptr);
6323 new->filename = NULL;
6324 new->filenames_sorted = FALSE;
6325 if (filespec != NULL)
6327 new->filename = filespec->name;
6328 new->filenames_sorted = filespec->sorted == by_name;
6330 new->section_list = section_list;
6331 new->keep_sections = keep_sections;
6332 lang_list_init (&new->children);
6333 analyze_walk_wild_section_handler (new);
6336 void
6337 lang_section_start (const char *name, etree_type *address,
6338 const segment_type *segment)
6340 lang_address_statement_type *ad;
6342 ad = new_stat (lang_address_statement, stat_ptr);
6343 ad->section_name = name;
6344 ad->address = address;
6345 ad->segment = segment;
6348 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6349 because of a -e argument on the command line, or zero if this is
6350 called by ENTRY in a linker script. Command line arguments take
6351 precedence. */
6353 void
6354 lang_add_entry (const char *name, bfd_boolean cmdline)
6356 if (entry_symbol.name == NULL
6357 || cmdline
6358 || ! entry_from_cmdline)
6360 entry_symbol.name = name;
6361 entry_from_cmdline = cmdline;
6365 /* Set the default start symbol to NAME. .em files should use this,
6366 not lang_add_entry, to override the use of "start" if neither the
6367 linker script nor the command line specifies an entry point. NAME
6368 must be permanently allocated. */
6369 void
6370 lang_default_entry (const char *name)
6372 entry_symbol_default = name;
6375 void
6376 lang_add_target (const char *name)
6378 lang_target_statement_type *new;
6380 new = new_stat (lang_target_statement, stat_ptr);
6381 new->target = name;
6384 void
6385 lang_add_map (const char *name)
6387 while (*name)
6389 switch (*name)
6391 case 'F':
6392 map_option_f = TRUE;
6393 break;
6395 name++;
6399 void
6400 lang_add_fill (fill_type *fill)
6402 lang_fill_statement_type *new;
6404 new = new_stat (lang_fill_statement, stat_ptr);
6405 new->fill = fill;
6408 void
6409 lang_add_data (int type, union etree_union *exp)
6411 lang_data_statement_type *new;
6413 new = new_stat (lang_data_statement, stat_ptr);
6414 new->exp = exp;
6415 new->type = type;
6418 /* Create a new reloc statement. RELOC is the BFD relocation type to
6419 generate. HOWTO is the corresponding howto structure (we could
6420 look this up, but the caller has already done so). SECTION is the
6421 section to generate a reloc against, or NAME is the name of the
6422 symbol to generate a reloc against. Exactly one of SECTION and
6423 NAME must be NULL. ADDEND is an expression for the addend. */
6425 void
6426 lang_add_reloc (bfd_reloc_code_real_type reloc,
6427 reloc_howto_type *howto,
6428 asection *section,
6429 const char *name,
6430 union etree_union *addend)
6432 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
6434 p->reloc = reloc;
6435 p->howto = howto;
6436 p->section = section;
6437 p->name = name;
6438 p->addend_exp = addend;
6440 p->addend_value = 0;
6441 p->output_section = NULL;
6442 p->output_offset = 0;
6445 lang_assignment_statement_type *
6446 lang_add_assignment (etree_type *exp)
6448 lang_assignment_statement_type *new;
6450 new = new_stat (lang_assignment_statement, stat_ptr);
6451 new->exp = exp;
6452 return new;
6455 void
6456 lang_add_attribute (enum statement_enum attribute)
6458 new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
6461 void
6462 lang_startup (const char *name)
6464 if (startup_file != NULL)
6466 einfo (_("%P%F: multiple STARTUP files\n"));
6468 first_file->filename = name;
6469 first_file->local_sym_name = name;
6470 first_file->real = TRUE;
6472 startup_file = name;
6475 void
6476 lang_float (bfd_boolean maybe)
6478 lang_float_flag = maybe;
6482 /* Work out the load- and run-time regions from a script statement, and
6483 store them in *LMA_REGION and *REGION respectively.
6485 MEMSPEC is the name of the run-time region, or the value of
6486 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6487 LMA_MEMSPEC is the name of the load-time region, or null if the
6488 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6489 had an explicit load address.
6491 It is an error to specify both a load region and a load address. */
6493 static void
6494 lang_get_regions (lang_memory_region_type **region,
6495 lang_memory_region_type **lma_region,
6496 const char *memspec,
6497 const char *lma_memspec,
6498 bfd_boolean have_lma,
6499 bfd_boolean have_vma)
6501 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
6503 /* If no runtime region or VMA has been specified, but the load region
6504 has been specified, then use the load region for the runtime region
6505 as well. */
6506 if (lma_memspec != NULL
6507 && ! have_vma
6508 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
6509 *region = *lma_region;
6510 else
6511 *region = lang_memory_region_lookup (memspec, FALSE);
6513 if (have_lma && lma_memspec != 0)
6514 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6517 void
6518 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
6519 lang_output_section_phdr_list *phdrs,
6520 const char *lma_memspec)
6522 lang_get_regions (&current_section->region,
6523 &current_section->lma_region,
6524 memspec, lma_memspec,
6525 current_section->load_base != NULL,
6526 current_section->addr_tree != NULL);
6528 /* If this section has no load region or base, but has the same
6529 region as the previous section, then propagate the previous
6530 section's load region. */
6532 if (!current_section->lma_region && !current_section->load_base
6533 && current_section->region == current_section->prev->region)
6534 current_section->lma_region = current_section->prev->lma_region;
6536 current_section->fill = fill;
6537 current_section->phdrs = phdrs;
6538 pop_stat_ptr ();
6541 /* Create an absolute symbol with the given name with the value of the
6542 address of first byte of the section named.
6544 If the symbol already exists, then do nothing. */
6546 void
6547 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
6549 struct bfd_link_hash_entry *h;
6551 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6552 if (h == NULL)
6553 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6555 if (h->type == bfd_link_hash_new
6556 || h->type == bfd_link_hash_undefined)
6558 asection *sec;
6560 h->type = bfd_link_hash_defined;
6562 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6563 if (sec == NULL)
6564 h->u.def.value = 0;
6565 else
6566 h->u.def.value = bfd_get_section_vma (link_info.output_bfd, sec);
6568 h->u.def.section = bfd_abs_section_ptr;
6572 /* Create an absolute symbol with the given name with the value of the
6573 address of the first byte after the end of the section named.
6575 If the symbol already exists, then do nothing. */
6577 void
6578 lang_abs_symbol_at_end_of (const char *secname, const char *name)
6580 struct bfd_link_hash_entry *h;
6582 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
6583 if (h == NULL)
6584 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6586 if (h->type == bfd_link_hash_new
6587 || h->type == bfd_link_hash_undefined)
6589 asection *sec;
6591 h->type = bfd_link_hash_defined;
6593 sec = bfd_get_section_by_name (link_info.output_bfd, secname);
6594 if (sec == NULL)
6595 h->u.def.value = 0;
6596 else
6597 h->u.def.value = (bfd_get_section_vma (link_info.output_bfd, sec)
6598 + TO_ADDR (sec->size));
6600 h->u.def.section = bfd_abs_section_ptr;
6604 void
6605 lang_statement_append (lang_statement_list_type *list,
6606 lang_statement_union_type *element,
6607 lang_statement_union_type **field)
6609 *(list->tail) = element;
6610 list->tail = field;
6613 /* Set the output format type. -oformat overrides scripts. */
6615 void
6616 lang_add_output_format (const char *format,
6617 const char *big,
6618 const char *little,
6619 int from_script)
6621 if (output_target == NULL || !from_script)
6623 if (command_line.endian == ENDIAN_BIG
6624 && big != NULL)
6625 format = big;
6626 else if (command_line.endian == ENDIAN_LITTLE
6627 && little != NULL)
6628 format = little;
6630 output_target = format;
6634 void
6635 lang_add_insert (const char *where, int is_before)
6637 lang_insert_statement_type *new;
6639 new = new_stat (lang_insert_statement, stat_ptr);
6640 new->where = where;
6641 new->is_before = is_before;
6642 saved_script_handle = previous_script_handle;
6645 /* Enter a group. This creates a new lang_group_statement, and sets
6646 stat_ptr to build new statements within the group. */
6648 void
6649 lang_enter_group (void)
6651 lang_group_statement_type *g;
6653 g = new_stat (lang_group_statement, stat_ptr);
6654 lang_list_init (&g->children);
6655 push_stat_ptr (&g->children);
6658 /* Leave a group. This just resets stat_ptr to start writing to the
6659 regular list of statements again. Note that this will not work if
6660 groups can occur inside anything else which can adjust stat_ptr,
6661 but currently they can't. */
6663 void
6664 lang_leave_group (void)
6666 pop_stat_ptr ();
6669 /* Add a new program header. This is called for each entry in a PHDRS
6670 command in a linker script. */
6672 void
6673 lang_new_phdr (const char *name,
6674 etree_type *type,
6675 bfd_boolean filehdr,
6676 bfd_boolean phdrs,
6677 etree_type *at,
6678 etree_type *flags)
6680 struct lang_phdr *n, **pp;
6682 n = stat_alloc (sizeof (struct lang_phdr));
6683 n->next = NULL;
6684 n->name = name;
6685 n->type = exp_get_value_int (type, 0, "program header type");
6686 n->filehdr = filehdr;
6687 n->phdrs = phdrs;
6688 n->at = at;
6689 n->flags = flags;
6691 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
6693 *pp = n;
6696 /* Record the program header information in the output BFD. FIXME: We
6697 should not be calling an ELF specific function here. */
6699 static void
6700 lang_record_phdrs (void)
6702 unsigned int alc;
6703 asection **secs;
6704 lang_output_section_phdr_list *last;
6705 struct lang_phdr *l;
6706 lang_output_section_statement_type *os;
6708 alc = 10;
6709 secs = xmalloc (alc * sizeof (asection *));
6710 last = NULL;
6712 for (l = lang_phdr_list; l != NULL; l = l->next)
6714 unsigned int c;
6715 flagword flags;
6716 bfd_vma at;
6718 c = 0;
6719 for (os = &lang_output_section_statement.head->output_section_statement;
6720 os != NULL;
6721 os = os->next)
6723 lang_output_section_phdr_list *pl;
6725 if (os->constraint < 0)
6726 continue;
6728 pl = os->phdrs;
6729 if (pl != NULL)
6730 last = pl;
6731 else
6733 if (os->sectype == noload_section
6734 || os->bfd_section == NULL
6735 || (os->bfd_section->flags & SEC_ALLOC) == 0)
6736 continue;
6738 /* Don't add orphans to PT_INTERP header. */
6739 if (l->type == 3)
6740 continue;
6742 if (last == NULL)
6744 lang_output_section_statement_type * tmp_os;
6746 /* If we have not run across a section with a program
6747 header assigned to it yet, then scan forwards to find
6748 one. This prevents inconsistencies in the linker's
6749 behaviour when a script has specified just a single
6750 header and there are sections in that script which are
6751 not assigned to it, and which occur before the first
6752 use of that header. See here for more details:
6753 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6754 for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
6755 if (tmp_os->phdrs)
6757 last = tmp_os->phdrs;
6758 break;
6760 if (last == NULL)
6761 einfo (_("%F%P: no sections assigned to phdrs\n"));
6763 pl = last;
6766 if (os->bfd_section == NULL)
6767 continue;
6769 for (; pl != NULL; pl = pl->next)
6771 if (strcmp (pl->name, l->name) == 0)
6773 if (c >= alc)
6775 alc *= 2;
6776 secs = xrealloc (secs, alc * sizeof (asection *));
6778 secs[c] = os->bfd_section;
6779 ++c;
6780 pl->used = TRUE;
6785 if (l->flags == NULL)
6786 flags = 0;
6787 else
6788 flags = exp_get_vma (l->flags, 0, "phdr flags");
6790 if (l->at == NULL)
6791 at = 0;
6792 else
6793 at = exp_get_vma (l->at, 0, "phdr load address");
6795 if (! bfd_record_phdr (link_info.output_bfd, l->type,
6796 l->flags != NULL, flags, l->at != NULL,
6797 at, l->filehdr, l->phdrs, c, secs))
6798 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6801 free (secs);
6803 /* Make sure all the phdr assignments succeeded. */
6804 for (os = &lang_output_section_statement.head->output_section_statement;
6805 os != NULL;
6806 os = os->next)
6808 lang_output_section_phdr_list *pl;
6810 if (os->constraint < 0
6811 || os->bfd_section == NULL)
6812 continue;
6814 for (pl = os->phdrs;
6815 pl != NULL;
6816 pl = pl->next)
6817 if (! pl->used && strcmp (pl->name, "NONE") != 0)
6818 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6819 os->name, pl->name);
6823 /* Record a list of sections which may not be cross referenced. */
6825 void
6826 lang_add_nocrossref (lang_nocrossref_type *l)
6828 struct lang_nocrossrefs *n;
6830 n = xmalloc (sizeof *n);
6831 n->next = nocrossref_list;
6832 n->list = l;
6833 nocrossref_list = n;
6835 /* Set notice_all so that we get informed about all symbols. */
6836 link_info.notice_all = TRUE;
6839 /* Overlay handling. We handle overlays with some static variables. */
6841 /* The overlay virtual address. */
6842 static etree_type *overlay_vma;
6843 /* And subsection alignment. */
6844 static etree_type *overlay_subalign;
6846 /* An expression for the maximum section size seen so far. */
6847 static etree_type *overlay_max;
6849 /* A list of all the sections in this overlay. */
6851 struct overlay_list {
6852 struct overlay_list *next;
6853 lang_output_section_statement_type *os;
6856 static struct overlay_list *overlay_list;
6858 /* Start handling an overlay. */
6860 void
6861 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
6863 /* The grammar should prevent nested overlays from occurring. */
6864 ASSERT (overlay_vma == NULL
6865 && overlay_subalign == NULL
6866 && overlay_max == NULL);
6868 overlay_vma = vma_expr;
6869 overlay_subalign = subalign;
6872 /* Start a section in an overlay. We handle this by calling
6873 lang_enter_output_section_statement with the correct VMA.
6874 lang_leave_overlay sets up the LMA and memory regions. */
6876 void
6877 lang_enter_overlay_section (const char *name)
6879 struct overlay_list *n;
6880 etree_type *size;
6882 lang_enter_output_section_statement (name, overlay_vma, overlay_section,
6883 0, overlay_subalign, 0, 0);
6885 /* If this is the first section, then base the VMA of future
6886 sections on this one. This will work correctly even if `.' is
6887 used in the addresses. */
6888 if (overlay_list == NULL)
6889 overlay_vma = exp_nameop (ADDR, name);
6891 /* Remember the section. */
6892 n = xmalloc (sizeof *n);
6893 n->os = current_section;
6894 n->next = overlay_list;
6895 overlay_list = n;
6897 size = exp_nameop (SIZEOF, name);
6899 /* Arrange to work out the maximum section end address. */
6900 if (overlay_max == NULL)
6901 overlay_max = size;
6902 else
6903 overlay_max = exp_binop (MAX_K, overlay_max, size);
6906 /* Finish a section in an overlay. There isn't any special to do
6907 here. */
6909 void
6910 lang_leave_overlay_section (fill_type *fill,
6911 lang_output_section_phdr_list *phdrs)
6913 const char *name;
6914 char *clean, *s2;
6915 const char *s1;
6916 char *buf;
6918 name = current_section->name;
6920 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6921 region and that no load-time region has been specified. It doesn't
6922 really matter what we say here, since lang_leave_overlay will
6923 override it. */
6924 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
6926 /* Define the magic symbols. */
6928 clean = xmalloc (strlen (name) + 1);
6929 s2 = clean;
6930 for (s1 = name; *s1 != '\0'; s1++)
6931 if (ISALNUM (*s1) || *s1 == '_')
6932 *s2++ = *s1;
6933 *s2 = '\0';
6935 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
6936 sprintf (buf, "__load_start_%s", clean);
6937 lang_add_assignment (exp_provide (buf,
6938 exp_nameop (LOADADDR, name),
6939 FALSE));
6941 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
6942 sprintf (buf, "__load_stop_%s", clean);
6943 lang_add_assignment (exp_provide (buf,
6944 exp_binop ('+',
6945 exp_nameop (LOADADDR, name),
6946 exp_nameop (SIZEOF, name)),
6947 FALSE));
6949 free (clean);
6952 /* Finish an overlay. If there are any overlay wide settings, this
6953 looks through all the sections in the overlay and sets them. */
6955 void
6956 lang_leave_overlay (etree_type *lma_expr,
6957 int nocrossrefs,
6958 fill_type *fill,
6959 const char *memspec,
6960 lang_output_section_phdr_list *phdrs,
6961 const char *lma_memspec)
6963 lang_memory_region_type *region;
6964 lang_memory_region_type *lma_region;
6965 struct overlay_list *l;
6966 lang_nocrossref_type *nocrossref;
6968 lang_get_regions (&region, &lma_region,
6969 memspec, lma_memspec,
6970 lma_expr != NULL, FALSE);
6972 nocrossref = NULL;
6974 /* After setting the size of the last section, set '.' to end of the
6975 overlay region. */
6976 if (overlay_list != NULL)
6977 overlay_list->os->update_dot_tree
6978 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
6980 l = overlay_list;
6981 while (l != NULL)
6983 struct overlay_list *next;
6985 if (fill != NULL && l->os->fill == NULL)
6986 l->os->fill = fill;
6988 l->os->region = region;
6989 l->os->lma_region = lma_region;
6991 /* The first section has the load address specified in the
6992 OVERLAY statement. The rest are worked out from that.
6993 The base address is not needed (and should be null) if
6994 an LMA region was specified. */
6995 if (l->next == 0)
6997 l->os->load_base = lma_expr;
6998 l->os->sectype = normal_section;
7000 if (phdrs != NULL && l->os->phdrs == NULL)
7001 l->os->phdrs = phdrs;
7003 if (nocrossrefs)
7005 lang_nocrossref_type *nc;
7007 nc = xmalloc (sizeof *nc);
7008 nc->name = l->os->name;
7009 nc->next = nocrossref;
7010 nocrossref = nc;
7013 next = l->next;
7014 free (l);
7015 l = next;
7018 if (nocrossref != NULL)
7019 lang_add_nocrossref (nocrossref);
7021 overlay_vma = NULL;
7022 overlay_list = NULL;
7023 overlay_max = NULL;
7026 /* Version handling. This is only useful for ELF. */
7028 /* This global variable holds the version tree that we build. */
7030 struct bfd_elf_version_tree *lang_elf_version_info;
7032 /* If PREV is NULL, return first version pattern matching particular symbol.
7033 If PREV is non-NULL, return first version pattern matching particular
7034 symbol after PREV (previously returned by lang_vers_match). */
7036 static struct bfd_elf_version_expr *
7037 lang_vers_match (struct bfd_elf_version_expr_head *head,
7038 struct bfd_elf_version_expr *prev,
7039 const char *sym)
7041 const char *cxx_sym = sym;
7042 const char *java_sym = sym;
7043 struct bfd_elf_version_expr *expr = NULL;
7045 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7047 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
7048 if (!cxx_sym)
7049 cxx_sym = sym;
7051 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7053 java_sym = cplus_demangle (sym, DMGL_JAVA);
7054 if (!java_sym)
7055 java_sym = sym;
7058 if (head->htab && (prev == NULL || prev->literal))
7060 struct bfd_elf_version_expr e;
7062 switch (prev ? prev->mask : 0)
7064 case 0:
7065 if (head->mask & BFD_ELF_VERSION_C_TYPE)
7067 e.pattern = sym;
7068 expr = htab_find (head->htab, &e);
7069 while (expr && strcmp (expr->pattern, sym) == 0)
7070 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
7071 goto out_ret;
7072 else
7073 expr = expr->next;
7075 /* Fallthrough */
7076 case BFD_ELF_VERSION_C_TYPE:
7077 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
7079 e.pattern = cxx_sym;
7080 expr = htab_find (head->htab, &e);
7081 while (expr && strcmp (expr->pattern, cxx_sym) == 0)
7082 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7083 goto out_ret;
7084 else
7085 expr = expr->next;
7087 /* Fallthrough */
7088 case BFD_ELF_VERSION_CXX_TYPE:
7089 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
7091 e.pattern = java_sym;
7092 expr = htab_find (head->htab, &e);
7093 while (expr && strcmp (expr->pattern, java_sym) == 0)
7094 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7095 goto out_ret;
7096 else
7097 expr = expr->next;
7099 /* Fallthrough */
7100 default:
7101 break;
7105 /* Finally, try the wildcards. */
7106 if (prev == NULL || prev->literal)
7107 expr = head->remaining;
7108 else
7109 expr = prev->next;
7110 for (; expr; expr = expr->next)
7112 const char *s;
7114 if (!expr->pattern)
7115 continue;
7117 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
7118 break;
7120 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
7121 s = java_sym;
7122 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
7123 s = cxx_sym;
7124 else
7125 s = sym;
7126 if (fnmatch (expr->pattern, s, 0) == 0)
7127 break;
7130 out_ret:
7131 if (cxx_sym != sym)
7132 free ((char *) cxx_sym);
7133 if (java_sym != sym)
7134 free ((char *) java_sym);
7135 return expr;
7138 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
7139 return a pointer to the symbol name with any backslash quotes removed. */
7141 static const char *
7142 realsymbol (const char *pattern)
7144 const char *p;
7145 bfd_boolean changed = FALSE, backslash = FALSE;
7146 char *s, *symbol = xmalloc (strlen (pattern) + 1);
7148 for (p = pattern, s = symbol; *p != '\0'; ++p)
7150 /* It is a glob pattern only if there is no preceding
7151 backslash. */
7152 if (backslash)
7154 /* Remove the preceding backslash. */
7155 *(s - 1) = *p;
7156 backslash = FALSE;
7157 changed = TRUE;
7159 else
7161 if (*p == '?' || *p == '*' || *p == '[')
7163 free (symbol);
7164 return NULL;
7167 *s++ = *p;
7168 backslash = *p == '\\';
7172 if (changed)
7174 *s = '\0';
7175 return symbol;
7177 else
7179 free (symbol);
7180 return pattern;
7184 /* This is called for each variable name or match expression. NEW is
7185 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7186 pattern to be matched against symbol names. */
7188 struct bfd_elf_version_expr *
7189 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
7190 const char *new,
7191 const char *lang,
7192 bfd_boolean literal_p)
7194 struct bfd_elf_version_expr *ret;
7196 ret = xmalloc (sizeof *ret);
7197 ret->next = orig;
7198 ret->symver = 0;
7199 ret->script = 0;
7200 ret->literal = TRUE;
7201 ret->pattern = literal_p ? new : realsymbol (new);
7202 if (ret->pattern == NULL)
7204 ret->pattern = new;
7205 ret->literal = FALSE;
7208 if (lang == NULL || strcasecmp (lang, "C") == 0)
7209 ret->mask = BFD_ELF_VERSION_C_TYPE;
7210 else if (strcasecmp (lang, "C++") == 0)
7211 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
7212 else if (strcasecmp (lang, "Java") == 0)
7213 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
7214 else
7216 einfo (_("%X%P: unknown language `%s' in version information\n"),
7217 lang);
7218 ret->mask = BFD_ELF_VERSION_C_TYPE;
7221 return ldemul_new_vers_pattern (ret);
7224 /* This is called for each set of variable names and match
7225 expressions. */
7227 struct bfd_elf_version_tree *
7228 lang_new_vers_node (struct bfd_elf_version_expr *globals,
7229 struct bfd_elf_version_expr *locals)
7231 struct bfd_elf_version_tree *ret;
7233 ret = xcalloc (1, sizeof *ret);
7234 ret->globals.list = globals;
7235 ret->locals.list = locals;
7236 ret->match = lang_vers_match;
7237 ret->name_indx = (unsigned int) -1;
7238 return ret;
7241 /* This static variable keeps track of version indices. */
7243 static int version_index;
7245 static hashval_t
7246 version_expr_head_hash (const void *p)
7248 const struct bfd_elf_version_expr *e = p;
7250 return htab_hash_string (e->pattern);
7253 static int
7254 version_expr_head_eq (const void *p1, const void *p2)
7256 const struct bfd_elf_version_expr *e1 = p1;
7257 const struct bfd_elf_version_expr *e2 = p2;
7259 return strcmp (e1->pattern, e2->pattern) == 0;
7262 static void
7263 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
7265 size_t count = 0;
7266 struct bfd_elf_version_expr *e, *next;
7267 struct bfd_elf_version_expr **list_loc, **remaining_loc;
7269 for (e = head->list; e; e = e->next)
7271 if (e->literal)
7272 count++;
7273 head->mask |= e->mask;
7276 if (count)
7278 head->htab = htab_create (count * 2, version_expr_head_hash,
7279 version_expr_head_eq, NULL);
7280 list_loc = &head->list;
7281 remaining_loc = &head->remaining;
7282 for (e = head->list; e; e = next)
7284 next = e->next;
7285 if (!e->literal)
7287 *remaining_loc = e;
7288 remaining_loc = &e->next;
7290 else
7292 void **loc = htab_find_slot (head->htab, e, INSERT);
7294 if (*loc)
7296 struct bfd_elf_version_expr *e1, *last;
7298 e1 = *loc;
7299 last = NULL;
7302 if (e1->mask == e->mask)
7304 last = NULL;
7305 break;
7307 last = e1;
7308 e1 = e1->next;
7310 while (e1 && strcmp (e1->pattern, e->pattern) == 0);
7312 if (last == NULL)
7314 /* This is a duplicate. */
7315 /* FIXME: Memory leak. Sometimes pattern is not
7316 xmalloced alone, but in larger chunk of memory. */
7317 /* free (e->pattern); */
7318 free (e);
7320 else
7322 e->next = last->next;
7323 last->next = e;
7326 else
7328 *loc = e;
7329 *list_loc = e;
7330 list_loc = &e->next;
7334 *remaining_loc = NULL;
7335 *list_loc = head->remaining;
7337 else
7338 head->remaining = head->list;
7341 /* This is called when we know the name and dependencies of the
7342 version. */
7344 void
7345 lang_register_vers_node (const char *name,
7346 struct bfd_elf_version_tree *version,
7347 struct bfd_elf_version_deps *deps)
7349 struct bfd_elf_version_tree *t, **pp;
7350 struct bfd_elf_version_expr *e1;
7352 if (name == NULL)
7353 name = "";
7355 if ((name[0] == '\0' && lang_elf_version_info != NULL)
7356 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
7358 einfo (_("%X%P: anonymous version tag cannot be combined"
7359 " with other version tags\n"));
7360 free (version);
7361 return;
7364 /* Make sure this node has a unique name. */
7365 for (t = lang_elf_version_info; t != NULL; t = t->next)
7366 if (strcmp (t->name, name) == 0)
7367 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
7369 lang_finalize_version_expr_head (&version->globals);
7370 lang_finalize_version_expr_head (&version->locals);
7372 /* Check the global and local match names, and make sure there
7373 aren't any duplicates. */
7375 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
7377 for (t = lang_elf_version_info; t != NULL; t = t->next)
7379 struct bfd_elf_version_expr *e2;
7381 if (t->locals.htab && e1->literal)
7383 e2 = htab_find (t->locals.htab, e1);
7384 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7386 if (e1->mask == e2->mask)
7387 einfo (_("%X%P: duplicate expression `%s'"
7388 " in version information\n"), e1->pattern);
7389 e2 = e2->next;
7392 else if (!e1->literal)
7393 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
7394 if (strcmp (e1->pattern, e2->pattern) == 0
7395 && e1->mask == e2->mask)
7396 einfo (_("%X%P: duplicate expression `%s'"
7397 " in version information\n"), e1->pattern);
7401 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
7403 for (t = lang_elf_version_info; t != NULL; t = t->next)
7405 struct bfd_elf_version_expr *e2;
7407 if (t->globals.htab && e1->literal)
7409 e2 = htab_find (t->globals.htab, e1);
7410 while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
7412 if (e1->mask == e2->mask)
7413 einfo (_("%X%P: duplicate expression `%s'"
7414 " in version information\n"),
7415 e1->pattern);
7416 e2 = e2->next;
7419 else if (!e1->literal)
7420 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
7421 if (strcmp (e1->pattern, e2->pattern) == 0
7422 && e1->mask == e2->mask)
7423 einfo (_("%X%P: duplicate expression `%s'"
7424 " in version information\n"), e1->pattern);
7428 version->deps = deps;
7429 version->name = name;
7430 if (name[0] != '\0')
7432 ++version_index;
7433 version->vernum = version_index;
7435 else
7436 version->vernum = 0;
7438 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
7440 *pp = version;
7443 /* This is called when we see a version dependency. */
7445 struct bfd_elf_version_deps *
7446 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
7448 struct bfd_elf_version_deps *ret;
7449 struct bfd_elf_version_tree *t;
7451 ret = xmalloc (sizeof *ret);
7452 ret->next = list;
7454 for (t = lang_elf_version_info; t != NULL; t = t->next)
7456 if (strcmp (t->name, name) == 0)
7458 ret->version_needed = t;
7459 return ret;
7463 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
7465 return ret;
7468 static void
7469 lang_do_version_exports_section (void)
7471 struct bfd_elf_version_expr *greg = NULL, *lreg;
7473 LANG_FOR_EACH_INPUT_STATEMENT (is)
7475 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
7476 char *contents, *p;
7477 bfd_size_type len;
7479 if (sec == NULL)
7480 continue;
7482 len = sec->size;
7483 contents = xmalloc (len);
7484 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
7485 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
7487 p = contents;
7488 while (p < contents + len)
7490 greg = lang_new_vers_pattern (greg, p, NULL, FALSE);
7491 p = strchr (p, '\0') + 1;
7494 /* Do not free the contents, as we used them creating the regex. */
7496 /* Do not include this section in the link. */
7497 sec->flags |= SEC_EXCLUDE | SEC_KEEP;
7500 lreg = lang_new_vers_pattern (NULL, "*", NULL, FALSE);
7501 lang_register_vers_node (command_line.version_exports_section,
7502 lang_new_vers_node (greg, lreg), NULL);
7505 void
7506 lang_add_unique (const char *name)
7508 struct unique_sections *ent;
7510 for (ent = unique_section_list; ent; ent = ent->next)
7511 if (strcmp (ent->name, name) == 0)
7512 return;
7514 ent = xmalloc (sizeof *ent);
7515 ent->name = xstrdup (name);
7516 ent->next = unique_section_list;
7517 unique_section_list = ent;
7520 /* Append the list of dynamic symbols to the existing one. */
7522 void
7523 lang_append_dynamic_list (struct bfd_elf_version_expr *dynamic)
7525 if (link_info.dynamic_list)
7527 struct bfd_elf_version_expr *tail;
7528 for (tail = dynamic; tail->next != NULL; tail = tail->next)
7530 tail->next = link_info.dynamic_list->head.list;
7531 link_info.dynamic_list->head.list = dynamic;
7533 else
7535 struct bfd_elf_dynamic_list *d;
7537 d = xcalloc (1, sizeof *d);
7538 d->head.list = dynamic;
7539 d->match = lang_vers_match;
7540 link_info.dynamic_list = d;
7544 /* Append the list of C++ typeinfo dynamic symbols to the existing
7545 one. */
7547 void
7548 lang_append_dynamic_list_cpp_typeinfo (void)
7550 const char * symbols [] =
7552 "typeinfo name for*",
7553 "typeinfo for*"
7555 struct bfd_elf_version_expr *dynamic = NULL;
7556 unsigned int i;
7558 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7559 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7560 FALSE);
7562 lang_append_dynamic_list (dynamic);
7565 /* Append the list of C++ operator new and delete dynamic symbols to the
7566 existing one. */
7568 void
7569 lang_append_dynamic_list_cpp_new (void)
7571 const char * symbols [] =
7573 "operator new*",
7574 "operator delete*"
7576 struct bfd_elf_version_expr *dynamic = NULL;
7577 unsigned int i;
7579 for (i = 0; i < ARRAY_SIZE (symbols); i++)
7580 dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
7581 FALSE);
7583 lang_append_dynamic_list (dynamic);