1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of the GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
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
;
65 static lang_statement_list_type
*stat_save
[10];
66 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
68 /* Forward declarations. */
69 static void exp_init_os (etree_type
*);
70 static void init_map_userdata (bfd
*, asection
*, void *);
71 static lang_input_statement_type
*lookup_name (const char *);
72 static struct bfd_hash_entry
*lang_definedness_newfunc
73 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
74 static void insert_undefined (const char *);
75 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
76 static void print_statement (lang_statement_union_type
*,
77 lang_output_section_statement_type
*);
78 static void print_statement_list (lang_statement_union_type
*,
79 lang_output_section_statement_type
*);
80 static void print_statements (void);
81 static void print_input_section (asection
*, bfd_boolean
);
82 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
83 static void lang_record_phdrs (void);
84 static void lang_do_version_exports_section (void);
85 static void lang_finalize_version_expr_head
86 (struct bfd_elf_version_expr_head
*);
88 /* Exported variables. */
89 lang_output_section_statement_type
*abs_output_section
;
90 lang_statement_list_type lang_output_section_statement
;
91 lang_statement_list_type
*stat_ptr
= &statement_list
;
92 lang_statement_list_type file_chain
= { NULL
, NULL
};
93 lang_statement_list_type input_file_chain
;
94 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
95 static const char *entry_symbol_default
= "start";
96 const char *entry_section
= ".text";
97 bfd_boolean entry_from_cmdline
;
98 bfd_boolean lang_has_input_file
= FALSE
;
99 bfd_boolean had_output_filename
= FALSE
;
100 bfd_boolean lang_float_flag
= FALSE
;
101 bfd_boolean delete_output_file_on_failure
= FALSE
;
102 struct lang_phdr
*lang_phdr_list
;
103 struct lang_nocrossrefs
*nocrossref_list
;
104 static struct unique_sections
*unique_section_list
;
105 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
107 /* Functions that traverse the linker script and might evaluate
108 DEFINED() need to increment this. */
109 int lang_statement_iteration
= 0;
111 etree_type
*base
; /* Relocation base - or null */
113 /* Return TRUE if the PATTERN argument is a wildcard pattern.
114 Although backslashes are treated specially if a pattern contains
115 wildcards, we do not consider the mere presence of a backslash to
116 be enough to cause the pattern to be treated as a wildcard.
117 That lets us handle DOS filenames more naturally. */
118 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
120 #define new_stat(x, y) \
121 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
123 #define outside_section_address(q) \
124 ((q)->output_offset + (q)->output_section->vma)
126 #define outside_symbol_address(q) \
127 ((q)->value + outside_section_address (q->section))
129 #define SECTION_NAME_MAP_LENGTH (16)
132 stat_alloc (size_t size
)
134 return obstack_alloc (&stat_obstack
, size
);
138 name_match (const char *pattern
, const char *name
)
140 if (wildcardp (pattern
))
141 return fnmatch (pattern
, name
, 0);
142 return strcmp (pattern
, name
);
145 /* If PATTERN is of the form archive:file, return a pointer to the
146 separator. If not, return NULL. */
149 archive_path (const char *pattern
)
153 if (link_info
.path_separator
== 0)
156 p
= strchr (pattern
, link_info
.path_separator
);
157 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
158 if (p
== NULL
|| link_info
.path_separator
!= ':')
161 /* Assume a match on the second char is part of drive specifier,
162 as in "c:\silly.dos". */
163 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
164 p
= strchr (p
+ 1, link_info
.path_separator
);
169 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
170 return whether F matches FILE_SPEC. */
173 input_statement_is_archive_path (const char *file_spec
, char *sep
,
174 lang_input_statement_type
*f
)
176 bfd_boolean match
= FALSE
;
179 || name_match (sep
+ 1, f
->filename
) == 0)
180 && ((sep
!= file_spec
)
181 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
185 if (sep
!= file_spec
)
187 const char *aname
= f
->the_bfd
->my_archive
->filename
;
189 match
= name_match (file_spec
, aname
) == 0;
190 *sep
= link_info
.path_separator
;
197 unique_section_p (const asection
*sec
)
199 struct unique_sections
*unam
;
202 if (link_info
.relocatable
203 && sec
->owner
!= NULL
204 && bfd_is_group_section (sec
->owner
, sec
))
208 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
209 if (name_match (unam
->name
, secnam
) == 0)
215 /* Generic traversal routines for finding matching sections. */
217 /* Try processing a section against a wildcard. This just calls
218 the callback unless the filename exclusion list is present
219 and excludes the file. It's hardly ever present so this
220 function is very fast. */
223 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
224 lang_input_statement_type
*file
,
226 struct wildcard_list
*sec
,
230 struct name_list
*list_tmp
;
232 /* Don't process sections from files which were excluded. */
233 for (list_tmp
= sec
->spec
.exclude_name_list
;
235 list_tmp
= list_tmp
->next
)
237 char *p
= archive_path (list_tmp
->name
);
241 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
245 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
248 /* FIXME: Perhaps remove the following at some stage? Matching
249 unadorned archives like this was never documented and has
250 been superceded by the archive:path syntax. */
251 else if (file
->the_bfd
!= NULL
252 && file
->the_bfd
->my_archive
!= NULL
253 && name_match (list_tmp
->name
,
254 file
->the_bfd
->my_archive
->filename
) == 0)
258 (*callback
) (ptr
, sec
, s
, file
, data
);
261 /* Lowest common denominator routine that can handle everything correctly,
265 walk_wild_section_general (lang_wild_statement_type
*ptr
,
266 lang_input_statement_type
*file
,
271 struct wildcard_list
*sec
;
273 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
275 sec
= ptr
->section_list
;
277 (*callback
) (ptr
, sec
, s
, file
, data
);
281 bfd_boolean skip
= FALSE
;
283 if (sec
->spec
.name
!= NULL
)
285 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
287 skip
= name_match (sec
->spec
.name
, sname
) != 0;
291 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
298 /* Routines to find a single section given its name. If there's more
299 than one section with that name, we report that. */
303 asection
*found_section
;
304 bfd_boolean multiple_sections_found
;
305 } section_iterator_callback_data
;
308 section_iterator_callback (bfd
*bfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
310 section_iterator_callback_data
*d
= data
;
312 if (d
->found_section
!= NULL
)
314 d
->multiple_sections_found
= TRUE
;
318 d
->found_section
= s
;
323 find_section (lang_input_statement_type
*file
,
324 struct wildcard_list
*sec
,
325 bfd_boolean
*multiple_sections_found
)
327 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
329 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
330 section_iterator_callback
, &cb_data
);
331 *multiple_sections_found
= cb_data
.multiple_sections_found
;
332 return cb_data
.found_section
;
335 /* Code for handling simple wildcards without going through fnmatch,
336 which can be expensive because of charset translations etc. */
338 /* A simple wild is a literal string followed by a single '*',
339 where the literal part is at least 4 characters long. */
342 is_simple_wild (const char *name
)
344 size_t len
= strcspn (name
, "*?[");
345 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
349 match_simple_wild (const char *pattern
, const char *name
)
351 /* The first four characters of the pattern are guaranteed valid
352 non-wildcard characters. So we can go faster. */
353 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
354 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
359 while (*pattern
!= '*')
360 if (*name
++ != *pattern
++)
366 /* Compare sections ASEC and BSEC according to SORT. */
369 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
378 case by_alignment_name
:
379 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
380 - bfd_section_alignment (asec
->owner
, asec
));
386 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
387 bfd_get_section_name (bsec
->owner
, bsec
));
390 case by_name_alignment
:
391 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
392 bfd_get_section_name (bsec
->owner
, bsec
));
398 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
399 - bfd_section_alignment (asec
->owner
, asec
));
406 /* Build a Binary Search Tree to sort sections, unlike insertion sort
407 used in wild_sort(). BST is considerably faster if the number of
408 of sections are large. */
410 static lang_section_bst_type
**
411 wild_sort_fast (lang_wild_statement_type
*wild
,
412 struct wildcard_list
*sec
,
413 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
416 lang_section_bst_type
**tree
;
419 if (!wild
->filenames_sorted
420 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
422 /* Append at the right end of tree. */
424 tree
= &((*tree
)->right
);
430 /* Find the correct node to append this section. */
431 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
432 tree
= &((*tree
)->left
);
434 tree
= &((*tree
)->right
);
440 /* Use wild_sort_fast to build a BST to sort sections. */
443 output_section_callback_fast (lang_wild_statement_type
*ptr
,
444 struct wildcard_list
*sec
,
446 lang_input_statement_type
*file
,
447 void *output ATTRIBUTE_UNUSED
)
449 lang_section_bst_type
*node
;
450 lang_section_bst_type
**tree
;
452 if (unique_section_p (section
))
455 node
= xmalloc (sizeof (lang_section_bst_type
));
458 node
->section
= section
;
460 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
465 /* Convert a sorted sections' BST back to list form. */
468 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
469 lang_section_bst_type
*tree
,
473 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
475 lang_add_section (&ptr
->children
, tree
->section
,
476 (lang_output_section_statement_type
*) output
);
479 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
484 /* Specialized, optimized routines for handling different kinds of
488 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
489 lang_input_statement_type
*file
,
493 /* We can just do a hash lookup for the section with the right name.
494 But if that lookup discovers more than one section with the name
495 (should be rare), we fall back to the general algorithm because
496 we would otherwise have to sort the sections to make sure they
497 get processed in the bfd's order. */
498 bfd_boolean multiple_sections_found
;
499 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
500 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
502 if (multiple_sections_found
)
503 walk_wild_section_general (ptr
, file
, callback
, data
);
505 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
509 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
510 lang_input_statement_type
*file
,
515 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
517 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
519 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
520 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
523 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
528 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
529 lang_input_statement_type
*file
,
534 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
535 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
536 bfd_boolean multiple_sections_found
;
537 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
539 if (multiple_sections_found
)
541 walk_wild_section_general (ptr
, file
, callback
, data
);
545 /* Note that if the section was not found, s0 is NULL and
546 we'll simply never succeed the s == s0 test below. */
547 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
549 /* Recall that in this code path, a section cannot satisfy more
550 than one spec, so if s == s0 then it cannot match
553 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
556 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
557 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
560 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
567 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
568 lang_input_statement_type
*file
,
573 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
574 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
575 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
576 bfd_boolean multiple_sections_found
;
577 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
579 if (multiple_sections_found
)
581 walk_wild_section_general (ptr
, file
, callback
, data
);
585 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
588 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
591 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
592 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
595 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
598 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
600 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
608 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
609 lang_input_statement_type
*file
,
614 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
615 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
616 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
617 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
618 bfd_boolean multiple_sections_found
;
619 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
621 if (multiple_sections_found
)
623 walk_wild_section_general (ptr
, file
, callback
, data
);
627 s1
= find_section (file
, sec1
, &multiple_sections_found
);
628 if (multiple_sections_found
)
630 walk_wild_section_general (ptr
, file
, callback
, data
);
634 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
637 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
640 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
643 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
644 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
648 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
652 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
654 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
662 walk_wild_section (lang_wild_statement_type
*ptr
,
663 lang_input_statement_type
*file
,
667 if (file
->just_syms_flag
)
670 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
673 /* Returns TRUE when name1 is a wildcard spec that might match
674 something name2 can match. We're conservative: we return FALSE
675 only if the prefixes of name1 and name2 are different up to the
676 first wildcard character. */
679 wild_spec_can_overlap (const char *name1
, const char *name2
)
681 size_t prefix1_len
= strcspn (name1
, "?*[");
682 size_t prefix2_len
= strcspn (name2
, "?*[");
683 size_t min_prefix_len
;
685 /* Note that if there is no wildcard character, then we treat the
686 terminating 0 as part of the prefix. Thus ".text" won't match
687 ".text." or ".text.*", for example. */
688 if (name1
[prefix1_len
] == '\0')
690 if (name2
[prefix2_len
] == '\0')
693 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
695 return memcmp (name1
, name2
, min_prefix_len
) == 0;
698 /* Select specialized code to handle various kinds of wildcard
702 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
705 int wild_name_count
= 0;
706 struct wildcard_list
*sec
;
710 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
711 ptr
->handler_data
[0] = NULL
;
712 ptr
->handler_data
[1] = NULL
;
713 ptr
->handler_data
[2] = NULL
;
714 ptr
->handler_data
[3] = NULL
;
717 /* Count how many wildcard_specs there are, and how many of those
718 actually use wildcards in the name. Also, bail out if any of the
719 wildcard names are NULL. (Can this actually happen?
720 walk_wild_section used to test for it.) And bail out if any
721 of the wildcards are more complex than a simple string
722 ending in a single '*'. */
723 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
726 if (sec
->spec
.name
== NULL
)
728 if (wildcardp (sec
->spec
.name
))
731 if (!is_simple_wild (sec
->spec
.name
))
736 /* The zero-spec case would be easy to optimize but it doesn't
737 happen in practice. Likewise, more than 4 specs doesn't
738 happen in practice. */
739 if (sec_count
== 0 || sec_count
> 4)
742 /* Check that no two specs can match the same section. */
743 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
745 struct wildcard_list
*sec2
;
746 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
748 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
753 signature
= (sec_count
<< 8) + wild_name_count
;
757 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
760 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
763 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
766 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
769 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
775 /* Now fill the data array with pointers to the specs, first the
776 specs with non-wildcard names, then the specs with wildcard
777 names. It's OK to process the specs in different order from the
778 given order, because we've already determined that no section
779 will match more than one spec. */
781 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
782 if (!wildcardp (sec
->spec
.name
))
783 ptr
->handler_data
[data_counter
++] = sec
;
784 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
785 if (wildcardp (sec
->spec
.name
))
786 ptr
->handler_data
[data_counter
++] = sec
;
789 /* Handle a wild statement for a single file F. */
792 walk_wild_file (lang_wild_statement_type
*s
,
793 lang_input_statement_type
*f
,
797 if (f
->the_bfd
== NULL
798 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
799 walk_wild_section (s
, f
, callback
, data
);
804 /* This is an archive file. We must map each member of the
805 archive separately. */
806 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
807 while (member
!= NULL
)
809 /* When lookup_name is called, it will call the add_symbols
810 entry point for the archive. For each element of the
811 archive which is included, BFD will call ldlang_add_file,
812 which will set the usrdata field of the member to the
813 lang_input_statement. */
814 if (member
->usrdata
!= NULL
)
816 walk_wild_section (s
, member
->usrdata
, callback
, data
);
819 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
825 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
827 const char *file_spec
= s
->filename
;
830 if (file_spec
== NULL
)
832 /* Perform the iteration over all files in the list. */
833 LANG_FOR_EACH_INPUT_STATEMENT (f
)
835 walk_wild_file (s
, f
, callback
, data
);
838 else if ((p
= archive_path (file_spec
)) != NULL
)
840 LANG_FOR_EACH_INPUT_STATEMENT (f
)
842 if (input_statement_is_archive_path (file_spec
, p
, f
))
843 walk_wild_file (s
, f
, callback
, data
);
846 else if (wildcardp (file_spec
))
848 LANG_FOR_EACH_INPUT_STATEMENT (f
)
850 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
851 walk_wild_file (s
, f
, callback
, data
);
856 lang_input_statement_type
*f
;
858 /* Perform the iteration over a single file. */
859 f
= lookup_name (file_spec
);
861 walk_wild_file (s
, f
, callback
, data
);
865 /* lang_for_each_statement walks the parse tree and calls the provided
866 function for each node. */
869 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
870 lang_statement_union_type
*s
)
872 for (; s
!= NULL
; s
= s
->header
.next
)
876 switch (s
->header
.type
)
878 case lang_constructors_statement_enum
:
879 lang_for_each_statement_worker (func
, constructor_list
.head
);
881 case lang_output_section_statement_enum
:
882 lang_for_each_statement_worker
883 (func
, s
->output_section_statement
.children
.head
);
885 case lang_wild_statement_enum
:
886 lang_for_each_statement_worker (func
,
887 s
->wild_statement
.children
.head
);
889 case lang_group_statement_enum
:
890 lang_for_each_statement_worker (func
,
891 s
->group_statement
.children
.head
);
893 case lang_data_statement_enum
:
894 case lang_reloc_statement_enum
:
895 case lang_object_symbols_statement_enum
:
896 case lang_output_statement_enum
:
897 case lang_target_statement_enum
:
898 case lang_input_section_enum
:
899 case lang_input_statement_enum
:
900 case lang_assignment_statement_enum
:
901 case lang_padding_statement_enum
:
902 case lang_address_statement_enum
:
903 case lang_fill_statement_enum
:
904 case lang_insert_statement_enum
:
914 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
916 lang_for_each_statement_worker (func
, statement_list
.head
);
919 /*----------------------------------------------------------------------*/
922 lang_list_init (lang_statement_list_type
*list
)
925 list
->tail
= &list
->head
;
929 push_stat_ptr (lang_statement_list_type
*new_ptr
)
931 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
933 *stat_save_ptr
++ = stat_ptr
;
940 if (stat_save_ptr
<= stat_save
)
942 stat_ptr
= *--stat_save_ptr
;
945 /* Build a new statement node for the parse tree. */
947 static lang_statement_union_type
*
948 new_statement (enum statement_enum type
,
950 lang_statement_list_type
*list
)
952 lang_statement_union_type
*new;
954 new = stat_alloc (size
);
955 new->header
.type
= type
;
956 new->header
.next
= NULL
;
957 lang_statement_append (list
, new, &new->header
.next
);
961 /* Build a new input file node for the language. There are several
962 ways in which we treat an input file, eg, we only look at symbols,
963 or prefix it with a -l etc.
965 We can be supplied with requests for input files more than once;
966 they may, for example be split over several lines like foo.o(.text)
967 foo.o(.data) etc, so when asked for a file we check that we haven't
968 got it already so we don't duplicate the bfd. */
970 static lang_input_statement_type
*
971 new_afile (const char *name
,
972 lang_input_file_enum_type file_type
,
974 bfd_boolean add_to_list
)
976 lang_input_statement_type
*p
;
979 p
= new_stat (lang_input_statement
, stat_ptr
);
982 p
= stat_alloc (sizeof (lang_input_statement_type
));
983 p
->header
.type
= lang_input_statement_enum
;
984 p
->header
.next
= NULL
;
987 lang_has_input_file
= TRUE
;
989 p
->sysrooted
= FALSE
;
991 if (file_type
== lang_input_file_is_l_enum
992 && name
[0] == ':' && name
[1] != '\0')
994 file_type
= lang_input_file_is_search_file_enum
;
1000 case lang_input_file_is_symbols_only_enum
:
1002 p
->is_archive
= FALSE
;
1004 p
->local_sym_name
= name
;
1005 p
->just_syms_flag
= TRUE
;
1006 p
->search_dirs_flag
= FALSE
;
1008 case lang_input_file_is_fake_enum
:
1010 p
->is_archive
= FALSE
;
1012 p
->local_sym_name
= name
;
1013 p
->just_syms_flag
= FALSE
;
1014 p
->search_dirs_flag
= FALSE
;
1016 case lang_input_file_is_l_enum
:
1017 p
->is_archive
= TRUE
;
1020 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1021 p
->just_syms_flag
= FALSE
;
1022 p
->search_dirs_flag
= TRUE
;
1024 case lang_input_file_is_marker_enum
:
1026 p
->is_archive
= FALSE
;
1028 p
->local_sym_name
= name
;
1029 p
->just_syms_flag
= FALSE
;
1030 p
->search_dirs_flag
= TRUE
;
1032 case lang_input_file_is_search_file_enum
:
1033 p
->sysrooted
= ldlang_sysrooted_script
;
1035 p
->is_archive
= FALSE
;
1037 p
->local_sym_name
= name
;
1038 p
->just_syms_flag
= FALSE
;
1039 p
->search_dirs_flag
= TRUE
;
1041 case lang_input_file_is_file_enum
:
1043 p
->is_archive
= FALSE
;
1045 p
->local_sym_name
= name
;
1046 p
->just_syms_flag
= FALSE
;
1047 p
->search_dirs_flag
= FALSE
;
1053 p
->next_real_file
= NULL
;
1055 p
->dynamic
= config
.dynamic_link
;
1056 p
->add_needed
= add_needed
;
1057 p
->as_needed
= as_needed
;
1058 p
->whole_archive
= whole_archive
;
1060 lang_statement_append (&input_file_chain
,
1061 (lang_statement_union_type
*) p
,
1062 &p
->next_real_file
);
1066 lang_input_statement_type
*
1067 lang_add_input_file (const char *name
,
1068 lang_input_file_enum_type file_type
,
1071 return new_afile (name
, file_type
, target
, TRUE
);
1074 struct out_section_hash_entry
1076 struct bfd_hash_entry root
;
1077 lang_statement_union_type s
;
1080 /* The hash table. */
1082 static struct bfd_hash_table output_section_statement_table
;
1084 /* Support routines for the hash table used by lang_output_section_find,
1085 initialize the table, fill in an entry and remove the table. */
1087 static struct bfd_hash_entry
*
1088 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1089 struct bfd_hash_table
*table
,
1092 lang_output_section_statement_type
**nextp
;
1093 struct out_section_hash_entry
*ret
;
1097 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
1102 entry
= bfd_hash_newfunc (entry
, table
, string
);
1106 ret
= (struct out_section_hash_entry
*) entry
;
1107 memset (&ret
->s
, 0, sizeof (ret
->s
));
1108 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1109 ret
->s
.output_section_statement
.subsection_alignment
= -1;
1110 ret
->s
.output_section_statement
.section_alignment
= -1;
1111 ret
->s
.output_section_statement
.block_value
= 1;
1112 lang_list_init (&ret
->s
.output_section_statement
.children
);
1113 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1115 /* For every output section statement added to the list, except the
1116 first one, lang_output_section_statement.tail points to the "next"
1117 field of the last element of the list. */
1118 if (lang_output_section_statement
.head
!= NULL
)
1119 ret
->s
.output_section_statement
.prev
1120 = ((lang_output_section_statement_type
*)
1121 ((char *) lang_output_section_statement
.tail
1122 - offsetof (lang_output_section_statement_type
, next
)));
1124 /* GCC's strict aliasing rules prevent us from just casting the
1125 address, so we store the pointer in a variable and cast that
1127 nextp
= &ret
->s
.output_section_statement
.next
;
1128 lang_statement_append (&lang_output_section_statement
,
1130 (lang_statement_union_type
**) nextp
);
1135 output_section_statement_table_init (void)
1137 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1138 output_section_statement_newfunc
,
1139 sizeof (struct out_section_hash_entry
),
1141 einfo (_("%P%F: can not create hash table: %E\n"));
1145 output_section_statement_table_free (void)
1147 bfd_hash_table_free (&output_section_statement_table
);
1150 /* Build enough state so that the parser can build its tree. */
1155 obstack_begin (&stat_obstack
, 1000);
1157 stat_ptr
= &statement_list
;
1159 output_section_statement_table_init ();
1161 lang_list_init (stat_ptr
);
1163 lang_list_init (&input_file_chain
);
1164 lang_list_init (&lang_output_section_statement
);
1165 lang_list_init (&file_chain
);
1166 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1168 abs_output_section
=
1169 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, TRUE
);
1171 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1173 /* The value "3" is ad-hoc, somewhat related to the expected number of
1174 DEFINED expressions in a linker script. For most default linker
1175 scripts, there are none. Why a hash table then? Well, it's somewhat
1176 simpler to re-use working machinery than using a linked list in terms
1177 of code-complexity here in ld, besides the initialization which just
1178 looks like other code here. */
1179 if (!bfd_hash_table_init_n (&lang_definedness_table
,
1180 lang_definedness_newfunc
,
1181 sizeof (struct lang_definedness_hash_entry
),
1183 einfo (_("%P%F: can not create hash table: %E\n"));
1189 output_section_statement_table_free ();
1192 /*----------------------------------------------------------------------
1193 A region is an area of memory declared with the
1194 MEMORY { name:org=exp, len=exp ... }
1197 We maintain a list of all the regions here.
1199 If no regions are specified in the script, then the default is used
1200 which is created when looked up to be the entire data space.
1202 If create is true we are creating a region inside a MEMORY block.
1203 In this case it is probably an error to create a region that has
1204 already been created. If we are not inside a MEMORY block it is
1205 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1206 and so we issue a warning.
1208 Each region has at least one name. The first name is either
1209 DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
1210 alias names to an existing region within a script with
1211 REGION_ALIAS (alias, region_name). Each name corresponds to at most one
1214 static lang_memory_region_type
*lang_memory_region_list
;
1215 static lang_memory_region_type
**lang_memory_region_list_tail
1216 = &lang_memory_region_list
;
1218 lang_memory_region_type
*
1219 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1221 lang_memory_region_name
*n
;
1222 lang_memory_region_type
*r
;
1223 lang_memory_region_type
*new;
1225 /* NAME is NULL for LMA memspecs if no region was specified. */
1229 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1230 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1231 if (strcmp (n
->name
, name
) == 0)
1234 einfo (_("%P:%S: warning: redeclaration of memory region `%s'\n"),
1239 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1240 einfo (_("%P:%S: warning: memory region `%s' not declared\n"), name
);
1242 new = stat_alloc (sizeof (lang_memory_region_type
));
1244 new->name_list
.name
= xstrdup (name
);
1245 new->name_list
.next
= NULL
;
1248 new->length
= ~(bfd_size_type
) 0;
1250 new->last_os
= NULL
;
1253 new->had_full_message
= FALSE
;
1255 *lang_memory_region_list_tail
= new;
1256 lang_memory_region_list_tail
= &new->next
;
1262 lang_memory_region_alias (const char * alias
, const char * region_name
)
1264 lang_memory_region_name
* n
;
1265 lang_memory_region_type
* r
;
1266 lang_memory_region_type
* region
;
1268 /* The default region must be unique. This ensures that it is not necessary
1269 to iterate through the name list if someone wants the check if a region is
1270 the default memory region. */
1271 if (strcmp (region_name
, DEFAULT_MEMORY_REGION
) == 0
1272 || strcmp (alias
, DEFAULT_MEMORY_REGION
) == 0)
1273 einfo (_("%F%P:%S: error: alias for default memory region\n"));
1275 /* Look for the target region and check if the alias is not already
1278 for (r
= lang_memory_region_list
; r
!= NULL
; r
= r
->next
)
1279 for (n
= &r
->name_list
; n
!= NULL
; n
= n
->next
)
1281 if (region
== NULL
&& strcmp (n
->name
, region_name
) == 0)
1283 if (strcmp (n
->name
, alias
) == 0)
1284 einfo (_("%F%P:%S: error: redefinition of memory region "
1289 /* Check if the target region exists. */
1291 einfo (_("%F%P:%S: error: memory region `%s' "
1292 "for alias `%s' does not exist\n"),
1296 /* Add alias to region name list. */
1297 n
= stat_alloc (sizeof (lang_memory_region_name
));
1298 n
->name
= xstrdup (alias
);
1299 n
->next
= region
->name_list
.next
;
1300 region
->name_list
.next
= n
;
1303 static lang_memory_region_type
*
1304 lang_memory_default (asection
* section
)
1306 lang_memory_region_type
*p
;
1308 flagword sec_flags
= section
->flags
;
1310 /* Override SEC_DATA to mean a writable section. */
1311 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1312 sec_flags
|= SEC_DATA
;
1314 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1316 if ((p
->flags
& sec_flags
) != 0
1317 && (p
->not_flags
& sec_flags
) == 0)
1322 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1325 /* Find or create an output_section_statement with the given NAME.
1326 If CONSTRAINT is non-zero match one with that constraint, otherwise
1327 match any non-negative constraint. If CREATE, always make a
1328 new output_section_statement for SPECIAL CONSTRAINT. */
1330 lang_output_section_statement_type
*
1331 lang_output_section_statement_lookup (const char *name
,
1335 struct out_section_hash_entry
*entry
;
1337 entry
= ((struct out_section_hash_entry
*)
1338 bfd_hash_lookup (&output_section_statement_table
, name
,
1343 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1347 if (entry
->s
.output_section_statement
.name
!= NULL
)
1349 /* We have a section of this name, but it might not have the correct
1351 struct out_section_hash_entry
*last_ent
;
1353 name
= entry
->s
.output_section_statement
.name
;
1354 if (create
&& constraint
== SPECIAL
)
1355 /* Not traversing to the end reverses the order of the second
1356 and subsequent SPECIAL sections in the hash table chain,
1357 but that shouldn't matter. */
1362 if (constraint
== entry
->s
.output_section_statement
.constraint
1364 && entry
->s
.output_section_statement
.constraint
>= 0))
1365 return &entry
->s
.output_section_statement
;
1367 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1369 while (entry
!= NULL
1370 && name
== entry
->s
.output_section_statement
.name
);
1376 = ((struct out_section_hash_entry
*)
1377 output_section_statement_newfunc (NULL
,
1378 &output_section_statement_table
,
1382 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1385 entry
->root
= last_ent
->root
;
1386 last_ent
->root
.next
= &entry
->root
;
1389 entry
->s
.output_section_statement
.name
= name
;
1390 entry
->s
.output_section_statement
.constraint
= constraint
;
1391 return &entry
->s
.output_section_statement
;
1394 /* Find the next output_section_statement with the same name as OS.
1395 If CONSTRAINT is non-zero, find one with that constraint otherwise
1396 match any non-negative constraint. */
1398 lang_output_section_statement_type
*
1399 next_matching_output_section_statement (lang_output_section_statement_type
*os
,
1402 /* All output_section_statements are actually part of a
1403 struct out_section_hash_entry. */
1404 struct out_section_hash_entry
*entry
= (struct out_section_hash_entry
*)
1406 - offsetof (struct out_section_hash_entry
, s
.output_section_statement
));
1407 const char *name
= os
->name
;
1409 ASSERT (name
== entry
->root
.string
);
1412 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1414 || name
!= entry
->s
.output_section_statement
.name
)
1417 while (constraint
!= entry
->s
.output_section_statement
.constraint
1419 || entry
->s
.output_section_statement
.constraint
< 0));
1421 return &entry
->s
.output_section_statement
;
1424 /* A variant of lang_output_section_find used by place_orphan.
1425 Returns the output statement that should precede a new output
1426 statement for SEC. If an exact match is found on certain flags,
1429 lang_output_section_statement_type
*
1430 lang_output_section_find_by_flags (const asection
*sec
,
1431 lang_output_section_statement_type
**exact
,
1432 lang_match_sec_type_func match_type
)
1434 lang_output_section_statement_type
*first
, *look
, *found
;
1437 /* We know the first statement on this list is *ABS*. May as well
1439 first
= &lang_output_section_statement
.head
->output_section_statement
;
1440 first
= first
->next
;
1442 /* First try for an exact match. */
1444 for (look
= first
; look
; look
= look
->next
)
1446 flags
= look
->flags
;
1447 if (look
->bfd_section
!= NULL
)
1449 flags
= look
->bfd_section
->flags
;
1450 if (match_type
&& !match_type (link_info
.output_bfd
,
1455 flags
^= sec
->flags
;
1456 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1457 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1467 if ((sec
->flags
& SEC_CODE
) != 0
1468 && (sec
->flags
& SEC_ALLOC
) != 0)
1470 /* Try for a rw code section. */
1471 for (look
= first
; look
; look
= look
->next
)
1473 flags
= look
->flags
;
1474 if (look
->bfd_section
!= NULL
)
1476 flags
= look
->bfd_section
->flags
;
1477 if (match_type
&& !match_type (link_info
.output_bfd
,
1482 flags
^= sec
->flags
;
1483 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1484 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1488 else if ((sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
)) != 0
1489 && (sec
->flags
& SEC_ALLOC
) != 0)
1491 /* .rodata can go after .text, .sdata2 after .rodata. */
1492 for (look
= first
; look
; look
= look
->next
)
1494 flags
= look
->flags
;
1495 if (look
->bfd_section
!= NULL
)
1497 flags
= look
->bfd_section
->flags
;
1498 if (match_type
&& !match_type (link_info
.output_bfd
,
1503 flags
^= sec
->flags
;
1504 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1506 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1510 else if ((sec
->flags
& SEC_SMALL_DATA
) != 0
1511 && (sec
->flags
& SEC_ALLOC
) != 0)
1513 /* .sdata goes after .data, .sbss after .sdata. */
1514 for (look
= first
; look
; look
= look
->next
)
1516 flags
= look
->flags
;
1517 if (look
->bfd_section
!= NULL
)
1519 flags
= look
->bfd_section
->flags
;
1520 if (match_type
&& !match_type (link_info
.output_bfd
,
1525 flags
^= sec
->flags
;
1526 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1527 | SEC_THREAD_LOCAL
))
1528 || ((look
->flags
& SEC_SMALL_DATA
)
1529 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1533 else if ((sec
->flags
& SEC_HAS_CONTENTS
) != 0
1534 && (sec
->flags
& SEC_ALLOC
) != 0)
1536 /* .data goes after .rodata. */
1537 for (look
= first
; look
; look
= look
->next
)
1539 flags
= look
->flags
;
1540 if (look
->bfd_section
!= NULL
)
1542 flags
= look
->bfd_section
->flags
;
1543 if (match_type
&& !match_type (link_info
.output_bfd
,
1548 flags
^= sec
->flags
;
1549 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1550 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1554 else if ((sec
->flags
& SEC_ALLOC
) != 0)
1556 /* .bss goes after any other alloc section. */
1557 for (look
= first
; look
; look
= look
->next
)
1559 flags
= look
->flags
;
1560 if (look
->bfd_section
!= NULL
)
1562 flags
= look
->bfd_section
->flags
;
1563 if (match_type
&& !match_type (link_info
.output_bfd
,
1568 flags
^= sec
->flags
;
1569 if (!(flags
& SEC_ALLOC
))
1575 /* non-alloc go last. */
1576 for (look
= first
; look
; look
= look
->next
)
1578 flags
= look
->flags
;
1579 if (look
->bfd_section
!= NULL
)
1580 flags
= look
->bfd_section
->flags
;
1581 flags
^= sec
->flags
;
1582 if (!(flags
& SEC_DEBUGGING
))
1588 if (found
|| !match_type
)
1591 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1594 /* Find the last output section before given output statement.
1595 Used by place_orphan. */
1598 output_prev_sec_find (lang_output_section_statement_type
*os
)
1600 lang_output_section_statement_type
*lookup
;
1602 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1604 if (lookup
->constraint
< 0)
1607 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1608 return lookup
->bfd_section
;
1614 /* Look for a suitable place for a new output section statement. The
1615 idea is to skip over anything that might be inside a SECTIONS {}
1616 statement in a script, before we find another output section
1617 statement. Assignments to "dot" before an output section statement
1618 are assumed to belong to it, except in two cases; The first
1619 assignment to dot, and assignments before non-alloc sections.
1620 Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
1621 similar assignments that set the initial address, or we might
1622 insert non-alloc note sections among assignments setting end of
1625 static lang_statement_union_type
**
1626 insert_os_after (lang_output_section_statement_type
*after
)
1628 lang_statement_union_type
**where
;
1629 lang_statement_union_type
**assign
= NULL
;
1630 bfd_boolean ignore_first
;
1633 = after
== &lang_output_section_statement
.head
->output_section_statement
;
1635 for (where
= &after
->header
.next
;
1637 where
= &(*where
)->header
.next
)
1639 switch ((*where
)->header
.type
)
1641 case lang_assignment_statement_enum
:
1644 lang_assignment_statement_type
*ass
;
1646 ass
= &(*where
)->assignment_statement
;
1647 if (ass
->exp
->type
.node_class
!= etree_assert
1648 && ass
->exp
->assign
.dst
[0] == '.'
1649 && ass
->exp
->assign
.dst
[1] == 0
1653 ignore_first
= FALSE
;
1655 case lang_wild_statement_enum
:
1656 case lang_input_section_enum
:
1657 case lang_object_symbols_statement_enum
:
1658 case lang_fill_statement_enum
:
1659 case lang_data_statement_enum
:
1660 case lang_reloc_statement_enum
:
1661 case lang_padding_statement_enum
:
1662 case lang_constructors_statement_enum
:
1665 case lang_output_section_statement_enum
:
1668 asection
*s
= (*where
)->output_section_statement
.bfd_section
;
1671 || s
->map_head
.s
== NULL
1672 || (s
->flags
& SEC_ALLOC
) != 0)
1676 case lang_input_statement_enum
:
1677 case lang_address_statement_enum
:
1678 case lang_target_statement_enum
:
1679 case lang_output_statement_enum
:
1680 case lang_group_statement_enum
:
1681 case lang_insert_statement_enum
:
1690 lang_output_section_statement_type
*
1691 lang_insert_orphan (asection
*s
,
1692 const char *secname
,
1694 lang_output_section_statement_type
*after
,
1695 struct orphan_save
*place
,
1696 etree_type
*address
,
1697 lang_statement_list_type
*add_child
)
1699 lang_statement_list_type add
;
1701 lang_output_section_statement_type
*os
;
1702 lang_output_section_statement_type
**os_tail
;
1704 /* If we have found an appropriate place for the output section
1705 statements for this orphan, add them to our own private list,
1706 inserting them later into the global statement list. */
1709 lang_list_init (&add
);
1710 push_stat_ptr (&add
);
1713 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1714 address
= exp_intop (0);
1716 os_tail
= ((lang_output_section_statement_type
**)
1717 lang_output_section_statement
.tail
);
1718 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1722 if (config
.build_constructors
&& *os_tail
== os
)
1724 /* If the name of the section is representable in C, then create
1725 symbols to mark the start and the end of the section. */
1726 for (ps
= secname
; *ps
!= '\0'; ps
++)
1727 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1732 etree_type
*e_align
;
1734 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1735 symname
[0] = bfd_get_symbol_leading_char (link_info
.output_bfd
);
1736 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1737 e_align
= exp_unop (ALIGN_K
,
1738 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1739 lang_add_assignment (exp_assop ('=', ".", e_align
));
1740 lang_add_assignment (exp_provide (symname
,
1742 exp_nameop (NAME
, ".")),
1747 if (add_child
== NULL
)
1748 add_child
= &os
->children
;
1749 lang_add_section (add_child
, s
, os
);
1751 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1753 if (ps
!= NULL
&& *ps
== '\0')
1757 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1758 symname
[0] = bfd_get_symbol_leading_char (link_info
.output_bfd
);
1759 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1760 lang_add_assignment (exp_provide (symname
,
1761 exp_nameop (NAME
, "."),
1765 /* Restore the global list pointer. */
1769 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1771 asection
*snew
, *as
;
1773 snew
= os
->bfd_section
;
1775 /* Shuffle the bfd section list to make the output file look
1776 neater. This is really only cosmetic. */
1777 if (place
->section
== NULL
1778 && after
!= (&lang_output_section_statement
.head
1779 ->output_section_statement
))
1781 asection
*bfd_section
= after
->bfd_section
;
1783 /* If the output statement hasn't been used to place any input
1784 sections (and thus doesn't have an output bfd_section),
1785 look for the closest prior output statement having an
1787 if (bfd_section
== NULL
)
1788 bfd_section
= output_prev_sec_find (after
);
1790 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1791 place
->section
= &bfd_section
->next
;
1794 if (place
->section
== NULL
)
1795 place
->section
= &link_info
.output_bfd
->sections
;
1797 as
= *place
->section
;
1801 /* Put the section at the end of the list. */
1803 /* Unlink the section. */
1804 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1806 /* Now tack it back on in the right place. */
1807 bfd_section_list_append (link_info
.output_bfd
, snew
);
1809 else if (as
!= snew
&& as
->prev
!= snew
)
1811 /* Unlink the section. */
1812 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1814 /* Now tack it back on in the right place. */
1815 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
1818 /* Save the end of this list. Further ophans of this type will
1819 follow the one we've just added. */
1820 place
->section
= &snew
->next
;
1822 /* The following is non-cosmetic. We try to put the output
1823 statements in some sort of reasonable order here, because they
1824 determine the final load addresses of the orphan sections.
1825 In addition, placing output statements in the wrong order may
1826 require extra segments. For instance, given a typical
1827 situation of all read-only sections placed in one segment and
1828 following that a segment containing all the read-write
1829 sections, we wouldn't want to place an orphan read/write
1830 section before or amongst the read-only ones. */
1831 if (add
.head
!= NULL
)
1833 lang_output_section_statement_type
*newly_added_os
;
1835 if (place
->stmt
== NULL
)
1837 lang_statement_union_type
**where
= insert_os_after (after
);
1842 place
->os_tail
= &after
->next
;
1846 /* Put it after the last orphan statement we added. */
1847 *add
.tail
= *place
->stmt
;
1848 *place
->stmt
= add
.head
;
1851 /* Fix the global list pointer if we happened to tack our
1852 new list at the tail. */
1853 if (*stat_ptr
->tail
== add
.head
)
1854 stat_ptr
->tail
= add
.tail
;
1856 /* Save the end of this list. */
1857 place
->stmt
= add
.tail
;
1859 /* Do the same for the list of output section statements. */
1860 newly_added_os
= *os_tail
;
1862 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1863 ((char *) place
->os_tail
1864 - offsetof (lang_output_section_statement_type
, next
));
1865 newly_added_os
->next
= *place
->os_tail
;
1866 if (newly_added_os
->next
!= NULL
)
1867 newly_added_os
->next
->prev
= newly_added_os
;
1868 *place
->os_tail
= newly_added_os
;
1869 place
->os_tail
= &newly_added_os
->next
;
1871 /* Fixing the global list pointer here is a little different.
1872 We added to the list in lang_enter_output_section_statement,
1873 trimmed off the new output_section_statment above when
1874 assigning *os_tail = NULL, but possibly added it back in
1875 the same place when assigning *place->os_tail. */
1876 if (*os_tail
== NULL
)
1877 lang_output_section_statement
.tail
1878 = (lang_statement_union_type
**) os_tail
;
1885 lang_map_flags (flagword flag
)
1887 if (flag
& SEC_ALLOC
)
1890 if (flag
& SEC_CODE
)
1893 if (flag
& SEC_READONLY
)
1896 if (flag
& SEC_DATA
)
1899 if (flag
& SEC_LOAD
)
1906 lang_memory_region_type
*m
;
1907 bfd_boolean dis_header_printed
= FALSE
;
1910 LANG_FOR_EACH_INPUT_STATEMENT (file
)
1914 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
1915 || file
->just_syms_flag
)
1918 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1919 if ((s
->output_section
== NULL
1920 || s
->output_section
->owner
!= link_info
.output_bfd
)
1921 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
1923 if (! dis_header_printed
)
1925 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
1926 dis_header_printed
= TRUE
;
1929 print_input_section (s
, TRUE
);
1933 minfo (_("\nMemory Configuration\n\n"));
1934 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1935 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1937 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1942 fprintf (config
.map_file
, "%-16s ", m
->name_list
.name
);
1944 sprintf_vma (buf
, m
->origin
);
1945 minfo ("0x%s ", buf
);
1953 minfo ("0x%V", m
->length
);
1954 if (m
->flags
|| m
->not_flags
)
1962 lang_map_flags (m
->flags
);
1968 lang_map_flags (m
->not_flags
);
1975 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1977 if (! link_info
.reduce_memory_overheads
)
1979 obstack_begin (&map_obstack
, 1000);
1980 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1981 bfd_map_over_sections (p
, init_map_userdata
, 0);
1982 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1984 lang_statement_iteration
++;
1985 print_statements ();
1989 init_map_userdata (bfd
*abfd ATTRIBUTE_UNUSED
,
1991 void *data ATTRIBUTE_UNUSED
)
1993 fat_section_userdata_type
*new_data
1994 = ((fat_section_userdata_type
*) (stat_alloc
1995 (sizeof (fat_section_userdata_type
))));
1997 ASSERT (get_userdata (sec
) == NULL
);
1998 get_userdata (sec
) = new_data
;
1999 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
2000 new_data
->map_symbol_def_count
= 0;
2004 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
2005 void *info ATTRIBUTE_UNUSED
)
2007 if (hash_entry
->type
== bfd_link_hash_defined
2008 || hash_entry
->type
== bfd_link_hash_defweak
)
2010 struct fat_user_section_struct
*ud
;
2011 struct map_symbol_def
*def
;
2013 ud
= get_userdata (hash_entry
->u
.def
.section
);
2016 /* ??? What do we have to do to initialize this beforehand? */
2017 /* The first time we get here is bfd_abs_section... */
2018 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
2019 ud
= get_userdata (hash_entry
->u
.def
.section
);
2021 else if (!ud
->map_symbol_def_tail
)
2022 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
2024 def
= obstack_alloc (&map_obstack
, sizeof *def
);
2025 def
->entry
= hash_entry
;
2026 *(ud
->map_symbol_def_tail
) = def
;
2027 ud
->map_symbol_def_tail
= &def
->next
;
2028 ud
->map_symbol_def_count
++;
2033 /* Initialize an output section. */
2036 init_os (lang_output_section_statement_type
*s
, asection
*isec
,
2039 if (s
->bfd_section
!= NULL
)
2042 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
2043 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
2045 if (s
->constraint
!= SPECIAL
)
2046 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
2047 if (s
->bfd_section
== NULL
)
2048 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
2050 if (s
->bfd_section
== NULL
)
2052 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
2053 link_info
.output_bfd
->xvec
->name
, s
->name
);
2055 s
->bfd_section
->output_section
= s
->bfd_section
;
2056 s
->bfd_section
->output_offset
= 0;
2058 if (!link_info
.reduce_memory_overheads
)
2060 fat_section_userdata_type
*new
2061 = stat_alloc (sizeof (fat_section_userdata_type
));
2062 memset (new, 0, sizeof (fat_section_userdata_type
));
2063 get_userdata (s
->bfd_section
) = new;
2066 /* If there is a base address, make sure that any sections it might
2067 mention are initialized. */
2068 if (s
->addr_tree
!= NULL
)
2069 exp_init_os (s
->addr_tree
);
2071 if (s
->load_base
!= NULL
)
2072 exp_init_os (s
->load_base
);
2074 /* If supplied an alignment, set it. */
2075 if (s
->section_alignment
!= -1)
2076 s
->bfd_section
->alignment_power
= s
->section_alignment
;
2079 bfd_init_private_section_data (isec
->owner
, isec
,
2080 link_info
.output_bfd
, s
->bfd_section
,
2084 /* Make sure that all output sections mentioned in an expression are
2088 exp_init_os (etree_type
*exp
)
2090 switch (exp
->type
.node_class
)
2094 exp_init_os (exp
->assign
.src
);
2098 exp_init_os (exp
->binary
.lhs
);
2099 exp_init_os (exp
->binary
.rhs
);
2103 exp_init_os (exp
->trinary
.cond
);
2104 exp_init_os (exp
->trinary
.lhs
);
2105 exp_init_os (exp
->trinary
.rhs
);
2109 exp_init_os (exp
->assert_s
.child
);
2113 exp_init_os (exp
->unary
.child
);
2117 switch (exp
->type
.node_code
)
2123 lang_output_section_statement_type
*os
;
2125 os
= lang_output_section_find (exp
->name
.name
);
2126 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2127 init_os (os
, NULL
, 0);
2138 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2140 lang_input_statement_type
*entry
= data
;
2142 /* If we are only reading symbols from this object, then we want to
2143 discard all sections. */
2144 if (entry
->just_syms_flag
)
2146 bfd_link_just_syms (abfd
, sec
, &link_info
);
2150 if (!(abfd
->flags
& DYNAMIC
))
2151 bfd_section_already_linked (abfd
, sec
, &link_info
);
2154 /* The wild routines.
2156 These expand statements like *(.text) and foo.o to a list of
2157 explicit actions, like foo.o(.text), bar.o(.text) and
2158 foo.o(.text, .data). */
2160 /* Add SECTION to the output section OUTPUT. Do this by creating a
2161 lang_input_section statement which is placed at PTR. FILE is the
2162 input file which holds SECTION. */
2165 lang_add_section (lang_statement_list_type
*ptr
,
2167 lang_output_section_statement_type
*output
)
2169 flagword flags
= section
->flags
;
2170 bfd_boolean discard
;
2172 /* Discard sections marked with SEC_EXCLUDE. */
2173 discard
= (flags
& SEC_EXCLUDE
) != 0;
2175 /* Discard input sections which are assigned to a section named
2176 DISCARD_SECTION_NAME. */
2177 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2180 /* Discard debugging sections if we are stripping debugging
2182 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2183 && (flags
& SEC_DEBUGGING
) != 0)
2188 if (section
->output_section
== NULL
)
2190 /* This prevents future calls from assigning this section. */
2191 section
->output_section
= bfd_abs_section_ptr
;
2196 if (section
->output_section
== NULL
)
2199 lang_input_section_type
*new;
2202 flags
= section
->flags
;
2204 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2205 to an output section, because we want to be able to include a
2206 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2207 section (I don't know why we want to do this, but we do).
2208 build_link_order in ldwrite.c handles this case by turning
2209 the embedded SEC_NEVER_LOAD section into a fill. */
2211 flags
&= ~ SEC_NEVER_LOAD
;
2213 switch (output
->sectype
)
2215 case normal_section
:
2216 case overlay_section
:
2218 case noalloc_section
:
2219 flags
&= ~SEC_ALLOC
;
2221 case noload_section
:
2223 flags
|= SEC_NEVER_LOAD
;
2227 if (output
->bfd_section
== NULL
)
2228 init_os (output
, section
, flags
);
2230 first
= ! output
->bfd_section
->linker_has_input
;
2231 output
->bfd_section
->linker_has_input
= 1;
2233 if (!link_info
.relocatable
2234 && !stripped_excluded_sections
)
2236 asection
*s
= output
->bfd_section
->map_tail
.s
;
2237 output
->bfd_section
->map_tail
.s
= section
;
2238 section
->map_head
.s
= NULL
;
2239 section
->map_tail
.s
= s
;
2241 s
->map_head
.s
= section
;
2243 output
->bfd_section
->map_head
.s
= section
;
2246 /* Add a section reference to the list. */
2247 new = new_stat (lang_input_section
, ptr
);
2249 new->section
= section
;
2250 section
->output_section
= output
->bfd_section
;
2252 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2253 already been processed. One reason to do this is that on pe
2254 format targets, .text$foo sections go into .text and it's odd
2255 to see .text with SEC_LINK_ONCE set. */
2257 if (! link_info
.relocatable
)
2258 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
2260 /* If this is not the first input section, and the SEC_READONLY
2261 flag is not currently set, then don't set it just because the
2262 input section has it set. */
2264 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
2265 flags
&= ~ SEC_READONLY
;
2267 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2269 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2270 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2271 || ((flags
& SEC_MERGE
)
2272 && output
->bfd_section
->entsize
!= section
->entsize
)))
2274 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2275 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2278 output
->bfd_section
->flags
|= flags
;
2280 if (flags
& SEC_MERGE
)
2281 output
->bfd_section
->entsize
= section
->entsize
;
2283 /* If SEC_READONLY is not set in the input section, then clear
2284 it from the output section. */
2285 if ((section
->flags
& SEC_READONLY
) == 0)
2286 output
->bfd_section
->flags
&= ~SEC_READONLY
;
2288 /* Copy over SEC_SMALL_DATA. */
2289 if (section
->flags
& SEC_SMALL_DATA
)
2290 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
2292 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2293 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2295 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
2296 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
2298 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2299 /* FIXME: This value should really be obtained from the bfd... */
2300 output
->block_value
= 128;
2305 /* Handle wildcard sorting. This returns the lang_input_section which
2306 should follow the one we are going to create for SECTION and FILE,
2307 based on the sorting requirements of WILD. It returns NULL if the
2308 new section should just go at the end of the current list. */
2310 static lang_statement_union_type
*
2311 wild_sort (lang_wild_statement_type
*wild
,
2312 struct wildcard_list
*sec
,
2313 lang_input_statement_type
*file
,
2316 const char *section_name
;
2317 lang_statement_union_type
*l
;
2319 if (!wild
->filenames_sorted
2320 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2323 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2324 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2326 lang_input_section_type
*ls
;
2328 if (l
->header
.type
!= lang_input_section_enum
)
2330 ls
= &l
->input_section
;
2332 /* Sorting by filename takes precedence over sorting by section
2335 if (wild
->filenames_sorted
)
2337 const char *fn
, *ln
;
2341 /* The PE support for the .idata section as generated by
2342 dlltool assumes that files will be sorted by the name of
2343 the archive and then the name of the file within the
2346 if (file
->the_bfd
!= NULL
2347 && bfd_my_archive (file
->the_bfd
) != NULL
)
2349 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2354 fn
= file
->filename
;
2358 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2360 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2365 ln
= ls
->section
->owner
->filename
;
2369 i
= strcmp (fn
, ln
);
2378 fn
= file
->filename
;
2380 ln
= ls
->section
->owner
->filename
;
2382 i
= strcmp (fn
, ln
);
2390 /* Here either the files are not sorted by name, or we are
2391 looking at the sections for this file. */
2393 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2394 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2401 /* Expand a wild statement for a particular FILE. SECTION may be
2402 NULL, in which case it is a wild card. */
2405 output_section_callback (lang_wild_statement_type
*ptr
,
2406 struct wildcard_list
*sec
,
2408 lang_input_statement_type
*file
,
2411 lang_statement_union_type
*before
;
2413 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2414 if (unique_section_p (section
))
2417 before
= wild_sort (ptr
, sec
, file
, section
);
2419 /* Here BEFORE points to the lang_input_section which
2420 should follow the one we are about to add. If BEFORE
2421 is NULL, then the section should just go at the end
2422 of the current list. */
2425 lang_add_section (&ptr
->children
, section
,
2426 (lang_output_section_statement_type
*) output
);
2429 lang_statement_list_type list
;
2430 lang_statement_union_type
**pp
;
2432 lang_list_init (&list
);
2433 lang_add_section (&list
, section
,
2434 (lang_output_section_statement_type
*) output
);
2436 /* If we are discarding the section, LIST.HEAD will
2438 if (list
.head
!= NULL
)
2440 ASSERT (list
.head
->header
.next
== NULL
);
2442 for (pp
= &ptr
->children
.head
;
2444 pp
= &(*pp
)->header
.next
)
2445 ASSERT (*pp
!= NULL
);
2447 list
.head
->header
.next
= *pp
;
2453 /* Check if all sections in a wild statement for a particular FILE
2457 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2458 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2460 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2463 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2464 if (unique_section_p (section
))
2467 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2468 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2471 /* This is passed a file name which must have been seen already and
2472 added to the statement tree. We will see if it has been opened
2473 already and had its symbols read. If not then we'll read it. */
2475 static lang_input_statement_type
*
2476 lookup_name (const char *name
)
2478 lang_input_statement_type
*search
;
2480 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2482 search
= (lang_input_statement_type
*) search
->next_real_file
)
2484 /* Use the local_sym_name as the name of the file that has
2485 already been loaded as filename might have been transformed
2486 via the search directory lookup mechanism. */
2487 const char *filename
= search
->local_sym_name
;
2489 if (filename
!= NULL
2490 && strcmp (filename
, name
) == 0)
2495 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2496 default_target
, FALSE
);
2498 /* If we have already added this file, or this file is not real
2499 don't add this file. */
2500 if (search
->loaded
|| !search
->real
)
2503 if (! load_symbols (search
, NULL
))
2509 /* Save LIST as a list of libraries whose symbols should not be exported. */
2514 struct excluded_lib
*next
;
2516 static struct excluded_lib
*excluded_libs
;
2519 add_excluded_libs (const char *list
)
2521 const char *p
= list
, *end
;
2525 struct excluded_lib
*entry
;
2526 end
= strpbrk (p
, ",:");
2528 end
= p
+ strlen (p
);
2529 entry
= xmalloc (sizeof (*entry
));
2530 entry
->next
= excluded_libs
;
2531 entry
->name
= xmalloc (end
- p
+ 1);
2532 memcpy (entry
->name
, p
, end
- p
);
2533 entry
->name
[end
- p
] = '\0';
2534 excluded_libs
= entry
;
2542 check_excluded_libs (bfd
*abfd
)
2544 struct excluded_lib
*lib
= excluded_libs
;
2548 int len
= strlen (lib
->name
);
2549 const char *filename
= lbasename (abfd
->filename
);
2551 if (strcmp (lib
->name
, "ALL") == 0)
2553 abfd
->no_export
= TRUE
;
2557 if (strncmp (lib
->name
, filename
, len
) == 0
2558 && (filename
[len
] == '\0'
2559 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2560 && filename
[len
+ 2] == '\0')))
2562 abfd
->no_export
= TRUE
;
2570 /* Get the symbols for an input file. */
2573 load_symbols (lang_input_statement_type
*entry
,
2574 lang_statement_list_type
*place
)
2581 ldfile_open_file (entry
);
2583 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2584 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2587 bfd_boolean save_ldlang_sysrooted_script
;
2588 bfd_boolean save_as_needed
, save_add_needed
;
2590 err
= bfd_get_error ();
2592 /* See if the emulation has some special knowledge. */
2593 if (ldemul_unrecognized_file (entry
))
2596 if (err
== bfd_error_file_ambiguously_recognized
)
2600 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2601 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2602 for (p
= matching
; *p
!= NULL
; p
++)
2606 else if (err
!= bfd_error_file_not_recognized
2608 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2610 bfd_close (entry
->the_bfd
);
2611 entry
->the_bfd
= NULL
;
2613 /* Try to interpret the file as a linker script. */
2614 ldfile_open_command_file (entry
->filename
);
2616 push_stat_ptr (place
);
2617 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2618 ldlang_sysrooted_script
= entry
->sysrooted
;
2619 save_as_needed
= as_needed
;
2620 as_needed
= entry
->as_needed
;
2621 save_add_needed
= add_needed
;
2622 add_needed
= entry
->add_needed
;
2624 ldfile_assumed_script
= TRUE
;
2625 parser_input
= input_script
;
2626 /* We want to use the same -Bdynamic/-Bstatic as the one for
2628 config
.dynamic_link
= entry
->dynamic
;
2630 ldfile_assumed_script
= FALSE
;
2632 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2633 as_needed
= save_as_needed
;
2634 add_needed
= save_add_needed
;
2640 if (ldemul_recognized_file (entry
))
2643 /* We don't call ldlang_add_file for an archive. Instead, the
2644 add_symbols entry point will call ldlang_add_file, via the
2645 add_archive_element callback, for each element of the archive
2647 switch (bfd_get_format (entry
->the_bfd
))
2653 ldlang_add_file (entry
);
2654 if (trace_files
|| trace_file_tries
)
2655 info_msg ("%I\n", entry
);
2659 check_excluded_libs (entry
->the_bfd
);
2661 if (entry
->whole_archive
)
2664 bfd_boolean loaded
= TRUE
;
2668 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2673 if (! bfd_check_format (member
, bfd_object
))
2675 einfo (_("%F%B: member %B in archive is not an object\n"),
2676 entry
->the_bfd
, member
);
2680 if (! ((*link_info
.callbacks
->add_archive_element
)
2681 (&link_info
, member
, "--whole-archive")))
2684 if (! bfd_link_add_symbols (member
, &link_info
))
2686 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2691 entry
->loaded
= loaded
;
2697 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2698 entry
->loaded
= TRUE
;
2700 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2702 return entry
->loaded
;
2705 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2706 may be NULL, indicating that it is a wildcard. Separate
2707 lang_input_section statements are created for each part of the
2708 expansion; they are added after the wild statement S. OUTPUT is
2709 the output section. */
2712 wild (lang_wild_statement_type
*s
,
2713 const char *target ATTRIBUTE_UNUSED
,
2714 lang_output_section_statement_type
*output
)
2716 struct wildcard_list
*sec
;
2718 if (s
->handler_data
[0]
2719 && s
->handler_data
[0]->spec
.sorted
== by_name
2720 && !s
->filenames_sorted
)
2722 lang_section_bst_type
*tree
;
2724 walk_wild (s
, output_section_callback_fast
, output
);
2729 output_section_callback_tree_to_list (s
, tree
, output
);
2734 walk_wild (s
, output_section_callback
, output
);
2736 if (default_common_section
== NULL
)
2737 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2738 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2740 /* Remember the section that common is going to in case we
2741 later get something which doesn't know where to put it. */
2742 default_common_section
= output
;
2747 /* Return TRUE iff target is the sought target. */
2750 get_target (const bfd_target
*target
, void *data
)
2752 const char *sought
= data
;
2754 return strcmp (target
->name
, sought
) == 0;
2757 /* Like strcpy() but convert to lower case as well. */
2760 stricpy (char *dest
, char *src
)
2764 while ((c
= *src
++) != 0)
2765 *dest
++ = TOLOWER (c
);
2770 /* Remove the first occurrence of needle (if any) in haystack
2774 strcut (char *haystack
, char *needle
)
2776 haystack
= strstr (haystack
, needle
);
2782 for (src
= haystack
+ strlen (needle
); *src
;)
2783 *haystack
++ = *src
++;
2789 /* Compare two target format name strings.
2790 Return a value indicating how "similar" they are. */
2793 name_compare (char *first
, char *second
)
2799 copy1
= xmalloc (strlen (first
) + 1);
2800 copy2
= xmalloc (strlen (second
) + 1);
2802 /* Convert the names to lower case. */
2803 stricpy (copy1
, first
);
2804 stricpy (copy2
, second
);
2806 /* Remove size and endian strings from the name. */
2807 strcut (copy1
, "big");
2808 strcut (copy1
, "little");
2809 strcut (copy2
, "big");
2810 strcut (copy2
, "little");
2812 /* Return a value based on how many characters match,
2813 starting from the beginning. If both strings are
2814 the same then return 10 * their length. */
2815 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2816 if (copy1
[result
] == 0)
2828 /* Set by closest_target_match() below. */
2829 static const bfd_target
*winner
;
2831 /* Scan all the valid bfd targets looking for one that has the endianness
2832 requirement that was specified on the command line, and is the nearest
2833 match to the original output target. */
2836 closest_target_match (const bfd_target
*target
, void *data
)
2838 const bfd_target
*original
= data
;
2840 if (command_line
.endian
== ENDIAN_BIG
2841 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2844 if (command_line
.endian
== ENDIAN_LITTLE
2845 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2848 /* Must be the same flavour. */
2849 if (target
->flavour
!= original
->flavour
)
2852 /* Ignore generic big and little endian elf vectors. */
2853 if (strcmp (target
->name
, "elf32-big") == 0
2854 || strcmp (target
->name
, "elf64-big") == 0
2855 || strcmp (target
->name
, "elf32-little") == 0
2856 || strcmp (target
->name
, "elf64-little") == 0)
2859 /* If we have not found a potential winner yet, then record this one. */
2866 /* Oh dear, we now have two potential candidates for a successful match.
2867 Compare their names and choose the better one. */
2868 if (name_compare (target
->name
, original
->name
)
2869 > name_compare (winner
->name
, original
->name
))
2872 /* Keep on searching until wqe have checked them all. */
2876 /* Return the BFD target format of the first input file. */
2879 get_first_input_target (void)
2881 char *target
= NULL
;
2883 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2885 if (s
->header
.type
== lang_input_statement_enum
2888 ldfile_open_file (s
);
2890 if (s
->the_bfd
!= NULL
2891 && bfd_check_format (s
->the_bfd
, bfd_object
))
2893 target
= bfd_get_target (s
->the_bfd
);
2905 lang_get_output_target (void)
2909 /* Has the user told us which output format to use? */
2910 if (output_target
!= NULL
)
2911 return output_target
;
2913 /* No - has the current target been set to something other than
2915 if (current_target
!= default_target
)
2916 return current_target
;
2918 /* No - can we determine the format of the first input file? */
2919 target
= get_first_input_target ();
2923 /* Failed - use the default output target. */
2924 return default_target
;
2927 /* Open the output file. */
2930 open_output (const char *name
)
2932 output_target
= lang_get_output_target ();
2934 /* Has the user requested a particular endianness on the command
2936 if (command_line
.endian
!= ENDIAN_UNSET
)
2938 const bfd_target
*target
;
2939 enum bfd_endian desired_endian
;
2941 /* Get the chosen target. */
2942 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2944 /* If the target is not supported, we cannot do anything. */
2947 if (command_line
.endian
== ENDIAN_BIG
)
2948 desired_endian
= BFD_ENDIAN_BIG
;
2950 desired_endian
= BFD_ENDIAN_LITTLE
;
2952 /* See if the target has the wrong endianness. This should
2953 not happen if the linker script has provided big and
2954 little endian alternatives, but some scrips don't do
2956 if (target
->byteorder
!= desired_endian
)
2958 /* If it does, then see if the target provides
2959 an alternative with the correct endianness. */
2960 if (target
->alternative_target
!= NULL
2961 && (target
->alternative_target
->byteorder
== desired_endian
))
2962 output_target
= target
->alternative_target
->name
;
2965 /* Try to find a target as similar as possible to
2966 the default target, but which has the desired
2967 endian characteristic. */
2968 bfd_search_for_target (closest_target_match
,
2971 /* Oh dear - we could not find any targets that
2972 satisfy our requirements. */
2974 einfo (_("%P: warning: could not find any targets"
2975 " that match endianness requirement\n"));
2977 output_target
= winner
->name
;
2983 link_info
.output_bfd
= bfd_openw (name
, output_target
);
2985 if (link_info
.output_bfd
== NULL
)
2987 if (bfd_get_error () == bfd_error_invalid_target
)
2988 einfo (_("%P%F: target %s not found\n"), output_target
);
2990 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2993 delete_output_file_on_failure
= TRUE
;
2995 if (! bfd_set_format (link_info
.output_bfd
, bfd_object
))
2996 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2997 if (! bfd_set_arch_mach (link_info
.output_bfd
,
2998 ldfile_output_architecture
,
2999 ldfile_output_machine
))
3000 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
3002 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
3003 if (link_info
.hash
== NULL
)
3004 einfo (_("%P%F: can not create hash table: %E\n"));
3006 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
3010 ldlang_open_output (lang_statement_union_type
*statement
)
3012 switch (statement
->header
.type
)
3014 case lang_output_statement_enum
:
3015 ASSERT (link_info
.output_bfd
== NULL
);
3016 open_output (statement
->output_statement
.name
);
3017 ldemul_set_output_arch ();
3018 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
3019 link_info
.output_bfd
->flags
|= D_PAGED
;
3021 link_info
.output_bfd
->flags
&= ~D_PAGED
;
3022 if (config
.text_read_only
)
3023 link_info
.output_bfd
->flags
|= WP_TEXT
;
3025 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
3026 if (link_info
.traditional_format
)
3027 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
3029 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
3032 case lang_target_statement_enum
:
3033 current_target
= statement
->target_statement
.target
;
3040 /* Convert between addresses in bytes and sizes in octets.
3041 For currently supported targets, octets_per_byte is always a power
3042 of two, so we can use shifts. */
3043 #define TO_ADDR(X) ((X) >> opb_shift)
3044 #define TO_SIZE(X) ((X) << opb_shift)
3046 /* Support the above. */
3047 static unsigned int opb_shift
= 0;
3052 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3053 ldfile_output_machine
);
3056 while ((x
& 1) == 0)
3064 /* Open all the input files. */
3067 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
3069 for (; s
!= NULL
; s
= s
->header
.next
)
3071 switch (s
->header
.type
)
3073 case lang_constructors_statement_enum
:
3074 open_input_bfds (constructor_list
.head
, force
);
3076 case lang_output_section_statement_enum
:
3077 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
3079 case lang_wild_statement_enum
:
3080 /* Maybe we should load the file's symbols. */
3081 if (s
->wild_statement
.filename
3082 && !wildcardp (s
->wild_statement
.filename
)
3083 && !archive_path (s
->wild_statement
.filename
))
3084 lookup_name (s
->wild_statement
.filename
);
3085 open_input_bfds (s
->wild_statement
.children
.head
, force
);
3087 case lang_group_statement_enum
:
3089 struct bfd_link_hash_entry
*undefs
;
3091 /* We must continually search the entries in the group
3092 until no new symbols are added to the list of undefined
3097 undefs
= link_info
.hash
->undefs_tail
;
3098 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
3100 while (undefs
!= link_info
.hash
->undefs_tail
);
3103 case lang_target_statement_enum
:
3104 current_target
= s
->target_statement
.target
;
3106 case lang_input_statement_enum
:
3107 if (s
->input_statement
.real
)
3109 lang_statement_union_type
**os_tail
;
3110 lang_statement_list_type add
;
3112 s
->input_statement
.target
= current_target
;
3114 /* If we are being called from within a group, and this
3115 is an archive which has already been searched, then
3116 force it to be researched unless the whole archive
3117 has been loaded already. */
3119 && !s
->input_statement
.whole_archive
3120 && s
->input_statement
.loaded
3121 && bfd_check_format (s
->input_statement
.the_bfd
,
3123 s
->input_statement
.loaded
= FALSE
;
3125 os_tail
= lang_output_section_statement
.tail
;
3126 lang_list_init (&add
);
3128 if (! load_symbols (&s
->input_statement
, &add
))
3129 config
.make_executable
= FALSE
;
3131 if (add
.head
!= NULL
)
3133 /* If this was a script with output sections then
3134 tack any added statements on to the end of the
3135 list. This avoids having to reorder the output
3136 section statement list. Very likely the user
3137 forgot -T, and whatever we do here will not meet
3138 naive user expectations. */
3139 if (os_tail
!= lang_output_section_statement
.tail
)
3141 einfo (_("%P: warning: %s contains output sections;"
3142 " did you forget -T?\n"),
3143 s
->input_statement
.filename
);
3144 *stat_ptr
->tail
= add
.head
;
3145 stat_ptr
->tail
= add
.tail
;
3149 *add
.tail
= s
->header
.next
;
3150 s
->header
.next
= add
.head
;
3161 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
3164 lang_track_definedness (const char *name
)
3166 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
3167 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
3170 /* New-function for the definedness hash table. */
3172 static struct bfd_hash_entry
*
3173 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
3174 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
3175 const char *name ATTRIBUTE_UNUSED
)
3177 struct lang_definedness_hash_entry
*ret
3178 = (struct lang_definedness_hash_entry
*) entry
;
3181 ret
= (struct lang_definedness_hash_entry
*)
3182 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
3185 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
3187 ret
->iteration
= -1;
3191 /* Return the iteration when the definition of NAME was last updated. A
3192 value of -1 means that the symbol is not defined in the linker script
3193 or the command line, but may be defined in the linker symbol table. */
3196 lang_symbol_definition_iteration (const char *name
)
3198 struct lang_definedness_hash_entry
*defentry
3199 = (struct lang_definedness_hash_entry
*)
3200 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3202 /* We've already created this one on the presence of DEFINED in the
3203 script, so it can't be NULL unless something is borked elsewhere in
3205 if (defentry
== NULL
)
3208 return defentry
->iteration
;
3211 /* Update the definedness state of NAME. */
3214 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
3216 struct lang_definedness_hash_entry
*defentry
3217 = (struct lang_definedness_hash_entry
*)
3218 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3220 /* We don't keep track of symbols not tested with DEFINED. */
3221 if (defentry
== NULL
)
3224 /* If the symbol was already defined, and not from an earlier statement
3225 iteration, don't update the definedness iteration, because that'd
3226 make the symbol seem defined in the linker script at this point, and
3227 it wasn't; it was defined in some object. If we do anyway, DEFINED
3228 would start to yield false before this point and the construct "sym =
3229 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3231 if (h
->type
!= bfd_link_hash_undefined
3232 && h
->type
!= bfd_link_hash_common
3233 && h
->type
!= bfd_link_hash_new
3234 && defentry
->iteration
== -1)
3237 defentry
->iteration
= lang_statement_iteration
;
3240 /* Add the supplied name to the symbol table as an undefined reference.
3241 This is a two step process as the symbol table doesn't even exist at
3242 the time the ld command line is processed. First we put the name
3243 on a list, then, once the output file has been opened, transfer the
3244 name to the symbol table. */
3246 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3248 #define ldlang_undef_chain_list_head entry_symbol.next
3251 ldlang_add_undef (const char *const name
)
3253 ldlang_undef_chain_list_type
*new =
3254 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
3256 new->next
= ldlang_undef_chain_list_head
;
3257 ldlang_undef_chain_list_head
= new;
3259 new->name
= xstrdup (name
);
3261 if (link_info
.output_bfd
!= NULL
)
3262 insert_undefined (new->name
);
3265 /* Insert NAME as undefined in the symbol table. */
3268 insert_undefined (const char *name
)
3270 struct bfd_link_hash_entry
*h
;
3272 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3274 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3275 if (h
->type
== bfd_link_hash_new
)
3277 h
->type
= bfd_link_hash_undefined
;
3278 h
->u
.undef
.abfd
= NULL
;
3279 bfd_link_add_undef (link_info
.hash
, h
);
3283 /* Run through the list of undefineds created above and place them
3284 into the linker hash table as undefined symbols belonging to the
3288 lang_place_undefineds (void)
3290 ldlang_undef_chain_list_type
*ptr
;
3292 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3293 insert_undefined (ptr
->name
);
3296 /* Check for all readonly or some readwrite sections. */
3299 check_input_sections
3300 (lang_statement_union_type
*s
,
3301 lang_output_section_statement_type
*output_section_statement
)
3303 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3305 switch (s
->header
.type
)
3307 case lang_wild_statement_enum
:
3308 walk_wild (&s
->wild_statement
, check_section_callback
,
3309 output_section_statement
);
3310 if (! output_section_statement
->all_input_readonly
)
3313 case lang_constructors_statement_enum
:
3314 check_input_sections (constructor_list
.head
,
3315 output_section_statement
);
3316 if (! output_section_statement
->all_input_readonly
)
3319 case lang_group_statement_enum
:
3320 check_input_sections (s
->group_statement
.children
.head
,
3321 output_section_statement
);
3322 if (! output_section_statement
->all_input_readonly
)
3331 /* Update wildcard statements if needed. */
3334 update_wild_statements (lang_statement_union_type
*s
)
3336 struct wildcard_list
*sec
;
3338 switch (sort_section
)
3348 for (; s
!= NULL
; s
= s
->header
.next
)
3350 switch (s
->header
.type
)
3355 case lang_wild_statement_enum
:
3356 sec
= s
->wild_statement
.section_list
;
3357 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3360 switch (sec
->spec
.sorted
)
3363 sec
->spec
.sorted
= sort_section
;
3366 if (sort_section
== by_alignment
)
3367 sec
->spec
.sorted
= by_name_alignment
;
3370 if (sort_section
== by_name
)
3371 sec
->spec
.sorted
= by_alignment_name
;
3379 case lang_constructors_statement_enum
:
3380 update_wild_statements (constructor_list
.head
);
3383 case lang_output_section_statement_enum
:
3384 update_wild_statements
3385 (s
->output_section_statement
.children
.head
);
3388 case lang_group_statement_enum
:
3389 update_wild_statements (s
->group_statement
.children
.head
);
3397 /* Open input files and attach to output sections. */
3400 map_input_to_output_sections
3401 (lang_statement_union_type
*s
, const char *target
,
3402 lang_output_section_statement_type
*os
)
3406 for (; s
!= NULL
; s
= s
->header
.next
)
3408 switch (s
->header
.type
)
3410 case lang_wild_statement_enum
:
3411 wild (&s
->wild_statement
, target
, os
);
3413 case lang_constructors_statement_enum
:
3414 map_input_to_output_sections (constructor_list
.head
,
3418 case lang_output_section_statement_enum
:
3419 if (s
->output_section_statement
.constraint
)
3421 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3422 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3424 s
->output_section_statement
.all_input_readonly
= TRUE
;
3425 check_input_sections (s
->output_section_statement
.children
.head
,
3426 &s
->output_section_statement
);
3427 if ((s
->output_section_statement
.all_input_readonly
3428 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3429 || (!s
->output_section_statement
.all_input_readonly
3430 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3432 s
->output_section_statement
.constraint
= -1;
3437 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3439 &s
->output_section_statement
);
3441 case lang_output_statement_enum
:
3443 case lang_target_statement_enum
:
3444 target
= s
->target_statement
.target
;
3446 case lang_group_statement_enum
:
3447 map_input_to_output_sections (s
->group_statement
.children
.head
,
3451 case lang_data_statement_enum
:
3452 /* Make sure that any sections mentioned in the expression
3454 exp_init_os (s
->data_statement
.exp
);
3455 flags
= SEC_HAS_CONTENTS
;
3456 /* The output section gets contents, and then we inspect for
3457 any flags set in the input script which override any ALLOC. */
3458 if (!(os
->flags
& SEC_NEVER_LOAD
))
3459 flags
|= SEC_ALLOC
| SEC_LOAD
;
3460 if (os
->bfd_section
== NULL
)
3461 init_os (os
, NULL
, flags
);
3463 os
->bfd_section
->flags
|= flags
;
3465 case lang_input_section_enum
:
3467 case lang_fill_statement_enum
:
3468 case lang_object_symbols_statement_enum
:
3469 case lang_reloc_statement_enum
:
3470 case lang_padding_statement_enum
:
3471 case lang_input_statement_enum
:
3472 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3473 init_os (os
, NULL
, 0);
3475 case lang_assignment_statement_enum
:
3476 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3477 init_os (os
, NULL
, 0);
3479 /* Make sure that any sections mentioned in the assignment
3481 exp_init_os (s
->assignment_statement
.exp
);
3483 case lang_address_statement_enum
:
3484 /* Mark the specified section with the supplied address.
3485 If this section was actually a segment marker, then the
3486 directive is ignored if the linker script explicitly
3487 processed the segment marker. Originally, the linker
3488 treated segment directives (like -Ttext on the
3489 command-line) as section directives. We honor the
3490 section directive semantics for backwards compatibilty;
3491 linker scripts that do not specifically check for
3492 SEGMENT_START automatically get the old semantics. */
3493 if (!s
->address_statement
.segment
3494 || !s
->address_statement
.segment
->used
)
3496 lang_output_section_statement_type
*aos
3497 = (lang_output_section_statement_lookup
3498 (s
->address_statement
.section_name
, 0, TRUE
));
3500 if (aos
->bfd_section
== NULL
)
3501 init_os (aos
, NULL
, 0);
3502 aos
->addr_tree
= s
->address_statement
.address
;
3505 case lang_insert_statement_enum
:
3511 /* An insert statement snips out all the linker statements from the
3512 start of the list and places them after the output section
3513 statement specified by the insert. This operation is complicated
3514 by the fact that we keep a doubly linked list of output section
3515 statements as well as the singly linked list of all statements. */
3518 process_insert_statements (void)
3520 lang_statement_union_type
**s
;
3521 lang_output_section_statement_type
*first_os
= NULL
;
3522 lang_output_section_statement_type
*last_os
= NULL
;
3523 lang_output_section_statement_type
*os
;
3525 /* "start of list" is actually the statement immediately after
3526 the special abs_section output statement, so that it isn't
3528 s
= &lang_output_section_statement
.head
;
3529 while (*(s
= &(*s
)->header
.next
) != NULL
)
3531 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
3533 /* Keep pointers to the first and last output section
3534 statement in the sequence we may be about to move. */
3535 os
= &(*s
)->output_section_statement
;
3537 ASSERT (last_os
== NULL
|| last_os
->next
== os
);
3540 /* Set constraint negative so that lang_output_section_find
3541 won't match this output section statement. At this
3542 stage in linking constraint has values in the range
3543 [-1, ONLY_IN_RW]. */
3544 last_os
->constraint
= -2 - last_os
->constraint
;
3545 if (first_os
== NULL
)
3548 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
3550 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
3551 lang_output_section_statement_type
*where
;
3552 lang_statement_union_type
**ptr
;
3553 lang_statement_union_type
*first
;
3555 where
= lang_output_section_find (i
->where
);
3556 if (where
!= NULL
&& i
->is_before
)
3559 where
= where
->prev
;
3560 while (where
!= NULL
&& where
->constraint
< 0);
3564 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
3568 /* Deal with reordering the output section statement list. */
3569 if (last_os
!= NULL
)
3571 asection
*first_sec
, *last_sec
;
3572 struct lang_output_section_statement_struct
**next
;
3574 /* Snip out the output sections we are moving. */
3575 first_os
->prev
->next
= last_os
->next
;
3576 if (last_os
->next
== NULL
)
3578 next
= &first_os
->prev
->next
;
3579 lang_output_section_statement
.tail
3580 = (lang_statement_union_type
**) next
;
3583 last_os
->next
->prev
= first_os
->prev
;
3584 /* Add them in at the new position. */
3585 last_os
->next
= where
->next
;
3586 if (where
->next
== NULL
)
3588 next
= &last_os
->next
;
3589 lang_output_section_statement
.tail
3590 = (lang_statement_union_type
**) next
;
3593 where
->next
->prev
= last_os
;
3594 first_os
->prev
= where
;
3595 where
->next
= first_os
;
3597 /* Move the bfd sections in the same way. */
3600 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3602 os
->constraint
= -2 - os
->constraint
;
3603 if (os
->bfd_section
!= NULL
3604 && os
->bfd_section
->owner
!= NULL
)
3606 last_sec
= os
->bfd_section
;
3607 if (first_sec
== NULL
)
3608 first_sec
= last_sec
;
3613 if (last_sec
!= NULL
)
3615 asection
*sec
= where
->bfd_section
;
3617 sec
= output_prev_sec_find (where
);
3619 /* The place we want to insert must come after the
3620 sections we are moving. So if we find no
3621 section or if the section is the same as our
3622 last section, then no move is needed. */
3623 if (sec
!= NULL
&& sec
!= last_sec
)
3625 /* Trim them off. */
3626 if (first_sec
->prev
!= NULL
)
3627 first_sec
->prev
->next
= last_sec
->next
;
3629 link_info
.output_bfd
->sections
= last_sec
->next
;
3630 if (last_sec
->next
!= NULL
)
3631 last_sec
->next
->prev
= first_sec
->prev
;
3633 link_info
.output_bfd
->section_last
= first_sec
->prev
;
3635 last_sec
->next
= sec
->next
;
3636 if (sec
->next
!= NULL
)
3637 sec
->next
->prev
= last_sec
;
3639 link_info
.output_bfd
->section_last
= last_sec
;
3640 first_sec
->prev
= sec
;
3641 sec
->next
= first_sec
;
3649 ptr
= insert_os_after (where
);
3650 /* Snip everything after the abs_section output statement we
3651 know is at the start of the list, up to and including
3652 the insert statement we are currently processing. */
3653 first
= lang_output_section_statement
.head
->header
.next
;
3654 lang_output_section_statement
.head
->header
.next
= (*s
)->header
.next
;
3655 /* Add them back where they belong. */
3658 statement_list
.tail
= s
;
3660 s
= &lang_output_section_statement
.head
;
3664 /* Undo constraint twiddling. */
3665 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3667 os
->constraint
= -2 - os
->constraint
;
3673 /* An output section might have been removed after its statement was
3674 added. For example, ldemul_before_allocation can remove dynamic
3675 sections if they turn out to be not needed. Clean them up here. */
3678 strip_excluded_output_sections (void)
3680 lang_output_section_statement_type
*os
;
3682 /* Run lang_size_sections (if not already done). */
3683 if (expld
.phase
!= lang_mark_phase_enum
)
3685 expld
.phase
= lang_mark_phase_enum
;
3686 expld
.dataseg
.phase
= exp_dataseg_none
;
3687 one_lang_size_sections_pass (NULL
, FALSE
);
3688 lang_reset_memory_regions ();
3691 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3695 asection
*output_section
;
3696 bfd_boolean exclude
;
3698 if (os
->constraint
< 0)
3701 output_section
= os
->bfd_section
;
3702 if (output_section
== NULL
)
3705 exclude
= (output_section
->rawsize
== 0
3706 && (output_section
->flags
& SEC_KEEP
) == 0
3707 && !bfd_section_removed_from_list (link_info
.output_bfd
,
3710 /* Some sections have not yet been sized, notably .gnu.version,
3711 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3712 input sections, so don't drop output sections that have such
3713 input sections unless they are also marked SEC_EXCLUDE. */
3714 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3718 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3719 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3720 && (s
->flags
& SEC_EXCLUDE
) == 0)
3727 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3728 output_section
->map_head
.link_order
= NULL
;
3729 output_section
->map_tail
.link_order
= NULL
;
3733 /* We don't set bfd_section to NULL since bfd_section of the
3734 removed output section statement may still be used. */
3735 if (!os
->section_relative_symbol
3736 && !os
->update_dot_tree
)
3738 output_section
->flags
|= SEC_EXCLUDE
;
3739 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
3740 link_info
.output_bfd
->section_count
--;
3744 /* Stop future calls to lang_add_section from messing with map_head
3745 and map_tail link_order fields. */
3746 stripped_excluded_sections
= TRUE
;
3750 print_output_section_statement
3751 (lang_output_section_statement_type
*output_section_statement
)
3753 asection
*section
= output_section_statement
->bfd_section
;
3756 if (output_section_statement
!= abs_output_section
)
3758 minfo ("\n%s", output_section_statement
->name
);
3760 if (section
!= NULL
)
3762 print_dot
= section
->vma
;
3764 len
= strlen (output_section_statement
->name
);
3765 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3770 while (len
< SECTION_NAME_MAP_LENGTH
)
3776 minfo ("0x%V %W", section
->vma
, section
->size
);
3778 if (section
->vma
!= section
->lma
)
3779 minfo (_(" load address 0x%V"), section
->lma
);
3781 if (output_section_statement
->update_dot_tree
!= NULL
)
3782 exp_fold_tree (output_section_statement
->update_dot_tree
,
3783 bfd_abs_section_ptr
, &print_dot
);
3789 print_statement_list (output_section_statement
->children
.head
,
3790 output_section_statement
);
3793 /* Scan for the use of the destination in the right hand side
3794 of an expression. In such cases we will not compute the
3795 correct expression, since the value of DST that is used on
3796 the right hand side will be its final value, not its value
3797 just before this expression is evaluated. */
3800 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3802 if (rhs
== NULL
|| dst
== NULL
)
3805 switch (rhs
->type
.node_class
)
3808 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3809 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3812 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3813 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3816 case etree_provided
:
3818 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3820 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3823 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3827 return strcmp (dst
, rhs
->value
.str
) == 0;
3832 return strcmp (dst
, rhs
->name
.name
) == 0;
3844 print_assignment (lang_assignment_statement_type
*assignment
,
3845 lang_output_section_statement_type
*output_section
)
3849 bfd_boolean computation_is_valid
= TRUE
;
3852 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3855 if (assignment
->exp
->type
.node_class
== etree_assert
)
3858 tree
= assignment
->exp
->assert_s
.child
;
3859 computation_is_valid
= TRUE
;
3863 const char *dst
= assignment
->exp
->assign
.dst
;
3865 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3866 tree
= assignment
->exp
->assign
.src
;
3867 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3870 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3871 if (expld
.result
.valid_p
)
3875 if (computation_is_valid
)
3877 value
= expld
.result
.value
;
3879 if (expld
.result
.section
)
3880 value
+= expld
.result
.section
->vma
;
3882 minfo ("0x%V", value
);
3888 struct bfd_link_hash_entry
*h
;
3890 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3891 FALSE
, FALSE
, TRUE
);
3894 value
= h
->u
.def
.value
;
3896 if (expld
.result
.section
)
3897 value
+= expld
.result
.section
->vma
;
3899 minfo ("[0x%V]", value
);
3902 minfo ("[unresolved]");
3914 exp_print_tree (assignment
->exp
);
3919 print_input_statement (lang_input_statement_type
*statm
)
3921 if (statm
->filename
!= NULL
3922 && (statm
->the_bfd
== NULL
3923 || (statm
->the_bfd
->flags
& BFD_LINKER_CREATED
) == 0))
3924 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3927 /* Print all symbols defined in a particular section. This is called
3928 via bfd_link_hash_traverse, or by print_all_symbols. */
3931 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3933 asection
*sec
= ptr
;
3935 if ((hash_entry
->type
== bfd_link_hash_defined
3936 || hash_entry
->type
== bfd_link_hash_defweak
)
3937 && sec
== hash_entry
->u
.def
.section
)
3941 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3944 (hash_entry
->u
.def
.value
3945 + hash_entry
->u
.def
.section
->output_offset
3946 + hash_entry
->u
.def
.section
->output_section
->vma
));
3948 minfo (" %T\n", hash_entry
->root
.string
);
3955 hash_entry_addr_cmp (const void *a
, const void *b
)
3957 const struct bfd_link_hash_entry
*l
= *(const struct bfd_link_hash_entry
**)a
;
3958 const struct bfd_link_hash_entry
*r
= *(const struct bfd_link_hash_entry
**)b
;
3960 if (l
->u
.def
.value
< r
->u
.def
.value
)
3962 else if (l
->u
.def
.value
> r
->u
.def
.value
)
3969 print_all_symbols (asection
*sec
)
3971 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3972 struct map_symbol_def
*def
;
3973 struct bfd_link_hash_entry
**entries
;
3979 *ud
->map_symbol_def_tail
= 0;
3981 /* Sort the symbols by address. */
3982 entries
= obstack_alloc (&map_obstack
,
3983 ud
->map_symbol_def_count
* sizeof (*entries
));
3985 for (i
= 0, def
= ud
->map_symbol_def_head
; def
; def
= def
->next
, i
++)
3986 entries
[i
] = def
->entry
;
3988 qsort (entries
, ud
->map_symbol_def_count
, sizeof (*entries
),
3989 hash_entry_addr_cmp
);
3991 /* Print the symbols. */
3992 for (i
= 0; i
< ud
->map_symbol_def_count
; i
++)
3993 print_one_symbol (entries
[i
], sec
);
3995 obstack_free (&map_obstack
, entries
);
3998 /* Print information about an input section to the map file. */
4001 print_input_section (asection
*i
, bfd_boolean is_discarded
)
4003 bfd_size_type size
= i
->size
;
4010 minfo ("%s", i
->name
);
4012 len
= 1 + strlen (i
->name
);
4013 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
4018 while (len
< SECTION_NAME_MAP_LENGTH
)
4024 if (i
->output_section
!= NULL
4025 && i
->output_section
->owner
== link_info
.output_bfd
)
4026 addr
= i
->output_section
->vma
+ i
->output_offset
;
4034 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
4036 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
4038 len
= SECTION_NAME_MAP_LENGTH
+ 3;
4050 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
4053 if (i
->output_section
!= NULL
4054 && i
->output_section
->owner
== link_info
.output_bfd
)
4056 if (link_info
.reduce_memory_overheads
)
4057 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
4059 print_all_symbols (i
);
4061 /* Update print_dot, but make sure that we do not move it
4062 backwards - this could happen if we have overlays and a
4063 later overlay is shorter than an earier one. */
4064 if (addr
+ TO_ADDR (size
) > print_dot
)
4065 print_dot
= addr
+ TO_ADDR (size
);
4070 print_fill_statement (lang_fill_statement_type
*fill
)
4074 fputs (" FILL mask 0x", config
.map_file
);
4075 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
4076 fprintf (config
.map_file
, "%02x", *p
);
4077 fputs ("\n", config
.map_file
);
4081 print_data_statement (lang_data_statement_type
*data
)
4089 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4092 addr
= data
->output_offset
;
4093 if (data
->output_section
!= NULL
)
4094 addr
+= data
->output_section
->vma
;
4122 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
4124 if (data
->exp
->type
.node_class
!= etree_value
)
4127 exp_print_tree (data
->exp
);
4132 print_dot
= addr
+ TO_ADDR (size
);
4135 /* Print an address statement. These are generated by options like
4139 print_address_statement (lang_address_statement_type
*address
)
4141 minfo (_("Address of section %s set to "), address
->section_name
);
4142 exp_print_tree (address
->address
);
4146 /* Print a reloc statement. */
4149 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4156 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4159 addr
= reloc
->output_offset
;
4160 if (reloc
->output_section
!= NULL
)
4161 addr
+= reloc
->output_section
->vma
;
4163 size
= bfd_get_reloc_size (reloc
->howto
);
4165 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
4167 if (reloc
->name
!= NULL
)
4168 minfo ("%s+", reloc
->name
);
4170 minfo ("%s+", reloc
->section
->name
);
4172 exp_print_tree (reloc
->addend_exp
);
4176 print_dot
= addr
+ TO_ADDR (size
);
4180 print_padding_statement (lang_padding_statement_type
*s
)
4188 len
= sizeof " *fill*" - 1;
4189 while (len
< SECTION_NAME_MAP_LENGTH
)
4195 addr
= s
->output_offset
;
4196 if (s
->output_section
!= NULL
)
4197 addr
+= s
->output_section
->vma
;
4198 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
4200 if (s
->fill
->size
!= 0)
4204 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4205 fprintf (config
.map_file
, "%02x", *p
);
4210 print_dot
= addr
+ TO_ADDR (s
->size
);
4214 print_wild_statement (lang_wild_statement_type
*w
,
4215 lang_output_section_statement_type
*os
)
4217 struct wildcard_list
*sec
;
4221 if (w
->filenames_sorted
)
4223 if (w
->filename
!= NULL
)
4224 minfo ("%s", w
->filename
);
4227 if (w
->filenames_sorted
)
4231 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4233 if (sec
->spec
.sorted
)
4235 if (sec
->spec
.exclude_name_list
!= NULL
)
4238 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4239 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4240 minfo (" %s", tmp
->name
);
4243 if (sec
->spec
.name
!= NULL
)
4244 minfo ("%s", sec
->spec
.name
);
4247 if (sec
->spec
.sorted
)
4256 print_statement_list (w
->children
.head
, os
);
4259 /* Print a group statement. */
4262 print_group (lang_group_statement_type
*s
,
4263 lang_output_section_statement_type
*os
)
4265 fprintf (config
.map_file
, "START GROUP\n");
4266 print_statement_list (s
->children
.head
, os
);
4267 fprintf (config
.map_file
, "END GROUP\n");
4270 /* Print the list of statements in S.
4271 This can be called for any statement type. */
4274 print_statement_list (lang_statement_union_type
*s
,
4275 lang_output_section_statement_type
*os
)
4279 print_statement (s
, os
);
4284 /* Print the first statement in statement list S.
4285 This can be called for any statement type. */
4288 print_statement (lang_statement_union_type
*s
,
4289 lang_output_section_statement_type
*os
)
4291 switch (s
->header
.type
)
4294 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
4297 case lang_constructors_statement_enum
:
4298 if (constructor_list
.head
!= NULL
)
4300 if (constructors_sorted
)
4301 minfo (" SORT (CONSTRUCTORS)\n");
4303 minfo (" CONSTRUCTORS\n");
4304 print_statement_list (constructor_list
.head
, os
);
4307 case lang_wild_statement_enum
:
4308 print_wild_statement (&s
->wild_statement
, os
);
4310 case lang_address_statement_enum
:
4311 print_address_statement (&s
->address_statement
);
4313 case lang_object_symbols_statement_enum
:
4314 minfo (" CREATE_OBJECT_SYMBOLS\n");
4316 case lang_fill_statement_enum
:
4317 print_fill_statement (&s
->fill_statement
);
4319 case lang_data_statement_enum
:
4320 print_data_statement (&s
->data_statement
);
4322 case lang_reloc_statement_enum
:
4323 print_reloc_statement (&s
->reloc_statement
);
4325 case lang_input_section_enum
:
4326 print_input_section (s
->input_section
.section
, FALSE
);
4328 case lang_padding_statement_enum
:
4329 print_padding_statement (&s
->padding_statement
);
4331 case lang_output_section_statement_enum
:
4332 print_output_section_statement (&s
->output_section_statement
);
4334 case lang_assignment_statement_enum
:
4335 print_assignment (&s
->assignment_statement
, os
);
4337 case lang_target_statement_enum
:
4338 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
4340 case lang_output_statement_enum
:
4341 minfo ("OUTPUT(%s", s
->output_statement
.name
);
4342 if (output_target
!= NULL
)
4343 minfo (" %s", output_target
);
4346 case lang_input_statement_enum
:
4347 print_input_statement (&s
->input_statement
);
4349 case lang_group_statement_enum
:
4350 print_group (&s
->group_statement
, os
);
4352 case lang_insert_statement_enum
:
4353 minfo ("INSERT %s %s\n",
4354 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
4355 s
->insert_statement
.where
);
4361 print_statements (void)
4363 print_statement_list (statement_list
.head
, abs_output_section
);
4366 /* Print the first N statements in statement list S to STDERR.
4367 If N == 0, nothing is printed.
4368 If N < 0, the entire list is printed.
4369 Intended to be called from GDB. */
4372 dprint_statement (lang_statement_union_type
*s
, int n
)
4374 FILE *map_save
= config
.map_file
;
4376 config
.map_file
= stderr
;
4379 print_statement_list (s
, abs_output_section
);
4382 while (s
&& --n
>= 0)
4384 print_statement (s
, abs_output_section
);
4389 config
.map_file
= map_save
;
4393 insert_pad (lang_statement_union_type
**ptr
,
4395 unsigned int alignment_needed
,
4396 asection
*output_section
,
4399 static fill_type zero_fill
= { 1, { 0 } };
4400 lang_statement_union_type
*pad
= NULL
;
4402 if (ptr
!= &statement_list
.head
)
4403 pad
= ((lang_statement_union_type
*)
4404 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4406 && pad
->header
.type
== lang_padding_statement_enum
4407 && pad
->padding_statement
.output_section
== output_section
)
4409 /* Use the existing pad statement. */
4411 else if ((pad
= *ptr
) != NULL
4412 && pad
->header
.type
== lang_padding_statement_enum
4413 && pad
->padding_statement
.output_section
== output_section
)
4415 /* Use the existing pad statement. */
4419 /* Make a new padding statement, linked into existing chain. */
4420 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4421 pad
->header
.next
= *ptr
;
4423 pad
->header
.type
= lang_padding_statement_enum
;
4424 pad
->padding_statement
.output_section
= output_section
;
4427 pad
->padding_statement
.fill
= fill
;
4429 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4430 pad
->padding_statement
.size
= alignment_needed
;
4431 output_section
->size
+= alignment_needed
;
4434 /* Work out how much this section will move the dot point. */
4438 (lang_statement_union_type
**this_ptr
,
4439 lang_output_section_statement_type
*output_section_statement
,
4443 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4444 asection
*i
= is
->section
;
4446 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
4447 && (i
->flags
& SEC_EXCLUDE
) == 0)
4449 unsigned int alignment_needed
;
4452 /* Align this section first to the input sections requirement,
4453 then to the output section's requirement. If this alignment
4454 is greater than any seen before, then record it too. Perform
4455 the alignment by inserting a magic 'padding' statement. */
4457 if (output_section_statement
->subsection_alignment
!= -1)
4458 i
->alignment_power
= output_section_statement
->subsection_alignment
;
4460 o
= output_section_statement
->bfd_section
;
4461 if (o
->alignment_power
< i
->alignment_power
)
4462 o
->alignment_power
= i
->alignment_power
;
4464 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4466 if (alignment_needed
!= 0)
4468 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4469 dot
+= alignment_needed
;
4472 /* Remember where in the output section this input section goes. */
4474 i
->output_offset
= dot
- o
->vma
;
4476 /* Mark how big the output section must be to contain this now. */
4477 dot
+= TO_ADDR (i
->size
);
4478 o
->size
= TO_SIZE (dot
- o
->vma
);
4482 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
4489 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4491 const asection
*sec1
= *(const asection
**) arg1
;
4492 const asection
*sec2
= *(const asection
**) arg2
;
4494 if (bfd_section_lma (sec1
->owner
, sec1
)
4495 < bfd_section_lma (sec2
->owner
, sec2
))
4497 else if (bfd_section_lma (sec1
->owner
, sec1
)
4498 > bfd_section_lma (sec2
->owner
, sec2
))
4500 else if (sec1
->id
< sec2
->id
)
4502 else if (sec1
->id
> sec2
->id
)
4508 #define IGNORE_SECTION(s) \
4509 ((s->flags & SEC_NEVER_LOAD) != 0 \
4510 || (s->flags & SEC_ALLOC) == 0 \
4511 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4512 && (s->flags & SEC_LOAD) == 0))
4514 /* Check to see if any allocated sections overlap with other allocated
4515 sections. This can happen if a linker script specifies the output
4516 section addresses of the two sections. Also check whether any memory
4517 region has overflowed. */
4520 lang_check_section_addresses (void)
4523 asection
**sections
, **spp
;
4530 lang_memory_region_type
*m
;
4532 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
4535 amt
= bfd_count_sections (link_info
.output_bfd
) * sizeof (asection
*);
4536 sections
= xmalloc (amt
);
4538 /* Scan all sections in the output list. */
4540 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4542 /* Only consider loadable sections with real contents. */
4543 if ((s
->flags
& SEC_NEVER_LOAD
)
4544 || !(s
->flags
& SEC_LOAD
)
4545 || !(s
->flags
& SEC_ALLOC
)
4549 sections
[count
] = s
;
4556 qsort (sections
, (size_t) count
, sizeof (asection
*),
4557 sort_sections_by_lma
);
4561 s_start
= bfd_section_lma (link_info
.output_bfd
, s
);
4562 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4563 for (count
--; count
; count
--)
4565 /* We must check the sections' LMA addresses not their VMA
4566 addresses because overlay sections can have overlapping VMAs
4567 but they must have distinct LMAs. */
4572 s_start
= bfd_section_lma (link_info
.output_bfd
, s
);
4573 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4575 /* Look for an overlap. */
4576 if (s_end
>= os_start
&& s_start
<= os_end
)
4577 einfo (_("%X%P: section %s loaded at [%V,%V] overlaps section %s loaded at [%V,%V]\n"),
4578 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4583 /* If any memory region has overflowed, report by how much.
4584 We do not issue this diagnostic for regions that had sections
4585 explicitly placed outside their bounds; os_region_check's
4586 diagnostics are adequate for that case.
4588 FIXME: It is conceivable that m->current - (m->origin + m->length)
4589 might overflow a 32-bit integer. There is, alas, no way to print
4590 a bfd_vma quantity in decimal. */
4591 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
4592 if (m
->had_full_message
)
4593 einfo (_("%X%P: region `%s' overflowed by %ld bytes\n"),
4594 m
->name_list
.name
, (long)(m
->current
- (m
->origin
+ m
->length
)));
4598 /* Make sure the new address is within the region. We explicitly permit the
4599 current address to be at the exact end of the region when the address is
4600 non-zero, in case the region is at the end of addressable memory and the
4601 calculation wraps around. */
4604 os_region_check (lang_output_section_statement_type
*os
,
4605 lang_memory_region_type
*region
,
4609 if ((region
->current
< region
->origin
4610 || (region
->current
- region
->origin
> region
->length
))
4611 && ((region
->current
!= region
->origin
+ region
->length
)
4616 einfo (_("%X%P: address 0x%v of %B section `%s'"
4617 " is not within region `%s'\n"),
4619 os
->bfd_section
->owner
,
4620 os
->bfd_section
->name
,
4621 region
->name_list
.name
);
4623 else if (!region
->had_full_message
)
4625 region
->had_full_message
= TRUE
;
4627 einfo (_("%X%P: %B section `%s' will not fit in region `%s'\n"),
4628 os
->bfd_section
->owner
,
4629 os
->bfd_section
->name
,
4630 region
->name_list
.name
);
4635 /* Set the sizes for all the output sections. */
4638 lang_size_sections_1
4639 (lang_statement_union_type
*s
,
4640 lang_output_section_statement_type
*output_section_statement
,
4641 lang_statement_union_type
**prev
,
4645 bfd_boolean check_regions
)
4647 /* Size up the sections from their constituent parts. */
4648 for (; s
!= NULL
; s
= s
->header
.next
)
4650 switch (s
->header
.type
)
4652 case lang_output_section_statement_enum
:
4654 bfd_vma newdot
, after
;
4655 lang_output_section_statement_type
*os
;
4656 lang_memory_region_type
*r
;
4658 os
= &s
->output_section_statement
;
4659 if (os
->addr_tree
!= NULL
)
4661 os
->processed_vma
= FALSE
;
4662 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4664 if (expld
.result
.valid_p
)
4665 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4666 else if (expld
.phase
!= lang_mark_phase_enum
)
4667 einfo (_("%F%S: non constant or forward reference"
4668 " address expression for section %s\n"),
4672 if (os
->bfd_section
== NULL
)
4673 /* This section was removed or never actually created. */
4676 /* If this is a COFF shared library section, use the size and
4677 address from the input section. FIXME: This is COFF
4678 specific; it would be cleaner if there were some other way
4679 to do this, but nothing simple comes to mind. */
4680 if (((bfd_get_flavour (link_info
.output_bfd
)
4681 == bfd_target_ecoff_flavour
)
4682 || (bfd_get_flavour (link_info
.output_bfd
)
4683 == bfd_target_coff_flavour
))
4684 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4688 if (os
->children
.head
== NULL
4689 || os
->children
.head
->header
.next
!= NULL
4690 || (os
->children
.head
->header
.type
4691 != lang_input_section_enum
))
4692 einfo (_("%P%X: Internal error on COFF shared library"
4693 " section %s\n"), os
->name
);
4695 input
= os
->children
.head
->input_section
.section
;
4696 bfd_set_section_vma (os
->bfd_section
->owner
,
4698 bfd_section_vma (input
->owner
, input
));
4699 os
->bfd_section
->size
= input
->size
;
4704 if (bfd_is_abs_section (os
->bfd_section
))
4706 /* No matter what happens, an abs section starts at zero. */
4707 ASSERT (os
->bfd_section
->vma
== 0);
4713 if (os
->addr_tree
== NULL
)
4715 /* No address specified for this section, get one
4716 from the region specification. */
4717 if (os
->region
== NULL
4718 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4719 && os
->region
->name_list
.name
[0] == '*'
4720 && strcmp (os
->region
->name_list
.name
,
4721 DEFAULT_MEMORY_REGION
) == 0))
4723 os
->region
= lang_memory_default (os
->bfd_section
);
4726 /* If a loadable section is using the default memory
4727 region, and some non default memory regions were
4728 defined, issue an error message. */
4730 && !IGNORE_SECTION (os
->bfd_section
)
4731 && ! link_info
.relocatable
4733 && strcmp (os
->region
->name_list
.name
,
4734 DEFAULT_MEMORY_REGION
) == 0
4735 && lang_memory_region_list
!= NULL
4736 && (strcmp (lang_memory_region_list
->name_list
.name
,
4737 DEFAULT_MEMORY_REGION
) != 0
4738 || lang_memory_region_list
->next
!= NULL
)
4739 && expld
.phase
!= lang_mark_phase_enum
)
4741 /* By default this is an error rather than just a
4742 warning because if we allocate the section to the
4743 default memory region we can end up creating an
4744 excessively large binary, or even seg faulting when
4745 attempting to perform a negative seek. See
4746 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4747 for an example of this. This behaviour can be
4748 overridden by the using the --no-check-sections
4750 if (command_line
.check_section_addresses
)
4751 einfo (_("%P%F: error: no memory region specified"
4752 " for loadable section `%s'\n"),
4753 bfd_get_section_name (link_info
.output_bfd
,
4756 einfo (_("%P: warning: no memory region specified"
4757 " for loadable section `%s'\n"),
4758 bfd_get_section_name (link_info
.output_bfd
,
4762 newdot
= os
->region
->current
;
4763 align
= os
->bfd_section
->alignment_power
;
4766 align
= os
->section_alignment
;
4768 /* Align to what the section needs. */
4771 bfd_vma savedot
= newdot
;
4772 newdot
= align_power (newdot
, align
);
4774 if (newdot
!= savedot
4775 && (config
.warn_section_align
4776 || os
->addr_tree
!= NULL
)
4777 && expld
.phase
!= lang_mark_phase_enum
)
4778 einfo (_("%P: warning: changing start of section"
4779 " %s by %lu bytes\n"),
4780 os
->name
, (unsigned long) (newdot
- savedot
));
4783 /* PR 6945: Do not update the vma's of output sections
4784 when performing a relocatable link on COFF objects. */
4785 if (! link_info
.relocatable
4786 || (bfd_get_flavour (link_info
.output_bfd
)
4787 != bfd_target_coff_flavour
))
4788 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4790 os
->bfd_section
->output_offset
= 0;
4793 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4794 os
->fill
, newdot
, relax
, check_regions
);
4796 os
->processed_vma
= TRUE
;
4798 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4799 /* Except for some special linker created sections,
4800 no output section should change from zero size
4801 after strip_excluded_output_sections. A non-zero
4802 size on an ignored section indicates that some
4803 input section was not sized early enough. */
4804 ASSERT (os
->bfd_section
->size
== 0);
4807 dot
= os
->bfd_section
->vma
;
4809 /* Put the section within the requested block size, or
4810 align at the block boundary. */
4812 + TO_ADDR (os
->bfd_section
->size
)
4813 + os
->block_value
- 1)
4814 & - (bfd_vma
) os
->block_value
);
4816 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4819 /* Set section lma. */
4822 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
4826 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
4827 os
->bfd_section
->lma
= lma
;
4829 else if (os
->lma_region
!= NULL
)
4831 bfd_vma lma
= os
->lma_region
->current
;
4833 if (os
->section_alignment
!= -1)
4834 lma
= align_power (lma
, os
->section_alignment
);
4835 os
->bfd_section
->lma
= lma
;
4837 else if (r
->last_os
!= NULL
4838 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
4843 last
= r
->last_os
->output_section_statement
.bfd_section
;
4845 /* A backwards move of dot should be accompanied by
4846 an explicit assignment to the section LMA (ie.
4847 os->load_base set) because backwards moves can
4848 create overlapping LMAs. */
4850 && os
->bfd_section
->size
!= 0
4851 && dot
+ os
->bfd_section
->size
<= last
->vma
)
4853 /* If dot moved backwards then leave lma equal to
4854 vma. This is the old default lma, which might
4855 just happen to work when the backwards move is
4856 sufficiently large. Nag if this changes anything,
4857 so people can fix their linker scripts. */
4859 if (last
->vma
!= last
->lma
)
4860 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4865 /* If this is an overlay, set the current lma to that
4866 at the end of the previous section. */
4867 if (os
->sectype
== overlay_section
)
4868 lma
= last
->lma
+ last
->size
;
4870 /* Otherwise, keep the same lma to vma relationship
4871 as the previous section. */
4873 lma
= dot
+ last
->lma
- last
->vma
;
4875 if (os
->section_alignment
!= -1)
4876 lma
= align_power (lma
, os
->section_alignment
);
4877 os
->bfd_section
->lma
= lma
;
4880 os
->processed_lma
= TRUE
;
4882 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4885 /* Keep track of normal sections using the default
4886 lma region. We use this to set the lma for
4887 following sections. Overlays or other linker
4888 script assignment to lma might mean that the
4889 default lma == vma is incorrect.
4890 To avoid warnings about dot moving backwards when using
4891 -Ttext, don't start tracking sections until we find one
4892 of non-zero size or with lma set differently to vma. */
4893 if (((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4894 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0)
4895 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0
4896 && (os
->bfd_section
->size
!= 0
4897 || (r
->last_os
== NULL
4898 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
4899 || (r
->last_os
!= NULL
4900 && dot
>= (r
->last_os
->output_section_statement
4901 .bfd_section
->vma
)))
4902 && os
->lma_region
== NULL
4903 && !link_info
.relocatable
)
4906 /* .tbss sections effectively have zero size. */
4907 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4908 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4909 || link_info
.relocatable
)
4910 dot
+= TO_ADDR (os
->bfd_section
->size
);
4912 if (os
->update_dot_tree
!= 0)
4913 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4915 /* Update dot in the region ?
4916 We only do this if the section is going to be allocated,
4917 since unallocated sections do not contribute to the region's
4918 overall size in memory.
4920 If the SEC_NEVER_LOAD bit is not set, it will affect the
4921 addresses of sections after it. We have to update
4923 if (os
->region
!= NULL
4924 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4925 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4927 os
->region
->current
= dot
;
4930 /* Make sure the new address is within the region. */
4931 os_region_check (os
, os
->region
, os
->addr_tree
,
4932 os
->bfd_section
->vma
);
4934 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
4935 && (os
->bfd_section
->flags
& SEC_LOAD
))
4937 os
->lma_region
->current
4938 = os
->bfd_section
->lma
+ TO_ADDR (os
->bfd_section
->size
);
4941 os_region_check (os
, os
->lma_region
, NULL
,
4942 os
->bfd_section
->lma
);
4948 case lang_constructors_statement_enum
:
4949 dot
= lang_size_sections_1 (constructor_list
.head
,
4950 output_section_statement
,
4951 &s
->wild_statement
.children
.head
,
4952 fill
, dot
, relax
, check_regions
);
4955 case lang_data_statement_enum
:
4957 unsigned int size
= 0;
4959 s
->data_statement
.output_offset
=
4960 dot
- output_section_statement
->bfd_section
->vma
;
4961 s
->data_statement
.output_section
=
4962 output_section_statement
->bfd_section
;
4964 /* We might refer to provided symbols in the expression, and
4965 need to mark them as needed. */
4966 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4968 switch (s
->data_statement
.type
)
4986 if (size
< TO_SIZE ((unsigned) 1))
4987 size
= TO_SIZE ((unsigned) 1);
4988 dot
+= TO_ADDR (size
);
4989 output_section_statement
->bfd_section
->size
+= size
;
4993 case lang_reloc_statement_enum
:
4997 s
->reloc_statement
.output_offset
=
4998 dot
- output_section_statement
->bfd_section
->vma
;
4999 s
->reloc_statement
.output_section
=
5000 output_section_statement
->bfd_section
;
5001 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
5002 dot
+= TO_ADDR (size
);
5003 output_section_statement
->bfd_section
->size
+= size
;
5007 case lang_wild_statement_enum
:
5008 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
5009 output_section_statement
,
5010 &s
->wild_statement
.children
.head
,
5011 fill
, dot
, relax
, check_regions
);
5014 case lang_object_symbols_statement_enum
:
5015 link_info
.create_object_symbols_section
=
5016 output_section_statement
->bfd_section
;
5019 case lang_output_statement_enum
:
5020 case lang_target_statement_enum
:
5023 case lang_input_section_enum
:
5027 i
= (*prev
)->input_section
.section
;
5032 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
5033 einfo (_("%P%F: can't relax section: %E\n"));
5037 dot
= size_input_section (prev
, output_section_statement
,
5038 output_section_statement
->fill
, dot
);
5042 case lang_input_statement_enum
:
5045 case lang_fill_statement_enum
:
5046 s
->fill_statement
.output_section
=
5047 output_section_statement
->bfd_section
;
5049 fill
= s
->fill_statement
.fill
;
5052 case lang_assignment_statement_enum
:
5054 bfd_vma newdot
= dot
;
5055 etree_type
*tree
= s
->assignment_statement
.exp
;
5057 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
5059 exp_fold_tree (tree
,
5060 output_section_statement
->bfd_section
,
5063 if (expld
.dataseg
.relro
== exp_dataseg_relro_start
)
5065 if (!expld
.dataseg
.relro_start_stat
)
5066 expld
.dataseg
.relro_start_stat
= s
;
5069 ASSERT (expld
.dataseg
.relro_start_stat
== s
);
5072 else if (expld
.dataseg
.relro
== exp_dataseg_relro_end
)
5074 if (!expld
.dataseg
.relro_end_stat
)
5075 expld
.dataseg
.relro_end_stat
= s
;
5078 ASSERT (expld
.dataseg
.relro_end_stat
== s
);
5081 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
5083 /* This symbol is relative to this section. */
5084 if ((tree
->type
.node_class
== etree_provided
5085 || tree
->type
.node_class
== etree_assign
)
5086 && (tree
->assign
.dst
[0] != '.'
5087 || tree
->assign
.dst
[1] != '\0'))
5088 output_section_statement
->section_relative_symbol
= 1;
5090 if (!output_section_statement
->ignored
)
5092 if (output_section_statement
== abs_output_section
)
5094 /* If we don't have an output section, then just adjust
5095 the default memory address. */
5096 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
5097 FALSE
)->current
= newdot
;
5099 else if (newdot
!= dot
)
5101 /* Insert a pad after this statement. We can't
5102 put the pad before when relaxing, in case the
5103 assignment references dot. */
5104 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
5105 output_section_statement
->bfd_section
, dot
);
5107 /* Don't neuter the pad below when relaxing. */
5110 /* If dot is advanced, this implies that the section
5111 should have space allocated to it, unless the
5112 user has explicitly stated that the section
5113 should never be loaded. */
5114 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
5115 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
5122 case lang_padding_statement_enum
:
5123 /* If this is the first time lang_size_sections is called,
5124 we won't have any padding statements. If this is the
5125 second or later passes when relaxing, we should allow
5126 padding to shrink. If padding is needed on this pass, it
5127 will be added back in. */
5128 s
->padding_statement
.size
= 0;
5130 /* Make sure output_offset is valid. If relaxation shrinks
5131 the section and this pad isn't needed, it's possible to
5132 have output_offset larger than the final size of the
5133 section. bfd_set_section_contents will complain even for
5134 a pad size of zero. */
5135 s
->padding_statement
.output_offset
5136 = dot
- output_section_statement
->bfd_section
->vma
;
5139 case lang_group_statement_enum
:
5140 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
5141 output_section_statement
,
5142 &s
->group_statement
.children
.head
,
5143 fill
, dot
, relax
, check_regions
);
5146 case lang_insert_statement_enum
:
5149 /* We can only get here when relaxing is turned on. */
5150 case lang_address_statement_enum
:
5157 prev
= &s
->header
.next
;
5162 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5163 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5164 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5165 segments. We are allowed an opportunity to override this decision. */
5168 ldlang_override_segment_assignment (struct bfd_link_info
* info ATTRIBUTE_UNUSED
,
5169 bfd
* abfd ATTRIBUTE_UNUSED
,
5170 asection
* current_section
,
5171 asection
* previous_section
,
5172 bfd_boolean new_segment
)
5174 lang_output_section_statement_type
* cur
;
5175 lang_output_section_statement_type
* prev
;
5177 /* The checks below are only necessary when the BFD library has decided
5178 that the two sections ought to be placed into the same segment. */
5182 /* Paranoia checks. */
5183 if (current_section
== NULL
|| previous_section
== NULL
)
5186 /* Find the memory regions associated with the two sections.
5187 We call lang_output_section_find() here rather than scanning the list
5188 of output sections looking for a matching section pointer because if
5189 we have a large number of sections then a hash lookup is faster. */
5190 cur
= lang_output_section_find (current_section
->name
);
5191 prev
= lang_output_section_find (previous_section
->name
);
5193 /* More paranoia. */
5194 if (cur
== NULL
|| prev
== NULL
)
5197 /* If the regions are different then force the sections to live in
5198 different segments. See the email thread starting at the following
5199 URL for the reasons why this is necessary:
5200 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5201 return cur
->region
!= prev
->region
;
5205 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
5207 lang_statement_iteration
++;
5208 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
5209 &statement_list
.head
, 0, 0, relax
, check_regions
);
5213 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
5215 expld
.phase
= lang_allocating_phase_enum
;
5216 expld
.dataseg
.phase
= exp_dataseg_none
;
5218 one_lang_size_sections_pass (relax
, check_regions
);
5219 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
5220 && link_info
.relro
&& expld
.dataseg
.relro_end
)
5222 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5223 to put expld.dataseg.relro on a (common) page boundary. */
5224 bfd_vma min_base
, old_base
, relro_end
, maxpage
;
5226 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
5227 maxpage
= expld
.dataseg
.maxpagesize
;
5228 /* MIN_BASE is the absolute minimum address we are allowed to start the
5229 read-write segment (byte before will be mapped read-only). */
5230 min_base
= (expld
.dataseg
.min_base
+ maxpage
- 1) & ~(maxpage
- 1);
5231 /* OLD_BASE is the address for a feasible minimum address which will
5232 still not cause a data overlap inside MAXPAGE causing file offset skip
5234 old_base
= expld
.dataseg
.base
;
5235 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
5236 & (expld
.dataseg
.pagesize
- 1));
5237 /* Compute the expected PT_GNU_RELRO segment end. */
5238 relro_end
= ((expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
5239 & ~(expld
.dataseg
.pagesize
- 1));
5240 if (min_base
+ maxpage
< expld
.dataseg
.base
)
5242 expld
.dataseg
.base
-= maxpage
;
5243 relro_end
-= maxpage
;
5245 lang_reset_memory_regions ();
5246 one_lang_size_sections_pass (relax
, check_regions
);
5247 if (expld
.dataseg
.relro_end
> relro_end
)
5249 /* The alignment of sections between DATA_SEGMENT_ALIGN
5250 and DATA_SEGMENT_RELRO_END caused huge padding to be
5251 inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so
5252 that the section alignments will fit in. */
5254 unsigned int max_alignment_power
= 0;
5256 /* Find maximum alignment power of sections between
5257 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5258 for (sec
= link_info
.output_bfd
->sections
; sec
; sec
= sec
->next
)
5259 if (sec
->vma
>= expld
.dataseg
.base
5260 && sec
->vma
< expld
.dataseg
.relro_end
5261 && sec
->alignment_power
> max_alignment_power
)
5262 max_alignment_power
= sec
->alignment_power
;
5264 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
5266 if (expld
.dataseg
.base
- (1 << max_alignment_power
) < old_base
)
5267 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
5268 expld
.dataseg
.base
-= (1 << max_alignment_power
);
5269 lang_reset_memory_regions ();
5270 one_lang_size_sections_pass (relax
, check_regions
);
5273 link_info
.relro_start
= expld
.dataseg
.base
;
5274 link_info
.relro_end
= expld
.dataseg
.relro_end
;
5276 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
5278 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5279 a page could be saved in the data segment. */
5280 bfd_vma first
, last
;
5282 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
5283 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
5285 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
5286 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
5287 && first
+ last
<= expld
.dataseg
.pagesize
)
5289 expld
.dataseg
.phase
= exp_dataseg_adjust
;
5290 lang_reset_memory_regions ();
5291 one_lang_size_sections_pass (relax
, check_regions
);
5295 expld
.phase
= lang_final_phase_enum
;
5298 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5301 lang_do_assignments_1 (lang_statement_union_type
*s
,
5302 lang_output_section_statement_type
*current_os
,
5306 for (; s
!= NULL
; s
= s
->header
.next
)
5308 switch (s
->header
.type
)
5310 case lang_constructors_statement_enum
:
5311 dot
= lang_do_assignments_1 (constructor_list
.head
,
5312 current_os
, fill
, dot
);
5315 case lang_output_section_statement_enum
:
5317 lang_output_section_statement_type
*os
;
5319 os
= &(s
->output_section_statement
);
5320 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
5322 dot
= os
->bfd_section
->vma
;
5324 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
5326 /* .tbss sections effectively have zero size. */
5327 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
5328 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
5329 || link_info
.relocatable
)
5330 dot
+= TO_ADDR (os
->bfd_section
->size
);
5332 if (os
->update_dot_tree
!= NULL
)
5333 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5338 case lang_wild_statement_enum
:
5340 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
5341 current_os
, fill
, dot
);
5344 case lang_object_symbols_statement_enum
:
5345 case lang_output_statement_enum
:
5346 case lang_target_statement_enum
:
5349 case lang_data_statement_enum
:
5350 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5351 if (expld
.result
.valid_p
)
5352 s
->data_statement
.value
= (expld
.result
.value
5353 + expld
.result
.section
->vma
);
5355 einfo (_("%F%P: invalid data statement\n"));
5358 switch (s
->data_statement
.type
)
5376 if (size
< TO_SIZE ((unsigned) 1))
5377 size
= TO_SIZE ((unsigned) 1);
5378 dot
+= TO_ADDR (size
);
5382 case lang_reloc_statement_enum
:
5383 exp_fold_tree (s
->reloc_statement
.addend_exp
,
5384 bfd_abs_section_ptr
, &dot
);
5385 if (expld
.result
.valid_p
)
5386 s
->reloc_statement
.addend_value
= expld
.result
.value
;
5388 einfo (_("%F%P: invalid reloc statement\n"));
5389 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
5392 case lang_input_section_enum
:
5394 asection
*in
= s
->input_section
.section
;
5396 if ((in
->flags
& SEC_EXCLUDE
) == 0)
5397 dot
+= TO_ADDR (in
->size
);
5401 case lang_input_statement_enum
:
5404 case lang_fill_statement_enum
:
5405 fill
= s
->fill_statement
.fill
;
5408 case lang_assignment_statement_enum
:
5409 exp_fold_tree (s
->assignment_statement
.exp
,
5410 current_os
->bfd_section
,
5414 case lang_padding_statement_enum
:
5415 dot
+= TO_ADDR (s
->padding_statement
.size
);
5418 case lang_group_statement_enum
:
5419 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
5420 current_os
, fill
, dot
);
5423 case lang_insert_statement_enum
:
5426 case lang_address_statement_enum
:
5438 lang_do_assignments (void)
5440 lang_statement_iteration
++;
5441 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
5444 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5445 operator .startof. (section_name), it produces an undefined symbol
5446 .startof.section_name. Similarly, when it sees
5447 .sizeof. (section_name), it produces an undefined symbol
5448 .sizeof.section_name. For all the output sections, we look for
5449 such symbols, and set them to the correct value. */
5452 lang_set_startof (void)
5456 if (link_info
.relocatable
)
5459 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5461 const char *secname
;
5463 struct bfd_link_hash_entry
*h
;
5465 secname
= bfd_get_section_name (link_info
.output_bfd
, s
);
5466 buf
= xmalloc (10 + strlen (secname
));
5468 sprintf (buf
, ".startof.%s", secname
);
5469 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5470 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5472 h
->type
= bfd_link_hash_defined
;
5473 h
->u
.def
.value
= bfd_get_section_vma (link_info
.output_bfd
, s
);
5474 h
->u
.def
.section
= bfd_abs_section_ptr
;
5477 sprintf (buf
, ".sizeof.%s", secname
);
5478 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5479 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5481 h
->type
= bfd_link_hash_defined
;
5482 h
->u
.def
.value
= TO_ADDR (s
->size
);
5483 h
->u
.def
.section
= bfd_abs_section_ptr
;
5493 struct bfd_link_hash_entry
*h
;
5496 if ((link_info
.relocatable
&& !link_info
.gc_sections
)
5497 || (link_info
.shared
&& !link_info
.executable
))
5498 warn
= entry_from_cmdline
;
5502 /* Force the user to specify a root when generating a relocatable with
5504 if (link_info
.gc_sections
&& link_info
.relocatable
5505 && (entry_symbol
.name
== NULL
5506 && ldlang_undef_chain_list_head
== NULL
))
5507 einfo (_("%P%F: gc-sections requires either an entry or "
5508 "an undefined symbol\n"));
5510 if (entry_symbol
.name
== NULL
)
5512 /* No entry has been specified. Look for the default entry, but
5513 don't warn if we don't find it. */
5514 entry_symbol
.name
= entry_symbol_default
;
5518 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
5519 FALSE
, FALSE
, TRUE
);
5521 && (h
->type
== bfd_link_hash_defined
5522 || h
->type
== bfd_link_hash_defweak
)
5523 && h
->u
.def
.section
->output_section
!= NULL
)
5527 val
= (h
->u
.def
.value
5528 + bfd_get_section_vma (link_info
.output_bfd
,
5529 h
->u
.def
.section
->output_section
)
5530 + h
->u
.def
.section
->output_offset
);
5531 if (! bfd_set_start_address (link_info
.output_bfd
, val
))
5532 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
5539 /* We couldn't find the entry symbol. Try parsing it as a
5541 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
5544 if (! bfd_set_start_address (link_info
.output_bfd
, val
))
5545 einfo (_("%P%F: can't set start address\n"));
5551 /* Can't find the entry symbol, and it's not a number. Use
5552 the first address in the text section. */
5553 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
5557 einfo (_("%P: warning: cannot find entry symbol %s;"
5558 " defaulting to %V\n"),
5560 bfd_get_section_vma (link_info
.output_bfd
, ts
));
5561 if (!(bfd_set_start_address
5562 (link_info
.output_bfd
,
5563 bfd_get_section_vma (link_info
.output_bfd
, ts
))))
5564 einfo (_("%P%F: can't set start address\n"));
5569 einfo (_("%P: warning: cannot find entry symbol %s;"
5570 " not setting start address\n"),
5576 /* Don't bfd_hash_table_free (&lang_definedness_table);
5577 map file output may result in a call of lang_track_definedness. */
5580 /* This is a small function used when we want to ignore errors from
5584 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
5586 /* Don't do anything. */
5589 /* Check that the architecture of all the input files is compatible
5590 with the output file. Also call the backend to let it do any
5591 other checking that is needed. */
5596 lang_statement_union_type
*file
;
5598 const bfd_arch_info_type
*compatible
;
5600 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
5602 input_bfd
= file
->input_statement
.the_bfd
;
5604 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
5605 command_line
.accept_unknown_input_arch
);
5607 /* In general it is not possible to perform a relocatable
5608 link between differing object formats when the input
5609 file has relocations, because the relocations in the
5610 input format may not have equivalent representations in
5611 the output format (and besides BFD does not translate
5612 relocs for other link purposes than a final link). */
5613 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
5614 && (compatible
== NULL
5615 || (bfd_get_flavour (input_bfd
)
5616 != bfd_get_flavour (link_info
.output_bfd
)))
5617 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
5619 einfo (_("%P%F: Relocatable linking with relocations from"
5620 " format %s (%B) to format %s (%B) is not supported\n"),
5621 bfd_get_target (input_bfd
), input_bfd
,
5622 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
5623 /* einfo with %F exits. */
5626 if (compatible
== NULL
)
5628 if (command_line
.warn_mismatch
)
5629 einfo (_("%P%X: %s architecture of input file `%B'"
5630 " is incompatible with %s output\n"),
5631 bfd_printable_name (input_bfd
), input_bfd
,
5632 bfd_printable_name (link_info
.output_bfd
));
5634 else if (bfd_count_sections (input_bfd
))
5636 /* If the input bfd has no contents, it shouldn't set the
5637 private data of the output bfd. */
5639 bfd_error_handler_type pfn
= NULL
;
5641 /* If we aren't supposed to warn about mismatched input
5642 files, temporarily set the BFD error handler to a
5643 function which will do nothing. We still want to call
5644 bfd_merge_private_bfd_data, since it may set up
5645 information which is needed in the output file. */
5646 if (! command_line
.warn_mismatch
)
5647 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
5648 if (! bfd_merge_private_bfd_data (input_bfd
, link_info
.output_bfd
))
5650 if (command_line
.warn_mismatch
)
5651 einfo (_("%P%X: failed to merge target specific data"
5652 " of file %B\n"), input_bfd
);
5654 if (! command_line
.warn_mismatch
)
5655 bfd_set_error_handler (pfn
);
5660 /* Look through all the global common symbols and attach them to the
5661 correct section. The -sort-common command line switch may be used
5662 to roughly sort the entries by alignment. */
5667 if (command_line
.inhibit_common_definition
)
5669 if (link_info
.relocatable
5670 && ! command_line
.force_common_definition
)
5673 if (! config
.sort_common
)
5674 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
5679 if (config
.sort_common
== sort_descending
)
5681 for (power
= 4; power
> 0; power
--)
5682 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5685 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5689 for (power
= 0; power
<= 4; power
++)
5690 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5693 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5698 /* Place one common symbol in the correct section. */
5701 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
5703 unsigned int power_of_two
;
5707 if (h
->type
!= bfd_link_hash_common
)
5711 power_of_two
= h
->u
.c
.p
->alignment_power
;
5713 if (config
.sort_common
== sort_descending
5714 && power_of_two
< *(unsigned int *) info
)
5716 else if (config
.sort_common
== sort_ascending
5717 && power_of_two
> *(unsigned int *) info
)
5720 section
= h
->u
.c
.p
->section
;
5721 if (!bfd_define_common_symbol (link_info
.output_bfd
, &link_info
, h
))
5722 einfo (_("%P%F: Could not define common symbol `%T': %E\n"),
5725 if (config
.map_file
!= NULL
)
5727 static bfd_boolean header_printed
;
5732 if (! header_printed
)
5734 minfo (_("\nAllocating common symbols\n"));
5735 minfo (_("Common symbol size file\n\n"));
5736 header_printed
= TRUE
;
5739 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
5740 DMGL_ANSI
| DMGL_PARAMS
);
5743 minfo ("%s", h
->root
.string
);
5744 len
= strlen (h
->root
.string
);
5749 len
= strlen (name
);
5765 if (size
<= 0xffffffff)
5766 sprintf (buf
, "%lx", (unsigned long) size
);
5768 sprintf_vma (buf
, size
);
5778 minfo ("%B\n", section
->owner
);
5784 /* Run through the input files and ensure that every input section has
5785 somewhere to go. If one is found without a destination then create
5786 an input request and place it into the statement tree. */
5789 lang_place_orphans (void)
5791 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5795 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5797 if (s
->output_section
== NULL
)
5799 /* This section of the file is not attached, root
5800 around for a sensible place for it to go. */
5802 if (file
->just_syms_flag
)
5803 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5804 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5805 s
->output_section
= bfd_abs_section_ptr
;
5806 else if (strcmp (s
->name
, "COMMON") == 0)
5808 /* This is a lonely common section which must have
5809 come from an archive. We attach to the section
5810 with the wildcard. */
5811 if (! link_info
.relocatable
5812 || command_line
.force_common_definition
)
5814 if (default_common_section
== NULL
)
5815 default_common_section
5816 = lang_output_section_statement_lookup (".bss", 0,
5818 lang_add_section (&default_common_section
->children
, s
,
5819 default_common_section
);
5824 const char *name
= s
->name
;
5827 if (config
.unique_orphan_sections
|| unique_section_p (s
))
5828 constraint
= SPECIAL
;
5830 if (!ldemul_place_orphan (s
, name
, constraint
))
5832 lang_output_section_statement_type
*os
;
5833 os
= lang_output_section_statement_lookup (name
,
5836 lang_add_section (&os
->children
, s
, os
);
5845 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5847 flagword
*ptr_flags
;
5849 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5855 *ptr_flags
|= SEC_ALLOC
;
5859 *ptr_flags
|= SEC_READONLY
;
5863 *ptr_flags
|= SEC_DATA
;
5867 *ptr_flags
|= SEC_CODE
;
5872 *ptr_flags
|= SEC_LOAD
;
5876 einfo (_("%P%F: invalid syntax in flags\n"));
5883 /* Call a function on each input file. This function will be called
5884 on an archive, but not on the elements. */
5887 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5889 lang_input_statement_type
*f
;
5891 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5893 f
= (lang_input_statement_type
*) f
->next_real_file
)
5897 /* Call a function on each file. The function will be called on all
5898 the elements of an archive which are included in the link, but will
5899 not be called on the archive file itself. */
5902 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5904 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5911 ldlang_add_file (lang_input_statement_type
*entry
)
5913 lang_statement_append (&file_chain
,
5914 (lang_statement_union_type
*) entry
,
5917 /* The BFD linker needs to have a list of all input BFDs involved in
5919 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5920 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
5922 *link_info
.input_bfds_tail
= entry
->the_bfd
;
5923 link_info
.input_bfds_tail
= &entry
->the_bfd
->link_next
;
5924 entry
->the_bfd
->usrdata
= entry
;
5925 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5927 /* Look through the sections and check for any which should not be
5928 included in the link. We need to do this now, so that we can
5929 notice when the backend linker tries to report multiple
5930 definition errors for symbols which are in sections we aren't
5931 going to link. FIXME: It might be better to entirely ignore
5932 symbols which are defined in sections which are going to be
5933 discarded. This would require modifying the backend linker for
5934 each backend which might set the SEC_LINK_ONCE flag. If we do
5935 this, we should probably handle SEC_EXCLUDE in the same way. */
5937 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5941 lang_add_output (const char *name
, int from_script
)
5943 /* Make -o on command line override OUTPUT in script. */
5944 if (!had_output_filename
|| !from_script
)
5946 output_filename
= name
;
5947 had_output_filename
= TRUE
;
5951 static lang_output_section_statement_type
*current_section
;
5962 for (l
= 0; l
< 32; l
++)
5964 if (i
>= (unsigned int) x
)
5972 lang_output_section_statement_type
*
5973 lang_enter_output_section_statement (const char *output_section_statement_name
,
5974 etree_type
*address_exp
,
5975 enum section_type sectype
,
5977 etree_type
*subalign
,
5981 lang_output_section_statement_type
*os
;
5983 os
= lang_output_section_statement_lookup (output_section_statement_name
,
5985 current_section
= os
;
5987 if (os
->addr_tree
== NULL
)
5989 os
->addr_tree
= address_exp
;
5991 os
->sectype
= sectype
;
5992 if (sectype
!= noload_section
)
5993 os
->flags
= SEC_NO_FLAGS
;
5995 os
->flags
= SEC_NEVER_LOAD
;
5996 os
->block_value
= 1;
5998 /* Make next things chain into subchain of this. */
5999 push_stat_ptr (&os
->children
);
6001 os
->subsection_alignment
=
6002 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
6003 os
->section_alignment
=
6004 topower (exp_get_value_int (align
, -1, "section alignment"));
6006 os
->load_base
= ebase
;
6013 lang_output_statement_type
*new;
6015 new = new_stat (lang_output_statement
, stat_ptr
);
6016 new->name
= output_filename
;
6019 /* Reset the current counters in the regions. */
6022 lang_reset_memory_regions (void)
6024 lang_memory_region_type
*p
= lang_memory_region_list
;
6026 lang_output_section_statement_type
*os
;
6028 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
6030 p
->current
= p
->origin
;
6034 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6038 os
->processed_vma
= FALSE
;
6039 os
->processed_lma
= FALSE
;
6042 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6044 /* Save the last size for possible use by bfd_relax_section. */
6045 o
->rawsize
= o
->size
;
6050 /* Worker for lang_gc_sections_1. */
6053 gc_section_callback (lang_wild_statement_type
*ptr
,
6054 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
6056 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
6057 void *data ATTRIBUTE_UNUSED
)
6059 /* If the wild pattern was marked KEEP, the member sections
6060 should be as well. */
6061 if (ptr
->keep_sections
)
6062 section
->flags
|= SEC_KEEP
;
6065 /* Iterate over sections marking them against GC. */
6068 lang_gc_sections_1 (lang_statement_union_type
*s
)
6070 for (; s
!= NULL
; s
= s
->header
.next
)
6072 switch (s
->header
.type
)
6074 case lang_wild_statement_enum
:
6075 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
6077 case lang_constructors_statement_enum
:
6078 lang_gc_sections_1 (constructor_list
.head
);
6080 case lang_output_section_statement_enum
:
6081 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
6083 case lang_group_statement_enum
:
6084 lang_gc_sections_1 (s
->group_statement
.children
.head
);
6093 lang_gc_sections (void)
6095 /* Keep all sections so marked in the link script. */
6097 lang_gc_sections_1 (statement_list
.head
);
6099 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
6100 the special case of debug info. (See bfd/stabs.c)
6101 Twiddle the flag here, to simplify later linker code. */
6102 if (link_info
.relocatable
)
6104 LANG_FOR_EACH_INPUT_STATEMENT (f
)
6107 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6108 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
6109 sec
->flags
&= ~SEC_EXCLUDE
;
6113 if (link_info
.gc_sections
)
6114 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
6117 /* Worker for lang_find_relro_sections_1. */
6120 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
6121 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
6123 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
6126 /* Discarded, excluded and ignored sections effectively have zero
6128 if (section
->output_section
!= NULL
6129 && section
->output_section
->owner
== link_info
.output_bfd
6130 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
6131 && !IGNORE_SECTION (section
)
6132 && section
->size
!= 0)
6134 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
6135 *has_relro_section
= TRUE
;
6139 /* Iterate over sections for relro sections. */
6142 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
6143 bfd_boolean
*has_relro_section
)
6145 if (*has_relro_section
)
6148 for (; s
!= NULL
; s
= s
->header
.next
)
6150 if (s
== expld
.dataseg
.relro_end_stat
)
6153 switch (s
->header
.type
)
6155 case lang_wild_statement_enum
:
6156 walk_wild (&s
->wild_statement
,
6157 find_relro_section_callback
,
6160 case lang_constructors_statement_enum
:
6161 lang_find_relro_sections_1 (constructor_list
.head
,
6164 case lang_output_section_statement_enum
:
6165 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
6168 case lang_group_statement_enum
:
6169 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
6179 lang_find_relro_sections (void)
6181 bfd_boolean has_relro_section
= FALSE
;
6183 /* Check all sections in the link script. */
6185 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
6186 &has_relro_section
);
6188 if (!has_relro_section
)
6189 link_info
.relro
= FALSE
;
6192 /* Relax all sections until bfd_relax_section gives up. */
6195 lang_relax_sections (bfd_boolean need_layout
)
6197 if (command_line
.relax
)
6199 /* We may need more than one relaxation pass. */
6200 int i
= link_info
.relax_pass
;
6202 /* The backend can use it to determine the current pass. */
6203 link_info
.relax_pass
= 0;
6207 /* Keep relaxing until bfd_relax_section gives up. */
6208 bfd_boolean relax_again
;
6210 link_info
.relax_trip
= -1;
6213 link_info
.relax_trip
++;
6215 /* Note: pe-dll.c does something like this also. If you find
6216 you need to change this code, you probably need to change
6217 pe-dll.c also. DJ */
6219 /* Do all the assignments with our current guesses as to
6221 lang_do_assignments ();
6223 /* We must do this after lang_do_assignments, because it uses
6225 lang_reset_memory_regions ();
6227 /* Perform another relax pass - this time we know where the
6228 globals are, so can make a better guess. */
6229 relax_again
= FALSE
;
6230 lang_size_sections (&relax_again
, FALSE
);
6232 while (relax_again
);
6234 link_info
.relax_pass
++;
6241 /* Final extra sizing to report errors. */
6242 lang_do_assignments ();
6243 lang_reset_memory_regions ();
6244 lang_size_sections (NULL
, TRUE
);
6251 /* Finalize dynamic list. */
6252 if (link_info
.dynamic_list
)
6253 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
6255 current_target
= default_target
;
6257 /* Open the output file. */
6258 lang_for_each_statement (ldlang_open_output
);
6261 ldemul_create_output_section_statements ();
6263 /* Add to the hash table all undefineds on the command line. */
6264 lang_place_undefineds ();
6266 if (!bfd_section_already_linked_table_init ())
6267 einfo (_("%P%F: Failed to create hash table\n"));
6269 /* Create a bfd for each input file. */
6270 current_target
= default_target
;
6271 open_input_bfds (statement_list
.head
, FALSE
);
6273 link_info
.gc_sym_list
= &entry_symbol
;
6274 if (entry_symbol
.name
== NULL
)
6275 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
6277 ldemul_after_open ();
6279 bfd_section_already_linked_table_free ();
6281 /* Make sure that we're not mixing architectures. We call this
6282 after all the input files have been opened, but before we do any
6283 other processing, so that any operations merge_private_bfd_data
6284 does on the output file will be known during the rest of the
6288 /* Handle .exports instead of a version script if we're told to do so. */
6289 if (command_line
.version_exports_section
)
6290 lang_do_version_exports_section ();
6292 /* Build all sets based on the information gathered from the input
6294 ldctor_build_sets ();
6296 /* Remove unreferenced sections if asked to. */
6297 lang_gc_sections ();
6299 /* Size up the common data. */
6302 /* Update wild statements. */
6303 update_wild_statements (statement_list
.head
);
6305 /* Run through the contours of the script and attach input sections
6306 to the correct output sections. */
6307 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
6309 process_insert_statements ();
6311 /* Find any sections not attached explicitly and handle them. */
6312 lang_place_orphans ();
6314 if (! link_info
.relocatable
)
6318 /* Merge SEC_MERGE sections. This has to be done after GC of
6319 sections, so that GCed sections are not merged, but before
6320 assigning dynamic symbols, since removing whole input sections
6322 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
6324 /* Look for a text section and set the readonly attribute in it. */
6325 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
6329 if (config
.text_read_only
)
6330 found
->flags
|= SEC_READONLY
;
6332 found
->flags
&= ~SEC_READONLY
;
6336 /* Do anything special before sizing sections. This is where ELF
6337 and other back-ends size dynamic sections. */
6338 ldemul_before_allocation ();
6340 /* We must record the program headers before we try to fix the
6341 section positions, since they will affect SIZEOF_HEADERS. */
6342 lang_record_phdrs ();
6344 /* Check relro sections. */
6345 if (link_info
.relro
&& ! link_info
.relocatable
)
6346 lang_find_relro_sections ();
6348 /* Size up the sections. */
6349 lang_size_sections (NULL
, !command_line
.relax
);
6351 /* See if anything special should be done now we know how big
6352 everything is. This is where relaxation is done. */
6353 ldemul_after_allocation ();
6355 /* Fix any .startof. or .sizeof. symbols. */
6356 lang_set_startof ();
6358 /* Do all the assignments, now that we know the final resting places
6359 of all the symbols. */
6361 lang_do_assignments ();
6365 /* Make sure that the section addresses make sense. */
6366 if (command_line
.check_section_addresses
)
6367 lang_check_section_addresses ();
6372 /* EXPORTED TO YACC */
6375 lang_add_wild (struct wildcard_spec
*filespec
,
6376 struct wildcard_list
*section_list
,
6377 bfd_boolean keep_sections
)
6379 struct wildcard_list
*curr
, *next
;
6380 lang_wild_statement_type
*new;
6382 /* Reverse the list as the parser puts it back to front. */
6383 for (curr
= section_list
, section_list
= NULL
;
6385 section_list
= curr
, curr
= next
)
6387 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
6388 placed_commons
= TRUE
;
6391 curr
->next
= section_list
;
6394 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
6396 if (strcmp (filespec
->name
, "*") == 0)
6397 filespec
->name
= NULL
;
6398 else if (! wildcardp (filespec
->name
))
6399 lang_has_input_file
= TRUE
;
6402 new = new_stat (lang_wild_statement
, stat_ptr
);
6403 new->filename
= NULL
;
6404 new->filenames_sorted
= FALSE
;
6405 if (filespec
!= NULL
)
6407 new->filename
= filespec
->name
;
6408 new->filenames_sorted
= filespec
->sorted
== by_name
;
6410 new->section_list
= section_list
;
6411 new->keep_sections
= keep_sections
;
6412 lang_list_init (&new->children
);
6413 analyze_walk_wild_section_handler (new);
6417 lang_section_start (const char *name
, etree_type
*address
,
6418 const segment_type
*segment
)
6420 lang_address_statement_type
*ad
;
6422 ad
= new_stat (lang_address_statement
, stat_ptr
);
6423 ad
->section_name
= name
;
6424 ad
->address
= address
;
6425 ad
->segment
= segment
;
6428 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6429 because of a -e argument on the command line, or zero if this is
6430 called by ENTRY in a linker script. Command line arguments take
6434 lang_add_entry (const char *name
, bfd_boolean cmdline
)
6436 if (entry_symbol
.name
== NULL
6438 || ! entry_from_cmdline
)
6440 entry_symbol
.name
= name
;
6441 entry_from_cmdline
= cmdline
;
6445 /* Set the default start symbol to NAME. .em files should use this,
6446 not lang_add_entry, to override the use of "start" if neither the
6447 linker script nor the command line specifies an entry point. NAME
6448 must be permanently allocated. */
6450 lang_default_entry (const char *name
)
6452 entry_symbol_default
= name
;
6456 lang_add_target (const char *name
)
6458 lang_target_statement_type
*new;
6460 new = new_stat (lang_target_statement
, stat_ptr
);
6465 lang_add_map (const char *name
)
6472 map_option_f
= TRUE
;
6480 lang_add_fill (fill_type
*fill
)
6482 lang_fill_statement_type
*new;
6484 new = new_stat (lang_fill_statement
, stat_ptr
);
6489 lang_add_data (int type
, union etree_union
*exp
)
6491 lang_data_statement_type
*new;
6493 new = new_stat (lang_data_statement
, stat_ptr
);
6498 /* Create a new reloc statement. RELOC is the BFD relocation type to
6499 generate. HOWTO is the corresponding howto structure (we could
6500 look this up, but the caller has already done so). SECTION is the
6501 section to generate a reloc against, or NAME is the name of the
6502 symbol to generate a reloc against. Exactly one of SECTION and
6503 NAME must be NULL. ADDEND is an expression for the addend. */
6506 lang_add_reloc (bfd_reloc_code_real_type reloc
,
6507 reloc_howto_type
*howto
,
6510 union etree_union
*addend
)
6512 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
6516 p
->section
= section
;
6518 p
->addend_exp
= addend
;
6520 p
->addend_value
= 0;
6521 p
->output_section
= NULL
;
6522 p
->output_offset
= 0;
6525 lang_assignment_statement_type
*
6526 lang_add_assignment (etree_type
*exp
)
6528 lang_assignment_statement_type
*new;
6530 new = new_stat (lang_assignment_statement
, stat_ptr
);
6536 lang_add_attribute (enum statement_enum attribute
)
6538 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
6542 lang_startup (const char *name
)
6544 if (startup_file
!= NULL
)
6546 einfo (_("%P%F: multiple STARTUP files\n"));
6548 first_file
->filename
= name
;
6549 first_file
->local_sym_name
= name
;
6550 first_file
->real
= TRUE
;
6552 startup_file
= name
;
6556 lang_float (bfd_boolean maybe
)
6558 lang_float_flag
= maybe
;
6562 /* Work out the load- and run-time regions from a script statement, and
6563 store them in *LMA_REGION and *REGION respectively.
6565 MEMSPEC is the name of the run-time region, or the value of
6566 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6567 LMA_MEMSPEC is the name of the load-time region, or null if the
6568 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6569 had an explicit load address.
6571 It is an error to specify both a load region and a load address. */
6574 lang_get_regions (lang_memory_region_type
**region
,
6575 lang_memory_region_type
**lma_region
,
6576 const char *memspec
,
6577 const char *lma_memspec
,
6578 bfd_boolean have_lma
,
6579 bfd_boolean have_vma
)
6581 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
6583 /* If no runtime region or VMA has been specified, but the load region
6584 has been specified, then use the load region for the runtime region
6586 if (lma_memspec
!= NULL
6588 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
6589 *region
= *lma_region
;
6591 *region
= lang_memory_region_lookup (memspec
, FALSE
);
6593 if (have_lma
&& lma_memspec
!= 0)
6594 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6598 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
6599 lang_output_section_phdr_list
*phdrs
,
6600 const char *lma_memspec
)
6602 lang_get_regions (¤t_section
->region
,
6603 ¤t_section
->lma_region
,
6604 memspec
, lma_memspec
,
6605 current_section
->load_base
!= NULL
,
6606 current_section
->addr_tree
!= NULL
);
6608 /* If this section has no load region or base, but has the same
6609 region as the previous section, then propagate the previous
6610 section's load region. */
6612 if (!current_section
->lma_region
&& !current_section
->load_base
6613 && current_section
->region
== current_section
->prev
->region
)
6614 current_section
->lma_region
= current_section
->prev
->lma_region
;
6616 current_section
->fill
= fill
;
6617 current_section
->phdrs
= phdrs
;
6621 /* Create an absolute symbol with the given name with the value of the
6622 address of first byte of the section named.
6624 If the symbol already exists, then do nothing. */
6627 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
6629 struct bfd_link_hash_entry
*h
;
6631 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6633 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6635 if (h
->type
== bfd_link_hash_new
6636 || h
->type
== bfd_link_hash_undefined
)
6640 h
->type
= bfd_link_hash_defined
;
6642 sec
= bfd_get_section_by_name (link_info
.output_bfd
, secname
);
6646 h
->u
.def
.value
= bfd_get_section_vma (link_info
.output_bfd
, sec
);
6648 h
->u
.def
.section
= bfd_abs_section_ptr
;
6652 /* Create an absolute symbol with the given name with the value of the
6653 address of the first byte after the end of the section named.
6655 If the symbol already exists, then do nothing. */
6658 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
6660 struct bfd_link_hash_entry
*h
;
6662 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6664 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6666 if (h
->type
== bfd_link_hash_new
6667 || h
->type
== bfd_link_hash_undefined
)
6671 h
->type
= bfd_link_hash_defined
;
6673 sec
= bfd_get_section_by_name (link_info
.output_bfd
, secname
);
6677 h
->u
.def
.value
= (bfd_get_section_vma (link_info
.output_bfd
, sec
)
6678 + TO_ADDR (sec
->size
));
6680 h
->u
.def
.section
= bfd_abs_section_ptr
;
6685 lang_statement_append (lang_statement_list_type
*list
,
6686 lang_statement_union_type
*element
,
6687 lang_statement_union_type
**field
)
6689 *(list
->tail
) = element
;
6693 /* Set the output format type. -oformat overrides scripts. */
6696 lang_add_output_format (const char *format
,
6701 if (output_target
== NULL
|| !from_script
)
6703 if (command_line
.endian
== ENDIAN_BIG
6706 else if (command_line
.endian
== ENDIAN_LITTLE
6710 output_target
= format
;
6715 lang_add_insert (const char *where
, int is_before
)
6717 lang_insert_statement_type
*new;
6719 new = new_stat (lang_insert_statement
, stat_ptr
);
6721 new->is_before
= is_before
;
6722 saved_script_handle
= previous_script_handle
;
6725 /* Enter a group. This creates a new lang_group_statement, and sets
6726 stat_ptr to build new statements within the group. */
6729 lang_enter_group (void)
6731 lang_group_statement_type
*g
;
6733 g
= new_stat (lang_group_statement
, stat_ptr
);
6734 lang_list_init (&g
->children
);
6735 push_stat_ptr (&g
->children
);
6738 /* Leave a group. This just resets stat_ptr to start writing to the
6739 regular list of statements again. Note that this will not work if
6740 groups can occur inside anything else which can adjust stat_ptr,
6741 but currently they can't. */
6744 lang_leave_group (void)
6749 /* Add a new program header. This is called for each entry in a PHDRS
6750 command in a linker script. */
6753 lang_new_phdr (const char *name
,
6755 bfd_boolean filehdr
,
6760 struct lang_phdr
*n
, **pp
;
6762 n
= stat_alloc (sizeof (struct lang_phdr
));
6765 n
->type
= exp_get_value_int (type
, 0, "program header type");
6766 n
->filehdr
= filehdr
;
6771 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6776 /* Record the program header information in the output BFD. FIXME: We
6777 should not be calling an ELF specific function here. */
6780 lang_record_phdrs (void)
6784 lang_output_section_phdr_list
*last
;
6785 struct lang_phdr
*l
;
6786 lang_output_section_statement_type
*os
;
6789 secs
= xmalloc (alc
* sizeof (asection
*));
6792 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
6799 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6803 lang_output_section_phdr_list
*pl
;
6805 if (os
->constraint
< 0)
6813 if (os
->sectype
== noload_section
6814 || os
->bfd_section
== NULL
6815 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6818 /* Don't add orphans to PT_INTERP header. */
6824 lang_output_section_statement_type
* tmp_os
;
6826 /* If we have not run across a section with a program
6827 header assigned to it yet, then scan forwards to find
6828 one. This prevents inconsistencies in the linker's
6829 behaviour when a script has specified just a single
6830 header and there are sections in that script which are
6831 not assigned to it, and which occur before the first
6832 use of that header. See here for more details:
6833 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6834 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
6837 last
= tmp_os
->phdrs
;
6841 einfo (_("%F%P: no sections assigned to phdrs\n"));
6846 if (os
->bfd_section
== NULL
)
6849 for (; pl
!= NULL
; pl
= pl
->next
)
6851 if (strcmp (pl
->name
, l
->name
) == 0)
6856 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6858 secs
[c
] = os
->bfd_section
;
6865 if (l
->flags
== NULL
)
6868 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6873 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6875 if (! bfd_record_phdr (link_info
.output_bfd
, l
->type
,
6876 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6877 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6878 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6883 /* Make sure all the phdr assignments succeeded. */
6884 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6888 lang_output_section_phdr_list
*pl
;
6890 if (os
->constraint
< 0
6891 || os
->bfd_section
== NULL
)
6894 for (pl
= os
->phdrs
;
6897 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6898 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6899 os
->name
, pl
->name
);
6903 /* Record a list of sections which may not be cross referenced. */
6906 lang_add_nocrossref (lang_nocrossref_type
*l
)
6908 struct lang_nocrossrefs
*n
;
6910 n
= xmalloc (sizeof *n
);
6911 n
->next
= nocrossref_list
;
6913 nocrossref_list
= n
;
6915 /* Set notice_all so that we get informed about all symbols. */
6916 link_info
.notice_all
= TRUE
;
6919 /* Overlay handling. We handle overlays with some static variables. */
6921 /* The overlay virtual address. */
6922 static etree_type
*overlay_vma
;
6923 /* And subsection alignment. */
6924 static etree_type
*overlay_subalign
;
6926 /* An expression for the maximum section size seen so far. */
6927 static etree_type
*overlay_max
;
6929 /* A list of all the sections in this overlay. */
6931 struct overlay_list
{
6932 struct overlay_list
*next
;
6933 lang_output_section_statement_type
*os
;
6936 static struct overlay_list
*overlay_list
;
6938 /* Start handling an overlay. */
6941 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6943 /* The grammar should prevent nested overlays from occurring. */
6944 ASSERT (overlay_vma
== NULL
6945 && overlay_subalign
== NULL
6946 && overlay_max
== NULL
);
6948 overlay_vma
= vma_expr
;
6949 overlay_subalign
= subalign
;
6952 /* Start a section in an overlay. We handle this by calling
6953 lang_enter_output_section_statement with the correct VMA.
6954 lang_leave_overlay sets up the LMA and memory regions. */
6957 lang_enter_overlay_section (const char *name
)
6959 struct overlay_list
*n
;
6962 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
6963 0, overlay_subalign
, 0, 0);
6965 /* If this is the first section, then base the VMA of future
6966 sections on this one. This will work correctly even if `.' is
6967 used in the addresses. */
6968 if (overlay_list
== NULL
)
6969 overlay_vma
= exp_nameop (ADDR
, name
);
6971 /* Remember the section. */
6972 n
= xmalloc (sizeof *n
);
6973 n
->os
= current_section
;
6974 n
->next
= overlay_list
;
6977 size
= exp_nameop (SIZEOF
, name
);
6979 /* Arrange to work out the maximum section end address. */
6980 if (overlay_max
== NULL
)
6983 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6986 /* Finish a section in an overlay. There isn't any special to do
6990 lang_leave_overlay_section (fill_type
*fill
,
6991 lang_output_section_phdr_list
*phdrs
)
6998 name
= current_section
->name
;
7000 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
7001 region and that no load-time region has been specified. It doesn't
7002 really matter what we say here, since lang_leave_overlay will
7004 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
7006 /* Define the magic symbols. */
7008 clean
= xmalloc (strlen (name
) + 1);
7010 for (s1
= name
; *s1
!= '\0'; s1
++)
7011 if (ISALNUM (*s1
) || *s1
== '_')
7015 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
7016 sprintf (buf
, "__load_start_%s", clean
);
7017 lang_add_assignment (exp_provide (buf
,
7018 exp_nameop (LOADADDR
, name
),
7021 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
7022 sprintf (buf
, "__load_stop_%s", clean
);
7023 lang_add_assignment (exp_provide (buf
,
7025 exp_nameop (LOADADDR
, name
),
7026 exp_nameop (SIZEOF
, name
)),
7032 /* Finish an overlay. If there are any overlay wide settings, this
7033 looks through all the sections in the overlay and sets them. */
7036 lang_leave_overlay (etree_type
*lma_expr
,
7039 const char *memspec
,
7040 lang_output_section_phdr_list
*phdrs
,
7041 const char *lma_memspec
)
7043 lang_memory_region_type
*region
;
7044 lang_memory_region_type
*lma_region
;
7045 struct overlay_list
*l
;
7046 lang_nocrossref_type
*nocrossref
;
7048 lang_get_regions (®ion
, &lma_region
,
7049 memspec
, lma_memspec
,
7050 lma_expr
!= NULL
, FALSE
);
7054 /* After setting the size of the last section, set '.' to end of the
7056 if (overlay_list
!= NULL
)
7057 overlay_list
->os
->update_dot_tree
7058 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
7063 struct overlay_list
*next
;
7065 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
7068 l
->os
->region
= region
;
7069 l
->os
->lma_region
= lma_region
;
7071 /* The first section has the load address specified in the
7072 OVERLAY statement. The rest are worked out from that.
7073 The base address is not needed (and should be null) if
7074 an LMA region was specified. */
7077 l
->os
->load_base
= lma_expr
;
7078 l
->os
->sectype
= normal_section
;
7080 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
7081 l
->os
->phdrs
= phdrs
;
7085 lang_nocrossref_type
*nc
;
7087 nc
= xmalloc (sizeof *nc
);
7088 nc
->name
= l
->os
->name
;
7089 nc
->next
= nocrossref
;
7098 if (nocrossref
!= NULL
)
7099 lang_add_nocrossref (nocrossref
);
7102 overlay_list
= NULL
;
7106 /* Version handling. This is only useful for ELF. */
7108 /* This global variable holds the version tree that we build. */
7110 struct bfd_elf_version_tree
*lang_elf_version_info
;
7112 /* If PREV is NULL, return first version pattern matching particular symbol.
7113 If PREV is non-NULL, return first version pattern matching particular
7114 symbol after PREV (previously returned by lang_vers_match). */
7116 static struct bfd_elf_version_expr
*
7117 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
7118 struct bfd_elf_version_expr
*prev
,
7121 const char *cxx_sym
= sym
;
7122 const char *java_sym
= sym
;
7123 struct bfd_elf_version_expr
*expr
= NULL
;
7125 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
7127 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
7131 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
7133 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
7138 if (head
->htab
&& (prev
== NULL
|| prev
->literal
))
7140 struct bfd_elf_version_expr e
;
7142 switch (prev
? prev
->mask
: 0)
7145 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
7148 expr
= htab_find (head
->htab
, &e
);
7149 while (expr
&& strcmp (expr
->pattern
, sym
) == 0)
7150 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
7156 case BFD_ELF_VERSION_C_TYPE
:
7157 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
7159 e
.pattern
= cxx_sym
;
7160 expr
= htab_find (head
->htab
, &e
);
7161 while (expr
&& strcmp (expr
->pattern
, cxx_sym
) == 0)
7162 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
7168 case BFD_ELF_VERSION_CXX_TYPE
:
7169 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
7171 e
.pattern
= java_sym
;
7172 expr
= htab_find (head
->htab
, &e
);
7173 while (expr
&& strcmp (expr
->pattern
, java_sym
) == 0)
7174 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
7185 /* Finally, try the wildcards. */
7186 if (prev
== NULL
|| prev
->literal
)
7187 expr
= head
->remaining
;
7190 for (; expr
; expr
= expr
->next
)
7197 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
7200 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
7202 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
7206 if (fnmatch (expr
->pattern
, s
, 0) == 0)
7212 free ((char *) cxx_sym
);
7213 if (java_sym
!= sym
)
7214 free ((char *) java_sym
);
7218 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
7219 return a pointer to the symbol name with any backslash quotes removed. */
7222 realsymbol (const char *pattern
)
7225 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
7226 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
7228 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
7230 /* It is a glob pattern only if there is no preceding
7234 /* Remove the preceding backslash. */
7241 if (*p
== '?' || *p
== '*' || *p
== '[')
7248 backslash
= *p
== '\\';
7264 /* This is called for each variable name or match expression. NEW is
7265 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7266 pattern to be matched against symbol names. */
7268 struct bfd_elf_version_expr
*
7269 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
7272 bfd_boolean literal_p
)
7274 struct bfd_elf_version_expr
*ret
;
7276 ret
= xmalloc (sizeof *ret
);
7280 ret
->literal
= TRUE
;
7281 ret
->pattern
= literal_p
? new : realsymbol (new);
7282 if (ret
->pattern
== NULL
)
7285 ret
->literal
= FALSE
;
7288 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
7289 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7290 else if (strcasecmp (lang
, "C++") == 0)
7291 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
7292 else if (strcasecmp (lang
, "Java") == 0)
7293 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
7296 einfo (_("%X%P: unknown language `%s' in version information\n"),
7298 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7301 return ldemul_new_vers_pattern (ret
);
7304 /* This is called for each set of variable names and match
7307 struct bfd_elf_version_tree
*
7308 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
7309 struct bfd_elf_version_expr
*locals
)
7311 struct bfd_elf_version_tree
*ret
;
7313 ret
= xcalloc (1, sizeof *ret
);
7314 ret
->globals
.list
= globals
;
7315 ret
->locals
.list
= locals
;
7316 ret
->match
= lang_vers_match
;
7317 ret
->name_indx
= (unsigned int) -1;
7321 /* This static variable keeps track of version indices. */
7323 static int version_index
;
7326 version_expr_head_hash (const void *p
)
7328 const struct bfd_elf_version_expr
*e
= p
;
7330 return htab_hash_string (e
->pattern
);
7334 version_expr_head_eq (const void *p1
, const void *p2
)
7336 const struct bfd_elf_version_expr
*e1
= p1
;
7337 const struct bfd_elf_version_expr
*e2
= p2
;
7339 return strcmp (e1
->pattern
, e2
->pattern
) == 0;
7343 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
7346 struct bfd_elf_version_expr
*e
, *next
;
7347 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
7349 for (e
= head
->list
; e
; e
= e
->next
)
7353 head
->mask
|= e
->mask
;
7358 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
7359 version_expr_head_eq
, NULL
);
7360 list_loc
= &head
->list
;
7361 remaining_loc
= &head
->remaining
;
7362 for (e
= head
->list
; e
; e
= next
)
7368 remaining_loc
= &e
->next
;
7372 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
7376 struct bfd_elf_version_expr
*e1
, *last
;
7382 if (e1
->mask
== e
->mask
)
7390 while (e1
&& strcmp (e1
->pattern
, e
->pattern
) == 0);
7394 /* This is a duplicate. */
7395 /* FIXME: Memory leak. Sometimes pattern is not
7396 xmalloced alone, but in larger chunk of memory. */
7397 /* free (e->pattern); */
7402 e
->next
= last
->next
;
7410 list_loc
= &e
->next
;
7414 *remaining_loc
= NULL
;
7415 *list_loc
= head
->remaining
;
7418 head
->remaining
= head
->list
;
7421 /* This is called when we know the name and dependencies of the
7425 lang_register_vers_node (const char *name
,
7426 struct bfd_elf_version_tree
*version
,
7427 struct bfd_elf_version_deps
*deps
)
7429 struct bfd_elf_version_tree
*t
, **pp
;
7430 struct bfd_elf_version_expr
*e1
;
7435 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
7436 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
7438 einfo (_("%X%P: anonymous version tag cannot be combined"
7439 " with other version tags\n"));
7444 /* Make sure this node has a unique name. */
7445 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7446 if (strcmp (t
->name
, name
) == 0)
7447 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
7449 lang_finalize_version_expr_head (&version
->globals
);
7450 lang_finalize_version_expr_head (&version
->locals
);
7452 /* Check the global and local match names, and make sure there
7453 aren't any duplicates. */
7455 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
7457 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7459 struct bfd_elf_version_expr
*e2
;
7461 if (t
->locals
.htab
&& e1
->literal
)
7463 e2
= htab_find (t
->locals
.htab
, e1
);
7464 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
7466 if (e1
->mask
== e2
->mask
)
7467 einfo (_("%X%P: duplicate expression `%s'"
7468 " in version information\n"), e1
->pattern
);
7472 else if (!e1
->literal
)
7473 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7474 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7475 && e1
->mask
== e2
->mask
)
7476 einfo (_("%X%P: duplicate expression `%s'"
7477 " in version information\n"), e1
->pattern
);
7481 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
7483 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7485 struct bfd_elf_version_expr
*e2
;
7487 if (t
->globals
.htab
&& e1
->literal
)
7489 e2
= htab_find (t
->globals
.htab
, e1
);
7490 while (e2
&& strcmp (e1
->pattern
, e2
->pattern
) == 0)
7492 if (e1
->mask
== e2
->mask
)
7493 einfo (_("%X%P: duplicate expression `%s'"
7494 " in version information\n"),
7499 else if (!e1
->literal
)
7500 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7501 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7502 && e1
->mask
== e2
->mask
)
7503 einfo (_("%X%P: duplicate expression `%s'"
7504 " in version information\n"), e1
->pattern
);
7508 version
->deps
= deps
;
7509 version
->name
= name
;
7510 if (name
[0] != '\0')
7513 version
->vernum
= version_index
;
7516 version
->vernum
= 0;
7518 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
7523 /* This is called when we see a version dependency. */
7525 struct bfd_elf_version_deps
*
7526 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
7528 struct bfd_elf_version_deps
*ret
;
7529 struct bfd_elf_version_tree
*t
;
7531 ret
= xmalloc (sizeof *ret
);
7534 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7536 if (strcmp (t
->name
, name
) == 0)
7538 ret
->version_needed
= t
;
7543 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
7549 lang_do_version_exports_section (void)
7551 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
7553 LANG_FOR_EACH_INPUT_STATEMENT (is
)
7555 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
7563 contents
= xmalloc (len
);
7564 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
7565 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
7568 while (p
< contents
+ len
)
7570 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
7571 p
= strchr (p
, '\0') + 1;
7574 /* Do not free the contents, as we used them creating the regex. */
7576 /* Do not include this section in the link. */
7577 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
7580 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
7581 lang_register_vers_node (command_line
.version_exports_section
,
7582 lang_new_vers_node (greg
, lreg
), NULL
);
7586 lang_add_unique (const char *name
)
7588 struct unique_sections
*ent
;
7590 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
7591 if (strcmp (ent
->name
, name
) == 0)
7594 ent
= xmalloc (sizeof *ent
);
7595 ent
->name
= xstrdup (name
);
7596 ent
->next
= unique_section_list
;
7597 unique_section_list
= ent
;
7600 /* Append the list of dynamic symbols to the existing one. */
7603 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
7605 if (link_info
.dynamic_list
)
7607 struct bfd_elf_version_expr
*tail
;
7608 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
7610 tail
->next
= link_info
.dynamic_list
->head
.list
;
7611 link_info
.dynamic_list
->head
.list
= dynamic
;
7615 struct bfd_elf_dynamic_list
*d
;
7617 d
= xcalloc (1, sizeof *d
);
7618 d
->head
.list
= dynamic
;
7619 d
->match
= lang_vers_match
;
7620 link_info
.dynamic_list
= d
;
7624 /* Append the list of C++ typeinfo dynamic symbols to the existing
7628 lang_append_dynamic_list_cpp_typeinfo (void)
7630 const char * symbols
[] =
7632 "typeinfo name for*",
7635 struct bfd_elf_version_expr
*dynamic
= NULL
;
7638 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7639 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7642 lang_append_dynamic_list (dynamic
);
7645 /* Append the list of C++ operator new and delete dynamic symbols to the
7649 lang_append_dynamic_list_cpp_new (void)
7651 const char * symbols
[] =
7656 struct bfd_elf_version_expr
*dynamic
= NULL
;
7659 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7660 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7663 lang_append_dynamic_list (dynamic
);