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 /* Locals variables. */
49 static struct obstack stat_obstack
;
50 static struct obstack map_obstack
;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file
;
55 static lang_statement_list_type input_file_chain
;
56 static bfd_boolean placed_commons
= FALSE
;
57 static bfd_boolean stripped_excluded_sections
= FALSE
;
58 static lang_output_section_statement_type
*default_common_section
;
59 static bfd_boolean map_option_f
;
60 static bfd_vma print_dot
;
61 static lang_input_statement_type
*first_file
;
62 static const char *current_target
;
63 static const char *output_target
;
64 static lang_statement_list_type statement_list
;
65 static struct lang_phdr
*lang_phdr_list
;
66 static struct bfd_hash_table lang_definedness_table
;
68 /* Forward declarations. */
69 static void exp_init_os (etree_type
*);
70 static void init_map_userdata (bfd
*, asection
*, void *);
71 static lang_input_statement_type
*lookup_name (const char *);
72 static bfd_boolean
load_symbols (lang_input_statement_type
*,
73 lang_statement_list_type
*);
74 static struct bfd_hash_entry
*lang_definedness_newfunc
75 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
76 static void insert_undefined (const char *);
77 static void print_all_symbols (asection
*);
78 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
79 static void print_statement (lang_statement_union_type
*,
80 lang_output_section_statement_type
*);
81 static void print_statement_list (lang_statement_union_type
*,
82 lang_output_section_statement_type
*);
83 static void print_statements (void);
84 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
85 static void lang_record_phdrs (void);
86 static void lang_do_version_exports_section (void);
88 /* Exported variables. */
89 lang_output_section_statement_type
*abs_output_section
;
90 lang_statement_list_type lang_output_section_statement
;
91 lang_statement_list_type
*stat_ptr
= &statement_list
;
92 lang_statement_list_type file_chain
= { NULL
, NULL
};
93 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
94 static const char *entry_symbol_default
= "start";
95 const char *entry_section
= ".text";
96 bfd_boolean entry_from_cmdline
;
97 bfd_boolean lang_has_input_file
= FALSE
;
98 bfd_boolean had_output_filename
= FALSE
;
99 bfd_boolean lang_float_flag
= FALSE
;
100 bfd_boolean delete_output_file_on_failure
= FALSE
;
101 struct lang_nocrossrefs
*nocrossref_list
;
102 static struct unique_sections
*unique_section_list
;
103 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
105 /* Functions that traverse the linker script and might evaluate
106 DEFINED() need to increment this. */
107 int lang_statement_iteration
= 0;
109 etree_type
*base
; /* Relocation base - or null */
111 /* Return TRUE if the PATTERN argument is a wildcard pattern.
112 Although backslashes are treated specially if a pattern contains
113 wildcards, we do not consider the mere presence of a backslash to
114 be enough to cause the pattern to be treated as a wildcard.
115 That lets us handle DOS filenames more naturally. */
116 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
118 #define new_stat(x, y) \
119 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
121 #define outside_section_address(q) \
122 ((q)->output_offset + (q)->output_section->vma)
124 #define outside_symbol_address(q) \
125 ((q)->value + outside_section_address (q->section))
127 #define SECTION_NAME_MAP_LENGTH (16)
130 stat_alloc (size_t size
)
132 return obstack_alloc (&stat_obstack
, size
);
136 unique_section_p (const asection
*sec
)
138 struct unique_sections
*unam
;
141 if (link_info
.relocatable
142 && sec
->owner
!= NULL
143 && bfd_is_group_section (sec
->owner
, sec
))
147 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
148 if (wildcardp (unam
->name
)
149 ? fnmatch (unam
->name
, secnam
, 0) == 0
150 : strcmp (unam
->name
, secnam
) == 0)
158 /* Generic traversal routines for finding matching sections. */
160 /* Try processing a section against a wildcard. This just calls
161 the callback unless the filename exclusion list is present
162 and excludes the file. It's hardly ever present so this
163 function is very fast. */
166 walk_wild_consider_section (lang_wild_statement_type
*ptr
,
167 lang_input_statement_type
*file
,
169 struct wildcard_list
*sec
,
173 bfd_boolean skip
= FALSE
;
174 struct name_list
*list_tmp
;
176 /* Don't process sections from files which were
178 for (list_tmp
= sec
->spec
.exclude_name_list
;
180 list_tmp
= list_tmp
->next
)
182 bfd_boolean is_wildcard
= wildcardp (list_tmp
->name
);
184 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
186 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
188 /* If this file is part of an archive, and the archive is
189 excluded, exclude this file. */
190 if (! skip
&& file
->the_bfd
!= NULL
191 && file
->the_bfd
->my_archive
!= NULL
192 && file
->the_bfd
->my_archive
->filename
!= NULL
)
195 skip
= fnmatch (list_tmp
->name
,
196 file
->the_bfd
->my_archive
->filename
,
199 skip
= strcmp (list_tmp
->name
,
200 file
->the_bfd
->my_archive
->filename
) == 0;
208 (*callback
) (ptr
, sec
, s
, file
, data
);
211 /* Lowest common denominator routine that can handle everything correctly,
215 walk_wild_section_general (lang_wild_statement_type
*ptr
,
216 lang_input_statement_type
*file
,
221 struct wildcard_list
*sec
;
223 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
225 sec
= ptr
->section_list
;
227 (*callback
) (ptr
, sec
, s
, file
, data
);
231 bfd_boolean skip
= FALSE
;
233 if (sec
->spec
.name
!= NULL
)
235 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
237 if (wildcardp (sec
->spec
.name
))
238 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
240 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
244 walk_wild_consider_section (ptr
, file
, s
, sec
, callback
, data
);
251 /* Routines to find a single section given its name. If there's more
252 than one section with that name, we report that. */
256 asection
*found_section
;
257 bfd_boolean multiple_sections_found
;
258 } section_iterator_callback_data
;
261 section_iterator_callback (bfd
*bfd ATTRIBUTE_UNUSED
, asection
*s
, void *data
)
263 section_iterator_callback_data
*d
= data
;
265 if (d
->found_section
!= NULL
)
267 d
->multiple_sections_found
= TRUE
;
271 d
->found_section
= s
;
276 find_section (lang_input_statement_type
*file
,
277 struct wildcard_list
*sec
,
278 bfd_boolean
*multiple_sections_found
)
280 section_iterator_callback_data cb_data
= { NULL
, FALSE
};
282 bfd_get_section_by_name_if (file
->the_bfd
, sec
->spec
.name
,
283 section_iterator_callback
, &cb_data
);
284 *multiple_sections_found
= cb_data
.multiple_sections_found
;
285 return cb_data
.found_section
;
288 /* Code for handling simple wildcards without going through fnmatch,
289 which can be expensive because of charset translations etc. */
291 /* A simple wild is a literal string followed by a single '*',
292 where the literal part is at least 4 characters long. */
295 is_simple_wild (const char *name
)
297 size_t len
= strcspn (name
, "*?[");
298 return len
>= 4 && name
[len
] == '*' && name
[len
+ 1] == '\0';
302 match_simple_wild (const char *pattern
, const char *name
)
304 /* The first four characters of the pattern are guaranteed valid
305 non-wildcard characters. So we can go faster. */
306 if (pattern
[0] != name
[0] || pattern
[1] != name
[1]
307 || pattern
[2] != name
[2] || pattern
[3] != name
[3])
312 while (*pattern
!= '*')
313 if (*name
++ != *pattern
++)
319 /* Specialized, optimized routines for handling different kinds of
323 walk_wild_section_specs1_wild0 (lang_wild_statement_type
*ptr
,
324 lang_input_statement_type
*file
,
328 /* We can just do a hash lookup for the section with the right name.
329 But if that lookup discovers more than one section with the name
330 (should be rare), we fall back to the general algorithm because
331 we would otherwise have to sort the sections to make sure they
332 get processed in the bfd's order. */
333 bfd_boolean multiple_sections_found
;
334 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
335 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
337 if (multiple_sections_found
)
338 walk_wild_section_general (ptr
, file
, callback
, data
);
340 walk_wild_consider_section (ptr
, file
, s0
, sec0
, callback
, data
);
344 walk_wild_section_specs1_wild1 (lang_wild_statement_type
*ptr
,
345 lang_input_statement_type
*file
,
350 struct wildcard_list
*wildsec0
= ptr
->handler_data
[0];
352 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
354 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
355 bfd_boolean skip
= !match_simple_wild (wildsec0
->spec
.name
, sname
);
358 walk_wild_consider_section (ptr
, file
, s
, wildsec0
, callback
, data
);
363 walk_wild_section_specs2_wild1 (lang_wild_statement_type
*ptr
,
364 lang_input_statement_type
*file
,
369 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
370 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
371 bfd_boolean multiple_sections_found
;
372 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
374 if (multiple_sections_found
)
376 walk_wild_section_general (ptr
, file
, callback
, data
);
380 /* Note that if the section was not found, s0 is NULL and
381 we'll simply never succeed the s == s0 test below. */
382 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
384 /* Recall that in this code path, a section cannot satisfy more
385 than one spec, so if s == s0 then it cannot match
388 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
391 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
392 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
395 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
,
402 walk_wild_section_specs3_wild2 (lang_wild_statement_type
*ptr
,
403 lang_input_statement_type
*file
,
408 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
409 struct wildcard_list
*wildsec1
= ptr
->handler_data
[1];
410 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
411 bfd_boolean multiple_sections_found
;
412 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
);
414 if (multiple_sections_found
)
416 walk_wild_section_general (ptr
, file
, callback
, data
);
420 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
423 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
426 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
427 bfd_boolean skip
= !match_simple_wild (wildsec1
->spec
.name
, sname
);
430 walk_wild_consider_section (ptr
, file
, s
, wildsec1
, callback
, data
);
433 skip
= !match_simple_wild (wildsec2
->spec
.name
, sname
);
435 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
443 walk_wild_section_specs4_wild2 (lang_wild_statement_type
*ptr
,
444 lang_input_statement_type
*file
,
449 struct wildcard_list
*sec0
= ptr
->handler_data
[0];
450 struct wildcard_list
*sec1
= ptr
->handler_data
[1];
451 struct wildcard_list
*wildsec2
= ptr
->handler_data
[2];
452 struct wildcard_list
*wildsec3
= ptr
->handler_data
[3];
453 bfd_boolean multiple_sections_found
;
454 asection
*s0
= find_section (file
, sec0
, &multiple_sections_found
), *s1
;
456 if (multiple_sections_found
)
458 walk_wild_section_general (ptr
, file
, callback
, data
);
462 s1
= find_section (file
, sec1
, &multiple_sections_found
);
463 if (multiple_sections_found
)
465 walk_wild_section_general (ptr
, file
, callback
, data
);
469 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
472 walk_wild_consider_section (ptr
, file
, s
, sec0
, callback
, data
);
475 walk_wild_consider_section (ptr
, file
, s
, sec1
, callback
, data
);
478 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
479 bfd_boolean skip
= !match_simple_wild (wildsec2
->spec
.name
,
483 walk_wild_consider_section (ptr
, file
, s
, wildsec2
, callback
,
487 skip
= !match_simple_wild (wildsec3
->spec
.name
, sname
);
489 walk_wild_consider_section (ptr
, file
, s
, wildsec3
,
497 walk_wild_section (lang_wild_statement_type
*ptr
,
498 lang_input_statement_type
*file
,
502 if (file
->just_syms_flag
)
505 (*ptr
->walk_wild_section_handler
) (ptr
, file
, callback
, data
);
508 /* Returns TRUE when name1 is a wildcard spec that might match
509 something name2 can match. We're conservative: we return FALSE
510 only if the prefixes of name1 and name2 are different up to the
511 first wildcard character. */
514 wild_spec_can_overlap (const char *name1
, const char *name2
)
516 size_t prefix1_len
= strcspn (name1
, "?*[");
517 size_t prefix2_len
= strcspn (name2
, "?*[");
518 size_t min_prefix_len
;
520 /* Note that if there is no wildcard character, then we treat the
521 terminating 0 as part of the prefix. Thus ".text" won't match
522 ".text." or ".text.*", for example. */
523 if (name1
[prefix1_len
] == '\0')
525 if (name2
[prefix2_len
] == '\0')
528 min_prefix_len
= prefix1_len
< prefix2_len
? prefix1_len
: prefix2_len
;
530 return memcmp (name1
, name2
, min_prefix_len
) == 0;
533 /* Select specialized code to handle various kinds of wildcard
537 analyze_walk_wild_section_handler (lang_wild_statement_type
*ptr
)
540 int wild_name_count
= 0;
541 struct wildcard_list
*sec
;
545 ptr
->walk_wild_section_handler
= walk_wild_section_general
;
547 /* Count how many wildcard_specs there are, and how many of those
548 actually use wildcards in the name. Also, bail out if any of the
549 wildcard names are NULL. (Can this actually happen?
550 walk_wild_section used to test for it.) And bail out if any
551 of the wildcards are more complex than a simple string
552 ending in a single '*'. */
553 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
556 if (sec
->spec
.name
== NULL
)
558 if (wildcardp (sec
->spec
.name
))
561 if (!is_simple_wild (sec
->spec
.name
))
566 /* The zero-spec case would be easy to optimize but it doesn't
567 happen in practice. Likewise, more than 4 specs doesn't
568 happen in practice. */
569 if (sec_count
== 0 || sec_count
> 4)
572 /* Check that no two specs can match the same section. */
573 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
575 struct wildcard_list
*sec2
;
576 for (sec2
= sec
->next
; sec2
!= NULL
; sec2
= sec2
->next
)
578 if (wild_spec_can_overlap (sec
->spec
.name
, sec2
->spec
.name
))
583 signature
= (sec_count
<< 8) + wild_name_count
;
587 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild0
;
590 ptr
->walk_wild_section_handler
= walk_wild_section_specs1_wild1
;
593 ptr
->walk_wild_section_handler
= walk_wild_section_specs2_wild1
;
596 ptr
->walk_wild_section_handler
= walk_wild_section_specs3_wild2
;
599 ptr
->walk_wild_section_handler
= walk_wild_section_specs4_wild2
;
605 /* Now fill the data array with pointers to the specs, first the
606 specs with non-wildcard names, then the specs with wildcard
607 names. It's OK to process the specs in different order from the
608 given order, because we've already determined that no section
609 will match more than one spec. */
611 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
612 if (!wildcardp (sec
->spec
.name
))
613 ptr
->handler_data
[data_counter
++] = sec
;
614 for (sec
= ptr
->section_list
; sec
!= NULL
; sec
= sec
->next
)
615 if (wildcardp (sec
->spec
.name
))
616 ptr
->handler_data
[data_counter
++] = sec
;
619 /* Handle a wild statement for a single file F. */
622 walk_wild_file (lang_wild_statement_type
*s
,
623 lang_input_statement_type
*f
,
627 if (f
->the_bfd
== NULL
628 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
629 walk_wild_section (s
, f
, callback
, data
);
634 /* This is an archive file. We must map each member of the
635 archive separately. */
636 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
637 while (member
!= NULL
)
639 /* When lookup_name is called, it will call the add_symbols
640 entry point for the archive. For each element of the
641 archive which is included, BFD will call ldlang_add_file,
642 which will set the usrdata field of the member to the
643 lang_input_statement. */
644 if (member
->usrdata
!= NULL
)
646 walk_wild_section (s
, member
->usrdata
, callback
, data
);
649 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
655 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
657 const char *file_spec
= s
->filename
;
659 if (file_spec
== NULL
)
661 /* Perform the iteration over all files in the list. */
662 LANG_FOR_EACH_INPUT_STATEMENT (f
)
664 walk_wild_file (s
, f
, callback
, data
);
667 else if (wildcardp (file_spec
))
669 LANG_FOR_EACH_INPUT_STATEMENT (f
)
671 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
672 walk_wild_file (s
, f
, callback
, data
);
677 lang_input_statement_type
*f
;
679 /* Perform the iteration over a single file. */
680 f
= lookup_name (file_spec
);
682 walk_wild_file (s
, f
, callback
, data
);
686 /* lang_for_each_statement walks the parse tree and calls the provided
687 function for each node. */
690 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
691 lang_statement_union_type
*s
)
693 for (; s
!= NULL
; s
= s
->header
.next
)
697 switch (s
->header
.type
)
699 case lang_constructors_statement_enum
:
700 lang_for_each_statement_worker (func
, constructor_list
.head
);
702 case lang_output_section_statement_enum
:
703 lang_for_each_statement_worker
704 (func
, s
->output_section_statement
.children
.head
);
706 case lang_wild_statement_enum
:
707 lang_for_each_statement_worker (func
,
708 s
->wild_statement
.children
.head
);
710 case lang_group_statement_enum
:
711 lang_for_each_statement_worker (func
,
712 s
->group_statement
.children
.head
);
714 case lang_data_statement_enum
:
715 case lang_reloc_statement_enum
:
716 case lang_object_symbols_statement_enum
:
717 case lang_output_statement_enum
:
718 case lang_target_statement_enum
:
719 case lang_input_section_enum
:
720 case lang_input_statement_enum
:
721 case lang_assignment_statement_enum
:
722 case lang_padding_statement_enum
:
723 case lang_address_statement_enum
:
724 case lang_fill_statement_enum
:
734 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
736 lang_for_each_statement_worker (func
, statement_list
.head
);
739 /*----------------------------------------------------------------------*/
742 lang_list_init (lang_statement_list_type
*list
)
745 list
->tail
= &list
->head
;
748 /* Build a new statement node for the parse tree. */
750 static lang_statement_union_type
*
751 new_statement (enum statement_enum type
,
753 lang_statement_list_type
*list
)
755 lang_statement_union_type
*new;
757 new = stat_alloc (size
);
758 new->header
.type
= type
;
759 new->header
.next
= NULL
;
760 lang_statement_append (list
, new, &new->header
.next
);
764 /* Build a new input file node for the language. There are several
765 ways in which we treat an input file, eg, we only look at symbols,
766 or prefix it with a -l etc.
768 We can be supplied with requests for input files more than once;
769 they may, for example be split over several lines like foo.o(.text)
770 foo.o(.data) etc, so when asked for a file we check that we haven't
771 got it already so we don't duplicate the bfd. */
773 static lang_input_statement_type
*
774 new_afile (const char *name
,
775 lang_input_file_enum_type file_type
,
777 bfd_boolean add_to_list
)
779 lang_input_statement_type
*p
;
782 p
= new_stat (lang_input_statement
, stat_ptr
);
785 p
= stat_alloc (sizeof (lang_input_statement_type
));
786 p
->header
.type
= lang_input_statement_enum
;
787 p
->header
.next
= NULL
;
790 lang_has_input_file
= TRUE
;
792 p
->sysrooted
= FALSE
;
795 case lang_input_file_is_symbols_only_enum
:
797 p
->is_archive
= FALSE
;
799 p
->local_sym_name
= name
;
800 p
->just_syms_flag
= TRUE
;
801 p
->search_dirs_flag
= FALSE
;
803 case lang_input_file_is_fake_enum
:
805 p
->is_archive
= FALSE
;
807 p
->local_sym_name
= name
;
808 p
->just_syms_flag
= FALSE
;
809 p
->search_dirs_flag
= FALSE
;
811 case lang_input_file_is_l_enum
:
812 p
->is_archive
= TRUE
;
815 p
->local_sym_name
= concat ("-l", name
, NULL
);
816 p
->just_syms_flag
= FALSE
;
817 p
->search_dirs_flag
= TRUE
;
819 case lang_input_file_is_marker_enum
:
821 p
->is_archive
= FALSE
;
823 p
->local_sym_name
= name
;
824 p
->just_syms_flag
= FALSE
;
825 p
->search_dirs_flag
= TRUE
;
827 case lang_input_file_is_search_file_enum
:
828 p
->sysrooted
= ldlang_sysrooted_script
;
830 p
->is_archive
= FALSE
;
832 p
->local_sym_name
= name
;
833 p
->just_syms_flag
= FALSE
;
834 p
->search_dirs_flag
= TRUE
;
836 case lang_input_file_is_file_enum
:
838 p
->is_archive
= FALSE
;
840 p
->local_sym_name
= name
;
841 p
->just_syms_flag
= FALSE
;
842 p
->search_dirs_flag
= FALSE
;
849 p
->next_real_file
= NULL
;
852 p
->dynamic
= config
.dynamic_link
;
853 p
->add_needed
= add_needed
;
854 p
->as_needed
= as_needed
;
855 p
->whole_archive
= whole_archive
;
857 lang_statement_append (&input_file_chain
,
858 (lang_statement_union_type
*) p
,
863 lang_input_statement_type
*
864 lang_add_input_file (const char *name
,
865 lang_input_file_enum_type file_type
,
868 lang_has_input_file
= TRUE
;
869 return new_afile (name
, file_type
, target
, TRUE
);
872 struct output_statement_hash_entry
874 struct bfd_hash_entry root
;
875 lang_output_section_statement_type os
;
878 /* The hash table. */
880 static struct bfd_hash_table output_statement_table
;
882 /* Support routines for the hash table used by lang_output_section_find,
883 initialize the table, fill in an entry and remove the table. */
885 static struct bfd_hash_entry
*
886 output_statement_newfunc (struct bfd_hash_entry
*entry
,
887 struct bfd_hash_table
*table
,
890 lang_output_section_statement_type
**nextp
;
891 struct output_statement_hash_entry
*ret
;
895 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
900 entry
= bfd_hash_newfunc (entry
, table
, string
);
904 ret
= (struct output_statement_hash_entry
*) entry
;
905 memset (&ret
->os
, 0, sizeof (ret
->os
));
906 ret
->os
.header
.type
= lang_output_section_statement_enum
;
907 ret
->os
.subsection_alignment
= -1;
908 ret
->os
.section_alignment
= -1;
909 ret
->os
.block_value
= 1;
910 lang_list_init (&ret
->os
.children
);
911 lang_statement_append (stat_ptr
,
912 (lang_statement_union_type
*) &ret
->os
,
913 &ret
->os
.header
.next
);
915 /* For every output section statement added to the list, except the
916 first one, lang_output_section_statement.tail points to the "next"
917 field of the last element of the list. */
918 if (lang_output_section_statement
.head
!= NULL
)
919 ret
->os
.prev
= (lang_output_section_statement_type
*)
920 ((char *) lang_output_section_statement
.tail
921 - offsetof (lang_output_section_statement_type
, next
));
923 /* GCC's strict aliasing rules prevent us from just casting the
924 address, so we store the pointer in a variable and cast that
926 nextp
= &ret
->os
.next
;
927 lang_statement_append (&lang_output_section_statement
,
928 (lang_statement_union_type
*) &ret
->os
,
929 (lang_statement_union_type
**) nextp
);
934 output_statement_table_init (void)
936 if (!bfd_hash_table_init_n (&output_statement_table
,
937 output_statement_newfunc
,
938 sizeof (struct output_statement_hash_entry
),
940 einfo (_("%P%F: can not create hash table: %E\n"));
944 output_statement_table_free (void)
946 bfd_hash_table_free (&output_statement_table
);
949 /* Build enough state so that the parser can build its tree. */
954 obstack_begin (&stat_obstack
, 1000);
956 stat_ptr
= &statement_list
;
958 output_statement_table_init ();
960 lang_list_init (stat_ptr
);
962 lang_list_init (&input_file_chain
);
963 lang_list_init (&lang_output_section_statement
);
964 lang_list_init (&file_chain
);
965 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
968 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
970 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
972 /* The value "3" is ad-hoc, somewhat related to the expected number of
973 DEFINED expressions in a linker script. For most default linker
974 scripts, there are none. Why a hash table then? Well, it's somewhat
975 simpler to re-use working machinery than using a linked list in terms
976 of code-complexity here in ld, besides the initialization which just
977 looks like other code here. */
978 if (!bfd_hash_table_init_n (&lang_definedness_table
,
979 lang_definedness_newfunc
,
980 sizeof (struct lang_definedness_hash_entry
),
982 einfo (_("%P%F: can not create hash table: %E\n"));
988 output_statement_table_free ();
991 /*----------------------------------------------------------------------
992 A region is an area of memory declared with the
993 MEMORY { name:org=exp, len=exp ... }
996 We maintain a list of all the regions here.
998 If no regions are specified in the script, then the default is used
999 which is created when looked up to be the entire data space.
1001 If create is true we are creating a region inside a MEMORY block.
1002 In this case it is probably an error to create a region that has
1003 already been created. If we are not inside a MEMORY block it is
1004 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1005 and so we issue a warning. */
1007 static lang_memory_region_type
*lang_memory_region_list
;
1008 static lang_memory_region_type
**lang_memory_region_list_tail
1009 = &lang_memory_region_list
;
1011 lang_memory_region_type
*
1012 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1014 lang_memory_region_type
*p
;
1015 lang_memory_region_type
*new;
1017 /* NAME is NULL for LMA memspecs if no region was specified. */
1021 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1022 if (strcmp (p
->name
, name
) == 0)
1025 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1030 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1031 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
1033 new = stat_alloc (sizeof (lang_memory_region_type
));
1035 new->name
= xstrdup (name
);
1038 *lang_memory_region_list_tail
= new;
1039 lang_memory_region_list_tail
= &new->next
;
1043 new->length
= ~(bfd_size_type
) 0;
1045 new->had_full_message
= FALSE
;
1050 static lang_memory_region_type
*
1051 lang_memory_default (asection
*section
)
1053 lang_memory_region_type
*p
;
1055 flagword sec_flags
= section
->flags
;
1057 /* Override SEC_DATA to mean a writable section. */
1058 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1059 sec_flags
|= SEC_DATA
;
1061 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1063 if ((p
->flags
& sec_flags
) != 0
1064 && (p
->not_flags
& sec_flags
) == 0)
1069 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1072 lang_output_section_statement_type
*
1073 lang_output_section_find (const char *const name
)
1075 struct output_statement_hash_entry
*entry
;
1078 entry
= ((struct output_statement_hash_entry
*)
1079 bfd_hash_lookup (&output_statement_table
, name
, FALSE
, FALSE
));
1083 hash
= entry
->root
.hash
;
1086 if (entry
->os
.constraint
!= -1)
1088 entry
= (struct output_statement_hash_entry
*) entry
->root
.next
;
1090 while (entry
!= NULL
1091 && entry
->root
.hash
== hash
1092 && strcmp (name
, entry
->os
.name
) == 0);
1097 static lang_output_section_statement_type
*
1098 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
1100 struct output_statement_hash_entry
*entry
;
1101 struct output_statement_hash_entry
*last_ent
;
1104 entry
= ((struct output_statement_hash_entry
*)
1105 bfd_hash_lookup (&output_statement_table
, name
, TRUE
, FALSE
));
1108 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1112 if (entry
->os
.name
!= NULL
)
1114 /* We have a section of this name, but it might not have the correct
1116 hash
= entry
->root
.hash
;
1119 if (entry
->os
.constraint
!= -1
1121 || (constraint
== entry
->os
.constraint
1122 && constraint
!= SPECIAL
)))
1125 entry
= (struct output_statement_hash_entry
*) entry
->root
.next
;
1127 while (entry
!= NULL
1128 && entry
->root
.hash
== hash
1129 && strcmp (name
, entry
->os
.name
) == 0);
1131 entry
= ((struct output_statement_hash_entry
*)
1132 output_statement_newfunc (NULL
, &output_statement_table
, name
));
1135 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1138 entry
->root
= last_ent
->root
;
1139 last_ent
->root
.next
= &entry
->root
;
1142 entry
->os
.name
= name
;
1143 entry
->os
.constraint
= constraint
;
1147 lang_output_section_statement_type
*
1148 lang_output_section_statement_lookup (const char *const name
)
1150 return lang_output_section_statement_lookup_1 (name
, 0);
1153 /* A variant of lang_output_section_find used by place_orphan.
1154 Returns the output statement that should precede a new output
1155 statement for SEC. If an exact match is found on certain flags,
1158 lang_output_section_statement_type
*
1159 lang_output_section_find_by_flags (const asection
*sec
,
1160 lang_output_section_statement_type
**exact
,
1161 lang_match_sec_type_func match_type
)
1163 lang_output_section_statement_type
*first
, *look
, *found
;
1166 /* We know the first statement on this list is *ABS*. May as well
1168 first
= &lang_output_section_statement
.head
->output_section_statement
;
1169 first
= first
->next
;
1171 /* First try for an exact match. */
1173 for (look
= first
; look
; look
= look
->next
)
1175 flags
= look
->flags
;
1176 if (look
->bfd_section
!= NULL
)
1178 flags
= look
->bfd_section
->flags
;
1179 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1183 flags
^= sec
->flags
;
1184 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1185 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1195 if (sec
->flags
& SEC_CODE
)
1197 /* Try for a rw code section. */
1198 for (look
= first
; look
; look
= look
->next
)
1200 flags
= look
->flags
;
1201 if (look
->bfd_section
!= NULL
)
1203 flags
= look
->bfd_section
->flags
;
1204 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1208 flags
^= sec
->flags
;
1209 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1210 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1214 else if (sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
))
1216 /* .rodata can go after .text, .sdata2 after .rodata. */
1217 for (look
= first
; look
; look
= look
->next
)
1219 flags
= look
->flags
;
1220 if (look
->bfd_section
!= NULL
)
1222 flags
= look
->bfd_section
->flags
;
1223 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1227 flags
^= sec
->flags
;
1228 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1230 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1234 else if (sec
->flags
& SEC_SMALL_DATA
)
1236 /* .sdata goes after .data, .sbss after .sdata. */
1237 for (look
= first
; look
; look
= look
->next
)
1239 flags
= look
->flags
;
1240 if (look
->bfd_section
!= NULL
)
1242 flags
= look
->bfd_section
->flags
;
1243 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1247 flags
^= sec
->flags
;
1248 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1249 | SEC_THREAD_LOCAL
))
1250 || ((look
->flags
& SEC_SMALL_DATA
)
1251 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1255 else if (sec
->flags
& SEC_HAS_CONTENTS
)
1257 /* .data goes after .rodata. */
1258 for (look
= first
; look
; look
= look
->next
)
1260 flags
= look
->flags
;
1261 if (look
->bfd_section
!= NULL
)
1263 flags
= look
->bfd_section
->flags
;
1264 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1268 flags
^= sec
->flags
;
1269 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1270 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1276 /* .bss goes last. */
1277 for (look
= first
; look
; look
= look
->next
)
1279 flags
= look
->flags
;
1280 if (look
->bfd_section
!= NULL
)
1282 flags
= look
->bfd_section
->flags
;
1283 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1287 flags
^= sec
->flags
;
1288 if (!(flags
& SEC_ALLOC
))
1293 if (found
|| !match_type
)
1296 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1299 /* Find the last output section before given output statement.
1300 Used by place_orphan. */
1303 output_prev_sec_find (lang_output_section_statement_type
*os
)
1305 lang_output_section_statement_type
*lookup
;
1307 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1309 if (lookup
->constraint
== -1)
1312 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1313 return lookup
->bfd_section
;
1319 lang_output_section_statement_type
*
1320 lang_insert_orphan (asection
*s
,
1321 const char *secname
,
1322 lang_output_section_statement_type
*after
,
1323 struct orphan_save
*place
,
1324 etree_type
*address
,
1325 lang_statement_list_type
*add_child
)
1327 lang_statement_list_type
*old
;
1328 lang_statement_list_type add
;
1330 etree_type
*load_base
;
1331 lang_output_section_statement_type
*os
;
1332 lang_output_section_statement_type
**os_tail
;
1334 /* Start building a list of statements for this section.
1335 First save the current statement pointer. */
1338 /* If we have found an appropriate place for the output section
1339 statements for this orphan, add them to our own private list,
1340 inserting them later into the global statement list. */
1344 lang_list_init (stat_ptr
);
1348 if (config
.build_constructors
)
1350 /* If the name of the section is representable in C, then create
1351 symbols to mark the start and the end of the section. */
1352 for (ps
= secname
; *ps
!= '\0'; ps
++)
1353 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1358 etree_type
*e_align
;
1360 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1361 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1362 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1363 e_align
= exp_unop (ALIGN_K
,
1364 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1365 lang_add_assignment (exp_assop ('=', ".", e_align
));
1366 lang_add_assignment (exp_assop ('=', symname
,
1367 exp_nameop (NAME
, ".")));
1371 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1372 address
= exp_intop (0);
1375 if (after
!= NULL
&& after
->load_base
!= NULL
)
1377 etree_type
*lma_from_vma
;
1378 lma_from_vma
= exp_binop ('-', after
->load_base
,
1379 exp_nameop (ADDR
, after
->name
));
1380 load_base
= exp_binop ('+', lma_from_vma
,
1381 exp_nameop (ADDR
, secname
));
1384 os_tail
= ((lang_output_section_statement_type
**)
1385 lang_output_section_statement
.tail
);
1386 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1389 if (add_child
== NULL
)
1390 add_child
= &os
->children
;
1391 lang_add_section (add_child
, s
, os
);
1393 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1395 if (config
.build_constructors
&& *ps
== '\0')
1399 /* lang_leave_ouput_section_statement resets stat_ptr.
1400 Put stat_ptr back where we want it. */
1404 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1405 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1406 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1407 lang_add_assignment (exp_assop ('=', symname
,
1408 exp_nameop (NAME
, ".")));
1411 /* Restore the global list pointer. */
1415 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1417 asection
*snew
, *as
;
1419 snew
= os
->bfd_section
;
1421 /* Shuffle the bfd section list to make the output file look
1422 neater. This is really only cosmetic. */
1423 if (place
->section
== NULL
1424 && after
!= (&lang_output_section_statement
.head
1425 ->output_section_statement
))
1427 asection
*bfd_section
= after
->bfd_section
;
1429 /* If the output statement hasn't been used to place any input
1430 sections (and thus doesn't have an output bfd_section),
1431 look for the closest prior output statement having an
1433 if (bfd_section
== NULL
)
1434 bfd_section
= output_prev_sec_find (after
);
1436 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1437 place
->section
= &bfd_section
->next
;
1440 if (place
->section
== NULL
)
1441 place
->section
= &output_bfd
->sections
;
1443 as
= *place
->section
;
1444 if (as
!= snew
&& as
->prev
!= snew
)
1446 /* Unlink the section. */
1447 bfd_section_list_remove (output_bfd
, snew
);
1449 /* Now tack it back on in the right place. */
1450 bfd_section_list_insert_before (output_bfd
, as
, snew
);
1453 /* Save the end of this list. Further ophans of this type will
1454 follow the one we've just added. */
1455 place
->section
= &snew
->next
;
1457 /* The following is non-cosmetic. We try to put the output
1458 statements in some sort of reasonable order here, because they
1459 determine the final load addresses of the orphan sections.
1460 In addition, placing output statements in the wrong order may
1461 require extra segments. For instance, given a typical
1462 situation of all read-only sections placed in one segment and
1463 following that a segment containing all the read-write
1464 sections, we wouldn't want to place an orphan read/write
1465 section before or amongst the read-only ones. */
1466 if (add
.head
!= NULL
)
1468 lang_output_section_statement_type
*newly_added_os
;
1470 if (place
->stmt
== NULL
)
1472 lang_statement_union_type
**where
;
1473 lang_statement_union_type
**assign
= NULL
;
1474 bfd_boolean ignore_first
;
1476 /* Look for a suitable place for the new statement list.
1477 The idea is to skip over anything that might be inside
1478 a SECTIONS {} statement in a script, before we find
1479 another output_section_statement. Assignments to "dot"
1480 before an output section statement are assumed to
1481 belong to it. An exception to this rule is made for
1482 the first assignment to dot, otherwise we might put an
1483 orphan before . = . + SIZEOF_HEADERS or similar
1484 assignments that set the initial address. */
1486 ignore_first
= after
== (&lang_output_section_statement
.head
1487 ->output_section_statement
);
1488 for (where
= &after
->header
.next
;
1490 where
= &(*where
)->header
.next
)
1492 switch ((*where
)->header
.type
)
1494 case lang_assignment_statement_enum
:
1497 lang_assignment_statement_type
*ass
;
1498 ass
= &(*where
)->assignment_statement
;
1499 if (ass
->exp
->type
.node_class
!= etree_assert
1500 && ass
->exp
->assign
.dst
[0] == '.'
1501 && ass
->exp
->assign
.dst
[1] == 0
1505 ignore_first
= FALSE
;
1507 case lang_wild_statement_enum
:
1508 case lang_input_section_enum
:
1509 case lang_object_symbols_statement_enum
:
1510 case lang_fill_statement_enum
:
1511 case lang_data_statement_enum
:
1512 case lang_reloc_statement_enum
:
1513 case lang_padding_statement_enum
:
1514 case lang_constructors_statement_enum
:
1517 case lang_output_section_statement_enum
:
1520 case lang_input_statement_enum
:
1521 case lang_address_statement_enum
:
1522 case lang_target_statement_enum
:
1523 case lang_output_statement_enum
:
1524 case lang_group_statement_enum
:
1525 case lang_afile_asection_pair_statement_enum
:
1534 place
->os_tail
= &after
->next
;
1538 /* Put it after the last orphan statement we added. */
1539 *add
.tail
= *place
->stmt
;
1540 *place
->stmt
= add
.head
;
1543 /* Fix the global list pointer if we happened to tack our
1544 new list at the tail. */
1545 if (*old
->tail
== add
.head
)
1546 old
->tail
= add
.tail
;
1548 /* Save the end of this list. */
1549 place
->stmt
= add
.tail
;
1551 /* Do the same for the list of output section statements. */
1552 newly_added_os
= *os_tail
;
1554 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1555 ((char *) place
->os_tail
1556 - offsetof (lang_output_section_statement_type
, next
));
1557 newly_added_os
->next
= *place
->os_tail
;
1558 if (newly_added_os
->next
!= NULL
)
1559 newly_added_os
->next
->prev
= newly_added_os
;
1560 *place
->os_tail
= newly_added_os
;
1561 place
->os_tail
= &newly_added_os
->next
;
1563 /* Fixing the global list pointer here is a little different.
1564 We added to the list in lang_enter_output_section_statement,
1565 trimmed off the new output_section_statment above when
1566 assigning *os_tail = NULL, but possibly added it back in
1567 the same place when assigning *place->os_tail. */
1568 if (*os_tail
== NULL
)
1569 lang_output_section_statement
.tail
1570 = (lang_statement_union_type
**) os_tail
;
1577 lang_map_flags (flagword flag
)
1579 if (flag
& SEC_ALLOC
)
1582 if (flag
& SEC_CODE
)
1585 if (flag
& SEC_READONLY
)
1588 if (flag
& SEC_DATA
)
1591 if (flag
& SEC_LOAD
)
1598 lang_memory_region_type
*m
;
1601 minfo (_("\nMemory Configuration\n\n"));
1602 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1603 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1605 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1610 fprintf (config
.map_file
, "%-16s ", m
->name
);
1612 sprintf_vma (buf
, m
->origin
);
1613 minfo ("0x%s ", buf
);
1621 minfo ("0x%V", m
->length
);
1622 if (m
->flags
|| m
->not_flags
)
1630 lang_map_flags (m
->flags
);
1636 lang_map_flags (m
->not_flags
);
1643 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1645 if (! command_line
.reduce_memory_overheads
)
1647 obstack_begin (&map_obstack
, 1000);
1648 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1649 bfd_map_over_sections (p
, init_map_userdata
, 0);
1650 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1652 print_statements ();
1656 init_map_userdata (abfd
, sec
, data
)
1657 bfd
*abfd ATTRIBUTE_UNUSED
;
1659 void *data ATTRIBUTE_UNUSED
;
1661 fat_section_userdata_type
*new_data
1662 = ((fat_section_userdata_type
*) (stat_alloc
1663 (sizeof (fat_section_userdata_type
))));
1665 ASSERT (get_userdata (sec
) == NULL
);
1666 get_userdata (sec
) = new_data
;
1667 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
1671 sort_def_symbol (hash_entry
, info
)
1672 struct bfd_link_hash_entry
*hash_entry
;
1673 void *info ATTRIBUTE_UNUSED
;
1675 if (hash_entry
->type
== bfd_link_hash_defined
1676 || hash_entry
->type
== bfd_link_hash_defweak
)
1678 struct fat_user_section_struct
*ud
;
1679 struct map_symbol_def
*def
;
1681 ud
= get_userdata (hash_entry
->u
.def
.section
);
1684 /* ??? What do we have to do to initialize this beforehand? */
1685 /* The first time we get here is bfd_abs_section... */
1686 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
1687 ud
= get_userdata (hash_entry
->u
.def
.section
);
1689 else if (!ud
->map_symbol_def_tail
)
1690 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
1692 def
= obstack_alloc (&map_obstack
, sizeof *def
);
1693 def
->entry
= hash_entry
;
1694 *(ud
->map_symbol_def_tail
) = def
;
1695 ud
->map_symbol_def_tail
= &def
->next
;
1700 /* Initialize an output section. */
1703 init_os (lang_output_section_statement_type
*s
, asection
*isec
)
1705 if (s
->bfd_section
!= NULL
)
1708 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1709 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1711 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
1712 if (s
->bfd_section
== NULL
)
1713 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
1714 if (s
->bfd_section
== NULL
)
1716 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1717 output_bfd
->xvec
->name
, s
->name
);
1719 s
->bfd_section
->output_section
= s
->bfd_section
;
1720 s
->bfd_section
->output_offset
= 0;
1721 if (!command_line
.reduce_memory_overheads
)
1723 fat_section_userdata_type
*new
1724 = stat_alloc (sizeof (fat_section_userdata_type
));
1725 memset (new, 0, sizeof (fat_section_userdata_type
));
1726 get_userdata (s
->bfd_section
) = new;
1730 /* If there is a base address, make sure that any sections it might
1731 mention are initialized. */
1732 if (s
->addr_tree
!= NULL
)
1733 exp_init_os (s
->addr_tree
);
1735 if (s
->load_base
!= NULL
)
1736 exp_init_os (s
->load_base
);
1738 /* If supplied an alignment, set it. */
1739 if (s
->section_alignment
!= -1)
1740 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1743 bfd_init_private_section_data (isec
->owner
, isec
,
1744 output_bfd
, s
->bfd_section
,
1748 /* Make sure that all output sections mentioned in an expression are
1752 exp_init_os (etree_type
*exp
)
1754 switch (exp
->type
.node_class
)
1758 exp_init_os (exp
->assign
.src
);
1762 exp_init_os (exp
->binary
.lhs
);
1763 exp_init_os (exp
->binary
.rhs
);
1767 exp_init_os (exp
->trinary
.cond
);
1768 exp_init_os (exp
->trinary
.lhs
);
1769 exp_init_os (exp
->trinary
.rhs
);
1773 exp_init_os (exp
->assert_s
.child
);
1777 exp_init_os (exp
->unary
.child
);
1781 switch (exp
->type
.node_code
)
1787 lang_output_section_statement_type
*os
;
1789 os
= lang_output_section_find (exp
->name
.name
);
1790 if (os
!= NULL
&& os
->bfd_section
== NULL
)
1802 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
1804 lang_input_statement_type
*entry
= data
;
1806 /* If we are only reading symbols from this object, then we want to
1807 discard all sections. */
1808 if (entry
->just_syms_flag
)
1810 bfd_link_just_syms (abfd
, sec
, &link_info
);
1814 if (!(abfd
->flags
& DYNAMIC
))
1815 bfd_section_already_linked (abfd
, sec
);
1818 /* The wild routines.
1820 These expand statements like *(.text) and foo.o to a list of
1821 explicit actions, like foo.o(.text), bar.o(.text) and
1822 foo.o(.text, .data). */
1824 /* Add SECTION to the output section OUTPUT. Do this by creating a
1825 lang_input_section statement which is placed at PTR. FILE is the
1826 input file which holds SECTION. */
1829 lang_add_section (lang_statement_list_type
*ptr
,
1831 lang_output_section_statement_type
*output
)
1833 flagword flags
= section
->flags
;
1834 bfd_boolean discard
;
1836 /* Discard sections marked with SEC_EXCLUDE. */
1837 discard
= (flags
& SEC_EXCLUDE
) != 0;
1839 /* Discard input sections which are assigned to a section named
1840 DISCARD_SECTION_NAME. */
1841 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1844 /* Discard debugging sections if we are stripping debugging
1846 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1847 && (flags
& SEC_DEBUGGING
) != 0)
1852 if (section
->output_section
== NULL
)
1854 /* This prevents future calls from assigning this section. */
1855 section
->output_section
= bfd_abs_section_ptr
;
1860 if (section
->output_section
== NULL
)
1863 lang_input_section_type
*new;
1866 if (output
->bfd_section
== NULL
)
1867 init_os (output
, section
);
1869 first
= ! output
->bfd_section
->linker_has_input
;
1870 output
->bfd_section
->linker_has_input
= 1;
1872 if (!link_info
.relocatable
1873 && !stripped_excluded_sections
)
1875 asection
*s
= output
->bfd_section
->map_tail
.s
;
1876 output
->bfd_section
->map_tail
.s
= section
;
1877 section
->map_head
.s
= NULL
;
1878 section
->map_tail
.s
= s
;
1880 s
->map_head
.s
= section
;
1882 output
->bfd_section
->map_head
.s
= section
;
1885 /* Add a section reference to the list. */
1886 new = new_stat (lang_input_section
, ptr
);
1888 new->section
= section
;
1889 section
->output_section
= output
->bfd_section
;
1891 flags
= section
->flags
;
1893 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1894 to an output section, because we want to be able to include a
1895 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1896 section (I don't know why we want to do this, but we do).
1897 build_link_order in ldwrite.c handles this case by turning
1898 the embedded SEC_NEVER_LOAD section into a fill. */
1900 flags
&= ~ SEC_NEVER_LOAD
;
1902 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1903 already been processed. One reason to do this is that on pe
1904 format targets, .text$foo sections go into .text and it's odd
1905 to see .text with SEC_LINK_ONCE set. */
1907 if (! link_info
.relocatable
)
1908 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1910 /* If this is not the first input section, and the SEC_READONLY
1911 flag is not currently set, then don't set it just because the
1912 input section has it set. */
1914 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
1915 flags
&= ~ SEC_READONLY
;
1917 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1919 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1920 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1921 || ((flags
& SEC_MERGE
)
1922 && output
->bfd_section
->entsize
!= section
->entsize
)))
1924 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1925 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1928 output
->bfd_section
->flags
|= flags
;
1930 if (flags
& SEC_MERGE
)
1931 output
->bfd_section
->entsize
= section
->entsize
;
1933 /* If SEC_READONLY is not set in the input section, then clear
1934 it from the output section. */
1935 if ((section
->flags
& SEC_READONLY
) == 0)
1936 output
->bfd_section
->flags
&= ~SEC_READONLY
;
1938 switch (output
->sectype
)
1940 case normal_section
:
1945 case overlay_section
:
1946 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1948 case noload_section
:
1949 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1950 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1954 /* Copy over SEC_SMALL_DATA. */
1955 if (section
->flags
& SEC_SMALL_DATA
)
1956 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
1958 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1959 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1961 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
1962 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
1964 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
1965 /* FIXME: This value should really be obtained from the bfd... */
1966 output
->block_value
= 128;
1971 /* Compare sections ASEC and BSEC according to SORT. */
1974 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
1983 case by_alignment_name
:
1984 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
1985 - bfd_section_alignment (asec
->owner
, asec
));
1991 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
1992 bfd_get_section_name (bsec
->owner
, bsec
));
1995 case by_name_alignment
:
1996 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
1997 bfd_get_section_name (bsec
->owner
, bsec
));
2003 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
2004 - bfd_section_alignment (asec
->owner
, asec
));
2011 /* Handle wildcard sorting. This returns the lang_input_section which
2012 should follow the one we are going to create for SECTION and FILE,
2013 based on the sorting requirements of WILD. It returns NULL if the
2014 new section should just go at the end of the current list. */
2016 static lang_statement_union_type
*
2017 wild_sort (lang_wild_statement_type
*wild
,
2018 struct wildcard_list
*sec
,
2019 lang_input_statement_type
*file
,
2022 const char *section_name
;
2023 lang_statement_union_type
*l
;
2025 if (!wild
->filenames_sorted
2026 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2029 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2030 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2032 lang_input_section_type
*ls
;
2034 if (l
->header
.type
!= lang_input_section_enum
)
2036 ls
= &l
->input_section
;
2038 /* Sorting by filename takes precedence over sorting by section
2041 if (wild
->filenames_sorted
)
2043 const char *fn
, *ln
;
2047 /* The PE support for the .idata section as generated by
2048 dlltool assumes that files will be sorted by the name of
2049 the archive and then the name of the file within the
2052 if (file
->the_bfd
!= NULL
2053 && bfd_my_archive (file
->the_bfd
) != NULL
)
2055 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2060 fn
= file
->filename
;
2064 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2066 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2071 ln
= ls
->section
->owner
->filename
;
2075 i
= strcmp (fn
, ln
);
2084 fn
= file
->filename
;
2086 ln
= ls
->section
->owner
->filename
;
2088 i
= strcmp (fn
, ln
);
2096 /* Here either the files are not sorted by name, or we are
2097 looking at the sections for this file. */
2099 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2100 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2107 /* Expand a wild statement for a particular FILE. SECTION may be
2108 NULL, in which case it is a wild card. */
2111 output_section_callback (lang_wild_statement_type
*ptr
,
2112 struct wildcard_list
*sec
,
2114 lang_input_statement_type
*file
,
2117 lang_statement_union_type
*before
;
2119 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2120 if (unique_section_p (section
))
2123 before
= wild_sort (ptr
, sec
, file
, section
);
2125 /* Here BEFORE points to the lang_input_section which
2126 should follow the one we are about to add. If BEFORE
2127 is NULL, then the section should just go at the end
2128 of the current list. */
2131 lang_add_section (&ptr
->children
, section
,
2132 (lang_output_section_statement_type
*) output
);
2135 lang_statement_list_type list
;
2136 lang_statement_union_type
**pp
;
2138 lang_list_init (&list
);
2139 lang_add_section (&list
, section
,
2140 (lang_output_section_statement_type
*) output
);
2142 /* If we are discarding the section, LIST.HEAD will
2144 if (list
.head
!= NULL
)
2146 ASSERT (list
.head
->header
.next
== NULL
);
2148 for (pp
= &ptr
->children
.head
;
2150 pp
= &(*pp
)->header
.next
)
2151 ASSERT (*pp
!= NULL
);
2153 list
.head
->header
.next
= *pp
;
2159 /* Check if all sections in a wild statement for a particular FILE
2163 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2164 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2166 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2169 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2170 if (unique_section_p (section
))
2173 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2174 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2177 /* This is passed a file name which must have been seen already and
2178 added to the statement tree. We will see if it has been opened
2179 already and had its symbols read. If not then we'll read it. */
2181 static lang_input_statement_type
*
2182 lookup_name (const char *name
)
2184 lang_input_statement_type
*search
;
2186 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2188 search
= (lang_input_statement_type
*) search
->next_real_file
)
2190 /* Use the local_sym_name as the name of the file that has
2191 already been loaded as filename might have been transformed
2192 via the search directory lookup mechanism. */
2193 const char * filename
= search
->local_sym_name
;
2195 if (filename
== NULL
&& name
== NULL
)
2197 if (filename
!= NULL
2199 && strcmp (filename
, name
) == 0)
2204 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2205 default_target
, FALSE
);
2207 /* If we have already added this file, or this file is not real
2208 (FIXME: can that ever actually happen?) or the name is NULL
2209 (FIXME: can that ever actually happen?) don't add this file. */
2212 || search
->filename
== NULL
)
2215 if (! load_symbols (search
, NULL
))
2221 /* Save LIST as a list of libraries whose symbols should not be exported. */
2226 struct excluded_lib
*next
;
2228 static struct excluded_lib
*excluded_libs
;
2231 add_excluded_libs (const char *list
)
2233 const char *p
= list
, *end
;
2237 struct excluded_lib
*entry
;
2238 end
= strpbrk (p
, ",:");
2240 end
= p
+ strlen (p
);
2241 entry
= xmalloc (sizeof (*entry
));
2242 entry
->next
= excluded_libs
;
2243 entry
->name
= xmalloc (end
- p
+ 1);
2244 memcpy (entry
->name
, p
, end
- p
);
2245 entry
->name
[end
- p
] = '\0';
2246 excluded_libs
= entry
;
2254 check_excluded_libs (bfd
*abfd
)
2256 struct excluded_lib
*lib
= excluded_libs
;
2260 int len
= strlen (lib
->name
);
2261 const char *filename
= lbasename (abfd
->filename
);
2263 if (strcmp (lib
->name
, "ALL") == 0)
2265 abfd
->no_export
= TRUE
;
2269 if (strncmp (lib
->name
, filename
, len
) == 0
2270 && (filename
[len
] == '\0'
2271 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2272 && filename
[len
+ 2] == '\0')))
2274 abfd
->no_export
= TRUE
;
2282 /* Get the symbols for an input file. */
2285 load_symbols (lang_input_statement_type
*entry
,
2286 lang_statement_list_type
*place
)
2293 ldfile_open_file (entry
);
2295 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2296 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2299 lang_statement_list_type
*hold
;
2300 bfd_boolean bad_load
= TRUE
;
2301 bfd_boolean save_ldlang_sysrooted_script
;
2303 err
= bfd_get_error ();
2305 /* See if the emulation has some special knowledge. */
2306 if (ldemul_unrecognized_file (entry
))
2309 if (err
== bfd_error_file_ambiguously_recognized
)
2313 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2314 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2315 for (p
= matching
; *p
!= NULL
; p
++)
2319 else if (err
!= bfd_error_file_not_recognized
2321 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2325 bfd_close (entry
->the_bfd
);
2326 entry
->the_bfd
= NULL
;
2328 /* Try to interpret the file as a linker script. */
2329 ldfile_open_command_file (entry
->filename
);
2333 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2334 ldlang_sysrooted_script
= entry
->sysrooted
;
2336 ldfile_assumed_script
= TRUE
;
2337 parser_input
= input_script
;
2338 /* We want to use the same -Bdynamic/-Bstatic as the one for
2340 config
.dynamic_link
= entry
->dynamic
;
2342 ldfile_assumed_script
= FALSE
;
2344 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2350 if (ldemul_recognized_file (entry
))
2353 /* We don't call ldlang_add_file for an archive. Instead, the
2354 add_symbols entry point will call ldlang_add_file, via the
2355 add_archive_element callback, for each element of the archive
2357 switch (bfd_get_format (entry
->the_bfd
))
2363 ldlang_add_file (entry
);
2364 if (trace_files
|| trace_file_tries
)
2365 info_msg ("%I\n", entry
);
2369 check_excluded_libs (entry
->the_bfd
);
2371 if (entry
->whole_archive
)
2374 bfd_boolean loaded
= TRUE
;
2378 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2383 if (! bfd_check_format (member
, bfd_object
))
2385 einfo (_("%F%B: member %B in archive is not an object\n"),
2386 entry
->the_bfd
, member
);
2390 if (! ((*link_info
.callbacks
->add_archive_element
)
2391 (&link_info
, member
, "--whole-archive")))
2394 if (! bfd_link_add_symbols (member
, &link_info
))
2396 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2401 entry
->loaded
= loaded
;
2407 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2408 entry
->loaded
= TRUE
;
2410 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2412 return entry
->loaded
;
2415 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2416 may be NULL, indicating that it is a wildcard. Separate
2417 lang_input_section statements are created for each part of the
2418 expansion; they are added after the wild statement S. OUTPUT is
2419 the output section. */
2422 wild (lang_wild_statement_type
*s
,
2423 const char *target ATTRIBUTE_UNUSED
,
2424 lang_output_section_statement_type
*output
)
2426 struct wildcard_list
*sec
;
2428 walk_wild (s
, output_section_callback
, output
);
2430 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2432 if (default_common_section
!= NULL
)
2434 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2436 /* Remember the section that common is going to in case we
2437 later get something which doesn't know where to put it. */
2438 default_common_section
= output
;
2443 /* Return TRUE iff target is the sought target. */
2446 get_target (const bfd_target
*target
, void *data
)
2448 const char *sought
= data
;
2450 return strcmp (target
->name
, sought
) == 0;
2453 /* Like strcpy() but convert to lower case as well. */
2456 stricpy (char *dest
, char *src
)
2460 while ((c
= *src
++) != 0)
2461 *dest
++ = TOLOWER (c
);
2466 /* Remove the first occurrence of needle (if any) in haystack
2470 strcut (char *haystack
, char *needle
)
2472 haystack
= strstr (haystack
, needle
);
2478 for (src
= haystack
+ strlen (needle
); *src
;)
2479 *haystack
++ = *src
++;
2485 /* Compare two target format name strings.
2486 Return a value indicating how "similar" they are. */
2489 name_compare (char *first
, char *second
)
2495 copy1
= xmalloc (strlen (first
) + 1);
2496 copy2
= xmalloc (strlen (second
) + 1);
2498 /* Convert the names to lower case. */
2499 stricpy (copy1
, first
);
2500 stricpy (copy2
, second
);
2502 /* Remove size and endian strings from the name. */
2503 strcut (copy1
, "big");
2504 strcut (copy1
, "little");
2505 strcut (copy2
, "big");
2506 strcut (copy2
, "little");
2508 /* Return a value based on how many characters match,
2509 starting from the beginning. If both strings are
2510 the same then return 10 * their length. */
2511 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2512 if (copy1
[result
] == 0)
2524 /* Set by closest_target_match() below. */
2525 static const bfd_target
*winner
;
2527 /* Scan all the valid bfd targets looking for one that has the endianness
2528 requirement that was specified on the command line, and is the nearest
2529 match to the original output target. */
2532 closest_target_match (const bfd_target
*target
, void *data
)
2534 const bfd_target
*original
= data
;
2536 if (command_line
.endian
== ENDIAN_BIG
2537 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2540 if (command_line
.endian
== ENDIAN_LITTLE
2541 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2544 /* Must be the same flavour. */
2545 if (target
->flavour
!= original
->flavour
)
2548 /* If we have not found a potential winner yet, then record this one. */
2555 /* Oh dear, we now have two potential candidates for a successful match.
2556 Compare their names and choose the better one. */
2557 if (name_compare (target
->name
, original
->name
)
2558 > name_compare (winner
->name
, original
->name
))
2561 /* Keep on searching until wqe have checked them all. */
2565 /* Return the BFD target format of the first input file. */
2568 get_first_input_target (void)
2570 char *target
= NULL
;
2572 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2574 if (s
->header
.type
== lang_input_statement_enum
2577 ldfile_open_file (s
);
2579 if (s
->the_bfd
!= NULL
2580 && bfd_check_format (s
->the_bfd
, bfd_object
))
2582 target
= bfd_get_target (s
->the_bfd
);
2594 lang_get_output_target (void)
2598 /* Has the user told us which output format to use? */
2599 if (output_target
!= NULL
)
2600 return output_target
;
2602 /* No - has the current target been set to something other than
2604 if (current_target
!= default_target
)
2605 return current_target
;
2607 /* No - can we determine the format of the first input file? */
2608 target
= get_first_input_target ();
2612 /* Failed - use the default output target. */
2613 return default_target
;
2616 /* Open the output file. */
2619 open_output (const char *name
)
2623 output_target
= lang_get_output_target ();
2625 /* Has the user requested a particular endianness on the command
2627 if (command_line
.endian
!= ENDIAN_UNSET
)
2629 const bfd_target
*target
;
2630 enum bfd_endian desired_endian
;
2632 /* Get the chosen target. */
2633 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2635 /* If the target is not supported, we cannot do anything. */
2638 if (command_line
.endian
== ENDIAN_BIG
)
2639 desired_endian
= BFD_ENDIAN_BIG
;
2641 desired_endian
= BFD_ENDIAN_LITTLE
;
2643 /* See if the target has the wrong endianness. This should
2644 not happen if the linker script has provided big and
2645 little endian alternatives, but some scrips don't do
2647 if (target
->byteorder
!= desired_endian
)
2649 /* If it does, then see if the target provides
2650 an alternative with the correct endianness. */
2651 if (target
->alternative_target
!= NULL
2652 && (target
->alternative_target
->byteorder
== desired_endian
))
2653 output_target
= target
->alternative_target
->name
;
2656 /* Try to find a target as similar as possible to
2657 the default target, but which has the desired
2658 endian characteristic. */
2659 bfd_search_for_target (closest_target_match
,
2662 /* Oh dear - we could not find any targets that
2663 satisfy our requirements. */
2665 einfo (_("%P: warning: could not find any targets"
2666 " that match endianness requirement\n"));
2668 output_target
= winner
->name
;
2674 output
= bfd_openw (name
, output_target
);
2678 if (bfd_get_error () == bfd_error_invalid_target
)
2679 einfo (_("%P%F: target %s not found\n"), output_target
);
2681 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2684 delete_output_file_on_failure
= TRUE
;
2686 if (! bfd_set_format (output
, bfd_object
))
2687 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2688 if (! bfd_set_arch_mach (output
,
2689 ldfile_output_architecture
,
2690 ldfile_output_machine
))
2691 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2693 link_info
.hash
= bfd_link_hash_table_create (output
);
2694 if (link_info
.hash
== NULL
)
2695 einfo (_("%P%F: can not create hash table: %E\n"));
2697 bfd_set_gp_size (output
, g_switch_value
);
2702 ldlang_open_output (lang_statement_union_type
*statement
)
2704 switch (statement
->header
.type
)
2706 case lang_output_statement_enum
:
2707 ASSERT (output_bfd
== NULL
);
2708 output_bfd
= open_output (statement
->output_statement
.name
);
2709 ldemul_set_output_arch ();
2710 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2711 output_bfd
->flags
|= D_PAGED
;
2713 output_bfd
->flags
&= ~D_PAGED
;
2714 if (config
.text_read_only
)
2715 output_bfd
->flags
|= WP_TEXT
;
2717 output_bfd
->flags
&= ~WP_TEXT
;
2718 if (link_info
.traditional_format
)
2719 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2721 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2724 case lang_target_statement_enum
:
2725 current_target
= statement
->target_statement
.target
;
2732 /* Convert between addresses in bytes and sizes in octets.
2733 For currently supported targets, octets_per_byte is always a power
2734 of two, so we can use shifts. */
2735 #define TO_ADDR(X) ((X) >> opb_shift)
2736 #define TO_SIZE(X) ((X) << opb_shift)
2738 /* Support the above. */
2739 static unsigned int opb_shift
= 0;
2744 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2745 ldfile_output_machine
);
2748 while ((x
& 1) == 0)
2756 /* Open all the input files. */
2759 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2761 for (; s
!= NULL
; s
= s
->header
.next
)
2763 switch (s
->header
.type
)
2765 case lang_constructors_statement_enum
:
2766 open_input_bfds (constructor_list
.head
, force
);
2768 case lang_output_section_statement_enum
:
2769 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2771 case lang_wild_statement_enum
:
2772 /* Maybe we should load the file's symbols. */
2773 if (s
->wild_statement
.filename
2774 && ! wildcardp (s
->wild_statement
.filename
))
2775 lookup_name (s
->wild_statement
.filename
);
2776 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2778 case lang_group_statement_enum
:
2780 struct bfd_link_hash_entry
*undefs
;
2782 /* We must continually search the entries in the group
2783 until no new symbols are added to the list of undefined
2788 undefs
= link_info
.hash
->undefs_tail
;
2789 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2791 while (undefs
!= link_info
.hash
->undefs_tail
);
2794 case lang_target_statement_enum
:
2795 current_target
= s
->target_statement
.target
;
2797 case lang_input_statement_enum
:
2798 if (s
->input_statement
.real
)
2800 lang_statement_list_type add
;
2802 s
->input_statement
.target
= current_target
;
2804 /* If we are being called from within a group, and this
2805 is an archive which has already been searched, then
2806 force it to be researched unless the whole archive
2807 has been loaded already. */
2809 && !s
->input_statement
.whole_archive
2810 && s
->input_statement
.loaded
2811 && bfd_check_format (s
->input_statement
.the_bfd
,
2813 s
->input_statement
.loaded
= FALSE
;
2815 lang_list_init (&add
);
2817 if (! load_symbols (&s
->input_statement
, &add
))
2818 config
.make_executable
= FALSE
;
2820 if (add
.head
!= NULL
)
2822 *add
.tail
= s
->header
.next
;
2823 s
->header
.next
= add
.head
;
2833 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2836 lang_track_definedness (const char *name
)
2838 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
2839 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
2842 /* New-function for the definedness hash table. */
2844 static struct bfd_hash_entry
*
2845 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
2846 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
2847 const char *name ATTRIBUTE_UNUSED
)
2849 struct lang_definedness_hash_entry
*ret
2850 = (struct lang_definedness_hash_entry
*) entry
;
2853 ret
= (struct lang_definedness_hash_entry
*)
2854 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
2857 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
2859 ret
->iteration
= -1;
2863 /* Return the iteration when the definition of NAME was last updated. A
2864 value of -1 means that the symbol is not defined in the linker script
2865 or the command line, but may be defined in the linker symbol table. */
2868 lang_symbol_definition_iteration (const char *name
)
2870 struct lang_definedness_hash_entry
*defentry
2871 = (struct lang_definedness_hash_entry
*)
2872 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
2874 /* We've already created this one on the presence of DEFINED in the
2875 script, so it can't be NULL unless something is borked elsewhere in
2877 if (defentry
== NULL
)
2880 return defentry
->iteration
;
2883 /* Update the definedness state of NAME. */
2886 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
2888 struct lang_definedness_hash_entry
*defentry
2889 = (struct lang_definedness_hash_entry
*)
2890 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
2892 /* We don't keep track of symbols not tested with DEFINED. */
2893 if (defentry
== NULL
)
2896 /* If the symbol was already defined, and not from an earlier statement
2897 iteration, don't update the definedness iteration, because that'd
2898 make the symbol seem defined in the linker script at this point, and
2899 it wasn't; it was defined in some object. If we do anyway, DEFINED
2900 would start to yield false before this point and the construct "sym =
2901 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2903 if (h
->type
!= bfd_link_hash_undefined
2904 && h
->type
!= bfd_link_hash_common
2905 && h
->type
!= bfd_link_hash_new
2906 && defentry
->iteration
== -1)
2909 defentry
->iteration
= lang_statement_iteration
;
2912 /* Add the supplied name to the symbol table as an undefined reference.
2913 This is a two step process as the symbol table doesn't even exist at
2914 the time the ld command line is processed. First we put the name
2915 on a list, then, once the output file has been opened, transfer the
2916 name to the symbol table. */
2918 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2920 #define ldlang_undef_chain_list_head entry_symbol.next
2923 ldlang_add_undef (const char *const name
)
2925 ldlang_undef_chain_list_type
*new =
2926 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
2928 new->next
= ldlang_undef_chain_list_head
;
2929 ldlang_undef_chain_list_head
= new;
2931 new->name
= xstrdup (name
);
2933 if (output_bfd
!= NULL
)
2934 insert_undefined (new->name
);
2937 /* Insert NAME as undefined in the symbol table. */
2940 insert_undefined (const char *name
)
2942 struct bfd_link_hash_entry
*h
;
2944 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2946 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2947 if (h
->type
== bfd_link_hash_new
)
2949 h
->type
= bfd_link_hash_undefined
;
2950 h
->u
.undef
.abfd
= NULL
;
2951 bfd_link_add_undef (link_info
.hash
, h
);
2955 /* Run through the list of undefineds created above and place them
2956 into the linker hash table as undefined symbols belonging to the
2960 lang_place_undefineds (void)
2962 ldlang_undef_chain_list_type
*ptr
;
2964 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
2965 insert_undefined (ptr
->name
);
2968 /* Check for all readonly or some readwrite sections. */
2971 check_input_sections
2972 (lang_statement_union_type
*s
,
2973 lang_output_section_statement_type
*output_section_statement
)
2975 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
2977 switch (s
->header
.type
)
2979 case lang_wild_statement_enum
:
2980 walk_wild (&s
->wild_statement
, check_section_callback
,
2981 output_section_statement
);
2982 if (! output_section_statement
->all_input_readonly
)
2985 case lang_constructors_statement_enum
:
2986 check_input_sections (constructor_list
.head
,
2987 output_section_statement
);
2988 if (! output_section_statement
->all_input_readonly
)
2991 case lang_group_statement_enum
:
2992 check_input_sections (s
->group_statement
.children
.head
,
2993 output_section_statement
);
2994 if (! output_section_statement
->all_input_readonly
)
3003 /* Update wildcard statements if needed. */
3006 update_wild_statements (lang_statement_union_type
*s
)
3008 struct wildcard_list
*sec
;
3010 switch (sort_section
)
3020 for (; s
!= NULL
; s
= s
->header
.next
)
3022 switch (s
->header
.type
)
3027 case lang_wild_statement_enum
:
3028 sec
= s
->wild_statement
.section_list
;
3031 switch (sec
->spec
.sorted
)
3034 sec
->spec
.sorted
= sort_section
;
3037 if (sort_section
== by_alignment
)
3038 sec
->spec
.sorted
= by_name_alignment
;
3041 if (sort_section
== by_name
)
3042 sec
->spec
.sorted
= by_alignment_name
;
3050 case lang_constructors_statement_enum
:
3051 update_wild_statements (constructor_list
.head
);
3054 case lang_output_section_statement_enum
:
3055 update_wild_statements
3056 (s
->output_section_statement
.children
.head
);
3059 case lang_group_statement_enum
:
3060 update_wild_statements (s
->group_statement
.children
.head
);
3068 /* Open input files and attach to output sections. */
3071 map_input_to_output_sections
3072 (lang_statement_union_type
*s
, const char *target
,
3073 lang_output_section_statement_type
*os
)
3075 for (; s
!= NULL
; s
= s
->header
.next
)
3077 switch (s
->header
.type
)
3079 case lang_wild_statement_enum
:
3080 wild (&s
->wild_statement
, target
, os
);
3082 case lang_constructors_statement_enum
:
3083 map_input_to_output_sections (constructor_list
.head
,
3087 case lang_output_section_statement_enum
:
3088 if (s
->output_section_statement
.constraint
)
3090 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3091 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3093 s
->output_section_statement
.all_input_readonly
= TRUE
;
3094 check_input_sections (s
->output_section_statement
.children
.head
,
3095 &s
->output_section_statement
);
3096 if ((s
->output_section_statement
.all_input_readonly
3097 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3098 || (!s
->output_section_statement
.all_input_readonly
3099 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3101 s
->output_section_statement
.constraint
= -1;
3106 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3108 &s
->output_section_statement
);
3110 case lang_output_statement_enum
:
3112 case lang_target_statement_enum
:
3113 target
= s
->target_statement
.target
;
3115 case lang_group_statement_enum
:
3116 map_input_to_output_sections (s
->group_statement
.children
.head
,
3120 case lang_data_statement_enum
:
3121 /* Make sure that any sections mentioned in the expression
3123 exp_init_os (s
->data_statement
.exp
);
3124 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3126 /* The output section gets contents, and then we inspect for
3127 any flags set in the input script which override any ALLOC. */
3128 os
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3129 if (!(os
->flags
& SEC_NEVER_LOAD
))
3130 os
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
3132 case lang_fill_statement_enum
:
3133 case lang_input_section_enum
:
3134 case lang_object_symbols_statement_enum
:
3135 case lang_reloc_statement_enum
:
3136 case lang_padding_statement_enum
:
3137 case lang_input_statement_enum
:
3138 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3141 case lang_assignment_statement_enum
:
3142 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3145 /* Make sure that any sections mentioned in the assignment
3147 exp_init_os (s
->assignment_statement
.exp
);
3149 case lang_afile_asection_pair_statement_enum
:
3152 case lang_address_statement_enum
:
3153 /* Mark the specified section with the supplied address.
3155 If this section was actually a segment marker, then the
3156 directive is ignored if the linker script explicitly
3157 processed the segment marker. Originally, the linker
3158 treated segment directives (like -Ttext on the
3159 command-line) as section directives. We honor the
3160 section directive semantics for backwards compatibilty;
3161 linker scripts that do not specifically check for
3162 SEGMENT_START automatically get the old semantics. */
3163 if (!s
->address_statement
.segment
3164 || !s
->address_statement
.segment
->used
)
3166 lang_output_section_statement_type
*aos
3167 = (lang_output_section_statement_lookup
3168 (s
->address_statement
.section_name
));
3170 if (aos
->bfd_section
== NULL
)
3171 init_os (aos
, NULL
);
3172 aos
->addr_tree
= s
->address_statement
.address
;
3179 /* An output section might have been removed after its statement was
3180 added. For example, ldemul_before_allocation can remove dynamic
3181 sections if they turn out to be not needed. Clean them up here. */
3184 strip_excluded_output_sections (void)
3186 lang_output_section_statement_type
*os
;
3188 /* Run lang_size_sections (if not already done). */
3189 if (expld
.phase
!= lang_mark_phase_enum
)
3191 expld
.phase
= lang_mark_phase_enum
;
3192 expld
.dataseg
.phase
= exp_dataseg_none
;
3193 one_lang_size_sections_pass (NULL
, FALSE
);
3194 lang_reset_memory_regions ();
3197 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3201 asection
*output_section
;
3202 bfd_boolean exclude
;
3204 if (os
->constraint
== -1)
3207 output_section
= os
->bfd_section
;
3208 if (output_section
== NULL
)
3211 exclude
= (output_section
->rawsize
== 0
3212 && (output_section
->flags
& SEC_KEEP
) == 0
3213 && !bfd_section_removed_from_list (output_bfd
,
3216 /* Some sections have not yet been sized, notably .gnu.version,
3217 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3218 input sections, so don't drop output sections that have such
3219 input sections unless they are also marked SEC_EXCLUDE. */
3220 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3224 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3225 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3226 && (s
->flags
& SEC_EXCLUDE
) == 0)
3233 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3234 output_section
->map_head
.link_order
= NULL
;
3235 output_section
->map_tail
.link_order
= NULL
;
3239 /* We don't set bfd_section to NULL since bfd_section of the
3240 removed output section statement may still be used. */
3242 output_section
->flags
|= SEC_EXCLUDE
;
3243 bfd_section_list_remove (output_bfd
, output_section
);
3244 output_bfd
->section_count
--;
3248 /* Stop future calls to lang_add_section from messing with map_head
3249 and map_tail link_order fields. */
3250 stripped_excluded_sections
= TRUE
;
3254 print_output_section_statement
3255 (lang_output_section_statement_type
*output_section_statement
)
3257 asection
*section
= output_section_statement
->bfd_section
;
3260 if (output_section_statement
!= abs_output_section
)
3262 minfo ("\n%s", output_section_statement
->name
);
3264 if (section
!= NULL
)
3266 print_dot
= section
->vma
;
3268 len
= strlen (output_section_statement
->name
);
3269 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3274 while (len
< SECTION_NAME_MAP_LENGTH
)
3280 minfo ("0x%V %W", section
->vma
, section
->size
);
3282 if (output_section_statement
->load_base
!= NULL
)
3286 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
3288 minfo (_(" load address 0x%V"), addr
);
3295 print_statement_list (output_section_statement
->children
.head
,
3296 output_section_statement
);
3299 /* Scan for the use of the destination in the right hand side
3300 of an expression. In such cases we will not compute the
3301 correct expression, since the value of DST that is used on
3302 the right hand side will be its final value, not its value
3303 just before this expression is evaluated. */
3306 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3308 if (rhs
== NULL
|| dst
== NULL
)
3311 switch (rhs
->type
.node_class
)
3314 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3315 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3318 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3319 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3322 case etree_provided
:
3324 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3326 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3329 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3333 return strcmp (dst
, rhs
->value
.str
) == 0;
3338 return strcmp (dst
, rhs
->name
.name
) == 0;
3350 print_assignment (lang_assignment_statement_type
*assignment
,
3351 lang_output_section_statement_type
*output_section
)
3355 bfd_boolean computation_is_valid
= TRUE
;
3358 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3361 if (assignment
->exp
->type
.node_class
== etree_assert
)
3364 tree
= assignment
->exp
->assert_s
.child
;
3365 computation_is_valid
= TRUE
;
3369 const char *dst
= assignment
->exp
->assign
.dst
;
3371 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3372 tree
= assignment
->exp
->assign
.src
;
3373 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3376 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3377 if (expld
.result
.valid_p
)
3381 if (computation_is_valid
)
3383 value
= expld
.result
.value
;
3385 if (expld
.result
.section
)
3386 value
+= expld
.result
.section
->vma
;
3388 minfo ("0x%V", value
);
3394 struct bfd_link_hash_entry
*h
;
3396 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3397 FALSE
, FALSE
, TRUE
);
3400 value
= h
->u
.def
.value
;
3402 if (expld
.result
.section
)
3403 value
+= expld
.result
.section
->vma
;
3405 minfo ("[0x%V]", value
);
3408 minfo ("[unresolved]");
3420 exp_print_tree (assignment
->exp
);
3425 print_input_statement (lang_input_statement_type
*statm
)
3427 if (statm
->filename
!= NULL
)
3429 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3433 /* Print all symbols defined in a particular section. This is called
3434 via bfd_link_hash_traverse, or by print_all_symbols. */
3437 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3439 asection
*sec
= ptr
;
3441 if ((hash_entry
->type
== bfd_link_hash_defined
3442 || hash_entry
->type
== bfd_link_hash_defweak
)
3443 && sec
== hash_entry
->u
.def
.section
)
3447 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3450 (hash_entry
->u
.def
.value
3451 + hash_entry
->u
.def
.section
->output_offset
3452 + hash_entry
->u
.def
.section
->output_section
->vma
));
3454 minfo (" %T\n", hash_entry
->root
.string
);
3461 print_all_symbols (sec
)
3464 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3465 struct map_symbol_def
*def
;
3470 *ud
->map_symbol_def_tail
= 0;
3471 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3472 print_one_symbol (def
->entry
, sec
);
3475 /* Print information about an input section to the map file. */
3478 print_input_section (lang_input_section_type
*in
)
3480 asection
*i
= in
->section
;
3481 bfd_size_type size
= i
->size
;
3490 minfo ("%s", i
->name
);
3492 len
= 1 + strlen (i
->name
);
3493 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3498 while (len
< SECTION_NAME_MAP_LENGTH
)
3504 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
3505 addr
= i
->output_section
->vma
+ i
->output_offset
;
3512 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3514 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3516 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3528 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3531 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
3533 if (command_line
.reduce_memory_overheads
)
3534 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3536 print_all_symbols (i
);
3538 print_dot
= addr
+ TO_ADDR (size
);
3544 print_fill_statement (lang_fill_statement_type
*fill
)
3548 fputs (" FILL mask 0x", config
.map_file
);
3549 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3550 fprintf (config
.map_file
, "%02x", *p
);
3551 fputs ("\n", config
.map_file
);
3555 print_data_statement (lang_data_statement_type
*data
)
3563 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3566 addr
= data
->output_offset
;
3567 if (data
->output_section
!= NULL
)
3568 addr
+= data
->output_section
->vma
;
3596 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3598 if (data
->exp
->type
.node_class
!= etree_value
)
3601 exp_print_tree (data
->exp
);
3606 print_dot
= addr
+ TO_ADDR (size
);
3609 /* Print an address statement. These are generated by options like
3613 print_address_statement (lang_address_statement_type
*address
)
3615 minfo (_("Address of section %s set to "), address
->section_name
);
3616 exp_print_tree (address
->address
);
3620 /* Print a reloc statement. */
3623 print_reloc_statement (lang_reloc_statement_type
*reloc
)
3630 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3633 addr
= reloc
->output_offset
;
3634 if (reloc
->output_section
!= NULL
)
3635 addr
+= reloc
->output_section
->vma
;
3637 size
= bfd_get_reloc_size (reloc
->howto
);
3639 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
3641 if (reloc
->name
!= NULL
)
3642 minfo ("%s+", reloc
->name
);
3644 minfo ("%s+", reloc
->section
->name
);
3646 exp_print_tree (reloc
->addend_exp
);
3650 print_dot
= addr
+ TO_ADDR (size
);
3654 print_padding_statement (lang_padding_statement_type
*s
)
3662 len
= sizeof " *fill*" - 1;
3663 while (len
< SECTION_NAME_MAP_LENGTH
)
3669 addr
= s
->output_offset
;
3670 if (s
->output_section
!= NULL
)
3671 addr
+= s
->output_section
->vma
;
3672 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
3674 if (s
->fill
->size
!= 0)
3678 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
3679 fprintf (config
.map_file
, "%02x", *p
);
3684 print_dot
= addr
+ TO_ADDR (s
->size
);
3688 print_wild_statement (lang_wild_statement_type
*w
,
3689 lang_output_section_statement_type
*os
)
3691 struct wildcard_list
*sec
;
3695 if (w
->filenames_sorted
)
3697 if (w
->filename
!= NULL
)
3698 minfo ("%s", w
->filename
);
3701 if (w
->filenames_sorted
)
3705 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
3707 if (sec
->spec
.sorted
)
3709 if (sec
->spec
.exclude_name_list
!= NULL
)
3712 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
3713 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
3714 minfo (" %s", tmp
->name
);
3717 if (sec
->spec
.name
!= NULL
)
3718 minfo ("%s", sec
->spec
.name
);
3721 if (sec
->spec
.sorted
)
3730 print_statement_list (w
->children
.head
, os
);
3733 /* Print a group statement. */
3736 print_group (lang_group_statement_type
*s
,
3737 lang_output_section_statement_type
*os
)
3739 fprintf (config
.map_file
, "START GROUP\n");
3740 print_statement_list (s
->children
.head
, os
);
3741 fprintf (config
.map_file
, "END GROUP\n");
3744 /* Print the list of statements in S.
3745 This can be called for any statement type. */
3748 print_statement_list (lang_statement_union_type
*s
,
3749 lang_output_section_statement_type
*os
)
3753 print_statement (s
, os
);
3758 /* Print the first statement in statement list S.
3759 This can be called for any statement type. */
3762 print_statement (lang_statement_union_type
*s
,
3763 lang_output_section_statement_type
*os
)
3765 switch (s
->header
.type
)
3768 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
3771 case lang_constructors_statement_enum
:
3772 if (constructor_list
.head
!= NULL
)
3774 if (constructors_sorted
)
3775 minfo (" SORT (CONSTRUCTORS)\n");
3777 minfo (" CONSTRUCTORS\n");
3778 print_statement_list (constructor_list
.head
, os
);
3781 case lang_wild_statement_enum
:
3782 print_wild_statement (&s
->wild_statement
, os
);
3784 case lang_address_statement_enum
:
3785 print_address_statement (&s
->address_statement
);
3787 case lang_object_symbols_statement_enum
:
3788 minfo (" CREATE_OBJECT_SYMBOLS\n");
3790 case lang_fill_statement_enum
:
3791 print_fill_statement (&s
->fill_statement
);
3793 case lang_data_statement_enum
:
3794 print_data_statement (&s
->data_statement
);
3796 case lang_reloc_statement_enum
:
3797 print_reloc_statement (&s
->reloc_statement
);
3799 case lang_input_section_enum
:
3800 print_input_section (&s
->input_section
);
3802 case lang_padding_statement_enum
:
3803 print_padding_statement (&s
->padding_statement
);
3805 case lang_output_section_statement_enum
:
3806 print_output_section_statement (&s
->output_section_statement
);
3808 case lang_assignment_statement_enum
:
3809 print_assignment (&s
->assignment_statement
, os
);
3811 case lang_target_statement_enum
:
3812 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
3814 case lang_output_statement_enum
:
3815 minfo ("OUTPUT(%s", s
->output_statement
.name
);
3816 if (output_target
!= NULL
)
3817 minfo (" %s", output_target
);
3820 case lang_input_statement_enum
:
3821 print_input_statement (&s
->input_statement
);
3823 case lang_group_statement_enum
:
3824 print_group (&s
->group_statement
, os
);
3826 case lang_afile_asection_pair_statement_enum
:
3833 print_statements (void)
3835 print_statement_list (statement_list
.head
, abs_output_section
);
3838 /* Print the first N statements in statement list S to STDERR.
3839 If N == 0, nothing is printed.
3840 If N < 0, the entire list is printed.
3841 Intended to be called from GDB. */
3844 dprint_statement (lang_statement_union_type
*s
, int n
)
3846 FILE *map_save
= config
.map_file
;
3848 config
.map_file
= stderr
;
3851 print_statement_list (s
, abs_output_section
);
3854 while (s
&& --n
>= 0)
3856 print_statement (s
, abs_output_section
);
3861 config
.map_file
= map_save
;
3865 insert_pad (lang_statement_union_type
**ptr
,
3867 unsigned int alignment_needed
,
3868 asection
*output_section
,
3871 static fill_type zero_fill
= { 1, { 0 } };
3872 lang_statement_union_type
*pad
= NULL
;
3874 if (ptr
!= &statement_list
.head
)
3875 pad
= ((lang_statement_union_type
*)
3876 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
3878 && pad
->header
.type
== lang_padding_statement_enum
3879 && pad
->padding_statement
.output_section
== output_section
)
3881 /* Use the existing pad statement. */
3883 else if ((pad
= *ptr
) != NULL
3884 && pad
->header
.type
== lang_padding_statement_enum
3885 && pad
->padding_statement
.output_section
== output_section
)
3887 /* Use the existing pad statement. */
3891 /* Make a new padding statement, linked into existing chain. */
3892 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
3893 pad
->header
.next
= *ptr
;
3895 pad
->header
.type
= lang_padding_statement_enum
;
3896 pad
->padding_statement
.output_section
= output_section
;
3899 pad
->padding_statement
.fill
= fill
;
3901 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
3902 pad
->padding_statement
.size
= alignment_needed
;
3903 output_section
->size
+= alignment_needed
;
3906 /* Work out how much this section will move the dot point. */
3910 (lang_statement_union_type
**this_ptr
,
3911 lang_output_section_statement_type
*output_section_statement
,
3915 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
3916 asection
*i
= is
->section
;
3918 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
3919 && (i
->flags
& SEC_EXCLUDE
) == 0)
3921 unsigned int alignment_needed
;
3924 /* Align this section first to the input sections requirement,
3925 then to the output section's requirement. If this alignment
3926 is greater than any seen before, then record it too. Perform
3927 the alignment by inserting a magic 'padding' statement. */
3929 if (output_section_statement
->subsection_alignment
!= -1)
3930 i
->alignment_power
= output_section_statement
->subsection_alignment
;
3932 o
= output_section_statement
->bfd_section
;
3933 if (o
->alignment_power
< i
->alignment_power
)
3934 o
->alignment_power
= i
->alignment_power
;
3936 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
3938 if (alignment_needed
!= 0)
3940 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
3941 dot
+= alignment_needed
;
3944 /* Remember where in the output section this input section goes. */
3946 i
->output_offset
= dot
- o
->vma
;
3948 /* Mark how big the output section must be to contain this now. */
3949 dot
+= TO_ADDR (i
->size
);
3950 o
->size
= TO_SIZE (dot
- o
->vma
);
3954 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
3961 sort_sections_by_lma (const void *arg1
, const void *arg2
)
3963 const asection
*sec1
= *(const asection
**) arg1
;
3964 const asection
*sec2
= *(const asection
**) arg2
;
3966 if (bfd_section_lma (sec1
->owner
, sec1
)
3967 < bfd_section_lma (sec2
->owner
, sec2
))
3969 else if (bfd_section_lma (sec1
->owner
, sec1
)
3970 > bfd_section_lma (sec2
->owner
, sec2
))
3976 #define IGNORE_SECTION(s) \
3977 ((s->flags & SEC_NEVER_LOAD) != 0 \
3978 || (s->flags & SEC_ALLOC) == 0 \
3979 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
3980 && (s->flags & SEC_LOAD) == 0))
3982 /* Check to see if any allocated sections overlap with other allocated
3983 sections. This can happen if a linker script specifies the output
3984 section addresses of the two sections. */
3987 lang_check_section_addresses (void)
3990 asection
**sections
, **spp
;
3998 if (bfd_count_sections (output_bfd
) <= 1)
4001 amt
= bfd_count_sections (output_bfd
) * sizeof (asection
*);
4002 sections
= xmalloc (amt
);
4004 /* Scan all sections in the output list. */
4006 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4008 /* Only consider loadable sections with real contents. */
4009 if (IGNORE_SECTION (s
) || s
->size
== 0)
4012 sections
[count
] = s
;
4019 qsort (sections
, (size_t) count
, sizeof (asection
*),
4020 sort_sections_by_lma
);
4024 s_start
= bfd_section_lma (output_bfd
, s
);
4025 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4026 for (count
--; count
; count
--)
4028 /* We must check the sections' LMA addresses not their VMA
4029 addresses because overlay sections can have overlapping VMAs
4030 but they must have distinct LMAs. */
4035 s_start
= bfd_section_lma (output_bfd
, s
);
4036 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4038 /* Look for an overlap. */
4039 if (s_end
>= os_start
&& s_start
<= os_end
)
4040 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4041 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4047 /* Make sure the new address is within the region. We explicitly permit the
4048 current address to be at the exact end of the region when the address is
4049 non-zero, in case the region is at the end of addressable memory and the
4050 calculation wraps around. */
4053 os_region_check (lang_output_section_statement_type
*os
,
4054 lang_memory_region_type
*region
,
4058 if ((region
->current
< region
->origin
4059 || (region
->current
- region
->origin
> region
->length
))
4060 && ((region
->current
!= region
->origin
+ region
->length
)
4065 einfo (_("%X%P: address 0x%v of %B section %s"
4066 " is not within region %s\n"),
4068 os
->bfd_section
->owner
,
4069 os
->bfd_section
->name
,
4074 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4076 os
->bfd_section
->owner
,
4077 os
->bfd_section
->name
);
4079 /* Reset the region pointer. */
4080 region
->current
= region
->origin
;
4084 /* Set the sizes for all the output sections. */
4087 lang_size_sections_1
4088 (lang_statement_union_type
*s
,
4089 lang_output_section_statement_type
*output_section_statement
,
4090 lang_statement_union_type
**prev
,
4094 bfd_boolean check_regions
)
4096 /* Size up the sections from their constituent parts. */
4097 for (; s
!= NULL
; s
= s
->header
.next
)
4099 switch (s
->header
.type
)
4101 case lang_output_section_statement_enum
:
4103 bfd_vma newdot
, after
;
4104 lang_output_section_statement_type
*os
;
4106 os
= &s
->output_section_statement
;
4107 if (os
->addr_tree
!= NULL
)
4109 os
->processed
= FALSE
;
4110 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4112 if (!expld
.result
.valid_p
4113 && expld
.phase
!= lang_mark_phase_enum
)
4114 einfo (_("%F%S: non constant or forward reference"
4115 " address expression for section %s\n"),
4118 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4121 if (os
->bfd_section
== NULL
)
4122 /* This section was removed or never actually created. */
4125 /* If this is a COFF shared library section, use the size and
4126 address from the input section. FIXME: This is COFF
4127 specific; it would be cleaner if there were some other way
4128 to do this, but nothing simple comes to mind. */
4129 if ((bfd_get_flavour (output_bfd
) == bfd_target_ecoff_flavour
4130 || bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
4131 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4135 if (os
->children
.head
== NULL
4136 || os
->children
.head
->header
.next
!= NULL
4137 || (os
->children
.head
->header
.type
4138 != lang_input_section_enum
))
4139 einfo (_("%P%X: Internal error on COFF shared library"
4140 " section %s\n"), os
->name
);
4142 input
= os
->children
.head
->input_section
.section
;
4143 bfd_set_section_vma (os
->bfd_section
->owner
,
4145 bfd_section_vma (input
->owner
, input
));
4146 os
->bfd_section
->size
= input
->size
;
4151 if (bfd_is_abs_section (os
->bfd_section
))
4153 /* No matter what happens, an abs section starts at zero. */
4154 ASSERT (os
->bfd_section
->vma
== 0);
4160 if (os
->addr_tree
== NULL
)
4162 /* No address specified for this section, get one
4163 from the region specification. */
4164 if (os
->region
== NULL
4165 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4166 && os
->region
->name
[0] == '*'
4167 && strcmp (os
->region
->name
,
4168 DEFAULT_MEMORY_REGION
) == 0))
4170 os
->region
= lang_memory_default (os
->bfd_section
);
4173 /* If a loadable section is using the default memory
4174 region, and some non default memory regions were
4175 defined, issue an error message. */
4176 if (!IGNORE_SECTION (os
->bfd_section
)
4177 && ! link_info
.relocatable
4179 && strcmp (os
->region
->name
,
4180 DEFAULT_MEMORY_REGION
) == 0
4181 && lang_memory_region_list
!= NULL
4182 && (strcmp (lang_memory_region_list
->name
,
4183 DEFAULT_MEMORY_REGION
) != 0
4184 || lang_memory_region_list
->next
!= NULL
)
4185 && expld
.phase
!= lang_mark_phase_enum
)
4187 /* By default this is an error rather than just a
4188 warning because if we allocate the section to the
4189 default memory region we can end up creating an
4190 excessively large binary, or even seg faulting when
4191 attempting to perform a negative seek. See
4192 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4193 for an example of this. This behaviour can be
4194 overridden by the using the --no-check-sections
4196 if (command_line
.check_section_addresses
)
4197 einfo (_("%P%F: error: no memory region specified"
4198 " for loadable section `%s'\n"),
4199 bfd_get_section_name (output_bfd
,
4202 einfo (_("%P: warning: no memory region specified"
4203 " for loadable section `%s'\n"),
4204 bfd_get_section_name (output_bfd
,
4208 newdot
= os
->region
->current
;
4209 align
= os
->bfd_section
->alignment_power
;
4212 align
= os
->section_alignment
;
4214 /* Align to what the section needs. */
4217 bfd_vma savedot
= newdot
;
4218 newdot
= align_power (newdot
, align
);
4220 if (newdot
!= savedot
4221 && (config
.warn_section_align
4222 || os
->addr_tree
!= NULL
)
4223 && expld
.phase
!= lang_mark_phase_enum
)
4224 einfo (_("%P: warning: changing start of section"
4225 " %s by %lu bytes\n"),
4226 os
->name
, (unsigned long) (newdot
- savedot
));
4229 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4231 os
->bfd_section
->output_offset
= 0;
4234 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4235 os
->fill
, newdot
, relax
, check_regions
);
4237 os
->processed
= TRUE
;
4239 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4241 ASSERT (os
->bfd_section
->size
== 0);
4245 dot
= os
->bfd_section
->vma
;
4247 /* Put the section within the requested block size, or
4248 align at the block boundary. */
4250 + TO_ADDR (os
->bfd_section
->size
)
4251 + os
->block_value
- 1)
4252 & - (bfd_vma
) os
->block_value
);
4254 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4256 /* .tbss sections effectively have zero size. */
4257 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4258 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4259 || link_info
.relocatable
)
4260 dot
+= TO_ADDR (os
->bfd_section
->size
);
4262 if (os
->update_dot_tree
!= 0)
4263 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4265 /* Update dot in the region ?
4266 We only do this if the section is going to be allocated,
4267 since unallocated sections do not contribute to the region's
4268 overall size in memory.
4270 If the SEC_NEVER_LOAD bit is not set, it will affect the
4271 addresses of sections after it. We have to update
4273 if (os
->region
!= NULL
4274 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4275 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4277 os
->region
->current
= dot
;
4280 /* Make sure the new address is within the region. */
4281 os_region_check (os
, os
->region
, os
->addr_tree
,
4282 os
->bfd_section
->vma
);
4284 /* If there's no load address specified, use the run
4285 region as the load region. */
4286 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
4287 os
->lma_region
= os
->region
;
4289 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
4291 /* Set load_base, which will be handled later. */
4292 os
->load_base
= exp_intop (os
->lma_region
->current
);
4293 os
->lma_region
->current
+=
4294 TO_ADDR (os
->bfd_section
->size
);
4296 os_region_check (os
, os
->lma_region
, NULL
,
4297 os
->bfd_section
->lma
);
4303 case lang_constructors_statement_enum
:
4304 dot
= lang_size_sections_1 (constructor_list
.head
,
4305 output_section_statement
,
4306 &s
->wild_statement
.children
.head
,
4307 fill
, dot
, relax
, check_regions
);
4310 case lang_data_statement_enum
:
4312 unsigned int size
= 0;
4314 s
->data_statement
.output_offset
=
4315 dot
- output_section_statement
->bfd_section
->vma
;
4316 s
->data_statement
.output_section
=
4317 output_section_statement
->bfd_section
;
4319 /* We might refer to provided symbols in the expression, and
4320 need to mark them as needed. */
4321 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4323 switch (s
->data_statement
.type
)
4341 if (size
< TO_SIZE ((unsigned) 1))
4342 size
= TO_SIZE ((unsigned) 1);
4343 dot
+= TO_ADDR (size
);
4344 output_section_statement
->bfd_section
->size
+= size
;
4348 case lang_reloc_statement_enum
:
4352 s
->reloc_statement
.output_offset
=
4353 dot
- output_section_statement
->bfd_section
->vma
;
4354 s
->reloc_statement
.output_section
=
4355 output_section_statement
->bfd_section
;
4356 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4357 dot
+= TO_ADDR (size
);
4358 output_section_statement
->bfd_section
->size
+= size
;
4362 case lang_wild_statement_enum
:
4363 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4364 output_section_statement
,
4365 &s
->wild_statement
.children
.head
,
4366 fill
, dot
, relax
, check_regions
);
4369 case lang_object_symbols_statement_enum
:
4370 link_info
.create_object_symbols_section
=
4371 output_section_statement
->bfd_section
;
4374 case lang_output_statement_enum
:
4375 case lang_target_statement_enum
:
4378 case lang_input_section_enum
:
4382 i
= (*prev
)->input_section
.section
;
4387 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4388 einfo (_("%P%F: can't relax section: %E\n"));
4392 dot
= size_input_section (prev
, output_section_statement
,
4393 output_section_statement
->fill
, dot
);
4397 case lang_input_statement_enum
:
4400 case lang_fill_statement_enum
:
4401 s
->fill_statement
.output_section
=
4402 output_section_statement
->bfd_section
;
4404 fill
= s
->fill_statement
.fill
;
4407 case lang_assignment_statement_enum
:
4409 bfd_vma newdot
= dot
;
4411 exp_fold_tree (s
->assignment_statement
.exp
,
4412 output_section_statement
->bfd_section
,
4415 if (newdot
!= dot
&& !output_section_statement
->ignored
)
4417 if (output_section_statement
== abs_output_section
)
4419 /* If we don't have an output section, then just adjust
4420 the default memory address. */
4421 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4422 FALSE
)->current
= newdot
;
4426 /* Insert a pad after this statement. We can't
4427 put the pad before when relaxing, in case the
4428 assignment references dot. */
4429 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4430 output_section_statement
->bfd_section
, dot
);
4432 /* Don't neuter the pad below when relaxing. */
4435 /* If dot is advanced, this implies that the section
4436 should have space allocated to it, unless the
4437 user has explicitly stated that the section
4438 should never be loaded. */
4439 if (!(output_section_statement
->flags
4440 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
4441 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4448 case lang_padding_statement_enum
:
4449 /* If this is the first time lang_size_sections is called,
4450 we won't have any padding statements. If this is the
4451 second or later passes when relaxing, we should allow
4452 padding to shrink. If padding is needed on this pass, it
4453 will be added back in. */
4454 s
->padding_statement
.size
= 0;
4456 /* Make sure output_offset is valid. If relaxation shrinks
4457 the section and this pad isn't needed, it's possible to
4458 have output_offset larger than the final size of the
4459 section. bfd_set_section_contents will complain even for
4460 a pad size of zero. */
4461 s
->padding_statement
.output_offset
4462 = dot
- output_section_statement
->bfd_section
->vma
;
4465 case lang_group_statement_enum
:
4466 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4467 output_section_statement
,
4468 &s
->group_statement
.children
.head
,
4469 fill
, dot
, relax
, check_regions
);
4476 /* We can only get here when relaxing is turned on. */
4477 case lang_address_statement_enum
:
4480 prev
= &s
->header
.next
;
4486 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
4488 lang_statement_iteration
++;
4489 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
4490 &statement_list
.head
, 0, 0, relax
, check_regions
);
4494 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
4496 expld
.phase
= lang_allocating_phase_enum
;
4497 expld
.dataseg
.phase
= exp_dataseg_none
;
4499 one_lang_size_sections_pass (relax
, check_regions
);
4500 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
4501 && link_info
.relro
&& expld
.dataseg
.relro_end
)
4503 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4504 to put expld.dataseg.relro on a (common) page boundary. */
4505 bfd_vma old_min_base
, relro_end
, maxpage
;
4507 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
4508 old_min_base
= expld
.dataseg
.min_base
;
4509 maxpage
= expld
.dataseg
.maxpagesize
;
4510 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
4511 & (expld
.dataseg
.pagesize
- 1));
4512 /* Compute the expected PT_GNU_RELRO segment end. */
4513 relro_end
= (expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
4514 & ~(expld
.dataseg
.pagesize
- 1);
4515 if (old_min_base
+ maxpage
< expld
.dataseg
.base
)
4517 expld
.dataseg
.base
-= maxpage
;
4518 relro_end
-= maxpage
;
4520 one_lang_size_sections_pass (relax
, check_regions
);
4521 if (expld
.dataseg
.relro_end
> relro_end
)
4523 /* The alignment of sections between DATA_SEGMENT_ALIGN
4524 and DATA_SEGMENT_RELRO_END caused huge padding to be
4525 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4527 unsigned int max_alignment_power
= 0;
4529 /* Find maximum alignment power of sections between
4530 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4531 for (sec
= output_bfd
->sections
; sec
; sec
= sec
->next
)
4532 if (sec
->vma
>= expld
.dataseg
.base
4533 && sec
->vma
< expld
.dataseg
.relro_end
4534 && sec
->alignment_power
> max_alignment_power
)
4535 max_alignment_power
= sec
->alignment_power
;
4537 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
4539 if (expld
.dataseg
.base
- (1 << max_alignment_power
)
4541 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
4542 expld
.dataseg
.base
-= (1 << max_alignment_power
);
4543 one_lang_size_sections_pass (relax
, check_regions
);
4546 link_info
.relro_start
= expld
.dataseg
.base
;
4547 link_info
.relro_end
= expld
.dataseg
.relro_end
;
4549 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
4551 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4552 a page could be saved in the data segment. */
4553 bfd_vma first
, last
;
4555 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
4556 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
4558 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
4559 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
4560 && first
+ last
<= expld
.dataseg
.pagesize
)
4562 expld
.dataseg
.phase
= exp_dataseg_adjust
;
4563 one_lang_size_sections_pass (relax
, check_regions
);
4567 expld
.phase
= lang_final_phase_enum
;
4570 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4573 lang_do_assignments_1
4574 (lang_statement_union_type
*s
,
4575 lang_output_section_statement_type
*output_section_statement
,
4579 for (; s
!= NULL
; s
= s
->header
.next
)
4581 switch (s
->header
.type
)
4583 case lang_constructors_statement_enum
:
4584 dot
= lang_do_assignments_1 (constructor_list
.head
,
4585 output_section_statement
,
4590 case lang_output_section_statement_enum
:
4592 lang_output_section_statement_type
*os
;
4594 os
= &(s
->output_section_statement
);
4595 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
4597 dot
= os
->bfd_section
->vma
;
4598 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
4599 /* .tbss sections effectively have zero size. */
4600 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4601 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4602 || link_info
.relocatable
)
4603 dot
+= TO_ADDR (os
->bfd_section
->size
);
4607 /* If nothing has been placed into the output section then
4608 it won't have a bfd_section. */
4609 if (os
->bfd_section
&& !os
->ignored
)
4611 os
->bfd_section
->lma
4612 = exp_get_abs_int (os
->load_base
, 0, "load base");
4618 case lang_wild_statement_enum
:
4620 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
4621 output_section_statement
,
4625 case lang_object_symbols_statement_enum
:
4626 case lang_output_statement_enum
:
4627 case lang_target_statement_enum
:
4630 case lang_data_statement_enum
:
4631 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4632 if (expld
.result
.valid_p
)
4633 s
->data_statement
.value
= (expld
.result
.value
4634 + expld
.result
.section
->vma
);
4636 einfo (_("%F%P: invalid data statement\n"));
4639 switch (s
->data_statement
.type
)
4657 if (size
< TO_SIZE ((unsigned) 1))
4658 size
= TO_SIZE ((unsigned) 1);
4659 dot
+= TO_ADDR (size
);
4663 case lang_reloc_statement_enum
:
4664 exp_fold_tree (s
->reloc_statement
.addend_exp
,
4665 bfd_abs_section_ptr
, &dot
);
4666 if (expld
.result
.valid_p
)
4667 s
->reloc_statement
.addend_value
= expld
.result
.value
;
4669 einfo (_("%F%P: invalid reloc statement\n"));
4670 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
4673 case lang_input_section_enum
:
4675 asection
*in
= s
->input_section
.section
;
4677 if ((in
->flags
& SEC_EXCLUDE
) == 0)
4678 dot
+= TO_ADDR (in
->size
);
4682 case lang_input_statement_enum
:
4685 case lang_fill_statement_enum
:
4686 fill
= s
->fill_statement
.fill
;
4689 case lang_assignment_statement_enum
:
4690 exp_fold_tree (s
->assignment_statement
.exp
,
4691 output_section_statement
->bfd_section
,
4695 case lang_padding_statement_enum
:
4696 dot
+= TO_ADDR (s
->padding_statement
.size
);
4699 case lang_group_statement_enum
:
4700 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
4701 output_section_statement
,
4709 case lang_address_statement_enum
:
4717 lang_do_assignments (void)
4719 lang_statement_iteration
++;
4720 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
4723 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4724 operator .startof. (section_name), it produces an undefined symbol
4725 .startof.section_name. Similarly, when it sees
4726 .sizeof. (section_name), it produces an undefined symbol
4727 .sizeof.section_name. For all the output sections, we look for
4728 such symbols, and set them to the correct value. */
4731 lang_set_startof (void)
4735 if (link_info
.relocatable
)
4738 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4740 const char *secname
;
4742 struct bfd_link_hash_entry
*h
;
4744 secname
= bfd_get_section_name (output_bfd
, s
);
4745 buf
= xmalloc (10 + strlen (secname
));
4747 sprintf (buf
, ".startof.%s", secname
);
4748 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4749 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4751 h
->type
= bfd_link_hash_defined
;
4752 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
4753 h
->u
.def
.section
= bfd_abs_section_ptr
;
4756 sprintf (buf
, ".sizeof.%s", secname
);
4757 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4758 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4760 h
->type
= bfd_link_hash_defined
;
4761 h
->u
.def
.value
= TO_ADDR (s
->size
);
4762 h
->u
.def
.section
= bfd_abs_section_ptr
;
4772 struct bfd_link_hash_entry
*h
;
4775 if (link_info
.relocatable
|| link_info
.shared
)
4780 if (entry_symbol
.name
== NULL
)
4782 /* No entry has been specified. Look for the default entry, but
4783 don't warn if we don't find it. */
4784 entry_symbol
.name
= entry_symbol_default
;
4788 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
4789 FALSE
, FALSE
, TRUE
);
4791 && (h
->type
== bfd_link_hash_defined
4792 || h
->type
== bfd_link_hash_defweak
)
4793 && h
->u
.def
.section
->output_section
!= NULL
)
4797 val
= (h
->u
.def
.value
4798 + bfd_get_section_vma (output_bfd
,
4799 h
->u
.def
.section
->output_section
)
4800 + h
->u
.def
.section
->output_offset
);
4801 if (! bfd_set_start_address (output_bfd
, val
))
4802 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
4809 /* We couldn't find the entry symbol. Try parsing it as a
4811 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
4814 if (! bfd_set_start_address (output_bfd
, val
))
4815 einfo (_("%P%F: can't set start address\n"));
4821 /* Can't find the entry symbol, and it's not a number. Use
4822 the first address in the text section. */
4823 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
4827 einfo (_("%P: warning: cannot find entry symbol %s;"
4828 " defaulting to %V\n"),
4830 bfd_get_section_vma (output_bfd
, ts
));
4831 if (! bfd_set_start_address (output_bfd
,
4832 bfd_get_section_vma (output_bfd
,
4834 einfo (_("%P%F: can't set start address\n"));
4839 einfo (_("%P: warning: cannot find entry symbol %s;"
4840 " not setting start address\n"),
4846 /* Don't bfd_hash_table_free (&lang_definedness_table);
4847 map file output may result in a call of lang_track_definedness. */
4850 /* This is a small function used when we want to ignore errors from
4854 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
4856 /* Don't do anything. */
4859 /* Check that the architecture of all the input files is compatible
4860 with the output file. Also call the backend to let it do any
4861 other checking that is needed. */
4866 lang_statement_union_type
*file
;
4868 const bfd_arch_info_type
*compatible
;
4870 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
4872 input_bfd
= file
->input_statement
.the_bfd
;
4874 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
4875 command_line
.accept_unknown_input_arch
);
4877 /* In general it is not possible to perform a relocatable
4878 link between differing object formats when the input
4879 file has relocations, because the relocations in the
4880 input format may not have equivalent representations in
4881 the output format (and besides BFD does not translate
4882 relocs for other link purposes than a final link). */
4883 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
4884 && (compatible
== NULL
4885 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
4886 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
4888 einfo (_("%P%F: Relocatable linking with relocations from"
4889 " format %s (%B) to format %s (%B) is not supported\n"),
4890 bfd_get_target (input_bfd
), input_bfd
,
4891 bfd_get_target (output_bfd
), output_bfd
);
4892 /* einfo with %F exits. */
4895 if (compatible
== NULL
)
4897 if (command_line
.warn_mismatch
)
4898 einfo (_("%P: warning: %s architecture of input file `%B'"
4899 " is incompatible with %s output\n"),
4900 bfd_printable_name (input_bfd
), input_bfd
,
4901 bfd_printable_name (output_bfd
));
4903 else if (bfd_count_sections (input_bfd
))
4905 /* If the input bfd has no contents, it shouldn't set the
4906 private data of the output bfd. */
4908 bfd_error_handler_type pfn
= NULL
;
4910 /* If we aren't supposed to warn about mismatched input
4911 files, temporarily set the BFD error handler to a
4912 function which will do nothing. We still want to call
4913 bfd_merge_private_bfd_data, since it may set up
4914 information which is needed in the output file. */
4915 if (! command_line
.warn_mismatch
)
4916 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
4917 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
4919 if (command_line
.warn_mismatch
)
4920 einfo (_("%P%X: failed to merge target specific data"
4921 " of file %B\n"), input_bfd
);
4923 if (! command_line
.warn_mismatch
)
4924 bfd_set_error_handler (pfn
);
4929 /* Look through all the global common symbols and attach them to the
4930 correct section. The -sort-common command line switch may be used
4931 to roughly sort the entries by size. */
4936 if (command_line
.inhibit_common_definition
)
4938 if (link_info
.relocatable
4939 && ! command_line
.force_common_definition
)
4942 if (! config
.sort_common
)
4943 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
4948 for (power
= 4; power
>= 0; power
--)
4949 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
4953 /* Place one common symbol in the correct section. */
4956 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
4958 unsigned int power_of_two
;
4962 if (h
->type
!= bfd_link_hash_common
)
4966 power_of_two
= h
->u
.c
.p
->alignment_power
;
4968 if (config
.sort_common
4969 && power_of_two
< (unsigned int) *(int *) info
)
4972 section
= h
->u
.c
.p
->section
;
4974 /* Increase the size of the section to align the common sym. */
4975 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
4976 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
4978 /* Adjust the alignment if necessary. */
4979 if (power_of_two
> section
->alignment_power
)
4980 section
->alignment_power
= power_of_two
;
4982 /* Change the symbol from common to defined. */
4983 h
->type
= bfd_link_hash_defined
;
4984 h
->u
.def
.section
= section
;
4985 h
->u
.def
.value
= section
->size
;
4987 /* Increase the size of the section. */
4988 section
->size
+= size
;
4990 /* Make sure the section is allocated in memory, and make sure that
4991 it is no longer a common section. */
4992 section
->flags
|= SEC_ALLOC
;
4993 section
->flags
&= ~SEC_IS_COMMON
;
4995 if (config
.map_file
!= NULL
)
4997 static bfd_boolean header_printed
;
5002 if (! header_printed
)
5004 minfo (_("\nAllocating common symbols\n"));
5005 minfo (_("Common symbol size file\n\n"));
5006 header_printed
= TRUE
;
5009 name
= demangle (h
->root
.string
);
5011 len
= strlen (name
);
5026 if (size
<= 0xffffffff)
5027 sprintf (buf
, "%lx", (unsigned long) size
);
5029 sprintf_vma (buf
, size
);
5039 minfo ("%B\n", section
->owner
);
5045 /* Run through the input files and ensure that every input section has
5046 somewhere to go. If one is found without a destination then create
5047 an input request and place it into the statement tree. */
5050 lang_place_orphans (void)
5052 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5056 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5058 if (s
->output_section
== NULL
)
5060 /* This section of the file is not attached, root
5061 around for a sensible place for it to go. */
5063 if (file
->just_syms_flag
)
5064 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5065 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5066 s
->output_section
= bfd_abs_section_ptr
;
5067 else if (strcmp (s
->name
, "COMMON") == 0)
5069 /* This is a lonely common section which must have
5070 come from an archive. We attach to the section
5071 with the wildcard. */
5072 if (! link_info
.relocatable
5073 || command_line
.force_common_definition
)
5075 if (default_common_section
== NULL
)
5077 default_common_section
=
5078 lang_output_section_statement_lookup (".bss");
5081 lang_add_section (&default_common_section
->children
, s
,
5082 default_common_section
);
5085 else if (ldemul_place_orphan (s
))
5089 lang_output_section_statement_type
*os
;
5091 os
= lang_output_section_statement_lookup (s
->name
);
5092 lang_add_section (&os
->children
, s
, os
);
5100 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5102 flagword
*ptr_flags
;
5104 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5110 *ptr_flags
|= SEC_ALLOC
;
5114 *ptr_flags
|= SEC_READONLY
;
5118 *ptr_flags
|= SEC_DATA
;
5122 *ptr_flags
|= SEC_CODE
;
5127 *ptr_flags
|= SEC_LOAD
;
5131 einfo (_("%P%F: invalid syntax in flags\n"));
5138 /* Call a function on each input file. This function will be called
5139 on an archive, but not on the elements. */
5142 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5144 lang_input_statement_type
*f
;
5146 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5148 f
= (lang_input_statement_type
*) f
->next_real_file
)
5152 /* Call a function on each file. The function will be called on all
5153 the elements of an archive which are included in the link, but will
5154 not be called on the archive file itself. */
5157 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5159 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5166 ldlang_add_file (lang_input_statement_type
*entry
)
5170 lang_statement_append (&file_chain
,
5171 (lang_statement_union_type
*) entry
,
5174 /* The BFD linker needs to have a list of all input BFDs involved in
5176 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5177 ASSERT (entry
->the_bfd
!= output_bfd
);
5178 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
5180 *pp
= entry
->the_bfd
;
5181 entry
->the_bfd
->usrdata
= entry
;
5182 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5184 /* Look through the sections and check for any which should not be
5185 included in the link. We need to do this now, so that we can
5186 notice when the backend linker tries to report multiple
5187 definition errors for symbols which are in sections we aren't
5188 going to link. FIXME: It might be better to entirely ignore
5189 symbols which are defined in sections which are going to be
5190 discarded. This would require modifying the backend linker for
5191 each backend which might set the SEC_LINK_ONCE flag. If we do
5192 this, we should probably handle SEC_EXCLUDE in the same way. */
5194 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5198 lang_add_output (const char *name
, int from_script
)
5200 /* Make -o on command line override OUTPUT in script. */
5201 if (!had_output_filename
|| !from_script
)
5203 output_filename
= name
;
5204 had_output_filename
= TRUE
;
5208 static lang_output_section_statement_type
*current_section
;
5219 for (l
= 0; l
< 32; l
++)
5221 if (i
>= (unsigned int) x
)
5229 lang_output_section_statement_type
*
5230 lang_enter_output_section_statement (const char *output_section_statement_name
,
5231 etree_type
*address_exp
,
5232 enum section_type sectype
,
5234 etree_type
*subalign
,
5238 lang_output_section_statement_type
*os
;
5240 os
= lang_output_section_statement_lookup_1 (output_section_statement_name
,
5242 current_section
= os
;
5244 /* Make next things chain into subchain of this. */
5246 if (os
->addr_tree
== NULL
)
5248 os
->addr_tree
= address_exp
;
5250 os
->sectype
= sectype
;
5251 if (sectype
!= noload_section
)
5252 os
->flags
= SEC_NO_FLAGS
;
5254 os
->flags
= SEC_NEVER_LOAD
;
5255 os
->block_value
= 1;
5256 stat_ptr
= &os
->children
;
5258 os
->subsection_alignment
=
5259 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5260 os
->section_alignment
=
5261 topower (exp_get_value_int (align
, -1, "section alignment"));
5263 os
->load_base
= ebase
;
5270 lang_output_statement_type
*new;
5272 new = new_stat (lang_output_statement
, stat_ptr
);
5273 new->name
= output_filename
;
5276 /* Reset the current counters in the regions. */
5279 lang_reset_memory_regions (void)
5281 lang_memory_region_type
*p
= lang_memory_region_list
;
5283 lang_output_section_statement_type
*os
;
5285 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5287 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
5288 p
->current
= p
->origin
;
5291 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5294 os
->processed
= FALSE
;
5296 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5298 /* Save the last size for possible use by bfd_relax_section. */
5299 o
->rawsize
= o
->size
;
5304 /* Worker for lang_gc_sections_1. */
5307 gc_section_callback (lang_wild_statement_type
*ptr
,
5308 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5310 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5311 void *data ATTRIBUTE_UNUSED
)
5313 /* If the wild pattern was marked KEEP, the member sections
5314 should be as well. */
5315 if (ptr
->keep_sections
)
5316 section
->flags
|= SEC_KEEP
;
5319 /* Iterate over sections marking them against GC. */
5322 lang_gc_sections_1 (lang_statement_union_type
*s
)
5324 for (; s
!= NULL
; s
= s
->header
.next
)
5326 switch (s
->header
.type
)
5328 case lang_wild_statement_enum
:
5329 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5331 case lang_constructors_statement_enum
:
5332 lang_gc_sections_1 (constructor_list
.head
);
5334 case lang_output_section_statement_enum
:
5335 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5337 case lang_group_statement_enum
:
5338 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5347 lang_gc_sections (void)
5349 struct bfd_link_hash_entry
*h
;
5350 ldlang_undef_chain_list_type
*ulist
;
5352 /* Keep all sections so marked in the link script. */
5354 lang_gc_sections_1 (statement_list
.head
);
5356 /* Keep all sections containing symbols undefined on the command-line,
5357 and the section containing the entry symbol. */
5359 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
5361 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
5362 FALSE
, FALSE
, FALSE
);
5365 && (h
->type
== bfd_link_hash_defined
5366 || h
->type
== bfd_link_hash_defweak
)
5367 && ! bfd_is_abs_section (h
->u
.def
.section
))
5369 h
->u
.def
.section
->flags
|= SEC_KEEP
;
5373 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5374 the special case of debug info. (See bfd/stabs.c)
5375 Twiddle the flag here, to simplify later linker code. */
5376 if (link_info
.relocatable
)
5378 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5381 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5382 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5383 sec
->flags
&= ~SEC_EXCLUDE
;
5387 if (link_info
.gc_sections
)
5388 bfd_gc_sections (output_bfd
, &link_info
);
5394 current_target
= default_target
;
5396 /* Open the output file. */
5397 lang_for_each_statement (ldlang_open_output
);
5400 ldemul_create_output_section_statements ();
5402 /* Add to the hash table all undefineds on the command line. */
5403 lang_place_undefineds ();
5405 if (!bfd_section_already_linked_table_init ())
5406 einfo (_("%P%F: Failed to create hash table\n"));
5408 /* Create a bfd for each input file. */
5409 current_target
= default_target
;
5410 open_input_bfds (statement_list
.head
, FALSE
);
5412 link_info
.gc_sym_list
= &entry_symbol
;
5413 if (entry_symbol
.name
== NULL
)
5414 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
5416 ldemul_after_open ();
5418 bfd_section_already_linked_table_free ();
5420 /* Make sure that we're not mixing architectures. We call this
5421 after all the input files have been opened, but before we do any
5422 other processing, so that any operations merge_private_bfd_data
5423 does on the output file will be known during the rest of the
5427 /* Handle .exports instead of a version script if we're told to do so. */
5428 if (command_line
.version_exports_section
)
5429 lang_do_version_exports_section ();
5431 /* Build all sets based on the information gathered from the input
5433 ldctor_build_sets ();
5435 /* Remove unreferenced sections if asked to. */
5436 lang_gc_sections ();
5438 /* Size up the common data. */
5441 /* Update wild statements. */
5442 update_wild_statements (statement_list
.head
);
5444 /* Run through the contours of the script and attach input sections
5445 to the correct output sections. */
5446 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
5448 /* Find any sections not attached explicitly and handle them. */
5449 lang_place_orphans ();
5451 if (! link_info
.relocatable
)
5455 /* Merge SEC_MERGE sections. This has to be done after GC of
5456 sections, so that GCed sections are not merged, but before
5457 assigning dynamic symbols, since removing whole input sections
5459 bfd_merge_sections (output_bfd
, &link_info
);
5461 /* Look for a text section and set the readonly attribute in it. */
5462 found
= bfd_get_section_by_name (output_bfd
, ".text");
5466 if (config
.text_read_only
)
5467 found
->flags
|= SEC_READONLY
;
5469 found
->flags
&= ~SEC_READONLY
;
5473 /* Do anything special before sizing sections. This is where ELF
5474 and other back-ends size dynamic sections. */
5475 ldemul_before_allocation ();
5477 /* We must record the program headers before we try to fix the
5478 section positions, since they will affect SIZEOF_HEADERS. */
5479 lang_record_phdrs ();
5481 /* Size up the sections. */
5482 lang_size_sections (NULL
, !command_line
.relax
);
5484 /* Now run around and relax if we can. */
5485 if (command_line
.relax
)
5487 /* Keep relaxing until bfd_relax_section gives up. */
5488 bfd_boolean relax_again
;
5492 relax_again
= FALSE
;
5494 /* Note: pe-dll.c does something like this also. If you find
5495 you need to change this code, you probably need to change
5496 pe-dll.c also. DJ */
5498 /* Do all the assignments with our current guesses as to
5500 lang_do_assignments ();
5502 /* We must do this after lang_do_assignments, because it uses
5504 lang_reset_memory_regions ();
5506 /* Perform another relax pass - this time we know where the
5507 globals are, so can make a better guess. */
5508 lang_size_sections (&relax_again
, FALSE
);
5510 /* If the normal relax is done and the relax finalize pass
5511 is not performed yet, we perform another relax pass. */
5512 if (!relax_again
&& link_info
.need_relax_finalize
)
5514 link_info
.need_relax_finalize
= FALSE
;
5518 while (relax_again
);
5520 /* Final extra sizing to report errors. */
5521 lang_do_assignments ();
5522 lang_reset_memory_regions ();
5523 lang_size_sections (NULL
, TRUE
);
5526 /* See if anything special should be done now we know how big
5528 ldemul_after_allocation ();
5530 /* Fix any .startof. or .sizeof. symbols. */
5531 lang_set_startof ();
5533 /* Do all the assignments, now that we know the final resting places
5534 of all the symbols. */
5536 lang_do_assignments ();
5538 /* Make sure that the section addresses make sense. */
5539 if (! link_info
.relocatable
5540 && command_line
.check_section_addresses
)
5541 lang_check_section_addresses ();
5548 /* EXPORTED TO YACC */
5551 lang_add_wild (struct wildcard_spec
*filespec
,
5552 struct wildcard_list
*section_list
,
5553 bfd_boolean keep_sections
)
5555 struct wildcard_list
*curr
, *next
;
5556 lang_wild_statement_type
*new;
5558 /* Reverse the list as the parser puts it back to front. */
5559 for (curr
= section_list
, section_list
= NULL
;
5561 section_list
= curr
, curr
= next
)
5563 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
5564 placed_commons
= TRUE
;
5567 curr
->next
= section_list
;
5570 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
5572 if (strcmp (filespec
->name
, "*") == 0)
5573 filespec
->name
= NULL
;
5574 else if (! wildcardp (filespec
->name
))
5575 lang_has_input_file
= TRUE
;
5578 new = new_stat (lang_wild_statement
, stat_ptr
);
5579 new->filename
= NULL
;
5580 new->filenames_sorted
= FALSE
;
5581 if (filespec
!= NULL
)
5583 new->filename
= filespec
->name
;
5584 new->filenames_sorted
= filespec
->sorted
== by_name
;
5586 new->section_list
= section_list
;
5587 new->keep_sections
= keep_sections
;
5588 lang_list_init (&new->children
);
5589 analyze_walk_wild_section_handler (new);
5593 lang_section_start (const char *name
, etree_type
*address
,
5594 const segment_type
*segment
)
5596 lang_address_statement_type
*ad
;
5598 ad
= new_stat (lang_address_statement
, stat_ptr
);
5599 ad
->section_name
= name
;
5600 ad
->address
= address
;
5601 ad
->segment
= segment
;
5604 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5605 because of a -e argument on the command line, or zero if this is
5606 called by ENTRY in a linker script. Command line arguments take
5610 lang_add_entry (const char *name
, bfd_boolean cmdline
)
5612 if (entry_symbol
.name
== NULL
5614 || ! entry_from_cmdline
)
5616 entry_symbol
.name
= name
;
5617 entry_from_cmdline
= cmdline
;
5621 /* Set the default start symbol to NAME. .em files should use this,
5622 not lang_add_entry, to override the use of "start" if neither the
5623 linker script nor the command line specifies an entry point. NAME
5624 must be permanently allocated. */
5626 lang_default_entry (const char *name
)
5628 entry_symbol_default
= name
;
5632 lang_add_target (const char *name
)
5634 lang_target_statement_type
*new;
5636 new = new_stat (lang_target_statement
, stat_ptr
);
5641 lang_add_map (const char *name
)
5648 map_option_f
= TRUE
;
5656 lang_add_fill (fill_type
*fill
)
5658 lang_fill_statement_type
*new;
5660 new = new_stat (lang_fill_statement
, stat_ptr
);
5665 lang_add_data (int type
, union etree_union
*exp
)
5667 lang_data_statement_type
*new;
5669 new = new_stat (lang_data_statement
, stat_ptr
);
5674 /* Create a new reloc statement. RELOC is the BFD relocation type to
5675 generate. HOWTO is the corresponding howto structure (we could
5676 look this up, but the caller has already done so). SECTION is the
5677 section to generate a reloc against, or NAME is the name of the
5678 symbol to generate a reloc against. Exactly one of SECTION and
5679 NAME must be NULL. ADDEND is an expression for the addend. */
5682 lang_add_reloc (bfd_reloc_code_real_type reloc
,
5683 reloc_howto_type
*howto
,
5686 union etree_union
*addend
)
5688 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
5692 p
->section
= section
;
5694 p
->addend_exp
= addend
;
5696 p
->addend_value
= 0;
5697 p
->output_section
= NULL
;
5698 p
->output_offset
= 0;
5701 lang_assignment_statement_type
*
5702 lang_add_assignment (etree_type
*exp
)
5704 lang_assignment_statement_type
*new;
5706 new = new_stat (lang_assignment_statement
, stat_ptr
);
5712 lang_add_attribute (enum statement_enum attribute
)
5714 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
5718 lang_startup (const char *name
)
5720 if (startup_file
!= NULL
)
5722 einfo (_("%P%F: multiple STARTUP files\n"));
5724 first_file
->filename
= name
;
5725 first_file
->local_sym_name
= name
;
5726 first_file
->real
= TRUE
;
5728 startup_file
= name
;
5732 lang_float (bfd_boolean maybe
)
5734 lang_float_flag
= maybe
;
5738 /* Work out the load- and run-time regions from a script statement, and
5739 store them in *LMA_REGION and *REGION respectively.
5741 MEMSPEC is the name of the run-time region, or the value of
5742 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5743 LMA_MEMSPEC is the name of the load-time region, or null if the
5744 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5745 had an explicit load address.
5747 It is an error to specify both a load region and a load address. */
5750 lang_get_regions (lang_memory_region_type
**region
,
5751 lang_memory_region_type
**lma_region
,
5752 const char *memspec
,
5753 const char *lma_memspec
,
5754 bfd_boolean have_lma
,
5755 bfd_boolean have_vma
)
5757 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
5759 /* If no runtime region or VMA has been specified, but the load region
5760 has been specified, then use the load region for the runtime region
5762 if (lma_memspec
!= NULL
5764 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
5765 *region
= *lma_region
;
5767 *region
= lang_memory_region_lookup (memspec
, FALSE
);
5769 if (have_lma
&& lma_memspec
!= 0)
5770 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5774 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
5775 lang_output_section_phdr_list
*phdrs
,
5776 const char *lma_memspec
)
5778 lang_get_regions (¤t_section
->region
,
5779 ¤t_section
->lma_region
,
5780 memspec
, lma_memspec
,
5781 current_section
->load_base
!= NULL
,
5782 current_section
->addr_tree
!= NULL
);
5783 current_section
->fill
= fill
;
5784 current_section
->phdrs
= phdrs
;
5785 stat_ptr
= &statement_list
;
5788 /* Create an absolute symbol with the given name with the value of the
5789 address of first byte of the section named.
5791 If the symbol already exists, then do nothing. */
5794 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
5796 struct bfd_link_hash_entry
*h
;
5798 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5800 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5802 if (h
->type
== bfd_link_hash_new
5803 || h
->type
== bfd_link_hash_undefined
)
5807 h
->type
= bfd_link_hash_defined
;
5809 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5813 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
5815 h
->u
.def
.section
= bfd_abs_section_ptr
;
5819 /* Create an absolute symbol with the given name with the value of the
5820 address of the first byte after the end of the section named.
5822 If the symbol already exists, then do nothing. */
5825 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
5827 struct bfd_link_hash_entry
*h
;
5829 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5831 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5833 if (h
->type
== bfd_link_hash_new
5834 || h
->type
== bfd_link_hash_undefined
)
5838 h
->type
= bfd_link_hash_defined
;
5840 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5844 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
5845 + TO_ADDR (sec
->size
));
5847 h
->u
.def
.section
= bfd_abs_section_ptr
;
5852 lang_statement_append (lang_statement_list_type
*list
,
5853 lang_statement_union_type
*element
,
5854 lang_statement_union_type
**field
)
5856 *(list
->tail
) = element
;
5860 /* Set the output format type. -oformat overrides scripts. */
5863 lang_add_output_format (const char *format
,
5868 if (output_target
== NULL
|| !from_script
)
5870 if (command_line
.endian
== ENDIAN_BIG
5873 else if (command_line
.endian
== ENDIAN_LITTLE
5877 output_target
= format
;
5881 /* Enter a group. This creates a new lang_group_statement, and sets
5882 stat_ptr to build new statements within the group. */
5885 lang_enter_group (void)
5887 lang_group_statement_type
*g
;
5889 g
= new_stat (lang_group_statement
, stat_ptr
);
5890 lang_list_init (&g
->children
);
5891 stat_ptr
= &g
->children
;
5894 /* Leave a group. This just resets stat_ptr to start writing to the
5895 regular list of statements again. Note that this will not work if
5896 groups can occur inside anything else which can adjust stat_ptr,
5897 but currently they can't. */
5900 lang_leave_group (void)
5902 stat_ptr
= &statement_list
;
5905 /* Add a new program header. This is called for each entry in a PHDRS
5906 command in a linker script. */
5909 lang_new_phdr (const char *name
,
5911 bfd_boolean filehdr
,
5916 struct lang_phdr
*n
, **pp
;
5918 n
= stat_alloc (sizeof (struct lang_phdr
));
5921 n
->type
= exp_get_value_int (type
, 0, "program header type");
5922 n
->filehdr
= filehdr
;
5927 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5932 /* Record the program header information in the output BFD. FIXME: We
5933 should not be calling an ELF specific function here. */
5936 lang_record_phdrs (void)
5940 lang_output_section_phdr_list
*last
;
5941 struct lang_phdr
*l
;
5942 lang_output_section_statement_type
*os
;
5945 secs
= xmalloc (alc
* sizeof (asection
*));
5947 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
5954 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5958 lang_output_section_phdr_list
*pl
;
5960 if (os
->constraint
== -1)
5968 if (os
->sectype
== noload_section
5969 || os
->bfd_section
== NULL
5970 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
5975 if (os
->bfd_section
== NULL
)
5978 for (; pl
!= NULL
; pl
= pl
->next
)
5980 if (strcmp (pl
->name
, l
->name
) == 0)
5985 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
5987 secs
[c
] = os
->bfd_section
;
5994 if (l
->flags
== NULL
)
5997 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6002 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6004 if (! bfd_record_phdr (output_bfd
, l
->type
,
6005 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6006 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6007 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6012 /* Make sure all the phdr assignments succeeded. */
6013 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6017 lang_output_section_phdr_list
*pl
;
6019 if (os
->constraint
== -1
6020 || os
->bfd_section
== NULL
)
6023 for (pl
= os
->phdrs
;
6026 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6027 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6028 os
->name
, pl
->name
);
6032 /* Record a list of sections which may not be cross referenced. */
6035 lang_add_nocrossref (lang_nocrossref_type
*l
)
6037 struct lang_nocrossrefs
*n
;
6039 n
= xmalloc (sizeof *n
);
6040 n
->next
= nocrossref_list
;
6042 nocrossref_list
= n
;
6044 /* Set notice_all so that we get informed about all symbols. */
6045 link_info
.notice_all
= TRUE
;
6048 /* Overlay handling. We handle overlays with some static variables. */
6050 /* The overlay virtual address. */
6051 static etree_type
*overlay_vma
;
6052 /* And subsection alignment. */
6053 static etree_type
*overlay_subalign
;
6055 /* An expression for the maximum section size seen so far. */
6056 static etree_type
*overlay_max
;
6058 /* A list of all the sections in this overlay. */
6060 struct overlay_list
{
6061 struct overlay_list
*next
;
6062 lang_output_section_statement_type
*os
;
6065 static struct overlay_list
*overlay_list
;
6067 /* Start handling an overlay. */
6070 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6072 /* The grammar should prevent nested overlays from occurring. */
6073 ASSERT (overlay_vma
== NULL
6074 && overlay_subalign
== NULL
6075 && overlay_max
== NULL
);
6077 overlay_vma
= vma_expr
;
6078 overlay_subalign
= subalign
;
6081 /* Start a section in an overlay. We handle this by calling
6082 lang_enter_output_section_statement with the correct VMA.
6083 lang_leave_overlay sets up the LMA and memory regions. */
6086 lang_enter_overlay_section (const char *name
)
6088 struct overlay_list
*n
;
6091 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
6092 0, overlay_subalign
, 0, 0);
6094 /* If this is the first section, then base the VMA of future
6095 sections on this one. This will work correctly even if `.' is
6096 used in the addresses. */
6097 if (overlay_list
== NULL
)
6098 overlay_vma
= exp_nameop (ADDR
, name
);
6100 /* Remember the section. */
6101 n
= xmalloc (sizeof *n
);
6102 n
->os
= current_section
;
6103 n
->next
= overlay_list
;
6106 size
= exp_nameop (SIZEOF
, name
);
6108 /* Arrange to work out the maximum section end address. */
6109 if (overlay_max
== NULL
)
6112 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6115 /* Finish a section in an overlay. There isn't any special to do
6119 lang_leave_overlay_section (fill_type
*fill
,
6120 lang_output_section_phdr_list
*phdrs
)
6127 name
= current_section
->name
;
6129 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6130 region and that no load-time region has been specified. It doesn't
6131 really matter what we say here, since lang_leave_overlay will
6133 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6135 /* Define the magic symbols. */
6137 clean
= xmalloc (strlen (name
) + 1);
6139 for (s1
= name
; *s1
!= '\0'; s1
++)
6140 if (ISALNUM (*s1
) || *s1
== '_')
6144 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6145 sprintf (buf
, "__load_start_%s", clean
);
6146 lang_add_assignment (exp_assop ('=', buf
,
6147 exp_nameop (LOADADDR
, name
)));
6149 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6150 sprintf (buf
, "__load_stop_%s", clean
);
6151 lang_add_assignment (exp_assop ('=', buf
,
6153 exp_nameop (LOADADDR
, name
),
6154 exp_nameop (SIZEOF
, name
))));
6159 /* Finish an overlay. If there are any overlay wide settings, this
6160 looks through all the sections in the overlay and sets them. */
6163 lang_leave_overlay (etree_type
*lma_expr
,
6166 const char *memspec
,
6167 lang_output_section_phdr_list
*phdrs
,
6168 const char *lma_memspec
)
6170 lang_memory_region_type
*region
;
6171 lang_memory_region_type
*lma_region
;
6172 struct overlay_list
*l
;
6173 lang_nocrossref_type
*nocrossref
;
6175 lang_get_regions (®ion
, &lma_region
,
6176 memspec
, lma_memspec
,
6177 lma_expr
!= NULL
, FALSE
);
6181 /* After setting the size of the last section, set '.' to end of the
6183 if (overlay_list
!= NULL
)
6184 overlay_list
->os
->update_dot_tree
6185 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6190 struct overlay_list
*next
;
6192 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6195 l
->os
->region
= region
;
6196 l
->os
->lma_region
= lma_region
;
6198 /* The first section has the load address specified in the
6199 OVERLAY statement. The rest are worked out from that.
6200 The base address is not needed (and should be null) if
6201 an LMA region was specified. */
6203 l
->os
->load_base
= lma_expr
;
6204 else if (lma_region
== 0)
6205 l
->os
->load_base
= exp_binop ('+',
6206 exp_nameop (LOADADDR
, l
->next
->os
->name
),
6207 exp_nameop (SIZEOF
, l
->next
->os
->name
));
6209 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6210 l
->os
->phdrs
= phdrs
;
6214 lang_nocrossref_type
*nc
;
6216 nc
= xmalloc (sizeof *nc
);
6217 nc
->name
= l
->os
->name
;
6218 nc
->next
= nocrossref
;
6227 if (nocrossref
!= NULL
)
6228 lang_add_nocrossref (nocrossref
);
6231 overlay_list
= NULL
;
6235 /* Version handling. This is only useful for ELF. */
6237 /* This global variable holds the version tree that we build. */
6239 struct bfd_elf_version_tree
*lang_elf_version_info
;
6241 /* If PREV is NULL, return first version pattern matching particular symbol.
6242 If PREV is non-NULL, return first version pattern matching particular
6243 symbol after PREV (previously returned by lang_vers_match). */
6245 static struct bfd_elf_version_expr
*
6246 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6247 struct bfd_elf_version_expr
*prev
,
6250 const char *cxx_sym
= sym
;
6251 const char *java_sym
= sym
;
6252 struct bfd_elf_version_expr
*expr
= NULL
;
6254 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6256 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6260 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6262 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6267 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6269 struct bfd_elf_version_expr e
;
6271 switch (prev
? prev
->mask
: 0)
6274 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6277 expr
= htab_find (head
->htab
, &e
);
6278 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
6279 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
6285 case BFD_ELF_VERSION_C_TYPE
:
6286 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6289 expr
= htab_find (head
->htab
, &e
);
6290 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
6291 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6297 case BFD_ELF_VERSION_CXX_TYPE
:
6298 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6300 e
.symbol
= java_sym
;
6301 expr
= htab_find (head
->htab
, &e
);
6302 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
6303 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6314 /* Finally, try the wildcards. */
6315 if (prev
== NULL
|| prev
->symbol
)
6316 expr
= head
->remaining
;
6319 for (; expr
; expr
= expr
->next
)
6326 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
6329 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6331 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6335 if (fnmatch (expr
->pattern
, s
, 0) == 0)
6341 free ((char *) cxx_sym
);
6342 if (java_sym
!= sym
)
6343 free ((char *) java_sym
);
6347 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6348 return a string pointing to the symbol name. */
6351 realsymbol (const char *pattern
)
6354 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
6355 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
6357 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
6359 /* It is a glob pattern only if there is no preceding
6361 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
6369 /* Remove the preceding backslash. */
6376 backslash
= *p
== '\\';
6391 /* This is called for each variable name or match expression. NEW is
6392 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6393 pattern to be matched against symbol names. */
6395 struct bfd_elf_version_expr
*
6396 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
6399 bfd_boolean literal_p
)
6401 struct bfd_elf_version_expr
*ret
;
6403 ret
= xmalloc (sizeof *ret
);
6405 ret
->pattern
= literal_p
? NULL
: new;
6408 ret
->symbol
= literal_p
? new : realsymbol (new);
6410 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
6411 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6412 else if (strcasecmp (lang
, "C++") == 0)
6413 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
6414 else if (strcasecmp (lang
, "Java") == 0)
6415 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
6418 einfo (_("%X%P: unknown language `%s' in version information\n"),
6420 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6423 return ldemul_new_vers_pattern (ret
);
6426 /* This is called for each set of variable names and match
6429 struct bfd_elf_version_tree
*
6430 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
6431 struct bfd_elf_version_expr
*locals
)
6433 struct bfd_elf_version_tree
*ret
;
6435 ret
= xcalloc (1, sizeof *ret
);
6436 ret
->globals
.list
= globals
;
6437 ret
->locals
.list
= locals
;
6438 ret
->match
= lang_vers_match
;
6439 ret
->name_indx
= (unsigned int) -1;
6443 /* This static variable keeps track of version indices. */
6445 static int version_index
;
6448 version_expr_head_hash (const void *p
)
6450 const struct bfd_elf_version_expr
*e
= p
;
6452 return htab_hash_string (e
->symbol
);
6456 version_expr_head_eq (const void *p1
, const void *p2
)
6458 const struct bfd_elf_version_expr
*e1
= p1
;
6459 const struct bfd_elf_version_expr
*e2
= p2
;
6461 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
6465 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
6468 struct bfd_elf_version_expr
*e
, *next
;
6469 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
6471 for (e
= head
->list
; e
; e
= e
->next
)
6475 head
->mask
|= e
->mask
;
6480 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
6481 version_expr_head_eq
, NULL
);
6482 list_loc
= &head
->list
;
6483 remaining_loc
= &head
->remaining
;
6484 for (e
= head
->list
; e
; e
= next
)
6490 remaining_loc
= &e
->next
;
6494 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
6498 struct bfd_elf_version_expr
*e1
, *last
;
6504 if (e1
->mask
== e
->mask
)
6512 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
6516 /* This is a duplicate. */
6517 /* FIXME: Memory leak. Sometimes pattern is not
6518 xmalloced alone, but in larger chunk of memory. */
6519 /* free (e->symbol); */
6524 e
->next
= last
->next
;
6532 list_loc
= &e
->next
;
6536 *remaining_loc
= NULL
;
6537 *list_loc
= head
->remaining
;
6540 head
->remaining
= head
->list
;
6543 /* This is called when we know the name and dependencies of the
6547 lang_register_vers_node (const char *name
,
6548 struct bfd_elf_version_tree
*version
,
6549 struct bfd_elf_version_deps
*deps
)
6551 struct bfd_elf_version_tree
*t
, **pp
;
6552 struct bfd_elf_version_expr
*e1
;
6557 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
6558 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
6560 einfo (_("%X%P: anonymous version tag cannot be combined"
6561 " with other version tags\n"));
6566 /* Make sure this node has a unique name. */
6567 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6568 if (strcmp (t
->name
, name
) == 0)
6569 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
6571 lang_finalize_version_expr_head (&version
->globals
);
6572 lang_finalize_version_expr_head (&version
->locals
);
6574 /* Check the global and local match names, and make sure there
6575 aren't any duplicates. */
6577 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
6579 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6581 struct bfd_elf_version_expr
*e2
;
6583 if (t
->locals
.htab
&& e1
->symbol
)
6585 e2
= htab_find (t
->locals
.htab
, e1
);
6586 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6588 if (e1
->mask
== e2
->mask
)
6589 einfo (_("%X%P: duplicate expression `%s'"
6590 " in version information\n"), e1
->symbol
);
6594 else if (!e1
->symbol
)
6595 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6596 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6597 && e1
->mask
== e2
->mask
)
6598 einfo (_("%X%P: duplicate expression `%s'"
6599 " in version information\n"), e1
->pattern
);
6603 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
6605 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6607 struct bfd_elf_version_expr
*e2
;
6609 if (t
->globals
.htab
&& e1
->symbol
)
6611 e2
= htab_find (t
->globals
.htab
, e1
);
6612 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6614 if (e1
->mask
== e2
->mask
)
6615 einfo (_("%X%P: duplicate expression `%s'"
6616 " in version information\n"),
6621 else if (!e1
->symbol
)
6622 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6623 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6624 && e1
->mask
== e2
->mask
)
6625 einfo (_("%X%P: duplicate expression `%s'"
6626 " in version information\n"), e1
->pattern
);
6630 version
->deps
= deps
;
6631 version
->name
= name
;
6632 if (name
[0] != '\0')
6635 version
->vernum
= version_index
;
6638 version
->vernum
= 0;
6640 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6645 /* This is called when we see a version dependency. */
6647 struct bfd_elf_version_deps
*
6648 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
6650 struct bfd_elf_version_deps
*ret
;
6651 struct bfd_elf_version_tree
*t
;
6653 ret
= xmalloc (sizeof *ret
);
6656 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6658 if (strcmp (t
->name
, name
) == 0)
6660 ret
->version_needed
= t
;
6665 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
6671 lang_do_version_exports_section (void)
6673 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
6675 LANG_FOR_EACH_INPUT_STATEMENT (is
)
6677 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
6685 contents
= xmalloc (len
);
6686 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
6687 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
6690 while (p
< contents
+ len
)
6692 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
6693 p
= strchr (p
, '\0') + 1;
6696 /* Do not free the contents, as we used them creating the regex. */
6698 /* Do not include this section in the link. */
6699 sec
->flags
|= SEC_EXCLUDE
;
6702 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
6703 lang_register_vers_node (command_line
.version_exports_section
,
6704 lang_new_vers_node (greg
, lreg
), NULL
);
6708 lang_add_unique (const char *name
)
6710 struct unique_sections
*ent
;
6712 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
6713 if (strcmp (ent
->name
, name
) == 0)
6716 ent
= xmalloc (sizeof *ent
);
6717 ent
->name
= xstrdup (name
);
6718 ent
->next
= unique_section_list
;
6719 unique_section_list
= ent
;