1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Locals variables. */
49 static struct obstack stat_obstack
;
50 static struct obstack map_obstack
;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file
;
55 static 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
;
66 /* Forward declarations. */
67 static void exp_init_os (etree_type
*);
68 static void init_map_userdata (bfd
*, asection
*, void *);
69 static lang_input_statement_type
*lookup_name (const char *);
70 static struct bfd_hash_entry
*lang_definedness_newfunc
71 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
72 static void insert_undefined (const char *);
73 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
74 static void print_statement (lang_statement_union_type
*,
75 lang_output_section_statement_type
*);
76 static void print_statement_list (lang_statement_union_type
*,
77 lang_output_section_statement_type
*);
78 static void print_statements (void);
79 static void print_input_section (asection
*);
80 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
81 static void lang_record_phdrs (void);
82 static void lang_do_version_exports_section (void);
83 static void lang_finalize_version_expr_head
84 (struct bfd_elf_version_expr_head
*);
86 /* Exported variables. */
87 lang_output_section_statement_type
*abs_output_section
;
88 lang_statement_list_type lang_output_section_statement
;
89 lang_statement_list_type
*stat_ptr
= &statement_list
;
90 lang_statement_list_type file_chain
= { NULL
, NULL
};
91 lang_statement_list_type input_file_chain
;
92 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
93 static const char *entry_symbol_default
= "start";
94 const char *entry_section
= ".text";
95 bfd_boolean entry_from_cmdline
;
96 bfd_boolean lang_has_input_file
= FALSE
;
97 bfd_boolean had_output_filename
= FALSE
;
98 bfd_boolean lang_float_flag
= FALSE
;
99 bfd_boolean delete_output_file_on_failure
= FALSE
;
100 struct lang_phdr
*lang_phdr_list
;
101 struct lang_nocrossrefs
*nocrossref_list
;
102 static struct unique_sections
*unique_section_list
;
103 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
105 /* Functions that traverse the linker script and might evaluate
106 DEFINED() need to increment this. */
107 int lang_statement_iteration
= 0;
109 etree_type
*base
; /* Relocation base - or null */
111 /* Return TRUE if the PATTERN argument is a wildcard pattern.
112 Although backslashes are treated specially if a pattern contains
113 wildcards, we do not consider the mere presence of a backslash to
114 be enough to cause the pattern to be treated as a wildcard.
115 That lets us handle DOS filenames more naturally. */
116 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
118 #define new_stat(x, y) \
119 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
121 #define outside_section_address(q) \
122 ((q)->output_offset + (q)->output_section->vma)
124 #define outside_symbol_address(q) \
125 ((q)->value + outside_section_address (q->section))
127 #define SECTION_NAME_MAP_LENGTH (16)
130 stat_alloc (size_t size
)
132 return obstack_alloc (&stat_obstack
, size
);
136 unique_section_p (const asection
*sec
)
138 struct unique_sections
*unam
;
141 if (link_info
.relocatable
142 && sec
->owner
!= NULL
143 && bfd_is_group_section (sec
->owner
, sec
))
147 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
148 if (wildcardp (unam
->name
)
149 ? fnmatch (unam
->name
, secnam
, 0) == 0
150 : strcmp (unam
->name
, secnam
) == 0)
158 /* Generic traversal routines for finding matching sections. */
160 /* Try processing a section against a wildcard. This just calls
161 the callback unless the filename exclusion list is present
162 and excludes the file. It's hardly ever present so this
163 function is very fast. */
166 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
167 lang_input_statement_type
*file
,
169 struct wildcard_list
*sec
,
173 bfd_boolean skip
= FALSE
;
174 struct name_list
*list_tmp
;
176 /* Don't process sections from files which were
178 for (list_tmp
= sec
->spec
.exclude_name_list
;
180 list_tmp
= list_tmp
->next
)
182 bfd_boolean is_wildcard
= wildcardp (list_tmp
->name
);
184 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
186 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
188 /* If this file is part of an archive, and the archive is
189 excluded, exclude this file. */
190 if (! skip
&& file
->the_bfd
!= NULL
191 && file
->the_bfd
->my_archive
!= NULL
192 && file
->the_bfd
->my_archive
->filename
!= NULL
)
195 skip
= fnmatch (list_tmp
->name
,
196 file
->the_bfd
->my_archive
->filename
,
199 skip
= strcmp (list_tmp
->name
,
200 file
->the_bfd
->my_archive
->filename
) == 0;
208 (*callback
) (ptr
, sec
, s
, file
, data
);
211 /* Lowest common denominator routine that can handle everything correctly,
215 walk_wild_section_general (lang_wild_statement_type
*ptr
,
216 lang_input_statement_type
*file
,
221 struct wildcard_list
*sec
;
223 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
225 sec
= ptr
->section_list
;
227 (*callback
) (ptr
, sec
, s
, file
, data
);
231 bfd_boolean skip
= FALSE
;
233 if (sec
->spec
.name
!= NULL
)
235 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
237 if (wildcardp (sec
->spec
.name
))
238 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
240 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
244 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
251 /* Routines to find a single section given its name. If there's more
252 than one section with that name, we report that. */
256 asection
*found_section
;
257 bfd_boolean multiple_sections_found
;
258 } section_iterator_callback_data
;
261 section_iterator_callback (bfd
*bfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
263 section_iterator_callback_data
*d
= data
;
265 if (d
->found_section
!= NULL
)
267 d
->multiple_sections_found
= TRUE
;
271 d
->found_section
= s
;
276 find_section (lang_input_statement_type
*file
,
277 struct wildcard_list
*sec
,
278 bfd_boolean
*multiple_sections_found
)
280 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
282 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
283 section_iterator_callback
, &cb_data
);
284 *multiple_sections_found
= cb_data
.multiple_sections_found
;
285 return cb_data
.found_section
;
288 /* Code for handling simple wildcards without going through fnmatch,
289 which can be expensive because of charset translations etc. */
291 /* A simple wild is a literal string followed by a single '*',
292 where the literal part is at least 4 characters long. */
295 is_simple_wild (const char *name
)
297 size_t len
= strcspn (name
, "*?[");
298 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
302 match_simple_wild (const char *pattern
, const char *name
)
304 /* The first four characters of the pattern are guaranteed valid
305 non-wildcard characters. So we can go faster. */
306 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
307 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
312 while (*pattern
!= '*')
313 if (*name
++ != *pattern
++)
319 /* Compare sections ASEC and BSEC according to SORT. */
322 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
331 case by_alignment_name
:
332 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
333 - bfd_section_alignment (asec
->owner
, asec
));
339 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
340 bfd_get_section_name (bsec
->owner
, bsec
));
343 case by_name_alignment
:
344 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
345 bfd_get_section_name (bsec
->owner
, bsec
));
351 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
352 - bfd_section_alignment (asec
->owner
, asec
));
359 /* Build a Binary Search Tree to sort sections, unlike insertion sort
360 used in wild_sort(). BST is considerably faster if the number of
361 of sections are large. */
363 static lang_section_bst_type
**
364 wild_sort_fast (lang_wild_statement_type
*wild
,
365 struct wildcard_list
*sec
,
366 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
369 lang_section_bst_type
**tree
;
372 if (!wild
->filenames_sorted
373 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
375 /* Append at the right end of tree. */
377 tree
= &((*tree
)->right
);
383 /* Find the correct node to append this section. */
384 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
385 tree
= &((*tree
)->left
);
387 tree
= &((*tree
)->right
);
393 /* Use wild_sort_fast to build a BST to sort sections. */
396 output_section_callback_fast (lang_wild_statement_type
*ptr
,
397 struct wildcard_list
*sec
,
399 lang_input_statement_type
*file
,
400 void *output ATTRIBUTE_UNUSED
)
402 lang_section_bst_type
*node
;
403 lang_section_bst_type
**tree
;
405 if (unique_section_p (section
))
408 node
= xmalloc (sizeof (lang_section_bst_type
));
411 node
->section
= section
;
413 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
418 /* Convert a sorted sections' BST back to list form. */
421 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
422 lang_section_bst_type
*tree
,
426 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
428 lang_add_section (&ptr
->children
, tree
->section
,
429 (lang_output_section_statement_type
*) output
);
432 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
437 /* Specialized, optimized routines for handling different kinds of
441 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
442 lang_input_statement_type
*file
,
446 /* We can just do a hash lookup for the section with the right name.
447 But if that lookup discovers more than one section with the name
448 (should be rare), we fall back to the general algorithm because
449 we would otherwise have to sort the sections to make sure they
450 get processed in the bfd's order. */
451 bfd_boolean multiple_sections_found
;
452 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
453 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
455 if (multiple_sections_found
)
456 walk_wild_section_general (ptr
, file
, callback
, data
);
458 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
462 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
463 lang_input_statement_type
*file
,
468 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
470 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
472 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
473 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
476 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
481 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
482 lang_input_statement_type
*file
,
487 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
488 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
489 bfd_boolean multiple_sections_found
;
490 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
492 if (multiple_sections_found
)
494 walk_wild_section_general (ptr
, file
, callback
, data
);
498 /* Note that if the section was not found, s0 is NULL and
499 we'll simply never succeed the s == s0 test below. */
500 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
502 /* Recall that in this code path, a section cannot satisfy more
503 than one spec, so if s == s0 then it cannot match
506 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
509 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
510 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
513 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
520 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
521 lang_input_statement_type
*file
,
526 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
527 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
528 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
529 bfd_boolean multiple_sections_found
;
530 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
532 if (multiple_sections_found
)
534 walk_wild_section_general (ptr
, file
, callback
, data
);
538 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
541 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
544 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
545 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
548 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
551 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
553 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
561 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
562 lang_input_statement_type
*file
,
567 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
568 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
569 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
570 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
571 bfd_boolean multiple_sections_found
;
572 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
574 if (multiple_sections_found
)
576 walk_wild_section_general (ptr
, file
, callback
, data
);
580 s1
= find_section (file
, sec1
, &multiple_sections_found
);
581 if (multiple_sections_found
)
583 walk_wild_section_general (ptr
, file
, callback
, data
);
587 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
590 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
593 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
596 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
597 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
601 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
605 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
607 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
615 walk_wild_section (lang_wild_statement_type
*ptr
,
616 lang_input_statement_type
*file
,
620 if (file
->just_syms_flag
)
623 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
626 /* Returns TRUE when name1 is a wildcard spec that might match
627 something name2 can match. We're conservative: we return FALSE
628 only if the prefixes of name1 and name2 are different up to the
629 first wildcard character. */
632 wild_spec_can_overlap (const char *name1
, const char *name2
)
634 size_t prefix1_len
= strcspn (name1
, "?*[");
635 size_t prefix2_len
= strcspn (name2
, "?*[");
636 size_t min_prefix_len
;
638 /* Note that if there is no wildcard character, then we treat the
639 terminating 0 as part of the prefix. Thus ".text" won't match
640 ".text." or ".text.*", for example. */
641 if (name1
[prefix1_len
] == '\0')
643 if (name2
[prefix2_len
] == '\0')
646 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
648 return memcmp (name1
, name2
, min_prefix_len
) == 0;
651 /* Select specialized code to handle various kinds of wildcard
655 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
658 int wild_name_count
= 0;
659 struct wildcard_list
*sec
;
663 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
664 ptr
->handler_data
[0] = NULL
;
665 ptr
->handler_data
[1] = NULL
;
666 ptr
->handler_data
[2] = NULL
;
667 ptr
->handler_data
[3] = NULL
;
670 /* Count how many wildcard_specs there are, and how many of those
671 actually use wildcards in the name. Also, bail out if any of the
672 wildcard names are NULL. (Can this actually happen?
673 walk_wild_section used to test for it.) And bail out if any
674 of the wildcards are more complex than a simple string
675 ending in a single '*'. */
676 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
679 if (sec
->spec
.name
== NULL
)
681 if (wildcardp (sec
->spec
.name
))
684 if (!is_simple_wild (sec
->spec
.name
))
689 /* The zero-spec case would be easy to optimize but it doesn't
690 happen in practice. Likewise, more than 4 specs doesn't
691 happen in practice. */
692 if (sec_count
== 0 || sec_count
> 4)
695 /* Check that no two specs can match the same section. */
696 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
698 struct wildcard_list
*sec2
;
699 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
701 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
706 signature
= (sec_count
<< 8) + wild_name_count
;
710 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
713 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
716 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
719 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
722 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
728 /* Now fill the data array with pointers to the specs, first the
729 specs with non-wildcard names, then the specs with wildcard
730 names. It's OK to process the specs in different order from the
731 given order, because we've already determined that no section
732 will match more than one spec. */
734 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
735 if (!wildcardp (sec
->spec
.name
))
736 ptr
->handler_data
[data_counter
++] = sec
;
737 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
738 if (wildcardp (sec
->spec
.name
))
739 ptr
->handler_data
[data_counter
++] = sec
;
742 /* Handle a wild statement for a single file F. */
745 walk_wild_file (lang_wild_statement_type
*s
,
746 lang_input_statement_type
*f
,
750 if (f
->the_bfd
== NULL
751 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
752 walk_wild_section (s
, f
, callback
, data
);
757 /* This is an archive file. We must map each member of the
758 archive separately. */
759 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
760 while (member
!= NULL
)
762 /* When lookup_name is called, it will call the add_symbols
763 entry point for the archive. For each element of the
764 archive which is included, BFD will call ldlang_add_file,
765 which will set the usrdata field of the member to the
766 lang_input_statement. */
767 if (member
->usrdata
!= NULL
)
769 walk_wild_section (s
, member
->usrdata
, callback
, data
);
772 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
778 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
780 const char *file_spec
= s
->filename
;
782 if (file_spec
== NULL
)
784 /* Perform the iteration over all files in the list. */
785 LANG_FOR_EACH_INPUT_STATEMENT (f
)
787 walk_wild_file (s
, f
, callback
, data
);
790 else if (wildcardp (file_spec
))
792 LANG_FOR_EACH_INPUT_STATEMENT (f
)
794 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
795 walk_wild_file (s
, f
, callback
, data
);
800 lang_input_statement_type
*f
;
802 /* Perform the iteration over a single file. */
803 f
= lookup_name (file_spec
);
805 walk_wild_file (s
, f
, callback
, data
);
809 /* lang_for_each_statement walks the parse tree and calls the provided
810 function for each node. */
813 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
814 lang_statement_union_type
*s
)
816 for (; s
!= NULL
; s
= s
->header
.next
)
820 switch (s
->header
.type
)
822 case lang_constructors_statement_enum
:
823 lang_for_each_statement_worker (func
, constructor_list
.head
);
825 case lang_output_section_statement_enum
:
826 lang_for_each_statement_worker
827 (func
, s
->output_section_statement
.children
.head
);
829 case lang_wild_statement_enum
:
830 lang_for_each_statement_worker (func
,
831 s
->wild_statement
.children
.head
);
833 case lang_group_statement_enum
:
834 lang_for_each_statement_worker (func
,
835 s
->group_statement
.children
.head
);
837 case lang_data_statement_enum
:
838 case lang_reloc_statement_enum
:
839 case lang_object_symbols_statement_enum
:
840 case lang_output_statement_enum
:
841 case lang_target_statement_enum
:
842 case lang_input_section_enum
:
843 case lang_input_statement_enum
:
844 case lang_assignment_statement_enum
:
845 case lang_padding_statement_enum
:
846 case lang_address_statement_enum
:
847 case lang_fill_statement_enum
:
848 case lang_insert_statement_enum
:
858 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
860 lang_for_each_statement_worker (func
, statement_list
.head
);
863 /*----------------------------------------------------------------------*/
866 lang_list_init (lang_statement_list_type
*list
)
869 list
->tail
= &list
->head
;
872 /* Build a new statement node for the parse tree. */
874 static lang_statement_union_type
*
875 new_statement (enum statement_enum type
,
877 lang_statement_list_type
*list
)
879 lang_statement_union_type
*new;
881 new = stat_alloc (size
);
882 new->header
.type
= type
;
883 new->header
.next
= NULL
;
884 lang_statement_append (list
, new, &new->header
.next
);
888 /* Build a new input file node for the language. There are several
889 ways in which we treat an input file, eg, we only look at symbols,
890 or prefix it with a -l etc.
892 We can be supplied with requests for input files more than once;
893 they may, for example be split over several lines like foo.o(.text)
894 foo.o(.data) etc, so when asked for a file we check that we haven't
895 got it already so we don't duplicate the bfd. */
897 static lang_input_statement_type
*
898 new_afile (const char *name
,
899 lang_input_file_enum_type file_type
,
901 bfd_boolean add_to_list
)
903 lang_input_statement_type
*p
;
906 p
= new_stat (lang_input_statement
, stat_ptr
);
909 p
= stat_alloc (sizeof (lang_input_statement_type
));
910 p
->header
.type
= lang_input_statement_enum
;
911 p
->header
.next
= NULL
;
914 lang_has_input_file
= TRUE
;
916 p
->sysrooted
= FALSE
;
918 if (file_type
== lang_input_file_is_l_enum
919 && name
[0] == ':' && name
[1] != '\0')
921 file_type
= lang_input_file_is_search_file_enum
;
927 case lang_input_file_is_symbols_only_enum
:
929 p
->is_archive
= FALSE
;
931 p
->local_sym_name
= name
;
932 p
->just_syms_flag
= TRUE
;
933 p
->search_dirs_flag
= FALSE
;
935 case lang_input_file_is_fake_enum
:
937 p
->is_archive
= FALSE
;
939 p
->local_sym_name
= name
;
940 p
->just_syms_flag
= FALSE
;
941 p
->search_dirs_flag
= FALSE
;
943 case lang_input_file_is_l_enum
:
944 p
->is_archive
= TRUE
;
947 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
948 p
->just_syms_flag
= FALSE
;
949 p
->search_dirs_flag
= TRUE
;
951 case lang_input_file_is_marker_enum
:
953 p
->is_archive
= FALSE
;
955 p
->local_sym_name
= name
;
956 p
->just_syms_flag
= FALSE
;
957 p
->search_dirs_flag
= TRUE
;
959 case lang_input_file_is_search_file_enum
:
960 p
->sysrooted
= ldlang_sysrooted_script
;
962 p
->is_archive
= FALSE
;
964 p
->local_sym_name
= name
;
965 p
->just_syms_flag
= FALSE
;
966 p
->search_dirs_flag
= TRUE
;
968 case lang_input_file_is_file_enum
:
970 p
->is_archive
= FALSE
;
972 p
->local_sym_name
= name
;
973 p
->just_syms_flag
= FALSE
;
974 p
->search_dirs_flag
= FALSE
;
981 p
->next_real_file
= NULL
;
984 p
->dynamic
= config
.dynamic_link
;
985 p
->add_needed
= add_needed
;
986 p
->as_needed
= as_needed
;
987 p
->whole_archive
= whole_archive
;
989 lang_statement_append (&input_file_chain
,
990 (lang_statement_union_type
*) p
,
995 lang_input_statement_type
*
996 lang_add_input_file (const char *name
,
997 lang_input_file_enum_type file_type
,
1000 return new_afile (name
, file_type
, target
, TRUE
);
1003 struct out_section_hash_entry
1005 struct bfd_hash_entry root
;
1006 lang_statement_union_type s
;
1009 /* The hash table. */
1011 static struct bfd_hash_table output_section_statement_table
;
1013 /* Support routines for the hash table used by lang_output_section_find,
1014 initialize the table, fill in an entry and remove the table. */
1016 static struct bfd_hash_entry
*
1017 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1018 struct bfd_hash_table
*table
,
1021 lang_output_section_statement_type
**nextp
;
1022 struct out_section_hash_entry
*ret
;
1026 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
1031 entry
= bfd_hash_newfunc (entry
, table
, string
);
1035 ret
= (struct out_section_hash_entry
*) entry
;
1036 memset (&ret
->s
, 0, sizeof (ret
->s
));
1037 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1038 ret
->s
.output_section_statement
.subsection_alignment
= -1;
1039 ret
->s
.output_section_statement
.section_alignment
= -1;
1040 ret
->s
.output_section_statement
.block_value
= 1;
1041 lang_list_init (&ret
->s
.output_section_statement
.children
);
1042 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1044 /* For every output section statement added to the list, except the
1045 first one, lang_output_section_statement.tail points to the "next"
1046 field of the last element of the list. */
1047 if (lang_output_section_statement
.head
!= NULL
)
1048 ret
->s
.output_section_statement
.prev
1049 = ((lang_output_section_statement_type
*)
1050 ((char *) lang_output_section_statement
.tail
1051 - offsetof (lang_output_section_statement_type
, next
)));
1053 /* GCC's strict aliasing rules prevent us from just casting the
1054 address, so we store the pointer in a variable and cast that
1056 nextp
= &ret
->s
.output_section_statement
.next
;
1057 lang_statement_append (&lang_output_section_statement
,
1059 (lang_statement_union_type
**) nextp
);
1064 output_section_statement_table_init (void)
1066 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1067 output_section_statement_newfunc
,
1068 sizeof (struct out_section_hash_entry
),
1070 einfo (_("%P%F: can not create hash table: %E\n"));
1074 output_section_statement_table_free (void)
1076 bfd_hash_table_free (&output_section_statement_table
);
1079 /* Build enough state so that the parser can build its tree. */
1084 obstack_begin (&stat_obstack
, 1000);
1086 stat_ptr
= &statement_list
;
1088 output_section_statement_table_init ();
1090 lang_list_init (stat_ptr
);
1092 lang_list_init (&input_file_chain
);
1093 lang_list_init (&lang_output_section_statement
);
1094 lang_list_init (&file_chain
);
1095 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1097 abs_output_section
=
1098 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
1100 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1102 /* The value "3" is ad-hoc, somewhat related to the expected number of
1103 DEFINED expressions in a linker script. For most default linker
1104 scripts, there are none. Why a hash table then? Well, it's somewhat
1105 simpler to re-use working machinery than using a linked list in terms
1106 of code-complexity here in ld, besides the initialization which just
1107 looks like other code here. */
1108 if (!bfd_hash_table_init_n (&lang_definedness_table
,
1109 lang_definedness_newfunc
,
1110 sizeof (struct lang_definedness_hash_entry
),
1112 einfo (_("%P%F: can not create hash table: %E\n"));
1118 output_section_statement_table_free ();
1121 /*----------------------------------------------------------------------
1122 A region is an area of memory declared with the
1123 MEMORY { name:org=exp, len=exp ... }
1126 We maintain a list of all the regions here.
1128 If no regions are specified in the script, then the default is used
1129 which is created when looked up to be the entire data space.
1131 If create is true we are creating a region inside a MEMORY block.
1132 In this case it is probably an error to create a region that has
1133 already been created. If we are not inside a MEMORY block it is
1134 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1135 and so we issue a warning. */
1137 static lang_memory_region_type
*lang_memory_region_list
;
1138 static lang_memory_region_type
**lang_memory_region_list_tail
1139 = &lang_memory_region_list
;
1141 lang_memory_region_type
*
1142 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1144 lang_memory_region_type
*p
;
1145 lang_memory_region_type
*new;
1147 /* NAME is NULL for LMA memspecs if no region was specified. */
1151 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1152 if (strcmp (p
->name
, name
) == 0)
1155 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1160 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1161 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
1163 new = stat_alloc (sizeof (lang_memory_region_type
));
1165 new->name
= xstrdup (name
);
1168 new->length
= ~(bfd_size_type
) 0;
1170 new->last_os
= NULL
;
1173 new->had_full_message
= FALSE
;
1175 *lang_memory_region_list_tail
= new;
1176 lang_memory_region_list_tail
= &new->next
;
1181 static lang_memory_region_type
*
1182 lang_memory_default (asection
*section
)
1184 lang_memory_region_type
*p
;
1186 flagword sec_flags
= section
->flags
;
1188 /* Override SEC_DATA to mean a writable section. */
1189 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1190 sec_flags
|= SEC_DATA
;
1192 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1194 if ((p
->flags
& sec_flags
) != 0
1195 && (p
->not_flags
& sec_flags
) == 0)
1200 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1203 lang_output_section_statement_type
*
1204 lang_output_section_find (const char *const name
)
1206 struct out_section_hash_entry
*entry
;
1209 entry
= ((struct out_section_hash_entry
*)
1210 bfd_hash_lookup (&output_section_statement_table
, name
,
1215 hash
= entry
->root
.hash
;
1218 if (entry
->s
.output_section_statement
.constraint
!= -1)
1219 return &entry
->s
.output_section_statement
;
1220 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1222 while (entry
!= NULL
1223 && entry
->root
.hash
== hash
1224 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1229 static lang_output_section_statement_type
*
1230 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
1232 struct out_section_hash_entry
*entry
;
1233 struct out_section_hash_entry
*last_ent
;
1236 entry
= ((struct out_section_hash_entry
*)
1237 bfd_hash_lookup (&output_section_statement_table
, name
,
1241 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1245 if (entry
->s
.output_section_statement
.name
!= NULL
)
1247 /* We have a section of this name, but it might not have the correct
1249 hash
= entry
->root
.hash
;
1252 if (entry
->s
.output_section_statement
.constraint
!= -1
1254 || (constraint
== entry
->s
.output_section_statement
.constraint
1255 && constraint
!= SPECIAL
)))
1256 return &entry
->s
.output_section_statement
;
1258 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1260 while (entry
!= NULL
1261 && entry
->root
.hash
== hash
1262 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1265 = ((struct out_section_hash_entry
*)
1266 output_section_statement_newfunc (NULL
,
1267 &output_section_statement_table
,
1271 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1274 entry
->root
= last_ent
->root
;
1275 last_ent
->root
.next
= &entry
->root
;
1278 entry
->s
.output_section_statement
.name
= name
;
1279 entry
->s
.output_section_statement
.constraint
= constraint
;
1280 return &entry
->s
.output_section_statement
;
1283 lang_output_section_statement_type
*
1284 lang_output_section_statement_lookup (const char *const name
)
1286 return lang_output_section_statement_lookup_1 (name
, 0);
1289 /* A variant of lang_output_section_find used by place_orphan.
1290 Returns the output statement that should precede a new output
1291 statement for SEC. If an exact match is found on certain flags,
1294 lang_output_section_statement_type
*
1295 lang_output_section_find_by_flags (const asection
*sec
,
1296 lang_output_section_statement_type
**exact
,
1297 lang_match_sec_type_func match_type
)
1299 lang_output_section_statement_type
*first
, *look
, *found
;
1302 /* We know the first statement on this list is *ABS*. May as well
1304 first
= &lang_output_section_statement
.head
->output_section_statement
;
1305 first
= first
->next
;
1307 /* First try for an exact match. */
1309 for (look
= first
; look
; look
= look
->next
)
1311 flags
= look
->flags
;
1312 if (look
->bfd_section
!= NULL
)
1314 flags
= look
->bfd_section
->flags
;
1315 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1319 flags
^= sec
->flags
;
1320 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1321 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1331 if (sec
->flags
& SEC_CODE
)
1333 /* Try for a rw code section. */
1334 for (look
= first
; look
; look
= look
->next
)
1336 flags
= look
->flags
;
1337 if (look
->bfd_section
!= NULL
)
1339 flags
= look
->bfd_section
->flags
;
1340 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1344 flags
^= sec
->flags
;
1345 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1346 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1350 else if (sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
))
1352 /* .rodata can go after .text, .sdata2 after .rodata. */
1353 for (look
= first
; look
; look
= look
->next
)
1355 flags
= look
->flags
;
1356 if (look
->bfd_section
!= NULL
)
1358 flags
= look
->bfd_section
->flags
;
1359 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1363 flags
^= sec
->flags
;
1364 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1366 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1370 else if (sec
->flags
& SEC_SMALL_DATA
)
1372 /* .sdata goes after .data, .sbss after .sdata. */
1373 for (look
= first
; look
; look
= look
->next
)
1375 flags
= look
->flags
;
1376 if (look
->bfd_section
!= NULL
)
1378 flags
= look
->bfd_section
->flags
;
1379 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1383 flags
^= sec
->flags
;
1384 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1385 | SEC_THREAD_LOCAL
))
1386 || ((look
->flags
& SEC_SMALL_DATA
)
1387 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1391 else if (sec
->flags
& SEC_HAS_CONTENTS
)
1393 /* .data goes after .rodata. */
1394 for (look
= first
; look
; look
= look
->next
)
1396 flags
= look
->flags
;
1397 if (look
->bfd_section
!= NULL
)
1399 flags
= look
->bfd_section
->flags
;
1400 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1404 flags
^= sec
->flags
;
1405 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1406 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1412 /* .bss goes last. */
1413 for (look
= first
; look
; look
= look
->next
)
1415 flags
= look
->flags
;
1416 if (look
->bfd_section
!= NULL
)
1418 flags
= look
->bfd_section
->flags
;
1419 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1423 flags
^= sec
->flags
;
1424 if (!(flags
& SEC_ALLOC
))
1429 if (found
|| !match_type
)
1432 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1435 /* Find the last output section before given output statement.
1436 Used by place_orphan. */
1439 output_prev_sec_find (lang_output_section_statement_type
*os
)
1441 lang_output_section_statement_type
*lookup
;
1443 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1445 if (lookup
->constraint
== -1)
1448 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1449 return lookup
->bfd_section
;
1455 /* Look for a suitable place for a new output section statement. The
1456 idea is to skip over anything that might be inside a SECTIONS {}
1457 statement in a script, before we find another output section
1458 statement. Assignments to "dot" before an output section statement
1459 are assumed to belong to it. An exception to this rule is made for
1460 the first assignment to dot, otherwise we might put an orphan
1461 before . = . + SIZEOF_HEADERS or similar assignments that set the
1464 static lang_statement_union_type
**
1465 insert_os_after (lang_output_section_statement_type
*after
)
1467 lang_statement_union_type
**where
;
1468 lang_statement_union_type
**assign
= NULL
;
1469 bfd_boolean ignore_first
;
1472 = after
== &lang_output_section_statement
.head
->output_section_statement
;
1474 for (where
= &after
->header
.next
;
1476 where
= &(*where
)->header
.next
)
1478 switch ((*where
)->header
.type
)
1480 case lang_assignment_statement_enum
:
1483 lang_assignment_statement_type
*ass
;
1485 ass
= &(*where
)->assignment_statement
;
1486 if (ass
->exp
->type
.node_class
!= etree_assert
1487 && ass
->exp
->assign
.dst
[0] == '.'
1488 && ass
->exp
->assign
.dst
[1] == 0
1492 ignore_first
= FALSE
;
1494 case lang_wild_statement_enum
:
1495 case lang_input_section_enum
:
1496 case lang_object_symbols_statement_enum
:
1497 case lang_fill_statement_enum
:
1498 case lang_data_statement_enum
:
1499 case lang_reloc_statement_enum
:
1500 case lang_padding_statement_enum
:
1501 case lang_constructors_statement_enum
:
1504 case lang_output_section_statement_enum
:
1508 case lang_input_statement_enum
:
1509 case lang_address_statement_enum
:
1510 case lang_target_statement_enum
:
1511 case lang_output_statement_enum
:
1512 case lang_group_statement_enum
:
1513 case lang_insert_statement_enum
:
1522 lang_output_section_statement_type
*
1523 lang_insert_orphan (asection
*s
,
1524 const char *secname
,
1525 lang_output_section_statement_type
*after
,
1526 struct orphan_save
*place
,
1527 etree_type
*address
,
1528 lang_statement_list_type
*add_child
)
1530 lang_statement_list_type
*old
;
1531 lang_statement_list_type add
;
1533 lang_output_section_statement_type
*os
;
1534 lang_output_section_statement_type
**os_tail
;
1536 /* Start building a list of statements for this section.
1537 First save the current statement pointer. */
1540 /* If we have found an appropriate place for the output section
1541 statements for this orphan, add them to our own private list,
1542 inserting them later into the global statement list. */
1546 lang_list_init (stat_ptr
);
1550 if (config
.build_constructors
)
1552 /* If the name of the section is representable in C, then create
1553 symbols to mark the start and the end of the section. */
1554 for (ps
= secname
; *ps
!= '\0'; ps
++)
1555 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1560 etree_type
*e_align
;
1562 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1563 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1564 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1565 e_align
= exp_unop (ALIGN_K
,
1566 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1567 lang_add_assignment (exp_assop ('=', ".", e_align
));
1568 lang_add_assignment (exp_provide (symname
,
1569 exp_nameop (NAME
, "."),
1574 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1575 address
= exp_intop (0);
1577 os_tail
= ((lang_output_section_statement_type
**)
1578 lang_output_section_statement
.tail
);
1579 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1582 if (add_child
== NULL
)
1583 add_child
= &os
->children
;
1584 lang_add_section (add_child
, s
, os
);
1586 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1588 if (config
.build_constructors
&& *ps
== '\0')
1592 /* lang_leave_ouput_section_statement resets stat_ptr.
1593 Put stat_ptr back where we want it. */
1597 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1598 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1599 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1600 lang_add_assignment (exp_provide (symname
,
1601 exp_nameop (NAME
, "."),
1605 /* Restore the global list pointer. */
1609 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1611 asection
*snew
, *as
;
1613 snew
= os
->bfd_section
;
1615 /* Shuffle the bfd section list to make the output file look
1616 neater. This is really only cosmetic. */
1617 if (place
->section
== NULL
1618 && after
!= (&lang_output_section_statement
.head
1619 ->output_section_statement
))
1621 asection
*bfd_section
= after
->bfd_section
;
1623 /* If the output statement hasn't been used to place any input
1624 sections (and thus doesn't have an output bfd_section),
1625 look for the closest prior output statement having an
1627 if (bfd_section
== NULL
)
1628 bfd_section
= output_prev_sec_find (after
);
1630 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1631 place
->section
= &bfd_section
->next
;
1634 if (place
->section
== NULL
)
1635 place
->section
= &output_bfd
->sections
;
1637 as
= *place
->section
;
1641 /* Put the section at the end of the list. */
1643 /* Unlink the section. */
1644 bfd_section_list_remove (output_bfd
, snew
);
1646 /* Now tack it back on in the right place. */
1647 bfd_section_list_append (output_bfd
, snew
);
1649 else if (as
!= snew
&& as
->prev
!= snew
)
1651 /* Unlink the section. */
1652 bfd_section_list_remove (output_bfd
, snew
);
1654 /* Now tack it back on in the right place. */
1655 bfd_section_list_insert_before (output_bfd
, as
, snew
);
1658 /* Save the end of this list. Further ophans of this type will
1659 follow the one we've just added. */
1660 place
->section
= &snew
->next
;
1662 /* The following is non-cosmetic. We try to put the output
1663 statements in some sort of reasonable order here, because they
1664 determine the final load addresses of the orphan sections.
1665 In addition, placing output statements in the wrong order may
1666 require extra segments. For instance, given a typical
1667 situation of all read-only sections placed in one segment and
1668 following that a segment containing all the read-write
1669 sections, we wouldn't want to place an orphan read/write
1670 section before or amongst the read-only ones. */
1671 if (add
.head
!= NULL
)
1673 lang_output_section_statement_type
*newly_added_os
;
1675 if (place
->stmt
== NULL
)
1677 lang_statement_union_type
**where
= insert_os_after (after
);
1682 place
->os_tail
= &after
->next
;
1686 /* Put it after the last orphan statement we added. */
1687 *add
.tail
= *place
->stmt
;
1688 *place
->stmt
= add
.head
;
1691 /* Fix the global list pointer if we happened to tack our
1692 new list at the tail. */
1693 if (*old
->tail
== add
.head
)
1694 old
->tail
= add
.tail
;
1696 /* Save the end of this list. */
1697 place
->stmt
= add
.tail
;
1699 /* Do the same for the list of output section statements. */
1700 newly_added_os
= *os_tail
;
1702 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1703 ((char *) place
->os_tail
1704 - offsetof (lang_output_section_statement_type
, next
));
1705 newly_added_os
->next
= *place
->os_tail
;
1706 if (newly_added_os
->next
!= NULL
)
1707 newly_added_os
->next
->prev
= newly_added_os
;
1708 *place
->os_tail
= newly_added_os
;
1709 place
->os_tail
= &newly_added_os
->next
;
1711 /* Fixing the global list pointer here is a little different.
1712 We added to the list in lang_enter_output_section_statement,
1713 trimmed off the new output_section_statment above when
1714 assigning *os_tail = NULL, but possibly added it back in
1715 the same place when assigning *place->os_tail. */
1716 if (*os_tail
== NULL
)
1717 lang_output_section_statement
.tail
1718 = (lang_statement_union_type
**) os_tail
;
1725 lang_map_flags (flagword flag
)
1727 if (flag
& SEC_ALLOC
)
1730 if (flag
& SEC_CODE
)
1733 if (flag
& SEC_READONLY
)
1736 if (flag
& SEC_DATA
)
1739 if (flag
& SEC_LOAD
)
1746 lang_memory_region_type
*m
;
1747 bfd_boolean dis_header_printed
= FALSE
;
1750 LANG_FOR_EACH_INPUT_STATEMENT (file
)
1754 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
1755 || file
->just_syms_flag
)
1758 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1759 if ((s
->output_section
== NULL
1760 || s
->output_section
->owner
!= output_bfd
)
1761 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
1763 if (! dis_header_printed
)
1765 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
1766 dis_header_printed
= TRUE
;
1769 print_input_section (s
);
1773 minfo (_("\nMemory Configuration\n\n"));
1774 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1775 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1777 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1782 fprintf (config
.map_file
, "%-16s ", m
->name
);
1784 sprintf_vma (buf
, m
->origin
);
1785 minfo ("0x%s ", buf
);
1793 minfo ("0x%V", m
->length
);
1794 if (m
->flags
|| m
->not_flags
)
1802 lang_map_flags (m
->flags
);
1808 lang_map_flags (m
->not_flags
);
1815 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1817 if (! link_info
.reduce_memory_overheads
)
1819 obstack_begin (&map_obstack
, 1000);
1820 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1821 bfd_map_over_sections (p
, init_map_userdata
, 0);
1822 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1824 lang_statement_iteration
++;
1825 print_statements ();
1829 init_map_userdata (abfd
, sec
, data
)
1830 bfd
*abfd ATTRIBUTE_UNUSED
;
1832 void *data ATTRIBUTE_UNUSED
;
1834 fat_section_userdata_type
*new_data
1835 = ((fat_section_userdata_type
*) (stat_alloc
1836 (sizeof (fat_section_userdata_type
))));
1838 ASSERT (get_userdata (sec
) == NULL
);
1839 get_userdata (sec
) = new_data
;
1840 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
1844 sort_def_symbol (hash_entry
, info
)
1845 struct bfd_link_hash_entry
*hash_entry
;
1846 void *info ATTRIBUTE_UNUSED
;
1848 if (hash_entry
->type
== bfd_link_hash_defined
1849 || hash_entry
->type
== bfd_link_hash_defweak
)
1851 struct fat_user_section_struct
*ud
;
1852 struct map_symbol_def
*def
;
1854 ud
= get_userdata (hash_entry
->u
.def
.section
);
1857 /* ??? What do we have to do to initialize this beforehand? */
1858 /* The first time we get here is bfd_abs_section... */
1859 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
1860 ud
= get_userdata (hash_entry
->u
.def
.section
);
1862 else if (!ud
->map_symbol_def_tail
)
1863 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
1865 def
= obstack_alloc (&map_obstack
, sizeof *def
);
1866 def
->entry
= hash_entry
;
1867 *(ud
->map_symbol_def_tail
) = def
;
1868 ud
->map_symbol_def_tail
= &def
->next
;
1873 /* Initialize an output section. */
1876 init_os (lang_output_section_statement_type
*s
, asection
*isec
,
1879 if (s
->bfd_section
!= NULL
)
1882 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1883 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1885 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
1886 if (s
->bfd_section
== NULL
)
1887 s
->bfd_section
= bfd_make_section_with_flags (output_bfd
, s
->name
,
1889 if (s
->bfd_section
== NULL
)
1891 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1892 output_bfd
->xvec
->name
, s
->name
);
1894 s
->bfd_section
->output_section
= s
->bfd_section
;
1895 s
->bfd_section
->output_offset
= 0;
1897 if (!link_info
.reduce_memory_overheads
)
1899 fat_section_userdata_type
*new
1900 = stat_alloc (sizeof (fat_section_userdata_type
));
1901 memset (new, 0, sizeof (fat_section_userdata_type
));
1902 get_userdata (s
->bfd_section
) = new;
1905 /* If there is a base address, make sure that any sections it might
1906 mention are initialized. */
1907 if (s
->addr_tree
!= NULL
)
1908 exp_init_os (s
->addr_tree
);
1910 if (s
->load_base
!= NULL
)
1911 exp_init_os (s
->load_base
);
1913 /* If supplied an alignment, set it. */
1914 if (s
->section_alignment
!= -1)
1915 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1918 bfd_init_private_section_data (isec
->owner
, isec
,
1919 output_bfd
, s
->bfd_section
,
1923 /* Make sure that all output sections mentioned in an expression are
1927 exp_init_os (etree_type
*exp
)
1929 switch (exp
->type
.node_class
)
1933 exp_init_os (exp
->assign
.src
);
1937 exp_init_os (exp
->binary
.lhs
);
1938 exp_init_os (exp
->binary
.rhs
);
1942 exp_init_os (exp
->trinary
.cond
);
1943 exp_init_os (exp
->trinary
.lhs
);
1944 exp_init_os (exp
->trinary
.rhs
);
1948 exp_init_os (exp
->assert_s
.child
);
1952 exp_init_os (exp
->unary
.child
);
1956 switch (exp
->type
.node_code
)
1962 lang_output_section_statement_type
*os
;
1964 os
= lang_output_section_find (exp
->name
.name
);
1965 if (os
!= NULL
&& os
->bfd_section
== NULL
)
1966 init_os (os
, NULL
, 0);
1977 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
1979 lang_input_statement_type
*entry
= data
;
1981 /* If we are only reading symbols from this object, then we want to
1982 discard all sections. */
1983 if (entry
->just_syms_flag
)
1985 bfd_link_just_syms (abfd
, sec
, &link_info
);
1989 if (!(abfd
->flags
& DYNAMIC
))
1990 bfd_section_already_linked (abfd
, sec
, &link_info
);
1993 /* The wild routines.
1995 These expand statements like *(.text) and foo.o to a list of
1996 explicit actions, like foo.o(.text), bar.o(.text) and
1997 foo.o(.text, .data). */
1999 /* Add SECTION to the output section OUTPUT. Do this by creating a
2000 lang_input_section statement which is placed at PTR. FILE is the
2001 input file which holds SECTION. */
2004 lang_add_section (lang_statement_list_type
*ptr
,
2006 lang_output_section_statement_type
*output
)
2008 flagword flags
= section
->flags
;
2009 bfd_boolean discard
;
2011 /* Discard sections marked with SEC_EXCLUDE. */
2012 discard
= (flags
& SEC_EXCLUDE
) != 0;
2014 /* Discard input sections which are assigned to a section named
2015 DISCARD_SECTION_NAME. */
2016 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2019 /* Discard debugging sections if we are stripping debugging
2021 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2022 && (flags
& SEC_DEBUGGING
) != 0)
2027 if (section
->output_section
== NULL
)
2029 /* This prevents future calls from assigning this section. */
2030 section
->output_section
= bfd_abs_section_ptr
;
2035 if (section
->output_section
== NULL
)
2038 lang_input_section_type
*new;
2041 flags
= section
->flags
;
2043 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2044 to an output section, because we want to be able to include a
2045 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2046 section (I don't know why we want to do this, but we do).
2047 build_link_order in ldwrite.c handles this case by turning
2048 the embedded SEC_NEVER_LOAD section into a fill. */
2050 flags
&= ~ SEC_NEVER_LOAD
;
2052 switch (output
->sectype
)
2054 case normal_section
:
2055 case overlay_section
:
2057 case noalloc_section
:
2058 flags
&= ~SEC_ALLOC
;
2060 case noload_section
:
2062 flags
|= SEC_NEVER_LOAD
;
2066 if (output
->bfd_section
== NULL
)
2067 init_os (output
, section
, flags
);
2069 first
= ! output
->bfd_section
->linker_has_input
;
2070 output
->bfd_section
->linker_has_input
= 1;
2072 if (!link_info
.relocatable
2073 && !stripped_excluded_sections
)
2075 asection
*s
= output
->bfd_section
->map_tail
.s
;
2076 output
->bfd_section
->map_tail
.s
= section
;
2077 section
->map_head
.s
= NULL
;
2078 section
->map_tail
.s
= s
;
2080 s
->map_head
.s
= section
;
2082 output
->bfd_section
->map_head
.s
= section
;
2085 /* Add a section reference to the list. */
2086 new = new_stat (lang_input_section
, ptr
);
2088 new->section
= section
;
2089 section
->output_section
= output
->bfd_section
;
2091 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2092 already been processed. One reason to do this is that on pe
2093 format targets, .text$foo sections go into .text and it's odd
2094 to see .text with SEC_LINK_ONCE set. */
2096 if (! link_info
.relocatable
)
2097 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
2099 /* If this is not the first input section, and the SEC_READONLY
2100 flag is not currently set, then don't set it just because the
2101 input section has it set. */
2103 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
2104 flags
&= ~ SEC_READONLY
;
2106 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2108 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2109 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2110 || ((flags
& SEC_MERGE
)
2111 && output
->bfd_section
->entsize
!= section
->entsize
)))
2113 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2114 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2117 output
->bfd_section
->flags
|= flags
;
2119 if (flags
& SEC_MERGE
)
2120 output
->bfd_section
->entsize
= section
->entsize
;
2122 /* If SEC_READONLY is not set in the input section, then clear
2123 it from the output section. */
2124 if ((section
->flags
& SEC_READONLY
) == 0)
2125 output
->bfd_section
->flags
&= ~SEC_READONLY
;
2127 /* Copy over SEC_SMALL_DATA. */
2128 if (section
->flags
& SEC_SMALL_DATA
)
2129 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
2131 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2132 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2134 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
2135 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
2137 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2138 /* FIXME: This value should really be obtained from the bfd... */
2139 output
->block_value
= 128;
2144 /* Handle wildcard sorting. This returns the lang_input_section which
2145 should follow the one we are going to create for SECTION and FILE,
2146 based on the sorting requirements of WILD. It returns NULL if the
2147 new section should just go at the end of the current list. */
2149 static lang_statement_union_type
*
2150 wild_sort (lang_wild_statement_type
*wild
,
2151 struct wildcard_list
*sec
,
2152 lang_input_statement_type
*file
,
2155 const char *section_name
;
2156 lang_statement_union_type
*l
;
2158 if (!wild
->filenames_sorted
2159 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2162 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2163 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2165 lang_input_section_type
*ls
;
2167 if (l
->header
.type
!= lang_input_section_enum
)
2169 ls
= &l
->input_section
;
2171 /* Sorting by filename takes precedence over sorting by section
2174 if (wild
->filenames_sorted
)
2176 const char *fn
, *ln
;
2180 /* The PE support for the .idata section as generated by
2181 dlltool assumes that files will be sorted by the name of
2182 the archive and then the name of the file within the
2185 if (file
->the_bfd
!= NULL
2186 && bfd_my_archive (file
->the_bfd
) != NULL
)
2188 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2193 fn
= file
->filename
;
2197 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2199 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2204 ln
= ls
->section
->owner
->filename
;
2208 i
= strcmp (fn
, ln
);
2217 fn
= file
->filename
;
2219 ln
= ls
->section
->owner
->filename
;
2221 i
= strcmp (fn
, ln
);
2229 /* Here either the files are not sorted by name, or we are
2230 looking at the sections for this file. */
2232 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2233 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2240 /* Expand a wild statement for a particular FILE. SECTION may be
2241 NULL, in which case it is a wild card. */
2244 output_section_callback (lang_wild_statement_type
*ptr
,
2245 struct wildcard_list
*sec
,
2247 lang_input_statement_type
*file
,
2250 lang_statement_union_type
*before
;
2252 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2253 if (unique_section_p (section
))
2256 before
= wild_sort (ptr
, sec
, file
, section
);
2258 /* Here BEFORE points to the lang_input_section which
2259 should follow the one we are about to add. If BEFORE
2260 is NULL, then the section should just go at the end
2261 of the current list. */
2264 lang_add_section (&ptr
->children
, section
,
2265 (lang_output_section_statement_type
*) output
);
2268 lang_statement_list_type list
;
2269 lang_statement_union_type
**pp
;
2271 lang_list_init (&list
);
2272 lang_add_section (&list
, section
,
2273 (lang_output_section_statement_type
*) output
);
2275 /* If we are discarding the section, LIST.HEAD will
2277 if (list
.head
!= NULL
)
2279 ASSERT (list
.head
->header
.next
== NULL
);
2281 for (pp
= &ptr
->children
.head
;
2283 pp
= &(*pp
)->header
.next
)
2284 ASSERT (*pp
!= NULL
);
2286 list
.head
->header
.next
= *pp
;
2292 /* Check if all sections in a wild statement for a particular FILE
2296 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2297 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2299 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2302 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2303 if (unique_section_p (section
))
2306 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2307 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2310 /* This is passed a file name which must have been seen already and
2311 added to the statement tree. We will see if it has been opened
2312 already and had its symbols read. If not then we'll read it. */
2314 static lang_input_statement_type
*
2315 lookup_name (const char *name
)
2317 lang_input_statement_type
*search
;
2319 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2321 search
= (lang_input_statement_type
*) search
->next_real_file
)
2323 /* Use the local_sym_name as the name of the file that has
2324 already been loaded as filename might have been transformed
2325 via the search directory lookup mechanism. */
2326 const char *filename
= search
->local_sym_name
;
2328 if (filename
!= NULL
2329 && strcmp (filename
, name
) == 0)
2334 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2335 default_target
, FALSE
);
2337 /* If we have already added this file, or this file is not real
2338 don't add this file. */
2339 if (search
->loaded
|| !search
->real
)
2342 if (! load_symbols (search
, NULL
))
2348 /* Save LIST as a list of libraries whose symbols should not be exported. */
2353 struct excluded_lib
*next
;
2355 static struct excluded_lib
*excluded_libs
;
2358 add_excluded_libs (const char *list
)
2360 const char *p
= list
, *end
;
2364 struct excluded_lib
*entry
;
2365 end
= strpbrk (p
, ",:");
2367 end
= p
+ strlen (p
);
2368 entry
= xmalloc (sizeof (*entry
));
2369 entry
->next
= excluded_libs
;
2370 entry
->name
= xmalloc (end
- p
+ 1);
2371 memcpy (entry
->name
, p
, end
- p
);
2372 entry
->name
[end
- p
] = '\0';
2373 excluded_libs
= entry
;
2381 check_excluded_libs (bfd
*abfd
)
2383 struct excluded_lib
*lib
= excluded_libs
;
2387 int len
= strlen (lib
->name
);
2388 const char *filename
= lbasename (abfd
->filename
);
2390 if (strcmp (lib
->name
, "ALL") == 0)
2392 abfd
->no_export
= TRUE
;
2396 if (strncmp (lib
->name
, filename
, len
) == 0
2397 && (filename
[len
] == '\0'
2398 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2399 && filename
[len
+ 2] == '\0')))
2401 abfd
->no_export
= TRUE
;
2409 /* Get the symbols for an input file. */
2412 load_symbols (lang_input_statement_type
*entry
,
2413 lang_statement_list_type
*place
)
2420 ldfile_open_file (entry
);
2422 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2423 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2426 lang_statement_list_type
*hold
;
2427 bfd_boolean bad_load
= TRUE
;
2428 bfd_boolean save_ldlang_sysrooted_script
;
2429 bfd_boolean save_as_needed
, save_add_needed
;
2431 err
= bfd_get_error ();
2433 /* See if the emulation has some special knowledge. */
2434 if (ldemul_unrecognized_file (entry
))
2437 if (err
== bfd_error_file_ambiguously_recognized
)
2441 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2442 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2443 for (p
= matching
; *p
!= NULL
; p
++)
2447 else if (err
!= bfd_error_file_not_recognized
2449 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2453 bfd_close (entry
->the_bfd
);
2454 entry
->the_bfd
= NULL
;
2456 /* Try to interpret the file as a linker script. */
2457 ldfile_open_command_file (entry
->filename
);
2461 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2462 ldlang_sysrooted_script
= entry
->sysrooted
;
2463 save_as_needed
= as_needed
;
2464 as_needed
= entry
->as_needed
;
2465 save_add_needed
= add_needed
;
2466 add_needed
= entry
->add_needed
;
2468 ldfile_assumed_script
= TRUE
;
2469 parser_input
= input_script
;
2470 /* We want to use the same -Bdynamic/-Bstatic as the one for
2472 config
.dynamic_link
= entry
->dynamic
;
2474 ldfile_assumed_script
= FALSE
;
2476 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2477 as_needed
= save_as_needed
;
2478 add_needed
= save_add_needed
;
2484 if (ldemul_recognized_file (entry
))
2487 /* We don't call ldlang_add_file for an archive. Instead, the
2488 add_symbols entry point will call ldlang_add_file, via the
2489 add_archive_element callback, for each element of the archive
2491 switch (bfd_get_format (entry
->the_bfd
))
2497 ldlang_add_file (entry
);
2498 if (trace_files
|| trace_file_tries
)
2499 info_msg ("%I\n", entry
);
2503 check_excluded_libs (entry
->the_bfd
);
2505 if (entry
->whole_archive
)
2508 bfd_boolean loaded
= TRUE
;
2512 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2517 if (! bfd_check_format (member
, bfd_object
))
2519 einfo (_("%F%B: member %B in archive is not an object\n"),
2520 entry
->the_bfd
, member
);
2524 if (! ((*link_info
.callbacks
->add_archive_element
)
2525 (&link_info
, member
, "--whole-archive")))
2528 if (! bfd_link_add_symbols (member
, &link_info
))
2530 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2535 entry
->loaded
= loaded
;
2541 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2542 entry
->loaded
= TRUE
;
2544 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2546 return entry
->loaded
;
2549 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2550 may be NULL, indicating that it is a wildcard. Separate
2551 lang_input_section statements are created for each part of the
2552 expansion; they are added after the wild statement S. OUTPUT is
2553 the output section. */
2556 wild (lang_wild_statement_type
*s
,
2557 const char *target ATTRIBUTE_UNUSED
,
2558 lang_output_section_statement_type
*output
)
2560 struct wildcard_list
*sec
;
2562 if (s
->handler_data
[0]
2563 && s
->handler_data
[0]->spec
.sorted
== by_name
2564 && !s
->filenames_sorted
)
2566 lang_section_bst_type
*tree
;
2568 walk_wild (s
, output_section_callback_fast
, output
);
2573 output_section_callback_tree_to_list (s
, tree
, output
);
2578 walk_wild (s
, output_section_callback
, output
);
2580 if (default_common_section
== NULL
)
2581 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2582 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2584 /* Remember the section that common is going to in case we
2585 later get something which doesn't know where to put it. */
2586 default_common_section
= output
;
2591 /* Return TRUE iff target is the sought target. */
2594 get_target (const bfd_target
*target
, void *data
)
2596 const char *sought
= data
;
2598 return strcmp (target
->name
, sought
) == 0;
2601 /* Like strcpy() but convert to lower case as well. */
2604 stricpy (char *dest
, char *src
)
2608 while ((c
= *src
++) != 0)
2609 *dest
++ = TOLOWER (c
);
2614 /* Remove the first occurrence of needle (if any) in haystack
2618 strcut (char *haystack
, char *needle
)
2620 haystack
= strstr (haystack
, needle
);
2626 for (src
= haystack
+ strlen (needle
); *src
;)
2627 *haystack
++ = *src
++;
2633 /* Compare two target format name strings.
2634 Return a value indicating how "similar" they are. */
2637 name_compare (char *first
, char *second
)
2643 copy1
= xmalloc (strlen (first
) + 1);
2644 copy2
= xmalloc (strlen (second
) + 1);
2646 /* Convert the names to lower case. */
2647 stricpy (copy1
, first
);
2648 stricpy (copy2
, second
);
2650 /* Remove size and endian strings from the name. */
2651 strcut (copy1
, "big");
2652 strcut (copy1
, "little");
2653 strcut (copy2
, "big");
2654 strcut (copy2
, "little");
2656 /* Return a value based on how many characters match,
2657 starting from the beginning. If both strings are
2658 the same then return 10 * their length. */
2659 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2660 if (copy1
[result
] == 0)
2672 /* Set by closest_target_match() below. */
2673 static const bfd_target
*winner
;
2675 /* Scan all the valid bfd targets looking for one that has the endianness
2676 requirement that was specified on the command line, and is the nearest
2677 match to the original output target. */
2680 closest_target_match (const bfd_target
*target
, void *data
)
2682 const bfd_target
*original
= data
;
2684 if (command_line
.endian
== ENDIAN_BIG
2685 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2688 if (command_line
.endian
== ENDIAN_LITTLE
2689 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2692 /* Must be the same flavour. */
2693 if (target
->flavour
!= original
->flavour
)
2696 /* If we have not found a potential winner yet, then record this one. */
2703 /* Oh dear, we now have two potential candidates for a successful match.
2704 Compare their names and choose the better one. */
2705 if (name_compare (target
->name
, original
->name
)
2706 > name_compare (winner
->name
, original
->name
))
2709 /* Keep on searching until wqe have checked them all. */
2713 /* Return the BFD target format of the first input file. */
2716 get_first_input_target (void)
2718 char *target
= NULL
;
2720 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2722 if (s
->header
.type
== lang_input_statement_enum
2725 ldfile_open_file (s
);
2727 if (s
->the_bfd
!= NULL
2728 && bfd_check_format (s
->the_bfd
, bfd_object
))
2730 target
= bfd_get_target (s
->the_bfd
);
2742 lang_get_output_target (void)
2746 /* Has the user told us which output format to use? */
2747 if (output_target
!= NULL
)
2748 return output_target
;
2750 /* No - has the current target been set to something other than
2752 if (current_target
!= default_target
)
2753 return current_target
;
2755 /* No - can we determine the format of the first input file? */
2756 target
= get_first_input_target ();
2760 /* Failed - use the default output target. */
2761 return default_target
;
2764 /* Open the output file. */
2767 open_output (const char *name
)
2771 output_target
= lang_get_output_target ();
2773 /* Has the user requested a particular endianness on the command
2775 if (command_line
.endian
!= ENDIAN_UNSET
)
2777 const bfd_target
*target
;
2778 enum bfd_endian desired_endian
;
2780 /* Get the chosen target. */
2781 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2783 /* If the target is not supported, we cannot do anything. */
2786 if (command_line
.endian
== ENDIAN_BIG
)
2787 desired_endian
= BFD_ENDIAN_BIG
;
2789 desired_endian
= BFD_ENDIAN_LITTLE
;
2791 /* See if the target has the wrong endianness. This should
2792 not happen if the linker script has provided big and
2793 little endian alternatives, but some scrips don't do
2795 if (target
->byteorder
!= desired_endian
)
2797 /* If it does, then see if the target provides
2798 an alternative with the correct endianness. */
2799 if (target
->alternative_target
!= NULL
2800 && (target
->alternative_target
->byteorder
== desired_endian
))
2801 output_target
= target
->alternative_target
->name
;
2804 /* Try to find a target as similar as possible to
2805 the default target, but which has the desired
2806 endian characteristic. */
2807 bfd_search_for_target (closest_target_match
,
2810 /* Oh dear - we could not find any targets that
2811 satisfy our requirements. */
2813 einfo (_("%P: warning: could not find any targets"
2814 " that match endianness requirement\n"));
2816 output_target
= winner
->name
;
2822 output
= bfd_openw (name
, output_target
);
2826 if (bfd_get_error () == bfd_error_invalid_target
)
2827 einfo (_("%P%F: target %s not found\n"), output_target
);
2829 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2832 delete_output_file_on_failure
= TRUE
;
2834 if (! bfd_set_format (output
, bfd_object
))
2835 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2836 if (! bfd_set_arch_mach (output
,
2837 ldfile_output_architecture
,
2838 ldfile_output_machine
))
2839 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2841 link_info
.hash
= bfd_link_hash_table_create (output
);
2842 if (link_info
.hash
== NULL
)
2843 einfo (_("%P%F: can not create hash table: %E\n"));
2845 bfd_set_gp_size (output
, g_switch_value
);
2850 ldlang_open_output (lang_statement_union_type
*statement
)
2852 switch (statement
->header
.type
)
2854 case lang_output_statement_enum
:
2855 ASSERT (output_bfd
== NULL
);
2856 output_bfd
= open_output (statement
->output_statement
.name
);
2857 ldemul_set_output_arch ();
2858 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2859 output_bfd
->flags
|= D_PAGED
;
2861 output_bfd
->flags
&= ~D_PAGED
;
2862 if (config
.text_read_only
)
2863 output_bfd
->flags
|= WP_TEXT
;
2865 output_bfd
->flags
&= ~WP_TEXT
;
2866 if (link_info
.traditional_format
)
2867 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2869 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2872 case lang_target_statement_enum
:
2873 current_target
= statement
->target_statement
.target
;
2880 /* Convert between addresses in bytes and sizes in octets.
2881 For currently supported targets, octets_per_byte is always a power
2882 of two, so we can use shifts. */
2883 #define TO_ADDR(X) ((X) >> opb_shift)
2884 #define TO_SIZE(X) ((X) << opb_shift)
2886 /* Support the above. */
2887 static unsigned int opb_shift
= 0;
2892 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2893 ldfile_output_machine
);
2896 while ((x
& 1) == 0)
2904 /* Open all the input files. */
2907 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2909 for (; s
!= NULL
; s
= s
->header
.next
)
2911 switch (s
->header
.type
)
2913 case lang_constructors_statement_enum
:
2914 open_input_bfds (constructor_list
.head
, force
);
2916 case lang_output_section_statement_enum
:
2917 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2919 case lang_wild_statement_enum
:
2920 /* Maybe we should load the file's symbols. */
2921 if (s
->wild_statement
.filename
2922 && ! wildcardp (s
->wild_statement
.filename
))
2923 lookup_name (s
->wild_statement
.filename
);
2924 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2926 case lang_group_statement_enum
:
2928 struct bfd_link_hash_entry
*undefs
;
2930 /* We must continually search the entries in the group
2931 until no new symbols are added to the list of undefined
2936 undefs
= link_info
.hash
->undefs_tail
;
2937 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2939 while (undefs
!= link_info
.hash
->undefs_tail
);
2942 case lang_target_statement_enum
:
2943 current_target
= s
->target_statement
.target
;
2945 case lang_input_statement_enum
:
2946 if (s
->input_statement
.real
)
2948 lang_statement_list_type add
;
2950 s
->input_statement
.target
= current_target
;
2952 /* If we are being called from within a group, and this
2953 is an archive which has already been searched, then
2954 force it to be researched unless the whole archive
2955 has been loaded already. */
2957 && !s
->input_statement
.whole_archive
2958 && s
->input_statement
.loaded
2959 && bfd_check_format (s
->input_statement
.the_bfd
,
2961 s
->input_statement
.loaded
= FALSE
;
2963 lang_list_init (&add
);
2965 if (! load_symbols (&s
->input_statement
, &add
))
2966 config
.make_executable
= FALSE
;
2968 if (add
.head
!= NULL
)
2970 *add
.tail
= s
->header
.next
;
2971 s
->header
.next
= add
.head
;
2981 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2984 lang_track_definedness (const char *name
)
2986 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
2987 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
2990 /* New-function for the definedness hash table. */
2992 static struct bfd_hash_entry
*
2993 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
2994 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
2995 const char *name ATTRIBUTE_UNUSED
)
2997 struct lang_definedness_hash_entry
*ret
2998 = (struct lang_definedness_hash_entry
*) entry
;
3001 ret
= (struct lang_definedness_hash_entry
*)
3002 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
3005 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
3007 ret
->iteration
= -1;
3011 /* Return the iteration when the definition of NAME was last updated. A
3012 value of -1 means that the symbol is not defined in the linker script
3013 or the command line, but may be defined in the linker symbol table. */
3016 lang_symbol_definition_iteration (const char *name
)
3018 struct lang_definedness_hash_entry
*defentry
3019 = (struct lang_definedness_hash_entry
*)
3020 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3022 /* We've already created this one on the presence of DEFINED in the
3023 script, so it can't be NULL unless something is borked elsewhere in
3025 if (defentry
== NULL
)
3028 return defentry
->iteration
;
3031 /* Update the definedness state of NAME. */
3034 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
3036 struct lang_definedness_hash_entry
*defentry
3037 = (struct lang_definedness_hash_entry
*)
3038 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3040 /* We don't keep track of symbols not tested with DEFINED. */
3041 if (defentry
== NULL
)
3044 /* If the symbol was already defined, and not from an earlier statement
3045 iteration, don't update the definedness iteration, because that'd
3046 make the symbol seem defined in the linker script at this point, and
3047 it wasn't; it was defined in some object. If we do anyway, DEFINED
3048 would start to yield false before this point and the construct "sym =
3049 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3051 if (h
->type
!= bfd_link_hash_undefined
3052 && h
->type
!= bfd_link_hash_common
3053 && h
->type
!= bfd_link_hash_new
3054 && defentry
->iteration
== -1)
3057 defentry
->iteration
= lang_statement_iteration
;
3060 /* Add the supplied name to the symbol table as an undefined reference.
3061 This is a two step process as the symbol table doesn't even exist at
3062 the time the ld command line is processed. First we put the name
3063 on a list, then, once the output file has been opened, transfer the
3064 name to the symbol table. */
3066 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3068 #define ldlang_undef_chain_list_head entry_symbol.next
3071 ldlang_add_undef (const char *const name
)
3073 ldlang_undef_chain_list_type
*new =
3074 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
3076 new->next
= ldlang_undef_chain_list_head
;
3077 ldlang_undef_chain_list_head
= new;
3079 new->name
= xstrdup (name
);
3081 if (output_bfd
!= NULL
)
3082 insert_undefined (new->name
);
3085 /* Insert NAME as undefined in the symbol table. */
3088 insert_undefined (const char *name
)
3090 struct bfd_link_hash_entry
*h
;
3092 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3094 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3095 if (h
->type
== bfd_link_hash_new
)
3097 h
->type
= bfd_link_hash_undefined
;
3098 h
->u
.undef
.abfd
= NULL
;
3099 bfd_link_add_undef (link_info
.hash
, h
);
3103 /* Run through the list of undefineds created above and place them
3104 into the linker hash table as undefined symbols belonging to the
3108 lang_place_undefineds (void)
3110 ldlang_undef_chain_list_type
*ptr
;
3112 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3113 insert_undefined (ptr
->name
);
3116 /* Check for all readonly or some readwrite sections. */
3119 check_input_sections
3120 (lang_statement_union_type
*s
,
3121 lang_output_section_statement_type
*output_section_statement
)
3123 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3125 switch (s
->header
.type
)
3127 case lang_wild_statement_enum
:
3128 walk_wild (&s
->wild_statement
, check_section_callback
,
3129 output_section_statement
);
3130 if (! output_section_statement
->all_input_readonly
)
3133 case lang_constructors_statement_enum
:
3134 check_input_sections (constructor_list
.head
,
3135 output_section_statement
);
3136 if (! output_section_statement
->all_input_readonly
)
3139 case lang_group_statement_enum
:
3140 check_input_sections (s
->group_statement
.children
.head
,
3141 output_section_statement
);
3142 if (! output_section_statement
->all_input_readonly
)
3151 /* Update wildcard statements if needed. */
3154 update_wild_statements (lang_statement_union_type
*s
)
3156 struct wildcard_list
*sec
;
3158 switch (sort_section
)
3168 for (; s
!= NULL
; s
= s
->header
.next
)
3170 switch (s
->header
.type
)
3175 case lang_wild_statement_enum
:
3176 sec
= s
->wild_statement
.section_list
;
3177 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3180 switch (sec
->spec
.sorted
)
3183 sec
->spec
.sorted
= sort_section
;
3186 if (sort_section
== by_alignment
)
3187 sec
->spec
.sorted
= by_name_alignment
;
3190 if (sort_section
== by_name
)
3191 sec
->spec
.sorted
= by_alignment_name
;
3199 case lang_constructors_statement_enum
:
3200 update_wild_statements (constructor_list
.head
);
3203 case lang_output_section_statement_enum
:
3204 update_wild_statements
3205 (s
->output_section_statement
.children
.head
);
3208 case lang_group_statement_enum
:
3209 update_wild_statements (s
->group_statement
.children
.head
);
3217 /* Open input files and attach to output sections. */
3220 map_input_to_output_sections
3221 (lang_statement_union_type
*s
, const char *target
,
3222 lang_output_section_statement_type
*os
)
3226 for (; s
!= NULL
; s
= s
->header
.next
)
3228 switch (s
->header
.type
)
3230 case lang_wild_statement_enum
:
3231 wild (&s
->wild_statement
, target
, os
);
3233 case lang_constructors_statement_enum
:
3234 map_input_to_output_sections (constructor_list
.head
,
3238 case lang_output_section_statement_enum
:
3239 if (s
->output_section_statement
.constraint
)
3241 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3242 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3244 s
->output_section_statement
.all_input_readonly
= TRUE
;
3245 check_input_sections (s
->output_section_statement
.children
.head
,
3246 &s
->output_section_statement
);
3247 if ((s
->output_section_statement
.all_input_readonly
3248 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3249 || (!s
->output_section_statement
.all_input_readonly
3250 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3252 s
->output_section_statement
.constraint
= -1;
3257 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3259 &s
->output_section_statement
);
3261 case lang_output_statement_enum
:
3263 case lang_target_statement_enum
:
3264 target
= s
->target_statement
.target
;
3266 case lang_group_statement_enum
:
3267 map_input_to_output_sections (s
->group_statement
.children
.head
,
3271 case lang_data_statement_enum
:
3272 /* Make sure that any sections mentioned in the expression
3274 exp_init_os (s
->data_statement
.exp
);
3275 flags
= SEC_HAS_CONTENTS
;
3276 /* The output section gets contents, and then we inspect for
3277 any flags set in the input script which override any ALLOC. */
3278 if (!(os
->flags
& SEC_NEVER_LOAD
))
3279 flags
|= SEC_ALLOC
| SEC_LOAD
;
3280 if (os
->bfd_section
== NULL
)
3281 init_os (os
, NULL
, flags
);
3283 os
->bfd_section
->flags
|= flags
;
3285 case lang_input_section_enum
:
3287 case lang_fill_statement_enum
:
3288 case lang_object_symbols_statement_enum
:
3289 case lang_reloc_statement_enum
:
3290 case lang_padding_statement_enum
:
3291 case lang_input_statement_enum
:
3292 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3293 init_os (os
, NULL
, 0);
3295 case lang_assignment_statement_enum
:
3296 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3297 init_os (os
, NULL
, 0);
3299 /* Make sure that any sections mentioned in the assignment
3301 exp_init_os (s
->assignment_statement
.exp
);
3303 case lang_address_statement_enum
:
3304 /* Mark the specified section with the supplied address.
3306 If this section was actually a segment marker, then the
3307 directive is ignored if the linker script explicitly
3308 processed the segment marker. Originally, the linker
3309 treated segment directives (like -Ttext on the
3310 command-line) as section directives. We honor the
3311 section directive semantics for backwards compatibilty;
3312 linker scripts that do not specifically check for
3313 SEGMENT_START automatically get the old semantics. */
3314 if (!s
->address_statement
.segment
3315 || !s
->address_statement
.segment
->used
)
3317 lang_output_section_statement_type
*aos
3318 = (lang_output_section_statement_lookup
3319 (s
->address_statement
.section_name
));
3321 if (aos
->bfd_section
== NULL
)
3322 init_os (aos
, NULL
, 0);
3323 aos
->addr_tree
= s
->address_statement
.address
;
3326 case lang_insert_statement_enum
:
3332 /* An insert statement snips out all the linker statements from the
3333 start of the list and places them after the output section
3334 statement specified by the insert. This operation is complicated
3335 by the fact that we keep a doubly linked list of output section
3336 statements as well as the singly linked list of all statements. */
3339 process_insert_statements (void)
3341 lang_statement_union_type
**s
;
3342 lang_output_section_statement_type
*first_os
= NULL
;
3343 lang_output_section_statement_type
*last_os
= NULL
;
3345 /* "start of list" is actually the statement immediately after
3346 the special abs_section output statement, so that it isn't
3348 s
= &lang_output_section_statement
.head
;
3349 while (*(s
= &(*s
)->header
.next
) != NULL
)
3351 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
3353 /* Keep pointers to the first and last output section
3354 statement in the sequence we may be about to move. */
3355 last_os
= &(*s
)->output_section_statement
;
3356 if (first_os
== NULL
)
3359 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
3361 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
3362 lang_output_section_statement_type
*where
;
3363 lang_output_section_statement_type
*os
;
3364 lang_statement_union_type
**ptr
;
3365 lang_statement_union_type
*first
;
3367 where
= lang_output_section_find (i
->where
);
3368 if (where
!= NULL
&& i
->is_before
)
3371 where
= where
->prev
;
3372 while (where
!= NULL
&& where
->constraint
== -1);
3376 einfo (_("%X%P: %s not found for insert\n"), i
->where
);
3379 /* You can't insert into the list you are moving. */
3380 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3381 if (os
== where
|| os
== last_os
)
3385 einfo (_("%X%P: %s not found for insert\n"), i
->where
);
3389 /* Deal with reordering the output section statement list. */
3390 if (last_os
!= NULL
)
3392 asection
*first_sec
, *last_sec
;
3393 struct lang_output_section_statement_struct
**next
;
3395 /* Snip out the output sections we are moving. */
3396 first_os
->prev
->next
= last_os
->next
;
3397 if (last_os
->next
== NULL
)
3399 next
= &first_os
->prev
->next
;
3400 lang_output_section_statement
.tail
3401 = (lang_statement_union_type
**) next
;
3404 last_os
->next
->prev
= first_os
->prev
;
3405 /* Add them in at the new position. */
3406 last_os
->next
= where
->next
;
3407 if (where
->next
== NULL
)
3409 next
= &last_os
->next
;
3410 lang_output_section_statement
.tail
3411 = (lang_statement_union_type
**) next
;
3414 where
->next
->prev
= last_os
;
3415 first_os
->prev
= where
;
3416 where
->next
= first_os
;
3418 /* Move the bfd sections in the same way. */
3421 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3423 if (os
->bfd_section
!= NULL
3424 && os
->bfd_section
->owner
!= NULL
)
3426 last_sec
= os
->bfd_section
;
3427 if (first_sec
== NULL
)
3428 first_sec
= last_sec
;
3433 if (last_sec
!= NULL
)
3435 asection
*sec
= where
->bfd_section
;
3437 sec
= output_prev_sec_find (where
);
3439 /* The place we want to insert must come after the
3440 sections we are moving. So if we find no
3441 section or if the section is the same as our
3442 last section, then no move is needed. */
3443 if (sec
!= NULL
&& sec
!= last_sec
)
3445 /* Trim them off. */
3446 if (first_sec
->prev
!= NULL
)
3447 first_sec
->prev
->next
= last_sec
->next
;
3449 output_bfd
->sections
= last_sec
->next
;
3450 if (last_sec
->next
!= NULL
)
3451 last_sec
->next
->prev
= first_sec
->prev
;
3453 output_bfd
->section_last
= first_sec
->prev
;
3455 last_sec
->next
= sec
->next
;
3456 if (sec
->next
!= NULL
)
3457 sec
->next
->prev
= last_sec
;
3459 output_bfd
->section_last
= last_sec
;
3460 first_sec
->prev
= sec
;
3461 sec
->next
= first_sec
;
3469 ptr
= insert_os_after (where
);
3470 /* Snip everything after the abs_section output statement we
3471 know is at the start of the list, up to and including
3472 the insert statement we are currently processing. */
3473 first
= lang_output_section_statement
.head
->header
.next
;
3474 lang_output_section_statement
.head
->header
.next
= (*s
)->header
.next
;
3475 /* Add them back where they belong. */
3478 statement_list
.tail
= s
;
3480 s
= &lang_output_section_statement
.head
;
3485 /* An output section might have been removed after its statement was
3486 added. For example, ldemul_before_allocation can remove dynamic
3487 sections if they turn out to be not needed. Clean them up here. */
3490 strip_excluded_output_sections (void)
3492 lang_output_section_statement_type
*os
;
3494 /* Run lang_size_sections (if not already done). */
3495 if (expld
.phase
!= lang_mark_phase_enum
)
3497 expld
.phase
= lang_mark_phase_enum
;
3498 expld
.dataseg
.phase
= exp_dataseg_none
;
3499 one_lang_size_sections_pass (NULL
, FALSE
);
3500 lang_reset_memory_regions ();
3503 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3507 asection
*output_section
;
3508 bfd_boolean exclude
;
3510 if (os
->constraint
== -1)
3513 output_section
= os
->bfd_section
;
3514 if (output_section
== NULL
)
3517 exclude
= (output_section
->rawsize
== 0
3518 && (output_section
->flags
& SEC_KEEP
) == 0
3519 && !bfd_section_removed_from_list (output_bfd
,
3522 /* Some sections have not yet been sized, notably .gnu.version,
3523 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3524 input sections, so don't drop output sections that have such
3525 input sections unless they are also marked SEC_EXCLUDE. */
3526 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3530 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3531 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3532 && (s
->flags
& SEC_EXCLUDE
) == 0)
3539 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3540 output_section
->map_head
.link_order
= NULL
;
3541 output_section
->map_tail
.link_order
= NULL
;
3545 /* We don't set bfd_section to NULL since bfd_section of the
3546 removed output section statement may still be used. */
3547 if (!os
->section_relative_symbol
3548 && !os
->update_dot_tree
)
3550 output_section
->flags
|= SEC_EXCLUDE
;
3551 bfd_section_list_remove (output_bfd
, output_section
);
3552 output_bfd
->section_count
--;
3556 /* Stop future calls to lang_add_section from messing with map_head
3557 and map_tail link_order fields. */
3558 stripped_excluded_sections
= TRUE
;
3562 print_output_section_statement
3563 (lang_output_section_statement_type
*output_section_statement
)
3565 asection
*section
= output_section_statement
->bfd_section
;
3568 if (output_section_statement
!= abs_output_section
)
3570 minfo ("\n%s", output_section_statement
->name
);
3572 if (section
!= NULL
)
3574 print_dot
= section
->vma
;
3576 len
= strlen (output_section_statement
->name
);
3577 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3582 while (len
< SECTION_NAME_MAP_LENGTH
)
3588 minfo ("0x%V %W", section
->vma
, section
->size
);
3590 if (section
->vma
!= section
->lma
)
3591 minfo (_(" load address 0x%V"), section
->lma
);
3597 print_statement_list (output_section_statement
->children
.head
,
3598 output_section_statement
);
3601 /* Scan for the use of the destination in the right hand side
3602 of an expression. In such cases we will not compute the
3603 correct expression, since the value of DST that is used on
3604 the right hand side will be its final value, not its value
3605 just before this expression is evaluated. */
3608 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3610 if (rhs
== NULL
|| dst
== NULL
)
3613 switch (rhs
->type
.node_class
)
3616 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3617 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3620 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3621 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3624 case etree_provided
:
3626 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3628 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3631 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3635 return strcmp (dst
, rhs
->value
.str
) == 0;
3640 return strcmp (dst
, rhs
->name
.name
) == 0;
3652 print_assignment (lang_assignment_statement_type
*assignment
,
3653 lang_output_section_statement_type
*output_section
)
3657 bfd_boolean computation_is_valid
= TRUE
;
3660 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3663 if (assignment
->exp
->type
.node_class
== etree_assert
)
3666 tree
= assignment
->exp
->assert_s
.child
;
3667 computation_is_valid
= TRUE
;
3671 const char *dst
= assignment
->exp
->assign
.dst
;
3673 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3674 tree
= assignment
->exp
->assign
.src
;
3675 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3678 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3679 if (expld
.result
.valid_p
)
3683 if (computation_is_valid
)
3685 value
= expld
.result
.value
;
3687 if (expld
.result
.section
)
3688 value
+= expld
.result
.section
->vma
;
3690 minfo ("0x%V", value
);
3696 struct bfd_link_hash_entry
*h
;
3698 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3699 FALSE
, FALSE
, TRUE
);
3702 value
= h
->u
.def
.value
;
3704 if (expld
.result
.section
)
3705 value
+= expld
.result
.section
->vma
;
3707 minfo ("[0x%V]", value
);
3710 minfo ("[unresolved]");
3722 exp_print_tree (assignment
->exp
);
3727 print_input_statement (lang_input_statement_type
*statm
)
3729 if (statm
->filename
!= NULL
)
3731 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3735 /* Print all symbols defined in a particular section. This is called
3736 via bfd_link_hash_traverse, or by print_all_symbols. */
3739 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3741 asection
*sec
= ptr
;
3743 if ((hash_entry
->type
== bfd_link_hash_defined
3744 || hash_entry
->type
== bfd_link_hash_defweak
)
3745 && sec
== hash_entry
->u
.def
.section
)
3749 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3752 (hash_entry
->u
.def
.value
3753 + hash_entry
->u
.def
.section
->output_offset
3754 + hash_entry
->u
.def
.section
->output_section
->vma
));
3756 minfo (" %T\n", hash_entry
->root
.string
);
3763 print_all_symbols (asection
*sec
)
3765 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3766 struct map_symbol_def
*def
;
3771 *ud
->map_symbol_def_tail
= 0;
3772 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3773 print_one_symbol (def
->entry
, sec
);
3776 /* Print information about an input section to the map file. */
3779 print_input_section (asection
*i
)
3781 bfd_size_type size
= i
->size
;
3788 minfo ("%s", i
->name
);
3790 len
= 1 + strlen (i
->name
);
3791 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3796 while (len
< SECTION_NAME_MAP_LENGTH
)
3802 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3803 addr
= i
->output_section
->vma
+ i
->output_offset
;
3810 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3812 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3814 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3826 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3829 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3831 if (link_info
.reduce_memory_overheads
)
3832 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3834 print_all_symbols (i
);
3836 print_dot
= addr
+ TO_ADDR (size
);
3841 print_fill_statement (lang_fill_statement_type
*fill
)
3845 fputs (" FILL mask 0x", config
.map_file
);
3846 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3847 fprintf (config
.map_file
, "%02x", *p
);
3848 fputs ("\n", config
.map_file
);
3852 print_data_statement (lang_data_statement_type
*data
)
3860 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3863 addr
= data
->output_offset
;
3864 if (data
->output_section
!= NULL
)
3865 addr
+= data
->output_section
->vma
;
3893 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3895 if (data
->exp
->type
.node_class
!= etree_value
)
3898 exp_print_tree (data
->exp
);
3903 print_dot
= addr
+ TO_ADDR (size
);
3906 /* Print an address statement. These are generated by options like
3910 print_address_statement (lang_address_statement_type
*address
)
3912 minfo (_("Address of section %s set to "), address
->section_name
);
3913 exp_print_tree (address
->address
);
3917 /* Print a reloc statement. */
3920 print_reloc_statement (lang_reloc_statement_type
*reloc
)
3927 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3930 addr
= reloc
->output_offset
;
3931 if (reloc
->output_section
!= NULL
)
3932 addr
+= reloc
->output_section
->vma
;
3934 size
= bfd_get_reloc_size (reloc
->howto
);
3936 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
3938 if (reloc
->name
!= NULL
)
3939 minfo ("%s+", reloc
->name
);
3941 minfo ("%s+", reloc
->section
->name
);
3943 exp_print_tree (reloc
->addend_exp
);
3947 print_dot
= addr
+ TO_ADDR (size
);
3951 print_padding_statement (lang_padding_statement_type
*s
)
3959 len
= sizeof " *fill*" - 1;
3960 while (len
< SECTION_NAME_MAP_LENGTH
)
3966 addr
= s
->output_offset
;
3967 if (s
->output_section
!= NULL
)
3968 addr
+= s
->output_section
->vma
;
3969 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
3971 if (s
->fill
->size
!= 0)
3975 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
3976 fprintf (config
.map_file
, "%02x", *p
);
3981 print_dot
= addr
+ TO_ADDR (s
->size
);
3985 print_wild_statement (lang_wild_statement_type
*w
,
3986 lang_output_section_statement_type
*os
)
3988 struct wildcard_list
*sec
;
3992 if (w
->filenames_sorted
)
3994 if (w
->filename
!= NULL
)
3995 minfo ("%s", w
->filename
);
3998 if (w
->filenames_sorted
)
4002 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4004 if (sec
->spec
.sorted
)
4006 if (sec
->spec
.exclude_name_list
!= NULL
)
4009 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4010 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4011 minfo (" %s", tmp
->name
);
4014 if (sec
->spec
.name
!= NULL
)
4015 minfo ("%s", sec
->spec
.name
);
4018 if (sec
->spec
.sorted
)
4027 print_statement_list (w
->children
.head
, os
);
4030 /* Print a group statement. */
4033 print_group (lang_group_statement_type
*s
,
4034 lang_output_section_statement_type
*os
)
4036 fprintf (config
.map_file
, "START GROUP\n");
4037 print_statement_list (s
->children
.head
, os
);
4038 fprintf (config
.map_file
, "END GROUP\n");
4041 /* Print the list of statements in S.
4042 This can be called for any statement type. */
4045 print_statement_list (lang_statement_union_type
*s
,
4046 lang_output_section_statement_type
*os
)
4050 print_statement (s
, os
);
4055 /* Print the first statement in statement list S.
4056 This can be called for any statement type. */
4059 print_statement (lang_statement_union_type
*s
,
4060 lang_output_section_statement_type
*os
)
4062 switch (s
->header
.type
)
4065 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
4068 case lang_constructors_statement_enum
:
4069 if (constructor_list
.head
!= NULL
)
4071 if (constructors_sorted
)
4072 minfo (" SORT (CONSTRUCTORS)\n");
4074 minfo (" CONSTRUCTORS\n");
4075 print_statement_list (constructor_list
.head
, os
);
4078 case lang_wild_statement_enum
:
4079 print_wild_statement (&s
->wild_statement
, os
);
4081 case lang_address_statement_enum
:
4082 print_address_statement (&s
->address_statement
);
4084 case lang_object_symbols_statement_enum
:
4085 minfo (" CREATE_OBJECT_SYMBOLS\n");
4087 case lang_fill_statement_enum
:
4088 print_fill_statement (&s
->fill_statement
);
4090 case lang_data_statement_enum
:
4091 print_data_statement (&s
->data_statement
);
4093 case lang_reloc_statement_enum
:
4094 print_reloc_statement (&s
->reloc_statement
);
4096 case lang_input_section_enum
:
4097 print_input_section (s
->input_section
.section
);
4099 case lang_padding_statement_enum
:
4100 print_padding_statement (&s
->padding_statement
);
4102 case lang_output_section_statement_enum
:
4103 print_output_section_statement (&s
->output_section_statement
);
4105 case lang_assignment_statement_enum
:
4106 print_assignment (&s
->assignment_statement
, os
);
4108 case lang_target_statement_enum
:
4109 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
4111 case lang_output_statement_enum
:
4112 minfo ("OUTPUT(%s", s
->output_statement
.name
);
4113 if (output_target
!= NULL
)
4114 minfo (" %s", output_target
);
4117 case lang_input_statement_enum
:
4118 print_input_statement (&s
->input_statement
);
4120 case lang_group_statement_enum
:
4121 print_group (&s
->group_statement
, os
);
4123 case lang_insert_statement_enum
:
4124 minfo ("INSERT %s %s\n",
4125 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
4126 s
->insert_statement
.where
);
4132 print_statements (void)
4134 print_statement_list (statement_list
.head
, abs_output_section
);
4137 /* Print the first N statements in statement list S to STDERR.
4138 If N == 0, nothing is printed.
4139 If N < 0, the entire list is printed.
4140 Intended to be called from GDB. */
4143 dprint_statement (lang_statement_union_type
*s
, int n
)
4145 FILE *map_save
= config
.map_file
;
4147 config
.map_file
= stderr
;
4150 print_statement_list (s
, abs_output_section
);
4153 while (s
&& --n
>= 0)
4155 print_statement (s
, abs_output_section
);
4160 config
.map_file
= map_save
;
4164 insert_pad (lang_statement_union_type
**ptr
,
4166 unsigned int alignment_needed
,
4167 asection
*output_section
,
4170 static fill_type zero_fill
= { 1, { 0 } };
4171 lang_statement_union_type
*pad
= NULL
;
4173 if (ptr
!= &statement_list
.head
)
4174 pad
= ((lang_statement_union_type
*)
4175 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4177 && pad
->header
.type
== lang_padding_statement_enum
4178 && pad
->padding_statement
.output_section
== output_section
)
4180 /* Use the existing pad statement. */
4182 else if ((pad
= *ptr
) != NULL
4183 && pad
->header
.type
== lang_padding_statement_enum
4184 && pad
->padding_statement
.output_section
== output_section
)
4186 /* Use the existing pad statement. */
4190 /* Make a new padding statement, linked into existing chain. */
4191 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4192 pad
->header
.next
= *ptr
;
4194 pad
->header
.type
= lang_padding_statement_enum
;
4195 pad
->padding_statement
.output_section
= output_section
;
4198 pad
->padding_statement
.fill
= fill
;
4200 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4201 pad
->padding_statement
.size
= alignment_needed
;
4202 output_section
->size
+= alignment_needed
;
4205 /* Work out how much this section will move the dot point. */
4209 (lang_statement_union_type
**this_ptr
,
4210 lang_output_section_statement_type
*output_section_statement
,
4214 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4215 asection
*i
= is
->section
;
4217 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
4218 && (i
->flags
& SEC_EXCLUDE
) == 0)
4220 unsigned int alignment_needed
;
4223 /* Align this section first to the input sections requirement,
4224 then to the output section's requirement. If this alignment
4225 is greater than any seen before, then record it too. Perform
4226 the alignment by inserting a magic 'padding' statement. */
4228 if (output_section_statement
->subsection_alignment
!= -1)
4229 i
->alignment_power
= output_section_statement
->subsection_alignment
;
4231 o
= output_section_statement
->bfd_section
;
4232 if (o
->alignment_power
< i
->alignment_power
)
4233 o
->alignment_power
= i
->alignment_power
;
4235 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4237 if (alignment_needed
!= 0)
4239 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4240 dot
+= alignment_needed
;
4243 /* Remember where in the output section this input section goes. */
4245 i
->output_offset
= dot
- o
->vma
;
4247 /* Mark how big the output section must be to contain this now. */
4248 dot
+= TO_ADDR (i
->size
);
4249 o
->size
= TO_SIZE (dot
- o
->vma
);
4253 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
4260 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4262 const asection
*sec1
= *(const asection
**) arg1
;
4263 const asection
*sec2
= *(const asection
**) arg2
;
4265 if (bfd_section_lma (sec1
->owner
, sec1
)
4266 < bfd_section_lma (sec2
->owner
, sec2
))
4268 else if (bfd_section_lma (sec1
->owner
, sec1
)
4269 > bfd_section_lma (sec2
->owner
, sec2
))
4271 else if (sec1
->id
< sec2
->id
)
4273 else if (sec1
->id
> sec2
->id
)
4279 #define IGNORE_SECTION(s) \
4280 ((s->flags & SEC_NEVER_LOAD) != 0 \
4281 || (s->flags & SEC_ALLOC) == 0 \
4282 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4283 && (s->flags & SEC_LOAD) == 0))
4285 /* Check to see if any allocated sections overlap with other allocated
4286 sections. This can happen if a linker script specifies the output
4287 section addresses of the two sections. Also check whether any memory
4288 region has overflowed. */
4291 lang_check_section_addresses (void)
4294 asection
**sections
, **spp
;
4301 lang_memory_region_type
*m
;
4303 if (bfd_count_sections (output_bfd
) <= 1)
4306 amt
= bfd_count_sections (output_bfd
) * sizeof (asection
*);
4307 sections
= xmalloc (amt
);
4309 /* Scan all sections in the output list. */
4311 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4313 /* Only consider loadable sections with real contents. */
4314 if (IGNORE_SECTION (s
) || s
->size
== 0)
4317 sections
[count
] = s
;
4324 qsort (sections
, (size_t) count
, sizeof (asection
*),
4325 sort_sections_by_lma
);
4329 s_start
= bfd_section_lma (output_bfd
, s
);
4330 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4331 for (count
--; count
; count
--)
4333 /* We must check the sections' LMA addresses not their VMA
4334 addresses because overlay sections can have overlapping VMAs
4335 but they must have distinct LMAs. */
4340 s_start
= bfd_section_lma (output_bfd
, s
);
4341 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4343 /* Look for an overlap. */
4344 if (s_end
>= os_start
&& s_start
<= os_end
)
4345 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4346 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4351 /* If any memory region has overflowed, report by how much.
4352 We do not issue this diagnostic for regions that had sections
4353 explicitly placed outside their bounds; os_region_check's
4354 diagnostics are adequate for that case.
4356 FIXME: It is conceivable that m->current - (m->origin + m->length)
4357 might overflow a 32-bit integer. There is, alas, no way to print
4358 a bfd_vma quantity in decimal. */
4359 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
4360 if (m
->had_full_message
)
4361 einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
4362 m
->name
, (long)(m
->current
- (m
->origin
+ m
->length
)));
4366 /* Make sure the new address is within the region. We explicitly permit the
4367 current address to be at the exact end of the region when the address is
4368 non-zero, in case the region is at the end of addressable memory and the
4369 calculation wraps around. */
4372 os_region_check (lang_output_section_statement_type
*os
,
4373 lang_memory_region_type
*region
,
4377 if ((region
->current
< region
->origin
4378 || (region
->current
- region
->origin
> region
->length
))
4379 && ((region
->current
!= region
->origin
+ region
->length
)
4384 einfo (_("%X%P: address 0x%v of %B section %s"
4385 " is not within region %s\n"),
4387 os
->bfd_section
->owner
,
4388 os
->bfd_section
->name
,
4391 else if (!region
->had_full_message
)
4393 region
->had_full_message
= TRUE
;
4395 einfo (_("%X%P: %B section %s will not fit in region %s\n"),
4396 os
->bfd_section
->owner
,
4397 os
->bfd_section
->name
,
4403 /* Set the sizes for all the output sections. */
4406 lang_size_sections_1
4407 (lang_statement_union_type
*s
,
4408 lang_output_section_statement_type
*output_section_statement
,
4409 lang_statement_union_type
**prev
,
4413 bfd_boolean check_regions
)
4415 /* Size up the sections from their constituent parts. */
4416 for (; s
!= NULL
; s
= s
->header
.next
)
4418 switch (s
->header
.type
)
4420 case lang_output_section_statement_enum
:
4422 bfd_vma newdot
, after
;
4423 lang_output_section_statement_type
*os
;
4424 lang_memory_region_type
*r
;
4426 os
= &s
->output_section_statement
;
4427 if (os
->addr_tree
!= NULL
)
4429 os
->processed_vma
= FALSE
;
4430 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4432 if (expld
.result
.valid_p
)
4433 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4434 else if (expld
.phase
!= lang_mark_phase_enum
)
4435 einfo (_("%F%S: non constant or forward reference"
4436 " address expression for section %s\n"),
4440 if (os
->bfd_section
== NULL
)
4441 /* This section was removed or never actually created. */
4444 /* If this is a COFF shared library section, use the size and
4445 address from the input section. FIXME: This is COFF
4446 specific; it would be cleaner if there were some other way
4447 to do this, but nothing simple comes to mind. */
4448 if ((bfd_get_flavour (output_bfd
) == bfd_target_ecoff_flavour
4449 || bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
4450 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4454 if (os
->children
.head
== NULL
4455 || os
->children
.head
->header
.next
!= NULL
4456 || (os
->children
.head
->header
.type
4457 != lang_input_section_enum
))
4458 einfo (_("%P%X: Internal error on COFF shared library"
4459 " section %s\n"), os
->name
);
4461 input
= os
->children
.head
->input_section
.section
;
4462 bfd_set_section_vma (os
->bfd_section
->owner
,
4464 bfd_section_vma (input
->owner
, input
));
4465 os
->bfd_section
->size
= input
->size
;
4470 if (bfd_is_abs_section (os
->bfd_section
))
4472 /* No matter what happens, an abs section starts at zero. */
4473 ASSERT (os
->bfd_section
->vma
== 0);
4479 if (os
->addr_tree
== NULL
)
4481 /* No address specified for this section, get one
4482 from the region specification. */
4483 if (os
->region
== NULL
4484 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4485 && os
->region
->name
[0] == '*'
4486 && strcmp (os
->region
->name
,
4487 DEFAULT_MEMORY_REGION
) == 0))
4489 os
->region
= lang_memory_default (os
->bfd_section
);
4492 /* If a loadable section is using the default memory
4493 region, and some non default memory regions were
4494 defined, issue an error message. */
4496 && !IGNORE_SECTION (os
->bfd_section
)
4497 && ! link_info
.relocatable
4499 && strcmp (os
->region
->name
,
4500 DEFAULT_MEMORY_REGION
) == 0
4501 && lang_memory_region_list
!= NULL
4502 && (strcmp (lang_memory_region_list
->name
,
4503 DEFAULT_MEMORY_REGION
) != 0
4504 || lang_memory_region_list
->next
!= NULL
)
4505 && expld
.phase
!= lang_mark_phase_enum
)
4507 /* By default this is an error rather than just a
4508 warning because if we allocate the section to the
4509 default memory region we can end up creating an
4510 excessively large binary, or even seg faulting when
4511 attempting to perform a negative seek. See
4512 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4513 for an example of this. This behaviour can be
4514 overridden by the using the --no-check-sections
4516 if (command_line
.check_section_addresses
)
4517 einfo (_("%P%F: error: no memory region specified"
4518 " for loadable section `%s'\n"),
4519 bfd_get_section_name (output_bfd
,
4522 einfo (_("%P: warning: no memory region specified"
4523 " for loadable section `%s'\n"),
4524 bfd_get_section_name (output_bfd
,
4528 newdot
= os
->region
->current
;
4529 align
= os
->bfd_section
->alignment_power
;
4532 align
= os
->section_alignment
;
4534 /* Align to what the section needs. */
4537 bfd_vma savedot
= newdot
;
4538 newdot
= align_power (newdot
, align
);
4540 if (newdot
!= savedot
4541 && (config
.warn_section_align
4542 || os
->addr_tree
!= NULL
)
4543 && expld
.phase
!= lang_mark_phase_enum
)
4544 einfo (_("%P: warning: changing start of section"
4545 " %s by %lu bytes\n"),
4546 os
->name
, (unsigned long) (newdot
- savedot
));
4549 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4551 os
->bfd_section
->output_offset
= 0;
4554 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4555 os
->fill
, newdot
, relax
, check_regions
);
4557 os
->processed_vma
= TRUE
;
4559 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4560 /* Except for some special linker created sections,
4561 no output section should change from zero size
4562 after strip_excluded_output_sections. A non-zero
4563 size on an ignored section indicates that some
4564 input section was not sized early enough. */
4565 ASSERT (os
->bfd_section
->size
== 0);
4568 dot
= os
->bfd_section
->vma
;
4570 /* Put the section within the requested block size, or
4571 align at the block boundary. */
4573 + TO_ADDR (os
->bfd_section
->size
)
4574 + os
->block_value
- 1)
4575 & - (bfd_vma
) os
->block_value
);
4577 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4580 /* Set section lma. */
4583 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
4587 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
4588 os
->bfd_section
->lma
= lma
;
4590 else if (os
->region
!= NULL
4591 && os
->lma_region
!= NULL
4592 && os
->lma_region
!= os
->region
)
4594 bfd_vma lma
= os
->lma_region
->current
;
4596 if (os
->section_alignment
!= -1)
4597 lma
= align_power (lma
, os
->section_alignment
);
4598 os
->bfd_section
->lma
= lma
;
4600 else if (r
->last_os
!= NULL
4601 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
4606 last
= r
->last_os
->output_section_statement
.bfd_section
;
4608 /* A backwards move of dot should be accompanied by
4609 an explicit assignment to the section LMA (ie.
4610 os->load_base set) because backwards moves can
4611 create overlapping LMAs. */
4613 && os
->bfd_section
->size
!= 0
4614 && dot
+ os
->bfd_section
->size
<= last
->vma
)
4616 /* If dot moved backwards then leave lma equal to
4617 vma. This is the old default lma, which might
4618 just happen to work when the backwards move is
4619 sufficiently large. Nag if this changes anything,
4620 so people can fix their linker scripts. */
4622 if (last
->vma
!= last
->lma
)
4623 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4628 /* If this is an overlay, set the current lma to that
4629 at the end of the previous section. */
4630 if (os
->sectype
== overlay_section
)
4631 lma
= last
->lma
+ last
->size
;
4633 /* Otherwise, keep the same lma to vma relationship
4634 as the previous section. */
4636 lma
= dot
+ last
->lma
- last
->vma
;
4638 if (os
->section_alignment
!= -1)
4639 lma
= align_power (lma
, os
->section_alignment
);
4640 os
->bfd_section
->lma
= lma
;
4643 os
->processed_lma
= TRUE
;
4645 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4648 /* Keep track of normal sections using the default
4649 lma region. We use this to set the lma for
4650 following sections. Overlays or other linker
4651 script assignment to lma might mean that the
4652 default lma == vma is incorrect.
4653 To avoid warnings about dot moving backwards when using
4654 -Ttext, don't start tracking sections until we find one
4655 of non-zero size or with lma set differently to vma. */
4656 if (((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4657 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0)
4658 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0
4659 && (os
->bfd_section
->size
!= 0
4660 || (r
->last_os
== NULL
4661 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
4662 || (r
->last_os
!= NULL
4663 && dot
>= (r
->last_os
->output_section_statement
4664 .bfd_section
->vma
)))
4665 && os
->lma_region
== NULL
4666 && !link_info
.relocatable
)
4669 /* .tbss sections effectively have zero size. */
4670 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4671 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4672 || link_info
.relocatable
)
4673 dot
+= TO_ADDR (os
->bfd_section
->size
);
4675 if (os
->update_dot_tree
!= 0)
4676 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4678 /* Update dot in the region ?
4679 We only do this if the section is going to be allocated,
4680 since unallocated sections do not contribute to the region's
4681 overall size in memory.
4683 If the SEC_NEVER_LOAD bit is not set, it will affect the
4684 addresses of sections after it. We have to update
4686 if (os
->region
!= NULL
4687 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4688 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4690 os
->region
->current
= dot
;
4693 /* Make sure the new address is within the region. */
4694 os_region_check (os
, os
->region
, os
->addr_tree
,
4695 os
->bfd_section
->vma
);
4697 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
4699 os
->lma_region
->current
4700 = os
->bfd_section
->lma
+ TO_ADDR (os
->bfd_section
->size
);
4703 os_region_check (os
, os
->lma_region
, NULL
,
4704 os
->bfd_section
->lma
);
4710 case lang_constructors_statement_enum
:
4711 dot
= lang_size_sections_1 (constructor_list
.head
,
4712 output_section_statement
,
4713 &s
->wild_statement
.children
.head
,
4714 fill
, dot
, relax
, check_regions
);
4717 case lang_data_statement_enum
:
4719 unsigned int size
= 0;
4721 s
->data_statement
.output_offset
=
4722 dot
- output_section_statement
->bfd_section
->vma
;
4723 s
->data_statement
.output_section
=
4724 output_section_statement
->bfd_section
;
4726 /* We might refer to provided symbols in the expression, and
4727 need to mark them as needed. */
4728 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4730 switch (s
->data_statement
.type
)
4748 if (size
< TO_SIZE ((unsigned) 1))
4749 size
= TO_SIZE ((unsigned) 1);
4750 dot
+= TO_ADDR (size
);
4751 output_section_statement
->bfd_section
->size
+= size
;
4755 case lang_reloc_statement_enum
:
4759 s
->reloc_statement
.output_offset
=
4760 dot
- output_section_statement
->bfd_section
->vma
;
4761 s
->reloc_statement
.output_section
=
4762 output_section_statement
->bfd_section
;
4763 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4764 dot
+= TO_ADDR (size
);
4765 output_section_statement
->bfd_section
->size
+= size
;
4769 case lang_wild_statement_enum
:
4770 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4771 output_section_statement
,
4772 &s
->wild_statement
.children
.head
,
4773 fill
, dot
, relax
, check_regions
);
4776 case lang_object_symbols_statement_enum
:
4777 link_info
.create_object_symbols_section
=
4778 output_section_statement
->bfd_section
;
4781 case lang_output_statement_enum
:
4782 case lang_target_statement_enum
:
4785 case lang_input_section_enum
:
4789 i
= (*prev
)->input_section
.section
;
4794 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4795 einfo (_("%P%F: can't relax section: %E\n"));
4799 dot
= size_input_section (prev
, output_section_statement
,
4800 output_section_statement
->fill
, dot
);
4804 case lang_input_statement_enum
:
4807 case lang_fill_statement_enum
:
4808 s
->fill_statement
.output_section
=
4809 output_section_statement
->bfd_section
;
4811 fill
= s
->fill_statement
.fill
;
4814 case lang_assignment_statement_enum
:
4816 bfd_vma newdot
= dot
;
4817 etree_type
*tree
= s
->assignment_statement
.exp
;
4819 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
4821 exp_fold_tree (tree
,
4822 output_section_statement
->bfd_section
,
4825 if (expld
.dataseg
.relro
== exp_dataseg_relro_start
)
4827 if (!expld
.dataseg
.relro_start_stat
)
4828 expld
.dataseg
.relro_start_stat
= s
;
4831 ASSERT (expld
.dataseg
.relro_start_stat
== s
);
4834 else if (expld
.dataseg
.relro
== exp_dataseg_relro_end
)
4836 if (!expld
.dataseg
.relro_end_stat
)
4837 expld
.dataseg
.relro_end_stat
= s
;
4840 ASSERT (expld
.dataseg
.relro_end_stat
== s
);
4843 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
4845 /* This symbol is relative to this section. */
4846 if ((tree
->type
.node_class
== etree_provided
4847 || tree
->type
.node_class
== etree_assign
)
4848 && (tree
->assign
.dst
[0] != '.'
4849 || tree
->assign
.dst
[1] != '\0'))
4850 output_section_statement
->section_relative_symbol
= 1;
4852 if (!output_section_statement
->ignored
)
4854 if (output_section_statement
== abs_output_section
)
4856 /* If we don't have an output section, then just adjust
4857 the default memory address. */
4858 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4859 FALSE
)->current
= newdot
;
4861 else if (newdot
!= dot
)
4863 /* Insert a pad after this statement. We can't
4864 put the pad before when relaxing, in case the
4865 assignment references dot. */
4866 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4867 output_section_statement
->bfd_section
, dot
);
4869 /* Don't neuter the pad below when relaxing. */
4872 /* If dot is advanced, this implies that the section
4873 should have space allocated to it, unless the
4874 user has explicitly stated that the section
4875 should never be loaded. */
4876 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
4877 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4884 case lang_padding_statement_enum
:
4885 /* If this is the first time lang_size_sections is called,
4886 we won't have any padding statements. If this is the
4887 second or later passes when relaxing, we should allow
4888 padding to shrink. If padding is needed on this pass, it
4889 will be added back in. */
4890 s
->padding_statement
.size
= 0;
4892 /* Make sure output_offset is valid. If relaxation shrinks
4893 the section and this pad isn't needed, it's possible to
4894 have output_offset larger than the final size of the
4895 section. bfd_set_section_contents will complain even for
4896 a pad size of zero. */
4897 s
->padding_statement
.output_offset
4898 = dot
- output_section_statement
->bfd_section
->vma
;
4901 case lang_group_statement_enum
:
4902 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4903 output_section_statement
,
4904 &s
->group_statement
.children
.head
,
4905 fill
, dot
, relax
, check_regions
);
4908 case lang_insert_statement_enum
:
4911 /* We can only get here when relaxing is turned on. */
4912 case lang_address_statement_enum
:
4919 prev
= &s
->header
.next
;
4924 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
4925 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
4926 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
4927 segments. We are allowed an opportunity to override this decision. */
4930 ldlang_override_segment_assignment (struct bfd_link_info
* info ATTRIBUTE_UNUSED
,
4931 bfd
* abfd ATTRIBUTE_UNUSED
,
4932 asection
* current_section
,
4933 asection
* previous_section
,
4934 bfd_boolean new_segment
)
4936 lang_output_section_statement_type
* cur
;
4937 lang_output_section_statement_type
* prev
;
4939 /* The checks below are only necessary when the BFD library has decided
4940 that the two sections ought to be placed into the same segment. */
4944 /* Paranoia checks. */
4945 if (current_section
== NULL
|| previous_section
== NULL
)
4948 /* Find the memory regions associated with the two sections.
4949 We call lang_output_section_find() here rather than scanning the list
4950 of output sections looking for a matching section pointer because if
4951 we have a large number of sections then a hash lookup is faster. */
4952 cur
= lang_output_section_find (current_section
->name
);
4953 prev
= lang_output_section_find (previous_section
->name
);
4955 /* More paranoia. */
4956 if (cur
== NULL
|| prev
== NULL
)
4959 /* If the regions are different then force the sections to live in
4960 different segments. See the email thread starting at the following
4961 URL for the reasons why this is necessary:
4962 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
4963 return cur
->region
!= prev
->region
;
4967 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
4969 lang_statement_iteration
++;
4970 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
4971 &statement_list
.head
, 0, 0, relax
, check_regions
);
4975 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
4977 expld
.phase
= lang_allocating_phase_enum
;
4978 expld
.dataseg
.phase
= exp_dataseg_none
;
4980 one_lang_size_sections_pass (relax
, check_regions
);
4981 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
4982 && link_info
.relro
&& expld
.dataseg
.relro_end
)
4984 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4985 to put expld.dataseg.relro on a (common) page boundary. */
4986 bfd_vma old_min_base
, relro_end
, maxpage
;
4988 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
4989 old_min_base
= expld
.dataseg
.min_base
;
4990 maxpage
= expld
.dataseg
.maxpagesize
;
4991 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
4992 & (expld
.dataseg
.pagesize
- 1));
4993 /* Compute the expected PT_GNU_RELRO segment end. */
4994 relro_end
= (expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
4995 & ~(expld
.dataseg
.pagesize
- 1);
4996 if (old_min_base
+ maxpage
< expld
.dataseg
.base
)
4998 expld
.dataseg
.base
-= maxpage
;
4999 relro_end
-= maxpage
;
5001 lang_reset_memory_regions ();
5002 one_lang_size_sections_pass (relax
, check_regions
);
5003 if (expld
.dataseg
.relro_end
> relro_end
)
5005 /* The alignment of sections between DATA_SEGMENT_ALIGN
5006 and DATA_SEGMENT_RELRO_END caused huge padding to be
5007 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
5009 unsigned int max_alignment_power
= 0;
5011 /* Find maximum alignment power of sections between
5012 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5013 for (sec
= output_bfd
->sections
; sec
; sec
= sec
->next
)
5014 if (sec
->vma
>= expld
.dataseg
.base
5015 && sec
->vma
< expld
.dataseg
.relro_end
5016 && sec
->alignment_power
> max_alignment_power
)
5017 max_alignment_power
= sec
->alignment_power
;
5019 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
5021 if (expld
.dataseg
.base
- (1 << max_alignment_power
)
5023 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
5024 expld
.dataseg
.base
-= (1 << max_alignment_power
);
5025 lang_reset_memory_regions ();
5026 one_lang_size_sections_pass (relax
, check_regions
);
5029 link_info
.relro_start
= expld
.dataseg
.base
;
5030 link_info
.relro_end
= expld
.dataseg
.relro_end
;
5032 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
5034 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5035 a page could be saved in the data segment. */
5036 bfd_vma first
, last
;
5038 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
5039 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
5041 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
5042 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
5043 && first
+ last
<= expld
.dataseg
.pagesize
)
5045 expld
.dataseg
.phase
= exp_dataseg_adjust
;
5046 lang_reset_memory_regions ();
5047 one_lang_size_sections_pass (relax
, check_regions
);
5051 expld
.phase
= lang_final_phase_enum
;
5054 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5057 lang_do_assignments_1 (lang_statement_union_type
*s
,
5058 lang_output_section_statement_type
*current_os
,
5062 for (; s
!= NULL
; s
= s
->header
.next
)
5064 switch (s
->header
.type
)
5066 case lang_constructors_statement_enum
:
5067 dot
= lang_do_assignments_1 (constructor_list
.head
,
5068 current_os
, fill
, dot
);
5071 case lang_output_section_statement_enum
:
5073 lang_output_section_statement_type
*os
;
5075 os
= &(s
->output_section_statement
);
5076 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
5078 dot
= os
->bfd_section
->vma
;
5080 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
5082 /* .tbss sections effectively have zero size. */
5083 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
5084 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
5085 || link_info
.relocatable
)
5086 dot
+= TO_ADDR (os
->bfd_section
->size
);
5091 case lang_wild_statement_enum
:
5093 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
5094 current_os
, fill
, dot
);
5097 case lang_object_symbols_statement_enum
:
5098 case lang_output_statement_enum
:
5099 case lang_target_statement_enum
:
5102 case lang_data_statement_enum
:
5103 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5104 if (expld
.result
.valid_p
)
5105 s
->data_statement
.value
= (expld
.result
.value
5106 + expld
.result
.section
->vma
);
5108 einfo (_("%F%P: invalid data statement\n"));
5111 switch (s
->data_statement
.type
)
5129 if (size
< TO_SIZE ((unsigned) 1))
5130 size
= TO_SIZE ((unsigned) 1);
5131 dot
+= TO_ADDR (size
);
5135 case lang_reloc_statement_enum
:
5136 exp_fold_tree (s
->reloc_statement
.addend_exp
,
5137 bfd_abs_section_ptr
, &dot
);
5138 if (expld
.result
.valid_p
)
5139 s
->reloc_statement
.addend_value
= expld
.result
.value
;
5141 einfo (_("%F%P: invalid reloc statement\n"));
5142 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
5145 case lang_input_section_enum
:
5147 asection
*in
= s
->input_section
.section
;
5149 if ((in
->flags
& SEC_EXCLUDE
) == 0)
5150 dot
+= TO_ADDR (in
->size
);
5154 case lang_input_statement_enum
:
5157 case lang_fill_statement_enum
:
5158 fill
= s
->fill_statement
.fill
;
5161 case lang_assignment_statement_enum
:
5162 exp_fold_tree (s
->assignment_statement
.exp
,
5163 current_os
->bfd_section
,
5167 case lang_padding_statement_enum
:
5168 dot
+= TO_ADDR (s
->padding_statement
.size
);
5171 case lang_group_statement_enum
:
5172 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
5173 current_os
, fill
, dot
);
5176 case lang_insert_statement_enum
:
5179 case lang_address_statement_enum
:
5191 lang_do_assignments (void)
5193 lang_statement_iteration
++;
5194 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
5197 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5198 operator .startof. (section_name), it produces an undefined symbol
5199 .startof.section_name. Similarly, when it sees
5200 .sizeof. (section_name), it produces an undefined symbol
5201 .sizeof.section_name. For all the output sections, we look for
5202 such symbols, and set them to the correct value. */
5205 lang_set_startof (void)
5209 if (link_info
.relocatable
)
5212 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5214 const char *secname
;
5216 struct bfd_link_hash_entry
*h
;
5218 secname
= bfd_get_section_name (output_bfd
, s
);
5219 buf
= xmalloc (10 + strlen (secname
));
5221 sprintf (buf
, ".startof.%s", secname
);
5222 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5223 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5225 h
->type
= bfd_link_hash_defined
;
5226 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
5227 h
->u
.def
.section
= bfd_abs_section_ptr
;
5230 sprintf (buf
, ".sizeof.%s", secname
);
5231 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5232 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5234 h
->type
= bfd_link_hash_defined
;
5235 h
->u
.def
.value
= TO_ADDR (s
->size
);
5236 h
->u
.def
.section
= bfd_abs_section_ptr
;
5246 struct bfd_link_hash_entry
*h
;
5249 if ((link_info
.relocatable
&& !link_info
.gc_sections
)
5250 || link_info
.shared
)
5251 warn
= entry_from_cmdline
;
5255 /* Force the user to specify a root when generating a relocatable with
5257 if (link_info
.gc_sections
&& link_info
.relocatable
5258 && (entry_symbol
.name
== NULL
5259 && ldlang_undef_chain_list_head
== NULL
))
5260 einfo (_("%P%F: gc-sections requires either an entry or "
5261 "an undefined symbol\n"));
5263 if (entry_symbol
.name
== NULL
)
5265 /* No entry has been specified. Look for the default entry, but
5266 don't warn if we don't find it. */
5267 entry_symbol
.name
= entry_symbol_default
;
5271 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
5272 FALSE
, FALSE
, TRUE
);
5274 && (h
->type
== bfd_link_hash_defined
5275 || h
->type
== bfd_link_hash_defweak
)
5276 && h
->u
.def
.section
->output_section
!= NULL
)
5280 val
= (h
->u
.def
.value
5281 + bfd_get_section_vma (output_bfd
,
5282 h
->u
.def
.section
->output_section
)
5283 + h
->u
.def
.section
->output_offset
);
5284 if (! bfd_set_start_address (output_bfd
, val
))
5285 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
5292 /* We couldn't find the entry symbol. Try parsing it as a
5294 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
5297 if (! bfd_set_start_address (output_bfd
, val
))
5298 einfo (_("%P%F: can't set start address\n"));
5304 /* Can't find the entry symbol, and it's not a number. Use
5305 the first address in the text section. */
5306 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
5310 einfo (_("%P: warning: cannot find entry symbol %s;"
5311 " defaulting to %V\n"),
5313 bfd_get_section_vma (output_bfd
, ts
));
5314 if (! bfd_set_start_address (output_bfd
,
5315 bfd_get_section_vma (output_bfd
,
5317 einfo (_("%P%F: can't set start address\n"));
5322 einfo (_("%P: warning: cannot find entry symbol %s;"
5323 " not setting start address\n"),
5329 /* Don't bfd_hash_table_free (&lang_definedness_table);
5330 map file output may result in a call of lang_track_definedness. */
5333 /* This is a small function used when we want to ignore errors from
5337 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
5339 /* Don't do anything. */
5342 /* Check that the architecture of all the input files is compatible
5343 with the output file. Also call the backend to let it do any
5344 other checking that is needed. */
5349 lang_statement_union_type
*file
;
5351 const bfd_arch_info_type
*compatible
;
5353 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
5355 input_bfd
= file
->input_statement
.the_bfd
;
5357 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
5358 command_line
.accept_unknown_input_arch
);
5360 /* In general it is not possible to perform a relocatable
5361 link between differing object formats when the input
5362 file has relocations, because the relocations in the
5363 input format may not have equivalent representations in
5364 the output format (and besides BFD does not translate
5365 relocs for other link purposes than a final link). */
5366 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
5367 && (compatible
== NULL
5368 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
5369 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
5371 einfo (_("%P%F: Relocatable linking with relocations from"
5372 " format %s (%B) to format %s (%B) is not supported\n"),
5373 bfd_get_target (input_bfd
), input_bfd
,
5374 bfd_get_target (output_bfd
), output_bfd
);
5375 /* einfo with %F exits. */
5378 if (compatible
== NULL
)
5380 if (command_line
.warn_mismatch
)
5381 einfo (_("%P%X: %s architecture of input file `%B'"
5382 " is incompatible with %s output\n"),
5383 bfd_printable_name (input_bfd
), input_bfd
,
5384 bfd_printable_name (output_bfd
));
5386 else if (bfd_count_sections (input_bfd
))
5388 /* If the input bfd has no contents, it shouldn't set the
5389 private data of the output bfd. */
5391 bfd_error_handler_type pfn
= NULL
;
5393 /* If we aren't supposed to warn about mismatched input
5394 files, temporarily set the BFD error handler to a
5395 function which will do nothing. We still want to call
5396 bfd_merge_private_bfd_data, since it may set up
5397 information which is needed in the output file. */
5398 if (! command_line
.warn_mismatch
)
5399 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
5400 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
5402 if (command_line
.warn_mismatch
)
5403 einfo (_("%P%X: failed to merge target specific data"
5404 " of file %B\n"), input_bfd
);
5406 if (! command_line
.warn_mismatch
)
5407 bfd_set_error_handler (pfn
);
5412 /* Look through all the global common symbols and attach them to the
5413 correct section. The -sort-common command line switch may be used
5414 to roughly sort the entries by size. */
5419 if (command_line
.inhibit_common_definition
)
5421 if (link_info
.relocatable
5422 && ! command_line
.force_common_definition
)
5425 if (! config
.sort_common
)
5426 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
5431 for (power
= 4; power
>= 0; power
--)
5432 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5436 /* Place one common symbol in the correct section. */
5439 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
5441 unsigned int power_of_two
;
5445 if (h
->type
!= bfd_link_hash_common
)
5449 power_of_two
= h
->u
.c
.p
->alignment_power
;
5451 if (config
.sort_common
5452 && power_of_two
< (unsigned int) *(int *) info
)
5455 section
= h
->u
.c
.p
->section
;
5457 /* Increase the size of the section to align the common sym. */
5458 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
5459 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
5461 /* Adjust the alignment if necessary. */
5462 if (power_of_two
> section
->alignment_power
)
5463 section
->alignment_power
= power_of_two
;
5465 /* Change the symbol from common to defined. */
5466 h
->type
= bfd_link_hash_defined
;
5467 h
->u
.def
.section
= section
;
5468 h
->u
.def
.value
= section
->size
;
5470 /* Increase the size of the section. */
5471 section
->size
+= size
;
5473 /* Make sure the section is allocated in memory, and make sure that
5474 it is no longer a common section. */
5475 section
->flags
|= SEC_ALLOC
;
5476 section
->flags
&= ~SEC_IS_COMMON
;
5478 if (config
.map_file
!= NULL
)
5480 static bfd_boolean header_printed
;
5485 if (! header_printed
)
5487 minfo (_("\nAllocating common symbols\n"));
5488 minfo (_("Common symbol size file\n\n"));
5489 header_printed
= TRUE
;
5492 name
= bfd_demangle (output_bfd
, h
->root
.string
,
5493 DMGL_ANSI
| DMGL_PARAMS
);
5496 minfo ("%s", h
->root
.string
);
5497 len
= strlen (h
->root
.string
);
5502 len
= strlen (name
);
5518 if (size
<= 0xffffffff)
5519 sprintf (buf
, "%lx", (unsigned long) size
);
5521 sprintf_vma (buf
, size
);
5531 minfo ("%B\n", section
->owner
);
5537 /* Run through the input files and ensure that every input section has
5538 somewhere to go. If one is found without a destination then create
5539 an input request and place it into the statement tree. */
5542 lang_place_orphans (void)
5544 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5548 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5550 if (s
->output_section
== NULL
)
5552 /* This section of the file is not attached, root
5553 around for a sensible place for it to go. */
5555 if (file
->just_syms_flag
)
5556 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5557 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5558 s
->output_section
= bfd_abs_section_ptr
;
5559 else if (strcmp (s
->name
, "COMMON") == 0)
5561 /* This is a lonely common section which must have
5562 come from an archive. We attach to the section
5563 with the wildcard. */
5564 if (! link_info
.relocatable
5565 || command_line
.force_common_definition
)
5567 if (default_common_section
== NULL
)
5569 default_common_section
=
5570 lang_output_section_statement_lookup (".bss");
5573 lang_add_section (&default_common_section
->children
, s
,
5574 default_common_section
);
5577 else if (ldemul_place_orphan (s
))
5581 lang_output_section_statement_type
*os
;
5583 os
= lang_output_section_statement_lookup (s
->name
);
5584 lang_add_section (&os
->children
, s
, os
);
5592 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5594 flagword
*ptr_flags
;
5596 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5602 *ptr_flags
|= SEC_ALLOC
;
5606 *ptr_flags
|= SEC_READONLY
;
5610 *ptr_flags
|= SEC_DATA
;
5614 *ptr_flags
|= SEC_CODE
;
5619 *ptr_flags
|= SEC_LOAD
;
5623 einfo (_("%P%F: invalid syntax in flags\n"));
5630 /* Call a function on each input file. This function will be called
5631 on an archive, but not on the elements. */
5634 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5636 lang_input_statement_type
*f
;
5638 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5640 f
= (lang_input_statement_type
*) f
->next_real_file
)
5644 /* Call a function on each file. The function will be called on all
5645 the elements of an archive which are included in the link, but will
5646 not be called on the archive file itself. */
5649 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5651 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5658 ldlang_add_file (lang_input_statement_type
*entry
)
5660 lang_statement_append (&file_chain
,
5661 (lang_statement_union_type
*) entry
,
5664 /* The BFD linker needs to have a list of all input BFDs involved in
5666 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5667 ASSERT (entry
->the_bfd
!= output_bfd
);
5669 *link_info
.input_bfds_tail
= entry
->the_bfd
;
5670 link_info
.input_bfds_tail
= &entry
->the_bfd
->link_next
;
5671 entry
->the_bfd
->usrdata
= entry
;
5672 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5674 /* Look through the sections and check for any which should not be
5675 included in the link. We need to do this now, so that we can
5676 notice when the backend linker tries to report multiple
5677 definition errors for symbols which are in sections we aren't
5678 going to link. FIXME: It might be better to entirely ignore
5679 symbols which are defined in sections which are going to be
5680 discarded. This would require modifying the backend linker for
5681 each backend which might set the SEC_LINK_ONCE flag. If we do
5682 this, we should probably handle SEC_EXCLUDE in the same way. */
5684 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5688 lang_add_output (const char *name
, int from_script
)
5690 /* Make -o on command line override OUTPUT in script. */
5691 if (!had_output_filename
|| !from_script
)
5693 output_filename
= name
;
5694 had_output_filename
= TRUE
;
5698 static lang_output_section_statement_type
*current_section
;
5709 for (l
= 0; l
< 32; l
++)
5711 if (i
>= (unsigned int) x
)
5719 lang_output_section_statement_type
*
5720 lang_enter_output_section_statement (const char *output_section_statement_name
,
5721 etree_type
*address_exp
,
5722 enum section_type sectype
,
5724 etree_type
*subalign
,
5728 lang_output_section_statement_type
*os
;
5730 os
= lang_output_section_statement_lookup_1 (output_section_statement_name
,
5732 current_section
= os
;
5734 /* Make next things chain into subchain of this. */
5736 if (os
->addr_tree
== NULL
)
5738 os
->addr_tree
= address_exp
;
5740 os
->sectype
= sectype
;
5741 if (sectype
!= noload_section
)
5742 os
->flags
= SEC_NO_FLAGS
;
5744 os
->flags
= SEC_NEVER_LOAD
;
5745 os
->block_value
= 1;
5746 stat_ptr
= &os
->children
;
5748 os
->subsection_alignment
=
5749 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5750 os
->section_alignment
=
5751 topower (exp_get_value_int (align
, -1, "section alignment"));
5753 os
->load_base
= ebase
;
5760 lang_output_statement_type
*new;
5762 new = new_stat (lang_output_statement
, stat_ptr
);
5763 new->name
= output_filename
;
5766 /* Reset the current counters in the regions. */
5769 lang_reset_memory_regions (void)
5771 lang_memory_region_type
*p
= lang_memory_region_list
;
5773 lang_output_section_statement_type
*os
;
5775 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5777 p
->current
= p
->origin
;
5781 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5785 os
->processed_vma
= FALSE
;
5786 os
->processed_lma
= FALSE
;
5789 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5791 /* Save the last size for possible use by bfd_relax_section. */
5792 o
->rawsize
= o
->size
;
5797 /* Worker for lang_gc_sections_1. */
5800 gc_section_callback (lang_wild_statement_type
*ptr
,
5801 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5803 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5804 void *data ATTRIBUTE_UNUSED
)
5806 /* If the wild pattern was marked KEEP, the member sections
5807 should be as well. */
5808 if (ptr
->keep_sections
)
5809 section
->flags
|= SEC_KEEP
;
5812 /* Iterate over sections marking them against GC. */
5815 lang_gc_sections_1 (lang_statement_union_type
*s
)
5817 for (; s
!= NULL
; s
= s
->header
.next
)
5819 switch (s
->header
.type
)
5821 case lang_wild_statement_enum
:
5822 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5824 case lang_constructors_statement_enum
:
5825 lang_gc_sections_1 (constructor_list
.head
);
5827 case lang_output_section_statement_enum
:
5828 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5830 case lang_group_statement_enum
:
5831 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5840 lang_gc_sections (void)
5842 /* Keep all sections so marked in the link script. */
5844 lang_gc_sections_1 (statement_list
.head
);
5846 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5847 the special case of debug info. (See bfd/stabs.c)
5848 Twiddle the flag here, to simplify later linker code. */
5849 if (link_info
.relocatable
)
5851 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5854 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5855 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5856 sec
->flags
&= ~SEC_EXCLUDE
;
5860 if (link_info
.gc_sections
)
5861 bfd_gc_sections (output_bfd
, &link_info
);
5864 /* Worker for lang_find_relro_sections_1. */
5867 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
5868 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5870 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5873 /* Discarded, excluded and ignored sections effectively have zero
5875 if (section
->output_section
!= NULL
5876 && section
->output_section
->owner
== output_bfd
5877 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
5878 && !IGNORE_SECTION (section
)
5879 && section
->size
!= 0)
5881 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
5882 *has_relro_section
= TRUE
;
5886 /* Iterate over sections for relro sections. */
5889 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
5890 bfd_boolean
*has_relro_section
)
5892 if (*has_relro_section
)
5895 for (; s
!= NULL
; s
= s
->header
.next
)
5897 if (s
== expld
.dataseg
.relro_end_stat
)
5900 switch (s
->header
.type
)
5902 case lang_wild_statement_enum
:
5903 walk_wild (&s
->wild_statement
,
5904 find_relro_section_callback
,
5907 case lang_constructors_statement_enum
:
5908 lang_find_relro_sections_1 (constructor_list
.head
,
5911 case lang_output_section_statement_enum
:
5912 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
5915 case lang_group_statement_enum
:
5916 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
5926 lang_find_relro_sections (void)
5928 bfd_boolean has_relro_section
= FALSE
;
5930 /* Check all sections in the link script. */
5932 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
5933 &has_relro_section
);
5935 if (!has_relro_section
)
5936 link_info
.relro
= FALSE
;
5939 /* Relax all sections until bfd_relax_section gives up. */
5942 relax_sections (void)
5944 /* Keep relaxing until bfd_relax_section gives up. */
5945 bfd_boolean relax_again
;
5947 link_info
.relax_trip
= -1;
5950 relax_again
= FALSE
;
5951 link_info
.relax_trip
++;
5953 /* Note: pe-dll.c does something like this also. If you find
5954 you need to change this code, you probably need to change
5955 pe-dll.c also. DJ */
5957 /* Do all the assignments with our current guesses as to
5959 lang_do_assignments ();
5961 /* We must do this after lang_do_assignments, because it uses
5963 lang_reset_memory_regions ();
5965 /* Perform another relax pass - this time we know where the
5966 globals are, so can make a better guess. */
5967 lang_size_sections (&relax_again
, FALSE
);
5969 while (relax_again
);
5975 /* Finalize dynamic list. */
5976 if (link_info
.dynamic_list
)
5977 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
5979 current_target
= default_target
;
5981 /* Open the output file. */
5982 lang_for_each_statement (ldlang_open_output
);
5985 ldemul_create_output_section_statements ();
5987 /* Add to the hash table all undefineds on the command line. */
5988 lang_place_undefineds ();
5990 if (!bfd_section_already_linked_table_init ())
5991 einfo (_("%P%F: Failed to create hash table\n"));
5993 /* Create a bfd for each input file. */
5994 current_target
= default_target
;
5995 open_input_bfds (statement_list
.head
, FALSE
);
5997 link_info
.gc_sym_list
= &entry_symbol
;
5998 if (entry_symbol
.name
== NULL
)
5999 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
6001 ldemul_after_open ();
6003 bfd_section_already_linked_table_free ();
6005 /* Make sure that we're not mixing architectures. We call this
6006 after all the input files have been opened, but before we do any
6007 other processing, so that any operations merge_private_bfd_data
6008 does on the output file will be known during the rest of the
6012 /* Handle .exports instead of a version script if we're told to do so. */
6013 if (command_line
.version_exports_section
)
6014 lang_do_version_exports_section ();
6016 /* Build all sets based on the information gathered from the input
6018 ldctor_build_sets ();
6020 /* Remove unreferenced sections if asked to. */
6021 lang_gc_sections ();
6023 /* Size up the common data. */
6026 /* Update wild statements. */
6027 update_wild_statements (statement_list
.head
);
6029 /* Run through the contours of the script and attach input sections
6030 to the correct output sections. */
6031 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
6033 process_insert_statements ();
6035 /* Find any sections not attached explicitly and handle them. */
6036 lang_place_orphans ();
6038 if (! link_info
.relocatable
)
6042 /* Merge SEC_MERGE sections. This has to be done after GC of
6043 sections, so that GCed sections are not merged, but before
6044 assigning dynamic symbols, since removing whole input sections
6046 bfd_merge_sections (output_bfd
, &link_info
);
6048 /* Look for a text section and set the readonly attribute in it. */
6049 found
= bfd_get_section_by_name (output_bfd
, ".text");
6053 if (config
.text_read_only
)
6054 found
->flags
|= SEC_READONLY
;
6056 found
->flags
&= ~SEC_READONLY
;
6060 /* Do anything special before sizing sections. This is where ELF
6061 and other back-ends size dynamic sections. */
6062 ldemul_before_allocation ();
6064 /* We must record the program headers before we try to fix the
6065 section positions, since they will affect SIZEOF_HEADERS. */
6066 lang_record_phdrs ();
6068 /* Check relro sections. */
6069 if (link_info
.relro
&& ! link_info
.relocatable
)
6070 lang_find_relro_sections ();
6072 /* Size up the sections. */
6073 lang_size_sections (NULL
, !command_line
.relax
);
6075 /* Now run around and relax if we can. */
6076 if (command_line
.relax
)
6078 /* We may need more than one relaxation pass. */
6079 int i
= link_info
.relax_pass
;
6081 /* The backend can use it to determine the current pass. */
6082 link_info
.relax_pass
= 0;
6087 link_info
.relax_pass
++;
6090 /* Final extra sizing to report errors. */
6091 lang_do_assignments ();
6092 lang_reset_memory_regions ();
6093 lang_size_sections (NULL
, TRUE
);
6096 /* See if anything special should be done now we know how big
6098 ldemul_after_allocation ();
6100 /* Fix any .startof. or .sizeof. symbols. */
6101 lang_set_startof ();
6103 /* Do all the assignments, now that we know the final resting places
6104 of all the symbols. */
6106 lang_do_assignments ();
6110 /* Make sure that the section addresses make sense. */
6111 if (! link_info
.relocatable
6112 && command_line
.check_section_addresses
)
6113 lang_check_section_addresses ();
6118 /* EXPORTED TO YACC */
6121 lang_add_wild (struct wildcard_spec
*filespec
,
6122 struct wildcard_list
*section_list
,
6123 bfd_boolean keep_sections
)
6125 struct wildcard_list
*curr
, *next
;
6126 lang_wild_statement_type
*new;
6128 /* Reverse the list as the parser puts it back to front. */
6129 for (curr
= section_list
, section_list
= NULL
;
6131 section_list
= curr
, curr
= next
)
6133 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
6134 placed_commons
= TRUE
;
6137 curr
->next
= section_list
;
6140 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
6142 if (strcmp (filespec
->name
, "*") == 0)
6143 filespec
->name
= NULL
;
6144 else if (! wildcardp (filespec
->name
))
6145 lang_has_input_file
= TRUE
;
6148 new = new_stat (lang_wild_statement
, stat_ptr
);
6149 new->filename
= NULL
;
6150 new->filenames_sorted
= FALSE
;
6151 if (filespec
!= NULL
)
6153 new->filename
= filespec
->name
;
6154 new->filenames_sorted
= filespec
->sorted
== by_name
;
6156 new->section_list
= section_list
;
6157 new->keep_sections
= keep_sections
;
6158 lang_list_init (&new->children
);
6159 analyze_walk_wild_section_handler (new);
6163 lang_section_start (const char *name
, etree_type
*address
,
6164 const segment_type
*segment
)
6166 lang_address_statement_type
*ad
;
6168 ad
= new_stat (lang_address_statement
, stat_ptr
);
6169 ad
->section_name
= name
;
6170 ad
->address
= address
;
6171 ad
->segment
= segment
;
6174 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6175 because of a -e argument on the command line, or zero if this is
6176 called by ENTRY in a linker script. Command line arguments take
6180 lang_add_entry (const char *name
, bfd_boolean cmdline
)
6182 if (entry_symbol
.name
== NULL
6184 || ! entry_from_cmdline
)
6186 entry_symbol
.name
= name
;
6187 entry_from_cmdline
= cmdline
;
6191 /* Set the default start symbol to NAME. .em files should use this,
6192 not lang_add_entry, to override the use of "start" if neither the
6193 linker script nor the command line specifies an entry point. NAME
6194 must be permanently allocated. */
6196 lang_default_entry (const char *name
)
6198 entry_symbol_default
= name
;
6202 lang_add_target (const char *name
)
6204 lang_target_statement_type
*new;
6206 new = new_stat (lang_target_statement
, stat_ptr
);
6211 lang_add_map (const char *name
)
6218 map_option_f
= TRUE
;
6226 lang_add_fill (fill_type
*fill
)
6228 lang_fill_statement_type
*new;
6230 new = new_stat (lang_fill_statement
, stat_ptr
);
6235 lang_add_data (int type
, union etree_union
*exp
)
6237 lang_data_statement_type
*new;
6239 new = new_stat (lang_data_statement
, stat_ptr
);
6244 /* Create a new reloc statement. RELOC is the BFD relocation type to
6245 generate. HOWTO is the corresponding howto structure (we could
6246 look this up, but the caller has already done so). SECTION is the
6247 section to generate a reloc against, or NAME is the name of the
6248 symbol to generate a reloc against. Exactly one of SECTION and
6249 NAME must be NULL. ADDEND is an expression for the addend. */
6252 lang_add_reloc (bfd_reloc_code_real_type reloc
,
6253 reloc_howto_type
*howto
,
6256 union etree_union
*addend
)
6258 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
6262 p
->section
= section
;
6264 p
->addend_exp
= addend
;
6266 p
->addend_value
= 0;
6267 p
->output_section
= NULL
;
6268 p
->output_offset
= 0;
6271 lang_assignment_statement_type
*
6272 lang_add_assignment (etree_type
*exp
)
6274 lang_assignment_statement_type
*new;
6276 new = new_stat (lang_assignment_statement
, stat_ptr
);
6282 lang_add_attribute (enum statement_enum attribute
)
6284 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
6288 lang_startup (const char *name
)
6290 if (startup_file
!= NULL
)
6292 einfo (_("%P%F: multiple STARTUP files\n"));
6294 first_file
->filename
= name
;
6295 first_file
->local_sym_name
= name
;
6296 first_file
->real
= TRUE
;
6298 startup_file
= name
;
6302 lang_float (bfd_boolean maybe
)
6304 lang_float_flag
= maybe
;
6308 /* Work out the load- and run-time regions from a script statement, and
6309 store them in *LMA_REGION and *REGION respectively.
6311 MEMSPEC is the name of the run-time region, or the value of
6312 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6313 LMA_MEMSPEC is the name of the load-time region, or null if the
6314 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6315 had an explicit load address.
6317 It is an error to specify both a load region and a load address. */
6320 lang_get_regions (lang_memory_region_type
**region
,
6321 lang_memory_region_type
**lma_region
,
6322 const char *memspec
,
6323 const char *lma_memspec
,
6324 bfd_boolean have_lma
,
6325 bfd_boolean have_vma
)
6327 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
6329 /* If no runtime region or VMA has been specified, but the load region
6330 has been specified, then use the load region for the runtime region
6332 if (lma_memspec
!= NULL
6334 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
6335 *region
= *lma_region
;
6337 *region
= lang_memory_region_lookup (memspec
, FALSE
);
6339 if (have_lma
&& lma_memspec
!= 0)
6340 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6344 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
6345 lang_output_section_phdr_list
*phdrs
,
6346 const char *lma_memspec
)
6348 lang_get_regions (¤t_section
->region
,
6349 ¤t_section
->lma_region
,
6350 memspec
, lma_memspec
,
6351 current_section
->load_base
!= NULL
,
6352 current_section
->addr_tree
!= NULL
);
6353 current_section
->fill
= fill
;
6354 current_section
->phdrs
= phdrs
;
6355 stat_ptr
= &statement_list
;
6358 /* Create an absolute symbol with the given name with the value of the
6359 address of first byte of the section named.
6361 If the symbol already exists, then do nothing. */
6364 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
6366 struct bfd_link_hash_entry
*h
;
6368 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6370 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6372 if (h
->type
== bfd_link_hash_new
6373 || h
->type
== bfd_link_hash_undefined
)
6377 h
->type
= bfd_link_hash_defined
;
6379 sec
= bfd_get_section_by_name (output_bfd
, secname
);
6383 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
6385 h
->u
.def
.section
= bfd_abs_section_ptr
;
6389 /* Create an absolute symbol with the given name with the value of the
6390 address of the first byte after the end of the section named.
6392 If the symbol already exists, then do nothing. */
6395 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
6397 struct bfd_link_hash_entry
*h
;
6399 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6401 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6403 if (h
->type
== bfd_link_hash_new
6404 || h
->type
== bfd_link_hash_undefined
)
6408 h
->type
= bfd_link_hash_defined
;
6410 sec
= bfd_get_section_by_name (output_bfd
, secname
);
6414 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
6415 + TO_ADDR (sec
->size
));
6417 h
->u
.def
.section
= bfd_abs_section_ptr
;
6422 lang_statement_append (lang_statement_list_type
*list
,
6423 lang_statement_union_type
*element
,
6424 lang_statement_union_type
**field
)
6426 *(list
->tail
) = element
;
6430 /* Set the output format type. -oformat overrides scripts. */
6433 lang_add_output_format (const char *format
,
6438 if (output_target
== NULL
|| !from_script
)
6440 if (command_line
.endian
== ENDIAN_BIG
6443 else if (command_line
.endian
== ENDIAN_LITTLE
6447 output_target
= format
;
6452 lang_add_insert (const char *where
, int is_before
)
6454 lang_insert_statement_type
*new;
6456 new = new_stat (lang_insert_statement
, stat_ptr
);
6458 new->is_before
= is_before
;
6459 saved_script_handle
= previous_script_handle
;
6462 /* Enter a group. This creates a new lang_group_statement, and sets
6463 stat_ptr to build new statements within the group. */
6466 lang_enter_group (void)
6468 lang_group_statement_type
*g
;
6470 g
= new_stat (lang_group_statement
, stat_ptr
);
6471 lang_list_init (&g
->children
);
6472 stat_ptr
= &g
->children
;
6475 /* Leave a group. This just resets stat_ptr to start writing to the
6476 regular list of statements again. Note that this will not work if
6477 groups can occur inside anything else which can adjust stat_ptr,
6478 but currently they can't. */
6481 lang_leave_group (void)
6483 stat_ptr
= &statement_list
;
6486 /* Add a new program header. This is called for each entry in a PHDRS
6487 command in a linker script. */
6490 lang_new_phdr (const char *name
,
6492 bfd_boolean filehdr
,
6497 struct lang_phdr
*n
, **pp
;
6499 n
= stat_alloc (sizeof (struct lang_phdr
));
6502 n
->type
= exp_get_value_int (type
, 0, "program header type");
6503 n
->filehdr
= filehdr
;
6508 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6513 /* Record the program header information in the output BFD. FIXME: We
6514 should not be calling an ELF specific function here. */
6517 lang_record_phdrs (void)
6521 lang_output_section_phdr_list
*last
;
6522 struct lang_phdr
*l
;
6523 lang_output_section_statement_type
*os
;
6526 secs
= xmalloc (alc
* sizeof (asection
*));
6529 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
6536 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6540 lang_output_section_phdr_list
*pl
;
6542 if (os
->constraint
== -1)
6550 if (os
->sectype
== noload_section
6551 || os
->bfd_section
== NULL
6552 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6557 lang_output_section_statement_type
* tmp_os
;
6559 /* If we have not run across a section with a program
6560 header assigned to it yet, then scan forwards to find
6561 one. This prevents inconsistencies in the linker's
6562 behaviour when a script has specified just a single
6563 header and there are sections in that script which are
6564 not assigned to it, and which occur before the first
6565 use of that header. See here for more details:
6566 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6567 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
6570 last
= tmp_os
->phdrs
;
6574 einfo (_("%F%P: no sections assigned to phdrs\n"));
6579 if (os
->bfd_section
== NULL
)
6582 for (; pl
!= NULL
; pl
= pl
->next
)
6584 if (strcmp (pl
->name
, l
->name
) == 0)
6589 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6591 secs
[c
] = os
->bfd_section
;
6598 if (l
->flags
== NULL
)
6601 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6606 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6608 if (! bfd_record_phdr (output_bfd
, l
->type
,
6609 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6610 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6611 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6616 /* Make sure all the phdr assignments succeeded. */
6617 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6621 lang_output_section_phdr_list
*pl
;
6623 if (os
->constraint
== -1
6624 || os
->bfd_section
== NULL
)
6627 for (pl
= os
->phdrs
;
6630 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6631 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6632 os
->name
, pl
->name
);
6636 /* Record a list of sections which may not be cross referenced. */
6639 lang_add_nocrossref (lang_nocrossref_type
*l
)
6641 struct lang_nocrossrefs
*n
;
6643 n
= xmalloc (sizeof *n
);
6644 n
->next
= nocrossref_list
;
6646 nocrossref_list
= n
;
6648 /* Set notice_all so that we get informed about all symbols. */
6649 link_info
.notice_all
= TRUE
;
6652 /* Overlay handling. We handle overlays with some static variables. */
6654 /* The overlay virtual address. */
6655 static etree_type
*overlay_vma
;
6656 /* And subsection alignment. */
6657 static etree_type
*overlay_subalign
;
6659 /* An expression for the maximum section size seen so far. */
6660 static etree_type
*overlay_max
;
6662 /* A list of all the sections in this overlay. */
6664 struct overlay_list
{
6665 struct overlay_list
*next
;
6666 lang_output_section_statement_type
*os
;
6669 static struct overlay_list
*overlay_list
;
6671 /* Start handling an overlay. */
6674 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6676 /* The grammar should prevent nested overlays from occurring. */
6677 ASSERT (overlay_vma
== NULL
6678 && overlay_subalign
== NULL
6679 && overlay_max
== NULL
);
6681 overlay_vma
= vma_expr
;
6682 overlay_subalign
= subalign
;
6685 /* Start a section in an overlay. We handle this by calling
6686 lang_enter_output_section_statement with the correct VMA.
6687 lang_leave_overlay sets up the LMA and memory regions. */
6690 lang_enter_overlay_section (const char *name
)
6692 struct overlay_list
*n
;
6695 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
6696 0, overlay_subalign
, 0, 0);
6698 /* If this is the first section, then base the VMA of future
6699 sections on this one. This will work correctly even if `.' is
6700 used in the addresses. */
6701 if (overlay_list
== NULL
)
6702 overlay_vma
= exp_nameop (ADDR
, name
);
6704 /* Remember the section. */
6705 n
= xmalloc (sizeof *n
);
6706 n
->os
= current_section
;
6707 n
->next
= overlay_list
;
6710 size
= exp_nameop (SIZEOF
, name
);
6712 /* Arrange to work out the maximum section end address. */
6713 if (overlay_max
== NULL
)
6716 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6719 /* Finish a section in an overlay. There isn't any special to do
6723 lang_leave_overlay_section (fill_type
*fill
,
6724 lang_output_section_phdr_list
*phdrs
)
6731 name
= current_section
->name
;
6733 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6734 region and that no load-time region has been specified. It doesn't
6735 really matter what we say here, since lang_leave_overlay will
6737 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6739 /* Define the magic symbols. */
6741 clean
= xmalloc (strlen (name
) + 1);
6743 for (s1
= name
; *s1
!= '\0'; s1
++)
6744 if (ISALNUM (*s1
) || *s1
== '_')
6748 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6749 sprintf (buf
, "__load_start_%s", clean
);
6750 lang_add_assignment (exp_provide (buf
,
6751 exp_nameop (LOADADDR
, name
),
6754 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6755 sprintf (buf
, "__load_stop_%s", clean
);
6756 lang_add_assignment (exp_provide (buf
,
6758 exp_nameop (LOADADDR
, name
),
6759 exp_nameop (SIZEOF
, name
)),
6765 /* Finish an overlay. If there are any overlay wide settings, this
6766 looks through all the sections in the overlay and sets them. */
6769 lang_leave_overlay (etree_type
*lma_expr
,
6772 const char *memspec
,
6773 lang_output_section_phdr_list
*phdrs
,
6774 const char *lma_memspec
)
6776 lang_memory_region_type
*region
;
6777 lang_memory_region_type
*lma_region
;
6778 struct overlay_list
*l
;
6779 lang_nocrossref_type
*nocrossref
;
6781 lang_get_regions (®ion
, &lma_region
,
6782 memspec
, lma_memspec
,
6783 lma_expr
!= NULL
, FALSE
);
6787 /* After setting the size of the last section, set '.' to end of the
6789 if (overlay_list
!= NULL
)
6790 overlay_list
->os
->update_dot_tree
6791 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6796 struct overlay_list
*next
;
6798 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6801 l
->os
->region
= region
;
6802 l
->os
->lma_region
= lma_region
;
6804 /* The first section has the load address specified in the
6805 OVERLAY statement. The rest are worked out from that.
6806 The base address is not needed (and should be null) if
6807 an LMA region was specified. */
6810 l
->os
->load_base
= lma_expr
;
6811 l
->os
->sectype
= normal_section
;
6813 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6814 l
->os
->phdrs
= phdrs
;
6818 lang_nocrossref_type
*nc
;
6820 nc
= xmalloc (sizeof *nc
);
6821 nc
->name
= l
->os
->name
;
6822 nc
->next
= nocrossref
;
6831 if (nocrossref
!= NULL
)
6832 lang_add_nocrossref (nocrossref
);
6835 overlay_list
= NULL
;
6839 /* Version handling. This is only useful for ELF. */
6841 /* This global variable holds the version tree that we build. */
6843 struct bfd_elf_version_tree
*lang_elf_version_info
;
6845 /* If PREV is NULL, return first version pattern matching particular symbol.
6846 If PREV is non-NULL, return first version pattern matching particular
6847 symbol after PREV (previously returned by lang_vers_match). */
6849 static struct bfd_elf_version_expr
*
6850 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6851 struct bfd_elf_version_expr
*prev
,
6854 const char *cxx_sym
= sym
;
6855 const char *java_sym
= sym
;
6856 struct bfd_elf_version_expr
*expr
= NULL
;
6858 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6860 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6864 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6866 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6871 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6873 struct bfd_elf_version_expr e
;
6875 switch (prev
? prev
->mask
: 0)
6878 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6881 expr
= htab_find (head
->htab
, &e
);
6882 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
6883 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
6889 case BFD_ELF_VERSION_C_TYPE
:
6890 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6893 expr
= htab_find (head
->htab
, &e
);
6894 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
6895 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6901 case BFD_ELF_VERSION_CXX_TYPE
:
6902 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6904 e
.symbol
= java_sym
;
6905 expr
= htab_find (head
->htab
, &e
);
6906 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
6907 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6918 /* Finally, try the wildcards. */
6919 if (prev
== NULL
|| prev
->symbol
)
6920 expr
= head
->remaining
;
6923 for (; expr
; expr
= expr
->next
)
6930 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
6933 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6935 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6939 if (fnmatch (expr
->pattern
, s
, 0) == 0)
6945 free ((char *) cxx_sym
);
6946 if (java_sym
!= sym
)
6947 free ((char *) java_sym
);
6951 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6952 return a string pointing to the symbol name. */
6955 realsymbol (const char *pattern
)
6958 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
6959 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
6961 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
6963 /* It is a glob pattern only if there is no preceding
6965 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
6973 /* Remove the preceding backslash. */
6980 backslash
= *p
== '\\';
6995 /* This is called for each variable name or match expression. NEW is
6996 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6997 pattern to be matched against symbol names. */
6999 struct bfd_elf_version_expr
*
7000 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
7003 bfd_boolean literal_p
)
7005 struct bfd_elf_version_expr
*ret
;
7007 ret
= xmalloc (sizeof *ret
);
7009 ret
->pattern
= literal_p
? NULL
: new;
7012 ret
->symbol
= literal_p
? new : realsymbol (new);
7014 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
7015 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7016 else if (strcasecmp (lang
, "C++") == 0)
7017 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
7018 else if (strcasecmp (lang
, "Java") == 0)
7019 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
7022 einfo (_("%X%P: unknown language `%s' in version information\n"),
7024 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7027 return ldemul_new_vers_pattern (ret
);
7030 /* This is called for each set of variable names and match
7033 struct bfd_elf_version_tree
*
7034 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
7035 struct bfd_elf_version_expr
*locals
)
7037 struct bfd_elf_version_tree
*ret
;
7039 ret
= xcalloc (1, sizeof *ret
);
7040 ret
->globals
.list
= globals
;
7041 ret
->locals
.list
= locals
;
7042 ret
->match
= lang_vers_match
;
7043 ret
->name_indx
= (unsigned int) -1;
7047 /* This static variable keeps track of version indices. */
7049 static int version_index
;
7052 version_expr_head_hash (const void *p
)
7054 const struct bfd_elf_version_expr
*e
= p
;
7056 return htab_hash_string (e
->symbol
);
7060 version_expr_head_eq (const void *p1
, const void *p2
)
7062 const struct bfd_elf_version_expr
*e1
= p1
;
7063 const struct bfd_elf_version_expr
*e2
= p2
;
7065 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
7069 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
7072 struct bfd_elf_version_expr
*e
, *next
;
7073 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
7075 for (e
= head
->list
; e
; e
= e
->next
)
7079 head
->mask
|= e
->mask
;
7084 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
7085 version_expr_head_eq
, NULL
);
7086 list_loc
= &head
->list
;
7087 remaining_loc
= &head
->remaining
;
7088 for (e
= head
->list
; e
; e
= next
)
7094 remaining_loc
= &e
->next
;
7098 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
7102 struct bfd_elf_version_expr
*e1
, *last
;
7108 if (e1
->mask
== e
->mask
)
7116 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
7120 /* This is a duplicate. */
7121 /* FIXME: Memory leak. Sometimes pattern is not
7122 xmalloced alone, but in larger chunk of memory. */
7123 /* free (e->symbol); */
7128 e
->next
= last
->next
;
7136 list_loc
= &e
->next
;
7140 *remaining_loc
= NULL
;
7141 *list_loc
= head
->remaining
;
7144 head
->remaining
= head
->list
;
7147 /* This is called when we know the name and dependencies of the
7151 lang_register_vers_node (const char *name
,
7152 struct bfd_elf_version_tree
*version
,
7153 struct bfd_elf_version_deps
*deps
)
7155 struct bfd_elf_version_tree
*t
, **pp
;
7156 struct bfd_elf_version_expr
*e1
;
7161 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
7162 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
7164 einfo (_("%X%P: anonymous version tag cannot be combined"
7165 " with other version tags\n"));
7170 /* Make sure this node has a unique name. */
7171 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7172 if (strcmp (t
->name
, name
) == 0)
7173 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
7175 lang_finalize_version_expr_head (&version
->globals
);
7176 lang_finalize_version_expr_head (&version
->locals
);
7178 /* Check the global and local match names, and make sure there
7179 aren't any duplicates. */
7181 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
7183 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7185 struct bfd_elf_version_expr
*e2
;
7187 if (t
->locals
.htab
&& e1
->symbol
)
7189 e2
= htab_find (t
->locals
.htab
, e1
);
7190 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
7192 if (e1
->mask
== e2
->mask
)
7193 einfo (_("%X%P: duplicate expression `%s'"
7194 " in version information\n"), e1
->symbol
);
7198 else if (!e1
->symbol
)
7199 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7200 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7201 && e1
->mask
== e2
->mask
)
7202 einfo (_("%X%P: duplicate expression `%s'"
7203 " in version information\n"), e1
->pattern
);
7207 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
7209 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7211 struct bfd_elf_version_expr
*e2
;
7213 if (t
->globals
.htab
&& e1
->symbol
)
7215 e2
= htab_find (t
->globals
.htab
, e1
);
7216 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
7218 if (e1
->mask
== e2
->mask
)
7219 einfo (_("%X%P: duplicate expression `%s'"
7220 " in version information\n"),
7225 else if (!e1
->symbol
)
7226 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7227 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7228 && e1
->mask
== e2
->mask
)
7229 einfo (_("%X%P: duplicate expression `%s'"
7230 " in version information\n"), e1
->pattern
);
7234 version
->deps
= deps
;
7235 version
->name
= name
;
7236 if (name
[0] != '\0')
7239 version
->vernum
= version_index
;
7242 version
->vernum
= 0;
7244 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
7249 /* This is called when we see a version dependency. */
7251 struct bfd_elf_version_deps
*
7252 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
7254 struct bfd_elf_version_deps
*ret
;
7255 struct bfd_elf_version_tree
*t
;
7257 ret
= xmalloc (sizeof *ret
);
7260 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7262 if (strcmp (t
->name
, name
) == 0)
7264 ret
->version_needed
= t
;
7269 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
7275 lang_do_version_exports_section (void)
7277 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
7279 LANG_FOR_EACH_INPUT_STATEMENT (is
)
7281 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
7289 contents
= xmalloc (len
);
7290 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
7291 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
7294 while (p
< contents
+ len
)
7296 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
7297 p
= strchr (p
, '\0') + 1;
7300 /* Do not free the contents, as we used them creating the regex. */
7302 /* Do not include this section in the link. */
7303 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
7306 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
7307 lang_register_vers_node (command_line
.version_exports_section
,
7308 lang_new_vers_node (greg
, lreg
), NULL
);
7312 lang_add_unique (const char *name
)
7314 struct unique_sections
*ent
;
7316 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
7317 if (strcmp (ent
->name
, name
) == 0)
7320 ent
= xmalloc (sizeof *ent
);
7321 ent
->name
= xstrdup (name
);
7322 ent
->next
= unique_section_list
;
7323 unique_section_list
= ent
;
7326 /* Append the list of dynamic symbols to the existing one. */
7329 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
7331 if (link_info
.dynamic_list
)
7333 struct bfd_elf_version_expr
*tail
;
7334 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
7336 tail
->next
= link_info
.dynamic_list
->head
.list
;
7337 link_info
.dynamic_list
->head
.list
= dynamic
;
7341 struct bfd_elf_dynamic_list
*d
;
7343 d
= xcalloc (1, sizeof *d
);
7344 d
->head
.list
= dynamic
;
7345 d
->match
= lang_vers_match
;
7346 link_info
.dynamic_list
= d
;
7350 /* Append the list of C++ typeinfo dynamic symbols to the existing
7354 lang_append_dynamic_list_cpp_typeinfo (void)
7356 const char * symbols
[] =
7358 "typeinfo name for*",
7361 struct bfd_elf_version_expr
*dynamic
= NULL
;
7364 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7365 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7368 lang_append_dynamic_list (dynamic
);
7371 /* Append the list of C++ operator new and delete dynamic symbols to the
7375 lang_append_dynamic_list_cpp_new (void)
7377 const char * symbols
[] =
7382 struct bfd_elf_version_expr
*dynamic
= NULL
;
7385 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7386 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7389 lang_append_dynamic_list (dynamic
);