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. */
27 #include "libiberty.h"
28 #include "safe-ctype.h"
47 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
50 /* Locals variables. */
51 static struct obstack stat_obstack
;
52 static struct obstack map_obstack
;
54 #define obstack_chunk_alloc xmalloc
55 #define obstack_chunk_free free
56 static const char *startup_file
;
57 static bfd_boolean placed_commons
= FALSE
;
58 static bfd_boolean stripped_excluded_sections
= FALSE
;
59 static lang_output_section_statement_type
*default_common_section
;
60 static bfd_boolean map_option_f
;
61 static bfd_vma print_dot
;
62 static lang_input_statement_type
*first_file
;
63 static const char *current_target
;
64 static const char *output_target
;
65 static lang_statement_list_type statement_list
;
66 static struct bfd_hash_table lang_definedness_table
;
67 static lang_statement_list_type
*stat_save
[10];
68 static lang_statement_list_type
**stat_save_ptr
= &stat_save
[0];
70 /* Forward declarations. */
71 static void exp_init_os (etree_type
*);
72 static void init_map_userdata (bfd
*, asection
*, void *);
73 static lang_input_statement_type
*lookup_name (const char *);
74 static struct bfd_hash_entry
*lang_definedness_newfunc
75 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
76 static void insert_undefined (const char *);
77 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
78 static void print_statement (lang_statement_union_type
*,
79 lang_output_section_statement_type
*);
80 static void print_statement_list (lang_statement_union_type
*,
81 lang_output_section_statement_type
*);
82 static void print_statements (void);
83 static void print_input_section (asection
*);
84 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
85 static void lang_record_phdrs (void);
86 static void lang_do_version_exports_section (void);
87 static void lang_finalize_version_expr_head
88 (struct bfd_elf_version_expr_head
*);
90 /* Exported variables. */
91 lang_output_section_statement_type
*abs_output_section
;
92 lang_statement_list_type lang_output_section_statement
;
93 lang_statement_list_type
*stat_ptr
= &statement_list
;
94 lang_statement_list_type file_chain
= { NULL
, NULL
};
95 lang_statement_list_type input_file_chain
;
96 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
97 static const char *entry_symbol_default
= "start";
98 const char *entry_section
= ".text";
99 bfd_boolean entry_from_cmdline
;
100 bfd_boolean lang_has_input_file
= FALSE
;
101 bfd_boolean had_output_filename
= FALSE
;
102 bfd_boolean lang_float_flag
= FALSE
;
103 bfd_boolean delete_output_file_on_failure
= FALSE
;
104 struct lang_phdr
*lang_phdr_list
;
105 struct lang_nocrossrefs
*nocrossref_list
;
106 static struct unique_sections
*unique_section_list
;
107 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
109 /* Functions that traverse the linker script and might evaluate
110 DEFINED() need to increment this. */
111 int lang_statement_iteration
= 0;
113 etree_type
*base
; /* Relocation base - or null */
115 /* Return TRUE if the PATTERN argument is a wildcard pattern.
116 Although backslashes are treated specially if a pattern contains
117 wildcards, we do not consider the mere presence of a backslash to
118 be enough to cause the pattern to be treated as a wildcard.
119 That lets us handle DOS filenames more naturally. */
120 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
122 #define new_stat(x, y) \
123 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
125 #define outside_section_address(q) \
126 ((q)->output_offset + (q)->output_section->vma)
128 #define outside_symbol_address(q) \
129 ((q)->value + outside_section_address (q->section))
131 #define SECTION_NAME_MAP_LENGTH (16)
134 stat_alloc (size_t size
)
136 return obstack_alloc (&stat_obstack
, size
);
140 name_match (const char *pattern
, const char *name
)
142 if (wildcardp (pattern
))
143 return fnmatch (pattern
, name
, 0);
144 return strcmp (pattern
, name
);
147 /* If PATTERN is of the form archive:file, return a pointer to the
148 separator. If not, return NULL. */
151 archive_path (const char *pattern
)
155 if (link_info
.path_separator
== 0)
158 p
= strchr (pattern
, link_info
.path_separator
);
159 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
160 if (p
== NULL
|| link_info
.path_separator
!= ':')
163 /* Assume a match on the second char is part of drive specifier,
164 as in "c:\silly.dos". */
165 if (p
== pattern
+ 1 && ISALPHA (*pattern
))
166 p
= strchr (p
+ 1, link_info
.path_separator
);
171 /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
172 return whether F matches FILE_SPEC. */
175 input_statement_is_archive_path (const char *file_spec
, char *sep
,
176 lang_input_statement_type
*f
)
178 bfd_boolean match
= FALSE
;
181 || name_match (sep
+ 1, f
->filename
) == 0)
182 && ((sep
!= file_spec
)
183 == (f
->the_bfd
!= NULL
&& f
->the_bfd
->my_archive
!= NULL
)))
187 if (sep
!= file_spec
)
189 const char *aname
= f
->the_bfd
->my_archive
->filename
;
191 match
= name_match (file_spec
, aname
) == 0;
192 *sep
= link_info
.path_separator
;
199 unique_section_p (const asection
*sec
)
201 struct unique_sections
*unam
;
204 if (link_info
.relocatable
205 && sec
->owner
!= NULL
206 && bfd_is_group_section (sec
->owner
, sec
))
210 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
211 if (name_match (unam
->name
, secnam
) == 0)
217 /* Generic traversal routines for finding matching sections. */
219 /* Try processing a section against a wildcard. This just calls
220 the callback unless the filename exclusion list is present
221 and excludes the file. It's hardly ever present so this
222 function is very fast. */
225 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
226 lang_input_statement_type
*file
,
228 struct wildcard_list
*sec
,
232 struct name_list
*list_tmp
;
234 /* Don't process sections from files which were excluded. */
235 for (list_tmp
= sec
->spec
.exclude_name_list
;
237 list_tmp
= list_tmp
->next
)
239 char *p
= archive_path (list_tmp
->name
);
243 if (input_statement_is_archive_path (list_tmp
->name
, p
, file
))
247 else if (name_match (list_tmp
->name
, file
->filename
) == 0)
250 /* FIXME: Perhaps remove the following at some stage? Matching
251 unadorned archives like this was never documented and has
252 been superceded by the archive:path syntax. */
253 else if (file
->the_bfd
!= NULL
254 && file
->the_bfd
->my_archive
!= NULL
255 && name_match (list_tmp
->name
,
256 file
->the_bfd
->my_archive
->filename
) == 0)
260 (*callback
) (ptr
, sec
, s
, file
, data
);
263 /* Lowest common denominator routine that can handle everything correctly,
267 walk_wild_section_general (lang_wild_statement_type
*ptr
,
268 lang_input_statement_type
*file
,
273 struct wildcard_list
*sec
;
275 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
277 sec
= ptr
->section_list
;
279 (*callback
) (ptr
, sec
, s
, file
, data
);
283 bfd_boolean skip
= FALSE
;
285 if (sec
->spec
.name
!= NULL
)
287 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
289 skip
= name_match (sec
->spec
.name
, sname
) != 0;
293 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
300 /* Routines to find a single section given its name. If there's more
301 than one section with that name, we report that. */
305 asection
*found_section
;
306 bfd_boolean multiple_sections_found
;
307 } section_iterator_callback_data
;
310 section_iterator_callback (bfd
*bfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
312 section_iterator_callback_data
*d
= data
;
314 if (d
->found_section
!= NULL
)
316 d
->multiple_sections_found
= TRUE
;
320 d
->found_section
= s
;
325 find_section (lang_input_statement_type
*file
,
326 struct wildcard_list
*sec
,
327 bfd_boolean
*multiple_sections_found
)
329 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
331 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
332 section_iterator_callback
, &cb_data
);
333 *multiple_sections_found
= cb_data
.multiple_sections_found
;
334 return cb_data
.found_section
;
337 /* Code for handling simple wildcards without going through fnmatch,
338 which can be expensive because of charset translations etc. */
340 /* A simple wild is a literal string followed by a single '*',
341 where the literal part is at least 4 characters long. */
344 is_simple_wild (const char *name
)
346 size_t len
= strcspn (name
, "*?[");
347 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
351 match_simple_wild (const char *pattern
, const char *name
)
353 /* The first four characters of the pattern are guaranteed valid
354 non-wildcard characters. So we can go faster. */
355 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
356 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
361 while (*pattern
!= '*')
362 if (*name
++ != *pattern
++)
368 /* Compare sections ASEC and BSEC according to SORT. */
371 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
380 case by_alignment_name
:
381 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
382 - bfd_section_alignment (asec
->owner
, asec
));
388 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
389 bfd_get_section_name (bsec
->owner
, bsec
));
392 case by_name_alignment
:
393 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
394 bfd_get_section_name (bsec
->owner
, bsec
));
400 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
401 - bfd_section_alignment (asec
->owner
, asec
));
408 /* Build a Binary Search Tree to sort sections, unlike insertion sort
409 used in wild_sort(). BST is considerably faster if the number of
410 of sections are large. */
412 static lang_section_bst_type
**
413 wild_sort_fast (lang_wild_statement_type
*wild
,
414 struct wildcard_list
*sec
,
415 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
418 lang_section_bst_type
**tree
;
421 if (!wild
->filenames_sorted
422 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
424 /* Append at the right end of tree. */
426 tree
= &((*tree
)->right
);
432 /* Find the correct node to append this section. */
433 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
434 tree
= &((*tree
)->left
);
436 tree
= &((*tree
)->right
);
442 /* Use wild_sort_fast to build a BST to sort sections. */
445 output_section_callback_fast (lang_wild_statement_type
*ptr
,
446 struct wildcard_list
*sec
,
448 lang_input_statement_type
*file
,
449 void *output ATTRIBUTE_UNUSED
)
451 lang_section_bst_type
*node
;
452 lang_section_bst_type
**tree
;
454 if (unique_section_p (section
))
457 node
= xmalloc (sizeof (lang_section_bst_type
));
460 node
->section
= section
;
462 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
467 /* Convert a sorted sections' BST back to list form. */
470 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
471 lang_section_bst_type
*tree
,
475 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
477 lang_add_section (&ptr
->children
, tree
->section
,
478 (lang_output_section_statement_type
*) output
);
481 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
486 /* Specialized, optimized routines for handling different kinds of
490 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
491 lang_input_statement_type
*file
,
495 /* We can just do a hash lookup for the section with the right name.
496 But if that lookup discovers more than one section with the name
497 (should be rare), we fall back to the general algorithm because
498 we would otherwise have to sort the sections to make sure they
499 get processed in the bfd's order. */
500 bfd_boolean multiple_sections_found
;
501 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
502 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
504 if (multiple_sections_found
)
505 walk_wild_section_general (ptr
, file
, callback
, data
);
507 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
511 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
512 lang_input_statement_type
*file
,
517 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
519 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
521 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
522 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
525 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
530 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
531 lang_input_statement_type
*file
,
536 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
537 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
538 bfd_boolean multiple_sections_found
;
539 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
541 if (multiple_sections_found
)
543 walk_wild_section_general (ptr
, file
, callback
, data
);
547 /* Note that if the section was not found, s0 is NULL and
548 we'll simply never succeed the s == s0 test below. */
549 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
551 /* Recall that in this code path, a section cannot satisfy more
552 than one spec, so if s == s0 then it cannot match
555 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
558 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
559 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
562 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
569 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
570 lang_input_statement_type
*file
,
575 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
576 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
577 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
578 bfd_boolean multiple_sections_found
;
579 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
581 if (multiple_sections_found
)
583 walk_wild_section_general (ptr
, file
, callback
, data
);
587 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
590 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
593 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
594 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
597 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
600 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
602 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
610 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
611 lang_input_statement_type
*file
,
616 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
617 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
618 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
619 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
620 bfd_boolean multiple_sections_found
;
621 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
623 if (multiple_sections_found
)
625 walk_wild_section_general (ptr
, file
, callback
, data
);
629 s1
= find_section (file
, sec1
, &multiple_sections_found
);
630 if (multiple_sections_found
)
632 walk_wild_section_general (ptr
, file
, callback
, data
);
636 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
639 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
642 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
645 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
646 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
650 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
654 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
656 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
664 walk_wild_section (lang_wild_statement_type
*ptr
,
665 lang_input_statement_type
*file
,
669 if (file
->just_syms_flag
)
672 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
675 /* Returns TRUE when name1 is a wildcard spec that might match
676 something name2 can match. We're conservative: we return FALSE
677 only if the prefixes of name1 and name2 are different up to the
678 first wildcard character. */
681 wild_spec_can_overlap (const char *name1
, const char *name2
)
683 size_t prefix1_len
= strcspn (name1
, "?*[");
684 size_t prefix2_len
= strcspn (name2
, "?*[");
685 size_t min_prefix_len
;
687 /* Note that if there is no wildcard character, then we treat the
688 terminating 0 as part of the prefix. Thus ".text" won't match
689 ".text." or ".text.*", for example. */
690 if (name1
[prefix1_len
] == '\0')
692 if (name2
[prefix2_len
] == '\0')
695 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
697 return memcmp (name1
, name2
, min_prefix_len
) == 0;
700 /* Select specialized code to handle various kinds of wildcard
704 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
707 int wild_name_count
= 0;
708 struct wildcard_list
*sec
;
712 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
713 ptr
->handler_data
[0] = NULL
;
714 ptr
->handler_data
[1] = NULL
;
715 ptr
->handler_data
[2] = NULL
;
716 ptr
->handler_data
[3] = NULL
;
719 /* Count how many wildcard_specs there are, and how many of those
720 actually use wildcards in the name. Also, bail out if any of the
721 wildcard names are NULL. (Can this actually happen?
722 walk_wild_section used to test for it.) And bail out if any
723 of the wildcards are more complex than a simple string
724 ending in a single '*'. */
725 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
728 if (sec
->spec
.name
== NULL
)
730 if (wildcardp (sec
->spec
.name
))
733 if (!is_simple_wild (sec
->spec
.name
))
738 /* The zero-spec case would be easy to optimize but it doesn't
739 happen in practice. Likewise, more than 4 specs doesn't
740 happen in practice. */
741 if (sec_count
== 0 || sec_count
> 4)
744 /* Check that no two specs can match the same section. */
745 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
747 struct wildcard_list
*sec2
;
748 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
750 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
755 signature
= (sec_count
<< 8) + wild_name_count
;
759 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
762 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
765 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
768 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
771 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
777 /* Now fill the data array with pointers to the specs, first the
778 specs with non-wildcard names, then the specs with wildcard
779 names. It's OK to process the specs in different order from the
780 given order, because we've already determined that no section
781 will match more than one spec. */
783 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
784 if (!wildcardp (sec
->spec
.name
))
785 ptr
->handler_data
[data_counter
++] = sec
;
786 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
787 if (wildcardp (sec
->spec
.name
))
788 ptr
->handler_data
[data_counter
++] = sec
;
791 /* Handle a wild statement for a single file F. */
794 walk_wild_file (lang_wild_statement_type
*s
,
795 lang_input_statement_type
*f
,
799 if (f
->the_bfd
== NULL
800 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
801 walk_wild_section (s
, f
, callback
, data
);
806 /* This is an archive file. We must map each member of the
807 archive separately. */
808 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
809 while (member
!= NULL
)
811 /* When lookup_name is called, it will call the add_symbols
812 entry point for the archive. For each element of the
813 archive which is included, BFD will call ldlang_add_file,
814 which will set the usrdata field of the member to the
815 lang_input_statement. */
816 if (member
->usrdata
!= NULL
)
818 walk_wild_section (s
, member
->usrdata
, callback
, data
);
821 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
827 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
829 const char *file_spec
= s
->filename
;
832 if (file_spec
== NULL
)
834 /* Perform the iteration over all files in the list. */
835 LANG_FOR_EACH_INPUT_STATEMENT (f
)
837 walk_wild_file (s
, f
, callback
, data
);
840 else if ((p
= archive_path (file_spec
)) != NULL
)
842 LANG_FOR_EACH_INPUT_STATEMENT (f
)
844 if (input_statement_is_archive_path (file_spec
, p
, f
))
845 walk_wild_file (s
, f
, callback
, data
);
848 else if (wildcardp (file_spec
))
850 LANG_FOR_EACH_INPUT_STATEMENT (f
)
852 if (fnmatch (file_spec
, f
->filename
, 0) == 0)
853 walk_wild_file (s
, f
, callback
, data
);
858 lang_input_statement_type
*f
;
860 /* Perform the iteration over a single file. */
861 f
= lookup_name (file_spec
);
863 walk_wild_file (s
, f
, callback
, data
);
867 /* lang_for_each_statement walks the parse tree and calls the provided
868 function for each node. */
871 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
872 lang_statement_union_type
*s
)
874 for (; s
!= NULL
; s
= s
->header
.next
)
878 switch (s
->header
.type
)
880 case lang_constructors_statement_enum
:
881 lang_for_each_statement_worker (func
, constructor_list
.head
);
883 case lang_output_section_statement_enum
:
884 lang_for_each_statement_worker
885 (func
, s
->output_section_statement
.children
.head
);
887 case lang_wild_statement_enum
:
888 lang_for_each_statement_worker (func
,
889 s
->wild_statement
.children
.head
);
891 case lang_group_statement_enum
:
892 lang_for_each_statement_worker (func
,
893 s
->group_statement
.children
.head
);
895 case lang_data_statement_enum
:
896 case lang_reloc_statement_enum
:
897 case lang_object_symbols_statement_enum
:
898 case lang_output_statement_enum
:
899 case lang_target_statement_enum
:
900 case lang_input_section_enum
:
901 case lang_input_statement_enum
:
902 case lang_assignment_statement_enum
:
903 case lang_padding_statement_enum
:
904 case lang_address_statement_enum
:
905 case lang_fill_statement_enum
:
906 case lang_insert_statement_enum
:
916 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
918 lang_for_each_statement_worker (func
, statement_list
.head
);
921 /*----------------------------------------------------------------------*/
924 lang_list_init (lang_statement_list_type
*list
)
927 list
->tail
= &list
->head
;
931 push_stat_ptr (lang_statement_list_type
*new_ptr
)
933 if (stat_save_ptr
>= stat_save
+ sizeof (stat_save
) / sizeof (stat_save
[0]))
935 *stat_save_ptr
++ = stat_ptr
;
942 if (stat_save_ptr
<= stat_save
)
944 stat_ptr
= *--stat_save_ptr
;
947 /* Build a new statement node for the parse tree. */
949 static lang_statement_union_type
*
950 new_statement (enum statement_enum type
,
952 lang_statement_list_type
*list
)
954 lang_statement_union_type
*new;
956 new = stat_alloc (size
);
957 new->header
.type
= type
;
958 new->header
.next
= NULL
;
959 lang_statement_append (list
, new, &new->header
.next
);
963 /* Build a new input file node for the language. There are several
964 ways in which we treat an input file, eg, we only look at symbols,
965 or prefix it with a -l etc.
967 We can be supplied with requests for input files more than once;
968 they may, for example be split over several lines like foo.o(.text)
969 foo.o(.data) etc, so when asked for a file we check that we haven't
970 got it already so we don't duplicate the bfd. */
972 static lang_input_statement_type
*
973 new_afile (const char *name
,
974 lang_input_file_enum_type file_type
,
976 bfd_boolean add_to_list
)
978 lang_input_statement_type
*p
;
981 p
= new_stat (lang_input_statement
, stat_ptr
);
984 p
= stat_alloc (sizeof (lang_input_statement_type
));
985 p
->header
.type
= lang_input_statement_enum
;
986 p
->header
.next
= NULL
;
989 lang_has_input_file
= TRUE
;
991 p
->sysrooted
= FALSE
;
993 if (file_type
== lang_input_file_is_l_enum
994 && name
[0] == ':' && name
[1] != '\0')
996 file_type
= lang_input_file_is_search_file_enum
;
1002 case lang_input_file_is_symbols_only_enum
:
1004 p
->is_archive
= FALSE
;
1006 p
->local_sym_name
= name
;
1007 p
->just_syms_flag
= TRUE
;
1008 p
->search_dirs_flag
= FALSE
;
1010 case lang_input_file_is_fake_enum
:
1012 p
->is_archive
= FALSE
;
1014 p
->local_sym_name
= name
;
1015 p
->just_syms_flag
= FALSE
;
1016 p
->search_dirs_flag
= FALSE
;
1018 case lang_input_file_is_l_enum
:
1019 p
->is_archive
= TRUE
;
1022 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
1023 p
->just_syms_flag
= FALSE
;
1024 p
->search_dirs_flag
= TRUE
;
1026 case lang_input_file_is_marker_enum
:
1028 p
->is_archive
= FALSE
;
1030 p
->local_sym_name
= name
;
1031 p
->just_syms_flag
= FALSE
;
1032 p
->search_dirs_flag
= TRUE
;
1034 case lang_input_file_is_search_file_enum
:
1035 p
->sysrooted
= ldlang_sysrooted_script
;
1037 p
->is_archive
= FALSE
;
1039 p
->local_sym_name
= name
;
1040 p
->just_syms_flag
= FALSE
;
1041 p
->search_dirs_flag
= TRUE
;
1043 case lang_input_file_is_file_enum
:
1045 p
->is_archive
= FALSE
;
1047 p
->local_sym_name
= name
;
1048 p
->just_syms_flag
= FALSE
;
1049 p
->search_dirs_flag
= FALSE
;
1055 p
->next_real_file
= NULL
;
1057 p
->dynamic
= config
.dynamic_link
;
1058 p
->add_needed
= add_needed
;
1059 p
->as_needed
= as_needed
;
1060 p
->whole_archive
= whole_archive
;
1062 lang_statement_append (&input_file_chain
,
1063 (lang_statement_union_type
*) p
,
1064 &p
->next_real_file
);
1068 lang_input_statement_type
*
1069 lang_add_input_file (const char *name
,
1070 lang_input_file_enum_type file_type
,
1073 return new_afile (name
, file_type
, target
, TRUE
);
1076 struct out_section_hash_entry
1078 struct bfd_hash_entry root
;
1079 lang_statement_union_type s
;
1082 /* The hash table. */
1084 static struct bfd_hash_table output_section_statement_table
;
1086 /* Support routines for the hash table used by lang_output_section_find,
1087 initialize the table, fill in an entry and remove the table. */
1089 static struct bfd_hash_entry
*
1090 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1091 struct bfd_hash_table
*table
,
1094 lang_output_section_statement_type
**nextp
;
1095 struct out_section_hash_entry
*ret
;
1099 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
1104 entry
= bfd_hash_newfunc (entry
, table
, string
);
1108 ret
= (struct out_section_hash_entry
*) entry
;
1109 memset (&ret
->s
, 0, sizeof (ret
->s
));
1110 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1111 ret
->s
.output_section_statement
.subsection_alignment
= -1;
1112 ret
->s
.output_section_statement
.section_alignment
= -1;
1113 ret
->s
.output_section_statement
.block_value
= 1;
1114 lang_list_init (&ret
->s
.output_section_statement
.children
);
1115 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1117 /* For every output section statement added to the list, except the
1118 first one, lang_output_section_statement.tail points to the "next"
1119 field of the last element of the list. */
1120 if (lang_output_section_statement
.head
!= NULL
)
1121 ret
->s
.output_section_statement
.prev
1122 = ((lang_output_section_statement_type
*)
1123 ((char *) lang_output_section_statement
.tail
1124 - offsetof (lang_output_section_statement_type
, next
)));
1126 /* GCC's strict aliasing rules prevent us from just casting the
1127 address, so we store the pointer in a variable and cast that
1129 nextp
= &ret
->s
.output_section_statement
.next
;
1130 lang_statement_append (&lang_output_section_statement
,
1132 (lang_statement_union_type
**) nextp
);
1137 output_section_statement_table_init (void)
1139 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1140 output_section_statement_newfunc
,
1141 sizeof (struct out_section_hash_entry
),
1143 einfo (_("%P%F: can not create hash table: %E\n"));
1147 output_section_statement_table_free (void)
1149 bfd_hash_table_free (&output_section_statement_table
);
1152 /* Build enough state so that the parser can build its tree. */
1157 obstack_begin (&stat_obstack
, 1000);
1159 stat_ptr
= &statement_list
;
1161 output_section_statement_table_init ();
1163 lang_list_init (stat_ptr
);
1165 lang_list_init (&input_file_chain
);
1166 lang_list_init (&lang_output_section_statement
);
1167 lang_list_init (&file_chain
);
1168 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1170 abs_output_section
=
1171 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
, 0, TRUE
);
1173 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1175 /* The value "3" is ad-hoc, somewhat related to the expected number of
1176 DEFINED expressions in a linker script. For most default linker
1177 scripts, there are none. Why a hash table then? Well, it's somewhat
1178 simpler to re-use working machinery than using a linked list in terms
1179 of code-complexity here in ld, besides the initialization which just
1180 looks like other code here. */
1181 if (!bfd_hash_table_init_n (&lang_definedness_table
,
1182 lang_definedness_newfunc
,
1183 sizeof (struct lang_definedness_hash_entry
),
1185 einfo (_("%P%F: can not create hash table: %E\n"));
1191 output_section_statement_table_free ();
1194 /*----------------------------------------------------------------------
1195 A region is an area of memory declared with the
1196 MEMORY { name:org=exp, len=exp ... }
1199 We maintain a list of all the regions here.
1201 If no regions are specified in the script, then the default is used
1202 which is created when looked up to be the entire data space.
1204 If create is true we are creating a region inside a MEMORY block.
1205 In this case it is probably an error to create a region that has
1206 already been created. If we are not inside a MEMORY block it is
1207 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1208 and so we issue a warning. */
1210 static lang_memory_region_type
*lang_memory_region_list
;
1211 static lang_memory_region_type
**lang_memory_region_list_tail
1212 = &lang_memory_region_list
;
1214 lang_memory_region_type
*
1215 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1217 lang_memory_region_type
*p
;
1218 lang_memory_region_type
*new;
1220 /* NAME is NULL for LMA memspecs if no region was specified. */
1224 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1225 if (strcmp (p
->name
, name
) == 0)
1228 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1233 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1234 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
1236 new = stat_alloc (sizeof (lang_memory_region_type
));
1238 new->name
= xstrdup (name
);
1241 new->length
= ~(bfd_size_type
) 0;
1243 new->last_os
= NULL
;
1246 new->had_full_message
= FALSE
;
1248 *lang_memory_region_list_tail
= new;
1249 lang_memory_region_list_tail
= &new->next
;
1254 static lang_memory_region_type
*
1255 lang_memory_default (asection
*section
)
1257 lang_memory_region_type
*p
;
1259 flagword sec_flags
= section
->flags
;
1261 /* Override SEC_DATA to mean a writable section. */
1262 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1263 sec_flags
|= SEC_DATA
;
1265 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1267 if ((p
->flags
& sec_flags
) != 0
1268 && (p
->not_flags
& sec_flags
) == 0)
1273 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1276 lang_output_section_statement_type
*
1277 lang_output_section_statement_lookup (const char *const name
,
1281 struct out_section_hash_entry
*entry
;
1283 entry
= ((struct out_section_hash_entry
*)
1284 bfd_hash_lookup (&output_section_statement_table
, name
,
1289 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1293 if (entry
->s
.output_section_statement
.name
!= NULL
)
1295 /* We have a section of this name, but it might not have the correct
1297 struct out_section_hash_entry
*last_ent
;
1298 unsigned long hash
= entry
->root
.hash
;
1300 if (create
&& constraint
== SPECIAL
)
1301 /* Not traversing to the end reverses the order of the second
1302 and subsequent SPECIAL sections in the hash table chain,
1303 but that shouldn't matter. */
1308 if (entry
->s
.output_section_statement
.constraint
>= 0
1311 == entry
->s
.output_section_statement
.constraint
)))
1312 return &entry
->s
.output_section_statement
;
1314 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1316 while (entry
!= NULL
1317 && entry
->root
.hash
== hash
1318 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1324 = ((struct out_section_hash_entry
*)
1325 output_section_statement_newfunc (NULL
,
1326 &output_section_statement_table
,
1330 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1333 entry
->root
= last_ent
->root
;
1334 last_ent
->root
.next
= &entry
->root
;
1337 entry
->s
.output_section_statement
.name
= name
;
1338 entry
->s
.output_section_statement
.constraint
= constraint
;
1339 return &entry
->s
.output_section_statement
;
1342 /* A variant of lang_output_section_find used by place_orphan.
1343 Returns the output statement that should precede a new output
1344 statement for SEC. If an exact match is found on certain flags,
1347 lang_output_section_statement_type
*
1348 lang_output_section_find_by_flags (const asection
*sec
,
1349 lang_output_section_statement_type
**exact
,
1350 lang_match_sec_type_func match_type
)
1352 lang_output_section_statement_type
*first
, *look
, *found
;
1355 /* We know the first statement on this list is *ABS*. May as well
1357 first
= &lang_output_section_statement
.head
->output_section_statement
;
1358 first
= first
->next
;
1360 /* First try for an exact match. */
1362 for (look
= first
; look
; look
= look
->next
)
1364 flags
= look
->flags
;
1365 if (look
->bfd_section
!= NULL
)
1367 flags
= look
->bfd_section
->flags
;
1368 if (match_type
&& !match_type (link_info
.output_bfd
,
1373 flags
^= sec
->flags
;
1374 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1375 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1385 if (sec
->flags
& SEC_CODE
)
1387 /* Try for a rw code section. */
1388 for (look
= first
; look
; look
= look
->next
)
1390 flags
= look
->flags
;
1391 if (look
->bfd_section
!= NULL
)
1393 flags
= look
->bfd_section
->flags
;
1394 if (match_type
&& !match_type (link_info
.output_bfd
,
1399 flags
^= sec
->flags
;
1400 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1401 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1405 else if (sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
))
1407 /* .rodata can go after .text, .sdata2 after .rodata. */
1408 for (look
= first
; look
; look
= look
->next
)
1410 flags
= look
->flags
;
1411 if (look
->bfd_section
!= NULL
)
1413 flags
= look
->bfd_section
->flags
;
1414 if (match_type
&& !match_type (link_info
.output_bfd
,
1419 flags
^= sec
->flags
;
1420 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1422 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1426 else if (sec
->flags
& SEC_SMALL_DATA
)
1428 /* .sdata goes after .data, .sbss after .sdata. */
1429 for (look
= first
; look
; look
= look
->next
)
1431 flags
= look
->flags
;
1432 if (look
->bfd_section
!= NULL
)
1434 flags
= look
->bfd_section
->flags
;
1435 if (match_type
&& !match_type (link_info
.output_bfd
,
1440 flags
^= sec
->flags
;
1441 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1442 | SEC_THREAD_LOCAL
))
1443 || ((look
->flags
& SEC_SMALL_DATA
)
1444 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1448 else if (sec
->flags
& SEC_HAS_CONTENTS
)
1450 /* .data goes after .rodata. */
1451 for (look
= first
; look
; look
= look
->next
)
1453 flags
= look
->flags
;
1454 if (look
->bfd_section
!= NULL
)
1456 flags
= look
->bfd_section
->flags
;
1457 if (match_type
&& !match_type (link_info
.output_bfd
,
1462 flags
^= sec
->flags
;
1463 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1464 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1470 /* .bss goes last. */
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_ALLOC
))
1488 if (found
|| !match_type
)
1491 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1494 /* Find the last output section before given output statement.
1495 Used by place_orphan. */
1498 output_prev_sec_find (lang_output_section_statement_type
*os
)
1500 lang_output_section_statement_type
*lookup
;
1502 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1504 if (lookup
->constraint
< 0)
1507 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1508 return lookup
->bfd_section
;
1514 /* Look for a suitable place for a new output section statement. The
1515 idea is to skip over anything that might be inside a SECTIONS {}
1516 statement in a script, before we find another output section
1517 statement. Assignments to "dot" before an output section statement
1518 are assumed to belong to it. An exception to this rule is made for
1519 the first assignment to dot, otherwise we might put an orphan
1520 before . = . + SIZEOF_HEADERS or similar assignments that set the
1523 static lang_statement_union_type
**
1524 insert_os_after (lang_output_section_statement_type
*after
)
1526 lang_statement_union_type
**where
;
1527 lang_statement_union_type
**assign
= NULL
;
1528 bfd_boolean ignore_first
;
1531 = after
== &lang_output_section_statement
.head
->output_section_statement
;
1533 for (where
= &after
->header
.next
;
1535 where
= &(*where
)->header
.next
)
1537 switch ((*where
)->header
.type
)
1539 case lang_assignment_statement_enum
:
1542 lang_assignment_statement_type
*ass
;
1544 ass
= &(*where
)->assignment_statement
;
1545 if (ass
->exp
->type
.node_class
!= etree_assert
1546 && ass
->exp
->assign
.dst
[0] == '.'
1547 && ass
->exp
->assign
.dst
[1] == 0
1551 ignore_first
= FALSE
;
1553 case lang_wild_statement_enum
:
1554 case lang_input_section_enum
:
1555 case lang_object_symbols_statement_enum
:
1556 case lang_fill_statement_enum
:
1557 case lang_data_statement_enum
:
1558 case lang_reloc_statement_enum
:
1559 case lang_padding_statement_enum
:
1560 case lang_constructors_statement_enum
:
1563 case lang_output_section_statement_enum
:
1567 case lang_input_statement_enum
:
1568 case lang_address_statement_enum
:
1569 case lang_target_statement_enum
:
1570 case lang_output_statement_enum
:
1571 case lang_group_statement_enum
:
1572 case lang_insert_statement_enum
:
1581 lang_output_section_statement_type
*
1582 lang_insert_orphan (asection
*s
,
1583 const char *secname
,
1585 lang_output_section_statement_type
*after
,
1586 struct orphan_save
*place
,
1587 etree_type
*address
,
1588 lang_statement_list_type
*add_child
)
1590 lang_statement_list_type add
;
1592 lang_output_section_statement_type
*os
;
1593 lang_output_section_statement_type
**os_tail
;
1595 /* If we have found an appropriate place for the output section
1596 statements for this orphan, add them to our own private list,
1597 inserting them later into the global statement list. */
1600 lang_list_init (&add
);
1601 push_stat_ptr (&add
);
1605 if (config
.build_constructors
)
1607 /* If the name of the section is representable in C, then create
1608 symbols to mark the start and the end of the section. */
1609 for (ps
= secname
; *ps
!= '\0'; ps
++)
1610 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1615 etree_type
*e_align
;
1617 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1618 symname
[0] = bfd_get_symbol_leading_char (link_info
.output_bfd
);
1619 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1620 e_align
= exp_unop (ALIGN_K
,
1621 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1622 lang_add_assignment (exp_assop ('=', ".", e_align
));
1623 lang_add_assignment (exp_provide (symname
,
1624 exp_nameop (NAME
, "."),
1629 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1630 address
= exp_intop (0);
1632 os_tail
= ((lang_output_section_statement_type
**)
1633 lang_output_section_statement
.tail
);
1634 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1637 if (add_child
== NULL
)
1638 add_child
= &os
->children
;
1639 lang_add_section (add_child
, s
, os
);
1641 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1643 if (config
.build_constructors
&& *ps
== '\0')
1647 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1648 symname
[0] = bfd_get_symbol_leading_char (link_info
.output_bfd
);
1649 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1650 lang_add_assignment (exp_provide (symname
,
1651 exp_nameop (NAME
, "."),
1655 /* Restore the global list pointer. */
1659 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1661 asection
*snew
, *as
;
1663 snew
= os
->bfd_section
;
1665 /* Shuffle the bfd section list to make the output file look
1666 neater. This is really only cosmetic. */
1667 if (place
->section
== NULL
1668 && after
!= (&lang_output_section_statement
.head
1669 ->output_section_statement
))
1671 asection
*bfd_section
= after
->bfd_section
;
1673 /* If the output statement hasn't been used to place any input
1674 sections (and thus doesn't have an output bfd_section),
1675 look for the closest prior output statement having an
1677 if (bfd_section
== NULL
)
1678 bfd_section
= output_prev_sec_find (after
);
1680 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1681 place
->section
= &bfd_section
->next
;
1684 if (place
->section
== NULL
)
1685 place
->section
= &link_info
.output_bfd
->sections
;
1687 as
= *place
->section
;
1691 /* Put the section at the end of the list. */
1693 /* Unlink the section. */
1694 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1696 /* Now tack it back on in the right place. */
1697 bfd_section_list_append (link_info
.output_bfd
, snew
);
1699 else if (as
!= snew
&& as
->prev
!= snew
)
1701 /* Unlink the section. */
1702 bfd_section_list_remove (link_info
.output_bfd
, snew
);
1704 /* Now tack it back on in the right place. */
1705 bfd_section_list_insert_before (link_info
.output_bfd
, as
, snew
);
1708 /* Save the end of this list. Further ophans of this type will
1709 follow the one we've just added. */
1710 place
->section
= &snew
->next
;
1712 /* The following is non-cosmetic. We try to put the output
1713 statements in some sort of reasonable order here, because they
1714 determine the final load addresses of the orphan sections.
1715 In addition, placing output statements in the wrong order may
1716 require extra segments. For instance, given a typical
1717 situation of all read-only sections placed in one segment and
1718 following that a segment containing all the read-write
1719 sections, we wouldn't want to place an orphan read/write
1720 section before or amongst the read-only ones. */
1721 if (add
.head
!= NULL
)
1723 lang_output_section_statement_type
*newly_added_os
;
1725 if (place
->stmt
== NULL
)
1727 lang_statement_union_type
**where
= insert_os_after (after
);
1732 place
->os_tail
= &after
->next
;
1736 /* Put it after the last orphan statement we added. */
1737 *add
.tail
= *place
->stmt
;
1738 *place
->stmt
= add
.head
;
1741 /* Fix the global list pointer if we happened to tack our
1742 new list at the tail. */
1743 if (*stat_ptr
->tail
== add
.head
)
1744 stat_ptr
->tail
= add
.tail
;
1746 /* Save the end of this list. */
1747 place
->stmt
= add
.tail
;
1749 /* Do the same for the list of output section statements. */
1750 newly_added_os
= *os_tail
;
1752 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1753 ((char *) place
->os_tail
1754 - offsetof (lang_output_section_statement_type
, next
));
1755 newly_added_os
->next
= *place
->os_tail
;
1756 if (newly_added_os
->next
!= NULL
)
1757 newly_added_os
->next
->prev
= newly_added_os
;
1758 *place
->os_tail
= newly_added_os
;
1759 place
->os_tail
= &newly_added_os
->next
;
1761 /* Fixing the global list pointer here is a little different.
1762 We added to the list in lang_enter_output_section_statement,
1763 trimmed off the new output_section_statment above when
1764 assigning *os_tail = NULL, but possibly added it back in
1765 the same place when assigning *place->os_tail. */
1766 if (*os_tail
== NULL
)
1767 lang_output_section_statement
.tail
1768 = (lang_statement_union_type
**) os_tail
;
1775 lang_map_flags (flagword flag
)
1777 if (flag
& SEC_ALLOC
)
1780 if (flag
& SEC_CODE
)
1783 if (flag
& SEC_READONLY
)
1786 if (flag
& SEC_DATA
)
1789 if (flag
& SEC_LOAD
)
1796 lang_memory_region_type
*m
;
1797 bfd_boolean dis_header_printed
= FALSE
;
1800 LANG_FOR_EACH_INPUT_STATEMENT (file
)
1804 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
1805 || file
->just_syms_flag
)
1808 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1809 if ((s
->output_section
== NULL
1810 || s
->output_section
->owner
!= link_info
.output_bfd
)
1811 && (s
->flags
& (SEC_LINKER_CREATED
| SEC_KEEP
)) == 0)
1813 if (! dis_header_printed
)
1815 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
1816 dis_header_printed
= TRUE
;
1819 print_input_section (s
);
1823 minfo (_("\nMemory Configuration\n\n"));
1824 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1825 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1827 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1832 fprintf (config
.map_file
, "%-16s ", m
->name
);
1834 sprintf_vma (buf
, m
->origin
);
1835 minfo ("0x%s ", buf
);
1843 minfo ("0x%V", m
->length
);
1844 if (m
->flags
|| m
->not_flags
)
1852 lang_map_flags (m
->flags
);
1858 lang_map_flags (m
->not_flags
);
1865 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1867 if (! link_info
.reduce_memory_overheads
)
1869 obstack_begin (&map_obstack
, 1000);
1870 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1871 bfd_map_over_sections (p
, init_map_userdata
, 0);
1872 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1874 lang_statement_iteration
++;
1875 print_statements ();
1879 init_map_userdata (bfd
*abfd ATTRIBUTE_UNUSED
,
1881 void *data ATTRIBUTE_UNUSED
)
1883 fat_section_userdata_type
*new_data
1884 = ((fat_section_userdata_type
*) (stat_alloc
1885 (sizeof (fat_section_userdata_type
))));
1887 ASSERT (get_userdata (sec
) == NULL
);
1888 get_userdata (sec
) = new_data
;
1889 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
1893 sort_def_symbol (struct bfd_link_hash_entry
*hash_entry
,
1894 void *info ATTRIBUTE_UNUSED
)
1896 if (hash_entry
->type
== bfd_link_hash_defined
1897 || hash_entry
->type
== bfd_link_hash_defweak
)
1899 struct fat_user_section_struct
*ud
;
1900 struct map_symbol_def
*def
;
1902 ud
= get_userdata (hash_entry
->u
.def
.section
);
1905 /* ??? What do we have to do to initialize this beforehand? */
1906 /* The first time we get here is bfd_abs_section... */
1907 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
1908 ud
= get_userdata (hash_entry
->u
.def
.section
);
1910 else if (!ud
->map_symbol_def_tail
)
1911 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
1913 def
= obstack_alloc (&map_obstack
, sizeof *def
);
1914 def
->entry
= hash_entry
;
1915 *(ud
->map_symbol_def_tail
) = def
;
1916 ud
->map_symbol_def_tail
= &def
->next
;
1921 /* Initialize an output section. */
1924 init_os (lang_output_section_statement_type
*s
, asection
*isec
,
1927 if (s
->bfd_section
!= NULL
)
1930 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1931 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1933 if (s
->constraint
!= SPECIAL
)
1934 s
->bfd_section
= bfd_get_section_by_name (link_info
.output_bfd
, s
->name
);
1935 if (s
->bfd_section
== NULL
)
1936 s
->bfd_section
= bfd_make_section_anyway_with_flags (link_info
.output_bfd
,
1938 if (s
->bfd_section
== NULL
)
1940 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1941 link_info
.output_bfd
->xvec
->name
, s
->name
);
1943 s
->bfd_section
->output_section
= s
->bfd_section
;
1944 s
->bfd_section
->output_offset
= 0;
1946 if (!link_info
.reduce_memory_overheads
)
1948 fat_section_userdata_type
*new
1949 = stat_alloc (sizeof (fat_section_userdata_type
));
1950 memset (new, 0, sizeof (fat_section_userdata_type
));
1951 get_userdata (s
->bfd_section
) = new;
1954 /* If there is a base address, make sure that any sections it might
1955 mention are initialized. */
1956 if (s
->addr_tree
!= NULL
)
1957 exp_init_os (s
->addr_tree
);
1959 if (s
->load_base
!= NULL
)
1960 exp_init_os (s
->load_base
);
1962 /* If supplied an alignment, set it. */
1963 if (s
->section_alignment
!= -1)
1964 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1967 bfd_init_private_section_data (isec
->owner
, isec
,
1968 link_info
.output_bfd
, s
->bfd_section
,
1972 /* Make sure that all output sections mentioned in an expression are
1976 exp_init_os (etree_type
*exp
)
1978 switch (exp
->type
.node_class
)
1982 exp_init_os (exp
->assign
.src
);
1986 exp_init_os (exp
->binary
.lhs
);
1987 exp_init_os (exp
->binary
.rhs
);
1991 exp_init_os (exp
->trinary
.cond
);
1992 exp_init_os (exp
->trinary
.lhs
);
1993 exp_init_os (exp
->trinary
.rhs
);
1997 exp_init_os (exp
->assert_s
.child
);
2001 exp_init_os (exp
->unary
.child
);
2005 switch (exp
->type
.node_code
)
2011 lang_output_section_statement_type
*os
;
2013 os
= lang_output_section_find (exp
->name
.name
);
2014 if (os
!= NULL
&& os
->bfd_section
== NULL
)
2015 init_os (os
, NULL
, 0);
2026 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
2028 lang_input_statement_type
*entry
= data
;
2030 /* If we are only reading symbols from this object, then we want to
2031 discard all sections. */
2032 if (entry
->just_syms_flag
)
2034 bfd_link_just_syms (abfd
, sec
, &link_info
);
2038 if (!(abfd
->flags
& DYNAMIC
))
2039 bfd_section_already_linked (abfd
, sec
, &link_info
);
2042 /* The wild routines.
2044 These expand statements like *(.text) and foo.o to a list of
2045 explicit actions, like foo.o(.text), bar.o(.text) and
2046 foo.o(.text, .data). */
2048 /* Add SECTION to the output section OUTPUT. Do this by creating a
2049 lang_input_section statement which is placed at PTR. FILE is the
2050 input file which holds SECTION. */
2053 lang_add_section (lang_statement_list_type
*ptr
,
2055 lang_output_section_statement_type
*output
)
2057 flagword flags
= section
->flags
;
2058 bfd_boolean discard
;
2060 /* Discard sections marked with SEC_EXCLUDE. */
2061 discard
= (flags
& SEC_EXCLUDE
) != 0;
2063 /* Discard input sections which are assigned to a section named
2064 DISCARD_SECTION_NAME. */
2065 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2068 /* Discard debugging sections if we are stripping debugging
2070 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2071 && (flags
& SEC_DEBUGGING
) != 0)
2076 if (section
->output_section
== NULL
)
2078 /* This prevents future calls from assigning this section. */
2079 section
->output_section
= bfd_abs_section_ptr
;
2084 if (section
->output_section
== NULL
)
2087 lang_input_section_type
*new;
2090 flags
= section
->flags
;
2092 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2093 to an output section, because we want to be able to include a
2094 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2095 section (I don't know why we want to do this, but we do).
2096 build_link_order in ldwrite.c handles this case by turning
2097 the embedded SEC_NEVER_LOAD section into a fill. */
2099 flags
&= ~ SEC_NEVER_LOAD
;
2101 switch (output
->sectype
)
2103 case normal_section
:
2104 case overlay_section
:
2106 case noalloc_section
:
2107 flags
&= ~SEC_ALLOC
;
2109 case noload_section
:
2111 flags
|= SEC_NEVER_LOAD
;
2115 if (output
->bfd_section
== NULL
)
2116 init_os (output
, section
, flags
);
2118 first
= ! output
->bfd_section
->linker_has_input
;
2119 output
->bfd_section
->linker_has_input
= 1;
2121 if (!link_info
.relocatable
2122 && !stripped_excluded_sections
)
2124 asection
*s
= output
->bfd_section
->map_tail
.s
;
2125 output
->bfd_section
->map_tail
.s
= section
;
2126 section
->map_head
.s
= NULL
;
2127 section
->map_tail
.s
= s
;
2129 s
->map_head
.s
= section
;
2131 output
->bfd_section
->map_head
.s
= section
;
2134 /* Add a section reference to the list. */
2135 new = new_stat (lang_input_section
, ptr
);
2137 new->section
= section
;
2138 section
->output_section
= output
->bfd_section
;
2140 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2141 already been processed. One reason to do this is that on pe
2142 format targets, .text$foo sections go into .text and it's odd
2143 to see .text with SEC_LINK_ONCE set. */
2145 if (! link_info
.relocatable
)
2146 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
2148 /* If this is not the first input section, and the SEC_READONLY
2149 flag is not currently set, then don't set it just because the
2150 input section has it set. */
2152 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
2153 flags
&= ~ SEC_READONLY
;
2155 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2157 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2158 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2159 || ((flags
& SEC_MERGE
)
2160 && output
->bfd_section
->entsize
!= section
->entsize
)))
2162 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2163 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2166 output
->bfd_section
->flags
|= flags
;
2168 if (flags
& SEC_MERGE
)
2169 output
->bfd_section
->entsize
= section
->entsize
;
2171 /* If SEC_READONLY is not set in the input section, then clear
2172 it from the output section. */
2173 if ((section
->flags
& SEC_READONLY
) == 0)
2174 output
->bfd_section
->flags
&= ~SEC_READONLY
;
2176 /* Copy over SEC_SMALL_DATA. */
2177 if (section
->flags
& SEC_SMALL_DATA
)
2178 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
2180 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2181 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2183 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
2184 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
2186 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2187 /* FIXME: This value should really be obtained from the bfd... */
2188 output
->block_value
= 128;
2193 /* Handle wildcard sorting. This returns the lang_input_section which
2194 should follow the one we are going to create for SECTION and FILE,
2195 based on the sorting requirements of WILD. It returns NULL if the
2196 new section should just go at the end of the current list. */
2198 static lang_statement_union_type
*
2199 wild_sort (lang_wild_statement_type
*wild
,
2200 struct wildcard_list
*sec
,
2201 lang_input_statement_type
*file
,
2204 const char *section_name
;
2205 lang_statement_union_type
*l
;
2207 if (!wild
->filenames_sorted
2208 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2211 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2212 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2214 lang_input_section_type
*ls
;
2216 if (l
->header
.type
!= lang_input_section_enum
)
2218 ls
= &l
->input_section
;
2220 /* Sorting by filename takes precedence over sorting by section
2223 if (wild
->filenames_sorted
)
2225 const char *fn
, *ln
;
2229 /* The PE support for the .idata section as generated by
2230 dlltool assumes that files will be sorted by the name of
2231 the archive and then the name of the file within the
2234 if (file
->the_bfd
!= NULL
2235 && bfd_my_archive (file
->the_bfd
) != NULL
)
2237 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2242 fn
= file
->filename
;
2246 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2248 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2253 ln
= ls
->section
->owner
->filename
;
2257 i
= strcmp (fn
, ln
);
2266 fn
= file
->filename
;
2268 ln
= ls
->section
->owner
->filename
;
2270 i
= strcmp (fn
, ln
);
2278 /* Here either the files are not sorted by name, or we are
2279 looking at the sections for this file. */
2281 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2282 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2289 /* Expand a wild statement for a particular FILE. SECTION may be
2290 NULL, in which case it is a wild card. */
2293 output_section_callback (lang_wild_statement_type
*ptr
,
2294 struct wildcard_list
*sec
,
2296 lang_input_statement_type
*file
,
2299 lang_statement_union_type
*before
;
2301 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2302 if (unique_section_p (section
))
2305 before
= wild_sort (ptr
, sec
, file
, section
);
2307 /* Here BEFORE points to the lang_input_section which
2308 should follow the one we are about to add. If BEFORE
2309 is NULL, then the section should just go at the end
2310 of the current list. */
2313 lang_add_section (&ptr
->children
, section
,
2314 (lang_output_section_statement_type
*) output
);
2317 lang_statement_list_type list
;
2318 lang_statement_union_type
**pp
;
2320 lang_list_init (&list
);
2321 lang_add_section (&list
, section
,
2322 (lang_output_section_statement_type
*) output
);
2324 /* If we are discarding the section, LIST.HEAD will
2326 if (list
.head
!= NULL
)
2328 ASSERT (list
.head
->header
.next
== NULL
);
2330 for (pp
= &ptr
->children
.head
;
2332 pp
= &(*pp
)->header
.next
)
2333 ASSERT (*pp
!= NULL
);
2335 list
.head
->header
.next
= *pp
;
2341 /* Check if all sections in a wild statement for a particular FILE
2345 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2346 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2348 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2351 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2352 if (unique_section_p (section
))
2355 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2356 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2359 /* This is passed a file name which must have been seen already and
2360 added to the statement tree. We will see if it has been opened
2361 already and had its symbols read. If not then we'll read it. */
2363 static lang_input_statement_type
*
2364 lookup_name (const char *name
)
2366 lang_input_statement_type
*search
;
2368 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2370 search
= (lang_input_statement_type
*) search
->next_real_file
)
2372 /* Use the local_sym_name as the name of the file that has
2373 already been loaded as filename might have been transformed
2374 via the search directory lookup mechanism. */
2375 const char *filename
= search
->local_sym_name
;
2377 if (filename
!= NULL
2378 && strcmp (filename
, name
) == 0)
2383 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2384 default_target
, FALSE
);
2386 /* If we have already added this file, or this file is not real
2387 don't add this file. */
2388 if (search
->loaded
|| !search
->real
)
2391 if (! load_symbols (search
, NULL
))
2397 /* Save LIST as a list of libraries whose symbols should not be exported. */
2402 struct excluded_lib
*next
;
2404 static struct excluded_lib
*excluded_libs
;
2407 add_excluded_libs (const char *list
)
2409 const char *p
= list
, *end
;
2413 struct excluded_lib
*entry
;
2414 end
= strpbrk (p
, ",:");
2416 end
= p
+ strlen (p
);
2417 entry
= xmalloc (sizeof (*entry
));
2418 entry
->next
= excluded_libs
;
2419 entry
->name
= xmalloc (end
- p
+ 1);
2420 memcpy (entry
->name
, p
, end
- p
);
2421 entry
->name
[end
- p
] = '\0';
2422 excluded_libs
= entry
;
2430 check_excluded_libs (bfd
*abfd
)
2432 struct excluded_lib
*lib
= excluded_libs
;
2436 int len
= strlen (lib
->name
);
2437 const char *filename
= lbasename (abfd
->filename
);
2439 if (strcmp (lib
->name
, "ALL") == 0)
2441 abfd
->no_export
= TRUE
;
2445 if (strncmp (lib
->name
, filename
, len
) == 0
2446 && (filename
[len
] == '\0'
2447 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2448 && filename
[len
+ 2] == '\0')))
2450 abfd
->no_export
= TRUE
;
2458 /* Get the symbols for an input file. */
2461 load_symbols (lang_input_statement_type
*entry
,
2462 lang_statement_list_type
*place
)
2469 ldfile_open_file (entry
);
2471 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2472 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2475 bfd_boolean save_ldlang_sysrooted_script
;
2476 bfd_boolean save_as_needed
, save_add_needed
;
2478 err
= bfd_get_error ();
2480 /* See if the emulation has some special knowledge. */
2481 if (ldemul_unrecognized_file (entry
))
2484 if (err
== bfd_error_file_ambiguously_recognized
)
2488 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2489 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2490 for (p
= matching
; *p
!= NULL
; p
++)
2494 else if (err
!= bfd_error_file_not_recognized
2496 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2498 bfd_close (entry
->the_bfd
);
2499 entry
->the_bfd
= NULL
;
2501 /* Try to interpret the file as a linker script. */
2502 ldfile_open_command_file (entry
->filename
);
2504 push_stat_ptr (place
);
2505 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2506 ldlang_sysrooted_script
= entry
->sysrooted
;
2507 save_as_needed
= as_needed
;
2508 as_needed
= entry
->as_needed
;
2509 save_add_needed
= add_needed
;
2510 add_needed
= entry
->add_needed
;
2512 ldfile_assumed_script
= TRUE
;
2513 parser_input
= input_script
;
2514 /* We want to use the same -Bdynamic/-Bstatic as the one for
2516 config
.dynamic_link
= entry
->dynamic
;
2518 ldfile_assumed_script
= FALSE
;
2520 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2521 as_needed
= save_as_needed
;
2522 add_needed
= save_add_needed
;
2528 if (ldemul_recognized_file (entry
))
2531 /* We don't call ldlang_add_file for an archive. Instead, the
2532 add_symbols entry point will call ldlang_add_file, via the
2533 add_archive_element callback, for each element of the archive
2535 switch (bfd_get_format (entry
->the_bfd
))
2541 ldlang_add_file (entry
);
2542 if (trace_files
|| trace_file_tries
)
2543 info_msg ("%I\n", entry
);
2547 check_excluded_libs (entry
->the_bfd
);
2549 if (entry
->whole_archive
)
2552 bfd_boolean loaded
= TRUE
;
2556 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2561 if (! bfd_check_format (member
, bfd_object
))
2563 einfo (_("%F%B: member %B in archive is not an object\n"),
2564 entry
->the_bfd
, member
);
2568 if (! ((*link_info
.callbacks
->add_archive_element
)
2569 (&link_info
, member
, "--whole-archive")))
2572 if (! bfd_link_add_symbols (member
, &link_info
))
2574 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2579 entry
->loaded
= loaded
;
2585 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2586 entry
->loaded
= TRUE
;
2588 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2590 return entry
->loaded
;
2593 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2594 may be NULL, indicating that it is a wildcard. Separate
2595 lang_input_section statements are created for each part of the
2596 expansion; they are added after the wild statement S. OUTPUT is
2597 the output section. */
2600 wild (lang_wild_statement_type
*s
,
2601 const char *target ATTRIBUTE_UNUSED
,
2602 lang_output_section_statement_type
*output
)
2604 struct wildcard_list
*sec
;
2606 if (s
->handler_data
[0]
2607 && s
->handler_data
[0]->spec
.sorted
== by_name
2608 && !s
->filenames_sorted
)
2610 lang_section_bst_type
*tree
;
2612 walk_wild (s
, output_section_callback_fast
, output
);
2617 output_section_callback_tree_to_list (s
, tree
, output
);
2622 walk_wild (s
, output_section_callback
, output
);
2624 if (default_common_section
== NULL
)
2625 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2626 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2628 /* Remember the section that common is going to in case we
2629 later get something which doesn't know where to put it. */
2630 default_common_section
= output
;
2635 /* Return TRUE iff target is the sought target. */
2638 get_target (const bfd_target
*target
, void *data
)
2640 const char *sought
= data
;
2642 return strcmp (target
->name
, sought
) == 0;
2645 /* Like strcpy() but convert to lower case as well. */
2648 stricpy (char *dest
, char *src
)
2652 while ((c
= *src
++) != 0)
2653 *dest
++ = TOLOWER (c
);
2658 /* Remove the first occurrence of needle (if any) in haystack
2662 strcut (char *haystack
, char *needle
)
2664 haystack
= strstr (haystack
, needle
);
2670 for (src
= haystack
+ strlen (needle
); *src
;)
2671 *haystack
++ = *src
++;
2677 /* Compare two target format name strings.
2678 Return a value indicating how "similar" they are. */
2681 name_compare (char *first
, char *second
)
2687 copy1
= xmalloc (strlen (first
) + 1);
2688 copy2
= xmalloc (strlen (second
) + 1);
2690 /* Convert the names to lower case. */
2691 stricpy (copy1
, first
);
2692 stricpy (copy2
, second
);
2694 /* Remove size and endian strings from the name. */
2695 strcut (copy1
, "big");
2696 strcut (copy1
, "little");
2697 strcut (copy2
, "big");
2698 strcut (copy2
, "little");
2700 /* Return a value based on how many characters match,
2701 starting from the beginning. If both strings are
2702 the same then return 10 * their length. */
2703 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2704 if (copy1
[result
] == 0)
2716 /* Set by closest_target_match() below. */
2717 static const bfd_target
*winner
;
2719 /* Scan all the valid bfd targets looking for one that has the endianness
2720 requirement that was specified on the command line, and is the nearest
2721 match to the original output target. */
2724 closest_target_match (const bfd_target
*target
, void *data
)
2726 const bfd_target
*original
= data
;
2728 if (command_line
.endian
== ENDIAN_BIG
2729 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2732 if (command_line
.endian
== ENDIAN_LITTLE
2733 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2736 /* Must be the same flavour. */
2737 if (target
->flavour
!= original
->flavour
)
2740 /* Ignore generic big and little endian elf vectors. */
2741 if (strcmp (target
->name
, "elf32-big") == 0
2742 || strcmp (target
->name
, "elf64-big") == 0
2743 || strcmp (target
->name
, "elf32-little") == 0
2744 || strcmp (target
->name
, "elf64-little") == 0)
2747 /* If we have not found a potential winner yet, then record this one. */
2754 /* Oh dear, we now have two potential candidates for a successful match.
2755 Compare their names and choose the better one. */
2756 if (name_compare (target
->name
, original
->name
)
2757 > name_compare (winner
->name
, original
->name
))
2760 /* Keep on searching until wqe have checked them all. */
2764 /* Return the BFD target format of the first input file. */
2767 get_first_input_target (void)
2769 char *target
= NULL
;
2771 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2773 if (s
->header
.type
== lang_input_statement_enum
2776 ldfile_open_file (s
);
2778 if (s
->the_bfd
!= NULL
2779 && bfd_check_format (s
->the_bfd
, bfd_object
))
2781 target
= bfd_get_target (s
->the_bfd
);
2793 lang_get_output_target (void)
2797 /* Has the user told us which output format to use? */
2798 if (output_target
!= NULL
)
2799 return output_target
;
2801 /* No - has the current target been set to something other than
2803 if (current_target
!= default_target
)
2804 return current_target
;
2806 /* No - can we determine the format of the first input file? */
2807 target
= get_first_input_target ();
2811 /* Failed - use the default output target. */
2812 return default_target
;
2815 /* Open the output file. */
2818 open_output (const char *name
)
2820 output_target
= lang_get_output_target ();
2822 /* Has the user requested a particular endianness on the command
2824 if (command_line
.endian
!= ENDIAN_UNSET
)
2826 const bfd_target
*target
;
2827 enum bfd_endian desired_endian
;
2829 /* Get the chosen target. */
2830 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2832 /* If the target is not supported, we cannot do anything. */
2835 if (command_line
.endian
== ENDIAN_BIG
)
2836 desired_endian
= BFD_ENDIAN_BIG
;
2838 desired_endian
= BFD_ENDIAN_LITTLE
;
2840 /* See if the target has the wrong endianness. This should
2841 not happen if the linker script has provided big and
2842 little endian alternatives, but some scrips don't do
2844 if (target
->byteorder
!= desired_endian
)
2846 /* If it does, then see if the target provides
2847 an alternative with the correct endianness. */
2848 if (target
->alternative_target
!= NULL
2849 && (target
->alternative_target
->byteorder
== desired_endian
))
2850 output_target
= target
->alternative_target
->name
;
2853 /* Try to find a target as similar as possible to
2854 the default target, but which has the desired
2855 endian characteristic. */
2856 bfd_search_for_target (closest_target_match
,
2859 /* Oh dear - we could not find any targets that
2860 satisfy our requirements. */
2862 einfo (_("%P: warning: could not find any targets"
2863 " that match endianness requirement\n"));
2865 output_target
= winner
->name
;
2871 link_info
.output_bfd
= bfd_openw (name
, output_target
);
2873 if (link_info
.output_bfd
== NULL
)
2875 if (bfd_get_error () == bfd_error_invalid_target
)
2876 einfo (_("%P%F: target %s not found\n"), output_target
);
2878 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2881 delete_output_file_on_failure
= TRUE
;
2883 if (! bfd_set_format (link_info
.output_bfd
, bfd_object
))
2884 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2885 if (! bfd_set_arch_mach (link_info
.output_bfd
,
2886 ldfile_output_architecture
,
2887 ldfile_output_machine
))
2888 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2890 link_info
.hash
= bfd_link_hash_table_create (link_info
.output_bfd
);
2891 if (link_info
.hash
== NULL
)
2892 einfo (_("%P%F: can not create hash table: %E\n"));
2894 bfd_set_gp_size (link_info
.output_bfd
, g_switch_value
);
2898 ldlang_open_output (lang_statement_union_type
*statement
)
2900 switch (statement
->header
.type
)
2902 case lang_output_statement_enum
:
2903 ASSERT (link_info
.output_bfd
== NULL
);
2904 open_output (statement
->output_statement
.name
);
2905 ldemul_set_output_arch ();
2906 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2907 link_info
.output_bfd
->flags
|= D_PAGED
;
2909 link_info
.output_bfd
->flags
&= ~D_PAGED
;
2910 if (config
.text_read_only
)
2911 link_info
.output_bfd
->flags
|= WP_TEXT
;
2913 link_info
.output_bfd
->flags
&= ~WP_TEXT
;
2914 if (link_info
.traditional_format
)
2915 link_info
.output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2917 link_info
.output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2920 case lang_target_statement_enum
:
2921 current_target
= statement
->target_statement
.target
;
2928 /* Convert between addresses in bytes and sizes in octets.
2929 For currently supported targets, octets_per_byte is always a power
2930 of two, so we can use shifts. */
2931 #define TO_ADDR(X) ((X) >> opb_shift)
2932 #define TO_SIZE(X) ((X) << opb_shift)
2934 /* Support the above. */
2935 static unsigned int opb_shift
= 0;
2940 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2941 ldfile_output_machine
);
2944 while ((x
& 1) == 0)
2952 /* Open all the input files. */
2955 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2957 for (; s
!= NULL
; s
= s
->header
.next
)
2959 switch (s
->header
.type
)
2961 case lang_constructors_statement_enum
:
2962 open_input_bfds (constructor_list
.head
, force
);
2964 case lang_output_section_statement_enum
:
2965 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2967 case lang_wild_statement_enum
:
2968 /* Maybe we should load the file's symbols. */
2969 if (s
->wild_statement
.filename
2970 && !wildcardp (s
->wild_statement
.filename
)
2971 && !archive_path (s
->wild_statement
.filename
))
2972 lookup_name (s
->wild_statement
.filename
);
2973 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2975 case lang_group_statement_enum
:
2977 struct bfd_link_hash_entry
*undefs
;
2979 /* We must continually search the entries in the group
2980 until no new symbols are added to the list of undefined
2985 undefs
= link_info
.hash
->undefs_tail
;
2986 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2988 while (undefs
!= link_info
.hash
->undefs_tail
);
2991 case lang_target_statement_enum
:
2992 current_target
= s
->target_statement
.target
;
2994 case lang_input_statement_enum
:
2995 if (s
->input_statement
.real
)
2997 lang_statement_union_type
**os_tail
;
2998 lang_statement_list_type add
;
3000 s
->input_statement
.target
= current_target
;
3002 /* If we are being called from within a group, and this
3003 is an archive which has already been searched, then
3004 force it to be researched unless the whole archive
3005 has been loaded already. */
3007 && !s
->input_statement
.whole_archive
3008 && s
->input_statement
.loaded
3009 && bfd_check_format (s
->input_statement
.the_bfd
,
3011 s
->input_statement
.loaded
= FALSE
;
3013 os_tail
= lang_output_section_statement
.tail
;
3014 lang_list_init (&add
);
3016 if (! load_symbols (&s
->input_statement
, &add
))
3017 config
.make_executable
= FALSE
;
3019 if (add
.head
!= NULL
)
3021 /* If this was a script with output sections then
3022 tack any added statements on to the end of the
3023 list. This avoids having to reorder the output
3024 section statement list. Very likely the user
3025 forgot -T, and whatever we do here will not meet
3026 naive user expectations. */
3027 if (os_tail
!= lang_output_section_statement
.tail
)
3029 einfo (_("%P: warning: %s contains output sections;"
3030 " did you forget -T?\n"),
3031 s
->input_statement
.filename
);
3032 *stat_ptr
->tail
= add
.head
;
3033 stat_ptr
->tail
= add
.tail
;
3037 *add
.tail
= s
->header
.next
;
3038 s
->header
.next
= add
.head
;
3049 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
3052 lang_track_definedness (const char *name
)
3054 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
3055 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
3058 /* New-function for the definedness hash table. */
3060 static struct bfd_hash_entry
*
3061 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
3062 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
3063 const char *name ATTRIBUTE_UNUSED
)
3065 struct lang_definedness_hash_entry
*ret
3066 = (struct lang_definedness_hash_entry
*) entry
;
3069 ret
= (struct lang_definedness_hash_entry
*)
3070 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
3073 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
3075 ret
->iteration
= -1;
3079 /* Return the iteration when the definition of NAME was last updated. A
3080 value of -1 means that the symbol is not defined in the linker script
3081 or the command line, but may be defined in the linker symbol table. */
3084 lang_symbol_definition_iteration (const char *name
)
3086 struct lang_definedness_hash_entry
*defentry
3087 = (struct lang_definedness_hash_entry
*)
3088 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3090 /* We've already created this one on the presence of DEFINED in the
3091 script, so it can't be NULL unless something is borked elsewhere in
3093 if (defentry
== NULL
)
3096 return defentry
->iteration
;
3099 /* Update the definedness state of NAME. */
3102 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
3104 struct lang_definedness_hash_entry
*defentry
3105 = (struct lang_definedness_hash_entry
*)
3106 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3108 /* We don't keep track of symbols not tested with DEFINED. */
3109 if (defentry
== NULL
)
3112 /* If the symbol was already defined, and not from an earlier statement
3113 iteration, don't update the definedness iteration, because that'd
3114 make the symbol seem defined in the linker script at this point, and
3115 it wasn't; it was defined in some object. If we do anyway, DEFINED
3116 would start to yield false before this point and the construct "sym =
3117 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3119 if (h
->type
!= bfd_link_hash_undefined
3120 && h
->type
!= bfd_link_hash_common
3121 && h
->type
!= bfd_link_hash_new
3122 && defentry
->iteration
== -1)
3125 defentry
->iteration
= lang_statement_iteration
;
3128 /* Add the supplied name to the symbol table as an undefined reference.
3129 This is a two step process as the symbol table doesn't even exist at
3130 the time the ld command line is processed. First we put the name
3131 on a list, then, once the output file has been opened, transfer the
3132 name to the symbol table. */
3134 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3136 #define ldlang_undef_chain_list_head entry_symbol.next
3139 ldlang_add_undef (const char *const name
)
3141 ldlang_undef_chain_list_type
*new =
3142 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
3144 new->next
= ldlang_undef_chain_list_head
;
3145 ldlang_undef_chain_list_head
= new;
3147 new->name
= xstrdup (name
);
3149 if (link_info
.output_bfd
!= NULL
)
3150 insert_undefined (new->name
);
3153 /* Insert NAME as undefined in the symbol table. */
3156 insert_undefined (const char *name
)
3158 struct bfd_link_hash_entry
*h
;
3160 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3162 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3163 if (h
->type
== bfd_link_hash_new
)
3165 h
->type
= bfd_link_hash_undefined
;
3166 h
->u
.undef
.abfd
= NULL
;
3167 bfd_link_add_undef (link_info
.hash
, h
);
3171 /* Run through the list of undefineds created above and place them
3172 into the linker hash table as undefined symbols belonging to the
3176 lang_place_undefineds (void)
3178 ldlang_undef_chain_list_type
*ptr
;
3180 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3181 insert_undefined (ptr
->name
);
3184 /* Check for all readonly or some readwrite sections. */
3187 check_input_sections
3188 (lang_statement_union_type
*s
,
3189 lang_output_section_statement_type
*output_section_statement
)
3191 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3193 switch (s
->header
.type
)
3195 case lang_wild_statement_enum
:
3196 walk_wild (&s
->wild_statement
, check_section_callback
,
3197 output_section_statement
);
3198 if (! output_section_statement
->all_input_readonly
)
3201 case lang_constructors_statement_enum
:
3202 check_input_sections (constructor_list
.head
,
3203 output_section_statement
);
3204 if (! output_section_statement
->all_input_readonly
)
3207 case lang_group_statement_enum
:
3208 check_input_sections (s
->group_statement
.children
.head
,
3209 output_section_statement
);
3210 if (! output_section_statement
->all_input_readonly
)
3219 /* Update wildcard statements if needed. */
3222 update_wild_statements (lang_statement_union_type
*s
)
3224 struct wildcard_list
*sec
;
3226 switch (sort_section
)
3236 for (; s
!= NULL
; s
= s
->header
.next
)
3238 switch (s
->header
.type
)
3243 case lang_wild_statement_enum
:
3244 sec
= s
->wild_statement
.section_list
;
3245 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3248 switch (sec
->spec
.sorted
)
3251 sec
->spec
.sorted
= sort_section
;
3254 if (sort_section
== by_alignment
)
3255 sec
->spec
.sorted
= by_name_alignment
;
3258 if (sort_section
== by_name
)
3259 sec
->spec
.sorted
= by_alignment_name
;
3267 case lang_constructors_statement_enum
:
3268 update_wild_statements (constructor_list
.head
);
3271 case lang_output_section_statement_enum
:
3272 update_wild_statements
3273 (s
->output_section_statement
.children
.head
);
3276 case lang_group_statement_enum
:
3277 update_wild_statements (s
->group_statement
.children
.head
);
3285 /* Open input files and attach to output sections. */
3288 map_input_to_output_sections
3289 (lang_statement_union_type
*s
, const char *target
,
3290 lang_output_section_statement_type
*os
)
3294 for (; s
!= NULL
; s
= s
->header
.next
)
3296 switch (s
->header
.type
)
3298 case lang_wild_statement_enum
:
3299 wild (&s
->wild_statement
, target
, os
);
3301 case lang_constructors_statement_enum
:
3302 map_input_to_output_sections (constructor_list
.head
,
3306 case lang_output_section_statement_enum
:
3307 if (s
->output_section_statement
.constraint
)
3309 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3310 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3312 s
->output_section_statement
.all_input_readonly
= TRUE
;
3313 check_input_sections (s
->output_section_statement
.children
.head
,
3314 &s
->output_section_statement
);
3315 if ((s
->output_section_statement
.all_input_readonly
3316 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3317 || (!s
->output_section_statement
.all_input_readonly
3318 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3320 s
->output_section_statement
.constraint
= -1;
3325 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3327 &s
->output_section_statement
);
3329 case lang_output_statement_enum
:
3331 case lang_target_statement_enum
:
3332 target
= s
->target_statement
.target
;
3334 case lang_group_statement_enum
:
3335 map_input_to_output_sections (s
->group_statement
.children
.head
,
3339 case lang_data_statement_enum
:
3340 /* Make sure that any sections mentioned in the expression
3342 exp_init_os (s
->data_statement
.exp
);
3343 flags
= SEC_HAS_CONTENTS
;
3344 /* The output section gets contents, and then we inspect for
3345 any flags set in the input script which override any ALLOC. */
3346 if (!(os
->flags
& SEC_NEVER_LOAD
))
3347 flags
|= SEC_ALLOC
| SEC_LOAD
;
3348 if (os
->bfd_section
== NULL
)
3349 init_os (os
, NULL
, flags
);
3351 os
->bfd_section
->flags
|= flags
;
3353 case lang_input_section_enum
:
3355 case lang_fill_statement_enum
:
3356 case lang_object_symbols_statement_enum
:
3357 case lang_reloc_statement_enum
:
3358 case lang_padding_statement_enum
:
3359 case lang_input_statement_enum
:
3360 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3361 init_os (os
, NULL
, 0);
3363 case lang_assignment_statement_enum
:
3364 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3365 init_os (os
, NULL
, 0);
3367 /* Make sure that any sections mentioned in the assignment
3369 exp_init_os (s
->assignment_statement
.exp
);
3371 case lang_address_statement_enum
:
3372 /* Mark the specified section with the supplied address.
3373 If this section was actually a segment marker, then the
3374 directive is ignored if the linker script explicitly
3375 processed the segment marker. Originally, the linker
3376 treated segment directives (like -Ttext on the
3377 command-line) as section directives. We honor the
3378 section directive semantics for backwards compatibilty;
3379 linker scripts that do not specifically check for
3380 SEGMENT_START automatically get the old semantics. */
3381 if (!s
->address_statement
.segment
3382 || !s
->address_statement
.segment
->used
)
3384 lang_output_section_statement_type
*aos
3385 = (lang_output_section_statement_lookup
3386 (s
->address_statement
.section_name
, 0, TRUE
));
3388 if (aos
->bfd_section
== NULL
)
3389 init_os (aos
, NULL
, 0);
3390 aos
->addr_tree
= s
->address_statement
.address
;
3393 case lang_insert_statement_enum
:
3399 /* An insert statement snips out all the linker statements from the
3400 start of the list and places them after the output section
3401 statement specified by the insert. This operation is complicated
3402 by the fact that we keep a doubly linked list of output section
3403 statements as well as the singly linked list of all statements. */
3406 process_insert_statements (void)
3408 lang_statement_union_type
**s
;
3409 lang_output_section_statement_type
*first_os
= NULL
;
3410 lang_output_section_statement_type
*last_os
= NULL
;
3411 lang_output_section_statement_type
*os
;
3413 /* "start of list" is actually the statement immediately after
3414 the special abs_section output statement, so that it isn't
3416 s
= &lang_output_section_statement
.head
;
3417 while (*(s
= &(*s
)->header
.next
) != NULL
)
3419 if ((*s
)->header
.type
== lang_output_section_statement_enum
)
3421 /* Keep pointers to the first and last output section
3422 statement in the sequence we may be about to move. */
3423 last_os
= &(*s
)->output_section_statement
;
3425 /* Set constraint negative so that lang_output_section_find
3426 won't match this output section statement. At this
3427 stage in linking constraint has values in the range
3428 [-1, ONLY_IN_RW]. */
3429 last_os
->constraint
= -2 - last_os
->constraint
;
3430 if (first_os
== NULL
)
3433 else if ((*s
)->header
.type
== lang_insert_statement_enum
)
3435 lang_insert_statement_type
*i
= &(*s
)->insert_statement
;
3436 lang_output_section_statement_type
*where
;
3437 lang_statement_union_type
**ptr
;
3438 lang_statement_union_type
*first
;
3440 where
= lang_output_section_find (i
->where
);
3441 if (where
!= NULL
&& i
->is_before
)
3444 where
= where
->prev
;
3445 while (where
!= NULL
&& where
->constraint
< 0);
3449 einfo (_("%F%P: %s not found for insert\n"), i
->where
);
3453 /* Deal with reordering the output section statement list. */
3454 if (last_os
!= NULL
)
3456 asection
*first_sec
, *last_sec
;
3457 struct lang_output_section_statement_struct
**next
;
3459 /* Snip out the output sections we are moving. */
3460 first_os
->prev
->next
= last_os
->next
;
3461 if (last_os
->next
== NULL
)
3463 next
= &first_os
->prev
->next
;
3464 lang_output_section_statement
.tail
3465 = (lang_statement_union_type
**) next
;
3468 last_os
->next
->prev
= first_os
->prev
;
3469 /* Add them in at the new position. */
3470 last_os
->next
= where
->next
;
3471 if (where
->next
== NULL
)
3473 next
= &last_os
->next
;
3474 lang_output_section_statement
.tail
3475 = (lang_statement_union_type
**) next
;
3478 where
->next
->prev
= last_os
;
3479 first_os
->prev
= where
;
3480 where
->next
= first_os
;
3482 /* Move the bfd sections in the same way. */
3485 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3487 os
->constraint
= -2 - os
->constraint
;
3488 if (os
->bfd_section
!= NULL
3489 && os
->bfd_section
->owner
!= NULL
)
3491 last_sec
= os
->bfd_section
;
3492 if (first_sec
== NULL
)
3493 first_sec
= last_sec
;
3498 if (last_sec
!= NULL
)
3500 asection
*sec
= where
->bfd_section
;
3502 sec
= output_prev_sec_find (where
);
3504 /* The place we want to insert must come after the
3505 sections we are moving. So if we find no
3506 section or if the section is the same as our
3507 last section, then no move is needed. */
3508 if (sec
!= NULL
&& sec
!= last_sec
)
3510 /* Trim them off. */
3511 if (first_sec
->prev
!= NULL
)
3512 first_sec
->prev
->next
= last_sec
->next
;
3514 link_info
.output_bfd
->sections
= last_sec
->next
;
3515 if (last_sec
->next
!= NULL
)
3516 last_sec
->next
->prev
= first_sec
->prev
;
3518 link_info
.output_bfd
->section_last
= first_sec
->prev
;
3520 last_sec
->next
= sec
->next
;
3521 if (sec
->next
!= NULL
)
3522 sec
->next
->prev
= last_sec
;
3524 link_info
.output_bfd
->section_last
= last_sec
;
3525 first_sec
->prev
= sec
;
3526 sec
->next
= first_sec
;
3534 ptr
= insert_os_after (where
);
3535 /* Snip everything after the abs_section output statement we
3536 know is at the start of the list, up to and including
3537 the insert statement we are currently processing. */
3538 first
= lang_output_section_statement
.head
->header
.next
;
3539 lang_output_section_statement
.head
->header
.next
= (*s
)->header
.next
;
3540 /* Add them back where they belong. */
3543 statement_list
.tail
= s
;
3545 s
= &lang_output_section_statement
.head
;
3549 /* Undo constraint twiddling. */
3550 for (os
= first_os
; os
!= NULL
; os
= os
->next
)
3552 os
->constraint
= -2 - os
->constraint
;
3558 /* An output section might have been removed after its statement was
3559 added. For example, ldemul_before_allocation can remove dynamic
3560 sections if they turn out to be not needed. Clean them up here. */
3563 strip_excluded_output_sections (void)
3565 lang_output_section_statement_type
*os
;
3567 /* Run lang_size_sections (if not already done). */
3568 if (expld
.phase
!= lang_mark_phase_enum
)
3570 expld
.phase
= lang_mark_phase_enum
;
3571 expld
.dataseg
.phase
= exp_dataseg_none
;
3572 one_lang_size_sections_pass (NULL
, FALSE
);
3573 lang_reset_memory_regions ();
3576 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3580 asection
*output_section
;
3581 bfd_boolean exclude
;
3583 if (os
->constraint
< 0)
3586 output_section
= os
->bfd_section
;
3587 if (output_section
== NULL
)
3590 exclude
= (output_section
->rawsize
== 0
3591 && (output_section
->flags
& SEC_KEEP
) == 0
3592 && !bfd_section_removed_from_list (link_info
.output_bfd
,
3595 /* Some sections have not yet been sized, notably .gnu.version,
3596 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3597 input sections, so don't drop output sections that have such
3598 input sections unless they are also marked SEC_EXCLUDE. */
3599 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3603 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3604 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3605 && (s
->flags
& SEC_EXCLUDE
) == 0)
3612 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3613 output_section
->map_head
.link_order
= NULL
;
3614 output_section
->map_tail
.link_order
= NULL
;
3618 /* We don't set bfd_section to NULL since bfd_section of the
3619 removed output section statement may still be used. */
3620 if (!os
->section_relative_symbol
3621 && !os
->update_dot_tree
)
3623 output_section
->flags
|= SEC_EXCLUDE
;
3624 bfd_section_list_remove (link_info
.output_bfd
, output_section
);
3625 link_info
.output_bfd
->section_count
--;
3629 /* Stop future calls to lang_add_section from messing with map_head
3630 and map_tail link_order fields. */
3631 stripped_excluded_sections
= TRUE
;
3635 print_output_section_statement
3636 (lang_output_section_statement_type
*output_section_statement
)
3638 asection
*section
= output_section_statement
->bfd_section
;
3641 if (output_section_statement
!= abs_output_section
)
3643 minfo ("\n%s", output_section_statement
->name
);
3645 if (section
!= NULL
)
3647 print_dot
= section
->vma
;
3649 len
= strlen (output_section_statement
->name
);
3650 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3655 while (len
< SECTION_NAME_MAP_LENGTH
)
3661 minfo ("0x%V %W", section
->vma
, section
->size
);
3663 if (section
->vma
!= section
->lma
)
3664 minfo (_(" load address 0x%V"), section
->lma
);
3666 if (output_section_statement
->update_dot_tree
!= NULL
)
3667 exp_fold_tree (output_section_statement
->update_dot_tree
,
3668 bfd_abs_section_ptr
, &print_dot
);
3674 print_statement_list (output_section_statement
->children
.head
,
3675 output_section_statement
);
3678 /* Scan for the use of the destination in the right hand side
3679 of an expression. In such cases we will not compute the
3680 correct expression, since the value of DST that is used on
3681 the right hand side will be its final value, not its value
3682 just before this expression is evaluated. */
3685 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3687 if (rhs
== NULL
|| dst
== NULL
)
3690 switch (rhs
->type
.node_class
)
3693 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3694 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3697 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3698 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3701 case etree_provided
:
3703 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3705 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3708 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3712 return strcmp (dst
, rhs
->value
.str
) == 0;
3717 return strcmp (dst
, rhs
->name
.name
) == 0;
3729 print_assignment (lang_assignment_statement_type
*assignment
,
3730 lang_output_section_statement_type
*output_section
)
3734 bfd_boolean computation_is_valid
= TRUE
;
3737 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3740 if (assignment
->exp
->type
.node_class
== etree_assert
)
3743 tree
= assignment
->exp
->assert_s
.child
;
3744 computation_is_valid
= TRUE
;
3748 const char *dst
= assignment
->exp
->assign
.dst
;
3750 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3751 tree
= assignment
->exp
->assign
.src
;
3752 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3755 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3756 if (expld
.result
.valid_p
)
3760 if (computation_is_valid
)
3762 value
= expld
.result
.value
;
3764 if (expld
.result
.section
)
3765 value
+= expld
.result
.section
->vma
;
3767 minfo ("0x%V", value
);
3773 struct bfd_link_hash_entry
*h
;
3775 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3776 FALSE
, FALSE
, TRUE
);
3779 value
= h
->u
.def
.value
;
3781 if (expld
.result
.section
)
3782 value
+= expld
.result
.section
->vma
;
3784 minfo ("[0x%V]", value
);
3787 minfo ("[unresolved]");
3799 exp_print_tree (assignment
->exp
);
3804 print_input_statement (lang_input_statement_type
*statm
)
3806 if (statm
->filename
!= NULL
3807 && (statm
->the_bfd
== NULL
3808 || (statm
->the_bfd
->flags
& BFD_LINKER_CREATED
) == 0))
3809 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3812 /* Print all symbols defined in a particular section. This is called
3813 via bfd_link_hash_traverse, or by print_all_symbols. */
3816 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3818 asection
*sec
= ptr
;
3820 if ((hash_entry
->type
== bfd_link_hash_defined
3821 || hash_entry
->type
== bfd_link_hash_defweak
)
3822 && sec
== hash_entry
->u
.def
.section
)
3826 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3829 (hash_entry
->u
.def
.value
3830 + hash_entry
->u
.def
.section
->output_offset
3831 + hash_entry
->u
.def
.section
->output_section
->vma
));
3833 minfo (" %T\n", hash_entry
->root
.string
);
3840 print_all_symbols (asection
*sec
)
3842 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3843 struct map_symbol_def
*def
;
3848 *ud
->map_symbol_def_tail
= 0;
3849 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3850 print_one_symbol (def
->entry
, sec
);
3853 /* Print information about an input section to the map file. */
3856 print_input_section (asection
*i
)
3858 bfd_size_type size
= i
->size
;
3865 minfo ("%s", i
->name
);
3867 len
= 1 + strlen (i
->name
);
3868 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3873 while (len
< SECTION_NAME_MAP_LENGTH
)
3879 if (i
->output_section
!= NULL
3880 && i
->output_section
->owner
== link_info
.output_bfd
)
3881 addr
= i
->output_section
->vma
+ i
->output_offset
;
3888 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3890 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3892 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3904 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3907 if (i
->output_section
!= NULL
3908 && i
->output_section
->owner
== link_info
.output_bfd
)
3910 if (link_info
.reduce_memory_overheads
)
3911 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3913 print_all_symbols (i
);
3915 /* Update print_dot, but make sure that we do not move it
3916 backwards - this could happen if we have overlays and a
3917 later overlay is shorter than an earier one. */
3918 if (addr
+ TO_ADDR (size
) > print_dot
)
3919 print_dot
= addr
+ TO_ADDR (size
);
3924 print_fill_statement (lang_fill_statement_type
*fill
)
3928 fputs (" FILL mask 0x", config
.map_file
);
3929 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3930 fprintf (config
.map_file
, "%02x", *p
);
3931 fputs ("\n", config
.map_file
);
3935 print_data_statement (lang_data_statement_type
*data
)
3943 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3946 addr
= data
->output_offset
;
3947 if (data
->output_section
!= NULL
)
3948 addr
+= data
->output_section
->vma
;
3976 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3978 if (data
->exp
->type
.node_class
!= etree_value
)
3981 exp_print_tree (data
->exp
);
3986 print_dot
= addr
+ TO_ADDR (size
);
3989 /* Print an address statement. These are generated by options like
3993 print_address_statement (lang_address_statement_type
*address
)
3995 minfo (_("Address of section %s set to "), address
->section_name
);
3996 exp_print_tree (address
->address
);
4000 /* Print a reloc statement. */
4003 print_reloc_statement (lang_reloc_statement_type
*reloc
)
4010 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
4013 addr
= reloc
->output_offset
;
4014 if (reloc
->output_section
!= NULL
)
4015 addr
+= reloc
->output_section
->vma
;
4017 size
= bfd_get_reloc_size (reloc
->howto
);
4019 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
4021 if (reloc
->name
!= NULL
)
4022 minfo ("%s+", reloc
->name
);
4024 minfo ("%s+", reloc
->section
->name
);
4026 exp_print_tree (reloc
->addend_exp
);
4030 print_dot
= addr
+ TO_ADDR (size
);
4034 print_padding_statement (lang_padding_statement_type
*s
)
4042 len
= sizeof " *fill*" - 1;
4043 while (len
< SECTION_NAME_MAP_LENGTH
)
4049 addr
= s
->output_offset
;
4050 if (s
->output_section
!= NULL
)
4051 addr
+= s
->output_section
->vma
;
4052 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
4054 if (s
->fill
->size
!= 0)
4058 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
4059 fprintf (config
.map_file
, "%02x", *p
);
4064 print_dot
= addr
+ TO_ADDR (s
->size
);
4068 print_wild_statement (lang_wild_statement_type
*w
,
4069 lang_output_section_statement_type
*os
)
4071 struct wildcard_list
*sec
;
4075 if (w
->filenames_sorted
)
4077 if (w
->filename
!= NULL
)
4078 minfo ("%s", w
->filename
);
4081 if (w
->filenames_sorted
)
4085 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
4087 if (sec
->spec
.sorted
)
4089 if (sec
->spec
.exclude_name_list
!= NULL
)
4092 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
4093 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
4094 minfo (" %s", tmp
->name
);
4097 if (sec
->spec
.name
!= NULL
)
4098 minfo ("%s", sec
->spec
.name
);
4101 if (sec
->spec
.sorted
)
4110 print_statement_list (w
->children
.head
, os
);
4113 /* Print a group statement. */
4116 print_group (lang_group_statement_type
*s
,
4117 lang_output_section_statement_type
*os
)
4119 fprintf (config
.map_file
, "START GROUP\n");
4120 print_statement_list (s
->children
.head
, os
);
4121 fprintf (config
.map_file
, "END GROUP\n");
4124 /* Print the list of statements in S.
4125 This can be called for any statement type. */
4128 print_statement_list (lang_statement_union_type
*s
,
4129 lang_output_section_statement_type
*os
)
4133 print_statement (s
, os
);
4138 /* Print the first statement in statement list S.
4139 This can be called for any statement type. */
4142 print_statement (lang_statement_union_type
*s
,
4143 lang_output_section_statement_type
*os
)
4145 switch (s
->header
.type
)
4148 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
4151 case lang_constructors_statement_enum
:
4152 if (constructor_list
.head
!= NULL
)
4154 if (constructors_sorted
)
4155 minfo (" SORT (CONSTRUCTORS)\n");
4157 minfo (" CONSTRUCTORS\n");
4158 print_statement_list (constructor_list
.head
, os
);
4161 case lang_wild_statement_enum
:
4162 print_wild_statement (&s
->wild_statement
, os
);
4164 case lang_address_statement_enum
:
4165 print_address_statement (&s
->address_statement
);
4167 case lang_object_symbols_statement_enum
:
4168 minfo (" CREATE_OBJECT_SYMBOLS\n");
4170 case lang_fill_statement_enum
:
4171 print_fill_statement (&s
->fill_statement
);
4173 case lang_data_statement_enum
:
4174 print_data_statement (&s
->data_statement
);
4176 case lang_reloc_statement_enum
:
4177 print_reloc_statement (&s
->reloc_statement
);
4179 case lang_input_section_enum
:
4180 print_input_section (s
->input_section
.section
);
4182 case lang_padding_statement_enum
:
4183 print_padding_statement (&s
->padding_statement
);
4185 case lang_output_section_statement_enum
:
4186 print_output_section_statement (&s
->output_section_statement
);
4188 case lang_assignment_statement_enum
:
4189 print_assignment (&s
->assignment_statement
, os
);
4191 case lang_target_statement_enum
:
4192 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
4194 case lang_output_statement_enum
:
4195 minfo ("OUTPUT(%s", s
->output_statement
.name
);
4196 if (output_target
!= NULL
)
4197 minfo (" %s", output_target
);
4200 case lang_input_statement_enum
:
4201 print_input_statement (&s
->input_statement
);
4203 case lang_group_statement_enum
:
4204 print_group (&s
->group_statement
, os
);
4206 case lang_insert_statement_enum
:
4207 minfo ("INSERT %s %s\n",
4208 s
->insert_statement
.is_before
? "BEFORE" : "AFTER",
4209 s
->insert_statement
.where
);
4215 print_statements (void)
4217 print_statement_list (statement_list
.head
, abs_output_section
);
4220 /* Print the first N statements in statement list S to STDERR.
4221 If N == 0, nothing is printed.
4222 If N < 0, the entire list is printed.
4223 Intended to be called from GDB. */
4226 dprint_statement (lang_statement_union_type
*s
, int n
)
4228 FILE *map_save
= config
.map_file
;
4230 config
.map_file
= stderr
;
4233 print_statement_list (s
, abs_output_section
);
4236 while (s
&& --n
>= 0)
4238 print_statement (s
, abs_output_section
);
4243 config
.map_file
= map_save
;
4247 insert_pad (lang_statement_union_type
**ptr
,
4249 unsigned int alignment_needed
,
4250 asection
*output_section
,
4253 static fill_type zero_fill
= { 1, { 0 } };
4254 lang_statement_union_type
*pad
= NULL
;
4256 if (ptr
!= &statement_list
.head
)
4257 pad
= ((lang_statement_union_type
*)
4258 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4260 && pad
->header
.type
== lang_padding_statement_enum
4261 && pad
->padding_statement
.output_section
== output_section
)
4263 /* Use the existing pad statement. */
4265 else if ((pad
= *ptr
) != NULL
4266 && pad
->header
.type
== lang_padding_statement_enum
4267 && pad
->padding_statement
.output_section
== output_section
)
4269 /* Use the existing pad statement. */
4273 /* Make a new padding statement, linked into existing chain. */
4274 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4275 pad
->header
.next
= *ptr
;
4277 pad
->header
.type
= lang_padding_statement_enum
;
4278 pad
->padding_statement
.output_section
= output_section
;
4281 pad
->padding_statement
.fill
= fill
;
4283 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4284 pad
->padding_statement
.size
= alignment_needed
;
4285 output_section
->size
+= alignment_needed
;
4288 /* Work out how much this section will move the dot point. */
4292 (lang_statement_union_type
**this_ptr
,
4293 lang_output_section_statement_type
*output_section_statement
,
4297 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4298 asection
*i
= is
->section
;
4300 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
4301 && (i
->flags
& SEC_EXCLUDE
) == 0)
4303 unsigned int alignment_needed
;
4306 /* Align this section first to the input sections requirement,
4307 then to the output section's requirement. If this alignment
4308 is greater than any seen before, then record it too. Perform
4309 the alignment by inserting a magic 'padding' statement. */
4311 if (output_section_statement
->subsection_alignment
!= -1)
4312 i
->alignment_power
= output_section_statement
->subsection_alignment
;
4314 o
= output_section_statement
->bfd_section
;
4315 if (o
->alignment_power
< i
->alignment_power
)
4316 o
->alignment_power
= i
->alignment_power
;
4318 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4320 if (alignment_needed
!= 0)
4322 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4323 dot
+= alignment_needed
;
4326 /* Remember where in the output section this input section goes. */
4328 i
->output_offset
= dot
- o
->vma
;
4330 /* Mark how big the output section must be to contain this now. */
4331 dot
+= TO_ADDR (i
->size
);
4332 o
->size
= TO_SIZE (dot
- o
->vma
);
4336 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
4343 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4345 const asection
*sec1
= *(const asection
**) arg1
;
4346 const asection
*sec2
= *(const asection
**) arg2
;
4348 if (bfd_section_lma (sec1
->owner
, sec1
)
4349 < bfd_section_lma (sec2
->owner
, sec2
))
4351 else if (bfd_section_lma (sec1
->owner
, sec1
)
4352 > bfd_section_lma (sec2
->owner
, sec2
))
4354 else if (sec1
->id
< sec2
->id
)
4356 else if (sec1
->id
> sec2
->id
)
4362 #define IGNORE_SECTION(s) \
4363 ((s->flags & SEC_NEVER_LOAD) != 0 \
4364 || (s->flags & SEC_ALLOC) == 0 \
4365 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4366 && (s->flags & SEC_LOAD) == 0))
4368 /* Check to see if any allocated sections overlap with other allocated
4369 sections. This can happen if a linker script specifies the output
4370 section addresses of the two sections. Also check whether any memory
4371 region has overflowed. */
4374 lang_check_section_addresses (void)
4377 asection
**sections
, **spp
;
4384 lang_memory_region_type
*m
;
4386 if (bfd_count_sections (link_info
.output_bfd
) <= 1)
4389 amt
= bfd_count_sections (link_info
.output_bfd
) * sizeof (asection
*);
4390 sections
= xmalloc (amt
);
4392 /* Scan all sections in the output list. */
4394 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4396 /* Only consider loadable sections with real contents. */
4397 if (IGNORE_SECTION (s
) || s
->size
== 0)
4400 sections
[count
] = s
;
4407 qsort (sections
, (size_t) count
, sizeof (asection
*),
4408 sort_sections_by_lma
);
4412 s_start
= bfd_section_lma (link_info
.output_bfd
, s
);
4413 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4414 for (count
--; count
; count
--)
4416 /* We must check the sections' LMA addresses not their VMA
4417 addresses because overlay sections can have overlapping VMAs
4418 but they must have distinct LMAs. */
4423 s_start
= bfd_section_lma (link_info
.output_bfd
, s
);
4424 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4426 /* Look for an overlap. */
4427 if (s_end
>= os_start
&& s_start
<= os_end
)
4428 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4429 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4434 /* If any memory region has overflowed, report by how much.
4435 We do not issue this diagnostic for regions that had sections
4436 explicitly placed outside their bounds; os_region_check's
4437 diagnostics are adequate for that case.
4439 FIXME: It is conceivable that m->current - (m->origin + m->length)
4440 might overflow a 32-bit integer. There is, alas, no way to print
4441 a bfd_vma quantity in decimal. */
4442 for (m
= lang_memory_region_list
; m
; m
= m
->next
)
4443 if (m
->had_full_message
)
4444 einfo (_("%X%P: region %s overflowed by %ld bytes\n"),
4445 m
->name
, (long)(m
->current
- (m
->origin
+ m
->length
)));
4449 /* Make sure the new address is within the region. We explicitly permit the
4450 current address to be at the exact end of the region when the address is
4451 non-zero, in case the region is at the end of addressable memory and the
4452 calculation wraps around. */
4455 os_region_check (lang_output_section_statement_type
*os
,
4456 lang_memory_region_type
*region
,
4460 if ((region
->current
< region
->origin
4461 || (region
->current
- region
->origin
> region
->length
))
4462 && ((region
->current
!= region
->origin
+ region
->length
)
4467 einfo (_("%X%P: address 0x%v of %B section %s"
4468 " is not within region %s\n"),
4470 os
->bfd_section
->owner
,
4471 os
->bfd_section
->name
,
4474 else if (!region
->had_full_message
)
4476 region
->had_full_message
= TRUE
;
4478 einfo (_("%X%P: %B section %s will not fit in region %s\n"),
4479 os
->bfd_section
->owner
,
4480 os
->bfd_section
->name
,
4486 /* Set the sizes for all the output sections. */
4489 lang_size_sections_1
4490 (lang_statement_union_type
*s
,
4491 lang_output_section_statement_type
*output_section_statement
,
4492 lang_statement_union_type
**prev
,
4496 bfd_boolean check_regions
)
4498 /* Size up the sections from their constituent parts. */
4499 for (; s
!= NULL
; s
= s
->header
.next
)
4501 switch (s
->header
.type
)
4503 case lang_output_section_statement_enum
:
4505 bfd_vma newdot
, after
;
4506 lang_output_section_statement_type
*os
;
4507 lang_memory_region_type
*r
;
4509 os
= &s
->output_section_statement
;
4510 if (os
->addr_tree
!= NULL
)
4512 os
->processed_vma
= FALSE
;
4513 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4515 if (expld
.result
.valid_p
)
4516 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4517 else if (expld
.phase
!= lang_mark_phase_enum
)
4518 einfo (_("%F%S: non constant or forward reference"
4519 " address expression for section %s\n"),
4523 if (os
->bfd_section
== NULL
)
4524 /* This section was removed or never actually created. */
4527 /* If this is a COFF shared library section, use the size and
4528 address from the input section. FIXME: This is COFF
4529 specific; it would be cleaner if there were some other way
4530 to do this, but nothing simple comes to mind. */
4531 if (((bfd_get_flavour (link_info
.output_bfd
)
4532 == bfd_target_ecoff_flavour
)
4533 || (bfd_get_flavour (link_info
.output_bfd
)
4534 == bfd_target_coff_flavour
))
4535 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4539 if (os
->children
.head
== NULL
4540 || os
->children
.head
->header
.next
!= NULL
4541 || (os
->children
.head
->header
.type
4542 != lang_input_section_enum
))
4543 einfo (_("%P%X: Internal error on COFF shared library"
4544 " section %s\n"), os
->name
);
4546 input
= os
->children
.head
->input_section
.section
;
4547 bfd_set_section_vma (os
->bfd_section
->owner
,
4549 bfd_section_vma (input
->owner
, input
));
4550 os
->bfd_section
->size
= input
->size
;
4555 if (bfd_is_abs_section (os
->bfd_section
))
4557 /* No matter what happens, an abs section starts at zero. */
4558 ASSERT (os
->bfd_section
->vma
== 0);
4564 if (os
->addr_tree
== NULL
)
4566 /* No address specified for this section, get one
4567 from the region specification. */
4568 if (os
->region
== NULL
4569 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4570 && os
->region
->name
[0] == '*'
4571 && strcmp (os
->region
->name
,
4572 DEFAULT_MEMORY_REGION
) == 0))
4574 os
->region
= lang_memory_default (os
->bfd_section
);
4577 /* If a loadable section is using the default memory
4578 region, and some non default memory regions were
4579 defined, issue an error message. */
4581 && !IGNORE_SECTION (os
->bfd_section
)
4582 && ! link_info
.relocatable
4584 && strcmp (os
->region
->name
,
4585 DEFAULT_MEMORY_REGION
) == 0
4586 && lang_memory_region_list
!= NULL
4587 && (strcmp (lang_memory_region_list
->name
,
4588 DEFAULT_MEMORY_REGION
) != 0
4589 || lang_memory_region_list
->next
!= NULL
)
4590 && expld
.phase
!= lang_mark_phase_enum
)
4592 /* By default this is an error rather than just a
4593 warning because if we allocate the section to the
4594 default memory region we can end up creating an
4595 excessively large binary, or even seg faulting when
4596 attempting to perform a negative seek. See
4597 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4598 for an example of this. This behaviour can be
4599 overridden by the using the --no-check-sections
4601 if (command_line
.check_section_addresses
)
4602 einfo (_("%P%F: error: no memory region specified"
4603 " for loadable section `%s'\n"),
4604 bfd_get_section_name (link_info
.output_bfd
,
4607 einfo (_("%P: warning: no memory region specified"
4608 " for loadable section `%s'\n"),
4609 bfd_get_section_name (link_info
.output_bfd
,
4613 newdot
= os
->region
->current
;
4614 align
= os
->bfd_section
->alignment_power
;
4617 align
= os
->section_alignment
;
4619 /* Align to what the section needs. */
4622 bfd_vma savedot
= newdot
;
4623 newdot
= align_power (newdot
, align
);
4625 if (newdot
!= savedot
4626 && (config
.warn_section_align
4627 || os
->addr_tree
!= NULL
)
4628 && expld
.phase
!= lang_mark_phase_enum
)
4629 einfo (_("%P: warning: changing start of section"
4630 " %s by %lu bytes\n"),
4631 os
->name
, (unsigned long) (newdot
- savedot
));
4634 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4636 os
->bfd_section
->output_offset
= 0;
4639 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4640 os
->fill
, newdot
, relax
, check_regions
);
4642 os
->processed_vma
= TRUE
;
4644 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4645 /* Except for some special linker created sections,
4646 no output section should change from zero size
4647 after strip_excluded_output_sections. A non-zero
4648 size on an ignored section indicates that some
4649 input section was not sized early enough. */
4650 ASSERT (os
->bfd_section
->size
== 0);
4653 dot
= os
->bfd_section
->vma
;
4655 /* Put the section within the requested block size, or
4656 align at the block boundary. */
4658 + TO_ADDR (os
->bfd_section
->size
)
4659 + os
->block_value
- 1)
4660 & - (bfd_vma
) os
->block_value
);
4662 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4665 /* Set section lma. */
4668 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
4672 bfd_vma lma
= exp_get_abs_int (os
->load_base
, 0, "load base");
4673 os
->bfd_section
->lma
= lma
;
4675 else if (os
->lma_region
!= NULL
)
4677 bfd_vma lma
= os
->lma_region
->current
;
4679 if (os
->section_alignment
!= -1)
4680 lma
= align_power (lma
, os
->section_alignment
);
4681 os
->bfd_section
->lma
= lma
;
4683 else if (r
->last_os
!= NULL
4684 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0)
4689 last
= r
->last_os
->output_section_statement
.bfd_section
;
4691 /* A backwards move of dot should be accompanied by
4692 an explicit assignment to the section LMA (ie.
4693 os->load_base set) because backwards moves can
4694 create overlapping LMAs. */
4696 && os
->bfd_section
->size
!= 0
4697 && dot
+ os
->bfd_section
->size
<= last
->vma
)
4699 /* If dot moved backwards then leave lma equal to
4700 vma. This is the old default lma, which might
4701 just happen to work when the backwards move is
4702 sufficiently large. Nag if this changes anything,
4703 so people can fix their linker scripts. */
4705 if (last
->vma
!= last
->lma
)
4706 einfo (_("%P: warning: dot moved backwards before `%s'\n"),
4711 /* If this is an overlay, set the current lma to that
4712 at the end of the previous section. */
4713 if (os
->sectype
== overlay_section
)
4714 lma
= last
->lma
+ last
->size
;
4716 /* Otherwise, keep the same lma to vma relationship
4717 as the previous section. */
4719 lma
= dot
+ last
->lma
- last
->vma
;
4721 if (os
->section_alignment
!= -1)
4722 lma
= align_power (lma
, os
->section_alignment
);
4723 os
->bfd_section
->lma
= lma
;
4726 os
->processed_lma
= TRUE
;
4728 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4731 /* Keep track of normal sections using the default
4732 lma region. We use this to set the lma for
4733 following sections. Overlays or other linker
4734 script assignment to lma might mean that the
4735 default lma == vma is incorrect.
4736 To avoid warnings about dot moving backwards when using
4737 -Ttext, don't start tracking sections until we find one
4738 of non-zero size or with lma set differently to vma. */
4739 if (((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4740 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0)
4741 && (os
->bfd_section
->flags
& SEC_ALLOC
) != 0
4742 && (os
->bfd_section
->size
!= 0
4743 || (r
->last_os
== NULL
4744 && os
->bfd_section
->vma
!= os
->bfd_section
->lma
)
4745 || (r
->last_os
!= NULL
4746 && dot
>= (r
->last_os
->output_section_statement
4747 .bfd_section
->vma
)))
4748 && os
->lma_region
== NULL
4749 && !link_info
.relocatable
)
4752 /* .tbss sections effectively have zero size. */
4753 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4754 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4755 || link_info
.relocatable
)
4756 dot
+= TO_ADDR (os
->bfd_section
->size
);
4758 if (os
->update_dot_tree
!= 0)
4759 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4761 /* Update dot in the region ?
4762 We only do this if the section is going to be allocated,
4763 since unallocated sections do not contribute to the region's
4764 overall size in memory.
4766 If the SEC_NEVER_LOAD bit is not set, it will affect the
4767 addresses of sections after it. We have to update
4769 if (os
->region
!= NULL
4770 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4771 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4773 os
->region
->current
= dot
;
4776 /* Make sure the new address is within the region. */
4777 os_region_check (os
, os
->region
, os
->addr_tree
,
4778 os
->bfd_section
->vma
);
4780 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
4781 && (os
->bfd_section
->flags
& SEC_LOAD
))
4783 os
->lma_region
->current
4784 = os
->bfd_section
->lma
+ TO_ADDR (os
->bfd_section
->size
);
4787 os_region_check (os
, os
->lma_region
, NULL
,
4788 os
->bfd_section
->lma
);
4794 case lang_constructors_statement_enum
:
4795 dot
= lang_size_sections_1 (constructor_list
.head
,
4796 output_section_statement
,
4797 &s
->wild_statement
.children
.head
,
4798 fill
, dot
, relax
, check_regions
);
4801 case lang_data_statement_enum
:
4803 unsigned int size
= 0;
4805 s
->data_statement
.output_offset
=
4806 dot
- output_section_statement
->bfd_section
->vma
;
4807 s
->data_statement
.output_section
=
4808 output_section_statement
->bfd_section
;
4810 /* We might refer to provided symbols in the expression, and
4811 need to mark them as needed. */
4812 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4814 switch (s
->data_statement
.type
)
4832 if (size
< TO_SIZE ((unsigned) 1))
4833 size
= TO_SIZE ((unsigned) 1);
4834 dot
+= TO_ADDR (size
);
4835 output_section_statement
->bfd_section
->size
+= size
;
4839 case lang_reloc_statement_enum
:
4843 s
->reloc_statement
.output_offset
=
4844 dot
- output_section_statement
->bfd_section
->vma
;
4845 s
->reloc_statement
.output_section
=
4846 output_section_statement
->bfd_section
;
4847 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4848 dot
+= TO_ADDR (size
);
4849 output_section_statement
->bfd_section
->size
+= size
;
4853 case lang_wild_statement_enum
:
4854 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4855 output_section_statement
,
4856 &s
->wild_statement
.children
.head
,
4857 fill
, dot
, relax
, check_regions
);
4860 case lang_object_symbols_statement_enum
:
4861 link_info
.create_object_symbols_section
=
4862 output_section_statement
->bfd_section
;
4865 case lang_output_statement_enum
:
4866 case lang_target_statement_enum
:
4869 case lang_input_section_enum
:
4873 i
= (*prev
)->input_section
.section
;
4878 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4879 einfo (_("%P%F: can't relax section: %E\n"));
4883 dot
= size_input_section (prev
, output_section_statement
,
4884 output_section_statement
->fill
, dot
);
4888 case lang_input_statement_enum
:
4891 case lang_fill_statement_enum
:
4892 s
->fill_statement
.output_section
=
4893 output_section_statement
->bfd_section
;
4895 fill
= s
->fill_statement
.fill
;
4898 case lang_assignment_statement_enum
:
4900 bfd_vma newdot
= dot
;
4901 etree_type
*tree
= s
->assignment_statement
.exp
;
4903 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
4905 exp_fold_tree (tree
,
4906 output_section_statement
->bfd_section
,
4909 if (expld
.dataseg
.relro
== exp_dataseg_relro_start
)
4911 if (!expld
.dataseg
.relro_start_stat
)
4912 expld
.dataseg
.relro_start_stat
= s
;
4915 ASSERT (expld
.dataseg
.relro_start_stat
== s
);
4918 else if (expld
.dataseg
.relro
== exp_dataseg_relro_end
)
4920 if (!expld
.dataseg
.relro_end_stat
)
4921 expld
.dataseg
.relro_end_stat
= s
;
4924 ASSERT (expld
.dataseg
.relro_end_stat
== s
);
4927 expld
.dataseg
.relro
= exp_dataseg_relro_none
;
4929 /* This symbol is relative to this section. */
4930 if ((tree
->type
.node_class
== etree_provided
4931 || tree
->type
.node_class
== etree_assign
)
4932 && (tree
->assign
.dst
[0] != '.'
4933 || tree
->assign
.dst
[1] != '\0'))
4934 output_section_statement
->section_relative_symbol
= 1;
4936 if (!output_section_statement
->ignored
)
4938 if (output_section_statement
== abs_output_section
)
4940 /* If we don't have an output section, then just adjust
4941 the default memory address. */
4942 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4943 FALSE
)->current
= newdot
;
4945 else if (newdot
!= dot
)
4947 /* Insert a pad after this statement. We can't
4948 put the pad before when relaxing, in case the
4949 assignment references dot. */
4950 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4951 output_section_statement
->bfd_section
, dot
);
4953 /* Don't neuter the pad below when relaxing. */
4956 /* If dot is advanced, this implies that the section
4957 should have space allocated to it, unless the
4958 user has explicitly stated that the section
4959 should never be loaded. */
4960 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
4961 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4968 case lang_padding_statement_enum
:
4969 /* If this is the first time lang_size_sections is called,
4970 we won't have any padding statements. If this is the
4971 second or later passes when relaxing, we should allow
4972 padding to shrink. If padding is needed on this pass, it
4973 will be added back in. */
4974 s
->padding_statement
.size
= 0;
4976 /* Make sure output_offset is valid. If relaxation shrinks
4977 the section and this pad isn't needed, it's possible to
4978 have output_offset larger than the final size of the
4979 section. bfd_set_section_contents will complain even for
4980 a pad size of zero. */
4981 s
->padding_statement
.output_offset
4982 = dot
- output_section_statement
->bfd_section
->vma
;
4985 case lang_group_statement_enum
:
4986 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4987 output_section_statement
,
4988 &s
->group_statement
.children
.head
,
4989 fill
, dot
, relax
, check_regions
);
4992 case lang_insert_statement_enum
:
4995 /* We can only get here when relaxing is turned on. */
4996 case lang_address_statement_enum
:
5003 prev
= &s
->header
.next
;
5008 /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
5009 The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
5010 CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
5011 segments. We are allowed an opportunity to override this decision. */
5014 ldlang_override_segment_assignment (struct bfd_link_info
* info ATTRIBUTE_UNUSED
,
5015 bfd
* abfd ATTRIBUTE_UNUSED
,
5016 asection
* current_section
,
5017 asection
* previous_section
,
5018 bfd_boolean new_segment
)
5020 lang_output_section_statement_type
* cur
;
5021 lang_output_section_statement_type
* prev
;
5023 /* The checks below are only necessary when the BFD library has decided
5024 that the two sections ought to be placed into the same segment. */
5028 /* Paranoia checks. */
5029 if (current_section
== NULL
|| previous_section
== NULL
)
5032 /* Find the memory regions associated with the two sections.
5033 We call lang_output_section_find() here rather than scanning the list
5034 of output sections looking for a matching section pointer because if
5035 we have a large number of sections then a hash lookup is faster. */
5036 cur
= lang_output_section_find (current_section
->name
);
5037 prev
= lang_output_section_find (previous_section
->name
);
5039 /* More paranoia. */
5040 if (cur
== NULL
|| prev
== NULL
)
5043 /* If the regions are different then force the sections to live in
5044 different segments. See the email thread starting at the following
5045 URL for the reasons why this is necessary:
5046 http://sourceware.org/ml/binutils/2007-02/msg00216.html */
5047 return cur
->region
!= prev
->region
;
5051 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
5053 lang_statement_iteration
++;
5054 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
5055 &statement_list
.head
, 0, 0, relax
, check_regions
);
5059 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
5061 expld
.phase
= lang_allocating_phase_enum
;
5062 expld
.dataseg
.phase
= exp_dataseg_none
;
5064 one_lang_size_sections_pass (relax
, check_regions
);
5065 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
5066 && link_info
.relro
&& expld
.dataseg
.relro_end
)
5068 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
5069 to put expld.dataseg.relro on a (common) page boundary. */
5070 bfd_vma min_base
, old_base
, relro_end
, maxpage
;
5072 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
5073 maxpage
= expld
.dataseg
.maxpagesize
;
5074 /* MIN_BASE is the absolute minimum address we are allowed to start the
5075 read-write segment (byte before will be mapped read-only). */
5076 min_base
= (expld
.dataseg
.min_base
+ maxpage
- 1) & ~(maxpage
- 1);
5077 /* OLD_BASE is the address for a feasible minimum address which will
5078 still not cause a data overlap inside MAXPAGE causing file offset skip
5080 old_base
= expld
.dataseg
.base
;
5081 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
5082 & (expld
.dataseg
.pagesize
- 1));
5083 /* Compute the expected PT_GNU_RELRO segment end. */
5084 relro_end
= ((expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
5085 & ~(expld
.dataseg
.pagesize
- 1));
5086 if (min_base
+ maxpage
< expld
.dataseg
.base
)
5088 expld
.dataseg
.base
-= maxpage
;
5089 relro_end
-= maxpage
;
5091 lang_reset_memory_regions ();
5092 one_lang_size_sections_pass (relax
, check_regions
);
5093 if (expld
.dataseg
.relro_end
> relro_end
)
5095 /* The alignment of sections between DATA_SEGMENT_ALIGN
5096 and DATA_SEGMENT_RELRO_END caused huge padding to be
5097 inserted at DATA_SEGMENT_RELRO_END. Try to start a bit lower so
5098 that the section alignments will fit in. */
5100 unsigned int max_alignment_power
= 0;
5102 /* Find maximum alignment power of sections between
5103 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
5104 for (sec
= link_info
.output_bfd
->sections
; sec
; sec
= sec
->next
)
5105 if (sec
->vma
>= expld
.dataseg
.base
5106 && sec
->vma
< expld
.dataseg
.relro_end
5107 && sec
->alignment_power
> max_alignment_power
)
5108 max_alignment_power
= sec
->alignment_power
;
5110 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
5112 if (expld
.dataseg
.base
- (1 << max_alignment_power
) < old_base
)
5113 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
5114 expld
.dataseg
.base
-= (1 << max_alignment_power
);
5115 lang_reset_memory_regions ();
5116 one_lang_size_sections_pass (relax
, check_regions
);
5119 link_info
.relro_start
= expld
.dataseg
.base
;
5120 link_info
.relro_end
= expld
.dataseg
.relro_end
;
5122 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
5124 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
5125 a page could be saved in the data segment. */
5126 bfd_vma first
, last
;
5128 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
5129 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
5131 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
5132 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
5133 && first
+ last
<= expld
.dataseg
.pagesize
)
5135 expld
.dataseg
.phase
= exp_dataseg_adjust
;
5136 lang_reset_memory_regions ();
5137 one_lang_size_sections_pass (relax
, check_regions
);
5141 expld
.phase
= lang_final_phase_enum
;
5144 /* Worker function for lang_do_assignments. Recursiveness goes here. */
5147 lang_do_assignments_1 (lang_statement_union_type
*s
,
5148 lang_output_section_statement_type
*current_os
,
5152 for (; s
!= NULL
; s
= s
->header
.next
)
5154 switch (s
->header
.type
)
5156 case lang_constructors_statement_enum
:
5157 dot
= lang_do_assignments_1 (constructor_list
.head
,
5158 current_os
, fill
, dot
);
5161 case lang_output_section_statement_enum
:
5163 lang_output_section_statement_type
*os
;
5165 os
= &(s
->output_section_statement
);
5166 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
5168 dot
= os
->bfd_section
->vma
;
5170 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
5172 /* .tbss sections effectively have zero size. */
5173 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
5174 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
5175 || link_info
.relocatable
)
5176 dot
+= TO_ADDR (os
->bfd_section
->size
);
5178 if (os
->update_dot_tree
!= NULL
)
5179 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
5184 case lang_wild_statement_enum
:
5186 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
5187 current_os
, fill
, dot
);
5190 case lang_object_symbols_statement_enum
:
5191 case lang_output_statement_enum
:
5192 case lang_target_statement_enum
:
5195 case lang_data_statement_enum
:
5196 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
5197 if (expld
.result
.valid_p
)
5198 s
->data_statement
.value
= (expld
.result
.value
5199 + expld
.result
.section
->vma
);
5201 einfo (_("%F%P: invalid data statement\n"));
5204 switch (s
->data_statement
.type
)
5222 if (size
< TO_SIZE ((unsigned) 1))
5223 size
= TO_SIZE ((unsigned) 1);
5224 dot
+= TO_ADDR (size
);
5228 case lang_reloc_statement_enum
:
5229 exp_fold_tree (s
->reloc_statement
.addend_exp
,
5230 bfd_abs_section_ptr
, &dot
);
5231 if (expld
.result
.valid_p
)
5232 s
->reloc_statement
.addend_value
= expld
.result
.value
;
5234 einfo (_("%F%P: invalid reloc statement\n"));
5235 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
5238 case lang_input_section_enum
:
5240 asection
*in
= s
->input_section
.section
;
5242 if ((in
->flags
& SEC_EXCLUDE
) == 0)
5243 dot
+= TO_ADDR (in
->size
);
5247 case lang_input_statement_enum
:
5250 case lang_fill_statement_enum
:
5251 fill
= s
->fill_statement
.fill
;
5254 case lang_assignment_statement_enum
:
5255 exp_fold_tree (s
->assignment_statement
.exp
,
5256 current_os
->bfd_section
,
5260 case lang_padding_statement_enum
:
5261 dot
+= TO_ADDR (s
->padding_statement
.size
);
5264 case lang_group_statement_enum
:
5265 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
5266 current_os
, fill
, dot
);
5269 case lang_insert_statement_enum
:
5272 case lang_address_statement_enum
:
5284 lang_do_assignments (void)
5286 lang_statement_iteration
++;
5287 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
5290 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
5291 operator .startof. (section_name), it produces an undefined symbol
5292 .startof.section_name. Similarly, when it sees
5293 .sizeof. (section_name), it produces an undefined symbol
5294 .sizeof.section_name. For all the output sections, we look for
5295 such symbols, and set them to the correct value. */
5298 lang_set_startof (void)
5302 if (link_info
.relocatable
)
5305 for (s
= link_info
.output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5307 const char *secname
;
5309 struct bfd_link_hash_entry
*h
;
5311 secname
= bfd_get_section_name (link_info
.output_bfd
, s
);
5312 buf
= xmalloc (10 + strlen (secname
));
5314 sprintf (buf
, ".startof.%s", secname
);
5315 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5316 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5318 h
->type
= bfd_link_hash_defined
;
5319 h
->u
.def
.value
= bfd_get_section_vma (link_info
.output_bfd
, s
);
5320 h
->u
.def
.section
= bfd_abs_section_ptr
;
5323 sprintf (buf
, ".sizeof.%s", secname
);
5324 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
5325 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
5327 h
->type
= bfd_link_hash_defined
;
5328 h
->u
.def
.value
= TO_ADDR (s
->size
);
5329 h
->u
.def
.section
= bfd_abs_section_ptr
;
5339 struct bfd_link_hash_entry
*h
;
5342 if ((link_info
.relocatable
&& !link_info
.gc_sections
)
5343 || link_info
.shared
)
5344 warn
= entry_from_cmdline
;
5348 /* Force the user to specify a root when generating a relocatable with
5350 if (link_info
.gc_sections
&& link_info
.relocatable
5351 && (entry_symbol
.name
== NULL
5352 && ldlang_undef_chain_list_head
== NULL
))
5353 einfo (_("%P%F: gc-sections requires either an entry or "
5354 "an undefined symbol\n"));
5356 if (entry_symbol
.name
== NULL
)
5358 /* No entry has been specified. Look for the default entry, but
5359 don't warn if we don't find it. */
5360 entry_symbol
.name
= entry_symbol_default
;
5364 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
5365 FALSE
, FALSE
, TRUE
);
5367 && (h
->type
== bfd_link_hash_defined
5368 || h
->type
== bfd_link_hash_defweak
)
5369 && h
->u
.def
.section
->output_section
!= NULL
)
5373 val
= (h
->u
.def
.value
5374 + bfd_get_section_vma (link_info
.output_bfd
,
5375 h
->u
.def
.section
->output_section
)
5376 + h
->u
.def
.section
->output_offset
);
5377 if (! bfd_set_start_address (link_info
.output_bfd
, val
))
5378 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
5385 /* We couldn't find the entry symbol. Try parsing it as a
5387 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
5390 if (! bfd_set_start_address (link_info
.output_bfd
, val
))
5391 einfo (_("%P%F: can't set start address\n"));
5397 /* Can't find the entry symbol, and it's not a number. Use
5398 the first address in the text section. */
5399 ts
= bfd_get_section_by_name (link_info
.output_bfd
, entry_section
);
5403 einfo (_("%P: warning: cannot find entry symbol %s;"
5404 " defaulting to %V\n"),
5406 bfd_get_section_vma (link_info
.output_bfd
, ts
));
5407 if (!(bfd_set_start_address
5408 (link_info
.output_bfd
,
5409 bfd_get_section_vma (link_info
.output_bfd
, ts
))))
5410 einfo (_("%P%F: can't set start address\n"));
5415 einfo (_("%P: warning: cannot find entry symbol %s;"
5416 " not setting start address\n"),
5422 /* Don't bfd_hash_table_free (&lang_definedness_table);
5423 map file output may result in a call of lang_track_definedness. */
5426 /* This is a small function used when we want to ignore errors from
5430 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
5432 /* Don't do anything. */
5435 /* Check that the architecture of all the input files is compatible
5436 with the output file. Also call the backend to let it do any
5437 other checking that is needed. */
5442 lang_statement_union_type
*file
;
5444 const bfd_arch_info_type
*compatible
;
5446 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
5448 input_bfd
= file
->input_statement
.the_bfd
;
5450 = bfd_arch_get_compatible (input_bfd
, link_info
.output_bfd
,
5451 command_line
.accept_unknown_input_arch
);
5453 /* In general it is not possible to perform a relocatable
5454 link between differing object formats when the input
5455 file has relocations, because the relocations in the
5456 input format may not have equivalent representations in
5457 the output format (and besides BFD does not translate
5458 relocs for other link purposes than a final link). */
5459 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
5460 && (compatible
== NULL
5461 || (bfd_get_flavour (input_bfd
)
5462 != bfd_get_flavour (link_info
.output_bfd
)))
5463 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
5465 einfo (_("%P%F: Relocatable linking with relocations from"
5466 " format %s (%B) to format %s (%B) is not supported\n"),
5467 bfd_get_target (input_bfd
), input_bfd
,
5468 bfd_get_target (link_info
.output_bfd
), link_info
.output_bfd
);
5469 /* einfo with %F exits. */
5472 if (compatible
== NULL
)
5474 if (command_line
.warn_mismatch
)
5475 einfo (_("%P%X: %s architecture of input file `%B'"
5476 " is incompatible with %s output\n"),
5477 bfd_printable_name (input_bfd
), input_bfd
,
5478 bfd_printable_name (link_info
.output_bfd
));
5480 else if (bfd_count_sections (input_bfd
))
5482 /* If the input bfd has no contents, it shouldn't set the
5483 private data of the output bfd. */
5485 bfd_error_handler_type pfn
= NULL
;
5487 /* If we aren't supposed to warn about mismatched input
5488 files, temporarily set the BFD error handler to a
5489 function which will do nothing. We still want to call
5490 bfd_merge_private_bfd_data, since it may set up
5491 information which is needed in the output file. */
5492 if (! command_line
.warn_mismatch
)
5493 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
5494 if (! bfd_merge_private_bfd_data (input_bfd
, link_info
.output_bfd
))
5496 if (command_line
.warn_mismatch
)
5497 einfo (_("%P%X: failed to merge target specific data"
5498 " of file %B\n"), input_bfd
);
5500 if (! command_line
.warn_mismatch
)
5501 bfd_set_error_handler (pfn
);
5506 /* Look through all the global common symbols and attach them to the
5507 correct section. The -sort-common command line switch may be used
5508 to roughly sort the entries by alignment. */
5513 if (command_line
.inhibit_common_definition
)
5515 if (link_info
.relocatable
5516 && ! command_line
.force_common_definition
)
5519 if (! config
.sort_common
)
5520 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
5525 if (config
.sort_common
== sort_descending
)
5527 for (power
= 4; power
> 0; power
--)
5528 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5531 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5535 for (power
= 0; power
<= 4; power
++)
5536 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5539 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5544 /* Place one common symbol in the correct section. */
5547 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
5549 unsigned int power_of_two
;
5553 if (h
->type
!= bfd_link_hash_common
)
5557 power_of_two
= h
->u
.c
.p
->alignment_power
;
5559 if (config
.sort_common
== sort_descending
5560 && power_of_two
< *(unsigned int *) info
)
5562 else if (config
.sort_common
== sort_ascending
5563 && power_of_two
> *(unsigned int *) info
)
5566 section
= h
->u
.c
.p
->section
;
5568 /* Increase the size of the section to align the common sym. */
5569 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
5570 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
5572 /* Adjust the alignment if necessary. */
5573 if (power_of_two
> section
->alignment_power
)
5574 section
->alignment_power
= power_of_two
;
5576 /* Change the symbol from common to defined. */
5577 h
->type
= bfd_link_hash_defined
;
5578 h
->u
.def
.section
= section
;
5579 h
->u
.def
.value
= section
->size
;
5581 /* Increase the size of the section. */
5582 section
->size
+= size
;
5584 /* Make sure the section is allocated in memory, and make sure that
5585 it is no longer a common section. */
5586 section
->flags
|= SEC_ALLOC
;
5587 section
->flags
&= ~SEC_IS_COMMON
;
5589 if (config
.map_file
!= NULL
)
5591 static bfd_boolean header_printed
;
5596 if (! header_printed
)
5598 minfo (_("\nAllocating common symbols\n"));
5599 minfo (_("Common symbol size file\n\n"));
5600 header_printed
= TRUE
;
5603 name
= bfd_demangle (link_info
.output_bfd
, h
->root
.string
,
5604 DMGL_ANSI
| DMGL_PARAMS
);
5607 minfo ("%s", h
->root
.string
);
5608 len
= strlen (h
->root
.string
);
5613 len
= strlen (name
);
5629 if (size
<= 0xffffffff)
5630 sprintf (buf
, "%lx", (unsigned long) size
);
5632 sprintf_vma (buf
, size
);
5642 minfo ("%B\n", section
->owner
);
5648 /* Run through the input files and ensure that every input section has
5649 somewhere to go. If one is found without a destination then create
5650 an input request and place it into the statement tree. */
5653 lang_place_orphans (void)
5655 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5659 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5661 if (s
->output_section
== NULL
)
5663 /* This section of the file is not attached, root
5664 around for a sensible place for it to go. */
5666 if (file
->just_syms_flag
)
5667 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5668 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5669 s
->output_section
= bfd_abs_section_ptr
;
5670 else if (strcmp (s
->name
, "COMMON") == 0)
5672 /* This is a lonely common section which must have
5673 come from an archive. We attach to the section
5674 with the wildcard. */
5675 if (! link_info
.relocatable
5676 || command_line
.force_common_definition
)
5678 if (default_common_section
== NULL
)
5679 default_common_section
5680 = lang_output_section_statement_lookup (".bss", 0,
5682 lang_add_section (&default_common_section
->children
, s
,
5683 default_common_section
);
5688 const char *name
= s
->name
;
5691 if (config
.unique_orphan_sections
|| unique_section_p (s
))
5692 constraint
= SPECIAL
;
5694 if (!ldemul_place_orphan (s
, name
, constraint
))
5696 lang_output_section_statement_type
*os
;
5697 os
= lang_output_section_statement_lookup (name
,
5700 lang_add_section (&os
->children
, s
, os
);
5709 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5711 flagword
*ptr_flags
;
5713 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5719 *ptr_flags
|= SEC_ALLOC
;
5723 *ptr_flags
|= SEC_READONLY
;
5727 *ptr_flags
|= SEC_DATA
;
5731 *ptr_flags
|= SEC_CODE
;
5736 *ptr_flags
|= SEC_LOAD
;
5740 einfo (_("%P%F: invalid syntax in flags\n"));
5747 /* Call a function on each input file. This function will be called
5748 on an archive, but not on the elements. */
5751 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5753 lang_input_statement_type
*f
;
5755 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5757 f
= (lang_input_statement_type
*) f
->next_real_file
)
5761 /* Call a function on each file. The function will be called on all
5762 the elements of an archive which are included in the link, but will
5763 not be called on the archive file itself. */
5766 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5768 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5775 ldlang_add_file (lang_input_statement_type
*entry
)
5777 lang_statement_append (&file_chain
,
5778 (lang_statement_union_type
*) entry
,
5781 /* The BFD linker needs to have a list of all input BFDs involved in
5783 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5784 ASSERT (entry
->the_bfd
!= link_info
.output_bfd
);
5786 *link_info
.input_bfds_tail
= entry
->the_bfd
;
5787 link_info
.input_bfds_tail
= &entry
->the_bfd
->link_next
;
5788 entry
->the_bfd
->usrdata
= entry
;
5789 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5791 /* Look through the sections and check for any which should not be
5792 included in the link. We need to do this now, so that we can
5793 notice when the backend linker tries to report multiple
5794 definition errors for symbols which are in sections we aren't
5795 going to link. FIXME: It might be better to entirely ignore
5796 symbols which are defined in sections which are going to be
5797 discarded. This would require modifying the backend linker for
5798 each backend which might set the SEC_LINK_ONCE flag. If we do
5799 this, we should probably handle SEC_EXCLUDE in the same way. */
5801 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5805 lang_add_output (const char *name
, int from_script
)
5807 /* Make -o on command line override OUTPUT in script. */
5808 if (!had_output_filename
|| !from_script
)
5810 output_filename
= name
;
5811 had_output_filename
= TRUE
;
5815 static lang_output_section_statement_type
*current_section
;
5826 for (l
= 0; l
< 32; l
++)
5828 if (i
>= (unsigned int) x
)
5836 lang_output_section_statement_type
*
5837 lang_enter_output_section_statement (const char *output_section_statement_name
,
5838 etree_type
*address_exp
,
5839 enum section_type sectype
,
5841 etree_type
*subalign
,
5845 lang_output_section_statement_type
*os
;
5847 os
= lang_output_section_statement_lookup (output_section_statement_name
,
5849 current_section
= os
;
5851 if (os
->addr_tree
== NULL
)
5853 os
->addr_tree
= address_exp
;
5855 os
->sectype
= sectype
;
5856 if (sectype
!= noload_section
)
5857 os
->flags
= SEC_NO_FLAGS
;
5859 os
->flags
= SEC_NEVER_LOAD
;
5860 os
->block_value
= 1;
5862 /* Make next things chain into subchain of this. */
5863 push_stat_ptr (&os
->children
);
5865 os
->subsection_alignment
=
5866 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5867 os
->section_alignment
=
5868 topower (exp_get_value_int (align
, -1, "section alignment"));
5870 os
->load_base
= ebase
;
5877 lang_output_statement_type
*new;
5879 new = new_stat (lang_output_statement
, stat_ptr
);
5880 new->name
= output_filename
;
5883 /* Reset the current counters in the regions. */
5886 lang_reset_memory_regions (void)
5888 lang_memory_region_type
*p
= lang_memory_region_list
;
5890 lang_output_section_statement_type
*os
;
5892 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5894 p
->current
= p
->origin
;
5898 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5902 os
->processed_vma
= FALSE
;
5903 os
->processed_lma
= FALSE
;
5906 for (o
= link_info
.output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5908 /* Save the last size for possible use by bfd_relax_section. */
5909 o
->rawsize
= o
->size
;
5914 /* Worker for lang_gc_sections_1. */
5917 gc_section_callback (lang_wild_statement_type
*ptr
,
5918 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5920 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5921 void *data ATTRIBUTE_UNUSED
)
5923 /* If the wild pattern was marked KEEP, the member sections
5924 should be as well. */
5925 if (ptr
->keep_sections
)
5926 section
->flags
|= SEC_KEEP
;
5929 /* Iterate over sections marking them against GC. */
5932 lang_gc_sections_1 (lang_statement_union_type
*s
)
5934 for (; s
!= NULL
; s
= s
->header
.next
)
5936 switch (s
->header
.type
)
5938 case lang_wild_statement_enum
:
5939 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5941 case lang_constructors_statement_enum
:
5942 lang_gc_sections_1 (constructor_list
.head
);
5944 case lang_output_section_statement_enum
:
5945 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5947 case lang_group_statement_enum
:
5948 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5957 lang_gc_sections (void)
5959 /* Keep all sections so marked in the link script. */
5961 lang_gc_sections_1 (statement_list
.head
);
5963 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5964 the special case of debug info. (See bfd/stabs.c)
5965 Twiddle the flag here, to simplify later linker code. */
5966 if (link_info
.relocatable
)
5968 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5971 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5972 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5973 sec
->flags
&= ~SEC_EXCLUDE
;
5977 if (link_info
.gc_sections
)
5978 bfd_gc_sections (link_info
.output_bfd
, &link_info
);
5981 /* Worker for lang_find_relro_sections_1. */
5984 find_relro_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
5985 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5987 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5990 /* Discarded, excluded and ignored sections effectively have zero
5992 if (section
->output_section
!= NULL
5993 && section
->output_section
->owner
== link_info
.output_bfd
5994 && (section
->output_section
->flags
& SEC_EXCLUDE
) == 0
5995 && !IGNORE_SECTION (section
)
5996 && section
->size
!= 0)
5998 bfd_boolean
*has_relro_section
= (bfd_boolean
*) data
;
5999 *has_relro_section
= TRUE
;
6003 /* Iterate over sections for relro sections. */
6006 lang_find_relro_sections_1 (lang_statement_union_type
*s
,
6007 bfd_boolean
*has_relro_section
)
6009 if (*has_relro_section
)
6012 for (; s
!= NULL
; s
= s
->header
.next
)
6014 if (s
== expld
.dataseg
.relro_end_stat
)
6017 switch (s
->header
.type
)
6019 case lang_wild_statement_enum
:
6020 walk_wild (&s
->wild_statement
,
6021 find_relro_section_callback
,
6024 case lang_constructors_statement_enum
:
6025 lang_find_relro_sections_1 (constructor_list
.head
,
6028 case lang_output_section_statement_enum
:
6029 lang_find_relro_sections_1 (s
->output_section_statement
.children
.head
,
6032 case lang_group_statement_enum
:
6033 lang_find_relro_sections_1 (s
->group_statement
.children
.head
,
6043 lang_find_relro_sections (void)
6045 bfd_boolean has_relro_section
= FALSE
;
6047 /* Check all sections in the link script. */
6049 lang_find_relro_sections_1 (expld
.dataseg
.relro_start_stat
,
6050 &has_relro_section
);
6052 if (!has_relro_section
)
6053 link_info
.relro
= FALSE
;
6056 /* Relax all sections until bfd_relax_section gives up. */
6059 relax_sections (void)
6061 /* Keep relaxing until bfd_relax_section gives up. */
6062 bfd_boolean relax_again
;
6064 link_info
.relax_trip
= -1;
6067 relax_again
= FALSE
;
6068 link_info
.relax_trip
++;
6070 /* Note: pe-dll.c does something like this also. If you find
6071 you need to change this code, you probably need to change
6072 pe-dll.c also. DJ */
6074 /* Do all the assignments with our current guesses as to
6076 lang_do_assignments ();
6078 /* We must do this after lang_do_assignments, because it uses
6080 lang_reset_memory_regions ();
6082 /* Perform another relax pass - this time we know where the
6083 globals are, so can make a better guess. */
6084 lang_size_sections (&relax_again
, FALSE
);
6086 while (relax_again
);
6092 /* Finalize dynamic list. */
6093 if (link_info
.dynamic_list
)
6094 lang_finalize_version_expr_head (&link_info
.dynamic_list
->head
);
6096 current_target
= default_target
;
6098 /* Open the output file. */
6099 lang_for_each_statement (ldlang_open_output
);
6102 ldemul_create_output_section_statements ();
6104 /* Add to the hash table all undefineds on the command line. */
6105 lang_place_undefineds ();
6107 if (!bfd_section_already_linked_table_init ())
6108 einfo (_("%P%F: Failed to create hash table\n"));
6110 /* Create a bfd for each input file. */
6111 current_target
= default_target
;
6112 open_input_bfds (statement_list
.head
, FALSE
);
6114 link_info
.gc_sym_list
= &entry_symbol
;
6115 if (entry_symbol
.name
== NULL
)
6116 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
6118 ldemul_after_open ();
6120 bfd_section_already_linked_table_free ();
6122 /* Make sure that we're not mixing architectures. We call this
6123 after all the input files have been opened, but before we do any
6124 other processing, so that any operations merge_private_bfd_data
6125 does on the output file will be known during the rest of the
6129 /* Handle .exports instead of a version script if we're told to do so. */
6130 if (command_line
.version_exports_section
)
6131 lang_do_version_exports_section ();
6133 /* Build all sets based on the information gathered from the input
6135 ldctor_build_sets ();
6137 /* Remove unreferenced sections if asked to. */
6138 lang_gc_sections ();
6140 /* Size up the common data. */
6143 /* Update wild statements. */
6144 update_wild_statements (statement_list
.head
);
6146 /* Run through the contours of the script and attach input sections
6147 to the correct output sections. */
6148 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
6150 process_insert_statements ();
6152 /* Find any sections not attached explicitly and handle them. */
6153 lang_place_orphans ();
6155 if (! link_info
.relocatable
)
6159 /* Merge SEC_MERGE sections. This has to be done after GC of
6160 sections, so that GCed sections are not merged, but before
6161 assigning dynamic symbols, since removing whole input sections
6163 bfd_merge_sections (link_info
.output_bfd
, &link_info
);
6165 /* Look for a text section and set the readonly attribute in it. */
6166 found
= bfd_get_section_by_name (link_info
.output_bfd
, ".text");
6170 if (config
.text_read_only
)
6171 found
->flags
|= SEC_READONLY
;
6173 found
->flags
&= ~SEC_READONLY
;
6177 /* Do anything special before sizing sections. This is where ELF
6178 and other back-ends size dynamic sections. */
6179 ldemul_before_allocation ();
6181 /* We must record the program headers before we try to fix the
6182 section positions, since they will affect SIZEOF_HEADERS. */
6183 lang_record_phdrs ();
6185 /* Check relro sections. */
6186 if (link_info
.relro
&& ! link_info
.relocatable
)
6187 lang_find_relro_sections ();
6189 /* Size up the sections. */
6190 lang_size_sections (NULL
, !command_line
.relax
);
6192 /* Now run around and relax if we can. */
6193 if (command_line
.relax
)
6195 /* We may need more than one relaxation pass. */
6196 int i
= link_info
.relax_pass
;
6198 /* The backend can use it to determine the current pass. */
6199 link_info
.relax_pass
= 0;
6204 link_info
.relax_pass
++;
6207 /* Final extra sizing to report errors. */
6208 lang_do_assignments ();
6209 lang_reset_memory_regions ();
6210 lang_size_sections (NULL
, TRUE
);
6213 /* See if anything special should be done now we know how big
6215 ldemul_after_allocation ();
6217 /* Fix any .startof. or .sizeof. symbols. */
6218 lang_set_startof ();
6220 /* Do all the assignments, now that we know the final resting places
6221 of all the symbols. */
6223 lang_do_assignments ();
6227 /* Make sure that the section addresses make sense. */
6228 if (! link_info
.relocatable
6229 && command_line
.check_section_addresses
)
6230 lang_check_section_addresses ();
6235 /* EXPORTED TO YACC */
6238 lang_add_wild (struct wildcard_spec
*filespec
,
6239 struct wildcard_list
*section_list
,
6240 bfd_boolean keep_sections
)
6242 struct wildcard_list
*curr
, *next
;
6243 lang_wild_statement_type
*new;
6245 /* Reverse the list as the parser puts it back to front. */
6246 for (curr
= section_list
, section_list
= NULL
;
6248 section_list
= curr
, curr
= next
)
6250 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
6251 placed_commons
= TRUE
;
6254 curr
->next
= section_list
;
6257 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
6259 if (strcmp (filespec
->name
, "*") == 0)
6260 filespec
->name
= NULL
;
6261 else if (! wildcardp (filespec
->name
))
6262 lang_has_input_file
= TRUE
;
6265 new = new_stat (lang_wild_statement
, stat_ptr
);
6266 new->filename
= NULL
;
6267 new->filenames_sorted
= FALSE
;
6268 if (filespec
!= NULL
)
6270 new->filename
= filespec
->name
;
6271 new->filenames_sorted
= filespec
->sorted
== by_name
;
6273 new->section_list
= section_list
;
6274 new->keep_sections
= keep_sections
;
6275 lang_list_init (&new->children
);
6276 analyze_walk_wild_section_handler (new);
6280 lang_section_start (const char *name
, etree_type
*address
,
6281 const segment_type
*segment
)
6283 lang_address_statement_type
*ad
;
6285 ad
= new_stat (lang_address_statement
, stat_ptr
);
6286 ad
->section_name
= name
;
6287 ad
->address
= address
;
6288 ad
->segment
= segment
;
6291 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
6292 because of a -e argument on the command line, or zero if this is
6293 called by ENTRY in a linker script. Command line arguments take
6297 lang_add_entry (const char *name
, bfd_boolean cmdline
)
6299 if (entry_symbol
.name
== NULL
6301 || ! entry_from_cmdline
)
6303 entry_symbol
.name
= name
;
6304 entry_from_cmdline
= cmdline
;
6308 /* Set the default start symbol to NAME. .em files should use this,
6309 not lang_add_entry, to override the use of "start" if neither the
6310 linker script nor the command line specifies an entry point. NAME
6311 must be permanently allocated. */
6313 lang_default_entry (const char *name
)
6315 entry_symbol_default
= name
;
6319 lang_add_target (const char *name
)
6321 lang_target_statement_type
*new;
6323 new = new_stat (lang_target_statement
, stat_ptr
);
6328 lang_add_map (const char *name
)
6335 map_option_f
= TRUE
;
6343 lang_add_fill (fill_type
*fill
)
6345 lang_fill_statement_type
*new;
6347 new = new_stat (lang_fill_statement
, stat_ptr
);
6352 lang_add_data (int type
, union etree_union
*exp
)
6354 lang_data_statement_type
*new;
6356 new = new_stat (lang_data_statement
, stat_ptr
);
6361 /* Create a new reloc statement. RELOC is the BFD relocation type to
6362 generate. HOWTO is the corresponding howto structure (we could
6363 look this up, but the caller has already done so). SECTION is the
6364 section to generate a reloc against, or NAME is the name of the
6365 symbol to generate a reloc against. Exactly one of SECTION and
6366 NAME must be NULL. ADDEND is an expression for the addend. */
6369 lang_add_reloc (bfd_reloc_code_real_type reloc
,
6370 reloc_howto_type
*howto
,
6373 union etree_union
*addend
)
6375 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
6379 p
->section
= section
;
6381 p
->addend_exp
= addend
;
6383 p
->addend_value
= 0;
6384 p
->output_section
= NULL
;
6385 p
->output_offset
= 0;
6388 lang_assignment_statement_type
*
6389 lang_add_assignment (etree_type
*exp
)
6391 lang_assignment_statement_type
*new;
6393 new = new_stat (lang_assignment_statement
, stat_ptr
);
6399 lang_add_attribute (enum statement_enum attribute
)
6401 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
6405 lang_startup (const char *name
)
6407 if (startup_file
!= NULL
)
6409 einfo (_("%P%F: multiple STARTUP files\n"));
6411 first_file
->filename
= name
;
6412 first_file
->local_sym_name
= name
;
6413 first_file
->real
= TRUE
;
6415 startup_file
= name
;
6419 lang_float (bfd_boolean maybe
)
6421 lang_float_flag
= maybe
;
6425 /* Work out the load- and run-time regions from a script statement, and
6426 store them in *LMA_REGION and *REGION respectively.
6428 MEMSPEC is the name of the run-time region, or the value of
6429 DEFAULT_MEMORY_REGION if the statement didn't specify one.
6430 LMA_MEMSPEC is the name of the load-time region, or null if the
6431 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
6432 had an explicit load address.
6434 It is an error to specify both a load region and a load address. */
6437 lang_get_regions (lang_memory_region_type
**region
,
6438 lang_memory_region_type
**lma_region
,
6439 const char *memspec
,
6440 const char *lma_memspec
,
6441 bfd_boolean have_lma
,
6442 bfd_boolean have_vma
)
6444 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
6446 /* If no runtime region or VMA has been specified, but the load region
6447 has been specified, then use the load region for the runtime region
6449 if (lma_memspec
!= NULL
6451 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
6452 *region
= *lma_region
;
6454 *region
= lang_memory_region_lookup (memspec
, FALSE
);
6456 if (have_lma
&& lma_memspec
!= 0)
6457 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
6461 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
6462 lang_output_section_phdr_list
*phdrs
,
6463 const char *lma_memspec
)
6465 lang_get_regions (¤t_section
->region
,
6466 ¤t_section
->lma_region
,
6467 memspec
, lma_memspec
,
6468 current_section
->load_base
!= NULL
,
6469 current_section
->addr_tree
!= NULL
);
6470 current_section
->fill
= fill
;
6471 current_section
->phdrs
= phdrs
;
6475 /* Create an absolute symbol with the given name with the value of the
6476 address of first byte of the section named.
6478 If the symbol already exists, then do nothing. */
6481 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
6483 struct bfd_link_hash_entry
*h
;
6485 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6487 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6489 if (h
->type
== bfd_link_hash_new
6490 || h
->type
== bfd_link_hash_undefined
)
6494 h
->type
= bfd_link_hash_defined
;
6496 sec
= bfd_get_section_by_name (link_info
.output_bfd
, secname
);
6500 h
->u
.def
.value
= bfd_get_section_vma (link_info
.output_bfd
, sec
);
6502 h
->u
.def
.section
= bfd_abs_section_ptr
;
6506 /* Create an absolute symbol with the given name with the value of the
6507 address of the first byte after the end of the section named.
6509 If the symbol already exists, then do nothing. */
6512 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
6514 struct bfd_link_hash_entry
*h
;
6516 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6518 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6520 if (h
->type
== bfd_link_hash_new
6521 || h
->type
== bfd_link_hash_undefined
)
6525 h
->type
= bfd_link_hash_defined
;
6527 sec
= bfd_get_section_by_name (link_info
.output_bfd
, secname
);
6531 h
->u
.def
.value
= (bfd_get_section_vma (link_info
.output_bfd
, sec
)
6532 + TO_ADDR (sec
->size
));
6534 h
->u
.def
.section
= bfd_abs_section_ptr
;
6539 lang_statement_append (lang_statement_list_type
*list
,
6540 lang_statement_union_type
*element
,
6541 lang_statement_union_type
**field
)
6543 *(list
->tail
) = element
;
6547 /* Set the output format type. -oformat overrides scripts. */
6550 lang_add_output_format (const char *format
,
6555 if (output_target
== NULL
|| !from_script
)
6557 if (command_line
.endian
== ENDIAN_BIG
6560 else if (command_line
.endian
== ENDIAN_LITTLE
6564 output_target
= format
;
6569 lang_add_insert (const char *where
, int is_before
)
6571 lang_insert_statement_type
*new;
6573 new = new_stat (lang_insert_statement
, stat_ptr
);
6575 new->is_before
= is_before
;
6576 saved_script_handle
= previous_script_handle
;
6579 /* Enter a group. This creates a new lang_group_statement, and sets
6580 stat_ptr to build new statements within the group. */
6583 lang_enter_group (void)
6585 lang_group_statement_type
*g
;
6587 g
= new_stat (lang_group_statement
, stat_ptr
);
6588 lang_list_init (&g
->children
);
6589 push_stat_ptr (&g
->children
);
6592 /* Leave a group. This just resets stat_ptr to start writing to the
6593 regular list of statements again. Note that this will not work if
6594 groups can occur inside anything else which can adjust stat_ptr,
6595 but currently they can't. */
6598 lang_leave_group (void)
6603 /* Add a new program header. This is called for each entry in a PHDRS
6604 command in a linker script. */
6607 lang_new_phdr (const char *name
,
6609 bfd_boolean filehdr
,
6614 struct lang_phdr
*n
, **pp
;
6616 n
= stat_alloc (sizeof (struct lang_phdr
));
6619 n
->type
= exp_get_value_int (type
, 0, "program header type");
6620 n
->filehdr
= filehdr
;
6625 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6630 /* Record the program header information in the output BFD. FIXME: We
6631 should not be calling an ELF specific function here. */
6634 lang_record_phdrs (void)
6638 lang_output_section_phdr_list
*last
;
6639 struct lang_phdr
*l
;
6640 lang_output_section_statement_type
*os
;
6643 secs
= xmalloc (alc
* sizeof (asection
*));
6646 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
6653 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6657 lang_output_section_phdr_list
*pl
;
6659 if (os
->constraint
< 0)
6667 if (os
->sectype
== noload_section
6668 || os
->bfd_section
== NULL
6669 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6674 lang_output_section_statement_type
* tmp_os
;
6676 /* If we have not run across a section with a program
6677 header assigned to it yet, then scan forwards to find
6678 one. This prevents inconsistencies in the linker's
6679 behaviour when a script has specified just a single
6680 header and there are sections in that script which are
6681 not assigned to it, and which occur before the first
6682 use of that header. See here for more details:
6683 http://sourceware.org/ml/binutils/2007-02/msg00291.html */
6684 for (tmp_os
= os
; tmp_os
; tmp_os
= tmp_os
->next
)
6687 last
= tmp_os
->phdrs
;
6691 einfo (_("%F%P: no sections assigned to phdrs\n"));
6696 if (os
->bfd_section
== NULL
)
6699 for (; pl
!= NULL
; pl
= pl
->next
)
6701 if (strcmp (pl
->name
, l
->name
) == 0)
6706 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6708 secs
[c
] = os
->bfd_section
;
6715 if (l
->flags
== NULL
)
6718 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6723 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6725 if (! bfd_record_phdr (link_info
.output_bfd
, l
->type
,
6726 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6727 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6728 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6733 /* Make sure all the phdr assignments succeeded. */
6734 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6738 lang_output_section_phdr_list
*pl
;
6740 if (os
->constraint
< 0
6741 || os
->bfd_section
== NULL
)
6744 for (pl
= os
->phdrs
;
6747 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6748 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6749 os
->name
, pl
->name
);
6753 /* Record a list of sections which may not be cross referenced. */
6756 lang_add_nocrossref (lang_nocrossref_type
*l
)
6758 struct lang_nocrossrefs
*n
;
6760 n
= xmalloc (sizeof *n
);
6761 n
->next
= nocrossref_list
;
6763 nocrossref_list
= n
;
6765 /* Set notice_all so that we get informed about all symbols. */
6766 link_info
.notice_all
= TRUE
;
6769 /* Overlay handling. We handle overlays with some static variables. */
6771 /* The overlay virtual address. */
6772 static etree_type
*overlay_vma
;
6773 /* And subsection alignment. */
6774 static etree_type
*overlay_subalign
;
6776 /* An expression for the maximum section size seen so far. */
6777 static etree_type
*overlay_max
;
6779 /* A list of all the sections in this overlay. */
6781 struct overlay_list
{
6782 struct overlay_list
*next
;
6783 lang_output_section_statement_type
*os
;
6786 static struct overlay_list
*overlay_list
;
6788 /* Start handling an overlay. */
6791 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6793 /* The grammar should prevent nested overlays from occurring. */
6794 ASSERT (overlay_vma
== NULL
6795 && overlay_subalign
== NULL
6796 && overlay_max
== NULL
);
6798 overlay_vma
= vma_expr
;
6799 overlay_subalign
= subalign
;
6802 /* Start a section in an overlay. We handle this by calling
6803 lang_enter_output_section_statement with the correct VMA.
6804 lang_leave_overlay sets up the LMA and memory regions. */
6807 lang_enter_overlay_section (const char *name
)
6809 struct overlay_list
*n
;
6812 lang_enter_output_section_statement (name
, overlay_vma
, overlay_section
,
6813 0, overlay_subalign
, 0, 0);
6815 /* If this is the first section, then base the VMA of future
6816 sections on this one. This will work correctly even if `.' is
6817 used in the addresses. */
6818 if (overlay_list
== NULL
)
6819 overlay_vma
= exp_nameop (ADDR
, name
);
6821 /* Remember the section. */
6822 n
= xmalloc (sizeof *n
);
6823 n
->os
= current_section
;
6824 n
->next
= overlay_list
;
6827 size
= exp_nameop (SIZEOF
, name
);
6829 /* Arrange to work out the maximum section end address. */
6830 if (overlay_max
== NULL
)
6833 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6836 /* Finish a section in an overlay. There isn't any special to do
6840 lang_leave_overlay_section (fill_type
*fill
,
6841 lang_output_section_phdr_list
*phdrs
)
6848 name
= current_section
->name
;
6850 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6851 region and that no load-time region has been specified. It doesn't
6852 really matter what we say here, since lang_leave_overlay will
6854 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6856 /* Define the magic symbols. */
6858 clean
= xmalloc (strlen (name
) + 1);
6860 for (s1
= name
; *s1
!= '\0'; s1
++)
6861 if (ISALNUM (*s1
) || *s1
== '_')
6865 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6866 sprintf (buf
, "__load_start_%s", clean
);
6867 lang_add_assignment (exp_provide (buf
,
6868 exp_nameop (LOADADDR
, name
),
6871 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6872 sprintf (buf
, "__load_stop_%s", clean
);
6873 lang_add_assignment (exp_provide (buf
,
6875 exp_nameop (LOADADDR
, name
),
6876 exp_nameop (SIZEOF
, name
)),
6882 /* Finish an overlay. If there are any overlay wide settings, this
6883 looks through all the sections in the overlay and sets them. */
6886 lang_leave_overlay (etree_type
*lma_expr
,
6889 const char *memspec
,
6890 lang_output_section_phdr_list
*phdrs
,
6891 const char *lma_memspec
)
6893 lang_memory_region_type
*region
;
6894 lang_memory_region_type
*lma_region
;
6895 struct overlay_list
*l
;
6896 lang_nocrossref_type
*nocrossref
;
6898 lang_get_regions (®ion
, &lma_region
,
6899 memspec
, lma_memspec
,
6900 lma_expr
!= NULL
, FALSE
);
6904 /* After setting the size of the last section, set '.' to end of the
6906 if (overlay_list
!= NULL
)
6907 overlay_list
->os
->update_dot_tree
6908 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6913 struct overlay_list
*next
;
6915 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6918 l
->os
->region
= region
;
6919 l
->os
->lma_region
= lma_region
;
6921 /* The first section has the load address specified in the
6922 OVERLAY statement. The rest are worked out from that.
6923 The base address is not needed (and should be null) if
6924 an LMA region was specified. */
6927 l
->os
->load_base
= lma_expr
;
6928 l
->os
->sectype
= normal_section
;
6930 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6931 l
->os
->phdrs
= phdrs
;
6935 lang_nocrossref_type
*nc
;
6937 nc
= xmalloc (sizeof *nc
);
6938 nc
->name
= l
->os
->name
;
6939 nc
->next
= nocrossref
;
6948 if (nocrossref
!= NULL
)
6949 lang_add_nocrossref (nocrossref
);
6952 overlay_list
= NULL
;
6956 /* Version handling. This is only useful for ELF. */
6958 /* This global variable holds the version tree that we build. */
6960 struct bfd_elf_version_tree
*lang_elf_version_info
;
6962 /* If PREV is NULL, return first version pattern matching particular symbol.
6963 If PREV is non-NULL, return first version pattern matching particular
6964 symbol after PREV (previously returned by lang_vers_match). */
6966 static struct bfd_elf_version_expr
*
6967 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6968 struct bfd_elf_version_expr
*prev
,
6971 const char *cxx_sym
= sym
;
6972 const char *java_sym
= sym
;
6973 struct bfd_elf_version_expr
*expr
= NULL
;
6975 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6977 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6981 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6983 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6988 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6990 struct bfd_elf_version_expr e
;
6992 switch (prev
? prev
->mask
: 0)
6995 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6998 expr
= htab_find (head
->htab
, &e
);
6999 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
7000 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
7006 case BFD_ELF_VERSION_C_TYPE
:
7007 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
7010 expr
= htab_find (head
->htab
, &e
);
7011 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
7012 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
7018 case BFD_ELF_VERSION_CXX_TYPE
:
7019 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
7021 e
.symbol
= java_sym
;
7022 expr
= htab_find (head
->htab
, &e
);
7023 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
7024 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
7035 /* Finally, try the wildcards. */
7036 if (prev
== NULL
|| prev
->symbol
)
7037 expr
= head
->remaining
;
7040 for (; expr
; expr
= expr
->next
)
7047 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
7050 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
7052 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
7056 if (fnmatch (expr
->pattern
, s
, 0) == 0)
7062 free ((char *) cxx_sym
);
7063 if (java_sym
!= sym
)
7064 free ((char *) java_sym
);
7068 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
7069 return a string pointing to the symbol name. */
7072 realsymbol (const char *pattern
)
7075 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
7076 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
7078 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
7080 /* It is a glob pattern only if there is no preceding
7082 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
7090 /* Remove the preceding backslash. */
7097 backslash
= *p
== '\\';
7112 /* This is called for each variable name or match expression. NEW is
7113 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
7114 pattern to be matched against symbol names. */
7116 struct bfd_elf_version_expr
*
7117 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
7120 bfd_boolean literal_p
)
7122 struct bfd_elf_version_expr
*ret
;
7124 ret
= xmalloc (sizeof *ret
);
7126 ret
->pattern
= literal_p
? NULL
: new;
7129 ret
->symbol
= literal_p
? new : realsymbol (new);
7131 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
7132 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7133 else if (strcasecmp (lang
, "C++") == 0)
7134 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
7135 else if (strcasecmp (lang
, "Java") == 0)
7136 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
7139 einfo (_("%X%P: unknown language `%s' in version information\n"),
7141 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
7144 return ldemul_new_vers_pattern (ret
);
7147 /* This is called for each set of variable names and match
7150 struct bfd_elf_version_tree
*
7151 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
7152 struct bfd_elf_version_expr
*locals
)
7154 struct bfd_elf_version_tree
*ret
;
7156 ret
= xcalloc (1, sizeof *ret
);
7157 ret
->globals
.list
= globals
;
7158 ret
->locals
.list
= locals
;
7159 ret
->match
= lang_vers_match
;
7160 ret
->name_indx
= (unsigned int) -1;
7164 /* This static variable keeps track of version indices. */
7166 static int version_index
;
7169 version_expr_head_hash (const void *p
)
7171 const struct bfd_elf_version_expr
*e
= p
;
7173 return htab_hash_string (e
->symbol
);
7177 version_expr_head_eq (const void *p1
, const void *p2
)
7179 const struct bfd_elf_version_expr
*e1
= p1
;
7180 const struct bfd_elf_version_expr
*e2
= p2
;
7182 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
7186 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
7189 struct bfd_elf_version_expr
*e
, *next
;
7190 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
7192 for (e
= head
->list
; e
; e
= e
->next
)
7196 head
->mask
|= e
->mask
;
7201 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
7202 version_expr_head_eq
, NULL
);
7203 list_loc
= &head
->list
;
7204 remaining_loc
= &head
->remaining
;
7205 for (e
= head
->list
; e
; e
= next
)
7211 remaining_loc
= &e
->next
;
7215 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
7219 struct bfd_elf_version_expr
*e1
, *last
;
7225 if (e1
->mask
== e
->mask
)
7233 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
7237 /* This is a duplicate. */
7238 /* FIXME: Memory leak. Sometimes pattern is not
7239 xmalloced alone, but in larger chunk of memory. */
7240 /* free (e->symbol); */
7245 e
->next
= last
->next
;
7253 list_loc
= &e
->next
;
7257 *remaining_loc
= NULL
;
7258 *list_loc
= head
->remaining
;
7261 head
->remaining
= head
->list
;
7264 /* This is called when we know the name and dependencies of the
7268 lang_register_vers_node (const char *name
,
7269 struct bfd_elf_version_tree
*version
,
7270 struct bfd_elf_version_deps
*deps
)
7272 struct bfd_elf_version_tree
*t
, **pp
;
7273 struct bfd_elf_version_expr
*e1
;
7278 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
7279 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
7281 einfo (_("%X%P: anonymous version tag cannot be combined"
7282 " with other version tags\n"));
7287 /* Make sure this node has a unique name. */
7288 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7289 if (strcmp (t
->name
, name
) == 0)
7290 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
7292 lang_finalize_version_expr_head (&version
->globals
);
7293 lang_finalize_version_expr_head (&version
->locals
);
7295 /* Check the global and local match names, and make sure there
7296 aren't any duplicates. */
7298 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
7300 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7302 struct bfd_elf_version_expr
*e2
;
7304 if (t
->locals
.htab
&& e1
->symbol
)
7306 e2
= htab_find (t
->locals
.htab
, e1
);
7307 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
7309 if (e1
->mask
== e2
->mask
)
7310 einfo (_("%X%P: duplicate expression `%s'"
7311 " in version information\n"), e1
->symbol
);
7315 else if (!e1
->symbol
)
7316 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7317 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7318 && e1
->mask
== e2
->mask
)
7319 einfo (_("%X%P: duplicate expression `%s'"
7320 " in version information\n"), e1
->pattern
);
7324 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
7326 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7328 struct bfd_elf_version_expr
*e2
;
7330 if (t
->globals
.htab
&& e1
->symbol
)
7332 e2
= htab_find (t
->globals
.htab
, e1
);
7333 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
7335 if (e1
->mask
== e2
->mask
)
7336 einfo (_("%X%P: duplicate expression `%s'"
7337 " in version information\n"),
7342 else if (!e1
->symbol
)
7343 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
7344 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
7345 && e1
->mask
== e2
->mask
)
7346 einfo (_("%X%P: duplicate expression `%s'"
7347 " in version information\n"), e1
->pattern
);
7351 version
->deps
= deps
;
7352 version
->name
= name
;
7353 if (name
[0] != '\0')
7356 version
->vernum
= version_index
;
7359 version
->vernum
= 0;
7361 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
7366 /* This is called when we see a version dependency. */
7368 struct bfd_elf_version_deps
*
7369 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
7371 struct bfd_elf_version_deps
*ret
;
7372 struct bfd_elf_version_tree
*t
;
7374 ret
= xmalloc (sizeof *ret
);
7377 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
7379 if (strcmp (t
->name
, name
) == 0)
7381 ret
->version_needed
= t
;
7386 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
7392 lang_do_version_exports_section (void)
7394 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
7396 LANG_FOR_EACH_INPUT_STATEMENT (is
)
7398 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
7406 contents
= xmalloc (len
);
7407 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
7408 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
7411 while (p
< contents
+ len
)
7413 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
7414 p
= strchr (p
, '\0') + 1;
7417 /* Do not free the contents, as we used them creating the regex. */
7419 /* Do not include this section in the link. */
7420 sec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
7423 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
7424 lang_register_vers_node (command_line
.version_exports_section
,
7425 lang_new_vers_node (greg
, lreg
), NULL
);
7429 lang_add_unique (const char *name
)
7431 struct unique_sections
*ent
;
7433 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
7434 if (strcmp (ent
->name
, name
) == 0)
7437 ent
= xmalloc (sizeof *ent
);
7438 ent
->name
= xstrdup (name
);
7439 ent
->next
= unique_section_list
;
7440 unique_section_list
= ent
;
7443 /* Append the list of dynamic symbols to the existing one. */
7446 lang_append_dynamic_list (struct bfd_elf_version_expr
*dynamic
)
7448 if (link_info
.dynamic_list
)
7450 struct bfd_elf_version_expr
*tail
;
7451 for (tail
= dynamic
; tail
->next
!= NULL
; tail
= tail
->next
)
7453 tail
->next
= link_info
.dynamic_list
->head
.list
;
7454 link_info
.dynamic_list
->head
.list
= dynamic
;
7458 struct bfd_elf_dynamic_list
*d
;
7460 d
= xcalloc (1, sizeof *d
);
7461 d
->head
.list
= dynamic
;
7462 d
->match
= lang_vers_match
;
7463 link_info
.dynamic_list
= d
;
7467 /* Append the list of C++ typeinfo dynamic symbols to the existing
7471 lang_append_dynamic_list_cpp_typeinfo (void)
7473 const char * symbols
[] =
7475 "typeinfo name for*",
7478 struct bfd_elf_version_expr
*dynamic
= NULL
;
7481 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7482 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7485 lang_append_dynamic_list (dynamic
);
7488 /* Append the list of C++ operator new and delete dynamic symbols to the
7492 lang_append_dynamic_list_cpp_new (void)
7494 const char * symbols
[] =
7499 struct bfd_elf_version_expr
*dynamic
= NULL
;
7502 for (i
= 0; i
< ARRAY_SIZE (symbols
); i
++)
7503 dynamic
= lang_new_vers_pattern (dynamic
, symbols
[i
], "C++",
7506 lang_append_dynamic_list (dynamic
);