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
,
1865 if (s
->bfd_section
!= NULL
)
1868 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1869 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1871 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
1872 if (s
->bfd_section
== NULL
)
1873 s
->bfd_section
= bfd_make_section_with_flags (output_bfd
, s
->name
,
1875 if (s
->bfd_section
== NULL
)
1877 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1878 output_bfd
->xvec
->name
, s
->name
);
1880 s
->bfd_section
->output_section
= s
->bfd_section
;
1881 s
->bfd_section
->output_offset
= 0;
1883 if (!command_line
.reduce_memory_overheads
)
1885 fat_section_userdata_type
*new
1886 = stat_alloc (sizeof (fat_section_userdata_type
));
1887 memset (new, 0, sizeof (fat_section_userdata_type
));
1888 get_userdata (s
->bfd_section
) = new;
1891 /* If there is a base address, make sure that any sections it might
1892 mention are initialized. */
1893 if (s
->addr_tree
!= NULL
)
1894 exp_init_os (s
->addr_tree
);
1896 if (s
->load_base
!= NULL
)
1897 exp_init_os (s
->load_base
);
1899 /* If supplied an alignment, set it. */
1900 if (s
->section_alignment
!= -1)
1901 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1904 bfd_init_private_section_data (isec
->owner
, isec
,
1905 output_bfd
, s
->bfd_section
,
1909 /* Make sure that all output sections mentioned in an expression are
1913 exp_init_os (etree_type
*exp
)
1915 switch (exp
->type
.node_class
)
1919 exp_init_os (exp
->assign
.src
);
1923 exp_init_os (exp
->binary
.lhs
);
1924 exp_init_os (exp
->binary
.rhs
);
1928 exp_init_os (exp
->trinary
.cond
);
1929 exp_init_os (exp
->trinary
.lhs
);
1930 exp_init_os (exp
->trinary
.rhs
);
1934 exp_init_os (exp
->assert_s
.child
);
1938 exp_init_os (exp
->unary
.child
);
1942 switch (exp
->type
.node_code
)
1948 lang_output_section_statement_type
*os
;
1950 os
= lang_output_section_find (exp
->name
.name
);
1951 if (os
!= NULL
&& os
->bfd_section
== NULL
)
1952 init_os (os
, NULL
, 0);
1963 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
1965 lang_input_statement_type
*entry
= data
;
1967 /* If we are only reading symbols from this object, then we want to
1968 discard all sections. */
1969 if (entry
->just_syms_flag
)
1971 bfd_link_just_syms (abfd
, sec
, &link_info
);
1975 if (!(abfd
->flags
& DYNAMIC
))
1976 bfd_section_already_linked (abfd
, sec
);
1979 /* The wild routines.
1981 These expand statements like *(.text) and foo.o to a list of
1982 explicit actions, like foo.o(.text), bar.o(.text) and
1983 foo.o(.text, .data). */
1985 /* Add SECTION to the output section OUTPUT. Do this by creating a
1986 lang_input_section statement which is placed at PTR. FILE is the
1987 input file which holds SECTION. */
1990 lang_add_section (lang_statement_list_type
*ptr
,
1992 lang_output_section_statement_type
*output
)
1994 flagword flags
= section
->flags
;
1995 bfd_boolean discard
;
1997 /* Discard sections marked with SEC_EXCLUDE. */
1998 discard
= (flags
& SEC_EXCLUDE
) != 0;
2000 /* Discard input sections which are assigned to a section named
2001 DISCARD_SECTION_NAME. */
2002 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
2005 /* Discard debugging sections if we are stripping debugging
2007 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
2008 && (flags
& SEC_DEBUGGING
) != 0)
2013 if (section
->output_section
== NULL
)
2015 /* This prevents future calls from assigning this section. */
2016 section
->output_section
= bfd_abs_section_ptr
;
2021 if (section
->output_section
== NULL
)
2024 lang_input_section_type
*new;
2027 flags
= section
->flags
;
2029 /* We don't copy the SEC_NEVER_LOAD flag from an input section
2030 to an output section, because we want to be able to include a
2031 SEC_NEVER_LOAD section in the middle of an otherwise loaded
2032 section (I don't know why we want to do this, but we do).
2033 build_link_order in ldwrite.c handles this case by turning
2034 the embedded SEC_NEVER_LOAD section into a fill. */
2036 flags
&= ~ SEC_NEVER_LOAD
;
2038 switch (output
->sectype
)
2040 case normal_section
:
2042 case noalloc_section
:
2043 flags
&= ~SEC_ALLOC
;
2045 case noload_section
:
2047 flags
|= SEC_NEVER_LOAD
;
2051 if (output
->bfd_section
== NULL
)
2052 init_os (output
, section
, flags
);
2054 first
= ! output
->bfd_section
->linker_has_input
;
2055 output
->bfd_section
->linker_has_input
= 1;
2057 if (!link_info
.relocatable
2058 && !stripped_excluded_sections
)
2060 asection
*s
= output
->bfd_section
->map_tail
.s
;
2061 output
->bfd_section
->map_tail
.s
= section
;
2062 section
->map_head
.s
= NULL
;
2063 section
->map_tail
.s
= s
;
2065 s
->map_head
.s
= section
;
2067 output
->bfd_section
->map_head
.s
= section
;
2070 /* Add a section reference to the list. */
2071 new = new_stat (lang_input_section
, ptr
);
2073 new->section
= section
;
2074 section
->output_section
= output
->bfd_section
;
2076 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
2077 already been processed. One reason to do this is that on pe
2078 format targets, .text$foo sections go into .text and it's odd
2079 to see .text with SEC_LINK_ONCE set. */
2081 if (! link_info
.relocatable
)
2082 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
2084 /* If this is not the first input section, and the SEC_READONLY
2085 flag is not currently set, then don't set it just because the
2086 input section has it set. */
2088 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
2089 flags
&= ~ SEC_READONLY
;
2091 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
2093 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
2094 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
2095 || ((flags
& SEC_MERGE
)
2096 && output
->bfd_section
->entsize
!= section
->entsize
)))
2098 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2099 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
2102 output
->bfd_section
->flags
|= flags
;
2104 if (flags
& SEC_MERGE
)
2105 output
->bfd_section
->entsize
= section
->entsize
;
2107 /* If SEC_READONLY is not set in the input section, then clear
2108 it from the output section. */
2109 if ((section
->flags
& SEC_READONLY
) == 0)
2110 output
->bfd_section
->flags
&= ~SEC_READONLY
;
2112 /* Copy over SEC_SMALL_DATA. */
2113 if (section
->flags
& SEC_SMALL_DATA
)
2114 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
2116 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
2117 output
->bfd_section
->alignment_power
= section
->alignment_power
;
2119 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
2120 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
2122 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2123 /* FIXME: This value should really be obtained from the bfd... */
2124 output
->block_value
= 128;
2129 /* Handle wildcard sorting. This returns the lang_input_section which
2130 should follow the one we are going to create for SECTION and FILE,
2131 based on the sorting requirements of WILD. It returns NULL if the
2132 new section should just go at the end of the current list. */
2134 static lang_statement_union_type
*
2135 wild_sort (lang_wild_statement_type
*wild
,
2136 struct wildcard_list
*sec
,
2137 lang_input_statement_type
*file
,
2140 const char *section_name
;
2141 lang_statement_union_type
*l
;
2143 if (!wild
->filenames_sorted
2144 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2147 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2148 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2150 lang_input_section_type
*ls
;
2152 if (l
->header
.type
!= lang_input_section_enum
)
2154 ls
= &l
->input_section
;
2156 /* Sorting by filename takes precedence over sorting by section
2159 if (wild
->filenames_sorted
)
2161 const char *fn
, *ln
;
2165 /* The PE support for the .idata section as generated by
2166 dlltool assumes that files will be sorted by the name of
2167 the archive and then the name of the file within the
2170 if (file
->the_bfd
!= NULL
2171 && bfd_my_archive (file
->the_bfd
) != NULL
)
2173 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2178 fn
= file
->filename
;
2182 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2184 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2189 ln
= ls
->section
->owner
->filename
;
2193 i
= strcmp (fn
, ln
);
2202 fn
= file
->filename
;
2204 ln
= ls
->section
->owner
->filename
;
2206 i
= strcmp (fn
, ln
);
2214 /* Here either the files are not sorted by name, or we are
2215 looking at the sections for this file. */
2217 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2218 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2225 /* Expand a wild statement for a particular FILE. SECTION may be
2226 NULL, in which case it is a wild card. */
2229 output_section_callback (lang_wild_statement_type
*ptr
,
2230 struct wildcard_list
*sec
,
2232 lang_input_statement_type
*file
,
2235 lang_statement_union_type
*before
;
2237 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2238 if (unique_section_p (section
))
2241 before
= wild_sort (ptr
, sec
, file
, section
);
2243 /* Here BEFORE points to the lang_input_section which
2244 should follow the one we are about to add. If BEFORE
2245 is NULL, then the section should just go at the end
2246 of the current list. */
2249 lang_add_section (&ptr
->children
, section
,
2250 (lang_output_section_statement_type
*) output
);
2253 lang_statement_list_type list
;
2254 lang_statement_union_type
**pp
;
2256 lang_list_init (&list
);
2257 lang_add_section (&list
, section
,
2258 (lang_output_section_statement_type
*) output
);
2260 /* If we are discarding the section, LIST.HEAD will
2262 if (list
.head
!= NULL
)
2264 ASSERT (list
.head
->header
.next
== NULL
);
2266 for (pp
= &ptr
->children
.head
;
2268 pp
= &(*pp
)->header
.next
)
2269 ASSERT (*pp
!= NULL
);
2271 list
.head
->header
.next
= *pp
;
2277 /* Check if all sections in a wild statement for a particular FILE
2281 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2282 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2284 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2287 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2288 if (unique_section_p (section
))
2291 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2292 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2295 /* This is passed a file name which must have been seen already and
2296 added to the statement tree. We will see if it has been opened
2297 already and had its symbols read. If not then we'll read it. */
2299 static lang_input_statement_type
*
2300 lookup_name (const char *name
)
2302 lang_input_statement_type
*search
;
2304 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2306 search
= (lang_input_statement_type
*) search
->next_real_file
)
2308 /* Use the local_sym_name as the name of the file that has
2309 already been loaded as filename might have been transformed
2310 via the search directory lookup mechanism. */
2311 const char *filename
= search
->local_sym_name
;
2313 if (filename
!= NULL
2314 && strcmp (filename
, name
) == 0)
2319 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2320 default_target
, FALSE
);
2322 /* If we have already added this file, or this file is not real
2323 don't add this file. */
2324 if (search
->loaded
|| !search
->real
)
2327 if (! load_symbols (search
, NULL
))
2333 /* Save LIST as a list of libraries whose symbols should not be exported. */
2338 struct excluded_lib
*next
;
2340 static struct excluded_lib
*excluded_libs
;
2343 add_excluded_libs (const char *list
)
2345 const char *p
= list
, *end
;
2349 struct excluded_lib
*entry
;
2350 end
= strpbrk (p
, ",:");
2352 end
= p
+ strlen (p
);
2353 entry
= xmalloc (sizeof (*entry
));
2354 entry
->next
= excluded_libs
;
2355 entry
->name
= xmalloc (end
- p
+ 1);
2356 memcpy (entry
->name
, p
, end
- p
);
2357 entry
->name
[end
- p
] = '\0';
2358 excluded_libs
= entry
;
2366 check_excluded_libs (bfd
*abfd
)
2368 struct excluded_lib
*lib
= excluded_libs
;
2372 int len
= strlen (lib
->name
);
2373 const char *filename
= lbasename (abfd
->filename
);
2375 if (strcmp (lib
->name
, "ALL") == 0)
2377 abfd
->no_export
= TRUE
;
2381 if (strncmp (lib
->name
, filename
, len
) == 0
2382 && (filename
[len
] == '\0'
2383 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2384 && filename
[len
+ 2] == '\0')))
2386 abfd
->no_export
= TRUE
;
2394 /* Get the symbols for an input file. */
2397 load_symbols (lang_input_statement_type
*entry
,
2398 lang_statement_list_type
*place
)
2405 ldfile_open_file (entry
);
2407 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2408 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2411 lang_statement_list_type
*hold
;
2412 bfd_boolean bad_load
= TRUE
;
2413 bfd_boolean save_ldlang_sysrooted_script
;
2414 bfd_boolean save_as_needed
, save_add_needed
;
2416 err
= bfd_get_error ();
2418 /* See if the emulation has some special knowledge. */
2419 if (ldemul_unrecognized_file (entry
))
2422 if (err
== bfd_error_file_ambiguously_recognized
)
2426 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2427 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2428 for (p
= matching
; *p
!= NULL
; p
++)
2432 else if (err
!= bfd_error_file_not_recognized
2434 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2438 bfd_close (entry
->the_bfd
);
2439 entry
->the_bfd
= NULL
;
2441 /* Try to interpret the file as a linker script. */
2442 ldfile_open_command_file (entry
->filename
);
2446 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2447 ldlang_sysrooted_script
= entry
->sysrooted
;
2448 save_as_needed
= as_needed
;
2449 as_needed
= entry
->as_needed
;
2450 save_add_needed
= add_needed
;
2451 add_needed
= entry
->add_needed
;
2453 ldfile_assumed_script
= TRUE
;
2454 parser_input
= input_script
;
2455 /* We want to use the same -Bdynamic/-Bstatic as the one for
2457 config
.dynamic_link
= entry
->dynamic
;
2459 ldfile_assumed_script
= FALSE
;
2461 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2462 as_needed
= save_as_needed
;
2463 add_needed
= save_add_needed
;
2469 if (ldemul_recognized_file (entry
))
2472 /* We don't call ldlang_add_file for an archive. Instead, the
2473 add_symbols entry point will call ldlang_add_file, via the
2474 add_archive_element callback, for each element of the archive
2476 switch (bfd_get_format (entry
->the_bfd
))
2482 ldlang_add_file (entry
);
2483 if (trace_files
|| trace_file_tries
)
2484 info_msg ("%I\n", entry
);
2488 check_excluded_libs (entry
->the_bfd
);
2490 if (entry
->whole_archive
)
2493 bfd_boolean loaded
= TRUE
;
2497 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2502 if (! bfd_check_format (member
, bfd_object
))
2504 einfo (_("%F%B: member %B in archive is not an object\n"),
2505 entry
->the_bfd
, member
);
2509 if (! ((*link_info
.callbacks
->add_archive_element
)
2510 (&link_info
, member
, "--whole-archive")))
2513 if (! bfd_link_add_symbols (member
, &link_info
))
2515 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2520 entry
->loaded
= loaded
;
2526 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2527 entry
->loaded
= TRUE
;
2529 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2531 return entry
->loaded
;
2534 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2535 may be NULL, indicating that it is a wildcard. Separate
2536 lang_input_section statements are created for each part of the
2537 expansion; they are added after the wild statement S. OUTPUT is
2538 the output section. */
2541 wild (lang_wild_statement_type
*s
,
2542 const char *target ATTRIBUTE_UNUSED
,
2543 lang_output_section_statement_type
*output
)
2545 struct wildcard_list
*sec
;
2547 if (s
->handler_data
[0]
2548 && s
->handler_data
[0]->spec
.sorted
== by_name
2549 && !s
->filenames_sorted
)
2551 lang_section_bst_type
*tree
;
2553 walk_wild (s
, output_section_callback_fast
, output
);
2555 tree
= (lang_section_bst_type
*) s
->handler_data
[1];
2557 output_section_callback_tree_to_list (s
, tree
, output
);
2558 s
->handler_data
[1] = NULL
;
2561 walk_wild (s
, output_section_callback
, output
);
2563 if (default_common_section
== NULL
)
2564 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2565 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2567 /* Remember the section that common is going to in case we
2568 later get something which doesn't know where to put it. */
2569 default_common_section
= output
;
2574 /* Return TRUE iff target is the sought target. */
2577 get_target (const bfd_target
*target
, void *data
)
2579 const char *sought
= data
;
2581 return strcmp (target
->name
, sought
) == 0;
2584 /* Like strcpy() but convert to lower case as well. */
2587 stricpy (char *dest
, char *src
)
2591 while ((c
= *src
++) != 0)
2592 *dest
++ = TOLOWER (c
);
2597 /* Remove the first occurrence of needle (if any) in haystack
2601 strcut (char *haystack
, char *needle
)
2603 haystack
= strstr (haystack
, needle
);
2609 for (src
= haystack
+ strlen (needle
); *src
;)
2610 *haystack
++ = *src
++;
2616 /* Compare two target format name strings.
2617 Return a value indicating how "similar" they are. */
2620 name_compare (char *first
, char *second
)
2626 copy1
= xmalloc (strlen (first
) + 1);
2627 copy2
= xmalloc (strlen (second
) + 1);
2629 /* Convert the names to lower case. */
2630 stricpy (copy1
, first
);
2631 stricpy (copy2
, second
);
2633 /* Remove size and endian strings from the name. */
2634 strcut (copy1
, "big");
2635 strcut (copy1
, "little");
2636 strcut (copy2
, "big");
2637 strcut (copy2
, "little");
2639 /* Return a value based on how many characters match,
2640 starting from the beginning. If both strings are
2641 the same then return 10 * their length. */
2642 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2643 if (copy1
[result
] == 0)
2655 /* Set by closest_target_match() below. */
2656 static const bfd_target
*winner
;
2658 /* Scan all the valid bfd targets looking for one that has the endianness
2659 requirement that was specified on the command line, and is the nearest
2660 match to the original output target. */
2663 closest_target_match (const bfd_target
*target
, void *data
)
2665 const bfd_target
*original
= data
;
2667 if (command_line
.endian
== ENDIAN_BIG
2668 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2671 if (command_line
.endian
== ENDIAN_LITTLE
2672 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2675 /* Must be the same flavour. */
2676 if (target
->flavour
!= original
->flavour
)
2679 /* If we have not found a potential winner yet, then record this one. */
2686 /* Oh dear, we now have two potential candidates for a successful match.
2687 Compare their names and choose the better one. */
2688 if (name_compare (target
->name
, original
->name
)
2689 > name_compare (winner
->name
, original
->name
))
2692 /* Keep on searching until wqe have checked them all. */
2696 /* Return the BFD target format of the first input file. */
2699 get_first_input_target (void)
2701 char *target
= NULL
;
2703 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2705 if (s
->header
.type
== lang_input_statement_enum
2708 ldfile_open_file (s
);
2710 if (s
->the_bfd
!= NULL
2711 && bfd_check_format (s
->the_bfd
, bfd_object
))
2713 target
= bfd_get_target (s
->the_bfd
);
2725 lang_get_output_target (void)
2729 /* Has the user told us which output format to use? */
2730 if (output_target
!= NULL
)
2731 return output_target
;
2733 /* No - has the current target been set to something other than
2735 if (current_target
!= default_target
)
2736 return current_target
;
2738 /* No - can we determine the format of the first input file? */
2739 target
= get_first_input_target ();
2743 /* Failed - use the default output target. */
2744 return default_target
;
2747 /* Open the output file. */
2750 open_output (const char *name
)
2754 output_target
= lang_get_output_target ();
2756 /* Has the user requested a particular endianness on the command
2758 if (command_line
.endian
!= ENDIAN_UNSET
)
2760 const bfd_target
*target
;
2761 enum bfd_endian desired_endian
;
2763 /* Get the chosen target. */
2764 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2766 /* If the target is not supported, we cannot do anything. */
2769 if (command_line
.endian
== ENDIAN_BIG
)
2770 desired_endian
= BFD_ENDIAN_BIG
;
2772 desired_endian
= BFD_ENDIAN_LITTLE
;
2774 /* See if the target has the wrong endianness. This should
2775 not happen if the linker script has provided big and
2776 little endian alternatives, but some scrips don't do
2778 if (target
->byteorder
!= desired_endian
)
2780 /* If it does, then see if the target provides
2781 an alternative with the correct endianness. */
2782 if (target
->alternative_target
!= NULL
2783 && (target
->alternative_target
->byteorder
== desired_endian
))
2784 output_target
= target
->alternative_target
->name
;
2787 /* Try to find a target as similar as possible to
2788 the default target, but which has the desired
2789 endian characteristic. */
2790 bfd_search_for_target (closest_target_match
,
2793 /* Oh dear - we could not find any targets that
2794 satisfy our requirements. */
2796 einfo (_("%P: warning: could not find any targets"
2797 " that match endianness requirement\n"));
2799 output_target
= winner
->name
;
2805 output
= bfd_openw (name
, output_target
);
2809 if (bfd_get_error () == bfd_error_invalid_target
)
2810 einfo (_("%P%F: target %s not found\n"), output_target
);
2812 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2815 delete_output_file_on_failure
= TRUE
;
2817 if (! bfd_set_format (output
, bfd_object
))
2818 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2819 if (! bfd_set_arch_mach (output
,
2820 ldfile_output_architecture
,
2821 ldfile_output_machine
))
2822 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2824 link_info
.hash
= bfd_link_hash_table_create (output
);
2825 if (link_info
.hash
== NULL
)
2826 einfo (_("%P%F: can not create hash table: %E\n"));
2828 bfd_set_gp_size (output
, g_switch_value
);
2833 ldlang_open_output (lang_statement_union_type
*statement
)
2835 switch (statement
->header
.type
)
2837 case lang_output_statement_enum
:
2838 ASSERT (output_bfd
== NULL
);
2839 output_bfd
= open_output (statement
->output_statement
.name
);
2840 ldemul_set_output_arch ();
2841 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2842 output_bfd
->flags
|= D_PAGED
;
2844 output_bfd
->flags
&= ~D_PAGED
;
2845 if (config
.text_read_only
)
2846 output_bfd
->flags
|= WP_TEXT
;
2848 output_bfd
->flags
&= ~WP_TEXT
;
2849 if (link_info
.traditional_format
)
2850 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2852 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2855 case lang_target_statement_enum
:
2856 current_target
= statement
->target_statement
.target
;
2863 /* Convert between addresses in bytes and sizes in octets.
2864 For currently supported targets, octets_per_byte is always a power
2865 of two, so we can use shifts. */
2866 #define TO_ADDR(X) ((X) >> opb_shift)
2867 #define TO_SIZE(X) ((X) << opb_shift)
2869 /* Support the above. */
2870 static unsigned int opb_shift
= 0;
2875 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2876 ldfile_output_machine
);
2879 while ((x
& 1) == 0)
2887 /* Open all the input files. */
2890 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2892 for (; s
!= NULL
; s
= s
->header
.next
)
2894 switch (s
->header
.type
)
2896 case lang_constructors_statement_enum
:
2897 open_input_bfds (constructor_list
.head
, force
);
2899 case lang_output_section_statement_enum
:
2900 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2902 case lang_wild_statement_enum
:
2903 /* Maybe we should load the file's symbols. */
2904 if (s
->wild_statement
.filename
2905 && ! wildcardp (s
->wild_statement
.filename
))
2906 lookup_name (s
->wild_statement
.filename
);
2907 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2909 case lang_group_statement_enum
:
2911 struct bfd_link_hash_entry
*undefs
;
2913 /* We must continually search the entries in the group
2914 until no new symbols are added to the list of undefined
2919 undefs
= link_info
.hash
->undefs_tail
;
2920 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2922 while (undefs
!= link_info
.hash
->undefs_tail
);
2925 case lang_target_statement_enum
:
2926 current_target
= s
->target_statement
.target
;
2928 case lang_input_statement_enum
:
2929 if (s
->input_statement
.real
)
2931 lang_statement_list_type add
;
2933 s
->input_statement
.target
= current_target
;
2935 /* If we are being called from within a group, and this
2936 is an archive which has already been searched, then
2937 force it to be researched unless the whole archive
2938 has been loaded already. */
2940 && !s
->input_statement
.whole_archive
2941 && s
->input_statement
.loaded
2942 && bfd_check_format (s
->input_statement
.the_bfd
,
2944 s
->input_statement
.loaded
= FALSE
;
2946 lang_list_init (&add
);
2948 if (! load_symbols (&s
->input_statement
, &add
))
2949 config
.make_executable
= FALSE
;
2951 if (add
.head
!= NULL
)
2953 *add
.tail
= s
->header
.next
;
2954 s
->header
.next
= add
.head
;
2964 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2967 lang_track_definedness (const char *name
)
2969 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
2970 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
2973 /* New-function for the definedness hash table. */
2975 static struct bfd_hash_entry
*
2976 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
2977 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
2978 const char *name ATTRIBUTE_UNUSED
)
2980 struct lang_definedness_hash_entry
*ret
2981 = (struct lang_definedness_hash_entry
*) entry
;
2984 ret
= (struct lang_definedness_hash_entry
*)
2985 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
2988 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
2990 ret
->iteration
= -1;
2994 /* Return the iteration when the definition of NAME was last updated. A
2995 value of -1 means that the symbol is not defined in the linker script
2996 or the command line, but may be defined in the linker symbol table. */
2999 lang_symbol_definition_iteration (const char *name
)
3001 struct lang_definedness_hash_entry
*defentry
3002 = (struct lang_definedness_hash_entry
*)
3003 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3005 /* We've already created this one on the presence of DEFINED in the
3006 script, so it can't be NULL unless something is borked elsewhere in
3008 if (defentry
== NULL
)
3011 return defentry
->iteration
;
3014 /* Update the definedness state of NAME. */
3017 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
3019 struct lang_definedness_hash_entry
*defentry
3020 = (struct lang_definedness_hash_entry
*)
3021 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
3023 /* We don't keep track of symbols not tested with DEFINED. */
3024 if (defentry
== NULL
)
3027 /* If the symbol was already defined, and not from an earlier statement
3028 iteration, don't update the definedness iteration, because that'd
3029 make the symbol seem defined in the linker script at this point, and
3030 it wasn't; it was defined in some object. If we do anyway, DEFINED
3031 would start to yield false before this point and the construct "sym =
3032 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
3034 if (h
->type
!= bfd_link_hash_undefined
3035 && h
->type
!= bfd_link_hash_common
3036 && h
->type
!= bfd_link_hash_new
3037 && defentry
->iteration
== -1)
3040 defentry
->iteration
= lang_statement_iteration
;
3043 /* Add the supplied name to the symbol table as an undefined reference.
3044 This is a two step process as the symbol table doesn't even exist at
3045 the time the ld command line is processed. First we put the name
3046 on a list, then, once the output file has been opened, transfer the
3047 name to the symbol table. */
3049 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
3051 #define ldlang_undef_chain_list_head entry_symbol.next
3054 ldlang_add_undef (const char *const name
)
3056 ldlang_undef_chain_list_type
*new =
3057 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
3059 new->next
= ldlang_undef_chain_list_head
;
3060 ldlang_undef_chain_list_head
= new;
3062 new->name
= xstrdup (name
);
3064 if (output_bfd
!= NULL
)
3065 insert_undefined (new->name
);
3068 /* Insert NAME as undefined in the symbol table. */
3071 insert_undefined (const char *name
)
3073 struct bfd_link_hash_entry
*h
;
3075 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
3077 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
3078 if (h
->type
== bfd_link_hash_new
)
3080 h
->type
= bfd_link_hash_undefined
;
3081 h
->u
.undef
.abfd
= NULL
;
3082 bfd_link_add_undef (link_info
.hash
, h
);
3086 /* Run through the list of undefineds created above and place them
3087 into the linker hash table as undefined symbols belonging to the
3091 lang_place_undefineds (void)
3093 ldlang_undef_chain_list_type
*ptr
;
3095 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
3096 insert_undefined (ptr
->name
);
3099 /* Check for all readonly or some readwrite sections. */
3102 check_input_sections
3103 (lang_statement_union_type
*s
,
3104 lang_output_section_statement_type
*output_section_statement
)
3106 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3108 switch (s
->header
.type
)
3110 case lang_wild_statement_enum
:
3111 walk_wild (&s
->wild_statement
, check_section_callback
,
3112 output_section_statement
);
3113 if (! output_section_statement
->all_input_readonly
)
3116 case lang_constructors_statement_enum
:
3117 check_input_sections (constructor_list
.head
,
3118 output_section_statement
);
3119 if (! output_section_statement
->all_input_readonly
)
3122 case lang_group_statement_enum
:
3123 check_input_sections (s
->group_statement
.children
.head
,
3124 output_section_statement
);
3125 if (! output_section_statement
->all_input_readonly
)
3134 /* Update wildcard statements if needed. */
3137 update_wild_statements (lang_statement_union_type
*s
)
3139 struct wildcard_list
*sec
;
3141 switch (sort_section
)
3151 for (; s
!= NULL
; s
= s
->header
.next
)
3153 switch (s
->header
.type
)
3158 case lang_wild_statement_enum
:
3159 sec
= s
->wild_statement
.section_list
;
3160 for (sec
= s
->wild_statement
.section_list
; sec
!= NULL
;
3163 switch (sec
->spec
.sorted
)
3166 sec
->spec
.sorted
= sort_section
;
3169 if (sort_section
== by_alignment
)
3170 sec
->spec
.sorted
= by_name_alignment
;
3173 if (sort_section
== by_name
)
3174 sec
->spec
.sorted
= by_alignment_name
;
3182 case lang_constructors_statement_enum
:
3183 update_wild_statements (constructor_list
.head
);
3186 case lang_output_section_statement_enum
:
3187 update_wild_statements
3188 (s
->output_section_statement
.children
.head
);
3191 case lang_group_statement_enum
:
3192 update_wild_statements (s
->group_statement
.children
.head
);
3200 /* Open input files and attach to output sections. */
3203 map_input_to_output_sections
3204 (lang_statement_union_type
*s
, const char *target
,
3205 lang_output_section_statement_type
*os
)
3209 for (; s
!= NULL
; s
= s
->header
.next
)
3211 switch (s
->header
.type
)
3213 case lang_wild_statement_enum
:
3214 wild (&s
->wild_statement
, target
, os
);
3216 case lang_constructors_statement_enum
:
3217 map_input_to_output_sections (constructor_list
.head
,
3221 case lang_output_section_statement_enum
:
3222 if (s
->output_section_statement
.constraint
)
3224 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3225 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3227 s
->output_section_statement
.all_input_readonly
= TRUE
;
3228 check_input_sections (s
->output_section_statement
.children
.head
,
3229 &s
->output_section_statement
);
3230 if ((s
->output_section_statement
.all_input_readonly
3231 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3232 || (!s
->output_section_statement
.all_input_readonly
3233 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3235 s
->output_section_statement
.constraint
= -1;
3240 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3242 &s
->output_section_statement
);
3244 case lang_output_statement_enum
:
3246 case lang_target_statement_enum
:
3247 target
= s
->target_statement
.target
;
3249 case lang_group_statement_enum
:
3250 map_input_to_output_sections (s
->group_statement
.children
.head
,
3254 case lang_data_statement_enum
:
3255 /* Make sure that any sections mentioned in the expression
3257 exp_init_os (s
->data_statement
.exp
);
3258 flags
= SEC_HAS_CONTENTS
;
3259 /* The output section gets contents, and then we inspect for
3260 any flags set in the input script which override any ALLOC. */
3261 if (!(os
->flags
& SEC_NEVER_LOAD
))
3262 flags
|= SEC_ALLOC
| SEC_LOAD
;
3263 if (os
->bfd_section
== NULL
)
3264 init_os (os
, NULL
, flags
);
3266 os
->bfd_section
->flags
|= flags
;
3268 case lang_input_section_enum
:
3270 case lang_fill_statement_enum
:
3271 case lang_object_symbols_statement_enum
:
3272 case lang_reloc_statement_enum
:
3273 case lang_padding_statement_enum
:
3274 case lang_input_statement_enum
:
3275 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3276 init_os (os
, NULL
, 0);
3278 case lang_assignment_statement_enum
:
3279 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3280 init_os (os
, NULL
, 0);
3282 /* Make sure that any sections mentioned in the assignment
3284 exp_init_os (s
->assignment_statement
.exp
);
3286 case lang_afile_asection_pair_statement_enum
:
3289 case lang_address_statement_enum
:
3290 /* Mark the specified section with the supplied address.
3292 If this section was actually a segment marker, then the
3293 directive is ignored if the linker script explicitly
3294 processed the segment marker. Originally, the linker
3295 treated segment directives (like -Ttext on the
3296 command-line) as section directives. We honor the
3297 section directive semantics for backwards compatibilty;
3298 linker scripts that do not specifically check for
3299 SEGMENT_START automatically get the old semantics. */
3300 if (!s
->address_statement
.segment
3301 || !s
->address_statement
.segment
->used
)
3303 lang_output_section_statement_type
*aos
3304 = (lang_output_section_statement_lookup
3305 (s
->address_statement
.section_name
));
3307 if (aos
->bfd_section
== NULL
)
3308 init_os (aos
, NULL
, 0);
3309 aos
->addr_tree
= s
->address_statement
.address
;
3316 /* An output section might have been removed after its statement was
3317 added. For example, ldemul_before_allocation can remove dynamic
3318 sections if they turn out to be not needed. Clean them up here. */
3321 strip_excluded_output_sections (void)
3323 lang_output_section_statement_type
*os
;
3325 /* Run lang_size_sections (if not already done). */
3326 if (expld
.phase
!= lang_mark_phase_enum
)
3328 expld
.phase
= lang_mark_phase_enum
;
3329 expld
.dataseg
.phase
= exp_dataseg_none
;
3330 one_lang_size_sections_pass (NULL
, FALSE
);
3331 lang_reset_memory_regions ();
3334 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3338 asection
*output_section
;
3339 bfd_boolean exclude
;
3341 if (os
->constraint
== -1)
3344 output_section
= os
->bfd_section
;
3345 if (output_section
== NULL
)
3348 exclude
= (output_section
->rawsize
== 0
3349 && (output_section
->flags
& SEC_KEEP
) == 0
3350 && !bfd_section_removed_from_list (output_bfd
,
3353 /* Some sections have not yet been sized, notably .gnu.version,
3354 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3355 input sections, so don't drop output sections that have such
3356 input sections unless they are also marked SEC_EXCLUDE. */
3357 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3361 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3362 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3363 && (s
->flags
& SEC_EXCLUDE
) == 0)
3370 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3371 output_section
->map_head
.link_order
= NULL
;
3372 output_section
->map_tail
.link_order
= NULL
;
3376 /* We don't set bfd_section to NULL since bfd_section of the
3377 removed output section statement may still be used. */
3379 output_section
->flags
|= SEC_EXCLUDE
;
3380 bfd_section_list_remove (output_bfd
, output_section
);
3381 output_bfd
->section_count
--;
3385 /* Stop future calls to lang_add_section from messing with map_head
3386 and map_tail link_order fields. */
3387 stripped_excluded_sections
= TRUE
;
3391 print_output_section_statement
3392 (lang_output_section_statement_type
*output_section_statement
)
3394 asection
*section
= output_section_statement
->bfd_section
;
3397 if (output_section_statement
!= abs_output_section
)
3399 minfo ("\n%s", output_section_statement
->name
);
3401 if (section
!= NULL
)
3403 print_dot
= section
->vma
;
3405 len
= strlen (output_section_statement
->name
);
3406 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3411 while (len
< SECTION_NAME_MAP_LENGTH
)
3417 minfo ("0x%V %W", section
->vma
, section
->size
);
3419 if (output_section_statement
->load_base
!= NULL
)
3423 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
3425 minfo (_(" load address 0x%V"), addr
);
3432 print_statement_list (output_section_statement
->children
.head
,
3433 output_section_statement
);
3436 /* Scan for the use of the destination in the right hand side
3437 of an expression. In such cases we will not compute the
3438 correct expression, since the value of DST that is used on
3439 the right hand side will be its final value, not its value
3440 just before this expression is evaluated. */
3443 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3445 if (rhs
== NULL
|| dst
== NULL
)
3448 switch (rhs
->type
.node_class
)
3451 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3452 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3455 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3456 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3459 case etree_provided
:
3461 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3463 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3466 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3470 return strcmp (dst
, rhs
->value
.str
) == 0;
3475 return strcmp (dst
, rhs
->name
.name
) == 0;
3487 print_assignment (lang_assignment_statement_type
*assignment
,
3488 lang_output_section_statement_type
*output_section
)
3492 bfd_boolean computation_is_valid
= TRUE
;
3495 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3498 if (assignment
->exp
->type
.node_class
== etree_assert
)
3501 tree
= assignment
->exp
->assert_s
.child
;
3502 computation_is_valid
= TRUE
;
3506 const char *dst
= assignment
->exp
->assign
.dst
;
3508 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3509 tree
= assignment
->exp
->assign
.src
;
3510 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3513 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3514 if (expld
.result
.valid_p
)
3518 if (computation_is_valid
)
3520 value
= expld
.result
.value
;
3522 if (expld
.result
.section
)
3523 value
+= expld
.result
.section
->vma
;
3525 minfo ("0x%V", value
);
3531 struct bfd_link_hash_entry
*h
;
3533 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3534 FALSE
, FALSE
, TRUE
);
3537 value
= h
->u
.def
.value
;
3539 if (expld
.result
.section
)
3540 value
+= expld
.result
.section
->vma
;
3542 minfo ("[0x%V]", value
);
3545 minfo ("[unresolved]");
3557 exp_print_tree (assignment
->exp
);
3562 print_input_statement (lang_input_statement_type
*statm
)
3564 if (statm
->filename
!= NULL
)
3566 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3570 /* Print all symbols defined in a particular section. This is called
3571 via bfd_link_hash_traverse, or by print_all_symbols. */
3574 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3576 asection
*sec
= ptr
;
3578 if ((hash_entry
->type
== bfd_link_hash_defined
3579 || hash_entry
->type
== bfd_link_hash_defweak
)
3580 && sec
== hash_entry
->u
.def
.section
)
3584 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3587 (hash_entry
->u
.def
.value
3588 + hash_entry
->u
.def
.section
->output_offset
3589 + hash_entry
->u
.def
.section
->output_section
->vma
));
3591 minfo (" %T\n", hash_entry
->root
.string
);
3598 print_all_symbols (asection
*sec
)
3600 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3601 struct map_symbol_def
*def
;
3606 *ud
->map_symbol_def_tail
= 0;
3607 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3608 print_one_symbol (def
->entry
, sec
);
3611 /* Print information about an input section to the map file. */
3614 print_input_section (asection
*i
)
3616 bfd_size_type size
= i
->size
;
3623 minfo ("%s", i
->name
);
3625 len
= 1 + strlen (i
->name
);
3626 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3631 while (len
< SECTION_NAME_MAP_LENGTH
)
3637 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3638 addr
= i
->output_section
->vma
+ i
->output_offset
;
3645 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3647 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3649 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3661 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3664 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3666 if (command_line
.reduce_memory_overheads
)
3667 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3669 print_all_symbols (i
);
3671 print_dot
= addr
+ TO_ADDR (size
);
3676 print_fill_statement (lang_fill_statement_type
*fill
)
3680 fputs (" FILL mask 0x", config
.map_file
);
3681 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3682 fprintf (config
.map_file
, "%02x", *p
);
3683 fputs ("\n", config
.map_file
);
3687 print_data_statement (lang_data_statement_type
*data
)
3695 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3698 addr
= data
->output_offset
;
3699 if (data
->output_section
!= NULL
)
3700 addr
+= data
->output_section
->vma
;
3728 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3730 if (data
->exp
->type
.node_class
!= etree_value
)
3733 exp_print_tree (data
->exp
);
3738 print_dot
= addr
+ TO_ADDR (size
);
3741 /* Print an address statement. These are generated by options like
3745 print_address_statement (lang_address_statement_type
*address
)
3747 minfo (_("Address of section %s set to "), address
->section_name
);
3748 exp_print_tree (address
->address
);
3752 /* Print a reloc statement. */
3755 print_reloc_statement (lang_reloc_statement_type
*reloc
)
3762 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3765 addr
= reloc
->output_offset
;
3766 if (reloc
->output_section
!= NULL
)
3767 addr
+= reloc
->output_section
->vma
;
3769 size
= bfd_get_reloc_size (reloc
->howto
);
3771 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
3773 if (reloc
->name
!= NULL
)
3774 minfo ("%s+", reloc
->name
);
3776 minfo ("%s+", reloc
->section
->name
);
3778 exp_print_tree (reloc
->addend_exp
);
3782 print_dot
= addr
+ TO_ADDR (size
);
3786 print_padding_statement (lang_padding_statement_type
*s
)
3794 len
= sizeof " *fill*" - 1;
3795 while (len
< SECTION_NAME_MAP_LENGTH
)
3801 addr
= s
->output_offset
;
3802 if (s
->output_section
!= NULL
)
3803 addr
+= s
->output_section
->vma
;
3804 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
3806 if (s
->fill
->size
!= 0)
3810 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
3811 fprintf (config
.map_file
, "%02x", *p
);
3816 print_dot
= addr
+ TO_ADDR (s
->size
);
3820 print_wild_statement (lang_wild_statement_type
*w
,
3821 lang_output_section_statement_type
*os
)
3823 struct wildcard_list
*sec
;
3827 if (w
->filenames_sorted
)
3829 if (w
->filename
!= NULL
)
3830 minfo ("%s", w
->filename
);
3833 if (w
->filenames_sorted
)
3837 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
3839 if (sec
->spec
.sorted
)
3841 if (sec
->spec
.exclude_name_list
!= NULL
)
3844 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
3845 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
3846 minfo (" %s", tmp
->name
);
3849 if (sec
->spec
.name
!= NULL
)
3850 minfo ("%s", sec
->spec
.name
);
3853 if (sec
->spec
.sorted
)
3862 print_statement_list (w
->children
.head
, os
);
3865 /* Print a group statement. */
3868 print_group (lang_group_statement_type
*s
,
3869 lang_output_section_statement_type
*os
)
3871 fprintf (config
.map_file
, "START GROUP\n");
3872 print_statement_list (s
->children
.head
, os
);
3873 fprintf (config
.map_file
, "END GROUP\n");
3876 /* Print the list of statements in S.
3877 This can be called for any statement type. */
3880 print_statement_list (lang_statement_union_type
*s
,
3881 lang_output_section_statement_type
*os
)
3885 print_statement (s
, os
);
3890 /* Print the first statement in statement list S.
3891 This can be called for any statement type. */
3894 print_statement (lang_statement_union_type
*s
,
3895 lang_output_section_statement_type
*os
)
3897 switch (s
->header
.type
)
3900 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
3903 case lang_constructors_statement_enum
:
3904 if (constructor_list
.head
!= NULL
)
3906 if (constructors_sorted
)
3907 minfo (" SORT (CONSTRUCTORS)\n");
3909 minfo (" CONSTRUCTORS\n");
3910 print_statement_list (constructor_list
.head
, os
);
3913 case lang_wild_statement_enum
:
3914 print_wild_statement (&s
->wild_statement
, os
);
3916 case lang_address_statement_enum
:
3917 print_address_statement (&s
->address_statement
);
3919 case lang_object_symbols_statement_enum
:
3920 minfo (" CREATE_OBJECT_SYMBOLS\n");
3922 case lang_fill_statement_enum
:
3923 print_fill_statement (&s
->fill_statement
);
3925 case lang_data_statement_enum
:
3926 print_data_statement (&s
->data_statement
);
3928 case lang_reloc_statement_enum
:
3929 print_reloc_statement (&s
->reloc_statement
);
3931 case lang_input_section_enum
:
3932 print_input_section (s
->input_section
.section
);
3934 case lang_padding_statement_enum
:
3935 print_padding_statement (&s
->padding_statement
);
3937 case lang_output_section_statement_enum
:
3938 print_output_section_statement (&s
->output_section_statement
);
3940 case lang_assignment_statement_enum
:
3941 print_assignment (&s
->assignment_statement
, os
);
3943 case lang_target_statement_enum
:
3944 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
3946 case lang_output_statement_enum
:
3947 minfo ("OUTPUT(%s", s
->output_statement
.name
);
3948 if (output_target
!= NULL
)
3949 minfo (" %s", output_target
);
3952 case lang_input_statement_enum
:
3953 print_input_statement (&s
->input_statement
);
3955 case lang_group_statement_enum
:
3956 print_group (&s
->group_statement
, os
);
3958 case lang_afile_asection_pair_statement_enum
:
3965 print_statements (void)
3967 print_statement_list (statement_list
.head
, abs_output_section
);
3970 /* Print the first N statements in statement list S to STDERR.
3971 If N == 0, nothing is printed.
3972 If N < 0, the entire list is printed.
3973 Intended to be called from GDB. */
3976 dprint_statement (lang_statement_union_type
*s
, int n
)
3978 FILE *map_save
= config
.map_file
;
3980 config
.map_file
= stderr
;
3983 print_statement_list (s
, abs_output_section
);
3986 while (s
&& --n
>= 0)
3988 print_statement (s
, abs_output_section
);
3993 config
.map_file
= map_save
;
3997 insert_pad (lang_statement_union_type
**ptr
,
3999 unsigned int alignment_needed
,
4000 asection
*output_section
,
4003 static fill_type zero_fill
= { 1, { 0 } };
4004 lang_statement_union_type
*pad
= NULL
;
4006 if (ptr
!= &statement_list
.head
)
4007 pad
= ((lang_statement_union_type
*)
4008 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
4010 && pad
->header
.type
== lang_padding_statement_enum
4011 && pad
->padding_statement
.output_section
== output_section
)
4013 /* Use the existing pad statement. */
4015 else if ((pad
= *ptr
) != NULL
4016 && pad
->header
.type
== lang_padding_statement_enum
4017 && pad
->padding_statement
.output_section
== output_section
)
4019 /* Use the existing pad statement. */
4023 /* Make a new padding statement, linked into existing chain. */
4024 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
4025 pad
->header
.next
= *ptr
;
4027 pad
->header
.type
= lang_padding_statement_enum
;
4028 pad
->padding_statement
.output_section
= output_section
;
4031 pad
->padding_statement
.fill
= fill
;
4033 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
4034 pad
->padding_statement
.size
= alignment_needed
;
4035 output_section
->size
+= alignment_needed
;
4038 /* Work out how much this section will move the dot point. */
4042 (lang_statement_union_type
**this_ptr
,
4043 lang_output_section_statement_type
*output_section_statement
,
4047 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
4048 asection
*i
= is
->section
;
4050 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
4051 && (i
->flags
& SEC_EXCLUDE
) == 0)
4053 unsigned int alignment_needed
;
4056 /* Align this section first to the input sections requirement,
4057 then to the output section's requirement. If this alignment
4058 is greater than any seen before, then record it too. Perform
4059 the alignment by inserting a magic 'padding' statement. */
4061 if (output_section_statement
->subsection_alignment
!= -1)
4062 i
->alignment_power
= output_section_statement
->subsection_alignment
;
4064 o
= output_section_statement
->bfd_section
;
4065 if (o
->alignment_power
< i
->alignment_power
)
4066 o
->alignment_power
= i
->alignment_power
;
4068 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
4070 if (alignment_needed
!= 0)
4072 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
4073 dot
+= alignment_needed
;
4076 /* Remember where in the output section this input section goes. */
4078 i
->output_offset
= dot
- o
->vma
;
4080 /* Mark how big the output section must be to contain this now. */
4081 dot
+= TO_ADDR (i
->size
);
4082 o
->size
= TO_SIZE (dot
- o
->vma
);
4086 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
4093 sort_sections_by_lma (const void *arg1
, const void *arg2
)
4095 const asection
*sec1
= *(const asection
**) arg1
;
4096 const asection
*sec2
= *(const asection
**) arg2
;
4098 if (bfd_section_lma (sec1
->owner
, sec1
)
4099 < bfd_section_lma (sec2
->owner
, sec2
))
4101 else if (bfd_section_lma (sec1
->owner
, sec1
)
4102 > bfd_section_lma (sec2
->owner
, sec2
))
4108 #define IGNORE_SECTION(s) \
4109 ((s->flags & SEC_NEVER_LOAD) != 0 \
4110 || (s->flags & SEC_ALLOC) == 0 \
4111 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4112 && (s->flags & SEC_LOAD) == 0))
4114 /* Check to see if any allocated sections overlap with other allocated
4115 sections. This can happen if a linker script specifies the output
4116 section addresses of the two sections. */
4119 lang_check_section_addresses (void)
4122 asection
**sections
, **spp
;
4130 if (bfd_count_sections (output_bfd
) <= 1)
4133 amt
= bfd_count_sections (output_bfd
) * sizeof (asection
*);
4134 sections
= xmalloc (amt
);
4136 /* Scan all sections in the output list. */
4138 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4140 /* Only consider loadable sections with real contents. */
4141 if (IGNORE_SECTION (s
) || s
->size
== 0)
4144 sections
[count
] = s
;
4151 qsort (sections
, (size_t) count
, sizeof (asection
*),
4152 sort_sections_by_lma
);
4156 s_start
= bfd_section_lma (output_bfd
, s
);
4157 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4158 for (count
--; count
; count
--)
4160 /* We must check the sections' LMA addresses not their VMA
4161 addresses because overlay sections can have overlapping VMAs
4162 but they must have distinct LMAs. */
4167 s_start
= bfd_section_lma (output_bfd
, s
);
4168 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4170 /* Look for an overlap. */
4171 if (s_end
>= os_start
&& s_start
<= os_end
)
4172 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4173 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4179 /* Make sure the new address is within the region. We explicitly permit the
4180 current address to be at the exact end of the region when the address is
4181 non-zero, in case the region is at the end of addressable memory and the
4182 calculation wraps around. */
4185 os_region_check (lang_output_section_statement_type
*os
,
4186 lang_memory_region_type
*region
,
4190 if ((region
->current
< region
->origin
4191 || (region
->current
- region
->origin
> region
->length
))
4192 && ((region
->current
!= region
->origin
+ region
->length
)
4197 einfo (_("%X%P: address 0x%v of %B section %s"
4198 " is not within region %s\n"),
4200 os
->bfd_section
->owner
,
4201 os
->bfd_section
->name
,
4206 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4208 os
->bfd_section
->owner
,
4209 os
->bfd_section
->name
);
4211 /* Reset the region pointer. */
4212 region
->current
= region
->origin
;
4216 /* Set the sizes for all the output sections. */
4219 lang_size_sections_1
4220 (lang_statement_union_type
*s
,
4221 lang_output_section_statement_type
*output_section_statement
,
4222 lang_statement_union_type
**prev
,
4226 bfd_boolean check_regions
)
4228 /* Size up the sections from their constituent parts. */
4229 for (; s
!= NULL
; s
= s
->header
.next
)
4231 switch (s
->header
.type
)
4233 case lang_output_section_statement_enum
:
4235 bfd_vma newdot
, after
;
4236 lang_output_section_statement_type
*os
;
4238 os
= &s
->output_section_statement
;
4239 if (os
->addr_tree
!= NULL
)
4241 os
->processed
= FALSE
;
4242 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4244 if (!expld
.result
.valid_p
4245 && expld
.phase
!= lang_mark_phase_enum
)
4246 einfo (_("%F%S: non constant or forward reference"
4247 " address expression for section %s\n"),
4250 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4253 if (os
->bfd_section
== NULL
)
4254 /* This section was removed or never actually created. */
4257 /* If this is a COFF shared library section, use the size and
4258 address from the input section. FIXME: This is COFF
4259 specific; it would be cleaner if there were some other way
4260 to do this, but nothing simple comes to mind. */
4261 if ((bfd_get_flavour (output_bfd
) == bfd_target_ecoff_flavour
4262 || bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
4263 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4267 if (os
->children
.head
== NULL
4268 || os
->children
.head
->header
.next
!= NULL
4269 || (os
->children
.head
->header
.type
4270 != lang_input_section_enum
))
4271 einfo (_("%P%X: Internal error on COFF shared library"
4272 " section %s\n"), os
->name
);
4274 input
= os
->children
.head
->input_section
.section
;
4275 bfd_set_section_vma (os
->bfd_section
->owner
,
4277 bfd_section_vma (input
->owner
, input
));
4278 os
->bfd_section
->size
= input
->size
;
4283 if (bfd_is_abs_section (os
->bfd_section
))
4285 /* No matter what happens, an abs section starts at zero. */
4286 ASSERT (os
->bfd_section
->vma
== 0);
4292 if (os
->addr_tree
== NULL
)
4294 /* No address specified for this section, get one
4295 from the region specification. */
4296 if (os
->region
== NULL
4297 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4298 && os
->region
->name
[0] == '*'
4299 && strcmp (os
->region
->name
,
4300 DEFAULT_MEMORY_REGION
) == 0))
4302 os
->region
= lang_memory_default (os
->bfd_section
);
4305 /* If a loadable section is using the default memory
4306 region, and some non default memory regions were
4307 defined, issue an error message. */
4309 && !IGNORE_SECTION (os
->bfd_section
)
4310 && ! link_info
.relocatable
4312 && strcmp (os
->region
->name
,
4313 DEFAULT_MEMORY_REGION
) == 0
4314 && lang_memory_region_list
!= NULL
4315 && (strcmp (lang_memory_region_list
->name
,
4316 DEFAULT_MEMORY_REGION
) != 0
4317 || lang_memory_region_list
->next
!= NULL
)
4318 && expld
.phase
!= lang_mark_phase_enum
)
4320 /* By default this is an error rather than just a
4321 warning because if we allocate the section to the
4322 default memory region we can end up creating an
4323 excessively large binary, or even seg faulting when
4324 attempting to perform a negative seek. See
4325 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4326 for an example of this. This behaviour can be
4327 overridden by the using the --no-check-sections
4329 if (command_line
.check_section_addresses
)
4330 einfo (_("%P%F: error: no memory region specified"
4331 " for loadable section `%s'\n"),
4332 bfd_get_section_name (output_bfd
,
4335 einfo (_("%P: warning: no memory region specified"
4336 " for loadable section `%s'\n"),
4337 bfd_get_section_name (output_bfd
,
4341 newdot
= os
->region
->current
;
4342 align
= os
->bfd_section
->alignment_power
;
4345 align
= os
->section_alignment
;
4347 /* Align to what the section needs. */
4350 bfd_vma savedot
= newdot
;
4351 newdot
= align_power (newdot
, align
);
4353 if (newdot
!= savedot
4354 && (config
.warn_section_align
4355 || os
->addr_tree
!= NULL
)
4356 && expld
.phase
!= lang_mark_phase_enum
)
4357 einfo (_("%P: warning: changing start of section"
4358 " %s by %lu bytes\n"),
4359 os
->name
, (unsigned long) (newdot
- savedot
));
4362 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4364 os
->bfd_section
->output_offset
= 0;
4367 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4368 os
->fill
, newdot
, relax
, check_regions
);
4370 os
->processed
= TRUE
;
4372 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4374 ASSERT (os
->bfd_section
->size
== 0);
4378 dot
= os
->bfd_section
->vma
;
4380 /* Put the section within the requested block size, or
4381 align at the block boundary. */
4383 + TO_ADDR (os
->bfd_section
->size
)
4384 + os
->block_value
- 1)
4385 & - (bfd_vma
) os
->block_value
);
4387 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4389 /* .tbss sections effectively have zero size. */
4390 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4391 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4392 || link_info
.relocatable
)
4393 dot
+= TO_ADDR (os
->bfd_section
->size
);
4395 if (os
->update_dot_tree
!= 0)
4396 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4398 /* Update dot in the region ?
4399 We only do this if the section is going to be allocated,
4400 since unallocated sections do not contribute to the region's
4401 overall size in memory.
4403 If the SEC_NEVER_LOAD bit is not set, it will affect the
4404 addresses of sections after it. We have to update
4406 if (os
->region
!= NULL
4407 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4408 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4410 os
->region
->current
= dot
;
4413 /* Make sure the new address is within the region. */
4414 os_region_check (os
, os
->region
, os
->addr_tree
,
4415 os
->bfd_section
->vma
);
4417 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
4419 /* Set load_base, which will be handled later. */
4420 os
->load_base
= exp_intop (os
->lma_region
->current
);
4421 os
->lma_region
->current
+=
4422 TO_ADDR (os
->bfd_section
->size
);
4425 os_region_check (os
, os
->lma_region
, NULL
,
4426 os
->lma_region
->current
);
4432 case lang_constructors_statement_enum
:
4433 dot
= lang_size_sections_1 (constructor_list
.head
,
4434 output_section_statement
,
4435 &s
->wild_statement
.children
.head
,
4436 fill
, dot
, relax
, check_regions
);
4439 case lang_data_statement_enum
:
4441 unsigned int size
= 0;
4443 s
->data_statement
.output_offset
=
4444 dot
- output_section_statement
->bfd_section
->vma
;
4445 s
->data_statement
.output_section
=
4446 output_section_statement
->bfd_section
;
4448 /* We might refer to provided symbols in the expression, and
4449 need to mark them as needed. */
4450 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4452 switch (s
->data_statement
.type
)
4470 if (size
< TO_SIZE ((unsigned) 1))
4471 size
= TO_SIZE ((unsigned) 1);
4472 dot
+= TO_ADDR (size
);
4473 output_section_statement
->bfd_section
->size
+= size
;
4477 case lang_reloc_statement_enum
:
4481 s
->reloc_statement
.output_offset
=
4482 dot
- output_section_statement
->bfd_section
->vma
;
4483 s
->reloc_statement
.output_section
=
4484 output_section_statement
->bfd_section
;
4485 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4486 dot
+= TO_ADDR (size
);
4487 output_section_statement
->bfd_section
->size
+= size
;
4491 case lang_wild_statement_enum
:
4492 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4493 output_section_statement
,
4494 &s
->wild_statement
.children
.head
,
4495 fill
, dot
, relax
, check_regions
);
4498 case lang_object_symbols_statement_enum
:
4499 link_info
.create_object_symbols_section
=
4500 output_section_statement
->bfd_section
;
4503 case lang_output_statement_enum
:
4504 case lang_target_statement_enum
:
4507 case lang_input_section_enum
:
4511 i
= (*prev
)->input_section
.section
;
4516 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4517 einfo (_("%P%F: can't relax section: %E\n"));
4521 dot
= size_input_section (prev
, output_section_statement
,
4522 output_section_statement
->fill
, dot
);
4526 case lang_input_statement_enum
:
4529 case lang_fill_statement_enum
:
4530 s
->fill_statement
.output_section
=
4531 output_section_statement
->bfd_section
;
4533 fill
= s
->fill_statement
.fill
;
4536 case lang_assignment_statement_enum
:
4538 bfd_vma newdot
= dot
;
4540 exp_fold_tree (s
->assignment_statement
.exp
,
4541 output_section_statement
->bfd_section
,
4544 if (newdot
!= dot
&& !output_section_statement
->ignored
)
4546 if (output_section_statement
== abs_output_section
)
4548 /* If we don't have an output section, then just adjust
4549 the default memory address. */
4550 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4551 FALSE
)->current
= newdot
;
4555 /* Insert a pad after this statement. We can't
4556 put the pad before when relaxing, in case the
4557 assignment references dot. */
4558 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4559 output_section_statement
->bfd_section
, dot
);
4561 /* Don't neuter the pad below when relaxing. */
4564 /* If dot is advanced, this implies that the section
4565 should have space allocated to it, unless the
4566 user has explicitly stated that the section
4567 should never be loaded. */
4568 if (!(output_section_statement
->flags
4569 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
4570 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4577 case lang_padding_statement_enum
:
4578 /* If this is the first time lang_size_sections is called,
4579 we won't have any padding statements. If this is the
4580 second or later passes when relaxing, we should allow
4581 padding to shrink. If padding is needed on this pass, it
4582 will be added back in. */
4583 s
->padding_statement
.size
= 0;
4585 /* Make sure output_offset is valid. If relaxation shrinks
4586 the section and this pad isn't needed, it's possible to
4587 have output_offset larger than the final size of the
4588 section. bfd_set_section_contents will complain even for
4589 a pad size of zero. */
4590 s
->padding_statement
.output_offset
4591 = dot
- output_section_statement
->bfd_section
->vma
;
4594 case lang_group_statement_enum
:
4595 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4596 output_section_statement
,
4597 &s
->group_statement
.children
.head
,
4598 fill
, dot
, relax
, check_regions
);
4605 /* We can only get here when relaxing is turned on. */
4606 case lang_address_statement_enum
:
4609 prev
= &s
->header
.next
;
4615 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
4617 lang_statement_iteration
++;
4618 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
4619 &statement_list
.head
, 0, 0, relax
, check_regions
);
4623 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
4625 expld
.phase
= lang_allocating_phase_enum
;
4626 expld
.dataseg
.phase
= exp_dataseg_none
;
4628 one_lang_size_sections_pass (relax
, check_regions
);
4629 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
4630 && link_info
.relro
&& expld
.dataseg
.relro_end
)
4632 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4633 to put expld.dataseg.relro on a (common) page boundary. */
4634 bfd_vma old_min_base
, relro_end
, maxpage
;
4636 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
4637 old_min_base
= expld
.dataseg
.min_base
;
4638 maxpage
= expld
.dataseg
.maxpagesize
;
4639 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
4640 & (expld
.dataseg
.pagesize
- 1));
4641 /* Compute the expected PT_GNU_RELRO segment end. */
4642 relro_end
= (expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
4643 & ~(expld
.dataseg
.pagesize
- 1);
4644 if (old_min_base
+ maxpage
< expld
.dataseg
.base
)
4646 expld
.dataseg
.base
-= maxpage
;
4647 relro_end
-= maxpage
;
4649 lang_reset_memory_regions ();
4650 one_lang_size_sections_pass (relax
, check_regions
);
4651 if (expld
.dataseg
.relro_end
> relro_end
)
4653 /* The alignment of sections between DATA_SEGMENT_ALIGN
4654 and DATA_SEGMENT_RELRO_END caused huge padding to be
4655 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4657 unsigned int max_alignment_power
= 0;
4659 /* Find maximum alignment power of sections between
4660 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4661 for (sec
= output_bfd
->sections
; sec
; sec
= sec
->next
)
4662 if (sec
->vma
>= expld
.dataseg
.base
4663 && sec
->vma
< expld
.dataseg
.relro_end
4664 && sec
->alignment_power
> max_alignment_power
)
4665 max_alignment_power
= sec
->alignment_power
;
4667 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
4669 if (expld
.dataseg
.base
- (1 << max_alignment_power
)
4671 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
4672 expld
.dataseg
.base
-= (1 << max_alignment_power
);
4673 lang_reset_memory_regions ();
4674 one_lang_size_sections_pass (relax
, check_regions
);
4677 link_info
.relro_start
= expld
.dataseg
.base
;
4678 link_info
.relro_end
= expld
.dataseg
.relro_end
;
4680 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
4682 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4683 a page could be saved in the data segment. */
4684 bfd_vma first
, last
;
4686 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
4687 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
4689 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
4690 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
4691 && first
+ last
<= expld
.dataseg
.pagesize
)
4693 expld
.dataseg
.phase
= exp_dataseg_adjust
;
4694 lang_reset_memory_regions ();
4695 one_lang_size_sections_pass (relax
, check_regions
);
4699 expld
.phase
= lang_final_phase_enum
;
4702 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4705 lang_do_assignments_1 (lang_statement_union_type
*s
,
4706 lang_output_section_statement_type
*current_os
,
4710 for (; s
!= NULL
; s
= s
->header
.next
)
4712 switch (s
->header
.type
)
4714 case lang_constructors_statement_enum
:
4715 dot
= lang_do_assignments_1 (constructor_list
.head
,
4716 current_os
, fill
, dot
);
4719 case lang_output_section_statement_enum
:
4721 lang_output_section_statement_type
*os
;
4723 os
= &(s
->output_section_statement
);
4724 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
4726 lang_memory_region_type
*r
;
4728 dot
= os
->bfd_section
->vma
;
4731 r
= lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
4734 os
->bfd_section
->lma
4735 = exp_get_abs_int (os
->load_base
, 0, "load base");
4736 else if (r
->last_os
!= NULL
)
4741 last
= r
->last_os
->output_section_statement
.bfd_section
;
4743 /* If the current vma overlaps the previous section,
4744 then set the current lma to that at the end of
4745 the previous section. The previous section was
4746 probably an overlay. */
4747 if ((dot
>= last
->vma
4748 && dot
< last
->vma
+ last
->size
)
4749 || (last
->vma
>= dot
4750 && last
->vma
< dot
+ os
->bfd_section
->size
))
4751 lma
= last
->lma
+ last
->size
;
4753 /* Otherwise, keep the same lma to vma relationship
4754 as the previous section. */
4756 lma
= dot
+ last
->lma
- last
->vma
;
4758 if (os
->section_alignment
!= -1)
4759 lma
= align_power (lma
, os
->section_alignment
);
4760 os
->bfd_section
->lma
= lma
;
4763 lang_do_assignments_1 (os
->children
.head
,
4766 /* .tbss sections effectively have zero size. */
4767 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4768 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4769 || link_info
.relocatable
)
4771 dot
+= TO_ADDR (os
->bfd_section
->size
);
4773 /* Keep track of normal sections using the default
4774 lma region. We use this to set the lma for
4775 following sections. Overlays or other linker
4776 script assignment to lma might mean that the
4777 default lma == vma is incorrect. */
4778 if (!link_info
.relocatable
4779 && os
->lma_region
== NULL
)
4786 case lang_wild_statement_enum
:
4788 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
4789 current_os
, fill
, dot
);
4792 case lang_object_symbols_statement_enum
:
4793 case lang_output_statement_enum
:
4794 case lang_target_statement_enum
:
4797 case lang_data_statement_enum
:
4798 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4799 if (expld
.result
.valid_p
)
4800 s
->data_statement
.value
= (expld
.result
.value
4801 + expld
.result
.section
->vma
);
4803 einfo (_("%F%P: invalid data statement\n"));
4806 switch (s
->data_statement
.type
)
4824 if (size
< TO_SIZE ((unsigned) 1))
4825 size
= TO_SIZE ((unsigned) 1);
4826 dot
+= TO_ADDR (size
);
4830 case lang_reloc_statement_enum
:
4831 exp_fold_tree (s
->reloc_statement
.addend_exp
,
4832 bfd_abs_section_ptr
, &dot
);
4833 if (expld
.result
.valid_p
)
4834 s
->reloc_statement
.addend_value
= expld
.result
.value
;
4836 einfo (_("%F%P: invalid reloc statement\n"));
4837 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
4840 case lang_input_section_enum
:
4842 asection
*in
= s
->input_section
.section
;
4844 if ((in
->flags
& SEC_EXCLUDE
) == 0)
4845 dot
+= TO_ADDR (in
->size
);
4849 case lang_input_statement_enum
:
4852 case lang_fill_statement_enum
:
4853 fill
= s
->fill_statement
.fill
;
4856 case lang_assignment_statement_enum
:
4857 exp_fold_tree (s
->assignment_statement
.exp
,
4858 current_os
->bfd_section
,
4862 case lang_padding_statement_enum
:
4863 dot
+= TO_ADDR (s
->padding_statement
.size
);
4866 case lang_group_statement_enum
:
4867 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
4868 current_os
, fill
, dot
);
4875 case lang_address_statement_enum
:
4883 lang_do_assignments (void)
4885 lang_statement_iteration
++;
4886 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
4889 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4890 operator .startof. (section_name), it produces an undefined symbol
4891 .startof.section_name. Similarly, when it sees
4892 .sizeof. (section_name), it produces an undefined symbol
4893 .sizeof.section_name. For all the output sections, we look for
4894 such symbols, and set them to the correct value. */
4897 lang_set_startof (void)
4901 if (link_info
.relocatable
)
4904 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4906 const char *secname
;
4908 struct bfd_link_hash_entry
*h
;
4910 secname
= bfd_get_section_name (output_bfd
, s
);
4911 buf
= xmalloc (10 + strlen (secname
));
4913 sprintf (buf
, ".startof.%s", secname
);
4914 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4915 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4917 h
->type
= bfd_link_hash_defined
;
4918 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
4919 h
->u
.def
.section
= bfd_abs_section_ptr
;
4922 sprintf (buf
, ".sizeof.%s", secname
);
4923 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4924 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4926 h
->type
= bfd_link_hash_defined
;
4927 h
->u
.def
.value
= TO_ADDR (s
->size
);
4928 h
->u
.def
.section
= bfd_abs_section_ptr
;
4938 struct bfd_link_hash_entry
*h
;
4941 if (link_info
.relocatable
|| link_info
.shared
)
4946 if (entry_symbol
.name
== NULL
)
4948 /* No entry has been specified. Look for the default entry, but
4949 don't warn if we don't find it. */
4950 entry_symbol
.name
= entry_symbol_default
;
4954 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
4955 FALSE
, FALSE
, TRUE
);
4957 && (h
->type
== bfd_link_hash_defined
4958 || h
->type
== bfd_link_hash_defweak
)
4959 && h
->u
.def
.section
->output_section
!= NULL
)
4963 val
= (h
->u
.def
.value
4964 + bfd_get_section_vma (output_bfd
,
4965 h
->u
.def
.section
->output_section
)
4966 + h
->u
.def
.section
->output_offset
);
4967 if (! bfd_set_start_address (output_bfd
, val
))
4968 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
4975 /* We couldn't find the entry symbol. Try parsing it as a
4977 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
4980 if (! bfd_set_start_address (output_bfd
, val
))
4981 einfo (_("%P%F: can't set start address\n"));
4987 /* Can't find the entry symbol, and it's not a number. Use
4988 the first address in the text section. */
4989 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
4993 einfo (_("%P: warning: cannot find entry symbol %s;"
4994 " defaulting to %V\n"),
4996 bfd_get_section_vma (output_bfd
, ts
));
4997 if (! bfd_set_start_address (output_bfd
,
4998 bfd_get_section_vma (output_bfd
,
5000 einfo (_("%P%F: can't set start address\n"));
5005 einfo (_("%P: warning: cannot find entry symbol %s;"
5006 " not setting start address\n"),
5012 /* Don't bfd_hash_table_free (&lang_definedness_table);
5013 map file output may result in a call of lang_track_definedness. */
5016 /* This is a small function used when we want to ignore errors from
5020 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
5022 /* Don't do anything. */
5025 /* Check that the architecture of all the input files is compatible
5026 with the output file. Also call the backend to let it do any
5027 other checking that is needed. */
5032 lang_statement_union_type
*file
;
5034 const bfd_arch_info_type
*compatible
;
5036 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
5038 input_bfd
= file
->input_statement
.the_bfd
;
5040 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
5041 command_line
.accept_unknown_input_arch
);
5043 /* In general it is not possible to perform a relocatable
5044 link between differing object formats when the input
5045 file has relocations, because the relocations in the
5046 input format may not have equivalent representations in
5047 the output format (and besides BFD does not translate
5048 relocs for other link purposes than a final link). */
5049 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
5050 && (compatible
== NULL
5051 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
5052 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
5054 einfo (_("%P%F: Relocatable linking with relocations from"
5055 " format %s (%B) to format %s (%B) is not supported\n"),
5056 bfd_get_target (input_bfd
), input_bfd
,
5057 bfd_get_target (output_bfd
), output_bfd
);
5058 /* einfo with %F exits. */
5061 if (compatible
== NULL
)
5063 if (command_line
.warn_mismatch
)
5064 einfo (_("%P: warning: %s architecture of input file `%B'"
5065 " is incompatible with %s output\n"),
5066 bfd_printable_name (input_bfd
), input_bfd
,
5067 bfd_printable_name (output_bfd
));
5069 else if (bfd_count_sections (input_bfd
))
5071 /* If the input bfd has no contents, it shouldn't set the
5072 private data of the output bfd. */
5074 bfd_error_handler_type pfn
= NULL
;
5076 /* If we aren't supposed to warn about mismatched input
5077 files, temporarily set the BFD error handler to a
5078 function which will do nothing. We still want to call
5079 bfd_merge_private_bfd_data, since it may set up
5080 information which is needed in the output file. */
5081 if (! command_line
.warn_mismatch
)
5082 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
5083 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
5085 if (command_line
.warn_mismatch
)
5086 einfo (_("%P%X: failed to merge target specific data"
5087 " of file %B\n"), input_bfd
);
5089 if (! command_line
.warn_mismatch
)
5090 bfd_set_error_handler (pfn
);
5095 /* Look through all the global common symbols and attach them to the
5096 correct section. The -sort-common command line switch may be used
5097 to roughly sort the entries by size. */
5102 if (command_line
.inhibit_common_definition
)
5104 if (link_info
.relocatable
5105 && ! command_line
.force_common_definition
)
5108 if (! config
.sort_common
)
5109 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
5114 for (power
= 4; power
>= 0; power
--)
5115 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
5119 /* Place one common symbol in the correct section. */
5122 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
5124 unsigned int power_of_two
;
5128 if (h
->type
!= bfd_link_hash_common
)
5132 power_of_two
= h
->u
.c
.p
->alignment_power
;
5134 if (config
.sort_common
5135 && power_of_two
< (unsigned int) *(int *) info
)
5138 section
= h
->u
.c
.p
->section
;
5140 /* Increase the size of the section to align the common sym. */
5141 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
5142 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
5144 /* Adjust the alignment if necessary. */
5145 if (power_of_two
> section
->alignment_power
)
5146 section
->alignment_power
= power_of_two
;
5148 /* Change the symbol from common to defined. */
5149 h
->type
= bfd_link_hash_defined
;
5150 h
->u
.def
.section
= section
;
5151 h
->u
.def
.value
= section
->size
;
5153 /* Increase the size of the section. */
5154 section
->size
+= size
;
5156 /* Make sure the section is allocated in memory, and make sure that
5157 it is no longer a common section. */
5158 section
->flags
|= SEC_ALLOC
;
5159 section
->flags
&= ~SEC_IS_COMMON
;
5161 if (config
.map_file
!= NULL
)
5163 static bfd_boolean header_printed
;
5168 if (! header_printed
)
5170 minfo (_("\nAllocating common symbols\n"));
5171 minfo (_("Common symbol size file\n\n"));
5172 header_printed
= TRUE
;
5175 name
= demangle (h
->root
.string
);
5177 len
= strlen (name
);
5192 if (size
<= 0xffffffff)
5193 sprintf (buf
, "%lx", (unsigned long) size
);
5195 sprintf_vma (buf
, size
);
5205 minfo ("%B\n", section
->owner
);
5211 /* Run through the input files and ensure that every input section has
5212 somewhere to go. If one is found without a destination then create
5213 an input request and place it into the statement tree. */
5216 lang_place_orphans (void)
5218 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5222 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5224 if (s
->output_section
== NULL
)
5226 /* This section of the file is not attached, root
5227 around for a sensible place for it to go. */
5229 if (file
->just_syms_flag
)
5230 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5231 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5232 s
->output_section
= bfd_abs_section_ptr
;
5233 else if (strcmp (s
->name
, "COMMON") == 0)
5235 /* This is a lonely common section which must have
5236 come from an archive. We attach to the section
5237 with the wildcard. */
5238 if (! link_info
.relocatable
5239 || command_line
.force_common_definition
)
5241 if (default_common_section
== NULL
)
5243 default_common_section
=
5244 lang_output_section_statement_lookup (".bss");
5247 lang_add_section (&default_common_section
->children
, s
,
5248 default_common_section
);
5251 else if (ldemul_place_orphan (s
))
5255 lang_output_section_statement_type
*os
;
5257 os
= lang_output_section_statement_lookup (s
->name
);
5258 lang_add_section (&os
->children
, s
, os
);
5266 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5268 flagword
*ptr_flags
;
5270 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5276 *ptr_flags
|= SEC_ALLOC
;
5280 *ptr_flags
|= SEC_READONLY
;
5284 *ptr_flags
|= SEC_DATA
;
5288 *ptr_flags
|= SEC_CODE
;
5293 *ptr_flags
|= SEC_LOAD
;
5297 einfo (_("%P%F: invalid syntax in flags\n"));
5304 /* Call a function on each input file. This function will be called
5305 on an archive, but not on the elements. */
5308 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5310 lang_input_statement_type
*f
;
5312 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5314 f
= (lang_input_statement_type
*) f
->next_real_file
)
5318 /* Call a function on each file. The function will be called on all
5319 the elements of an archive which are included in the link, but will
5320 not be called on the archive file itself. */
5323 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5325 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5332 ldlang_add_file (lang_input_statement_type
*entry
)
5336 lang_statement_append (&file_chain
,
5337 (lang_statement_union_type
*) entry
,
5340 /* The BFD linker needs to have a list of all input BFDs involved in
5342 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5343 ASSERT (entry
->the_bfd
!= output_bfd
);
5344 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
5346 *pp
= entry
->the_bfd
;
5347 entry
->the_bfd
->usrdata
= entry
;
5348 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5350 /* Look through the sections and check for any which should not be
5351 included in the link. We need to do this now, so that we can
5352 notice when the backend linker tries to report multiple
5353 definition errors for symbols which are in sections we aren't
5354 going to link. FIXME: It might be better to entirely ignore
5355 symbols which are defined in sections which are going to be
5356 discarded. This would require modifying the backend linker for
5357 each backend which might set the SEC_LINK_ONCE flag. If we do
5358 this, we should probably handle SEC_EXCLUDE in the same way. */
5360 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5364 lang_add_output (const char *name
, int from_script
)
5366 /* Make -o on command line override OUTPUT in script. */
5367 if (!had_output_filename
|| !from_script
)
5369 output_filename
= name
;
5370 had_output_filename
= TRUE
;
5374 static lang_output_section_statement_type
*current_section
;
5385 for (l
= 0; l
< 32; l
++)
5387 if (i
>= (unsigned int) x
)
5395 lang_output_section_statement_type
*
5396 lang_enter_output_section_statement (const char *output_section_statement_name
,
5397 etree_type
*address_exp
,
5398 enum section_type sectype
,
5400 etree_type
*subalign
,
5404 lang_output_section_statement_type
*os
;
5406 os
= lang_output_section_statement_lookup_1 (output_section_statement_name
,
5408 current_section
= os
;
5410 /* Make next things chain into subchain of this. */
5412 if (os
->addr_tree
== NULL
)
5414 os
->addr_tree
= address_exp
;
5416 os
->sectype
= sectype
;
5417 if (sectype
!= noload_section
)
5418 os
->flags
= SEC_NO_FLAGS
;
5420 os
->flags
= SEC_NEVER_LOAD
;
5421 os
->block_value
= 1;
5422 stat_ptr
= &os
->children
;
5424 os
->subsection_alignment
=
5425 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5426 os
->section_alignment
=
5427 topower (exp_get_value_int (align
, -1, "section alignment"));
5429 os
->load_base
= ebase
;
5436 lang_output_statement_type
*new;
5438 new = new_stat (lang_output_statement
, stat_ptr
);
5439 new->name
= output_filename
;
5442 /* Reset the current counters in the regions. */
5445 lang_reset_memory_regions (void)
5447 lang_memory_region_type
*p
= lang_memory_region_list
;
5449 lang_output_section_statement_type
*os
;
5451 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5453 p
->current
= p
->origin
;
5457 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5460 os
->processed
= FALSE
;
5462 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5464 /* Save the last size for possible use by bfd_relax_section. */
5465 o
->rawsize
= o
->size
;
5470 /* Worker for lang_gc_sections_1. */
5473 gc_section_callback (lang_wild_statement_type
*ptr
,
5474 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5476 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5477 void *data ATTRIBUTE_UNUSED
)
5479 /* If the wild pattern was marked KEEP, the member sections
5480 should be as well. */
5481 if (ptr
->keep_sections
)
5482 section
->flags
|= SEC_KEEP
;
5485 /* Iterate over sections marking them against GC. */
5488 lang_gc_sections_1 (lang_statement_union_type
*s
)
5490 for (; s
!= NULL
; s
= s
->header
.next
)
5492 switch (s
->header
.type
)
5494 case lang_wild_statement_enum
:
5495 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5497 case lang_constructors_statement_enum
:
5498 lang_gc_sections_1 (constructor_list
.head
);
5500 case lang_output_section_statement_enum
:
5501 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5503 case lang_group_statement_enum
:
5504 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5513 lang_gc_sections (void)
5515 struct bfd_link_hash_entry
*h
;
5516 ldlang_undef_chain_list_type
*ulist
;
5518 /* Keep all sections so marked in the link script. */
5520 lang_gc_sections_1 (statement_list
.head
);
5522 /* Keep all sections containing symbols undefined on the command-line,
5523 and the section containing the entry symbol. */
5525 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
5527 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
5528 FALSE
, FALSE
, FALSE
);
5531 && (h
->type
== bfd_link_hash_defined
5532 || h
->type
== bfd_link_hash_defweak
)
5533 && ! bfd_is_abs_section (h
->u
.def
.section
))
5535 h
->u
.def
.section
->flags
|= SEC_KEEP
;
5539 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5540 the special case of debug info. (See bfd/stabs.c)
5541 Twiddle the flag here, to simplify later linker code. */
5542 if (link_info
.relocatable
)
5544 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5547 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5548 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5549 sec
->flags
&= ~SEC_EXCLUDE
;
5553 if (link_info
.gc_sections
)
5554 bfd_gc_sections (output_bfd
, &link_info
);
5557 /* Relax all sections until bfd_relax_section gives up. */
5560 relax_sections (void)
5562 /* Keep relaxing until bfd_relax_section gives up. */
5563 bfd_boolean relax_again
;
5567 relax_again
= FALSE
;
5569 /* Note: pe-dll.c does something like this also. If you find
5570 you need to change this code, you probably need to change
5571 pe-dll.c also. DJ */
5573 /* Do all the assignments with our current guesses as to
5575 lang_do_assignments ();
5577 /* We must do this after lang_do_assignments, because it uses
5579 lang_reset_memory_regions ();
5581 /* Perform another relax pass - this time we know where the
5582 globals are, so can make a better guess. */
5583 lang_size_sections (&relax_again
, FALSE
);
5585 while (relax_again
);
5591 current_target
= default_target
;
5593 /* Open the output file. */
5594 lang_for_each_statement (ldlang_open_output
);
5597 ldemul_create_output_section_statements ();
5599 /* Add to the hash table all undefineds on the command line. */
5600 lang_place_undefineds ();
5602 if (!bfd_section_already_linked_table_init ())
5603 einfo (_("%P%F: Failed to create hash table\n"));
5605 /* Create a bfd for each input file. */
5606 current_target
= default_target
;
5607 open_input_bfds (statement_list
.head
, FALSE
);
5609 link_info
.gc_sym_list
= &entry_symbol
;
5610 if (entry_symbol
.name
== NULL
)
5611 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
5613 ldemul_after_open ();
5615 bfd_section_already_linked_table_free ();
5617 /* Make sure that we're not mixing architectures. We call this
5618 after all the input files have been opened, but before we do any
5619 other processing, so that any operations merge_private_bfd_data
5620 does on the output file will be known during the rest of the
5624 /* Handle .exports instead of a version script if we're told to do so. */
5625 if (command_line
.version_exports_section
)
5626 lang_do_version_exports_section ();
5628 /* Build all sets based on the information gathered from the input
5630 ldctor_build_sets ();
5632 /* Remove unreferenced sections if asked to. */
5633 lang_gc_sections ();
5635 /* Size up the common data. */
5638 /* Update wild statements. */
5639 update_wild_statements (statement_list
.head
);
5641 /* Run through the contours of the script and attach input sections
5642 to the correct output sections. */
5643 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
5645 /* Find any sections not attached explicitly and handle them. */
5646 lang_place_orphans ();
5648 if (! link_info
.relocatable
)
5652 /* Merge SEC_MERGE sections. This has to be done after GC of
5653 sections, so that GCed sections are not merged, but before
5654 assigning dynamic symbols, since removing whole input sections
5656 bfd_merge_sections (output_bfd
, &link_info
);
5658 /* Look for a text section and set the readonly attribute in it. */
5659 found
= bfd_get_section_by_name (output_bfd
, ".text");
5663 if (config
.text_read_only
)
5664 found
->flags
|= SEC_READONLY
;
5666 found
->flags
&= ~SEC_READONLY
;
5670 /* Do anything special before sizing sections. This is where ELF
5671 and other back-ends size dynamic sections. */
5672 ldemul_before_allocation ();
5674 /* We must record the program headers before we try to fix the
5675 section positions, since they will affect SIZEOF_HEADERS. */
5676 lang_record_phdrs ();
5678 /* Size up the sections. */
5679 lang_size_sections (NULL
, !command_line
.relax
);
5681 /* Now run around and relax if we can. */
5682 if (command_line
.relax
)
5684 /* We may need more than one relaxation pass. */
5685 int i
= link_info
.relax_pass
;
5687 /* The backend can use it to determine the current pass. */
5688 link_info
.relax_pass
= 0;
5693 link_info
.relax_pass
++;
5696 /* Final extra sizing to report errors. */
5697 lang_do_assignments ();
5698 lang_reset_memory_regions ();
5699 lang_size_sections (NULL
, TRUE
);
5702 /* See if anything special should be done now we know how big
5704 ldemul_after_allocation ();
5706 /* Fix any .startof. or .sizeof. symbols. */
5707 lang_set_startof ();
5709 /* Do all the assignments, now that we know the final resting places
5710 of all the symbols. */
5712 lang_do_assignments ();
5716 /* Make sure that the section addresses make sense. */
5717 if (! link_info
.relocatable
5718 && command_line
.check_section_addresses
)
5719 lang_check_section_addresses ();
5724 /* EXPORTED TO YACC */
5727 lang_add_wild (struct wildcard_spec
*filespec
,
5728 struct wildcard_list
*section_list
,
5729 bfd_boolean keep_sections
)
5731 struct wildcard_list
*curr
, *next
;
5732 lang_wild_statement_type
*new;
5734 /* Reverse the list as the parser puts it back to front. */
5735 for (curr
= section_list
, section_list
= NULL
;
5737 section_list
= curr
, curr
= next
)
5739 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
5740 placed_commons
= TRUE
;
5743 curr
->next
= section_list
;
5746 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
5748 if (strcmp (filespec
->name
, "*") == 0)
5749 filespec
->name
= NULL
;
5750 else if (! wildcardp (filespec
->name
))
5751 lang_has_input_file
= TRUE
;
5754 new = new_stat (lang_wild_statement
, stat_ptr
);
5755 new->filename
= NULL
;
5756 new->filenames_sorted
= FALSE
;
5757 if (filespec
!= NULL
)
5759 new->filename
= filespec
->name
;
5760 new->filenames_sorted
= filespec
->sorted
== by_name
;
5762 new->section_list
= section_list
;
5763 new->keep_sections
= keep_sections
;
5764 lang_list_init (&new->children
);
5765 analyze_walk_wild_section_handler (new);
5769 lang_section_start (const char *name
, etree_type
*address
,
5770 const segment_type
*segment
)
5772 lang_address_statement_type
*ad
;
5774 ad
= new_stat (lang_address_statement
, stat_ptr
);
5775 ad
->section_name
= name
;
5776 ad
->address
= address
;
5777 ad
->segment
= segment
;
5780 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5781 because of a -e argument on the command line, or zero if this is
5782 called by ENTRY in a linker script. Command line arguments take
5786 lang_add_entry (const char *name
, bfd_boolean cmdline
)
5788 if (entry_symbol
.name
== NULL
5790 || ! entry_from_cmdline
)
5792 entry_symbol
.name
= name
;
5793 entry_from_cmdline
= cmdline
;
5797 /* Set the default start symbol to NAME. .em files should use this,
5798 not lang_add_entry, to override the use of "start" if neither the
5799 linker script nor the command line specifies an entry point. NAME
5800 must be permanently allocated. */
5802 lang_default_entry (const char *name
)
5804 entry_symbol_default
= name
;
5808 lang_add_target (const char *name
)
5810 lang_target_statement_type
*new;
5812 new = new_stat (lang_target_statement
, stat_ptr
);
5817 lang_add_map (const char *name
)
5824 map_option_f
= TRUE
;
5832 lang_add_fill (fill_type
*fill
)
5834 lang_fill_statement_type
*new;
5836 new = new_stat (lang_fill_statement
, stat_ptr
);
5841 lang_add_data (int type
, union etree_union
*exp
)
5843 lang_data_statement_type
*new;
5845 new = new_stat (lang_data_statement
, stat_ptr
);
5850 /* Create a new reloc statement. RELOC is the BFD relocation type to
5851 generate. HOWTO is the corresponding howto structure (we could
5852 look this up, but the caller has already done so). SECTION is the
5853 section to generate a reloc against, or NAME is the name of the
5854 symbol to generate a reloc against. Exactly one of SECTION and
5855 NAME must be NULL. ADDEND is an expression for the addend. */
5858 lang_add_reloc (bfd_reloc_code_real_type reloc
,
5859 reloc_howto_type
*howto
,
5862 union etree_union
*addend
)
5864 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
5868 p
->section
= section
;
5870 p
->addend_exp
= addend
;
5872 p
->addend_value
= 0;
5873 p
->output_section
= NULL
;
5874 p
->output_offset
= 0;
5877 lang_assignment_statement_type
*
5878 lang_add_assignment (etree_type
*exp
)
5880 lang_assignment_statement_type
*new;
5882 new = new_stat (lang_assignment_statement
, stat_ptr
);
5888 lang_add_attribute (enum statement_enum attribute
)
5890 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
5894 lang_startup (const char *name
)
5896 if (startup_file
!= NULL
)
5898 einfo (_("%P%F: multiple STARTUP files\n"));
5900 first_file
->filename
= name
;
5901 first_file
->local_sym_name
= name
;
5902 first_file
->real
= TRUE
;
5904 startup_file
= name
;
5908 lang_float (bfd_boolean maybe
)
5910 lang_float_flag
= maybe
;
5914 /* Work out the load- and run-time regions from a script statement, and
5915 store them in *LMA_REGION and *REGION respectively.
5917 MEMSPEC is the name of the run-time region, or the value of
5918 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5919 LMA_MEMSPEC is the name of the load-time region, or null if the
5920 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5921 had an explicit load address.
5923 It is an error to specify both a load region and a load address. */
5926 lang_get_regions (lang_memory_region_type
**region
,
5927 lang_memory_region_type
**lma_region
,
5928 const char *memspec
,
5929 const char *lma_memspec
,
5930 bfd_boolean have_lma
,
5931 bfd_boolean have_vma
)
5933 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
5935 /* If no runtime region or VMA has been specified, but the load region
5936 has been specified, then use the load region for the runtime region
5938 if (lma_memspec
!= NULL
5940 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
5941 *region
= *lma_region
;
5943 *region
= lang_memory_region_lookup (memspec
, FALSE
);
5945 if (have_lma
&& lma_memspec
!= 0)
5946 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5950 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
5951 lang_output_section_phdr_list
*phdrs
,
5952 const char *lma_memspec
)
5954 lang_get_regions (¤t_section
->region
,
5955 ¤t_section
->lma_region
,
5956 memspec
, lma_memspec
,
5957 current_section
->load_base
!= NULL
,
5958 current_section
->addr_tree
!= NULL
);
5959 current_section
->fill
= fill
;
5960 current_section
->phdrs
= phdrs
;
5961 stat_ptr
= &statement_list
;
5964 /* Create an absolute symbol with the given name with the value of the
5965 address of first byte of the section named.
5967 If the symbol already exists, then do nothing. */
5970 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
5972 struct bfd_link_hash_entry
*h
;
5974 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5976 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5978 if (h
->type
== bfd_link_hash_new
5979 || h
->type
== bfd_link_hash_undefined
)
5983 h
->type
= bfd_link_hash_defined
;
5985 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5989 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
5991 h
->u
.def
.section
= bfd_abs_section_ptr
;
5995 /* Create an absolute symbol with the given name with the value of the
5996 address of the first byte after the end of the section named.
5998 If the symbol already exists, then do nothing. */
6001 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
6003 struct bfd_link_hash_entry
*h
;
6005 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
6007 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
6009 if (h
->type
== bfd_link_hash_new
6010 || h
->type
== bfd_link_hash_undefined
)
6014 h
->type
= bfd_link_hash_defined
;
6016 sec
= bfd_get_section_by_name (output_bfd
, secname
);
6020 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
6021 + TO_ADDR (sec
->size
));
6023 h
->u
.def
.section
= bfd_abs_section_ptr
;
6028 lang_statement_append (lang_statement_list_type
*list
,
6029 lang_statement_union_type
*element
,
6030 lang_statement_union_type
**field
)
6032 *(list
->tail
) = element
;
6036 /* Set the output format type. -oformat overrides scripts. */
6039 lang_add_output_format (const char *format
,
6044 if (output_target
== NULL
|| !from_script
)
6046 if (command_line
.endian
== ENDIAN_BIG
6049 else if (command_line
.endian
== ENDIAN_LITTLE
6053 output_target
= format
;
6057 /* Enter a group. This creates a new lang_group_statement, and sets
6058 stat_ptr to build new statements within the group. */
6061 lang_enter_group (void)
6063 lang_group_statement_type
*g
;
6065 g
= new_stat (lang_group_statement
, stat_ptr
);
6066 lang_list_init (&g
->children
);
6067 stat_ptr
= &g
->children
;
6070 /* Leave a group. This just resets stat_ptr to start writing to the
6071 regular list of statements again. Note that this will not work if
6072 groups can occur inside anything else which can adjust stat_ptr,
6073 but currently they can't. */
6076 lang_leave_group (void)
6078 stat_ptr
= &statement_list
;
6081 /* Add a new program header. This is called for each entry in a PHDRS
6082 command in a linker script. */
6085 lang_new_phdr (const char *name
,
6087 bfd_boolean filehdr
,
6092 struct lang_phdr
*n
, **pp
;
6094 n
= stat_alloc (sizeof (struct lang_phdr
));
6097 n
->type
= exp_get_value_int (type
, 0, "program header type");
6098 n
->filehdr
= filehdr
;
6103 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6108 /* Record the program header information in the output BFD. FIXME: We
6109 should not be calling an ELF specific function here. */
6112 lang_record_phdrs (void)
6116 lang_output_section_phdr_list
*last
;
6117 struct lang_phdr
*l
;
6118 lang_output_section_statement_type
*os
;
6121 secs
= xmalloc (alc
* sizeof (asection
*));
6123 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
6130 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6134 lang_output_section_phdr_list
*pl
;
6136 if (os
->constraint
== -1)
6144 if (os
->sectype
== noload_section
6145 || os
->bfd_section
== NULL
6146 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6151 if (os
->bfd_section
== NULL
)
6154 for (; pl
!= NULL
; pl
= pl
->next
)
6156 if (strcmp (pl
->name
, l
->name
) == 0)
6161 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6163 secs
[c
] = os
->bfd_section
;
6170 if (l
->flags
== NULL
)
6173 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6178 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6180 if (! bfd_record_phdr (output_bfd
, l
->type
,
6181 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6182 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6183 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6188 /* Make sure all the phdr assignments succeeded. */
6189 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6193 lang_output_section_phdr_list
*pl
;
6195 if (os
->constraint
== -1
6196 || os
->bfd_section
== NULL
)
6199 for (pl
= os
->phdrs
;
6202 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6203 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6204 os
->name
, pl
->name
);
6208 /* Record a list of sections which may not be cross referenced. */
6211 lang_add_nocrossref (lang_nocrossref_type
*l
)
6213 struct lang_nocrossrefs
*n
;
6215 n
= xmalloc (sizeof *n
);
6216 n
->next
= nocrossref_list
;
6218 nocrossref_list
= n
;
6220 /* Set notice_all so that we get informed about all symbols. */
6221 link_info
.notice_all
= TRUE
;
6224 /* Overlay handling. We handle overlays with some static variables. */
6226 /* The overlay virtual address. */
6227 static etree_type
*overlay_vma
;
6228 /* And subsection alignment. */
6229 static etree_type
*overlay_subalign
;
6231 /* An expression for the maximum section size seen so far. */
6232 static etree_type
*overlay_max
;
6234 /* A list of all the sections in this overlay. */
6236 struct overlay_list
{
6237 struct overlay_list
*next
;
6238 lang_output_section_statement_type
*os
;
6241 static struct overlay_list
*overlay_list
;
6243 /* Start handling an overlay. */
6246 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6248 /* The grammar should prevent nested overlays from occurring. */
6249 ASSERT (overlay_vma
== NULL
6250 && overlay_subalign
== NULL
6251 && overlay_max
== NULL
);
6253 overlay_vma
= vma_expr
;
6254 overlay_subalign
= subalign
;
6257 /* Start a section in an overlay. We handle this by calling
6258 lang_enter_output_section_statement with the correct VMA.
6259 lang_leave_overlay sets up the LMA and memory regions. */
6262 lang_enter_overlay_section (const char *name
)
6264 struct overlay_list
*n
;
6267 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
6268 0, overlay_subalign
, 0, 0);
6270 /* If this is the first section, then base the VMA of future
6271 sections on this one. This will work correctly even if `.' is
6272 used in the addresses. */
6273 if (overlay_list
== NULL
)
6274 overlay_vma
= exp_nameop (ADDR
, name
);
6276 /* Remember the section. */
6277 n
= xmalloc (sizeof *n
);
6278 n
->os
= current_section
;
6279 n
->next
= overlay_list
;
6282 size
= exp_nameop (SIZEOF
, name
);
6284 /* Arrange to work out the maximum section end address. */
6285 if (overlay_max
== NULL
)
6288 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6291 /* Finish a section in an overlay. There isn't any special to do
6295 lang_leave_overlay_section (fill_type
*fill
,
6296 lang_output_section_phdr_list
*phdrs
)
6303 name
= current_section
->name
;
6305 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6306 region and that no load-time region has been specified. It doesn't
6307 really matter what we say here, since lang_leave_overlay will
6309 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6311 /* Define the magic symbols. */
6313 clean
= xmalloc (strlen (name
) + 1);
6315 for (s1
= name
; *s1
!= '\0'; s1
++)
6316 if (ISALNUM (*s1
) || *s1
== '_')
6320 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6321 sprintf (buf
, "__load_start_%s", clean
);
6322 lang_add_assignment (exp_assop ('=', buf
,
6323 exp_nameop (LOADADDR
, name
)));
6325 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6326 sprintf (buf
, "__load_stop_%s", clean
);
6327 lang_add_assignment (exp_assop ('=', buf
,
6329 exp_nameop (LOADADDR
, name
),
6330 exp_nameop (SIZEOF
, name
))));
6335 /* Finish an overlay. If there are any overlay wide settings, this
6336 looks through all the sections in the overlay and sets them. */
6339 lang_leave_overlay (etree_type
*lma_expr
,
6342 const char *memspec
,
6343 lang_output_section_phdr_list
*phdrs
,
6344 const char *lma_memspec
)
6346 lang_memory_region_type
*region
;
6347 lang_memory_region_type
*lma_region
;
6348 struct overlay_list
*l
;
6349 lang_nocrossref_type
*nocrossref
;
6351 lang_get_regions (®ion
, &lma_region
,
6352 memspec
, lma_memspec
,
6353 lma_expr
!= NULL
, FALSE
);
6357 /* After setting the size of the last section, set '.' to end of the
6359 if (overlay_list
!= NULL
)
6360 overlay_list
->os
->update_dot_tree
6361 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6366 struct overlay_list
*next
;
6368 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6371 l
->os
->region
= region
;
6372 l
->os
->lma_region
= lma_region
;
6374 /* The first section has the load address specified in the
6375 OVERLAY statement. The rest are worked out from that.
6376 The base address is not needed (and should be null) if
6377 an LMA region was specified. */
6379 l
->os
->load_base
= lma_expr
;
6381 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6382 l
->os
->phdrs
= phdrs
;
6386 lang_nocrossref_type
*nc
;
6388 nc
= xmalloc (sizeof *nc
);
6389 nc
->name
= l
->os
->name
;
6390 nc
->next
= nocrossref
;
6399 if (nocrossref
!= NULL
)
6400 lang_add_nocrossref (nocrossref
);
6403 overlay_list
= NULL
;
6407 /* Version handling. This is only useful for ELF. */
6409 /* This global variable holds the version tree that we build. */
6411 struct bfd_elf_version_tree
*lang_elf_version_info
;
6413 /* If PREV is NULL, return first version pattern matching particular symbol.
6414 If PREV is non-NULL, return first version pattern matching particular
6415 symbol after PREV (previously returned by lang_vers_match). */
6417 static struct bfd_elf_version_expr
*
6418 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6419 struct bfd_elf_version_expr
*prev
,
6422 const char *cxx_sym
= sym
;
6423 const char *java_sym
= sym
;
6424 struct bfd_elf_version_expr
*expr
= NULL
;
6426 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6428 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6432 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6434 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6439 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6441 struct bfd_elf_version_expr e
;
6443 switch (prev
? prev
->mask
: 0)
6446 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6449 expr
= htab_find (head
->htab
, &e
);
6450 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
6451 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
6457 case BFD_ELF_VERSION_C_TYPE
:
6458 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6461 expr
= htab_find (head
->htab
, &e
);
6462 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
6463 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6469 case BFD_ELF_VERSION_CXX_TYPE
:
6470 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6472 e
.symbol
= java_sym
;
6473 expr
= htab_find (head
->htab
, &e
);
6474 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
6475 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6486 /* Finally, try the wildcards. */
6487 if (prev
== NULL
|| prev
->symbol
)
6488 expr
= head
->remaining
;
6491 for (; expr
; expr
= expr
->next
)
6498 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
6501 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6503 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6507 if (fnmatch (expr
->pattern
, s
, 0) == 0)
6513 free ((char *) cxx_sym
);
6514 if (java_sym
!= sym
)
6515 free ((char *) java_sym
);
6519 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6520 return a string pointing to the symbol name. */
6523 realsymbol (const char *pattern
)
6526 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
6527 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
6529 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
6531 /* It is a glob pattern only if there is no preceding
6533 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
6541 /* Remove the preceding backslash. */
6548 backslash
= *p
== '\\';
6563 /* This is called for each variable name or match expression. NEW is
6564 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6565 pattern to be matched against symbol names. */
6567 struct bfd_elf_version_expr
*
6568 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
6571 bfd_boolean literal_p
)
6573 struct bfd_elf_version_expr
*ret
;
6575 ret
= xmalloc (sizeof *ret
);
6577 ret
->pattern
= literal_p
? NULL
: new;
6580 ret
->symbol
= literal_p
? new : realsymbol (new);
6582 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
6583 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6584 else if (strcasecmp (lang
, "C++") == 0)
6585 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
6586 else if (strcasecmp (lang
, "Java") == 0)
6587 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
6590 einfo (_("%X%P: unknown language `%s' in version information\n"),
6592 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6595 return ldemul_new_vers_pattern (ret
);
6598 /* This is called for each set of variable names and match
6601 struct bfd_elf_version_tree
*
6602 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
6603 struct bfd_elf_version_expr
*locals
)
6605 struct bfd_elf_version_tree
*ret
;
6607 ret
= xcalloc (1, sizeof *ret
);
6608 ret
->globals
.list
= globals
;
6609 ret
->locals
.list
= locals
;
6610 ret
->match
= lang_vers_match
;
6611 ret
->name_indx
= (unsigned int) -1;
6615 /* This static variable keeps track of version indices. */
6617 static int version_index
;
6620 version_expr_head_hash (const void *p
)
6622 const struct bfd_elf_version_expr
*e
= p
;
6624 return htab_hash_string (e
->symbol
);
6628 version_expr_head_eq (const void *p1
, const void *p2
)
6630 const struct bfd_elf_version_expr
*e1
= p1
;
6631 const struct bfd_elf_version_expr
*e2
= p2
;
6633 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
6637 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
6640 struct bfd_elf_version_expr
*e
, *next
;
6641 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
6643 for (e
= head
->list
; e
; e
= e
->next
)
6647 head
->mask
|= e
->mask
;
6652 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
6653 version_expr_head_eq
, NULL
);
6654 list_loc
= &head
->list
;
6655 remaining_loc
= &head
->remaining
;
6656 for (e
= head
->list
; e
; e
= next
)
6662 remaining_loc
= &e
->next
;
6666 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
6670 struct bfd_elf_version_expr
*e1
, *last
;
6676 if (e1
->mask
== e
->mask
)
6684 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
6688 /* This is a duplicate. */
6689 /* FIXME: Memory leak. Sometimes pattern is not
6690 xmalloced alone, but in larger chunk of memory. */
6691 /* free (e->symbol); */
6696 e
->next
= last
->next
;
6704 list_loc
= &e
->next
;
6708 *remaining_loc
= NULL
;
6709 *list_loc
= head
->remaining
;
6712 head
->remaining
= head
->list
;
6715 /* This is called when we know the name and dependencies of the
6719 lang_register_vers_node (const char *name
,
6720 struct bfd_elf_version_tree
*version
,
6721 struct bfd_elf_version_deps
*deps
)
6723 struct bfd_elf_version_tree
*t
, **pp
;
6724 struct bfd_elf_version_expr
*e1
;
6729 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
6730 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
6732 einfo (_("%X%P: anonymous version tag cannot be combined"
6733 " with other version tags\n"));
6738 /* Make sure this node has a unique name. */
6739 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6740 if (strcmp (t
->name
, name
) == 0)
6741 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
6743 lang_finalize_version_expr_head (&version
->globals
);
6744 lang_finalize_version_expr_head (&version
->locals
);
6746 /* Check the global and local match names, and make sure there
6747 aren't any duplicates. */
6749 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
6751 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6753 struct bfd_elf_version_expr
*e2
;
6755 if (t
->locals
.htab
&& e1
->symbol
)
6757 e2
= htab_find (t
->locals
.htab
, e1
);
6758 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6760 if (e1
->mask
== e2
->mask
)
6761 einfo (_("%X%P: duplicate expression `%s'"
6762 " in version information\n"), e1
->symbol
);
6766 else if (!e1
->symbol
)
6767 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6768 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6769 && e1
->mask
== e2
->mask
)
6770 einfo (_("%X%P: duplicate expression `%s'"
6771 " in version information\n"), e1
->pattern
);
6775 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
6777 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6779 struct bfd_elf_version_expr
*e2
;
6781 if (t
->globals
.htab
&& e1
->symbol
)
6783 e2
= htab_find (t
->globals
.htab
, e1
);
6784 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6786 if (e1
->mask
== e2
->mask
)
6787 einfo (_("%X%P: duplicate expression `%s'"
6788 " in version information\n"),
6793 else if (!e1
->symbol
)
6794 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6795 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6796 && e1
->mask
== e2
->mask
)
6797 einfo (_("%X%P: duplicate expression `%s'"
6798 " in version information\n"), e1
->pattern
);
6802 version
->deps
= deps
;
6803 version
->name
= name
;
6804 if (name
[0] != '\0')
6807 version
->vernum
= version_index
;
6810 version
->vernum
= 0;
6812 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6817 /* This is called when we see a version dependency. */
6819 struct bfd_elf_version_deps
*
6820 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
6822 struct bfd_elf_version_deps
*ret
;
6823 struct bfd_elf_version_tree
*t
;
6825 ret
= xmalloc (sizeof *ret
);
6828 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6830 if (strcmp (t
->name
, name
) == 0)
6832 ret
->version_needed
= t
;
6837 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
6843 lang_do_version_exports_section (void)
6845 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
6847 LANG_FOR_EACH_INPUT_STATEMENT (is
)
6849 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
6857 contents
= xmalloc (len
);
6858 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
6859 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
6862 while (p
< contents
+ len
)
6864 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
6865 p
= strchr (p
, '\0') + 1;
6868 /* Do not free the contents, as we used them creating the regex. */
6870 /* Do not include this section in the link. */
6871 sec
->flags
|= SEC_EXCLUDE
;
6874 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
6875 lang_register_vers_node (command_line
.version_exports_section
,
6876 lang_new_vers_node (greg
, lreg
), NULL
);
6880 lang_add_unique (const char *name
)
6882 struct unique_sections
*ent
;
6884 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
6885 if (strcmp (ent
->name
, name
) == 0)
6888 ent
= xmalloc (sizeof *ent
);
6889 ent
->name
= xstrdup (name
);
6890 ent
->next
= unique_section_list
;
6891 unique_section_list
= ent
;