1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD 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 2, or (at your option)
13 GLD 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 GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
48 /* Binary search tree structure to
49 efficiently sort sections by name. */
50 typedef struct lang_section_bst
53 struct lang_section_bst
*left
;
54 struct lang_section_bst
*right
;
55 } lang_section_bst_type
;
57 /* Locals variables. */
58 static struct obstack stat_obstack
;
59 static struct obstack map_obstack
;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
63 static const char *startup_file
;
64 static lang_statement_list_type input_file_chain
;
65 static bfd_boolean placed_commons
= FALSE
;
66 static bfd_boolean stripped_excluded_sections
= FALSE
;
67 static lang_output_section_statement_type
*default_common_section
;
68 static bfd_boolean map_option_f
;
69 static bfd_vma print_dot
;
70 static lang_input_statement_type
*first_file
;
71 static const char *current_target
;
72 static const char *output_target
;
73 static lang_statement_list_type statement_list
;
74 static struct bfd_hash_table lang_definedness_table
;
76 /* Forward declarations. */
77 static void exp_init_os (etree_type
*);
78 static void init_map_userdata (bfd
*, asection
*, void *);
79 static lang_input_statement_type
*lookup_name (const char *);
80 static bfd_boolean
load_symbols (lang_input_statement_type
*,
81 lang_statement_list_type
*);
82 static struct bfd_hash_entry
*lang_definedness_newfunc
83 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
84 static void insert_undefined (const char *);
85 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
86 static void print_statement (lang_statement_union_type
*,
87 lang_output_section_statement_type
*);
88 static void print_statement_list (lang_statement_union_type
*,
89 lang_output_section_statement_type
*);
90 static void print_statements (void);
91 static void print_input_section (asection
*);
92 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
93 static void lang_record_phdrs (void);
94 static void lang_do_version_exports_section (void);
96 /* Exported variables. */
97 lang_output_section_statement_type
*abs_output_section
;
98 lang_statement_list_type lang_output_section_statement
;
99 lang_statement_list_type
*stat_ptr
= &statement_list
;
100 lang_statement_list_type file_chain
= { NULL
, NULL
};
101 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
102 static const char *entry_symbol_default
= "start";
103 const char *entry_section
= ".text";
104 bfd_boolean entry_from_cmdline
;
105 bfd_boolean lang_has_input_file
= FALSE
;
106 bfd_boolean had_output_filename
= FALSE
;
107 bfd_boolean lang_float_flag
= FALSE
;
108 bfd_boolean delete_output_file_on_failure
= FALSE
;
109 struct lang_phdr
*lang_phdr_list
;
110 struct lang_nocrossrefs
*nocrossref_list
;
111 static struct unique_sections
*unique_section_list
;
112 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
114 /* Functions that traverse the linker script and might evaluate
115 DEFINED() need to increment this. */
116 int lang_statement_iteration
= 0;
118 etree_type
*base
; /* Relocation base - or null */
120 /* Return TRUE if the PATTERN argument is a wildcard pattern.
121 Although backslashes are treated specially if a pattern contains
122 wildcards, we do not consider the mere presence of a backslash to
123 be enough to cause the pattern to be treated as a wildcard.
124 That lets us handle DOS filenames more naturally. */
125 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
127 #define new_stat(x, y) \
128 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
130 #define outside_section_address(q) \
131 ((q)->output_offset + (q)->output_section->vma)
133 #define outside_symbol_address(q) \
134 ((q)->value + outside_section_address (q->section))
136 #define SECTION_NAME_MAP_LENGTH (16)
139 stat_alloc (size_t size
)
141 return obstack_alloc (&stat_obstack
, size
);
145 unique_section_p (const asection
*sec
)
147 struct unique_sections
*unam
;
150 if (link_info
.relocatable
151 && sec
->owner
!= NULL
152 && bfd_is_group_section (sec
->owner
, sec
))
156 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
157 if (wildcardp (unam
->name
)
158 ? fnmatch (unam
->name
, secnam
, 0) == 0
159 : strcmp (unam
->name
, secnam
) == 0)
167 /* Generic traversal routines for finding matching sections. */
169 /* Try processing a section against a wildcard. This just calls
170 the callback unless the filename exclusion list is present
171 and excludes the file. It's hardly ever present so this
172 function is very fast. */
175 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
176 lang_input_statement_type
*file
,
178 struct wildcard_list
*sec
,
182 bfd_boolean skip
= FALSE
;
183 struct name_list
*list_tmp
;
185 /* Don't process sections from files which were
187 for (list_tmp
= sec
->spec
.exclude_name_list
;
189 list_tmp
= list_tmp
->next
)
191 bfd_boolean is_wildcard
= wildcardp (list_tmp
->name
);
193 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
195 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
197 /* If this file is part of an archive, and the archive is
198 excluded, exclude this file. */
199 if (! skip
&& file
->the_bfd
!= NULL
200 && file
->the_bfd
->my_archive
!= NULL
201 && file
->the_bfd
->my_archive
->filename
!= NULL
)
204 skip
= fnmatch (list_tmp
->name
,
205 file
->the_bfd
->my_archive
->filename
,
208 skip
= strcmp (list_tmp
->name
,
209 file
->the_bfd
->my_archive
->filename
) == 0;
217 (*callback
) (ptr
, sec
, s
, file
, data
);
220 /* Lowest common denominator routine that can handle everything correctly,
224 walk_wild_section_general (lang_wild_statement_type
*ptr
,
225 lang_input_statement_type
*file
,
230 struct wildcard_list
*sec
;
232 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
234 sec
= ptr
->section_list
;
236 (*callback
) (ptr
, sec
, s
, file
, data
);
240 bfd_boolean skip
= FALSE
;
242 if (sec
->spec
.name
!= NULL
)
244 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
246 if (wildcardp (sec
->spec
.name
))
247 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
249 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
253 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
260 /* Routines to find a single section given its name. If there's more
261 than one section with that name, we report that. */
265 asection
*found_section
;
266 bfd_boolean multiple_sections_found
;
267 } section_iterator_callback_data
;
270 section_iterator_callback (bfd
*bfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
272 section_iterator_callback_data
*d
= data
;
274 if (d
->found_section
!= NULL
)
276 d
->multiple_sections_found
= TRUE
;
280 d
->found_section
= s
;
285 find_section (lang_input_statement_type
*file
,
286 struct wildcard_list
*sec
,
287 bfd_boolean
*multiple_sections_found
)
289 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
291 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
292 section_iterator_callback
, &cb_data
);
293 *multiple_sections_found
= cb_data
.multiple_sections_found
;
294 return cb_data
.found_section
;
297 /* Code for handling simple wildcards without going through fnmatch,
298 which can be expensive because of charset translations etc. */
300 /* A simple wild is a literal string followed by a single '*',
301 where the literal part is at least 4 characters long. */
304 is_simple_wild (const char *name
)
306 size_t len
= strcspn (name
, "*?[");
307 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
311 match_simple_wild (const char *pattern
, const char *name
)
313 /* The first four characters of the pattern are guaranteed valid
314 non-wildcard characters. So we can go faster. */
315 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
316 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
321 while (*pattern
!= '*')
322 if (*name
++ != *pattern
++)
328 /* Compare sections ASEC and BSEC according to SORT. */
331 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
340 case by_alignment_name
:
341 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
342 - bfd_section_alignment (asec
->owner
, asec
));
348 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
349 bfd_get_section_name (bsec
->owner
, bsec
));
352 case by_name_alignment
:
353 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
354 bfd_get_section_name (bsec
->owner
, bsec
));
360 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
361 - bfd_section_alignment (asec
->owner
, asec
));
368 /* Build a Binary Search Tree to sort sections, unlike insertion sort
369 used in wild_sort(). BST is considerably faster if the number of
370 of sections are large. */
372 static lang_section_bst_type
**
373 wild_sort_fast (lang_wild_statement_type
*wild
,
374 struct wildcard_list
*sec
,
375 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
378 lang_section_bst_type
**tree
;
380 tree
= (lang_section_bst_type
**) &wild
->handler_data
[1];
381 if (!wild
->filenames_sorted
382 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
384 /* Append at the right end of tree. */
386 tree
= &((*tree
)->right
);
392 /* Find the correct node to append this section. */
393 if (compare_section (sec
->spec
.sorted
, section
, (*tree
)->section
) < 0)
394 tree
= &((*tree
)->left
);
396 tree
= &((*tree
)->right
);
402 /* Use wild_sort_fast to build a BST to sort sections. */
405 output_section_callback_fast (lang_wild_statement_type
*ptr
,
406 struct wildcard_list
*sec
,
408 lang_input_statement_type
*file
,
409 void *output ATTRIBUTE_UNUSED
)
411 lang_section_bst_type
*node
;
412 lang_section_bst_type
**tree
;
414 if (unique_section_p (section
))
417 node
= xmalloc (sizeof (lang_section_bst_type
));
420 node
->section
= section
;
422 tree
= wild_sort_fast (ptr
, sec
, file
, section
);
427 /* Convert a sorted sections' BST back to list form. */
430 output_section_callback_tree_to_list (lang_wild_statement_type
*ptr
,
431 lang_section_bst_type
*tree
,
435 output_section_callback_tree_to_list (ptr
, tree
->left
, output
);
437 lang_add_section (&ptr
->children
, tree
->section
,
438 (lang_output_section_statement_type
*) output
);
441 output_section_callback_tree_to_list (ptr
, tree
->right
, output
);
446 /* Specialized, optimized routines for handling different kinds of
450 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
451 lang_input_statement_type
*file
,
455 /* We can just do a hash lookup for the section with the right name.
456 But if that lookup discovers more than one section with the name
457 (should be rare), we fall back to the general algorithm because
458 we would otherwise have to sort the sections to make sure they
459 get processed in the bfd's order. */
460 bfd_boolean multiple_sections_found
;
461 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
462 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
464 if (multiple_sections_found
)
465 walk_wild_section_general (ptr
, file
, callback
, data
);
467 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
471 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
472 lang_input_statement_type
*file
,
477 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
479 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
481 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
482 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
485 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
490 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
491 lang_input_statement_type
*file
,
496 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
497 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
498 bfd_boolean multiple_sections_found
;
499 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
501 if (multiple_sections_found
)
503 walk_wild_section_general (ptr
, file
, callback
, data
);
507 /* Note that if the section was not found, s0 is NULL and
508 we'll simply never succeed the s == s0 test below. */
509 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
511 /* Recall that in this code path, a section cannot satisfy more
512 than one spec, so if s == s0 then it cannot match
515 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
518 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
519 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
522 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
529 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
530 lang_input_statement_type
*file
,
535 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
536 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
537 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
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 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
550 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
553 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
554 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
557 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
560 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
562 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
570 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
571 lang_input_statement_type
*file
,
576 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
577 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
578 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
579 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
580 bfd_boolean multiple_sections_found
;
581 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
583 if (multiple_sections_found
)
585 walk_wild_section_general (ptr
, file
, callback
, data
);
589 s1
= find_section (file
, sec1
, &multiple_sections_found
);
590 if (multiple_sections_found
)
592 walk_wild_section_general (ptr
, file
, callback
, data
);
596 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
599 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
602 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
605 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
606 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
610 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
614 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
616 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
624 walk_wild_section (lang_wild_statement_type
*ptr
,
625 lang_input_statement_type
*file
,
629 if (file
->just_syms_flag
)
632 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
635 /* Returns TRUE when name1 is a wildcard spec that might match
636 something name2 can match. We're conservative: we return FALSE
637 only if the prefixes of name1 and name2 are different up to the
638 first wildcard character. */
641 wild_spec_can_overlap (const char *name1
, const char *name2
)
643 size_t prefix1_len
= strcspn (name1
, "?*[");
644 size_t prefix2_len
= strcspn (name2
, "?*[");
645 size_t min_prefix_len
;
647 /* Note that if there is no wildcard character, then we treat the
648 terminating 0 as part of the prefix. Thus ".text" won't match
649 ".text." or ".text.*", for example. */
650 if (name1
[prefix1_len
] == '\0')
652 if (name2
[prefix2_len
] == '\0')
655 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
657 return memcmp (name1
, name2
, min_prefix_len
) == 0;
660 /* Select specialized code to handle various kinds of wildcard
664 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
667 int wild_name_count
= 0;
668 struct wildcard_list
*sec
;
672 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
673 ptr
->handler_data
[0] = NULL
;
674 ptr
->handler_data
[1] = NULL
;
675 ptr
->handler_data
[2] = NULL
;
676 ptr
->handler_data
[3] = NULL
;
678 /* Count how many wildcard_specs there are, and how many of those
679 actually use wildcards in the name. Also, bail out if any of the
680 wildcard names are NULL. (Can this actually happen?
681 walk_wild_section used to test for it.) And bail out if any
682 of the wildcards are more complex than a simple string
683 ending in a single '*'. */
684 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
687 if (sec
->spec
.name
== NULL
)
689 if (wildcardp (sec
->spec
.name
))
692 if (!is_simple_wild (sec
->spec
.name
))
697 /* The zero-spec case would be easy to optimize but it doesn't
698 happen in practice. Likewise, more than 4 specs doesn't
699 happen in practice. */
700 if (sec_count
== 0 || sec_count
> 4)
703 /* Check that no two specs can match the same section. */
704 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
706 struct wildcard_list
*sec2
;
707 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
709 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
714 signature
= (sec_count
<< 8) + wild_name_count
;
718 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
721 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
724 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
727 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
730 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
736 /* Now fill the data array with pointers to the specs, first the
737 specs with non-wildcard names, then the specs with wildcard
738 names. It's OK to process the specs in different order from the
739 given order, because we've already determined that no section
740 will match more than one spec. */
742 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
743 if (!wildcardp (sec
->spec
.name
))
744 ptr
->handler_data
[data_counter
++] = sec
;
745 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
746 if (wildcardp (sec
->spec
.name
))
747 ptr
->handler_data
[data_counter
++] = sec
;
750 /* Handle a wild statement for a single file F. */
753 walk_wild_file (lang_wild_statement_type
*s
,
754 lang_input_statement_type
*f
,
758 if (f
->the_bfd
== NULL
759 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
760 walk_wild_section (s
, f
, callback
, data
);
765 /* This is an archive file. We must map each member of the
766 archive separately. */
767 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
768 while (member
!= NULL
)
770 /* When lookup_name is called, it will call the add_symbols
771 entry point for the archive. For each element of the
772 archive which is included, BFD will call ldlang_add_file,
773 which will set the usrdata field of the member to the
774 lang_input_statement. */
775 if (member
->usrdata
!= NULL
)
777 walk_wild_section (s
, member
->usrdata
, callback
, data
);
780 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
786 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
788 const char *file_spec
= s
->filename
;
790 if (file_spec
== NULL
)
792 /* Perform the iteration over all files in the list. */
793 LANG_FOR_EACH_INPUT_STATEMENT (f
)
795 walk_wild_file (s
, f
, callback
, data
);
798 else if (wildcardp (file_spec
))
800 LANG_FOR_EACH_INPUT_STATEMENT (f
)
802 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
803 walk_wild_file (s
, f
, callback
, data
);
808 lang_input_statement_type
*f
;
810 /* Perform the iteration over a single file. */
811 f
= lookup_name (file_spec
);
813 walk_wild_file (s
, f
, callback
, data
);
817 /* lang_for_each_statement walks the parse tree and calls the provided
818 function for each node. */
821 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
822 lang_statement_union_type
*s
)
824 for (; s
!= NULL
; s
= s
->header
.next
)
828 switch (s
->header
.type
)
830 case lang_constructors_statement_enum
:
831 lang_for_each_statement_worker (func
, constructor_list
.head
);
833 case lang_output_section_statement_enum
:
834 lang_for_each_statement_worker
835 (func
, s
->output_section_statement
.children
.head
);
837 case lang_wild_statement_enum
:
838 lang_for_each_statement_worker (func
,
839 s
->wild_statement
.children
.head
);
841 case lang_group_statement_enum
:
842 lang_for_each_statement_worker (func
,
843 s
->group_statement
.children
.head
);
845 case lang_data_statement_enum
:
846 case lang_reloc_statement_enum
:
847 case lang_object_symbols_statement_enum
:
848 case lang_output_statement_enum
:
849 case lang_target_statement_enum
:
850 case lang_input_section_enum
:
851 case lang_input_statement_enum
:
852 case lang_assignment_statement_enum
:
853 case lang_padding_statement_enum
:
854 case lang_address_statement_enum
:
855 case lang_fill_statement_enum
:
865 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
867 lang_for_each_statement_worker (func
, statement_list
.head
);
870 /*----------------------------------------------------------------------*/
873 lang_list_init (lang_statement_list_type
*list
)
876 list
->tail
= &list
->head
;
879 /* Build a new statement node for the parse tree. */
881 static lang_statement_union_type
*
882 new_statement (enum statement_enum type
,
884 lang_statement_list_type
*list
)
886 lang_statement_union_type
*new;
888 new = stat_alloc (size
);
889 new->header
.type
= type
;
890 new->header
.next
= NULL
;
891 lang_statement_append (list
, new, &new->header
.next
);
895 /* Build a new input file node for the language. There are several
896 ways in which we treat an input file, eg, we only look at symbols,
897 or prefix it with a -l etc.
899 We can be supplied with requests for input files more than once;
900 they may, for example be split over several lines like foo.o(.text)
901 foo.o(.data) etc, so when asked for a file we check that we haven't
902 got it already so we don't duplicate the bfd. */
904 static lang_input_statement_type
*
905 new_afile (const char *name
,
906 lang_input_file_enum_type file_type
,
908 bfd_boolean add_to_list
)
910 lang_input_statement_type
*p
;
913 p
= new_stat (lang_input_statement
, stat_ptr
);
916 p
= stat_alloc (sizeof (lang_input_statement_type
));
917 p
->header
.type
= lang_input_statement_enum
;
918 p
->header
.next
= NULL
;
921 lang_has_input_file
= TRUE
;
923 p
->sysrooted
= FALSE
;
926 case lang_input_file_is_symbols_only_enum
:
928 p
->is_archive
= FALSE
;
930 p
->local_sym_name
= name
;
931 p
->just_syms_flag
= TRUE
;
932 p
->search_dirs_flag
= FALSE
;
934 case lang_input_file_is_fake_enum
:
936 p
->is_archive
= FALSE
;
938 p
->local_sym_name
= name
;
939 p
->just_syms_flag
= FALSE
;
940 p
->search_dirs_flag
= FALSE
;
942 case lang_input_file_is_l_enum
:
943 p
->is_archive
= TRUE
;
946 p
->local_sym_name
= concat ("-l", name
, NULL
);
947 p
->just_syms_flag
= FALSE
;
948 p
->search_dirs_flag
= TRUE
;
950 case lang_input_file_is_marker_enum
:
952 p
->is_archive
= FALSE
;
954 p
->local_sym_name
= name
;
955 p
->just_syms_flag
= FALSE
;
956 p
->search_dirs_flag
= TRUE
;
958 case lang_input_file_is_search_file_enum
:
959 p
->sysrooted
= ldlang_sysrooted_script
;
961 p
->is_archive
= FALSE
;
963 p
->local_sym_name
= name
;
964 p
->just_syms_flag
= FALSE
;
965 p
->search_dirs_flag
= TRUE
;
967 case lang_input_file_is_file_enum
:
969 p
->is_archive
= FALSE
;
971 p
->local_sym_name
= name
;
972 p
->just_syms_flag
= FALSE
;
973 p
->search_dirs_flag
= FALSE
;
980 p
->next_real_file
= NULL
;
983 p
->dynamic
= config
.dynamic_link
;
984 p
->add_needed
= add_needed
;
985 p
->as_needed
= as_needed
;
986 p
->whole_archive
= whole_archive
;
988 lang_statement_append (&input_file_chain
,
989 (lang_statement_union_type
*) p
,
994 lang_input_statement_type
*
995 lang_add_input_file (const char *name
,
996 lang_input_file_enum_type file_type
,
999 lang_has_input_file
= TRUE
;
1000 return new_afile (name
, file_type
, target
, TRUE
);
1003 struct out_section_hash_entry
1005 struct bfd_hash_entry root
;
1006 lang_statement_union_type s
;
1009 /* The hash table. */
1011 static struct bfd_hash_table output_section_statement_table
;
1013 /* Support routines for the hash table used by lang_output_section_find,
1014 initialize the table, fill in an entry and remove the table. */
1016 static struct bfd_hash_entry
*
1017 output_section_statement_newfunc (struct bfd_hash_entry
*entry
,
1018 struct bfd_hash_table
*table
,
1021 lang_output_section_statement_type
**nextp
;
1022 struct out_section_hash_entry
*ret
;
1026 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
1031 entry
= bfd_hash_newfunc (entry
, table
, string
);
1035 ret
= (struct out_section_hash_entry
*) entry
;
1036 memset (&ret
->s
, 0, sizeof (ret
->s
));
1037 ret
->s
.header
.type
= lang_output_section_statement_enum
;
1038 ret
->s
.output_section_statement
.subsection_alignment
= -1;
1039 ret
->s
.output_section_statement
.section_alignment
= -1;
1040 ret
->s
.output_section_statement
.block_value
= 1;
1041 lang_list_init (&ret
->s
.output_section_statement
.children
);
1042 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
1044 /* For every output section statement added to the list, except the
1045 first one, lang_output_section_statement.tail points to the "next"
1046 field of the last element of the list. */
1047 if (lang_output_section_statement
.head
!= NULL
)
1048 ret
->s
.output_section_statement
.prev
1049 = ((lang_output_section_statement_type
*)
1050 ((char *) lang_output_section_statement
.tail
1051 - offsetof (lang_output_section_statement_type
, next
)));
1053 /* GCC's strict aliasing rules prevent us from just casting the
1054 address, so we store the pointer in a variable and cast that
1056 nextp
= &ret
->s
.output_section_statement
.next
;
1057 lang_statement_append (&lang_output_section_statement
,
1059 (lang_statement_union_type
**) nextp
);
1064 output_section_statement_table_init (void)
1066 if (!bfd_hash_table_init_n (&output_section_statement_table
,
1067 output_section_statement_newfunc
,
1068 sizeof (struct out_section_hash_entry
),
1070 einfo (_("%P%F: can not create hash table: %E\n"));
1074 output_section_statement_table_free (void)
1076 bfd_hash_table_free (&output_section_statement_table
);
1079 /* Build enough state so that the parser can build its tree. */
1084 obstack_begin (&stat_obstack
, 1000);
1086 stat_ptr
= &statement_list
;
1088 output_section_statement_table_init ();
1090 lang_list_init (stat_ptr
);
1092 lang_list_init (&input_file_chain
);
1093 lang_list_init (&lang_output_section_statement
);
1094 lang_list_init (&file_chain
);
1095 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
1097 abs_output_section
=
1098 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
1100 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
1102 /* The value "3" is ad-hoc, somewhat related to the expected number of
1103 DEFINED expressions in a linker script. For most default linker
1104 scripts, there are none. Why a hash table then? Well, it's somewhat
1105 simpler to re-use working machinery than using a linked list in terms
1106 of code-complexity here in ld, besides the initialization which just
1107 looks like other code here. */
1108 if (!bfd_hash_table_init_n (&lang_definedness_table
,
1109 lang_definedness_newfunc
,
1110 sizeof (struct lang_definedness_hash_entry
),
1112 einfo (_("%P%F: can not create hash table: %E\n"));
1118 output_section_statement_table_free ();
1121 /*----------------------------------------------------------------------
1122 A region is an area of memory declared with the
1123 MEMORY { name:org=exp, len=exp ... }
1126 We maintain a list of all the regions here.
1128 If no regions are specified in the script, then the default is used
1129 which is created when looked up to be the entire data space.
1131 If create is true we are creating a region inside a MEMORY block.
1132 In this case it is probably an error to create a region that has
1133 already been created. If we are not inside a MEMORY block it is
1134 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1135 and so we issue a warning. */
1137 static lang_memory_region_type
*lang_memory_region_list
;
1138 static lang_memory_region_type
**lang_memory_region_list_tail
1139 = &lang_memory_region_list
;
1141 lang_memory_region_type
*
1142 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1144 lang_memory_region_type
*p
;
1145 lang_memory_region_type
*new;
1147 /* NAME is NULL for LMA memspecs if no region was specified. */
1151 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1152 if (strcmp (p
->name
, name
) == 0)
1155 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1160 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1161 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
1163 new = stat_alloc (sizeof (lang_memory_region_type
));
1165 new->name
= xstrdup (name
);
1168 new->length
= ~(bfd_size_type
) 0;
1170 new->last_os
= NULL
;
1173 new->had_full_message
= FALSE
;
1175 *lang_memory_region_list_tail
= new;
1176 lang_memory_region_list_tail
= &new->next
;
1181 static lang_memory_region_type
*
1182 lang_memory_default (asection
*section
)
1184 lang_memory_region_type
*p
;
1186 flagword sec_flags
= section
->flags
;
1188 /* Override SEC_DATA to mean a writable section. */
1189 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1190 sec_flags
|= SEC_DATA
;
1192 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1194 if ((p
->flags
& sec_flags
) != 0
1195 && (p
->not_flags
& sec_flags
) == 0)
1200 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1203 lang_output_section_statement_type
*
1204 lang_output_section_find (const char *const name
)
1206 struct out_section_hash_entry
*entry
;
1209 entry
= ((struct out_section_hash_entry
*)
1210 bfd_hash_lookup (&output_section_statement_table
, name
,
1215 hash
= entry
->root
.hash
;
1218 if (entry
->s
.output_section_statement
.constraint
!= -1)
1219 return &entry
->s
.output_section_statement
;
1220 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1222 while (entry
!= NULL
1223 && entry
->root
.hash
== hash
1224 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1229 static lang_output_section_statement_type
*
1230 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
1232 struct out_section_hash_entry
*entry
;
1233 struct out_section_hash_entry
*last_ent
;
1236 entry
= ((struct out_section_hash_entry
*)
1237 bfd_hash_lookup (&output_section_statement_table
, name
,
1241 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1245 if (entry
->s
.output_section_statement
.name
!= NULL
)
1247 /* We have a section of this name, but it might not have the correct
1249 hash
= entry
->root
.hash
;
1252 if (entry
->s
.output_section_statement
.constraint
!= -1
1254 || (constraint
== entry
->s
.output_section_statement
.constraint
1255 && constraint
!= SPECIAL
)))
1256 return &entry
->s
.output_section_statement
;
1258 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1260 while (entry
!= NULL
1261 && entry
->root
.hash
== hash
1262 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1265 = ((struct out_section_hash_entry
*)
1266 output_section_statement_newfunc (NULL
,
1267 &output_section_statement_table
,
1271 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1274 entry
->root
= last_ent
->root
;
1275 last_ent
->root
.next
= &entry
->root
;
1278 entry
->s
.output_section_statement
.name
= name
;
1279 entry
->s
.output_section_statement
.constraint
= constraint
;
1280 return &entry
->s
.output_section_statement
;
1283 lang_output_section_statement_type
*
1284 lang_output_section_statement_lookup (const char *const name
)
1286 return lang_output_section_statement_lookup_1 (name
, 0);
1289 /* A variant of lang_output_section_find used by place_orphan.
1290 Returns the output statement that should precede a new output
1291 statement for SEC. If an exact match is found on certain flags,
1294 lang_output_section_statement_type
*
1295 lang_output_section_find_by_flags (const asection
*sec
,
1296 lang_output_section_statement_type
**exact
,
1297 lang_match_sec_type_func match_type
)
1299 lang_output_section_statement_type
*first
, *look
, *found
;
1302 /* We know the first statement on this list is *ABS*. May as well
1304 first
= &lang_output_section_statement
.head
->output_section_statement
;
1305 first
= first
->next
;
1307 /* First try for an exact match. */
1309 for (look
= first
; look
; look
= look
->next
)
1311 flags
= look
->flags
;
1312 if (look
->bfd_section
!= NULL
)
1314 flags
= look
->bfd_section
->flags
;
1315 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1319 flags
^= sec
->flags
;
1320 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1321 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1331 if (sec
->flags
& SEC_CODE
)
1333 /* Try for a rw code section. */
1334 for (look
= first
; look
; look
= look
->next
)
1336 flags
= look
->flags
;
1337 if (look
->bfd_section
!= NULL
)
1339 flags
= look
->bfd_section
->flags
;
1340 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1344 flags
^= sec
->flags
;
1345 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1346 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1350 else if (sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
))
1352 /* .rodata can go after .text, .sdata2 after .rodata. */
1353 for (look
= first
; look
; look
= look
->next
)
1355 flags
= look
->flags
;
1356 if (look
->bfd_section
!= NULL
)
1358 flags
= look
->bfd_section
->flags
;
1359 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1363 flags
^= sec
->flags
;
1364 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1366 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1370 else if (sec
->flags
& SEC_SMALL_DATA
)
1372 /* .sdata goes after .data, .sbss after .sdata. */
1373 for (look
= first
; look
; look
= look
->next
)
1375 flags
= look
->flags
;
1376 if (look
->bfd_section
!= NULL
)
1378 flags
= look
->bfd_section
->flags
;
1379 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1383 flags
^= sec
->flags
;
1384 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1385 | SEC_THREAD_LOCAL
))
1386 || ((look
->flags
& SEC_SMALL_DATA
)
1387 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1391 else if (sec
->flags
& SEC_HAS_CONTENTS
)
1393 /* .data goes after .rodata. */
1394 for (look
= first
; look
; look
= look
->next
)
1396 flags
= look
->flags
;
1397 if (look
->bfd_section
!= NULL
)
1399 flags
= look
->bfd_section
->flags
;
1400 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1404 flags
^= sec
->flags
;
1405 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1406 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1412 /* .bss goes last. */
1413 for (look
= first
; look
; look
= look
->next
)
1415 flags
= look
->flags
;
1416 if (look
->bfd_section
!= NULL
)
1418 flags
= look
->bfd_section
->flags
;
1419 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1423 flags
^= sec
->flags
;
1424 if (!(flags
& SEC_ALLOC
))
1429 if (found
|| !match_type
)
1432 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1435 /* Find the last output section before given output statement.
1436 Used by place_orphan. */
1439 output_prev_sec_find (lang_output_section_statement_type
*os
)
1441 lang_output_section_statement_type
*lookup
;
1443 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1445 if (lookup
->constraint
== -1)
1448 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1449 return lookup
->bfd_section
;
1455 lang_output_section_statement_type
*
1456 lang_insert_orphan (asection
*s
,
1457 const char *secname
,
1458 lang_output_section_statement_type
*after
,
1459 struct orphan_save
*place
,
1460 etree_type
*address
,
1461 lang_statement_list_type
*add_child
)
1463 lang_statement_list_type
*old
;
1464 lang_statement_list_type add
;
1466 lang_output_section_statement_type
*os
;
1467 lang_output_section_statement_type
**os_tail
;
1469 /* Start building a list of statements for this section.
1470 First save the current statement pointer. */
1473 /* If we have found an appropriate place for the output section
1474 statements for this orphan, add them to our own private list,
1475 inserting them later into the global statement list. */
1479 lang_list_init (stat_ptr
);
1483 if (config
.build_constructors
)
1485 /* If the name of the section is representable in C, then create
1486 symbols to mark the start and the end of the section. */
1487 for (ps
= secname
; *ps
!= '\0'; ps
++)
1488 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1493 etree_type
*e_align
;
1495 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1496 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1497 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1498 e_align
= exp_unop (ALIGN_K
,
1499 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1500 lang_add_assignment (exp_assop ('=', ".", e_align
));
1501 lang_add_assignment (exp_assop ('=', symname
,
1502 exp_nameop (NAME
, ".")));
1506 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1507 address
= exp_intop (0);
1509 os_tail
= ((lang_output_section_statement_type
**)
1510 lang_output_section_statement
.tail
);
1511 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1514 if (add_child
== NULL
)
1515 add_child
= &os
->children
;
1516 lang_add_section (add_child
, s
, os
);
1518 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1520 if (config
.build_constructors
&& *ps
== '\0')
1524 /* lang_leave_ouput_section_statement resets stat_ptr.
1525 Put stat_ptr back where we want it. */
1529 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1530 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1531 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1532 lang_add_assignment (exp_assop ('=', symname
,
1533 exp_nameop (NAME
, ".")));
1536 /* Restore the global list pointer. */
1540 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1542 asection
*snew
, *as
;
1544 snew
= os
->bfd_section
;
1546 /* Shuffle the bfd section list to make the output file look
1547 neater. This is really only cosmetic. */
1548 if (place
->section
== NULL
1549 && after
!= (&lang_output_section_statement
.head
1550 ->output_section_statement
))
1552 asection
*bfd_section
= after
->bfd_section
;
1554 /* If the output statement hasn't been used to place any input
1555 sections (and thus doesn't have an output bfd_section),
1556 look for the closest prior output statement having an
1558 if (bfd_section
== NULL
)
1559 bfd_section
= output_prev_sec_find (after
);
1561 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1562 place
->section
= &bfd_section
->next
;
1565 if (place
->section
== NULL
)
1566 place
->section
= &output_bfd
->sections
;
1568 as
= *place
->section
;
1572 /* Put the section at the end of the list. */
1574 /* Unlink the section. */
1575 bfd_section_list_remove (output_bfd
, snew
);
1577 /* Now tack it back on in the right place. */
1578 bfd_section_list_append (output_bfd
, snew
);
1580 else if (as
!= snew
&& as
->prev
!= snew
)
1582 /* Unlink the section. */
1583 bfd_section_list_remove (output_bfd
, snew
);
1585 /* Now tack it back on in the right place. */
1586 bfd_section_list_insert_before (output_bfd
, as
, snew
);
1589 /* Save the end of this list. Further ophans of this type will
1590 follow the one we've just added. */
1591 place
->section
= &snew
->next
;
1593 /* The following is non-cosmetic. We try to put the output
1594 statements in some sort of reasonable order here, because they
1595 determine the final load addresses of the orphan sections.
1596 In addition, placing output statements in the wrong order may
1597 require extra segments. For instance, given a typical
1598 situation of all read-only sections placed in one segment and
1599 following that a segment containing all the read-write
1600 sections, we wouldn't want to place an orphan read/write
1601 section before or amongst the read-only ones. */
1602 if (add
.head
!= NULL
)
1604 lang_output_section_statement_type
*newly_added_os
;
1606 if (place
->stmt
== NULL
)
1608 lang_statement_union_type
**where
;
1609 lang_statement_union_type
**assign
= NULL
;
1610 bfd_boolean ignore_first
;
1612 /* Look for a suitable place for the new statement list.
1613 The idea is to skip over anything that might be inside
1614 a SECTIONS {} statement in a script, before we find
1615 another output_section_statement. Assignments to "dot"
1616 before an output section statement are assumed to
1617 belong to it. An exception to this rule is made for
1618 the first assignment to dot, otherwise we might put an
1619 orphan before . = . + SIZEOF_HEADERS or similar
1620 assignments that set the initial address. */
1622 ignore_first
= after
== (&lang_output_section_statement
.head
1623 ->output_section_statement
);
1624 for (where
= &after
->header
.next
;
1626 where
= &(*where
)->header
.next
)
1628 switch ((*where
)->header
.type
)
1630 case lang_assignment_statement_enum
:
1633 lang_assignment_statement_type
*ass
;
1634 ass
= &(*where
)->assignment_statement
;
1635 if (ass
->exp
->type
.node_class
!= etree_assert
1636 && ass
->exp
->assign
.dst
[0] == '.'
1637 && ass
->exp
->assign
.dst
[1] == 0
1641 ignore_first
= FALSE
;
1643 case lang_wild_statement_enum
:
1644 case lang_input_section_enum
:
1645 case lang_object_symbols_statement_enum
:
1646 case lang_fill_statement_enum
:
1647 case lang_data_statement_enum
:
1648 case lang_reloc_statement_enum
:
1649 case lang_padding_statement_enum
:
1650 case lang_constructors_statement_enum
:
1653 case lang_output_section_statement_enum
:
1656 case lang_input_statement_enum
:
1657 case lang_address_statement_enum
:
1658 case lang_target_statement_enum
:
1659 case lang_output_statement_enum
:
1660 case lang_group_statement_enum
:
1661 case lang_afile_asection_pair_statement_enum
:
1670 place
->os_tail
= &after
->next
;
1674 /* Put it after the last orphan statement we added. */
1675 *add
.tail
= *place
->stmt
;
1676 *place
->stmt
= add
.head
;
1679 /* Fix the global list pointer if we happened to tack our
1680 new list at the tail. */
1681 if (*old
->tail
== add
.head
)
1682 old
->tail
= add
.tail
;
1684 /* Save the end of this list. */
1685 place
->stmt
= add
.tail
;
1687 /* Do the same for the list of output section statements. */
1688 newly_added_os
= *os_tail
;
1690 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1691 ((char *) place
->os_tail
1692 - offsetof (lang_output_section_statement_type
, next
));
1693 newly_added_os
->next
= *place
->os_tail
;
1694 if (newly_added_os
->next
!= NULL
)
1695 newly_added_os
->next
->prev
= newly_added_os
;
1696 *place
->os_tail
= newly_added_os
;
1697 place
->os_tail
= &newly_added_os
->next
;
1699 /* Fixing the global list pointer here is a little different.
1700 We added to the list in lang_enter_output_section_statement,
1701 trimmed off the new output_section_statment above when
1702 assigning *os_tail = NULL, but possibly added it back in
1703 the same place when assigning *place->os_tail. */
1704 if (*os_tail
== NULL
)
1705 lang_output_section_statement
.tail
1706 = (lang_statement_union_type
**) os_tail
;
1713 lang_map_flags (flagword flag
)
1715 if (flag
& SEC_ALLOC
)
1718 if (flag
& SEC_CODE
)
1721 if (flag
& SEC_READONLY
)
1724 if (flag
& SEC_DATA
)
1727 if (flag
& SEC_LOAD
)
1734 lang_memory_region_type
*m
;
1735 bfd_boolean dis_header_printed
= FALSE
;
1738 LANG_FOR_EACH_INPUT_STATEMENT (file
)
1742 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
1743 || file
->just_syms_flag
)
1746 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1747 if (s
->output_section
== NULL
1748 || s
->output_section
->owner
!= output_bfd
)
1750 if (! dis_header_printed
)
1752 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
1753 dis_header_printed
= TRUE
;
1756 print_input_section (s
);
1760 minfo (_("\nMemory Configuration\n\n"));
1761 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1762 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1764 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1769 fprintf (config
.map_file
, "%-16s ", m
->name
);
1771 sprintf_vma (buf
, m
->origin
);
1772 minfo ("0x%s ", buf
);
1780 minfo ("0x%V", m
->length
);
1781 if (m
->flags
|| m
->not_flags
)
1789 lang_map_flags (m
->flags
);
1795 lang_map_flags (m
->not_flags
);
1802 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1804 if (! command_line
.reduce_memory_overheads
)
1806 obstack_begin (&map_obstack
, 1000);
1807 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1808 bfd_map_over_sections (p
, init_map_userdata
, 0);
1809 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1811 print_statements ();
1815 init_map_userdata (abfd
, sec
, data
)
1816 bfd
*abfd ATTRIBUTE_UNUSED
;
1818 void *data ATTRIBUTE_UNUSED
;
1820 fat_section_userdata_type
*new_data
1821 = ((fat_section_userdata_type
*) (stat_alloc
1822 (sizeof (fat_section_userdata_type
))));
1824 ASSERT (get_userdata (sec
) == NULL
);
1825 get_userdata (sec
) = new_data
;
1826 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
1830 sort_def_symbol (hash_entry
, info
)
1831 struct bfd_link_hash_entry
*hash_entry
;
1832 void *info ATTRIBUTE_UNUSED
;
1834 if (hash_entry
->type
== bfd_link_hash_defined
1835 || hash_entry
->type
== bfd_link_hash_defweak
)
1837 struct fat_user_section_struct
*ud
;
1838 struct map_symbol_def
*def
;
1840 ud
= get_userdata (hash_entry
->u
.def
.section
);
1843 /* ??? What do we have to do to initialize this beforehand? */
1844 /* The first time we get here is bfd_abs_section... */
1845 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
1846 ud
= get_userdata (hash_entry
->u
.def
.section
);
1848 else if (!ud
->map_symbol_def_tail
)
1849 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
1851 def
= obstack_alloc (&map_obstack
, sizeof *def
);
1852 def
->entry
= hash_entry
;
1853 *(ud
->map_symbol_def_tail
) = def
;
1854 ud
->map_symbol_def_tail
= &def
->next
;
1859 /* Initialize an output section. */
1862 init_os (lang_output_section_statement_type
*s
, asection
*isec
)
1864 if (s
->bfd_section
!= NULL
)
1867 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1868 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1870 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
1871 if (s
->bfd_section
== NULL
)
1872 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
1873 if (s
->bfd_section
== NULL
)
1875 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1876 output_bfd
->xvec
->name
, s
->name
);
1878 s
->bfd_section
->output_section
= s
->bfd_section
;
1879 s
->bfd_section
->output_offset
= 0;
1881 if (!command_line
.reduce_memory_overheads
)
1883 fat_section_userdata_type
*new
1884 = stat_alloc (sizeof (fat_section_userdata_type
));
1885 memset (new, 0, sizeof (fat_section_userdata_type
));
1886 get_userdata (s
->bfd_section
) = new;
1889 /* If there is a base address, make sure that any sections it might
1890 mention are initialized. */
1891 if (s
->addr_tree
!= NULL
)
1892 exp_init_os (s
->addr_tree
);
1894 if (s
->load_base
!= NULL
)
1895 exp_init_os (s
->load_base
);
1897 /* If supplied an alignment, set it. */
1898 if (s
->section_alignment
!= -1)
1899 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1902 bfd_init_private_section_data (isec
->owner
, isec
,
1903 output_bfd
, s
->bfd_section
,
1907 /* Make sure that all output sections mentioned in an expression are
1911 exp_init_os (etree_type
*exp
)
1913 switch (exp
->type
.node_class
)
1917 exp_init_os (exp
->assign
.src
);
1921 exp_init_os (exp
->binary
.lhs
);
1922 exp_init_os (exp
->binary
.rhs
);
1926 exp_init_os (exp
->trinary
.cond
);
1927 exp_init_os (exp
->trinary
.lhs
);
1928 exp_init_os (exp
->trinary
.rhs
);
1932 exp_init_os (exp
->assert_s
.child
);
1936 exp_init_os (exp
->unary
.child
);
1940 switch (exp
->type
.node_code
)
1946 lang_output_section_statement_type
*os
;
1948 os
= lang_output_section_find (exp
->name
.name
);
1949 if (os
!= NULL
&& os
->bfd_section
== NULL
)
1961 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
1963 lang_input_statement_type
*entry
= data
;
1965 /* If we are only reading symbols from this object, then we want to
1966 discard all sections. */
1967 if (entry
->just_syms_flag
)
1969 bfd_link_just_syms (abfd
, sec
, &link_info
);
1973 if (!(abfd
->flags
& DYNAMIC
))
1974 bfd_section_already_linked (abfd
, sec
);
1977 /* The wild routines.
1979 These expand statements like *(.text) and foo.o to a list of
1980 explicit actions, like foo.o(.text), bar.o(.text) and
1981 foo.o(.text, .data). */
1983 /* Add SECTION to the output section OUTPUT. Do this by creating a
1984 lang_input_section statement which is placed at PTR. FILE is the
1985 input file which holds SECTION. */
1988 lang_add_section (lang_statement_list_type
*ptr
,
1990 lang_output_section_statement_type
*output
)
1992 flagword flags
= section
->flags
;
1993 bfd_boolean discard
;
1995 /* Discard sections marked with SEC_EXCLUDE. */
1996 discard
= (flags
& SEC_EXCLUDE
) != 0;
1998 /* Discard input sections which are assigned to a section named
1999 DISCARD_SECTION_NAME. */
2000 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2003 /* Discard debugging sections if we are stripping debugging
2005 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2006 && (flags
& SEC_DEBUGGING
) != 0)
2011 if (section
->output_section
== NULL
)
2013 /* This prevents future calls from assigning this section. */
2014 section
->output_section
= bfd_abs_section_ptr
;
2019 if (section
->output_section
== NULL
)
2022 lang_input_section_type
*new;
2025 if (output
->bfd_section
== NULL
)
2026 init_os (output
, section
);
2028 first
= ! output
->bfd_section
->linker_has_input
;
2029 output
->bfd_section
->linker_has_input
= 1;
2031 if (!link_info
.relocatable
2032 && !stripped_excluded_sections
)
2034 asection
*s
= output
->bfd_section
->map_tail
.s
;
2035 output
->bfd_section
->map_tail
.s
= section
;
2036 section
->map_head
.s
= NULL
;
2037 section
->map_tail
.s
= s
;
2039 s
->map_head
.s
= section
;
2041 output
->bfd_section
->map_head
.s
= section
;
2044 /* Add a section reference to the list. */
2045 new = new_stat (lang_input_section
, ptr
);
2047 new->section
= section
;
2048 section
->output_section
= output
->bfd_section
;
2050 flags
= section
->flags
;
2052 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2053 to an output section, because we want to be able to include a
2054 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2055 section (I don't know why we want to do this, but we do).
2056 build_link_order in ldwrite.c handles this case by turning
2057 the embedded SEC_NEVER_LOAD section into a fill. */
2059 flags
&= ~ SEC_NEVER_LOAD
;
2061 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2062 already been processed. One reason to do this is that on pe
2063 format targets, .text$foo sections go into .text and it's odd
2064 to see .text with SEC_LINK_ONCE set. */
2066 if (! link_info
.relocatable
)
2067 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
2069 /* If this is not the first input section, and the SEC_READONLY
2070 flag is not currently set, then don't set it just because the
2071 input section has it set. */
2073 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
2074 flags
&= ~ SEC_READONLY
;
2076 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2078 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2079 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2080 || ((flags
& SEC_MERGE
)
2081 && output
->bfd_section
->entsize
!= section
->entsize
)))
2083 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2084 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2087 output
->bfd_section
->flags
|= flags
;
2089 if (flags
& SEC_MERGE
)
2090 output
->bfd_section
->entsize
= section
->entsize
;
2092 /* If SEC_READONLY is not set in the input section, then clear
2093 it from the output section. */
2094 if ((section
->flags
& SEC_READONLY
) == 0)
2095 output
->bfd_section
->flags
&= ~SEC_READONLY
;
2097 switch (output
->sectype
)
2099 case normal_section
:
2101 case noalloc_section
:
2102 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
2104 case noload_section
:
2105 output
->bfd_section
->flags
&= ~SEC_LOAD
;
2106 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
2110 /* Copy over SEC_SMALL_DATA. */
2111 if (section
->flags
& SEC_SMALL_DATA
)
2112 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
2114 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2115 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2117 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
2118 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
2120 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2121 /* FIXME: This value should really be obtained from the bfd... */
2122 output
->block_value
= 128;
2127 /* Handle wildcard sorting. This returns the lang_input_section which
2128 should follow the one we are going to create for SECTION and FILE,
2129 based on the sorting requirements of WILD. It returns NULL if the
2130 new section should just go at the end of the current list. */
2132 static lang_statement_union_type
*
2133 wild_sort (lang_wild_statement_type
*wild
,
2134 struct wildcard_list
*sec
,
2135 lang_input_statement_type
*file
,
2138 const char *section_name
;
2139 lang_statement_union_type
*l
;
2141 if (!wild
->filenames_sorted
2142 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2145 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2146 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2148 lang_input_section_type
*ls
;
2150 if (l
->header
.type
!= lang_input_section_enum
)
2152 ls
= &l
->input_section
;
2154 /* Sorting by filename takes precedence over sorting by section
2157 if (wild
->filenames_sorted
)
2159 const char *fn
, *ln
;
2163 /* The PE support for the .idata section as generated by
2164 dlltool assumes that files will be sorted by the name of
2165 the archive and then the name of the file within the
2168 if (file
->the_bfd
!= NULL
2169 && bfd_my_archive (file
->the_bfd
) != NULL
)
2171 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2176 fn
= file
->filename
;
2180 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2182 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2187 ln
= ls
->section
->owner
->filename
;
2191 i
= strcmp (fn
, ln
);
2200 fn
= file
->filename
;
2202 ln
= ls
->section
->owner
->filename
;
2204 i
= strcmp (fn
, ln
);
2212 /* Here either the files are not sorted by name, or we are
2213 looking at the sections for this file. */
2215 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2216 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2223 /* Expand a wild statement for a particular FILE. SECTION may be
2224 NULL, in which case it is a wild card. */
2227 output_section_callback (lang_wild_statement_type
*ptr
,
2228 struct wildcard_list
*sec
,
2230 lang_input_statement_type
*file
,
2233 lang_statement_union_type
*before
;
2235 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2236 if (unique_section_p (section
))
2239 before
= wild_sort (ptr
, sec
, file
, section
);
2241 /* Here BEFORE points to the lang_input_section which
2242 should follow the one we are about to add. If BEFORE
2243 is NULL, then the section should just go at the end
2244 of the current list. */
2247 lang_add_section (&ptr
->children
, section
,
2248 (lang_output_section_statement_type
*) output
);
2251 lang_statement_list_type list
;
2252 lang_statement_union_type
**pp
;
2254 lang_list_init (&list
);
2255 lang_add_section (&list
, section
,
2256 (lang_output_section_statement_type
*) output
);
2258 /* If we are discarding the section, LIST.HEAD will
2260 if (list
.head
!= NULL
)
2262 ASSERT (list
.head
->header
.next
== NULL
);
2264 for (pp
= &ptr
->children
.head
;
2266 pp
= &(*pp
)->header
.next
)
2267 ASSERT (*pp
!= NULL
);
2269 list
.head
->header
.next
= *pp
;
2275 /* Check if all sections in a wild statement for a particular FILE
2279 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2280 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2282 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2285 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2286 if (unique_section_p (section
))
2289 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2290 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2293 /* This is passed a file name which must have been seen already and
2294 added to the statement tree. We will see if it has been opened
2295 already and had its symbols read. If not then we'll read it. */
2297 static lang_input_statement_type
*
2298 lookup_name (const char *name
)
2300 lang_input_statement_type
*search
;
2302 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2304 search
= (lang_input_statement_type
*) search
->next_real_file
)
2306 /* Use the local_sym_name as the name of the file that has
2307 already been loaded as filename might have been transformed
2308 via the search directory lookup mechanism. */
2309 const char *filename
= search
->local_sym_name
;
2311 if (filename
!= NULL
2312 && strcmp (filename
, name
) == 0)
2317 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2318 default_target
, FALSE
);
2320 /* If we have already added this file, or this file is not real
2321 don't add this file. */
2322 if (search
->loaded
|| !search
->real
)
2325 if (! load_symbols (search
, NULL
))
2331 /* Save LIST as a list of libraries whose symbols should not be exported. */
2336 struct excluded_lib
*next
;
2338 static struct excluded_lib
*excluded_libs
;
2341 add_excluded_libs (const char *list
)
2343 const char *p
= list
, *end
;
2347 struct excluded_lib
*entry
;
2348 end
= strpbrk (p
, ",:");
2350 end
= p
+ strlen (p
);
2351 entry
= xmalloc (sizeof (*entry
));
2352 entry
->next
= excluded_libs
;
2353 entry
->name
= xmalloc (end
- p
+ 1);
2354 memcpy (entry
->name
, p
, end
- p
);
2355 entry
->name
[end
- p
] = '\0';
2356 excluded_libs
= entry
;
2364 check_excluded_libs (bfd
*abfd
)
2366 struct excluded_lib
*lib
= excluded_libs
;
2370 int len
= strlen (lib
->name
);
2371 const char *filename
= lbasename (abfd
->filename
);
2373 if (strcmp (lib
->name
, "ALL") == 0)
2375 abfd
->no_export
= TRUE
;
2379 if (strncmp (lib
->name
, filename
, len
) == 0
2380 && (filename
[len
] == '\0'
2381 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2382 && filename
[len
+ 2] == '\0')))
2384 abfd
->no_export
= TRUE
;
2392 /* Get the symbols for an input file. */
2395 load_symbols (lang_input_statement_type
*entry
,
2396 lang_statement_list_type
*place
)
2403 ldfile_open_file (entry
);
2405 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2406 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2409 lang_statement_list_type
*hold
;
2410 bfd_boolean bad_load
= TRUE
;
2411 bfd_boolean save_ldlang_sysrooted_script
;
2412 bfd_boolean save_as_needed
, save_add_needed
;
2414 err
= bfd_get_error ();
2416 /* See if the emulation has some special knowledge. */
2417 if (ldemul_unrecognized_file (entry
))
2420 if (err
== bfd_error_file_ambiguously_recognized
)
2424 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2425 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2426 for (p
= matching
; *p
!= NULL
; p
++)
2430 else if (err
!= bfd_error_file_not_recognized
2432 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2436 bfd_close (entry
->the_bfd
);
2437 entry
->the_bfd
= NULL
;
2439 /* Try to interpret the file as a linker script. */
2440 ldfile_open_command_file (entry
->filename
);
2444 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2445 ldlang_sysrooted_script
= entry
->sysrooted
;
2446 save_as_needed
= as_needed
;
2447 as_needed
= entry
->as_needed
;
2448 save_add_needed
= add_needed
;
2449 add_needed
= entry
->add_needed
;
2451 ldfile_assumed_script
= TRUE
;
2452 parser_input
= input_script
;
2453 /* We want to use the same -Bdynamic/-Bstatic as the one for
2455 config
.dynamic_link
= entry
->dynamic
;
2457 ldfile_assumed_script
= FALSE
;
2459 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2460 as_needed
= save_as_needed
;
2461 add_needed
= save_add_needed
;
2467 if (ldemul_recognized_file (entry
))
2470 /* We don't call ldlang_add_file for an archive. Instead, the
2471 add_symbols entry point will call ldlang_add_file, via the
2472 add_archive_element callback, for each element of the archive
2474 switch (bfd_get_format (entry
->the_bfd
))
2480 ldlang_add_file (entry
);
2481 if (trace_files
|| trace_file_tries
)
2482 info_msg ("%I\n", entry
);
2486 check_excluded_libs (entry
->the_bfd
);
2488 if (entry
->whole_archive
)
2491 bfd_boolean loaded
= TRUE
;
2495 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2500 if (! bfd_check_format (member
, bfd_object
))
2502 einfo (_("%F%B: member %B in archive is not an object\n"),
2503 entry
->the_bfd
, member
);
2507 if (! ((*link_info
.callbacks
->add_archive_element
)
2508 (&link_info
, member
, "--whole-archive")))
2511 if (! bfd_link_add_symbols (member
, &link_info
))
2513 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2518 entry
->loaded
= loaded
;
2524 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2525 entry
->loaded
= TRUE
;
2527 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2529 return entry
->loaded
;
2532 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2533 may be NULL, indicating that it is a wildcard. Separate
2534 lang_input_section statements are created for each part of the
2535 expansion; they are added after the wild statement S. OUTPUT is
2536 the output section. */
2539 wild (lang_wild_statement_type
*s
,
2540 const char *target ATTRIBUTE_UNUSED
,
2541 lang_output_section_statement_type
*output
)
2543 struct wildcard_list
*sec
;
2545 if (s
->handler_data
[0]
2546 && s
->handler_data
[0]->spec
.sorted
== by_name
2547 && !s
->filenames_sorted
)
2549 lang_section_bst_type
*tree
;
2551 walk_wild (s
, output_section_callback_fast
, output
);
2553 tree
= (lang_section_bst_type
*) s
->handler_data
[1];
2555 output_section_callback_tree_to_list (s
, tree
, output
);
2556 s
->handler_data
[1] = NULL
;
2559 walk_wild (s
, output_section_callback
, output
);
2561 if (default_common_section
== NULL
)
2562 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2563 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2565 /* Remember the section that common is going to in case we
2566 later get something which doesn't know where to put it. */
2567 default_common_section
= output
;
2572 /* Return TRUE iff target is the sought target. */
2575 get_target (const bfd_target
*target
, void *data
)
2577 const char *sought
= data
;
2579 return strcmp (target
->name
, sought
) == 0;
2582 /* Like strcpy() but convert to lower case as well. */
2585 stricpy (char *dest
, char *src
)
2589 while ((c
= *src
++) != 0)
2590 *dest
++ = TOLOWER (c
);
2595 /* Remove the first occurrence of needle (if any) in haystack
2599 strcut (char *haystack
, char *needle
)
2601 haystack
= strstr (haystack
, needle
);
2607 for (src
= haystack
+ strlen (needle
); *src
;)
2608 *haystack
++ = *src
++;
2614 /* Compare two target format name strings.
2615 Return a value indicating how "similar" they are. */
2618 name_compare (char *first
, char *second
)
2624 copy1
= xmalloc (strlen (first
) + 1);
2625 copy2
= xmalloc (strlen (second
) + 1);
2627 /* Convert the names to lower case. */
2628 stricpy (copy1
, first
);
2629 stricpy (copy2
, second
);
2631 /* Remove size and endian strings from the name. */
2632 strcut (copy1
, "big");
2633 strcut (copy1
, "little");
2634 strcut (copy2
, "big");
2635 strcut (copy2
, "little");
2637 /* Return a value based on how many characters match,
2638 starting from the beginning. If both strings are
2639 the same then return 10 * their length. */
2640 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2641 if (copy1
[result
] == 0)
2653 /* Set by closest_target_match() below. */
2654 static const bfd_target
*winner
;
2656 /* Scan all the valid bfd targets looking for one that has the endianness
2657 requirement that was specified on the command line, and is the nearest
2658 match to the original output target. */
2661 closest_target_match (const bfd_target
*target
, void *data
)
2663 const bfd_target
*original
= data
;
2665 if (command_line
.endian
== ENDIAN_BIG
2666 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2669 if (command_line
.endian
== ENDIAN_LITTLE
2670 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2673 /* Must be the same flavour. */
2674 if (target
->flavour
!= original
->flavour
)
2677 /* If we have not found a potential winner yet, then record this one. */
2684 /* Oh dear, we now have two potential candidates for a successful match.
2685 Compare their names and choose the better one. */
2686 if (name_compare (target
->name
, original
->name
)
2687 > name_compare (winner
->name
, original
->name
))
2690 /* Keep on searching until wqe have checked them all. */
2694 /* Return the BFD target format of the first input file. */
2697 get_first_input_target (void)
2699 char *target
= NULL
;
2701 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2703 if (s
->header
.type
== lang_input_statement_enum
2706 ldfile_open_file (s
);
2708 if (s
->the_bfd
!= NULL
2709 && bfd_check_format (s
->the_bfd
, bfd_object
))
2711 target
= bfd_get_target (s
->the_bfd
);
2723 lang_get_output_target (void)
2727 /* Has the user told us which output format to use? */
2728 if (output_target
!= NULL
)
2729 return output_target
;
2731 /* No - has the current target been set to something other than
2733 if (current_target
!= default_target
)
2734 return current_target
;
2736 /* No - can we determine the format of the first input file? */
2737 target
= get_first_input_target ();
2741 /* Failed - use the default output target. */
2742 return default_target
;
2745 /* Open the output file. */
2748 open_output (const char *name
)
2752 output_target
= lang_get_output_target ();
2754 /* Has the user requested a particular endianness on the command
2756 if (command_line
.endian
!= ENDIAN_UNSET
)
2758 const bfd_target
*target
;
2759 enum bfd_endian desired_endian
;
2761 /* Get the chosen target. */
2762 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2764 /* If the target is not supported, we cannot do anything. */
2767 if (command_line
.endian
== ENDIAN_BIG
)
2768 desired_endian
= BFD_ENDIAN_BIG
;
2770 desired_endian
= BFD_ENDIAN_LITTLE
;
2772 /* See if the target has the wrong endianness. This should
2773 not happen if the linker script has provided big and
2774 little endian alternatives, but some scrips don't do
2776 if (target
->byteorder
!= desired_endian
)
2778 /* If it does, then see if the target provides
2779 an alternative with the correct endianness. */
2780 if (target
->alternative_target
!= NULL
2781 && (target
->alternative_target
->byteorder
== desired_endian
))
2782 output_target
= target
->alternative_target
->name
;
2785 /* Try to find a target as similar as possible to
2786 the default target, but which has the desired
2787 endian characteristic. */
2788 bfd_search_for_target (closest_target_match
,
2791 /* Oh dear - we could not find any targets that
2792 satisfy our requirements. */
2794 einfo (_("%P: warning: could not find any targets"
2795 " that match endianness requirement\n"));
2797 output_target
= winner
->name
;
2803 output
= bfd_openw (name
, output_target
);
2807 if (bfd_get_error () == bfd_error_invalid_target
)
2808 einfo (_("%P%F: target %s not found\n"), output_target
);
2810 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2813 delete_output_file_on_failure
= TRUE
;
2815 if (! bfd_set_format (output
, bfd_object
))
2816 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2817 if (! bfd_set_arch_mach (output
,
2818 ldfile_output_architecture
,
2819 ldfile_output_machine
))
2820 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2822 link_info
.hash
= bfd_link_hash_table_create (output
);
2823 if (link_info
.hash
== NULL
)
2824 einfo (_("%P%F: can not create hash table: %E\n"));
2826 bfd_set_gp_size (output
, g_switch_value
);
2831 ldlang_open_output (lang_statement_union_type
*statement
)
2833 switch (statement
->header
.type
)
2835 case lang_output_statement_enum
:
2836 ASSERT (output_bfd
== NULL
);
2837 output_bfd
= open_output (statement
->output_statement
.name
);
2838 ldemul_set_output_arch ();
2839 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2840 output_bfd
->flags
|= D_PAGED
;
2842 output_bfd
->flags
&= ~D_PAGED
;
2843 if (config
.text_read_only
)
2844 output_bfd
->flags
|= WP_TEXT
;
2846 output_bfd
->flags
&= ~WP_TEXT
;
2847 if (link_info
.traditional_format
)
2848 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2850 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2853 case lang_target_statement_enum
:
2854 current_target
= statement
->target_statement
.target
;
2861 /* Convert between addresses in bytes and sizes in octets.
2862 For currently supported targets, octets_per_byte is always a power
2863 of two, so we can use shifts. */
2864 #define TO_ADDR(X) ((X) >> opb_shift)
2865 #define TO_SIZE(X) ((X) << opb_shift)
2867 /* Support the above. */
2868 static unsigned int opb_shift
= 0;
2873 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2874 ldfile_output_machine
);
2877 while ((x
& 1) == 0)
2885 /* Open all the input files. */
2888 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2890 for (; s
!= NULL
; s
= s
->header
.next
)
2892 switch (s
->header
.type
)
2894 case lang_constructors_statement_enum
:
2895 open_input_bfds (constructor_list
.head
, force
);
2897 case lang_output_section_statement_enum
:
2898 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2900 case lang_wild_statement_enum
:
2901 /* Maybe we should load the file's symbols. */
2902 if (s
->wild_statement
.filename
2903 && ! wildcardp (s
->wild_statement
.filename
))
2904 lookup_name (s
->wild_statement
.filename
);
2905 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2907 case lang_group_statement_enum
:
2909 struct bfd_link_hash_entry
*undefs
;
2911 /* We must continually search the entries in the group
2912 until no new symbols are added to the list of undefined
2917 undefs
= link_info
.hash
->undefs_tail
;
2918 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2920 while (undefs
!= link_info
.hash
->undefs_tail
);
2923 case lang_target_statement_enum
:
2924 current_target
= s
->target_statement
.target
;
2926 case lang_input_statement_enum
:
2927 if (s
->input_statement
.real
)
2929 lang_statement_list_type add
;
2931 s
->input_statement
.target
= current_target
;
2933 /* If we are being called from within a group, and this
2934 is an archive which has already been searched, then
2935 force it to be researched unless the whole archive
2936 has been loaded already. */
2938 && !s
->input_statement
.whole_archive
2939 && s
->input_statement
.loaded
2940 && bfd_check_format (s
->input_statement
.the_bfd
,
2942 s
->input_statement
.loaded
= FALSE
;
2944 lang_list_init (&add
);
2946 if (! load_symbols (&s
->input_statement
, &add
))
2947 config
.make_executable
= FALSE
;
2949 if (add
.head
!= NULL
)
2951 *add
.tail
= s
->header
.next
;
2952 s
->header
.next
= add
.head
;
2962 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2965 lang_track_definedness (const char *name
)
2967 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
2968 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
2971 /* New-function for the definedness hash table. */
2973 static struct bfd_hash_entry
*
2974 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
2975 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
2976 const char *name ATTRIBUTE_UNUSED
)
2978 struct lang_definedness_hash_entry
*ret
2979 = (struct lang_definedness_hash_entry
*) entry
;
2982 ret
= (struct lang_definedness_hash_entry
*)
2983 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
2986 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
2988 ret
->iteration
= -1;
2992 /* Return the iteration when the definition of NAME was last updated. A
2993 value of -1 means that the symbol is not defined in the linker script
2994 or the command line, but may be defined in the linker symbol table. */
2997 lang_symbol_definition_iteration (const char *name
)
2999 struct lang_definedness_hash_entry
*defentry
3000 = (struct lang_definedness_hash_entry
*)
3001 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3003 /* We've already created this one on the presence of DEFINED in the
3004 script, so it can't be NULL unless something is borked elsewhere in
3006 if (defentry
== NULL
)
3009 return defentry
->iteration
;
3012 /* Update the definedness state of NAME. */
3015 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
3017 struct lang_definedness_hash_entry
*defentry
3018 = (struct lang_definedness_hash_entry
*)
3019 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3021 /* We don't keep track of symbols not tested with DEFINED. */
3022 if (defentry
== NULL
)
3025 /* If the symbol was already defined, and not from an earlier statement
3026 iteration, don't update the definedness iteration, because that'd
3027 make the symbol seem defined in the linker script at this point, and
3028 it wasn't; it was defined in some object. If we do anyway, DEFINED
3029 would start to yield false before this point and the construct "sym =
3030 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3032 if (h
->type
!= bfd_link_hash_undefined
3033 && h
->type
!= bfd_link_hash_common
3034 && h
->type
!= bfd_link_hash_new
3035 && defentry
->iteration
== -1)
3038 defentry
->iteration
= lang_statement_iteration
;
3041 /* Add the supplied name to the symbol table as an undefined reference.
3042 This is a two step process as the symbol table doesn't even exist at
3043 the time the ld command line is processed. First we put the name
3044 on a list, then, once the output file has been opened, transfer the
3045 name to the symbol table. */
3047 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3049 #define ldlang_undef_chain_list_head entry_symbol.next
3052 ldlang_add_undef (const char *const name
)
3054 ldlang_undef_chain_list_type
*new =
3055 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
3057 new->next
= ldlang_undef_chain_list_head
;
3058 ldlang_undef_chain_list_head
= new;
3060 new->name
= xstrdup (name
);
3062 if (output_bfd
!= NULL
)
3063 insert_undefined (new->name
);
3066 /* Insert NAME as undefined in the symbol table. */
3069 insert_undefined (const char *name
)
3071 struct bfd_link_hash_entry
*h
;
3073 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3075 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3076 if (h
->type
== bfd_link_hash_new
)
3078 h
->type
= bfd_link_hash_undefined
;
3079 h
->u
.undef
.abfd
= NULL
;
3080 bfd_link_add_undef (link_info
.hash
, h
);
3084 /* Run through the list of undefineds created above and place them
3085 into the linker hash table as undefined symbols belonging to the
3089 lang_place_undefineds (void)
3091 ldlang_undef_chain_list_type
*ptr
;
3093 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3094 insert_undefined (ptr
->name
);
3097 /* Check for all readonly or some readwrite sections. */
3100 check_input_sections
3101 (lang_statement_union_type
*s
,
3102 lang_output_section_statement_type
*output_section_statement
)
3104 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3106 switch (s
->header
.type
)
3108 case lang_wild_statement_enum
:
3109 walk_wild (&s
->wild_statement
, check_section_callback
,
3110 output_section_statement
);
3111 if (! output_section_statement
->all_input_readonly
)
3114 case lang_constructors_statement_enum
:
3115 check_input_sections (constructor_list
.head
,
3116 output_section_statement
);
3117 if (! output_section_statement
->all_input_readonly
)
3120 case lang_group_statement_enum
:
3121 check_input_sections (s
->group_statement
.children
.head
,
3122 output_section_statement
);
3123 if (! output_section_statement
->all_input_readonly
)
3132 /* Update wildcard statements if needed. */
3135 update_wild_statements (lang_statement_union_type
*s
)
3137 struct wildcard_list
*sec
;
3139 switch (sort_section
)
3149 for (; s
!= NULL
; s
= s
->header
.next
)
3151 switch (s
->header
.type
)
3156 case lang_wild_statement_enum
:
3157 sec
= s
->wild_statement
.section_list
;
3160 switch (sec
->spec
.sorted
)
3163 sec
->spec
.sorted
= sort_section
;
3166 if (sort_section
== by_alignment
)
3167 sec
->spec
.sorted
= by_name_alignment
;
3170 if (sort_section
== by_name
)
3171 sec
->spec
.sorted
= by_alignment_name
;
3179 case lang_constructors_statement_enum
:
3180 update_wild_statements (constructor_list
.head
);
3183 case lang_output_section_statement_enum
:
3184 update_wild_statements
3185 (s
->output_section_statement
.children
.head
);
3188 case lang_group_statement_enum
:
3189 update_wild_statements (s
->group_statement
.children
.head
);
3197 /* Open input files and attach to output sections. */
3200 map_input_to_output_sections
3201 (lang_statement_union_type
*s
, const char *target
,
3202 lang_output_section_statement_type
*os
)
3204 for (; s
!= NULL
; s
= s
->header
.next
)
3206 switch (s
->header
.type
)
3208 case lang_wild_statement_enum
:
3209 wild (&s
->wild_statement
, target
, os
);
3211 case lang_constructors_statement_enum
:
3212 map_input_to_output_sections (constructor_list
.head
,
3216 case lang_output_section_statement_enum
:
3217 if (s
->output_section_statement
.constraint
)
3219 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3220 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3222 s
->output_section_statement
.all_input_readonly
= TRUE
;
3223 check_input_sections (s
->output_section_statement
.children
.head
,
3224 &s
->output_section_statement
);
3225 if ((s
->output_section_statement
.all_input_readonly
3226 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3227 || (!s
->output_section_statement
.all_input_readonly
3228 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3230 s
->output_section_statement
.constraint
= -1;
3235 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3237 &s
->output_section_statement
);
3239 case lang_output_statement_enum
:
3241 case lang_target_statement_enum
:
3242 target
= s
->target_statement
.target
;
3244 case lang_group_statement_enum
:
3245 map_input_to_output_sections (s
->group_statement
.children
.head
,
3249 case lang_data_statement_enum
:
3250 /* Make sure that any sections mentioned in the expression
3252 exp_init_os (s
->data_statement
.exp
);
3253 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3255 /* The output section gets contents, and then we inspect for
3256 any flags set in the input script which override any ALLOC. */
3257 os
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3258 if (!(os
->flags
& SEC_NEVER_LOAD
))
3259 os
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
3261 case lang_input_section_enum
:
3263 case lang_fill_statement_enum
:
3264 case lang_object_symbols_statement_enum
:
3265 case lang_reloc_statement_enum
:
3266 case lang_padding_statement_enum
:
3267 case lang_input_statement_enum
:
3268 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3271 case lang_assignment_statement_enum
:
3272 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3275 /* Make sure that any sections mentioned in the assignment
3277 exp_init_os (s
->assignment_statement
.exp
);
3279 case lang_afile_asection_pair_statement_enum
:
3282 case lang_address_statement_enum
:
3283 /* Mark the specified section with the supplied address.
3285 If this section was actually a segment marker, then the
3286 directive is ignored if the linker script explicitly
3287 processed the segment marker. Originally, the linker
3288 treated segment directives (like -Ttext on the
3289 command-line) as section directives. We honor the
3290 section directive semantics for backwards compatibilty;
3291 linker scripts that do not specifically check for
3292 SEGMENT_START automatically get the old semantics. */
3293 if (!s
->address_statement
.segment
3294 || !s
->address_statement
.segment
->used
)
3296 lang_output_section_statement_type
*aos
3297 = (lang_output_section_statement_lookup
3298 (s
->address_statement
.section_name
));
3300 if (aos
->bfd_section
== NULL
)
3301 init_os (aos
, NULL
);
3302 aos
->addr_tree
= s
->address_statement
.address
;
3309 /* An output section might have been removed after its statement was
3310 added. For example, ldemul_before_allocation can remove dynamic
3311 sections if they turn out to be not needed. Clean them up here. */
3314 strip_excluded_output_sections (void)
3316 lang_output_section_statement_type
*os
;
3318 /* Run lang_size_sections (if not already done). */
3319 if (expld
.phase
!= lang_mark_phase_enum
)
3321 expld
.phase
= lang_mark_phase_enum
;
3322 expld
.dataseg
.phase
= exp_dataseg_none
;
3323 one_lang_size_sections_pass (NULL
, FALSE
);
3324 lang_reset_memory_regions ();
3327 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3331 asection
*output_section
;
3332 bfd_boolean exclude
;
3334 if (os
->constraint
== -1)
3337 output_section
= os
->bfd_section
;
3338 if (output_section
== NULL
)
3341 exclude
= (output_section
->rawsize
== 0
3342 && (output_section
->flags
& SEC_KEEP
) == 0
3343 && !bfd_section_removed_from_list (output_bfd
,
3346 /* Some sections have not yet been sized, notably .gnu.version,
3347 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3348 input sections, so don't drop output sections that have such
3349 input sections unless they are also marked SEC_EXCLUDE. */
3350 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3354 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3355 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3356 && (s
->flags
& SEC_EXCLUDE
) == 0)
3363 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3364 output_section
->map_head
.link_order
= NULL
;
3365 output_section
->map_tail
.link_order
= NULL
;
3369 /* We don't set bfd_section to NULL since bfd_section of the
3370 removed output section statement may still be used. */
3372 output_section
->flags
|= SEC_EXCLUDE
;
3373 bfd_section_list_remove (output_bfd
, output_section
);
3374 output_bfd
->section_count
--;
3378 /* Stop future calls to lang_add_section from messing with map_head
3379 and map_tail link_order fields. */
3380 stripped_excluded_sections
= TRUE
;
3384 print_output_section_statement
3385 (lang_output_section_statement_type
*output_section_statement
)
3387 asection
*section
= output_section_statement
->bfd_section
;
3390 if (output_section_statement
!= abs_output_section
)
3392 minfo ("\n%s", output_section_statement
->name
);
3394 if (section
!= NULL
)
3396 print_dot
= section
->vma
;
3398 len
= strlen (output_section_statement
->name
);
3399 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3404 while (len
< SECTION_NAME_MAP_LENGTH
)
3410 minfo ("0x%V %W", section
->vma
, section
->size
);
3412 if (output_section_statement
->load_base
!= NULL
)
3416 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
3418 minfo (_(" load address 0x%V"), addr
);
3425 print_statement_list (output_section_statement
->children
.head
,
3426 output_section_statement
);
3429 /* Scan for the use of the destination in the right hand side
3430 of an expression. In such cases we will not compute the
3431 correct expression, since the value of DST that is used on
3432 the right hand side will be its final value, not its value
3433 just before this expression is evaluated. */
3436 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3438 if (rhs
== NULL
|| dst
== NULL
)
3441 switch (rhs
->type
.node_class
)
3444 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3445 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3448 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3449 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3452 case etree_provided
:
3454 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3456 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3459 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3463 return strcmp (dst
, rhs
->value
.str
) == 0;
3468 return strcmp (dst
, rhs
->name
.name
) == 0;
3480 print_assignment (lang_assignment_statement_type
*assignment
,
3481 lang_output_section_statement_type
*output_section
)
3485 bfd_boolean computation_is_valid
= TRUE
;
3488 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3491 if (assignment
->exp
->type
.node_class
== etree_assert
)
3494 tree
= assignment
->exp
->assert_s
.child
;
3495 computation_is_valid
= TRUE
;
3499 const char *dst
= assignment
->exp
->assign
.dst
;
3501 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3502 tree
= assignment
->exp
->assign
.src
;
3503 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3506 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3507 if (expld
.result
.valid_p
)
3511 if (computation_is_valid
)
3513 value
= expld
.result
.value
;
3515 if (expld
.result
.section
)
3516 value
+= expld
.result
.section
->vma
;
3518 minfo ("0x%V", value
);
3524 struct bfd_link_hash_entry
*h
;
3526 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3527 FALSE
, FALSE
, TRUE
);
3530 value
= h
->u
.def
.value
;
3532 if (expld
.result
.section
)
3533 value
+= expld
.result
.section
->vma
;
3535 minfo ("[0x%V]", value
);
3538 minfo ("[unresolved]");
3550 exp_print_tree (assignment
->exp
);
3555 print_input_statement (lang_input_statement_type
*statm
)
3557 if (statm
->filename
!= NULL
)
3559 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3563 /* Print all symbols defined in a particular section. This is called
3564 via bfd_link_hash_traverse, or by print_all_symbols. */
3567 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3569 asection
*sec
= ptr
;
3571 if ((hash_entry
->type
== bfd_link_hash_defined
3572 || hash_entry
->type
== bfd_link_hash_defweak
)
3573 && sec
== hash_entry
->u
.def
.section
)
3577 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3580 (hash_entry
->u
.def
.value
3581 + hash_entry
->u
.def
.section
->output_offset
3582 + hash_entry
->u
.def
.section
->output_section
->vma
));
3584 minfo (" %T\n", hash_entry
->root
.string
);
3591 print_all_symbols (asection
*sec
)
3593 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3594 struct map_symbol_def
*def
;
3599 *ud
->map_symbol_def_tail
= 0;
3600 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3601 print_one_symbol (def
->entry
, sec
);
3604 /* Print information about an input section to the map file. */
3607 print_input_section (asection
*i
)
3609 bfd_size_type size
= i
->size
;
3616 minfo ("%s", i
->name
);
3618 len
= 1 + strlen (i
->name
);
3619 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3624 while (len
< SECTION_NAME_MAP_LENGTH
)
3630 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3631 addr
= i
->output_section
->vma
+ i
->output_offset
;
3638 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3640 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3642 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3654 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3657 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3659 if (command_line
.reduce_memory_overheads
)
3660 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3662 print_all_symbols (i
);
3664 print_dot
= addr
+ TO_ADDR (size
);
3669 print_fill_statement (lang_fill_statement_type
*fill
)
3673 fputs (" FILL mask 0x", config
.map_file
);
3674 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3675 fprintf (config
.map_file
, "%02x", *p
);
3676 fputs ("\n", config
.map_file
);
3680 print_data_statement (lang_data_statement_type
*data
)
3688 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3691 addr
= data
->output_offset
;
3692 if (data
->output_section
!= NULL
)
3693 addr
+= data
->output_section
->vma
;
3721 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3723 if (data
->exp
->type
.node_class
!= etree_value
)
3726 exp_print_tree (data
->exp
);
3731 print_dot
= addr
+ TO_ADDR (size
);
3734 /* Print an address statement. These are generated by options like
3738 print_address_statement (lang_address_statement_type
*address
)
3740 minfo (_("Address of section %s set to "), address
->section_name
);
3741 exp_print_tree (address
->address
);
3745 /* Print a reloc statement. */
3748 print_reloc_statement (lang_reloc_statement_type
*reloc
)
3755 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3758 addr
= reloc
->output_offset
;
3759 if (reloc
->output_section
!= NULL
)
3760 addr
+= reloc
->output_section
->vma
;
3762 size
= bfd_get_reloc_size (reloc
->howto
);
3764 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
3766 if (reloc
->name
!= NULL
)
3767 minfo ("%s+", reloc
->name
);
3769 minfo ("%s+", reloc
->section
->name
);
3771 exp_print_tree (reloc
->addend_exp
);
3775 print_dot
= addr
+ TO_ADDR (size
);
3779 print_padding_statement (lang_padding_statement_type
*s
)
3787 len
= sizeof " *fill*" - 1;
3788 while (len
< SECTION_NAME_MAP_LENGTH
)
3794 addr
= s
->output_offset
;
3795 if (s
->output_section
!= NULL
)
3796 addr
+= s
->output_section
->vma
;
3797 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
3799 if (s
->fill
->size
!= 0)
3803 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
3804 fprintf (config
.map_file
, "%02x", *p
);
3809 print_dot
= addr
+ TO_ADDR (s
->size
);
3813 print_wild_statement (lang_wild_statement_type
*w
,
3814 lang_output_section_statement_type
*os
)
3816 struct wildcard_list
*sec
;
3820 if (w
->filenames_sorted
)
3822 if (w
->filename
!= NULL
)
3823 minfo ("%s", w
->filename
);
3826 if (w
->filenames_sorted
)
3830 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
3832 if (sec
->spec
.sorted
)
3834 if (sec
->spec
.exclude_name_list
!= NULL
)
3837 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
3838 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
3839 minfo (" %s", tmp
->name
);
3842 if (sec
->spec
.name
!= NULL
)
3843 minfo ("%s", sec
->spec
.name
);
3846 if (sec
->spec
.sorted
)
3855 print_statement_list (w
->children
.head
, os
);
3858 /* Print a group statement. */
3861 print_group (lang_group_statement_type
*s
,
3862 lang_output_section_statement_type
*os
)
3864 fprintf (config
.map_file
, "START GROUP\n");
3865 print_statement_list (s
->children
.head
, os
);
3866 fprintf (config
.map_file
, "END GROUP\n");
3869 /* Print the list of statements in S.
3870 This can be called for any statement type. */
3873 print_statement_list (lang_statement_union_type
*s
,
3874 lang_output_section_statement_type
*os
)
3878 print_statement (s
, os
);
3883 /* Print the first statement in statement list S.
3884 This can be called for any statement type. */
3887 print_statement (lang_statement_union_type
*s
,
3888 lang_output_section_statement_type
*os
)
3890 switch (s
->header
.type
)
3893 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
3896 case lang_constructors_statement_enum
:
3897 if (constructor_list
.head
!= NULL
)
3899 if (constructors_sorted
)
3900 minfo (" SORT (CONSTRUCTORS)\n");
3902 minfo (" CONSTRUCTORS\n");
3903 print_statement_list (constructor_list
.head
, os
);
3906 case lang_wild_statement_enum
:
3907 print_wild_statement (&s
->wild_statement
, os
);
3909 case lang_address_statement_enum
:
3910 print_address_statement (&s
->address_statement
);
3912 case lang_object_symbols_statement_enum
:
3913 minfo (" CREATE_OBJECT_SYMBOLS\n");
3915 case lang_fill_statement_enum
:
3916 print_fill_statement (&s
->fill_statement
);
3918 case lang_data_statement_enum
:
3919 print_data_statement (&s
->data_statement
);
3921 case lang_reloc_statement_enum
:
3922 print_reloc_statement (&s
->reloc_statement
);
3924 case lang_input_section_enum
:
3925 print_input_section (s
->input_section
.section
);
3927 case lang_padding_statement_enum
:
3928 print_padding_statement (&s
->padding_statement
);
3930 case lang_output_section_statement_enum
:
3931 print_output_section_statement (&s
->output_section_statement
);
3933 case lang_assignment_statement_enum
:
3934 print_assignment (&s
->assignment_statement
, os
);
3936 case lang_target_statement_enum
:
3937 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
3939 case lang_output_statement_enum
:
3940 minfo ("OUTPUT(%s", s
->output_statement
.name
);
3941 if (output_target
!= NULL
)
3942 minfo (" %s", output_target
);
3945 case lang_input_statement_enum
:
3946 print_input_statement (&s
->input_statement
);
3948 case lang_group_statement_enum
:
3949 print_group (&s
->group_statement
, os
);
3951 case lang_afile_asection_pair_statement_enum
:
3958 print_statements (void)
3960 print_statement_list (statement_list
.head
, abs_output_section
);
3963 /* Print the first N statements in statement list S to STDERR.
3964 If N == 0, nothing is printed.
3965 If N < 0, the entire list is printed.
3966 Intended to be called from GDB. */
3969 dprint_statement (lang_statement_union_type
*s
, int n
)
3971 FILE *map_save
= config
.map_file
;
3973 config
.map_file
= stderr
;
3976 print_statement_list (s
, abs_output_section
);
3979 while (s
&& --n
>= 0)
3981 print_statement (s
, abs_output_section
);
3986 config
.map_file
= map_save
;
3990 insert_pad (lang_statement_union_type
**ptr
,
3992 unsigned int alignment_needed
,
3993 asection
*output_section
,
3996 static fill_type zero_fill
= { 1, { 0 } };
3997 lang_statement_union_type
*pad
= NULL
;
3999 if (ptr
!= &statement_list
.head
)
4000 pad
= ((lang_statement_union_type
*)
4001 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4003 && pad
->header
.type
== lang_padding_statement_enum
4004 && pad
->padding_statement
.output_section
== output_section
)
4006 /* Use the existing pad statement. */
4008 else if ((pad
= *ptr
) != NULL
4009 && pad
->header
.type
== lang_padding_statement_enum
4010 && pad
->padding_statement
.output_section
== output_section
)
4012 /* Use the existing pad statement. */
4016 /* Make a new padding statement, linked into existing chain. */
4017 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4018 pad
->header
.next
= *ptr
;
4020 pad
->header
.type
= lang_padding_statement_enum
;
4021 pad
->padding_statement
.output_section
= output_section
;
4024 pad
->padding_statement
.fill
= fill
;
4026 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4027 pad
->padding_statement
.size
= alignment_needed
;
4028 output_section
->size
+= alignment_needed
;
4031 /* Work out how much this section will move the dot point. */
4035 (lang_statement_union_type
**this_ptr
,
4036 lang_output_section_statement_type
*output_section_statement
,
4040 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4041 asection
*i
= is
->section
;
4043 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
4044 && (i
->flags
& SEC_EXCLUDE
) == 0)
4046 unsigned int alignment_needed
;
4049 /* Align this section first to the input sections requirement,
4050 then to the output section's requirement. If this alignment
4051 is greater than any seen before, then record it too. Perform
4052 the alignment by inserting a magic 'padding' statement. */
4054 if (output_section_statement
->subsection_alignment
!= -1)
4055 i
->alignment_power
= output_section_statement
->subsection_alignment
;
4057 o
= output_section_statement
->bfd_section
;
4058 if (o
->alignment_power
< i
->alignment_power
)
4059 o
->alignment_power
= i
->alignment_power
;
4061 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4063 if (alignment_needed
!= 0)
4065 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4066 dot
+= alignment_needed
;
4069 /* Remember where in the output section this input section goes. */
4071 i
->output_offset
= dot
- o
->vma
;
4073 /* Mark how big the output section must be to contain this now. */
4074 dot
+= TO_ADDR (i
->size
);
4075 o
->size
= TO_SIZE (dot
- o
->vma
);
4079 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
4086 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4088 const asection
*sec1
= *(const asection
**) arg1
;
4089 const asection
*sec2
= *(const asection
**) arg2
;
4091 if (bfd_section_lma (sec1
->owner
, sec1
)
4092 < bfd_section_lma (sec2
->owner
, sec2
))
4094 else if (bfd_section_lma (sec1
->owner
, sec1
)
4095 > bfd_section_lma (sec2
->owner
, sec2
))
4101 #define IGNORE_SECTION(s) \
4102 ((s->flags & SEC_NEVER_LOAD) != 0 \
4103 || (s->flags & SEC_ALLOC) == 0 \
4104 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4105 && (s->flags & SEC_LOAD) == 0))
4107 /* Check to see if any allocated sections overlap with other allocated
4108 sections. This can happen if a linker script specifies the output
4109 section addresses of the two sections. */
4112 lang_check_section_addresses (void)
4115 asection
**sections
, **spp
;
4123 if (bfd_count_sections (output_bfd
) <= 1)
4126 amt
= bfd_count_sections (output_bfd
) * sizeof (asection
*);
4127 sections
= xmalloc (amt
);
4129 /* Scan all sections in the output list. */
4131 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4133 /* Only consider loadable sections with real contents. */
4134 if (IGNORE_SECTION (s
) || s
->size
== 0)
4137 sections
[count
] = s
;
4144 qsort (sections
, (size_t) count
, sizeof (asection
*),
4145 sort_sections_by_lma
);
4149 s_start
= bfd_section_lma (output_bfd
, s
);
4150 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4151 for (count
--; count
; count
--)
4153 /* We must check the sections' LMA addresses not their VMA
4154 addresses because overlay sections can have overlapping VMAs
4155 but they must have distinct LMAs. */
4160 s_start
= bfd_section_lma (output_bfd
, s
);
4161 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4163 /* Look for an overlap. */
4164 if (s_end
>= os_start
&& s_start
<= os_end
)
4165 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4166 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4172 /* Make sure the new address is within the region. We explicitly permit the
4173 current address to be at the exact end of the region when the address is
4174 non-zero, in case the region is at the end of addressable memory and the
4175 calculation wraps around. */
4178 os_region_check (lang_output_section_statement_type
*os
,
4179 lang_memory_region_type
*region
,
4183 if ((region
->current
< region
->origin
4184 || (region
->current
- region
->origin
> region
->length
))
4185 && ((region
->current
!= region
->origin
+ region
->length
)
4190 einfo (_("%X%P: address 0x%v of %B section %s"
4191 " is not within region %s\n"),
4193 os
->bfd_section
->owner
,
4194 os
->bfd_section
->name
,
4199 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4201 os
->bfd_section
->owner
,
4202 os
->bfd_section
->name
);
4204 /* Reset the region pointer. */
4205 region
->current
= region
->origin
;
4209 /* Set the sizes for all the output sections. */
4212 lang_size_sections_1
4213 (lang_statement_union_type
*s
,
4214 lang_output_section_statement_type
*output_section_statement
,
4215 lang_statement_union_type
**prev
,
4219 bfd_boolean check_regions
)
4221 /* Size up the sections from their constituent parts. */
4222 for (; s
!= NULL
; s
= s
->header
.next
)
4224 switch (s
->header
.type
)
4226 case lang_output_section_statement_enum
:
4228 bfd_vma newdot
, after
;
4229 lang_output_section_statement_type
*os
;
4231 os
= &s
->output_section_statement
;
4232 if (os
->addr_tree
!= NULL
)
4234 os
->processed
= FALSE
;
4235 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4237 if (!expld
.result
.valid_p
4238 && expld
.phase
!= lang_mark_phase_enum
)
4239 einfo (_("%F%S: non constant or forward reference"
4240 " address expression for section %s\n"),
4243 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4246 if (os
->bfd_section
== NULL
)
4247 /* This section was removed or never actually created. */
4250 /* If this is a COFF shared library section, use the size and
4251 address from the input section. FIXME: This is COFF
4252 specific; it would be cleaner if there were some other way
4253 to do this, but nothing simple comes to mind. */
4254 if ((bfd_get_flavour (output_bfd
) == bfd_target_ecoff_flavour
4255 || bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
4256 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4260 if (os
->children
.head
== NULL
4261 || os
->children
.head
->header
.next
!= NULL
4262 || (os
->children
.head
->header
.type
4263 != lang_input_section_enum
))
4264 einfo (_("%P%X: Internal error on COFF shared library"
4265 " section %s\n"), os
->name
);
4267 input
= os
->children
.head
->input_section
.section
;
4268 bfd_set_section_vma (os
->bfd_section
->owner
,
4270 bfd_section_vma (input
->owner
, input
));
4271 os
->bfd_section
->size
= input
->size
;
4276 if (bfd_is_abs_section (os
->bfd_section
))
4278 /* No matter what happens, an abs section starts at zero. */
4279 ASSERT (os
->bfd_section
->vma
== 0);
4285 if (os
->addr_tree
== NULL
)
4287 /* No address specified for this section, get one
4288 from the region specification. */
4289 if (os
->region
== NULL
4290 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4291 && os
->region
->name
[0] == '*'
4292 && strcmp (os
->region
->name
,
4293 DEFAULT_MEMORY_REGION
) == 0))
4295 os
->region
= lang_memory_default (os
->bfd_section
);
4298 /* If a loadable section is using the default memory
4299 region, and some non default memory regions were
4300 defined, issue an error message. */
4302 && !IGNORE_SECTION (os
->bfd_section
)
4303 && ! link_info
.relocatable
4305 && strcmp (os
->region
->name
,
4306 DEFAULT_MEMORY_REGION
) == 0
4307 && lang_memory_region_list
!= NULL
4308 && (strcmp (lang_memory_region_list
->name
,
4309 DEFAULT_MEMORY_REGION
) != 0
4310 || lang_memory_region_list
->next
!= NULL
)
4311 && expld
.phase
!= lang_mark_phase_enum
)
4313 /* By default this is an error rather than just a
4314 warning because if we allocate the section to the
4315 default memory region we can end up creating an
4316 excessively large binary, or even seg faulting when
4317 attempting to perform a negative seek. See
4318 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4319 for an example of this. This behaviour can be
4320 overridden by the using the --no-check-sections
4322 if (command_line
.check_section_addresses
)
4323 einfo (_("%P%F: error: no memory region specified"
4324 " for loadable section `%s'\n"),
4325 bfd_get_section_name (output_bfd
,
4328 einfo (_("%P: warning: no memory region specified"
4329 " for loadable section `%s'\n"),
4330 bfd_get_section_name (output_bfd
,
4334 newdot
= os
->region
->current
;
4335 align
= os
->bfd_section
->alignment_power
;
4338 align
= os
->section_alignment
;
4340 /* Align to what the section needs. */
4343 bfd_vma savedot
= newdot
;
4344 newdot
= align_power (newdot
, align
);
4346 if (newdot
!= savedot
4347 && (config
.warn_section_align
4348 || os
->addr_tree
!= NULL
)
4349 && expld
.phase
!= lang_mark_phase_enum
)
4350 einfo (_("%P: warning: changing start of section"
4351 " %s by %lu bytes\n"),
4352 os
->name
, (unsigned long) (newdot
- savedot
));
4355 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4357 os
->bfd_section
->output_offset
= 0;
4360 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4361 os
->fill
, newdot
, relax
, check_regions
);
4363 os
->processed
= TRUE
;
4365 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4367 ASSERT (os
->bfd_section
->size
== 0);
4371 dot
= os
->bfd_section
->vma
;
4373 /* Put the section within the requested block size, or
4374 align at the block boundary. */
4376 + TO_ADDR (os
->bfd_section
->size
)
4377 + os
->block_value
- 1)
4378 & - (bfd_vma
) os
->block_value
);
4380 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4382 /* .tbss sections effectively have zero size. */
4383 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4384 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4385 || link_info
.relocatable
)
4386 dot
+= TO_ADDR (os
->bfd_section
->size
);
4388 if (os
->update_dot_tree
!= 0)
4389 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4391 /* Update dot in the region ?
4392 We only do this if the section is going to be allocated,
4393 since unallocated sections do not contribute to the region's
4394 overall size in memory.
4396 If the SEC_NEVER_LOAD bit is not set, it will affect the
4397 addresses of sections after it. We have to update
4399 if (os
->region
!= NULL
4400 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4401 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4403 os
->region
->current
= dot
;
4406 /* Make sure the new address is within the region. */
4407 os_region_check (os
, os
->region
, os
->addr_tree
,
4408 os
->bfd_section
->vma
);
4410 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
4412 /* Set load_base, which will be handled later. */
4413 os
->load_base
= exp_intop (os
->lma_region
->current
);
4414 os
->lma_region
->current
+=
4415 TO_ADDR (os
->bfd_section
->size
);
4418 os_region_check (os
, os
->lma_region
, NULL
,
4419 os
->lma_region
->current
);
4425 case lang_constructors_statement_enum
:
4426 dot
= lang_size_sections_1 (constructor_list
.head
,
4427 output_section_statement
,
4428 &s
->wild_statement
.children
.head
,
4429 fill
, dot
, relax
, check_regions
);
4432 case lang_data_statement_enum
:
4434 unsigned int size
= 0;
4436 s
->data_statement
.output_offset
=
4437 dot
- output_section_statement
->bfd_section
->vma
;
4438 s
->data_statement
.output_section
=
4439 output_section_statement
->bfd_section
;
4441 /* We might refer to provided symbols in the expression, and
4442 need to mark them as needed. */
4443 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4445 switch (s
->data_statement
.type
)
4463 if (size
< TO_SIZE ((unsigned) 1))
4464 size
= TO_SIZE ((unsigned) 1);
4465 dot
+= TO_ADDR (size
);
4466 output_section_statement
->bfd_section
->size
+= size
;
4470 case lang_reloc_statement_enum
:
4474 s
->reloc_statement
.output_offset
=
4475 dot
- output_section_statement
->bfd_section
->vma
;
4476 s
->reloc_statement
.output_section
=
4477 output_section_statement
->bfd_section
;
4478 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4479 dot
+= TO_ADDR (size
);
4480 output_section_statement
->bfd_section
->size
+= size
;
4484 case lang_wild_statement_enum
:
4485 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4486 output_section_statement
,
4487 &s
->wild_statement
.children
.head
,
4488 fill
, dot
, relax
, check_regions
);
4491 case lang_object_symbols_statement_enum
:
4492 link_info
.create_object_symbols_section
=
4493 output_section_statement
->bfd_section
;
4496 case lang_output_statement_enum
:
4497 case lang_target_statement_enum
:
4500 case lang_input_section_enum
:
4504 i
= (*prev
)->input_section
.section
;
4509 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4510 einfo (_("%P%F: can't relax section: %E\n"));
4514 dot
= size_input_section (prev
, output_section_statement
,
4515 output_section_statement
->fill
, dot
);
4519 case lang_input_statement_enum
:
4522 case lang_fill_statement_enum
:
4523 s
->fill_statement
.output_section
=
4524 output_section_statement
->bfd_section
;
4526 fill
= s
->fill_statement
.fill
;
4529 case lang_assignment_statement_enum
:
4531 bfd_vma newdot
= dot
;
4533 exp_fold_tree (s
->assignment_statement
.exp
,
4534 output_section_statement
->bfd_section
,
4537 if (newdot
!= dot
&& !output_section_statement
->ignored
)
4539 if (output_section_statement
== abs_output_section
)
4541 /* If we don't have an output section, then just adjust
4542 the default memory address. */
4543 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4544 FALSE
)->current
= newdot
;
4548 /* Insert a pad after this statement. We can't
4549 put the pad before when relaxing, in case the
4550 assignment references dot. */
4551 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4552 output_section_statement
->bfd_section
, dot
);
4554 /* Don't neuter the pad below when relaxing. */
4557 /* If dot is advanced, this implies that the section
4558 should have space allocated to it, unless the
4559 user has explicitly stated that the section
4560 should never be loaded. */
4561 if (!(output_section_statement
->flags
4562 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
4563 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4570 case lang_padding_statement_enum
:
4571 /* If this is the first time lang_size_sections is called,
4572 we won't have any padding statements. If this is the
4573 second or later passes when relaxing, we should allow
4574 padding to shrink. If padding is needed on this pass, it
4575 will be added back in. */
4576 s
->padding_statement
.size
= 0;
4578 /* Make sure output_offset is valid. If relaxation shrinks
4579 the section and this pad isn't needed, it's possible to
4580 have output_offset larger than the final size of the
4581 section. bfd_set_section_contents will complain even for
4582 a pad size of zero. */
4583 s
->padding_statement
.output_offset
4584 = dot
- output_section_statement
->bfd_section
->vma
;
4587 case lang_group_statement_enum
:
4588 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4589 output_section_statement
,
4590 &s
->group_statement
.children
.head
,
4591 fill
, dot
, relax
, check_regions
);
4598 /* We can only get here when relaxing is turned on. */
4599 case lang_address_statement_enum
:
4602 prev
= &s
->header
.next
;
4608 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
4610 lang_statement_iteration
++;
4611 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
4612 &statement_list
.head
, 0, 0, relax
, check_regions
);
4616 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
4618 expld
.phase
= lang_allocating_phase_enum
;
4619 expld
.dataseg
.phase
= exp_dataseg_none
;
4621 one_lang_size_sections_pass (relax
, check_regions
);
4622 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
4623 && link_info
.relro
&& expld
.dataseg
.relro_end
)
4625 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4626 to put expld.dataseg.relro on a (common) page boundary. */
4627 bfd_vma old_min_base
, relro_end
, maxpage
;
4629 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
4630 old_min_base
= expld
.dataseg
.min_base
;
4631 maxpage
= expld
.dataseg
.maxpagesize
;
4632 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
4633 & (expld
.dataseg
.pagesize
- 1));
4634 /* Compute the expected PT_GNU_RELRO segment end. */
4635 relro_end
= (expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
4636 & ~(expld
.dataseg
.pagesize
- 1);
4637 if (old_min_base
+ maxpage
< expld
.dataseg
.base
)
4639 expld
.dataseg
.base
-= maxpage
;
4640 relro_end
-= maxpage
;
4642 lang_reset_memory_regions ();
4643 one_lang_size_sections_pass (relax
, check_regions
);
4644 if (expld
.dataseg
.relro_end
> relro_end
)
4646 /* The alignment of sections between DATA_SEGMENT_ALIGN
4647 and DATA_SEGMENT_RELRO_END caused huge padding to be
4648 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4650 unsigned int max_alignment_power
= 0;
4652 /* Find maximum alignment power of sections between
4653 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4654 for (sec
= output_bfd
->sections
; sec
; sec
= sec
->next
)
4655 if (sec
->vma
>= expld
.dataseg
.base
4656 && sec
->vma
< expld
.dataseg
.relro_end
4657 && sec
->alignment_power
> max_alignment_power
)
4658 max_alignment_power
= sec
->alignment_power
;
4660 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
4662 if (expld
.dataseg
.base
- (1 << max_alignment_power
)
4664 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
4665 expld
.dataseg
.base
-= (1 << max_alignment_power
);
4666 lang_reset_memory_regions ();
4667 one_lang_size_sections_pass (relax
, check_regions
);
4670 link_info
.relro_start
= expld
.dataseg
.base
;
4671 link_info
.relro_end
= expld
.dataseg
.relro_end
;
4673 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
4675 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4676 a page could be saved in the data segment. */
4677 bfd_vma first
, last
;
4679 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
4680 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
4682 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
4683 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
4684 && first
+ last
<= expld
.dataseg
.pagesize
)
4686 expld
.dataseg
.phase
= exp_dataseg_adjust
;
4687 lang_reset_memory_regions ();
4688 one_lang_size_sections_pass (relax
, check_regions
);
4692 expld
.phase
= lang_final_phase_enum
;
4695 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4698 lang_do_assignments_1 (lang_statement_union_type
*s
,
4699 lang_output_section_statement_type
*current_os
,
4703 for (; s
!= NULL
; s
= s
->header
.next
)
4705 switch (s
->header
.type
)
4707 case lang_constructors_statement_enum
:
4708 dot
= lang_do_assignments_1 (constructor_list
.head
,
4709 current_os
, fill
, dot
);
4712 case lang_output_section_statement_enum
:
4714 lang_output_section_statement_type
*os
;
4716 os
= &(s
->output_section_statement
);
4717 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
4719 lang_memory_region_type
*r
;
4721 dot
= os
->bfd_section
->vma
;
4724 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
4727 os
->bfd_section
->lma
4728 = exp_get_abs_int (os
->load_base
, 0, "load base");
4729 else if (r
->last_os
!= NULL
)
4734 last
= r
->last_os
->output_section_statement
.bfd_section
;
4736 /* If the current vma overlaps the previous section,
4737 then set the current lma to that at the end of
4738 the previous section. The previous section was
4739 probably an overlay. */
4740 if ((dot
>= last
->vma
4741 && dot
< last
->vma
+ last
->size
)
4742 || (last
->vma
>= dot
4743 && last
->vma
< dot
+ os
->bfd_section
->size
))
4744 lma
= last
->lma
+ last
->size
;
4746 /* Otherwise, keep the same lma to vma relationship
4747 as the previous section. */
4749 lma
= dot
+ last
->lma
- last
->vma
;
4751 if (os
->section_alignment
!= -1)
4752 lma
= align_power (lma
, os
->section_alignment
);
4753 os
->bfd_section
->lma
= lma
;
4756 lang_do_assignments_1 (os
->children
.head
,
4759 /* .tbss sections effectively have zero size. */
4760 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4761 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4762 || link_info
.relocatable
)
4764 dot
+= TO_ADDR (os
->bfd_section
->size
);
4766 /* Keep track of normal sections using the default
4767 lma region. We use this to set the lma for
4768 following sections. Overlays or other linker
4769 script assignment to lma might mean that the
4770 default lma == vma is incorrect. */
4771 if (!link_info
.relocatable
4772 && os
->lma_region
== NULL
)
4779 case lang_wild_statement_enum
:
4781 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
4782 current_os
, fill
, dot
);
4785 case lang_object_symbols_statement_enum
:
4786 case lang_output_statement_enum
:
4787 case lang_target_statement_enum
:
4790 case lang_data_statement_enum
:
4791 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4792 if (expld
.result
.valid_p
)
4793 s
->data_statement
.value
= (expld
.result
.value
4794 + expld
.result
.section
->vma
);
4796 einfo (_("%F%P: invalid data statement\n"));
4799 switch (s
->data_statement
.type
)
4817 if (size
< TO_SIZE ((unsigned) 1))
4818 size
= TO_SIZE ((unsigned) 1);
4819 dot
+= TO_ADDR (size
);
4823 case lang_reloc_statement_enum
:
4824 exp_fold_tree (s
->reloc_statement
.addend_exp
,
4825 bfd_abs_section_ptr
, &dot
);
4826 if (expld
.result
.valid_p
)
4827 s
->reloc_statement
.addend_value
= expld
.result
.value
;
4829 einfo (_("%F%P: invalid reloc statement\n"));
4830 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
4833 case lang_input_section_enum
:
4835 asection
*in
= s
->input_section
.section
;
4837 if ((in
->flags
& SEC_EXCLUDE
) == 0)
4838 dot
+= TO_ADDR (in
->size
);
4842 case lang_input_statement_enum
:
4845 case lang_fill_statement_enum
:
4846 fill
= s
->fill_statement
.fill
;
4849 case lang_assignment_statement_enum
:
4850 exp_fold_tree (s
->assignment_statement
.exp
,
4851 current_os
->bfd_section
,
4855 case lang_padding_statement_enum
:
4856 dot
+= TO_ADDR (s
->padding_statement
.size
);
4859 case lang_group_statement_enum
:
4860 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
4861 current_os
, fill
, dot
);
4868 case lang_address_statement_enum
:
4876 lang_do_assignments (void)
4878 lang_statement_iteration
++;
4879 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
4882 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4883 operator .startof. (section_name), it produces an undefined symbol
4884 .startof.section_name. Similarly, when it sees
4885 .sizeof. (section_name), it produces an undefined symbol
4886 .sizeof.section_name. For all the output sections, we look for
4887 such symbols, and set them to the correct value. */
4890 lang_set_startof (void)
4894 if (link_info
.relocatable
)
4897 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4899 const char *secname
;
4901 struct bfd_link_hash_entry
*h
;
4903 secname
= bfd_get_section_name (output_bfd
, s
);
4904 buf
= xmalloc (10 + strlen (secname
));
4906 sprintf (buf
, ".startof.%s", secname
);
4907 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4908 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4910 h
->type
= bfd_link_hash_defined
;
4911 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
4912 h
->u
.def
.section
= bfd_abs_section_ptr
;
4915 sprintf (buf
, ".sizeof.%s", secname
);
4916 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4917 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4919 h
->type
= bfd_link_hash_defined
;
4920 h
->u
.def
.value
= TO_ADDR (s
->size
);
4921 h
->u
.def
.section
= bfd_abs_section_ptr
;
4931 struct bfd_link_hash_entry
*h
;
4934 if (link_info
.relocatable
|| link_info
.shared
)
4939 if (entry_symbol
.name
== NULL
)
4941 /* No entry has been specified. Look for the default entry, but
4942 don't warn if we don't find it. */
4943 entry_symbol
.name
= entry_symbol_default
;
4947 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
4948 FALSE
, FALSE
, TRUE
);
4950 && (h
->type
== bfd_link_hash_defined
4951 || h
->type
== bfd_link_hash_defweak
)
4952 && h
->u
.def
.section
->output_section
!= NULL
)
4956 val
= (h
->u
.def
.value
4957 + bfd_get_section_vma (output_bfd
,
4958 h
->u
.def
.section
->output_section
)
4959 + h
->u
.def
.section
->output_offset
);
4960 if (! bfd_set_start_address (output_bfd
, val
))
4961 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
4968 /* We couldn't find the entry symbol. Try parsing it as a
4970 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
4973 if (! bfd_set_start_address (output_bfd
, val
))
4974 einfo (_("%P%F: can't set start address\n"));
4980 /* Can't find the entry symbol, and it's not a number. Use
4981 the first address in the text section. */
4982 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
4986 einfo (_("%P: warning: cannot find entry symbol %s;"
4987 " defaulting to %V\n"),
4989 bfd_get_section_vma (output_bfd
, ts
));
4990 if (! bfd_set_start_address (output_bfd
,
4991 bfd_get_section_vma (output_bfd
,
4993 einfo (_("%P%F: can't set start address\n"));
4998 einfo (_("%P: warning: cannot find entry symbol %s;"
4999 " not setting start address\n"),
5005 /* Don't bfd_hash_table_free (&lang_definedness_table);
5006 map file output may result in a call of lang_track_definedness. */
5009 /* This is a small function used when we want to ignore errors from
5013 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
5015 /* Don't do anything. */
5018 /* Check that the architecture of all the input files is compatible
5019 with the output file. Also call the backend to let it do any
5020 other checking that is needed. */
5025 lang_statement_union_type
*file
;
5027 const bfd_arch_info_type
*compatible
;
5029 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
5031 input_bfd
= file
->input_statement
.the_bfd
;
5033 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
5034 command_line
.accept_unknown_input_arch
);
5036 /* In general it is not possible to perform a relocatable
5037 link between differing object formats when the input
5038 file has relocations, because the relocations in the
5039 input format may not have equivalent representations in
5040 the output format (and besides BFD does not translate
5041 relocs for other link purposes than a final link). */
5042 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
5043 && (compatible
== NULL
5044 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
5045 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
5047 einfo (_("%P%F: Relocatable linking with relocations from"
5048 " format %s (%B) to format %s (%B) is not supported\n"),
5049 bfd_get_target (input_bfd
), input_bfd
,
5050 bfd_get_target (output_bfd
), output_bfd
);
5051 /* einfo with %F exits. */
5054 if (compatible
== NULL
)
5056 if (command_line
.warn_mismatch
)
5057 einfo (_("%P: warning: %s architecture of input file `%B'"
5058 " is incompatible with %s output\n"),
5059 bfd_printable_name (input_bfd
), input_bfd
,
5060 bfd_printable_name (output_bfd
));
5062 else if (bfd_count_sections (input_bfd
))
5064 /* If the input bfd has no contents, it shouldn't set the
5065 private data of the output bfd. */
5067 bfd_error_handler_type pfn
= NULL
;
5069 /* If we aren't supposed to warn about mismatched input
5070 files, temporarily set the BFD error handler to a
5071 function which will do nothing. We still want to call
5072 bfd_merge_private_bfd_data, since it may set up
5073 information which is needed in the output file. */
5074 if (! command_line
.warn_mismatch
)
5075 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
5076 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
5078 if (command_line
.warn_mismatch
)
5079 einfo (_("%P%X: failed to merge target specific data"
5080 " of file %B\n"), input_bfd
);
5082 if (! command_line
.warn_mismatch
)
5083 bfd_set_error_handler (pfn
);
5088 /* Look through all the global common symbols and attach them to the
5089 correct section. The -sort-common command line switch may be used
5090 to roughly sort the entries by size. */
5095 if (command_line
.inhibit_common_definition
)
5097 if (link_info
.relocatable
5098 && ! command_line
.force_common_definition
)
5101 if (! config
.sort_common
)
5102 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
5107 for (power
= 4; power
>= 0; power
--)
5108 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5112 /* Place one common symbol in the correct section. */
5115 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
5117 unsigned int power_of_two
;
5121 if (h
->type
!= bfd_link_hash_common
)
5125 power_of_two
= h
->u
.c
.p
->alignment_power
;
5127 if (config
.sort_common
5128 && power_of_two
< (unsigned int) *(int *) info
)
5131 section
= h
->u
.c
.p
->section
;
5133 /* Increase the size of the section to align the common sym. */
5134 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
5135 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
5137 /* Adjust the alignment if necessary. */
5138 if (power_of_two
> section
->alignment_power
)
5139 section
->alignment_power
= power_of_two
;
5141 /* Change the symbol from common to defined. */
5142 h
->type
= bfd_link_hash_defined
;
5143 h
->u
.def
.section
= section
;
5144 h
->u
.def
.value
= section
->size
;
5146 /* Increase the size of the section. */
5147 section
->size
+= size
;
5149 /* Make sure the section is allocated in memory, and make sure that
5150 it is no longer a common section. */
5151 section
->flags
|= SEC_ALLOC
;
5152 section
->flags
&= ~SEC_IS_COMMON
;
5154 if (config
.map_file
!= NULL
)
5156 static bfd_boolean header_printed
;
5161 if (! header_printed
)
5163 minfo (_("\nAllocating common symbols\n"));
5164 minfo (_("Common symbol size file\n\n"));
5165 header_printed
= TRUE
;
5168 name
= demangle (h
->root
.string
);
5170 len
= strlen (name
);
5185 if (size
<= 0xffffffff)
5186 sprintf (buf
, "%lx", (unsigned long) size
);
5188 sprintf_vma (buf
, size
);
5198 minfo ("%B\n", section
->owner
);
5204 /* Run through the input files and ensure that every input section has
5205 somewhere to go. If one is found without a destination then create
5206 an input request and place it into the statement tree. */
5209 lang_place_orphans (void)
5211 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5215 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5217 if (s
->output_section
== NULL
)
5219 /* This section of the file is not attached, root
5220 around for a sensible place for it to go. */
5222 if (file
->just_syms_flag
)
5223 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5224 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5225 s
->output_section
= bfd_abs_section_ptr
;
5226 else if (strcmp (s
->name
, "COMMON") == 0)
5228 /* This is a lonely common section which must have
5229 come from an archive. We attach to the section
5230 with the wildcard. */
5231 if (! link_info
.relocatable
5232 || command_line
.force_common_definition
)
5234 if (default_common_section
== NULL
)
5236 default_common_section
=
5237 lang_output_section_statement_lookup (".bss");
5240 lang_add_section (&default_common_section
->children
, s
,
5241 default_common_section
);
5244 else if (ldemul_place_orphan (s
))
5248 lang_output_section_statement_type
*os
;
5250 os
= lang_output_section_statement_lookup (s
->name
);
5251 lang_add_section (&os
->children
, s
, os
);
5259 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5261 flagword
*ptr_flags
;
5263 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5269 *ptr_flags
|= SEC_ALLOC
;
5273 *ptr_flags
|= SEC_READONLY
;
5277 *ptr_flags
|= SEC_DATA
;
5281 *ptr_flags
|= SEC_CODE
;
5286 *ptr_flags
|= SEC_LOAD
;
5290 einfo (_("%P%F: invalid syntax in flags\n"));
5297 /* Call a function on each input file. This function will be called
5298 on an archive, but not on the elements. */
5301 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5303 lang_input_statement_type
*f
;
5305 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5307 f
= (lang_input_statement_type
*) f
->next_real_file
)
5311 /* Call a function on each file. The function will be called on all
5312 the elements of an archive which are included in the link, but will
5313 not be called on the archive file itself. */
5316 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5318 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5325 ldlang_add_file (lang_input_statement_type
*entry
)
5329 lang_statement_append (&file_chain
,
5330 (lang_statement_union_type
*) entry
,
5333 /* The BFD linker needs to have a list of all input BFDs involved in
5335 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5336 ASSERT (entry
->the_bfd
!= output_bfd
);
5337 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
5339 *pp
= entry
->the_bfd
;
5340 entry
->the_bfd
->usrdata
= entry
;
5341 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5343 /* Look through the sections and check for any which should not be
5344 included in the link. We need to do this now, so that we can
5345 notice when the backend linker tries to report multiple
5346 definition errors for symbols which are in sections we aren't
5347 going to link. FIXME: It might be better to entirely ignore
5348 symbols which are defined in sections which are going to be
5349 discarded. This would require modifying the backend linker for
5350 each backend which might set the SEC_LINK_ONCE flag. If we do
5351 this, we should probably handle SEC_EXCLUDE in the same way. */
5353 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5357 lang_add_output (const char *name
, int from_script
)
5359 /* Make -o on command line override OUTPUT in script. */
5360 if (!had_output_filename
|| !from_script
)
5362 output_filename
= name
;
5363 had_output_filename
= TRUE
;
5367 static lang_output_section_statement_type
*current_section
;
5378 for (l
= 0; l
< 32; l
++)
5380 if (i
>= (unsigned int) x
)
5388 lang_output_section_statement_type
*
5389 lang_enter_output_section_statement (const char *output_section_statement_name
,
5390 etree_type
*address_exp
,
5391 enum section_type sectype
,
5393 etree_type
*subalign
,
5397 lang_output_section_statement_type
*os
;
5399 os
= lang_output_section_statement_lookup_1 (output_section_statement_name
,
5401 current_section
= os
;
5403 /* Make next things chain into subchain of this. */
5405 if (os
->addr_tree
== NULL
)
5407 os
->addr_tree
= address_exp
;
5409 os
->sectype
= sectype
;
5410 if (sectype
!= noload_section
)
5411 os
->flags
= SEC_NO_FLAGS
;
5413 os
->flags
= SEC_NEVER_LOAD
;
5414 os
->block_value
= 1;
5415 stat_ptr
= &os
->children
;
5417 os
->subsection_alignment
=
5418 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5419 os
->section_alignment
=
5420 topower (exp_get_value_int (align
, -1, "section alignment"));
5422 os
->load_base
= ebase
;
5429 lang_output_statement_type
*new;
5431 new = new_stat (lang_output_statement
, stat_ptr
);
5432 new->name
= output_filename
;
5435 /* Reset the current counters in the regions. */
5438 lang_reset_memory_regions (void)
5440 lang_memory_region_type
*p
= lang_memory_region_list
;
5442 lang_output_section_statement_type
*os
;
5444 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5446 p
->current
= p
->origin
;
5450 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5453 os
->processed
= FALSE
;
5455 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5457 /* Save the last size for possible use by bfd_relax_section. */
5458 o
->rawsize
= o
->size
;
5463 /* Worker for lang_gc_sections_1. */
5466 gc_section_callback (lang_wild_statement_type
*ptr
,
5467 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5469 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5470 void *data ATTRIBUTE_UNUSED
)
5472 /* If the wild pattern was marked KEEP, the member sections
5473 should be as well. */
5474 if (ptr
->keep_sections
)
5475 section
->flags
|= SEC_KEEP
;
5478 /* Iterate over sections marking them against GC. */
5481 lang_gc_sections_1 (lang_statement_union_type
*s
)
5483 for (; s
!= NULL
; s
= s
->header
.next
)
5485 switch (s
->header
.type
)
5487 case lang_wild_statement_enum
:
5488 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5490 case lang_constructors_statement_enum
:
5491 lang_gc_sections_1 (constructor_list
.head
);
5493 case lang_output_section_statement_enum
:
5494 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5496 case lang_group_statement_enum
:
5497 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5506 lang_gc_sections (void)
5508 struct bfd_link_hash_entry
*h
;
5509 ldlang_undef_chain_list_type
*ulist
;
5511 /* Keep all sections so marked in the link script. */
5513 lang_gc_sections_1 (statement_list
.head
);
5515 /* Keep all sections containing symbols undefined on the command-line,
5516 and the section containing the entry symbol. */
5518 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
5520 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
5521 FALSE
, FALSE
, FALSE
);
5524 && (h
->type
== bfd_link_hash_defined
5525 || h
->type
== bfd_link_hash_defweak
)
5526 && ! bfd_is_abs_section (h
->u
.def
.section
))
5528 h
->u
.def
.section
->flags
|= SEC_KEEP
;
5532 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5533 the special case of debug info. (See bfd/stabs.c)
5534 Twiddle the flag here, to simplify later linker code. */
5535 if (link_info
.relocatable
)
5537 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5540 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5541 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5542 sec
->flags
&= ~SEC_EXCLUDE
;
5546 if (link_info
.gc_sections
)
5547 bfd_gc_sections (output_bfd
, &link_info
);
5550 /* Relax all sections until bfd_relax_section gives up. */
5553 relax_sections (void)
5555 /* Keep relaxing until bfd_relax_section gives up. */
5556 bfd_boolean relax_again
;
5560 relax_again
= FALSE
;
5562 /* Note: pe-dll.c does something like this also. If you find
5563 you need to change this code, you probably need to change
5564 pe-dll.c also. DJ */
5566 /* Do all the assignments with our current guesses as to
5568 lang_do_assignments ();
5570 /* We must do this after lang_do_assignments, because it uses
5572 lang_reset_memory_regions ();
5574 /* Perform another relax pass - this time we know where the
5575 globals are, so can make a better guess. */
5576 lang_size_sections (&relax_again
, FALSE
);
5578 while (relax_again
);
5584 current_target
= default_target
;
5586 /* Open the output file. */
5587 lang_for_each_statement (ldlang_open_output
);
5590 ldemul_create_output_section_statements ();
5592 /* Add to the hash table all undefineds on the command line. */
5593 lang_place_undefineds ();
5595 if (!bfd_section_already_linked_table_init ())
5596 einfo (_("%P%F: Failed to create hash table\n"));
5598 /* Create a bfd for each input file. */
5599 current_target
= default_target
;
5600 open_input_bfds (statement_list
.head
, FALSE
);
5602 link_info
.gc_sym_list
= &entry_symbol
;
5603 if (entry_symbol
.name
== NULL
)
5604 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
5606 ldemul_after_open ();
5608 bfd_section_already_linked_table_free ();
5610 /* Make sure that we're not mixing architectures. We call this
5611 after all the input files have been opened, but before we do any
5612 other processing, so that any operations merge_private_bfd_data
5613 does on the output file will be known during the rest of the
5617 /* Handle .exports instead of a version script if we're told to do so. */
5618 if (command_line
.version_exports_section
)
5619 lang_do_version_exports_section ();
5621 /* Build all sets based on the information gathered from the input
5623 ldctor_build_sets ();
5625 /* Remove unreferenced sections if asked to. */
5626 lang_gc_sections ();
5628 /* Size up the common data. */
5631 /* Update wild statements. */
5632 update_wild_statements (statement_list
.head
);
5634 /* Run through the contours of the script and attach input sections
5635 to the correct output sections. */
5636 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
5638 /* Find any sections not attached explicitly and handle them. */
5639 lang_place_orphans ();
5641 if (! link_info
.relocatable
)
5645 /* Merge SEC_MERGE sections. This has to be done after GC of
5646 sections, so that GCed sections are not merged, but before
5647 assigning dynamic symbols, since removing whole input sections
5649 bfd_merge_sections (output_bfd
, &link_info
);
5651 /* Look for a text section and set the readonly attribute in it. */
5652 found
= bfd_get_section_by_name (output_bfd
, ".text");
5656 if (config
.text_read_only
)
5657 found
->flags
|= SEC_READONLY
;
5659 found
->flags
&= ~SEC_READONLY
;
5663 /* Do anything special before sizing sections. This is where ELF
5664 and other back-ends size dynamic sections. */
5665 ldemul_before_allocation ();
5667 /* We must record the program headers before we try to fix the
5668 section positions, since they will affect SIZEOF_HEADERS. */
5669 lang_record_phdrs ();
5671 /* Size up the sections. */
5672 lang_size_sections (NULL
, !command_line
.relax
);
5674 /* Now run around and relax if we can. */
5675 if (command_line
.relax
)
5677 /* We may need more than one relaxation pass. */
5678 int i
= link_info
.relax_pass
;
5680 /* The backend can use it to determine the current pass. */
5681 link_info
.relax_pass
= 0;
5686 link_info
.relax_pass
++;
5689 /* Final extra sizing to report errors. */
5690 lang_do_assignments ();
5691 lang_reset_memory_regions ();
5692 lang_size_sections (NULL
, TRUE
);
5695 /* See if anything special should be done now we know how big
5697 ldemul_after_allocation ();
5699 /* Fix any .startof. or .sizeof. symbols. */
5700 lang_set_startof ();
5702 /* Do all the assignments, now that we know the final resting places
5703 of all the symbols. */
5705 lang_do_assignments ();
5709 /* Make sure that the section addresses make sense. */
5710 if (! link_info
.relocatable
5711 && command_line
.check_section_addresses
)
5712 lang_check_section_addresses ();
5717 /* EXPORTED TO YACC */
5720 lang_add_wild (struct wildcard_spec
*filespec
,
5721 struct wildcard_list
*section_list
,
5722 bfd_boolean keep_sections
)
5724 struct wildcard_list
*curr
, *next
;
5725 lang_wild_statement_type
*new;
5727 /* Reverse the list as the parser puts it back to front. */
5728 for (curr
= section_list
, section_list
= NULL
;
5730 section_list
= curr
, curr
= next
)
5732 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
5733 placed_commons
= TRUE
;
5736 curr
->next
= section_list
;
5739 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
5741 if (strcmp (filespec
->name
, "*") == 0)
5742 filespec
->name
= NULL
;
5743 else if (! wildcardp (filespec
->name
))
5744 lang_has_input_file
= TRUE
;
5747 new = new_stat (lang_wild_statement
, stat_ptr
);
5748 new->filename
= NULL
;
5749 new->filenames_sorted
= FALSE
;
5750 if (filespec
!= NULL
)
5752 new->filename
= filespec
->name
;
5753 new->filenames_sorted
= filespec
->sorted
== by_name
;
5755 new->section_list
= section_list
;
5756 new->keep_sections
= keep_sections
;
5757 lang_list_init (&new->children
);
5758 analyze_walk_wild_section_handler (new);
5762 lang_section_start (const char *name
, etree_type
*address
,
5763 const segment_type
*segment
)
5765 lang_address_statement_type
*ad
;
5767 ad
= new_stat (lang_address_statement
, stat_ptr
);
5768 ad
->section_name
= name
;
5769 ad
->address
= address
;
5770 ad
->segment
= segment
;
5773 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5774 because of a -e argument on the command line, or zero if this is
5775 called by ENTRY in a linker script. Command line arguments take
5779 lang_add_entry (const char *name
, bfd_boolean cmdline
)
5781 if (entry_symbol
.name
== NULL
5783 || ! entry_from_cmdline
)
5785 entry_symbol
.name
= name
;
5786 entry_from_cmdline
= cmdline
;
5790 /* Set the default start symbol to NAME. .em files should use this,
5791 not lang_add_entry, to override the use of "start" if neither the
5792 linker script nor the command line specifies an entry point. NAME
5793 must be permanently allocated. */
5795 lang_default_entry (const char *name
)
5797 entry_symbol_default
= name
;
5801 lang_add_target (const char *name
)
5803 lang_target_statement_type
*new;
5805 new = new_stat (lang_target_statement
, stat_ptr
);
5810 lang_add_map (const char *name
)
5817 map_option_f
= TRUE
;
5825 lang_add_fill (fill_type
*fill
)
5827 lang_fill_statement_type
*new;
5829 new = new_stat (lang_fill_statement
, stat_ptr
);
5834 lang_add_data (int type
, union etree_union
*exp
)
5836 lang_data_statement_type
*new;
5838 new = new_stat (lang_data_statement
, stat_ptr
);
5843 /* Create a new reloc statement. RELOC is the BFD relocation type to
5844 generate. HOWTO is the corresponding howto structure (we could
5845 look this up, but the caller has already done so). SECTION is the
5846 section to generate a reloc against, or NAME is the name of the
5847 symbol to generate a reloc against. Exactly one of SECTION and
5848 NAME must be NULL. ADDEND is an expression for the addend. */
5851 lang_add_reloc (bfd_reloc_code_real_type reloc
,
5852 reloc_howto_type
*howto
,
5855 union etree_union
*addend
)
5857 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
5861 p
->section
= section
;
5863 p
->addend_exp
= addend
;
5865 p
->addend_value
= 0;
5866 p
->output_section
= NULL
;
5867 p
->output_offset
= 0;
5870 lang_assignment_statement_type
*
5871 lang_add_assignment (etree_type
*exp
)
5873 lang_assignment_statement_type
*new;
5875 new = new_stat (lang_assignment_statement
, stat_ptr
);
5881 lang_add_attribute (enum statement_enum attribute
)
5883 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
5887 lang_startup (const char *name
)
5889 if (startup_file
!= NULL
)
5891 einfo (_("%P%F: multiple STARTUP files\n"));
5893 first_file
->filename
= name
;
5894 first_file
->local_sym_name
= name
;
5895 first_file
->real
= TRUE
;
5897 startup_file
= name
;
5901 lang_float (bfd_boolean maybe
)
5903 lang_float_flag
= maybe
;
5907 /* Work out the load- and run-time regions from a script statement, and
5908 store them in *LMA_REGION and *REGION respectively.
5910 MEMSPEC is the name of the run-time region, or the value of
5911 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5912 LMA_MEMSPEC is the name of the load-time region, or null if the
5913 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5914 had an explicit load address.
5916 It is an error to specify both a load region and a load address. */
5919 lang_get_regions (lang_memory_region_type
**region
,
5920 lang_memory_region_type
**lma_region
,
5921 const char *memspec
,
5922 const char *lma_memspec
,
5923 bfd_boolean have_lma
,
5924 bfd_boolean have_vma
)
5926 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
5928 /* If no runtime region or VMA has been specified, but the load region
5929 has been specified, then use the load region for the runtime region
5931 if (lma_memspec
!= NULL
5933 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
5934 *region
= *lma_region
;
5936 *region
= lang_memory_region_lookup (memspec
, FALSE
);
5938 if (have_lma
&& lma_memspec
!= 0)
5939 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5943 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
5944 lang_output_section_phdr_list
*phdrs
,
5945 const char *lma_memspec
)
5947 lang_get_regions (¤t_section
->region
,
5948 ¤t_section
->lma_region
,
5949 memspec
, lma_memspec
,
5950 current_section
->load_base
!= NULL
,
5951 current_section
->addr_tree
!= NULL
);
5952 current_section
->fill
= fill
;
5953 current_section
->phdrs
= phdrs
;
5954 stat_ptr
= &statement_list
;
5957 /* Create an absolute symbol with the given name with the value of the
5958 address of first byte of the section named.
5960 If the symbol already exists, then do nothing. */
5963 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
5965 struct bfd_link_hash_entry
*h
;
5967 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5969 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5971 if (h
->type
== bfd_link_hash_new
5972 || h
->type
== bfd_link_hash_undefined
)
5976 h
->type
= bfd_link_hash_defined
;
5978 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5982 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
5984 h
->u
.def
.section
= bfd_abs_section_ptr
;
5988 /* Create an absolute symbol with the given name with the value of the
5989 address of the first byte after the end of the section named.
5991 If the symbol already exists, then do nothing. */
5994 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
5996 struct bfd_link_hash_entry
*h
;
5998 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6000 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6002 if (h
->type
== bfd_link_hash_new
6003 || h
->type
== bfd_link_hash_undefined
)
6007 h
->type
= bfd_link_hash_defined
;
6009 sec
= bfd_get_section_by_name (output_bfd
, secname
);
6013 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
6014 + TO_ADDR (sec
->size
));
6016 h
->u
.def
.section
= bfd_abs_section_ptr
;
6021 lang_statement_append (lang_statement_list_type
*list
,
6022 lang_statement_union_type
*element
,
6023 lang_statement_union_type
**field
)
6025 *(list
->tail
) = element
;
6029 /* Set the output format type. -oformat overrides scripts. */
6032 lang_add_output_format (const char *format
,
6037 if (output_target
== NULL
|| !from_script
)
6039 if (command_line
.endian
== ENDIAN_BIG
6042 else if (command_line
.endian
== ENDIAN_LITTLE
6046 output_target
= format
;
6050 /* Enter a group. This creates a new lang_group_statement, and sets
6051 stat_ptr to build new statements within the group. */
6054 lang_enter_group (void)
6056 lang_group_statement_type
*g
;
6058 g
= new_stat (lang_group_statement
, stat_ptr
);
6059 lang_list_init (&g
->children
);
6060 stat_ptr
= &g
->children
;
6063 /* Leave a group. This just resets stat_ptr to start writing to the
6064 regular list of statements again. Note that this will not work if
6065 groups can occur inside anything else which can adjust stat_ptr,
6066 but currently they can't. */
6069 lang_leave_group (void)
6071 stat_ptr
= &statement_list
;
6074 /* Add a new program header. This is called for each entry in a PHDRS
6075 command in a linker script. */
6078 lang_new_phdr (const char *name
,
6080 bfd_boolean filehdr
,
6085 struct lang_phdr
*n
, **pp
;
6087 n
= stat_alloc (sizeof (struct lang_phdr
));
6090 n
->type
= exp_get_value_int (type
, 0, "program header type");
6091 n
->filehdr
= filehdr
;
6096 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6101 /* Record the program header information in the output BFD. FIXME: We
6102 should not be calling an ELF specific function here. */
6105 lang_record_phdrs (void)
6109 lang_output_section_phdr_list
*last
;
6110 struct lang_phdr
*l
;
6111 lang_output_section_statement_type
*os
;
6114 secs
= xmalloc (alc
* sizeof (asection
*));
6116 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
6123 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6127 lang_output_section_phdr_list
*pl
;
6129 if (os
->constraint
== -1)
6137 if (os
->sectype
== noload_section
6138 || os
->bfd_section
== NULL
6139 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6144 if (os
->bfd_section
== NULL
)
6147 for (; pl
!= NULL
; pl
= pl
->next
)
6149 if (strcmp (pl
->name
, l
->name
) == 0)
6154 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6156 secs
[c
] = os
->bfd_section
;
6163 if (l
->flags
== NULL
)
6166 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6171 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6173 if (! bfd_record_phdr (output_bfd
, l
->type
,
6174 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6175 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6176 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6181 /* Make sure all the phdr assignments succeeded. */
6182 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6186 lang_output_section_phdr_list
*pl
;
6188 if (os
->constraint
== -1
6189 || os
->bfd_section
== NULL
)
6192 for (pl
= os
->phdrs
;
6195 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6196 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6197 os
->name
, pl
->name
);
6201 /* Record a list of sections which may not be cross referenced. */
6204 lang_add_nocrossref (lang_nocrossref_type
*l
)
6206 struct lang_nocrossrefs
*n
;
6208 n
= xmalloc (sizeof *n
);
6209 n
->next
= nocrossref_list
;
6211 nocrossref_list
= n
;
6213 /* Set notice_all so that we get informed about all symbols. */
6214 link_info
.notice_all
= TRUE
;
6217 /* Overlay handling. We handle overlays with some static variables. */
6219 /* The overlay virtual address. */
6220 static etree_type
*overlay_vma
;
6221 /* And subsection alignment. */
6222 static etree_type
*overlay_subalign
;
6224 /* An expression for the maximum section size seen so far. */
6225 static etree_type
*overlay_max
;
6227 /* A list of all the sections in this overlay. */
6229 struct overlay_list
{
6230 struct overlay_list
*next
;
6231 lang_output_section_statement_type
*os
;
6234 static struct overlay_list
*overlay_list
;
6236 /* Start handling an overlay. */
6239 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6241 /* The grammar should prevent nested overlays from occurring. */
6242 ASSERT (overlay_vma
== NULL
6243 && overlay_subalign
== NULL
6244 && overlay_max
== NULL
);
6246 overlay_vma
= vma_expr
;
6247 overlay_subalign
= subalign
;
6250 /* Start a section in an overlay. We handle this by calling
6251 lang_enter_output_section_statement with the correct VMA.
6252 lang_leave_overlay sets up the LMA and memory regions. */
6255 lang_enter_overlay_section (const char *name
)
6257 struct overlay_list
*n
;
6260 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
6261 0, overlay_subalign
, 0, 0);
6263 /* If this is the first section, then base the VMA of future
6264 sections on this one. This will work correctly even if `.' is
6265 used in the addresses. */
6266 if (overlay_list
== NULL
)
6267 overlay_vma
= exp_nameop (ADDR
, name
);
6269 /* Remember the section. */
6270 n
= xmalloc (sizeof *n
);
6271 n
->os
= current_section
;
6272 n
->next
= overlay_list
;
6275 size
= exp_nameop (SIZEOF
, name
);
6277 /* Arrange to work out the maximum section end address. */
6278 if (overlay_max
== NULL
)
6281 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6284 /* Finish a section in an overlay. There isn't any special to do
6288 lang_leave_overlay_section (fill_type
*fill
,
6289 lang_output_section_phdr_list
*phdrs
)
6296 name
= current_section
->name
;
6298 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6299 region and that no load-time region has been specified. It doesn't
6300 really matter what we say here, since lang_leave_overlay will
6302 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6304 /* Define the magic symbols. */
6306 clean
= xmalloc (strlen (name
) + 1);
6308 for (s1
= name
; *s1
!= '\0'; s1
++)
6309 if (ISALNUM (*s1
) || *s1
== '_')
6313 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6314 sprintf (buf
, "__load_start_%s", clean
);
6315 lang_add_assignment (exp_assop ('=', buf
,
6316 exp_nameop (LOADADDR
, name
)));
6318 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6319 sprintf (buf
, "__load_stop_%s", clean
);
6320 lang_add_assignment (exp_assop ('=', buf
,
6322 exp_nameop (LOADADDR
, name
),
6323 exp_nameop (SIZEOF
, name
))));
6328 /* Finish an overlay. If there are any overlay wide settings, this
6329 looks through all the sections in the overlay and sets them. */
6332 lang_leave_overlay (etree_type
*lma_expr
,
6335 const char *memspec
,
6336 lang_output_section_phdr_list
*phdrs
,
6337 const char *lma_memspec
)
6339 lang_memory_region_type
*region
;
6340 lang_memory_region_type
*lma_region
;
6341 struct overlay_list
*l
;
6342 lang_nocrossref_type
*nocrossref
;
6344 lang_get_regions (®ion
, &lma_region
,
6345 memspec
, lma_memspec
,
6346 lma_expr
!= NULL
, FALSE
);
6350 /* After setting the size of the last section, set '.' to end of the
6352 if (overlay_list
!= NULL
)
6353 overlay_list
->os
->update_dot_tree
6354 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6359 struct overlay_list
*next
;
6361 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6364 l
->os
->region
= region
;
6365 l
->os
->lma_region
= lma_region
;
6367 /* The first section has the load address specified in the
6368 OVERLAY statement. The rest are worked out from that.
6369 The base address is not needed (and should be null) if
6370 an LMA region was specified. */
6372 l
->os
->load_base
= lma_expr
;
6374 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6375 l
->os
->phdrs
= phdrs
;
6379 lang_nocrossref_type
*nc
;
6381 nc
= xmalloc (sizeof *nc
);
6382 nc
->name
= l
->os
->name
;
6383 nc
->next
= nocrossref
;
6392 if (nocrossref
!= NULL
)
6393 lang_add_nocrossref (nocrossref
);
6396 overlay_list
= NULL
;
6400 /* Version handling. This is only useful for ELF. */
6402 /* This global variable holds the version tree that we build. */
6404 struct bfd_elf_version_tree
*lang_elf_version_info
;
6406 /* If PREV is NULL, return first version pattern matching particular symbol.
6407 If PREV is non-NULL, return first version pattern matching particular
6408 symbol after PREV (previously returned by lang_vers_match). */
6410 static struct bfd_elf_version_expr
*
6411 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6412 struct bfd_elf_version_expr
*prev
,
6415 const char *cxx_sym
= sym
;
6416 const char *java_sym
= sym
;
6417 struct bfd_elf_version_expr
*expr
= NULL
;
6419 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6421 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6425 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6427 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6432 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6434 struct bfd_elf_version_expr e
;
6436 switch (prev
? prev
->mask
: 0)
6439 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6442 expr
= htab_find (head
->htab
, &e
);
6443 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
6444 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
6450 case BFD_ELF_VERSION_C_TYPE
:
6451 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6454 expr
= htab_find (head
->htab
, &e
);
6455 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
6456 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6462 case BFD_ELF_VERSION_CXX_TYPE
:
6463 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6465 e
.symbol
= java_sym
;
6466 expr
= htab_find (head
->htab
, &e
);
6467 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
6468 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6479 /* Finally, try the wildcards. */
6480 if (prev
== NULL
|| prev
->symbol
)
6481 expr
= head
->remaining
;
6484 for (; expr
; expr
= expr
->next
)
6491 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
6494 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6496 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6500 if (fnmatch (expr
->pattern
, s
, 0) == 0)
6506 free ((char *) cxx_sym
);
6507 if (java_sym
!= sym
)
6508 free ((char *) java_sym
);
6512 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6513 return a string pointing to the symbol name. */
6516 realsymbol (const char *pattern
)
6519 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
6520 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
6522 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
6524 /* It is a glob pattern only if there is no preceding
6526 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
6534 /* Remove the preceding backslash. */
6541 backslash
= *p
== '\\';
6556 /* This is called for each variable name or match expression. NEW is
6557 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6558 pattern to be matched against symbol names. */
6560 struct bfd_elf_version_expr
*
6561 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
6564 bfd_boolean literal_p
)
6566 struct bfd_elf_version_expr
*ret
;
6568 ret
= xmalloc (sizeof *ret
);
6570 ret
->pattern
= literal_p
? NULL
: new;
6573 ret
->symbol
= literal_p
? new : realsymbol (new);
6575 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
6576 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6577 else if (strcasecmp (lang
, "C++") == 0)
6578 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
6579 else if (strcasecmp (lang
, "Java") == 0)
6580 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
6583 einfo (_("%X%P: unknown language `%s' in version information\n"),
6585 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6588 return ldemul_new_vers_pattern (ret
);
6591 /* This is called for each set of variable names and match
6594 struct bfd_elf_version_tree
*
6595 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
6596 struct bfd_elf_version_expr
*locals
)
6598 struct bfd_elf_version_tree
*ret
;
6600 ret
= xcalloc (1, sizeof *ret
);
6601 ret
->globals
.list
= globals
;
6602 ret
->locals
.list
= locals
;
6603 ret
->match
= lang_vers_match
;
6604 ret
->name_indx
= (unsigned int) -1;
6608 /* This static variable keeps track of version indices. */
6610 static int version_index
;
6613 version_expr_head_hash (const void *p
)
6615 const struct bfd_elf_version_expr
*e
= p
;
6617 return htab_hash_string (e
->symbol
);
6621 version_expr_head_eq (const void *p1
, const void *p2
)
6623 const struct bfd_elf_version_expr
*e1
= p1
;
6624 const struct bfd_elf_version_expr
*e2
= p2
;
6626 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
6630 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
6633 struct bfd_elf_version_expr
*e
, *next
;
6634 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
6636 for (e
= head
->list
; e
; e
= e
->next
)
6640 head
->mask
|= e
->mask
;
6645 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
6646 version_expr_head_eq
, NULL
);
6647 list_loc
= &head
->list
;
6648 remaining_loc
= &head
->remaining
;
6649 for (e
= head
->list
; e
; e
= next
)
6655 remaining_loc
= &e
->next
;
6659 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
6663 struct bfd_elf_version_expr
*e1
, *last
;
6669 if (e1
->mask
== e
->mask
)
6677 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
6681 /* This is a duplicate. */
6682 /* FIXME: Memory leak. Sometimes pattern is not
6683 xmalloced alone, but in larger chunk of memory. */
6684 /* free (e->symbol); */
6689 e
->next
= last
->next
;
6697 list_loc
= &e
->next
;
6701 *remaining_loc
= NULL
;
6702 *list_loc
= head
->remaining
;
6705 head
->remaining
= head
->list
;
6708 /* This is called when we know the name and dependencies of the
6712 lang_register_vers_node (const char *name
,
6713 struct bfd_elf_version_tree
*version
,
6714 struct bfd_elf_version_deps
*deps
)
6716 struct bfd_elf_version_tree
*t
, **pp
;
6717 struct bfd_elf_version_expr
*e1
;
6722 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
6723 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
6725 einfo (_("%X%P: anonymous version tag cannot be combined"
6726 " with other version tags\n"));
6731 /* Make sure this node has a unique name. */
6732 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6733 if (strcmp (t
->name
, name
) == 0)
6734 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
6736 lang_finalize_version_expr_head (&version
->globals
);
6737 lang_finalize_version_expr_head (&version
->locals
);
6739 /* Check the global and local match names, and make sure there
6740 aren't any duplicates. */
6742 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
6744 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6746 struct bfd_elf_version_expr
*e2
;
6748 if (t
->locals
.htab
&& e1
->symbol
)
6750 e2
= htab_find (t
->locals
.htab
, e1
);
6751 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6753 if (e1
->mask
== e2
->mask
)
6754 einfo (_("%X%P: duplicate expression `%s'"
6755 " in version information\n"), e1
->symbol
);
6759 else if (!e1
->symbol
)
6760 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6761 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6762 && e1
->mask
== e2
->mask
)
6763 einfo (_("%X%P: duplicate expression `%s'"
6764 " in version information\n"), e1
->pattern
);
6768 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
6770 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6772 struct bfd_elf_version_expr
*e2
;
6774 if (t
->globals
.htab
&& e1
->symbol
)
6776 e2
= htab_find (t
->globals
.htab
, e1
);
6777 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6779 if (e1
->mask
== e2
->mask
)
6780 einfo (_("%X%P: duplicate expression `%s'"
6781 " in version information\n"),
6786 else if (!e1
->symbol
)
6787 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6788 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6789 && e1
->mask
== e2
->mask
)
6790 einfo (_("%X%P: duplicate expression `%s'"
6791 " in version information\n"), e1
->pattern
);
6795 version
->deps
= deps
;
6796 version
->name
= name
;
6797 if (name
[0] != '\0')
6800 version
->vernum
= version_index
;
6803 version
->vernum
= 0;
6805 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6810 /* This is called when we see a version dependency. */
6812 struct bfd_elf_version_deps
*
6813 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
6815 struct bfd_elf_version_deps
*ret
;
6816 struct bfd_elf_version_tree
*t
;
6818 ret
= xmalloc (sizeof *ret
);
6821 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6823 if (strcmp (t
->name
, name
) == 0)
6825 ret
->version_needed
= t
;
6830 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
6836 lang_do_version_exports_section (void)
6838 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
6840 LANG_FOR_EACH_INPUT_STATEMENT (is
)
6842 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
6850 contents
= xmalloc (len
);
6851 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
6852 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
6855 while (p
< contents
+ len
)
6857 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
6858 p
= strchr (p
, '\0') + 1;
6861 /* Do not free the contents, as we used them creating the regex. */
6863 /* Do not include this section in the link. */
6864 sec
->flags
|= SEC_EXCLUDE
;
6867 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
6868 lang_register_vers_node (command_line
.version_exports_section
,
6869 lang_new_vers_node (greg
, lreg
), NULL
);
6873 lang_add_unique (const char *name
)
6875 struct unique_sections
*ent
;
6877 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
6878 if (strcmp (ent
->name
, name
) == 0)
6881 ent
= xmalloc (sizeof *ent
);
6882 ent
->name
= xstrdup (name
);
6883 ent
->next
= unique_section_list
;
6884 unique_section_list
= ent
;