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 bfd_hash_table lang_definedness_table
;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type
*);
69 static void init_map_userdata (bfd
*, asection
*, void *);
70 static lang_input_statement_type
*lookup_name (const char *);
71 static bfd_boolean
load_symbols (lang_input_statement_type
*,
72 lang_statement_list_type
*);
73 static struct bfd_hash_entry
*lang_definedness_newfunc
74 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
75 static void insert_undefined (const char *);
76 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
77 static void print_statement (lang_statement_union_type
*,
78 lang_output_section_statement_type
*);
79 static void print_statement_list (lang_statement_union_type
*,
80 lang_output_section_statement_type
*);
81 static void print_statements (void);
82 static void print_input_section (asection
*);
83 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 /* Exported variables. */
88 lang_output_section_statement_type
*abs_output_section
;
89 lang_statement_list_type lang_output_section_statement
;
90 lang_statement_list_type
*stat_ptr
= &statement_list
;
91 lang_statement_list_type file_chain
= { NULL
, NULL
};
92 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
93 static const char *entry_symbol_default
= "start";
94 const char *entry_section
= ".text";
95 bfd_boolean entry_from_cmdline
;
96 bfd_boolean lang_has_input_file
= FALSE
;
97 bfd_boolean had_output_filename
= FALSE
;
98 bfd_boolean lang_float_flag
= FALSE
;
99 bfd_boolean delete_output_file_on_failure
= FALSE
;
100 struct lang_phdr
*lang_phdr_list
;
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 out_section_hash_entry
874 struct bfd_hash_entry root
;
875 lang_statement_union_type s
;
878 /* The hash table. */
880 static struct bfd_hash_table output_section_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_section_statement_newfunc (struct bfd_hash_entry
*entry
,
887 struct bfd_hash_table
*table
,
890 lang_output_section_statement_type
**nextp
;
891 struct out_section_hash_entry
*ret
;
895 entry
= bfd_hash_allocate (table
, sizeof (*ret
));
900 entry
= bfd_hash_newfunc (entry
, table
, string
);
904 ret
= (struct out_section_hash_entry
*) entry
;
905 memset (&ret
->s
, 0, sizeof (ret
->s
));
906 ret
->s
.header
.type
= lang_output_section_statement_enum
;
907 ret
->s
.output_section_statement
.subsection_alignment
= -1;
908 ret
->s
.output_section_statement
.section_alignment
= -1;
909 ret
->s
.output_section_statement
.block_value
= 1;
910 lang_list_init (&ret
->s
.output_section_statement
.children
);
911 lang_statement_append (stat_ptr
, &ret
->s
, &ret
->s
.header
.next
);
913 /* For every output section statement added to the list, except the
914 first one, lang_output_section_statement.tail points to the "next"
915 field of the last element of the list. */
916 if (lang_output_section_statement
.head
!= NULL
)
917 ret
->s
.output_section_statement
.prev
918 = ((lang_output_section_statement_type
*)
919 ((char *) lang_output_section_statement
.tail
920 - offsetof (lang_output_section_statement_type
, next
)));
922 /* GCC's strict aliasing rules prevent us from just casting the
923 address, so we store the pointer in a variable and cast that
925 nextp
= &ret
->s
.output_section_statement
.next
;
926 lang_statement_append (&lang_output_section_statement
,
928 (lang_statement_union_type
**) nextp
);
933 output_section_statement_table_init (void)
935 if (!bfd_hash_table_init_n (&output_section_statement_table
,
936 output_section_statement_newfunc
,
937 sizeof (struct out_section_hash_entry
),
939 einfo (_("%P%F: can not create hash table: %E\n"));
943 output_section_statement_table_free (void)
945 bfd_hash_table_free (&output_section_statement_table
);
948 /* Build enough state so that the parser can build its tree. */
953 obstack_begin (&stat_obstack
, 1000);
955 stat_ptr
= &statement_list
;
957 output_section_statement_table_init ();
959 lang_list_init (stat_ptr
);
961 lang_list_init (&input_file_chain
);
962 lang_list_init (&lang_output_section_statement
);
963 lang_list_init (&file_chain
);
964 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
967 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
969 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
971 /* The value "3" is ad-hoc, somewhat related to the expected number of
972 DEFINED expressions in a linker script. For most default linker
973 scripts, there are none. Why a hash table then? Well, it's somewhat
974 simpler to re-use working machinery than using a linked list in terms
975 of code-complexity here in ld, besides the initialization which just
976 looks like other code here. */
977 if (!bfd_hash_table_init_n (&lang_definedness_table
,
978 lang_definedness_newfunc
,
979 sizeof (struct lang_definedness_hash_entry
),
981 einfo (_("%P%F: can not create hash table: %E\n"));
987 output_section_statement_table_free ();
990 /*----------------------------------------------------------------------
991 A region is an area of memory declared with the
992 MEMORY { name:org=exp, len=exp ... }
995 We maintain a list of all the regions here.
997 If no regions are specified in the script, then the default is used
998 which is created when looked up to be the entire data space.
1000 If create is true we are creating a region inside a MEMORY block.
1001 In this case it is probably an error to create a region that has
1002 already been created. If we are not inside a MEMORY block it is
1003 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
1004 and so we issue a warning. */
1006 static lang_memory_region_type
*lang_memory_region_list
;
1007 static lang_memory_region_type
**lang_memory_region_list_tail
1008 = &lang_memory_region_list
;
1010 lang_memory_region_type
*
1011 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
1013 lang_memory_region_type
*p
;
1014 lang_memory_region_type
*new;
1016 /* NAME is NULL for LMA memspecs if no region was specified. */
1020 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1021 if (strcmp (p
->name
, name
) == 0)
1024 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
1029 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
1030 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
1032 new = stat_alloc (sizeof (lang_memory_region_type
));
1034 new->name
= xstrdup (name
);
1037 *lang_memory_region_list_tail
= new;
1038 lang_memory_region_list_tail
= &new->next
;
1042 new->length
= ~(bfd_size_type
) 0;
1044 new->had_full_message
= FALSE
;
1049 static lang_memory_region_type
*
1050 lang_memory_default (asection
*section
)
1052 lang_memory_region_type
*p
;
1054 flagword sec_flags
= section
->flags
;
1056 /* Override SEC_DATA to mean a writable section. */
1057 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
1058 sec_flags
|= SEC_DATA
;
1060 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
1062 if ((p
->flags
& sec_flags
) != 0
1063 && (p
->not_flags
& sec_flags
) == 0)
1068 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
1071 lang_output_section_statement_type
*
1072 lang_output_section_find (const char *const name
)
1074 struct out_section_hash_entry
*entry
;
1077 entry
= ((struct out_section_hash_entry
*)
1078 bfd_hash_lookup (&output_section_statement_table
, name
,
1083 hash
= entry
->root
.hash
;
1086 if (entry
->s
.output_section_statement
.constraint
!= -1)
1087 return &entry
->s
.output_section_statement
;
1088 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1090 while (entry
!= NULL
1091 && entry
->root
.hash
== hash
1092 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1097 static lang_output_section_statement_type
*
1098 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
1100 struct out_section_hash_entry
*entry
;
1101 struct out_section_hash_entry
*last_ent
;
1104 entry
= ((struct out_section_hash_entry
*)
1105 bfd_hash_lookup (&output_section_statement_table
, name
,
1109 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1113 if (entry
->s
.output_section_statement
.name
!= NULL
)
1115 /* We have a section of this name, but it might not have the correct
1117 hash
= entry
->root
.hash
;
1120 if (entry
->s
.output_section_statement
.constraint
!= -1
1122 || (constraint
== entry
->s
.output_section_statement
.constraint
1123 && constraint
!= SPECIAL
)))
1124 return &entry
->s
.output_section_statement
;
1126 entry
= (struct out_section_hash_entry
*) entry
->root
.next
;
1128 while (entry
!= NULL
1129 && entry
->root
.hash
== hash
1130 && strcmp (name
, entry
->s
.output_section_statement
.name
) == 0);
1133 = ((struct out_section_hash_entry
*)
1134 output_section_statement_newfunc (NULL
,
1135 &output_section_statement_table
,
1139 einfo (_("%P%F: failed creating section `%s': %E\n"), name
);
1142 entry
->root
= last_ent
->root
;
1143 last_ent
->root
.next
= &entry
->root
;
1146 entry
->s
.output_section_statement
.name
= name
;
1147 entry
->s
.output_section_statement
.constraint
= constraint
;
1148 return &entry
->s
.output_section_statement
;
1151 lang_output_section_statement_type
*
1152 lang_output_section_statement_lookup (const char *const name
)
1154 return lang_output_section_statement_lookup_1 (name
, 0);
1157 /* A variant of lang_output_section_find used by place_orphan.
1158 Returns the output statement that should precede a new output
1159 statement for SEC. If an exact match is found on certain flags,
1162 lang_output_section_statement_type
*
1163 lang_output_section_find_by_flags (const asection
*sec
,
1164 lang_output_section_statement_type
**exact
,
1165 lang_match_sec_type_func match_type
)
1167 lang_output_section_statement_type
*first
, *look
, *found
;
1170 /* We know the first statement on this list is *ABS*. May as well
1172 first
= &lang_output_section_statement
.head
->output_section_statement
;
1173 first
= first
->next
;
1175 /* First try for an exact match. */
1177 for (look
= first
; look
; look
= look
->next
)
1179 flags
= look
->flags
;
1180 if (look
->bfd_section
!= NULL
)
1182 flags
= look
->bfd_section
->flags
;
1183 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1187 flags
^= sec
->flags
;
1188 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
1189 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1199 if (sec
->flags
& SEC_CODE
)
1201 /* Try for a rw code section. */
1202 for (look
= first
; look
; look
= look
->next
)
1204 flags
= look
->flags
;
1205 if (look
->bfd_section
!= NULL
)
1207 flags
= look
->bfd_section
->flags
;
1208 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1212 flags
^= sec
->flags
;
1213 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1214 | SEC_CODE
| SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1218 else if (sec
->flags
& (SEC_READONLY
| SEC_THREAD_LOCAL
))
1220 /* .rodata can go after .text, .sdata2 after .rodata. */
1221 for (look
= first
; look
; look
= look
->next
)
1223 flags
= look
->flags
;
1224 if (look
->bfd_section
!= NULL
)
1226 flags
= look
->bfd_section
->flags
;
1227 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1231 flags
^= sec
->flags
;
1232 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1234 && !(look
->flags
& (SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1238 else if (sec
->flags
& SEC_SMALL_DATA
)
1240 /* .sdata goes after .data, .sbss after .sdata. */
1241 for (look
= first
; look
; look
= look
->next
)
1243 flags
= look
->flags
;
1244 if (look
->bfd_section
!= NULL
)
1246 flags
= look
->bfd_section
->flags
;
1247 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1251 flags
^= sec
->flags
;
1252 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1253 | SEC_THREAD_LOCAL
))
1254 || ((look
->flags
& SEC_SMALL_DATA
)
1255 && !(sec
->flags
& SEC_HAS_CONTENTS
)))
1259 else if (sec
->flags
& SEC_HAS_CONTENTS
)
1261 /* .data goes after .rodata. */
1262 for (look
= first
; look
; look
= look
->next
)
1264 flags
= look
->flags
;
1265 if (look
->bfd_section
!= NULL
)
1267 flags
= look
->bfd_section
->flags
;
1268 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1272 flags
^= sec
->flags
;
1273 if (!(flags
& (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
1274 | SEC_SMALL_DATA
| SEC_THREAD_LOCAL
)))
1280 /* .bss goes last. */
1281 for (look
= first
; look
; look
= look
->next
)
1283 flags
= look
->flags
;
1284 if (look
->bfd_section
!= NULL
)
1286 flags
= look
->bfd_section
->flags
;
1287 if (match_type
&& !match_type (output_bfd
, look
->bfd_section
,
1291 flags
^= sec
->flags
;
1292 if (!(flags
& SEC_ALLOC
))
1297 if (found
|| !match_type
)
1300 return lang_output_section_find_by_flags (sec
, NULL
, NULL
);
1303 /* Find the last output section before given output statement.
1304 Used by place_orphan. */
1307 output_prev_sec_find (lang_output_section_statement_type
*os
)
1309 lang_output_section_statement_type
*lookup
;
1311 for (lookup
= os
->prev
; lookup
!= NULL
; lookup
= lookup
->prev
)
1313 if (lookup
->constraint
== -1)
1316 if (lookup
->bfd_section
!= NULL
&& lookup
->bfd_section
->owner
!= NULL
)
1317 return lookup
->bfd_section
;
1323 lang_output_section_statement_type
*
1324 lang_insert_orphan (asection
*s
,
1325 const char *secname
,
1326 lang_output_section_statement_type
*after
,
1327 struct orphan_save
*place
,
1328 etree_type
*address
,
1329 lang_statement_list_type
*add_child
)
1331 lang_statement_list_type
*old
;
1332 lang_statement_list_type add
;
1334 etree_type
*load_base
;
1335 lang_output_section_statement_type
*os
;
1336 lang_output_section_statement_type
**os_tail
;
1338 /* Start building a list of statements for this section.
1339 First save the current statement pointer. */
1342 /* If we have found an appropriate place for the output section
1343 statements for this orphan, add them to our own private list,
1344 inserting them later into the global statement list. */
1348 lang_list_init (stat_ptr
);
1352 if (config
.build_constructors
)
1354 /* If the name of the section is representable in C, then create
1355 symbols to mark the start and the end of the section. */
1356 for (ps
= secname
; *ps
!= '\0'; ps
++)
1357 if (! ISALNUM ((unsigned char) *ps
) && *ps
!= '_')
1362 etree_type
*e_align
;
1364 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__start_" + 1);
1365 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1366 sprintf (symname
+ (symname
[0] != 0), "__start_%s", secname
);
1367 e_align
= exp_unop (ALIGN_K
,
1368 exp_intop ((bfd_vma
) 1 << s
->alignment_power
));
1369 lang_add_assignment (exp_assop ('=', ".", e_align
));
1370 lang_add_assignment (exp_assop ('=', symname
,
1371 exp_nameop (NAME
, ".")));
1375 if (link_info
.relocatable
|| (s
->flags
& (SEC_LOAD
| SEC_ALLOC
)) == 0)
1376 address
= exp_intop (0);
1379 if (after
!= NULL
&& after
->load_base
!= NULL
)
1381 etree_type
*lma_from_vma
;
1382 lma_from_vma
= exp_binop ('-', after
->load_base
,
1383 exp_nameop (ADDR
, after
->name
));
1384 load_base
= exp_binop ('+', lma_from_vma
,
1385 exp_nameop (ADDR
, secname
));
1388 os_tail
= ((lang_output_section_statement_type
**)
1389 lang_output_section_statement
.tail
);
1390 os
= lang_enter_output_section_statement (secname
, address
, 0, NULL
, NULL
,
1393 if (add_child
== NULL
)
1394 add_child
= &os
->children
;
1395 lang_add_section (add_child
, s
, os
);
1397 lang_leave_output_section_statement (0, "*default*", NULL
, NULL
);
1399 if (config
.build_constructors
&& *ps
== '\0')
1403 /* lang_leave_ouput_section_statement resets stat_ptr.
1404 Put stat_ptr back where we want it. */
1408 symname
= (char *) xmalloc (ps
- secname
+ sizeof "__stop_" + 1);
1409 symname
[0] = bfd_get_symbol_leading_char (output_bfd
);
1410 sprintf (symname
+ (symname
[0] != 0), "__stop_%s", secname
);
1411 lang_add_assignment (exp_assop ('=', symname
,
1412 exp_nameop (NAME
, ".")));
1415 /* Restore the global list pointer. */
1419 if (after
!= NULL
&& os
->bfd_section
!= NULL
)
1421 asection
*snew
, *as
;
1423 snew
= os
->bfd_section
;
1425 /* Shuffle the bfd section list to make the output file look
1426 neater. This is really only cosmetic. */
1427 if (place
->section
== NULL
1428 && after
!= (&lang_output_section_statement
.head
1429 ->output_section_statement
))
1431 asection
*bfd_section
= after
->bfd_section
;
1433 /* If the output statement hasn't been used to place any input
1434 sections (and thus doesn't have an output bfd_section),
1435 look for the closest prior output statement having an
1437 if (bfd_section
== NULL
)
1438 bfd_section
= output_prev_sec_find (after
);
1440 if (bfd_section
!= NULL
&& bfd_section
!= snew
)
1441 place
->section
= &bfd_section
->next
;
1444 if (place
->section
== NULL
)
1445 place
->section
= &output_bfd
->sections
;
1447 as
= *place
->section
;
1451 /* Put the section at the end of the list. */
1453 /* Unlink the section. */
1454 bfd_section_list_remove (output_bfd
, snew
);
1456 /* Now tack it back on in the right place. */
1457 bfd_section_list_append (output_bfd
, snew
);
1459 else if (as
!= snew
&& as
->prev
!= snew
)
1461 /* Unlink the section. */
1462 bfd_section_list_remove (output_bfd
, snew
);
1464 /* Now tack it back on in the right place. */
1465 bfd_section_list_insert_before (output_bfd
, as
, snew
);
1468 /* Save the end of this list. Further ophans of this type will
1469 follow the one we've just added. */
1470 place
->section
= &snew
->next
;
1472 /* The following is non-cosmetic. We try to put the output
1473 statements in some sort of reasonable order here, because they
1474 determine the final load addresses of the orphan sections.
1475 In addition, placing output statements in the wrong order may
1476 require extra segments. For instance, given a typical
1477 situation of all read-only sections placed in one segment and
1478 following that a segment containing all the read-write
1479 sections, we wouldn't want to place an orphan read/write
1480 section before or amongst the read-only ones. */
1481 if (add
.head
!= NULL
)
1483 lang_output_section_statement_type
*newly_added_os
;
1485 if (place
->stmt
== NULL
)
1487 lang_statement_union_type
**where
;
1488 lang_statement_union_type
**assign
= NULL
;
1489 bfd_boolean ignore_first
;
1491 /* Look for a suitable place for the new statement list.
1492 The idea is to skip over anything that might be inside
1493 a SECTIONS {} statement in a script, before we find
1494 another output_section_statement. Assignments to "dot"
1495 before an output section statement are assumed to
1496 belong to it. An exception to this rule is made for
1497 the first assignment to dot, otherwise we might put an
1498 orphan before . = . + SIZEOF_HEADERS or similar
1499 assignments that set the initial address. */
1501 ignore_first
= after
== (&lang_output_section_statement
.head
1502 ->output_section_statement
);
1503 for (where
= &after
->header
.next
;
1505 where
= &(*where
)->header
.next
)
1507 switch ((*where
)->header
.type
)
1509 case lang_assignment_statement_enum
:
1512 lang_assignment_statement_type
*ass
;
1513 ass
= &(*where
)->assignment_statement
;
1514 if (ass
->exp
->type
.node_class
!= etree_assert
1515 && ass
->exp
->assign
.dst
[0] == '.'
1516 && ass
->exp
->assign
.dst
[1] == 0
1520 ignore_first
= FALSE
;
1522 case lang_wild_statement_enum
:
1523 case lang_input_section_enum
:
1524 case lang_object_symbols_statement_enum
:
1525 case lang_fill_statement_enum
:
1526 case lang_data_statement_enum
:
1527 case lang_reloc_statement_enum
:
1528 case lang_padding_statement_enum
:
1529 case lang_constructors_statement_enum
:
1532 case lang_output_section_statement_enum
:
1535 case lang_input_statement_enum
:
1536 case lang_address_statement_enum
:
1537 case lang_target_statement_enum
:
1538 case lang_output_statement_enum
:
1539 case lang_group_statement_enum
:
1540 case lang_afile_asection_pair_statement_enum
:
1549 place
->os_tail
= &after
->next
;
1553 /* Put it after the last orphan statement we added. */
1554 *add
.tail
= *place
->stmt
;
1555 *place
->stmt
= add
.head
;
1558 /* Fix the global list pointer if we happened to tack our
1559 new list at the tail. */
1560 if (*old
->tail
== add
.head
)
1561 old
->tail
= add
.tail
;
1563 /* Save the end of this list. */
1564 place
->stmt
= add
.tail
;
1566 /* Do the same for the list of output section statements. */
1567 newly_added_os
= *os_tail
;
1569 newly_added_os
->prev
= (lang_output_section_statement_type
*)
1570 ((char *) place
->os_tail
1571 - offsetof (lang_output_section_statement_type
, next
));
1572 newly_added_os
->next
= *place
->os_tail
;
1573 if (newly_added_os
->next
!= NULL
)
1574 newly_added_os
->next
->prev
= newly_added_os
;
1575 *place
->os_tail
= newly_added_os
;
1576 place
->os_tail
= &newly_added_os
->next
;
1578 /* Fixing the global list pointer here is a little different.
1579 We added to the list in lang_enter_output_section_statement,
1580 trimmed off the new output_section_statment above when
1581 assigning *os_tail = NULL, but possibly added it back in
1582 the same place when assigning *place->os_tail. */
1583 if (*os_tail
== NULL
)
1584 lang_output_section_statement
.tail
1585 = (lang_statement_union_type
**) os_tail
;
1592 lang_map_flags (flagword flag
)
1594 if (flag
& SEC_ALLOC
)
1597 if (flag
& SEC_CODE
)
1600 if (flag
& SEC_READONLY
)
1603 if (flag
& SEC_DATA
)
1606 if (flag
& SEC_LOAD
)
1613 lang_memory_region_type
*m
;
1614 bfd_boolean dis_header_printed
= FALSE
;
1617 LANG_FOR_EACH_INPUT_STATEMENT (file
)
1621 if ((file
->the_bfd
->flags
& (BFD_LINKER_CREATED
| DYNAMIC
)) != 0
1622 || file
->just_syms_flag
)
1625 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
1626 if (s
->output_section
== NULL
1627 || s
->output_section
->owner
!= output_bfd
)
1629 if (! dis_header_printed
)
1631 fprintf (config
.map_file
, _("\nDiscarded input sections\n\n"));
1632 dis_header_printed
= TRUE
;
1635 print_input_section (s
);
1639 minfo (_("\nMemory Configuration\n\n"));
1640 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
1641 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1643 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
1648 fprintf (config
.map_file
, "%-16s ", m
->name
);
1650 sprintf_vma (buf
, m
->origin
);
1651 minfo ("0x%s ", buf
);
1659 minfo ("0x%V", m
->length
);
1660 if (m
->flags
|| m
->not_flags
)
1668 lang_map_flags (m
->flags
);
1674 lang_map_flags (m
->not_flags
);
1681 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
1683 if (! command_line
.reduce_memory_overheads
)
1685 obstack_begin (&map_obstack
, 1000);
1686 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
1687 bfd_map_over_sections (p
, init_map_userdata
, 0);
1688 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
1690 print_statements ();
1694 init_map_userdata (abfd
, sec
, data
)
1695 bfd
*abfd ATTRIBUTE_UNUSED
;
1697 void *data ATTRIBUTE_UNUSED
;
1699 fat_section_userdata_type
*new_data
1700 = ((fat_section_userdata_type
*) (stat_alloc
1701 (sizeof (fat_section_userdata_type
))));
1703 ASSERT (get_userdata (sec
) == NULL
);
1704 get_userdata (sec
) = new_data
;
1705 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
1709 sort_def_symbol (hash_entry
, info
)
1710 struct bfd_link_hash_entry
*hash_entry
;
1711 void *info ATTRIBUTE_UNUSED
;
1713 if (hash_entry
->type
== bfd_link_hash_defined
1714 || hash_entry
->type
== bfd_link_hash_defweak
)
1716 struct fat_user_section_struct
*ud
;
1717 struct map_symbol_def
*def
;
1719 ud
= get_userdata (hash_entry
->u
.def
.section
);
1722 /* ??? What do we have to do to initialize this beforehand? */
1723 /* The first time we get here is bfd_abs_section... */
1724 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
1725 ud
= get_userdata (hash_entry
->u
.def
.section
);
1727 else if (!ud
->map_symbol_def_tail
)
1728 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
1730 def
= obstack_alloc (&map_obstack
, sizeof *def
);
1731 def
->entry
= hash_entry
;
1732 *(ud
->map_symbol_def_tail
) = def
;
1733 ud
->map_symbol_def_tail
= &def
->next
;
1738 /* Initialize an output section. */
1741 init_os (lang_output_section_statement_type
*s
, asection
*isec
)
1743 if (s
->bfd_section
!= NULL
)
1746 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
1747 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
1749 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
1750 if (s
->bfd_section
== NULL
)
1751 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
1752 if (s
->bfd_section
== NULL
)
1754 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1755 output_bfd
->xvec
->name
, s
->name
);
1757 s
->bfd_section
->output_section
= s
->bfd_section
;
1758 s
->bfd_section
->output_offset
= 0;
1760 if (!command_line
.reduce_memory_overheads
)
1762 fat_section_userdata_type
*new
1763 = stat_alloc (sizeof (fat_section_userdata_type
));
1764 memset (new, 0, sizeof (fat_section_userdata_type
));
1765 get_userdata (s
->bfd_section
) = new;
1768 /* If there is a base address, make sure that any sections it might
1769 mention are initialized. */
1770 if (s
->addr_tree
!= NULL
)
1771 exp_init_os (s
->addr_tree
);
1773 if (s
->load_base
!= NULL
)
1774 exp_init_os (s
->load_base
);
1776 /* If supplied an alignment, set it. */
1777 if (s
->section_alignment
!= -1)
1778 s
->bfd_section
->alignment_power
= s
->section_alignment
;
1781 bfd_init_private_section_data (isec
->owner
, isec
,
1782 output_bfd
, s
->bfd_section
,
1786 /* Make sure that all output sections mentioned in an expression are
1790 exp_init_os (etree_type
*exp
)
1792 switch (exp
->type
.node_class
)
1796 exp_init_os (exp
->assign
.src
);
1800 exp_init_os (exp
->binary
.lhs
);
1801 exp_init_os (exp
->binary
.rhs
);
1805 exp_init_os (exp
->trinary
.cond
);
1806 exp_init_os (exp
->trinary
.lhs
);
1807 exp_init_os (exp
->trinary
.rhs
);
1811 exp_init_os (exp
->assert_s
.child
);
1815 exp_init_os (exp
->unary
.child
);
1819 switch (exp
->type
.node_code
)
1825 lang_output_section_statement_type
*os
;
1827 os
= lang_output_section_find (exp
->name
.name
);
1828 if (os
!= NULL
&& os
->bfd_section
== NULL
)
1840 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
1842 lang_input_statement_type
*entry
= data
;
1844 /* If we are only reading symbols from this object, then we want to
1845 discard all sections. */
1846 if (entry
->just_syms_flag
)
1848 bfd_link_just_syms (abfd
, sec
, &link_info
);
1852 if (!(abfd
->flags
& DYNAMIC
))
1853 bfd_section_already_linked (abfd
, sec
);
1856 /* The wild routines.
1858 These expand statements like *(.text) and foo.o to a list of
1859 explicit actions, like foo.o(.text), bar.o(.text) and
1860 foo.o(.text, .data). */
1862 /* Add SECTION to the output section OUTPUT. Do this by creating a
1863 lang_input_section statement which is placed at PTR. FILE is the
1864 input file which holds SECTION. */
1867 lang_add_section (lang_statement_list_type
*ptr
,
1869 lang_output_section_statement_type
*output
)
1871 flagword flags
= section
->flags
;
1872 bfd_boolean discard
;
1874 /* Discard sections marked with SEC_EXCLUDE. */
1875 discard
= (flags
& SEC_EXCLUDE
) != 0;
1877 /* Discard input sections which are assigned to a section named
1878 DISCARD_SECTION_NAME. */
1879 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1882 /* Discard debugging sections if we are stripping debugging
1884 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1885 && (flags
& SEC_DEBUGGING
) != 0)
1890 if (section
->output_section
== NULL
)
1892 /* This prevents future calls from assigning this section. */
1893 section
->output_section
= bfd_abs_section_ptr
;
1898 if (section
->output_section
== NULL
)
1901 lang_input_section_type
*new;
1904 if (output
->bfd_section
== NULL
)
1905 init_os (output
, section
);
1907 first
= ! output
->bfd_section
->linker_has_input
;
1908 output
->bfd_section
->linker_has_input
= 1;
1910 if (!link_info
.relocatable
1911 && !stripped_excluded_sections
)
1913 asection
*s
= output
->bfd_section
->map_tail
.s
;
1914 output
->bfd_section
->map_tail
.s
= section
;
1915 section
->map_head
.s
= NULL
;
1916 section
->map_tail
.s
= s
;
1918 s
->map_head
.s
= section
;
1920 output
->bfd_section
->map_head
.s
= section
;
1923 /* Add a section reference to the list. */
1924 new = new_stat (lang_input_section
, ptr
);
1926 new->section
= section
;
1927 section
->output_section
= output
->bfd_section
;
1929 flags
= section
->flags
;
1931 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1932 to an output section, because we want to be able to include a
1933 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1934 section (I don't know why we want to do this, but we do).
1935 build_link_order in ldwrite.c handles this case by turning
1936 the embedded SEC_NEVER_LOAD section into a fill. */
1938 flags
&= ~ SEC_NEVER_LOAD
;
1940 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1941 already been processed. One reason to do this is that on pe
1942 format targets, .text$foo sections go into .text and it's odd
1943 to see .text with SEC_LINK_ONCE set. */
1945 if (! link_info
.relocatable
)
1946 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1948 /* If this is not the first input section, and the SEC_READONLY
1949 flag is not currently set, then don't set it just because the
1950 input section has it set. */
1952 if (! first
&& (output
->bfd_section
->flags
& SEC_READONLY
) == 0)
1953 flags
&= ~ SEC_READONLY
;
1955 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1957 && ((output
->bfd_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1958 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1959 || ((flags
& SEC_MERGE
)
1960 && output
->bfd_section
->entsize
!= section
->entsize
)))
1962 output
->bfd_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1963 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1966 output
->bfd_section
->flags
|= flags
;
1968 if (flags
& SEC_MERGE
)
1969 output
->bfd_section
->entsize
= section
->entsize
;
1971 /* If SEC_READONLY is not set in the input section, then clear
1972 it from the output section. */
1973 if ((section
->flags
& SEC_READONLY
) == 0)
1974 output
->bfd_section
->flags
&= ~SEC_READONLY
;
1976 switch (output
->sectype
)
1978 case normal_section
:
1980 case noalloc_section
:
1981 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1983 case noload_section
:
1984 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1985 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1989 /* Copy over SEC_SMALL_DATA. */
1990 if (section
->flags
& SEC_SMALL_DATA
)
1991 output
->bfd_section
->flags
|= SEC_SMALL_DATA
;
1993 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1994 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1996 if (bfd_get_arch (section
->owner
) == bfd_arch_tic54x
1997 && (section
->flags
& SEC_TIC54X_BLOCK
) != 0)
1999 output
->bfd_section
->flags
|= SEC_TIC54X_BLOCK
;
2000 /* FIXME: This value should really be obtained from the bfd... */
2001 output
->block_value
= 128;
2006 /* Compare sections ASEC and BSEC according to SORT. */
2009 compare_section (sort_type sort
, asection
*asec
, asection
*bsec
)
2018 case by_alignment_name
:
2019 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
2020 - bfd_section_alignment (asec
->owner
, asec
));
2026 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
2027 bfd_get_section_name (bsec
->owner
, bsec
));
2030 case by_name_alignment
:
2031 ret
= strcmp (bfd_get_section_name (asec
->owner
, asec
),
2032 bfd_get_section_name (bsec
->owner
, bsec
));
2038 ret
= (bfd_section_alignment (bsec
->owner
, bsec
)
2039 - bfd_section_alignment (asec
->owner
, asec
));
2046 /* Handle wildcard sorting. This returns the lang_input_section which
2047 should follow the one we are going to create for SECTION and FILE,
2048 based on the sorting requirements of WILD. It returns NULL if the
2049 new section should just go at the end of the current list. */
2051 static lang_statement_union_type
*
2052 wild_sort (lang_wild_statement_type
*wild
,
2053 struct wildcard_list
*sec
,
2054 lang_input_statement_type
*file
,
2057 const char *section_name
;
2058 lang_statement_union_type
*l
;
2060 if (!wild
->filenames_sorted
2061 && (sec
== NULL
|| sec
->spec
.sorted
== none
))
2064 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
2065 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
2067 lang_input_section_type
*ls
;
2069 if (l
->header
.type
!= lang_input_section_enum
)
2071 ls
= &l
->input_section
;
2073 /* Sorting by filename takes precedence over sorting by section
2076 if (wild
->filenames_sorted
)
2078 const char *fn
, *ln
;
2082 /* The PE support for the .idata section as generated by
2083 dlltool assumes that files will be sorted by the name of
2084 the archive and then the name of the file within the
2087 if (file
->the_bfd
!= NULL
2088 && bfd_my_archive (file
->the_bfd
) != NULL
)
2090 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
2095 fn
= file
->filename
;
2099 if (bfd_my_archive (ls
->section
->owner
) != NULL
)
2101 ln
= bfd_get_filename (bfd_my_archive (ls
->section
->owner
));
2106 ln
= ls
->section
->owner
->filename
;
2110 i
= strcmp (fn
, ln
);
2119 fn
= file
->filename
;
2121 ln
= ls
->section
->owner
->filename
;
2123 i
= strcmp (fn
, ln
);
2131 /* Here either the files are not sorted by name, or we are
2132 looking at the sections for this file. */
2134 if (sec
!= NULL
&& sec
->spec
.sorted
!= none
)
2135 if (compare_section (sec
->spec
.sorted
, section
, ls
->section
) < 0)
2142 /* Expand a wild statement for a particular FILE. SECTION may be
2143 NULL, in which case it is a wild card. */
2146 output_section_callback (lang_wild_statement_type
*ptr
,
2147 struct wildcard_list
*sec
,
2149 lang_input_statement_type
*file
,
2152 lang_statement_union_type
*before
;
2154 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2155 if (unique_section_p (section
))
2158 before
= wild_sort (ptr
, sec
, file
, section
);
2160 /* Here BEFORE points to the lang_input_section which
2161 should follow the one we are about to add. If BEFORE
2162 is NULL, then the section should just go at the end
2163 of the current list. */
2166 lang_add_section (&ptr
->children
, section
,
2167 (lang_output_section_statement_type
*) output
);
2170 lang_statement_list_type list
;
2171 lang_statement_union_type
**pp
;
2173 lang_list_init (&list
);
2174 lang_add_section (&list
, section
,
2175 (lang_output_section_statement_type
*) output
);
2177 /* If we are discarding the section, LIST.HEAD will
2179 if (list
.head
!= NULL
)
2181 ASSERT (list
.head
->header
.next
== NULL
);
2183 for (pp
= &ptr
->children
.head
;
2185 pp
= &(*pp
)->header
.next
)
2186 ASSERT (*pp
!= NULL
);
2188 list
.head
->header
.next
= *pp
;
2194 /* Check if all sections in a wild statement for a particular FILE
2198 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
2199 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
2201 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
2204 /* Exclude sections that match UNIQUE_SECTION_LIST. */
2205 if (unique_section_p (section
))
2208 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
2209 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
2212 /* This is passed a file name which must have been seen already and
2213 added to the statement tree. We will see if it has been opened
2214 already and had its symbols read. If not then we'll read it. */
2216 static lang_input_statement_type
*
2217 lookup_name (const char *name
)
2219 lang_input_statement_type
*search
;
2221 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
2223 search
= (lang_input_statement_type
*) search
->next_real_file
)
2225 /* Use the local_sym_name as the name of the file that has
2226 already been loaded as filename might have been transformed
2227 via the search directory lookup mechanism. */
2228 const char *filename
= search
->local_sym_name
;
2230 if (filename
!= NULL
2231 && strcmp (filename
, name
) == 0)
2236 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
2237 default_target
, FALSE
);
2239 /* If we have already added this file, or this file is not real
2240 don't add this file. */
2241 if (search
->loaded
|| !search
->real
)
2244 if (! load_symbols (search
, NULL
))
2250 /* Save LIST as a list of libraries whose symbols should not be exported. */
2255 struct excluded_lib
*next
;
2257 static struct excluded_lib
*excluded_libs
;
2260 add_excluded_libs (const char *list
)
2262 const char *p
= list
, *end
;
2266 struct excluded_lib
*entry
;
2267 end
= strpbrk (p
, ",:");
2269 end
= p
+ strlen (p
);
2270 entry
= xmalloc (sizeof (*entry
));
2271 entry
->next
= excluded_libs
;
2272 entry
->name
= xmalloc (end
- p
+ 1);
2273 memcpy (entry
->name
, p
, end
- p
);
2274 entry
->name
[end
- p
] = '\0';
2275 excluded_libs
= entry
;
2283 check_excluded_libs (bfd
*abfd
)
2285 struct excluded_lib
*lib
= excluded_libs
;
2289 int len
= strlen (lib
->name
);
2290 const char *filename
= lbasename (abfd
->filename
);
2292 if (strcmp (lib
->name
, "ALL") == 0)
2294 abfd
->no_export
= TRUE
;
2298 if (strncmp (lib
->name
, filename
, len
) == 0
2299 && (filename
[len
] == '\0'
2300 || (filename
[len
] == '.' && filename
[len
+ 1] == 'a'
2301 && filename
[len
+ 2] == '\0')))
2303 abfd
->no_export
= TRUE
;
2311 /* Get the symbols for an input file. */
2314 load_symbols (lang_input_statement_type
*entry
,
2315 lang_statement_list_type
*place
)
2322 ldfile_open_file (entry
);
2324 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
2325 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
2328 lang_statement_list_type
*hold
;
2329 bfd_boolean bad_load
= TRUE
;
2330 bfd_boolean save_ldlang_sysrooted_script
;
2331 bfd_boolean save_as_needed
, save_add_needed
;
2333 err
= bfd_get_error ();
2335 /* See if the emulation has some special knowledge. */
2336 if (ldemul_unrecognized_file (entry
))
2339 if (err
== bfd_error_file_ambiguously_recognized
)
2343 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
2344 einfo (_("%B: matching formats:"), entry
->the_bfd
);
2345 for (p
= matching
; *p
!= NULL
; p
++)
2349 else if (err
!= bfd_error_file_not_recognized
2351 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
2355 bfd_close (entry
->the_bfd
);
2356 entry
->the_bfd
= NULL
;
2358 /* Try to interpret the file as a linker script. */
2359 ldfile_open_command_file (entry
->filename
);
2363 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
2364 ldlang_sysrooted_script
= entry
->sysrooted
;
2365 save_as_needed
= as_needed
;
2366 as_needed
= entry
->as_needed
;
2367 save_add_needed
= add_needed
;
2368 add_needed
= entry
->add_needed
;
2370 ldfile_assumed_script
= TRUE
;
2371 parser_input
= input_script
;
2372 /* We want to use the same -Bdynamic/-Bstatic as the one for
2374 config
.dynamic_link
= entry
->dynamic
;
2376 ldfile_assumed_script
= FALSE
;
2378 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
2379 as_needed
= save_as_needed
;
2380 add_needed
= save_add_needed
;
2386 if (ldemul_recognized_file (entry
))
2389 /* We don't call ldlang_add_file for an archive. Instead, the
2390 add_symbols entry point will call ldlang_add_file, via the
2391 add_archive_element callback, for each element of the archive
2393 switch (bfd_get_format (entry
->the_bfd
))
2399 ldlang_add_file (entry
);
2400 if (trace_files
|| trace_file_tries
)
2401 info_msg ("%I\n", entry
);
2405 check_excluded_libs (entry
->the_bfd
);
2407 if (entry
->whole_archive
)
2410 bfd_boolean loaded
= TRUE
;
2414 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
2419 if (! bfd_check_format (member
, bfd_object
))
2421 einfo (_("%F%B: member %B in archive is not an object\n"),
2422 entry
->the_bfd
, member
);
2426 if (! ((*link_info
.callbacks
->add_archive_element
)
2427 (&link_info
, member
, "--whole-archive")))
2430 if (! bfd_link_add_symbols (member
, &link_info
))
2432 einfo (_("%F%B: could not read symbols: %E\n"), member
);
2437 entry
->loaded
= loaded
;
2443 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
2444 entry
->loaded
= TRUE
;
2446 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
2448 return entry
->loaded
;
2451 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
2452 may be NULL, indicating that it is a wildcard. Separate
2453 lang_input_section statements are created for each part of the
2454 expansion; they are added after the wild statement S. OUTPUT is
2455 the output section. */
2458 wild (lang_wild_statement_type
*s
,
2459 const char *target ATTRIBUTE_UNUSED
,
2460 lang_output_section_statement_type
*output
)
2462 struct wildcard_list
*sec
;
2464 walk_wild (s
, output_section_callback
, output
);
2466 if (default_common_section
== NULL
)
2467 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
2468 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
2470 /* Remember the section that common is going to in case we
2471 later get something which doesn't know where to put it. */
2472 default_common_section
= output
;
2477 /* Return TRUE iff target is the sought target. */
2480 get_target (const bfd_target
*target
, void *data
)
2482 const char *sought
= data
;
2484 return strcmp (target
->name
, sought
) == 0;
2487 /* Like strcpy() but convert to lower case as well. */
2490 stricpy (char *dest
, char *src
)
2494 while ((c
= *src
++) != 0)
2495 *dest
++ = TOLOWER (c
);
2500 /* Remove the first occurrence of needle (if any) in haystack
2504 strcut (char *haystack
, char *needle
)
2506 haystack
= strstr (haystack
, needle
);
2512 for (src
= haystack
+ strlen (needle
); *src
;)
2513 *haystack
++ = *src
++;
2519 /* Compare two target format name strings.
2520 Return a value indicating how "similar" they are. */
2523 name_compare (char *first
, char *second
)
2529 copy1
= xmalloc (strlen (first
) + 1);
2530 copy2
= xmalloc (strlen (second
) + 1);
2532 /* Convert the names to lower case. */
2533 stricpy (copy1
, first
);
2534 stricpy (copy2
, second
);
2536 /* Remove size and endian strings from the name. */
2537 strcut (copy1
, "big");
2538 strcut (copy1
, "little");
2539 strcut (copy2
, "big");
2540 strcut (copy2
, "little");
2542 /* Return a value based on how many characters match,
2543 starting from the beginning. If both strings are
2544 the same then return 10 * their length. */
2545 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
2546 if (copy1
[result
] == 0)
2558 /* Set by closest_target_match() below. */
2559 static const bfd_target
*winner
;
2561 /* Scan all the valid bfd targets looking for one that has the endianness
2562 requirement that was specified on the command line, and is the nearest
2563 match to the original output target. */
2566 closest_target_match (const bfd_target
*target
, void *data
)
2568 const bfd_target
*original
= data
;
2570 if (command_line
.endian
== ENDIAN_BIG
2571 && target
->byteorder
!= BFD_ENDIAN_BIG
)
2574 if (command_line
.endian
== ENDIAN_LITTLE
2575 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
2578 /* Must be the same flavour. */
2579 if (target
->flavour
!= original
->flavour
)
2582 /* If we have not found a potential winner yet, then record this one. */
2589 /* Oh dear, we now have two potential candidates for a successful match.
2590 Compare their names and choose the better one. */
2591 if (name_compare (target
->name
, original
->name
)
2592 > name_compare (winner
->name
, original
->name
))
2595 /* Keep on searching until wqe have checked them all. */
2599 /* Return the BFD target format of the first input file. */
2602 get_first_input_target (void)
2604 char *target
= NULL
;
2606 LANG_FOR_EACH_INPUT_STATEMENT (s
)
2608 if (s
->header
.type
== lang_input_statement_enum
2611 ldfile_open_file (s
);
2613 if (s
->the_bfd
!= NULL
2614 && bfd_check_format (s
->the_bfd
, bfd_object
))
2616 target
= bfd_get_target (s
->the_bfd
);
2628 lang_get_output_target (void)
2632 /* Has the user told us which output format to use? */
2633 if (output_target
!= NULL
)
2634 return output_target
;
2636 /* No - has the current target been set to something other than
2638 if (current_target
!= default_target
)
2639 return current_target
;
2641 /* No - can we determine the format of the first input file? */
2642 target
= get_first_input_target ();
2646 /* Failed - use the default output target. */
2647 return default_target
;
2650 /* Open the output file. */
2653 open_output (const char *name
)
2657 output_target
= lang_get_output_target ();
2659 /* Has the user requested a particular endianness on the command
2661 if (command_line
.endian
!= ENDIAN_UNSET
)
2663 const bfd_target
*target
;
2664 enum bfd_endian desired_endian
;
2666 /* Get the chosen target. */
2667 target
= bfd_search_for_target (get_target
, (void *) output_target
);
2669 /* If the target is not supported, we cannot do anything. */
2672 if (command_line
.endian
== ENDIAN_BIG
)
2673 desired_endian
= BFD_ENDIAN_BIG
;
2675 desired_endian
= BFD_ENDIAN_LITTLE
;
2677 /* See if the target has the wrong endianness. This should
2678 not happen if the linker script has provided big and
2679 little endian alternatives, but some scrips don't do
2681 if (target
->byteorder
!= desired_endian
)
2683 /* If it does, then see if the target provides
2684 an alternative with the correct endianness. */
2685 if (target
->alternative_target
!= NULL
2686 && (target
->alternative_target
->byteorder
== desired_endian
))
2687 output_target
= target
->alternative_target
->name
;
2690 /* Try to find a target as similar as possible to
2691 the default target, but which has the desired
2692 endian characteristic. */
2693 bfd_search_for_target (closest_target_match
,
2696 /* Oh dear - we could not find any targets that
2697 satisfy our requirements. */
2699 einfo (_("%P: warning: could not find any targets"
2700 " that match endianness requirement\n"));
2702 output_target
= winner
->name
;
2708 output
= bfd_openw (name
, output_target
);
2712 if (bfd_get_error () == bfd_error_invalid_target
)
2713 einfo (_("%P%F: target %s not found\n"), output_target
);
2715 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
2718 delete_output_file_on_failure
= TRUE
;
2720 if (! bfd_set_format (output
, bfd_object
))
2721 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
2722 if (! bfd_set_arch_mach (output
,
2723 ldfile_output_architecture
,
2724 ldfile_output_machine
))
2725 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
2727 link_info
.hash
= bfd_link_hash_table_create (output
);
2728 if (link_info
.hash
== NULL
)
2729 einfo (_("%P%F: can not create hash table: %E\n"));
2731 bfd_set_gp_size (output
, g_switch_value
);
2736 ldlang_open_output (lang_statement_union_type
*statement
)
2738 switch (statement
->header
.type
)
2740 case lang_output_statement_enum
:
2741 ASSERT (output_bfd
== NULL
);
2742 output_bfd
= open_output (statement
->output_statement
.name
);
2743 ldemul_set_output_arch ();
2744 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
2745 output_bfd
->flags
|= D_PAGED
;
2747 output_bfd
->flags
&= ~D_PAGED
;
2748 if (config
.text_read_only
)
2749 output_bfd
->flags
|= WP_TEXT
;
2751 output_bfd
->flags
&= ~WP_TEXT
;
2752 if (link_info
.traditional_format
)
2753 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
2755 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
2758 case lang_target_statement_enum
:
2759 current_target
= statement
->target_statement
.target
;
2766 /* Convert between addresses in bytes and sizes in octets.
2767 For currently supported targets, octets_per_byte is always a power
2768 of two, so we can use shifts. */
2769 #define TO_ADDR(X) ((X) >> opb_shift)
2770 #define TO_SIZE(X) ((X) << opb_shift)
2772 /* Support the above. */
2773 static unsigned int opb_shift
= 0;
2778 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2779 ldfile_output_machine
);
2782 while ((x
& 1) == 0)
2790 /* Open all the input files. */
2793 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
2795 for (; s
!= NULL
; s
= s
->header
.next
)
2797 switch (s
->header
.type
)
2799 case lang_constructors_statement_enum
:
2800 open_input_bfds (constructor_list
.head
, force
);
2802 case lang_output_section_statement_enum
:
2803 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2805 case lang_wild_statement_enum
:
2806 /* Maybe we should load the file's symbols. */
2807 if (s
->wild_statement
.filename
2808 && ! wildcardp (s
->wild_statement
.filename
))
2809 lookup_name (s
->wild_statement
.filename
);
2810 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2812 case lang_group_statement_enum
:
2814 struct bfd_link_hash_entry
*undefs
;
2816 /* We must continually search the entries in the group
2817 until no new symbols are added to the list of undefined
2822 undefs
= link_info
.hash
->undefs_tail
;
2823 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2825 while (undefs
!= link_info
.hash
->undefs_tail
);
2828 case lang_target_statement_enum
:
2829 current_target
= s
->target_statement
.target
;
2831 case lang_input_statement_enum
:
2832 if (s
->input_statement
.real
)
2834 lang_statement_list_type add
;
2836 s
->input_statement
.target
= current_target
;
2838 /* If we are being called from within a group, and this
2839 is an archive which has already been searched, then
2840 force it to be researched unless the whole archive
2841 has been loaded already. */
2843 && !s
->input_statement
.whole_archive
2844 && s
->input_statement
.loaded
2845 && bfd_check_format (s
->input_statement
.the_bfd
,
2847 s
->input_statement
.loaded
= FALSE
;
2849 lang_list_init (&add
);
2851 if (! load_symbols (&s
->input_statement
, &add
))
2852 config
.make_executable
= FALSE
;
2854 if (add
.head
!= NULL
)
2856 *add
.tail
= s
->header
.next
;
2857 s
->header
.next
= add
.head
;
2867 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2870 lang_track_definedness (const char *name
)
2872 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
2873 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
2876 /* New-function for the definedness hash table. */
2878 static struct bfd_hash_entry
*
2879 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
2880 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
2881 const char *name ATTRIBUTE_UNUSED
)
2883 struct lang_definedness_hash_entry
*ret
2884 = (struct lang_definedness_hash_entry
*) entry
;
2887 ret
= (struct lang_definedness_hash_entry
*)
2888 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
2891 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
2893 ret
->iteration
= -1;
2897 /* Return the iteration when the definition of NAME was last updated. A
2898 value of -1 means that the symbol is not defined in the linker script
2899 or the command line, but may be defined in the linker symbol table. */
2902 lang_symbol_definition_iteration (const char *name
)
2904 struct lang_definedness_hash_entry
*defentry
2905 = (struct lang_definedness_hash_entry
*)
2906 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
2908 /* We've already created this one on the presence of DEFINED in the
2909 script, so it can't be NULL unless something is borked elsewhere in
2911 if (defentry
== NULL
)
2914 return defentry
->iteration
;
2917 /* Update the definedness state of NAME. */
2920 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
2922 struct lang_definedness_hash_entry
*defentry
2923 = (struct lang_definedness_hash_entry
*)
2924 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
2926 /* We don't keep track of symbols not tested with DEFINED. */
2927 if (defentry
== NULL
)
2930 /* If the symbol was already defined, and not from an earlier statement
2931 iteration, don't update the definedness iteration, because that'd
2932 make the symbol seem defined in the linker script at this point, and
2933 it wasn't; it was defined in some object. If we do anyway, DEFINED
2934 would start to yield false before this point and the construct "sym =
2935 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2937 if (h
->type
!= bfd_link_hash_undefined
2938 && h
->type
!= bfd_link_hash_common
2939 && h
->type
!= bfd_link_hash_new
2940 && defentry
->iteration
== -1)
2943 defentry
->iteration
= lang_statement_iteration
;
2946 /* Add the supplied name to the symbol table as an undefined reference.
2947 This is a two step process as the symbol table doesn't even exist at
2948 the time the ld command line is processed. First we put the name
2949 on a list, then, once the output file has been opened, transfer the
2950 name to the symbol table. */
2952 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2954 #define ldlang_undef_chain_list_head entry_symbol.next
2957 ldlang_add_undef (const char *const name
)
2959 ldlang_undef_chain_list_type
*new =
2960 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
2962 new->next
= ldlang_undef_chain_list_head
;
2963 ldlang_undef_chain_list_head
= new;
2965 new->name
= xstrdup (name
);
2967 if (output_bfd
!= NULL
)
2968 insert_undefined (new->name
);
2971 /* Insert NAME as undefined in the symbol table. */
2974 insert_undefined (const char *name
)
2976 struct bfd_link_hash_entry
*h
;
2978 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2980 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2981 if (h
->type
== bfd_link_hash_new
)
2983 h
->type
= bfd_link_hash_undefined
;
2984 h
->u
.undef
.abfd
= NULL
;
2985 bfd_link_add_undef (link_info
.hash
, h
);
2989 /* Run through the list of undefineds created above and place them
2990 into the linker hash table as undefined symbols belonging to the
2994 lang_place_undefineds (void)
2996 ldlang_undef_chain_list_type
*ptr
;
2998 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
2999 insert_undefined (ptr
->name
);
3002 /* Check for all readonly or some readwrite sections. */
3005 check_input_sections
3006 (lang_statement_union_type
*s
,
3007 lang_output_section_statement_type
*output_section_statement
)
3009 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3011 switch (s
->header
.type
)
3013 case lang_wild_statement_enum
:
3014 walk_wild (&s
->wild_statement
, check_section_callback
,
3015 output_section_statement
);
3016 if (! output_section_statement
->all_input_readonly
)
3019 case lang_constructors_statement_enum
:
3020 check_input_sections (constructor_list
.head
,
3021 output_section_statement
);
3022 if (! output_section_statement
->all_input_readonly
)
3025 case lang_group_statement_enum
:
3026 check_input_sections (s
->group_statement
.children
.head
,
3027 output_section_statement
);
3028 if (! output_section_statement
->all_input_readonly
)
3037 /* Update wildcard statements if needed. */
3040 update_wild_statements (lang_statement_union_type
*s
)
3042 struct wildcard_list
*sec
;
3044 switch (sort_section
)
3054 for (; s
!= NULL
; s
= s
->header
.next
)
3056 switch (s
->header
.type
)
3061 case lang_wild_statement_enum
:
3062 sec
= s
->wild_statement
.section_list
;
3065 switch (sec
->spec
.sorted
)
3068 sec
->spec
.sorted
= sort_section
;
3071 if (sort_section
== by_alignment
)
3072 sec
->spec
.sorted
= by_name_alignment
;
3075 if (sort_section
== by_name
)
3076 sec
->spec
.sorted
= by_alignment_name
;
3084 case lang_constructors_statement_enum
:
3085 update_wild_statements (constructor_list
.head
);
3088 case lang_output_section_statement_enum
:
3089 update_wild_statements
3090 (s
->output_section_statement
.children
.head
);
3093 case lang_group_statement_enum
:
3094 update_wild_statements (s
->group_statement
.children
.head
);
3102 /* Open input files and attach to output sections. */
3105 map_input_to_output_sections
3106 (lang_statement_union_type
*s
, const char *target
,
3107 lang_output_section_statement_type
*os
)
3109 for (; s
!= NULL
; s
= s
->header
.next
)
3111 switch (s
->header
.type
)
3113 case lang_wild_statement_enum
:
3114 wild (&s
->wild_statement
, target
, os
);
3116 case lang_constructors_statement_enum
:
3117 map_input_to_output_sections (constructor_list
.head
,
3121 case lang_output_section_statement_enum
:
3122 if (s
->output_section_statement
.constraint
)
3124 if (s
->output_section_statement
.constraint
!= ONLY_IF_RW
3125 && s
->output_section_statement
.constraint
!= ONLY_IF_RO
)
3127 s
->output_section_statement
.all_input_readonly
= TRUE
;
3128 check_input_sections (s
->output_section_statement
.children
.head
,
3129 &s
->output_section_statement
);
3130 if ((s
->output_section_statement
.all_input_readonly
3131 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
3132 || (!s
->output_section_statement
.all_input_readonly
3133 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
3135 s
->output_section_statement
.constraint
= -1;
3140 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
3142 &s
->output_section_statement
);
3144 case lang_output_statement_enum
:
3146 case lang_target_statement_enum
:
3147 target
= s
->target_statement
.target
;
3149 case lang_group_statement_enum
:
3150 map_input_to_output_sections (s
->group_statement
.children
.head
,
3154 case lang_data_statement_enum
:
3155 /* Make sure that any sections mentioned in the expression
3157 exp_init_os (s
->data_statement
.exp
);
3158 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3160 /* The output section gets contents, and then we inspect for
3161 any flags set in the input script which override any ALLOC. */
3162 os
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3163 if (!(os
->flags
& SEC_NEVER_LOAD
))
3164 os
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
3166 case lang_input_section_enum
:
3168 case lang_fill_statement_enum
:
3169 case lang_object_symbols_statement_enum
:
3170 case lang_reloc_statement_enum
:
3171 case lang_padding_statement_enum
:
3172 case lang_input_statement_enum
:
3173 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3176 case lang_assignment_statement_enum
:
3177 if (os
!= NULL
&& os
->bfd_section
== NULL
)
3180 /* Make sure that any sections mentioned in the assignment
3182 exp_init_os (s
->assignment_statement
.exp
);
3184 case lang_afile_asection_pair_statement_enum
:
3187 case lang_address_statement_enum
:
3188 /* Mark the specified section with the supplied address.
3190 If this section was actually a segment marker, then the
3191 directive is ignored if the linker script explicitly
3192 processed the segment marker. Originally, the linker
3193 treated segment directives (like -Ttext on the
3194 command-line) as section directives. We honor the
3195 section directive semantics for backwards compatibilty;
3196 linker scripts that do not specifically check for
3197 SEGMENT_START automatically get the old semantics. */
3198 if (!s
->address_statement
.segment
3199 || !s
->address_statement
.segment
->used
)
3201 lang_output_section_statement_type
*aos
3202 = (lang_output_section_statement_lookup
3203 (s
->address_statement
.section_name
));
3205 if (aos
->bfd_section
== NULL
)
3206 init_os (aos
, NULL
);
3207 aos
->addr_tree
= s
->address_statement
.address
;
3214 /* An output section might have been removed after its statement was
3215 added. For example, ldemul_before_allocation can remove dynamic
3216 sections if they turn out to be not needed. Clean them up here. */
3219 strip_excluded_output_sections (void)
3221 lang_output_section_statement_type
*os
;
3223 /* Run lang_size_sections (if not already done). */
3224 if (expld
.phase
!= lang_mark_phase_enum
)
3226 expld
.phase
= lang_mark_phase_enum
;
3227 expld
.dataseg
.phase
= exp_dataseg_none
;
3228 one_lang_size_sections_pass (NULL
, FALSE
);
3229 lang_reset_memory_regions ();
3232 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
3236 asection
*output_section
;
3237 bfd_boolean exclude
;
3239 if (os
->constraint
== -1)
3242 output_section
= os
->bfd_section
;
3243 if (output_section
== NULL
)
3246 exclude
= (output_section
->rawsize
== 0
3247 && (output_section
->flags
& SEC_KEEP
) == 0
3248 && !bfd_section_removed_from_list (output_bfd
,
3251 /* Some sections have not yet been sized, notably .gnu.version,
3252 .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
3253 input sections, so don't drop output sections that have such
3254 input sections unless they are also marked SEC_EXCLUDE. */
3255 if (exclude
&& output_section
->map_head
.s
!= NULL
)
3259 for (s
= output_section
->map_head
.s
; s
!= NULL
; s
= s
->map_head
.s
)
3260 if ((s
->flags
& SEC_LINKER_CREATED
) != 0
3261 && (s
->flags
& SEC_EXCLUDE
) == 0)
3268 /* TODO: Don't just junk map_head.s, turn them into link_orders. */
3269 output_section
->map_head
.link_order
= NULL
;
3270 output_section
->map_tail
.link_order
= NULL
;
3274 /* We don't set bfd_section to NULL since bfd_section of the
3275 removed output section statement may still be used. */
3277 output_section
->flags
|= SEC_EXCLUDE
;
3278 bfd_section_list_remove (output_bfd
, output_section
);
3279 output_bfd
->section_count
--;
3283 /* Stop future calls to lang_add_section from messing with map_head
3284 and map_tail link_order fields. */
3285 stripped_excluded_sections
= TRUE
;
3289 print_output_section_statement
3290 (lang_output_section_statement_type
*output_section_statement
)
3292 asection
*section
= output_section_statement
->bfd_section
;
3295 if (output_section_statement
!= abs_output_section
)
3297 minfo ("\n%s", output_section_statement
->name
);
3299 if (section
!= NULL
)
3301 print_dot
= section
->vma
;
3303 len
= strlen (output_section_statement
->name
);
3304 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3309 while (len
< SECTION_NAME_MAP_LENGTH
)
3315 minfo ("0x%V %W", section
->vma
, section
->size
);
3317 if (output_section_statement
->load_base
!= NULL
)
3321 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
3323 minfo (_(" load address 0x%V"), addr
);
3330 print_statement_list (output_section_statement
->children
.head
,
3331 output_section_statement
);
3334 /* Scan for the use of the destination in the right hand side
3335 of an expression. In such cases we will not compute the
3336 correct expression, since the value of DST that is used on
3337 the right hand side will be its final value, not its value
3338 just before this expression is evaluated. */
3341 scan_for_self_assignment (const char * dst
, etree_type
* rhs
)
3343 if (rhs
== NULL
|| dst
== NULL
)
3346 switch (rhs
->type
.node_class
)
3349 return scan_for_self_assignment (dst
, rhs
->binary
.lhs
)
3350 || scan_for_self_assignment (dst
, rhs
->binary
.rhs
);
3353 return scan_for_self_assignment (dst
, rhs
->trinary
.lhs
)
3354 || scan_for_self_assignment (dst
, rhs
->trinary
.rhs
);
3357 case etree_provided
:
3359 if (strcmp (dst
, rhs
->assign
.dst
) == 0)
3361 return scan_for_self_assignment (dst
, rhs
->assign
.src
);
3364 return scan_for_self_assignment (dst
, rhs
->unary
.child
);
3368 return strcmp (dst
, rhs
->value
.str
) == 0;
3373 return strcmp (dst
, rhs
->name
.name
) == 0;
3385 print_assignment (lang_assignment_statement_type
*assignment
,
3386 lang_output_section_statement_type
*output_section
)
3390 bfd_boolean computation_is_valid
= TRUE
;
3393 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3396 if (assignment
->exp
->type
.node_class
== etree_assert
)
3399 tree
= assignment
->exp
->assert_s
.child
;
3400 computation_is_valid
= TRUE
;
3404 const char *dst
= assignment
->exp
->assign
.dst
;
3406 is_dot
= (dst
[0] == '.' && dst
[1] == 0);
3407 tree
= assignment
->exp
->assign
.src
;
3408 computation_is_valid
= is_dot
|| (scan_for_self_assignment (dst
, tree
) == FALSE
);
3411 exp_fold_tree (tree
, output_section
->bfd_section
, &print_dot
);
3412 if (expld
.result
.valid_p
)
3416 if (computation_is_valid
)
3418 value
= expld
.result
.value
;
3420 if (expld
.result
.section
)
3421 value
+= expld
.result
.section
->vma
;
3423 minfo ("0x%V", value
);
3429 struct bfd_link_hash_entry
*h
;
3431 h
= bfd_link_hash_lookup (link_info
.hash
, assignment
->exp
->assign
.dst
,
3432 FALSE
, FALSE
, TRUE
);
3435 value
= h
->u
.def
.value
;
3437 if (expld
.result
.section
)
3438 value
+= expld
.result
.section
->vma
;
3440 minfo ("[0x%V]", value
);
3443 minfo ("[unresolved]");
3455 exp_print_tree (assignment
->exp
);
3460 print_input_statement (lang_input_statement_type
*statm
)
3462 if (statm
->filename
!= NULL
)
3464 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
3468 /* Print all symbols defined in a particular section. This is called
3469 via bfd_link_hash_traverse, or by print_all_symbols. */
3472 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
3474 asection
*sec
= ptr
;
3476 if ((hash_entry
->type
== bfd_link_hash_defined
3477 || hash_entry
->type
== bfd_link_hash_defweak
)
3478 && sec
== hash_entry
->u
.def
.section
)
3482 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3485 (hash_entry
->u
.def
.value
3486 + hash_entry
->u
.def
.section
->output_offset
3487 + hash_entry
->u
.def
.section
->output_section
->vma
));
3489 minfo (" %T\n", hash_entry
->root
.string
);
3496 print_all_symbols (asection
*sec
)
3498 struct fat_user_section_struct
*ud
= get_userdata (sec
);
3499 struct map_symbol_def
*def
;
3504 *ud
->map_symbol_def_tail
= 0;
3505 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
3506 print_one_symbol (def
->entry
, sec
);
3509 /* Print information about an input section to the map file. */
3512 print_input_section (asection
*i
)
3514 bfd_size_type size
= i
->size
;
3521 minfo ("%s", i
->name
);
3523 len
= 1 + strlen (i
->name
);
3524 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
3529 while (len
< SECTION_NAME_MAP_LENGTH
)
3535 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3536 addr
= i
->output_section
->vma
+ i
->output_offset
;
3543 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
3545 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
3547 len
= SECTION_NAME_MAP_LENGTH
+ 3;
3559 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
3562 if (i
->output_section
!= NULL
&& i
->output_section
->owner
== output_bfd
)
3564 if (command_line
.reduce_memory_overheads
)
3565 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
3567 print_all_symbols (i
);
3569 print_dot
= addr
+ TO_ADDR (size
);
3574 print_fill_statement (lang_fill_statement_type
*fill
)
3578 fputs (" FILL mask 0x", config
.map_file
);
3579 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
3580 fprintf (config
.map_file
, "%02x", *p
);
3581 fputs ("\n", config
.map_file
);
3585 print_data_statement (lang_data_statement_type
*data
)
3593 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3596 addr
= data
->output_offset
;
3597 if (data
->output_section
!= NULL
)
3598 addr
+= data
->output_section
->vma
;
3626 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
3628 if (data
->exp
->type
.node_class
!= etree_value
)
3631 exp_print_tree (data
->exp
);
3636 print_dot
= addr
+ TO_ADDR (size
);
3639 /* Print an address statement. These are generated by options like
3643 print_address_statement (lang_address_statement_type
*address
)
3645 minfo (_("Address of section %s set to "), address
->section_name
);
3646 exp_print_tree (address
->address
);
3650 /* Print a reloc statement. */
3653 print_reloc_statement (lang_reloc_statement_type
*reloc
)
3660 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
3663 addr
= reloc
->output_offset
;
3664 if (reloc
->output_section
!= NULL
)
3665 addr
+= reloc
->output_section
->vma
;
3667 size
= bfd_get_reloc_size (reloc
->howto
);
3669 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
3671 if (reloc
->name
!= NULL
)
3672 minfo ("%s+", reloc
->name
);
3674 minfo ("%s+", reloc
->section
->name
);
3676 exp_print_tree (reloc
->addend_exp
);
3680 print_dot
= addr
+ TO_ADDR (size
);
3684 print_padding_statement (lang_padding_statement_type
*s
)
3692 len
= sizeof " *fill*" - 1;
3693 while (len
< SECTION_NAME_MAP_LENGTH
)
3699 addr
= s
->output_offset
;
3700 if (s
->output_section
!= NULL
)
3701 addr
+= s
->output_section
->vma
;
3702 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
3704 if (s
->fill
->size
!= 0)
3708 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
3709 fprintf (config
.map_file
, "%02x", *p
);
3714 print_dot
= addr
+ TO_ADDR (s
->size
);
3718 print_wild_statement (lang_wild_statement_type
*w
,
3719 lang_output_section_statement_type
*os
)
3721 struct wildcard_list
*sec
;
3725 if (w
->filenames_sorted
)
3727 if (w
->filename
!= NULL
)
3728 minfo ("%s", w
->filename
);
3731 if (w
->filenames_sorted
)
3735 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
3737 if (sec
->spec
.sorted
)
3739 if (sec
->spec
.exclude_name_list
!= NULL
)
3742 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
3743 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
3744 minfo (" %s", tmp
->name
);
3747 if (sec
->spec
.name
!= NULL
)
3748 minfo ("%s", sec
->spec
.name
);
3751 if (sec
->spec
.sorted
)
3760 print_statement_list (w
->children
.head
, os
);
3763 /* Print a group statement. */
3766 print_group (lang_group_statement_type
*s
,
3767 lang_output_section_statement_type
*os
)
3769 fprintf (config
.map_file
, "START GROUP\n");
3770 print_statement_list (s
->children
.head
, os
);
3771 fprintf (config
.map_file
, "END GROUP\n");
3774 /* Print the list of statements in S.
3775 This can be called for any statement type. */
3778 print_statement_list (lang_statement_union_type
*s
,
3779 lang_output_section_statement_type
*os
)
3783 print_statement (s
, os
);
3788 /* Print the first statement in statement list S.
3789 This can be called for any statement type. */
3792 print_statement (lang_statement_union_type
*s
,
3793 lang_output_section_statement_type
*os
)
3795 switch (s
->header
.type
)
3798 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
3801 case lang_constructors_statement_enum
:
3802 if (constructor_list
.head
!= NULL
)
3804 if (constructors_sorted
)
3805 minfo (" SORT (CONSTRUCTORS)\n");
3807 minfo (" CONSTRUCTORS\n");
3808 print_statement_list (constructor_list
.head
, os
);
3811 case lang_wild_statement_enum
:
3812 print_wild_statement (&s
->wild_statement
, os
);
3814 case lang_address_statement_enum
:
3815 print_address_statement (&s
->address_statement
);
3817 case lang_object_symbols_statement_enum
:
3818 minfo (" CREATE_OBJECT_SYMBOLS\n");
3820 case lang_fill_statement_enum
:
3821 print_fill_statement (&s
->fill_statement
);
3823 case lang_data_statement_enum
:
3824 print_data_statement (&s
->data_statement
);
3826 case lang_reloc_statement_enum
:
3827 print_reloc_statement (&s
->reloc_statement
);
3829 case lang_input_section_enum
:
3830 print_input_section (s
->input_section
.section
);
3832 case lang_padding_statement_enum
:
3833 print_padding_statement (&s
->padding_statement
);
3835 case lang_output_section_statement_enum
:
3836 print_output_section_statement (&s
->output_section_statement
);
3838 case lang_assignment_statement_enum
:
3839 print_assignment (&s
->assignment_statement
, os
);
3841 case lang_target_statement_enum
:
3842 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
3844 case lang_output_statement_enum
:
3845 minfo ("OUTPUT(%s", s
->output_statement
.name
);
3846 if (output_target
!= NULL
)
3847 minfo (" %s", output_target
);
3850 case lang_input_statement_enum
:
3851 print_input_statement (&s
->input_statement
);
3853 case lang_group_statement_enum
:
3854 print_group (&s
->group_statement
, os
);
3856 case lang_afile_asection_pair_statement_enum
:
3863 print_statements (void)
3865 print_statement_list (statement_list
.head
, abs_output_section
);
3868 /* Print the first N statements in statement list S to STDERR.
3869 If N == 0, nothing is printed.
3870 If N < 0, the entire list is printed.
3871 Intended to be called from GDB. */
3874 dprint_statement (lang_statement_union_type
*s
, int n
)
3876 FILE *map_save
= config
.map_file
;
3878 config
.map_file
= stderr
;
3881 print_statement_list (s
, abs_output_section
);
3884 while (s
&& --n
>= 0)
3886 print_statement (s
, abs_output_section
);
3891 config
.map_file
= map_save
;
3895 insert_pad (lang_statement_union_type
**ptr
,
3897 unsigned int alignment_needed
,
3898 asection
*output_section
,
3901 static fill_type zero_fill
= { 1, { 0 } };
3902 lang_statement_union_type
*pad
= NULL
;
3904 if (ptr
!= &statement_list
.head
)
3905 pad
= ((lang_statement_union_type
*)
3906 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
3908 && pad
->header
.type
== lang_padding_statement_enum
3909 && pad
->padding_statement
.output_section
== output_section
)
3911 /* Use the existing pad statement. */
3913 else if ((pad
= *ptr
) != NULL
3914 && pad
->header
.type
== lang_padding_statement_enum
3915 && pad
->padding_statement
.output_section
== output_section
)
3917 /* Use the existing pad statement. */
3921 /* Make a new padding statement, linked into existing chain. */
3922 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
3923 pad
->header
.next
= *ptr
;
3925 pad
->header
.type
= lang_padding_statement_enum
;
3926 pad
->padding_statement
.output_section
= output_section
;
3929 pad
->padding_statement
.fill
= fill
;
3931 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
3932 pad
->padding_statement
.size
= alignment_needed
;
3933 output_section
->size
+= alignment_needed
;
3936 /* Work out how much this section will move the dot point. */
3940 (lang_statement_union_type
**this_ptr
,
3941 lang_output_section_statement_type
*output_section_statement
,
3945 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
3946 asection
*i
= is
->section
;
3948 if (!((lang_input_statement_type
*) i
->owner
->usrdata
)->just_syms_flag
3949 && (i
->flags
& SEC_EXCLUDE
) == 0)
3951 unsigned int alignment_needed
;
3954 /* Align this section first to the input sections requirement,
3955 then to the output section's requirement. If this alignment
3956 is greater than any seen before, then record it too. Perform
3957 the alignment by inserting a magic 'padding' statement. */
3959 if (output_section_statement
->subsection_alignment
!= -1)
3960 i
->alignment_power
= output_section_statement
->subsection_alignment
;
3962 o
= output_section_statement
->bfd_section
;
3963 if (o
->alignment_power
< i
->alignment_power
)
3964 o
->alignment_power
= i
->alignment_power
;
3966 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
3968 if (alignment_needed
!= 0)
3970 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
3971 dot
+= alignment_needed
;
3974 /* Remember where in the output section this input section goes. */
3976 i
->output_offset
= dot
- o
->vma
;
3978 /* Mark how big the output section must be to contain this now. */
3979 dot
+= TO_ADDR (i
->size
);
3980 o
->size
= TO_SIZE (dot
- o
->vma
);
3984 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
3991 sort_sections_by_lma (const void *arg1
, const void *arg2
)
3993 const asection
*sec1
= *(const asection
**) arg1
;
3994 const asection
*sec2
= *(const asection
**) arg2
;
3996 if (bfd_section_lma (sec1
->owner
, sec1
)
3997 < bfd_section_lma (sec2
->owner
, sec2
))
3999 else if (bfd_section_lma (sec1
->owner
, sec1
)
4000 > bfd_section_lma (sec2
->owner
, sec2
))
4006 #define IGNORE_SECTION(s) \
4007 ((s->flags & SEC_NEVER_LOAD) != 0 \
4008 || (s->flags & SEC_ALLOC) == 0 \
4009 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
4010 && (s->flags & SEC_LOAD) == 0))
4012 /* Check to see if any allocated sections overlap with other allocated
4013 sections. This can happen if a linker script specifies the output
4014 section addresses of the two sections. */
4017 lang_check_section_addresses (void)
4020 asection
**sections
, **spp
;
4028 if (bfd_count_sections (output_bfd
) <= 1)
4031 amt
= bfd_count_sections (output_bfd
) * sizeof (asection
*);
4032 sections
= xmalloc (amt
);
4034 /* Scan all sections in the output list. */
4036 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4038 /* Only consider loadable sections with real contents. */
4039 if (IGNORE_SECTION (s
) || s
->size
== 0)
4042 sections
[count
] = s
;
4049 qsort (sections
, (size_t) count
, sizeof (asection
*),
4050 sort_sections_by_lma
);
4054 s_start
= bfd_section_lma (output_bfd
, s
);
4055 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4056 for (count
--; count
; count
--)
4058 /* We must check the sections' LMA addresses not their VMA
4059 addresses because overlay sections can have overlapping VMAs
4060 but they must have distinct LMAs. */
4065 s_start
= bfd_section_lma (output_bfd
, s
);
4066 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
4068 /* Look for an overlap. */
4069 if (s_end
>= os_start
&& s_start
<= os_end
)
4070 einfo (_("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
4071 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
4077 /* Make sure the new address is within the region. We explicitly permit the
4078 current address to be at the exact end of the region when the address is
4079 non-zero, in case the region is at the end of addressable memory and the
4080 calculation wraps around. */
4083 os_region_check (lang_output_section_statement_type
*os
,
4084 lang_memory_region_type
*region
,
4088 if ((region
->current
< region
->origin
4089 || (region
->current
- region
->origin
> region
->length
))
4090 && ((region
->current
!= region
->origin
+ region
->length
)
4095 einfo (_("%X%P: address 0x%v of %B section %s"
4096 " is not within region %s\n"),
4098 os
->bfd_section
->owner
,
4099 os
->bfd_section
->name
,
4104 einfo (_("%X%P: region %s is full (%B section %s)\n"),
4106 os
->bfd_section
->owner
,
4107 os
->bfd_section
->name
);
4109 /* Reset the region pointer. */
4110 region
->current
= region
->origin
;
4114 /* Set the sizes for all the output sections. */
4117 lang_size_sections_1
4118 (lang_statement_union_type
*s
,
4119 lang_output_section_statement_type
*output_section_statement
,
4120 lang_statement_union_type
**prev
,
4124 bfd_boolean check_regions
)
4126 /* Size up the sections from their constituent parts. */
4127 for (; s
!= NULL
; s
= s
->header
.next
)
4129 switch (s
->header
.type
)
4131 case lang_output_section_statement_enum
:
4133 bfd_vma newdot
, after
;
4134 lang_output_section_statement_type
*os
;
4136 os
= &s
->output_section_statement
;
4137 if (os
->addr_tree
!= NULL
)
4139 os
->processed
= FALSE
;
4140 exp_fold_tree (os
->addr_tree
, bfd_abs_section_ptr
, &dot
);
4142 if (!expld
.result
.valid_p
4143 && expld
.phase
!= lang_mark_phase_enum
)
4144 einfo (_("%F%S: non constant or forward reference"
4145 " address expression for section %s\n"),
4148 dot
= expld
.result
.value
+ expld
.result
.section
->vma
;
4151 if (os
->bfd_section
== NULL
)
4152 /* This section was removed or never actually created. */
4155 /* If this is a COFF shared library section, use the size and
4156 address from the input section. FIXME: This is COFF
4157 specific; it would be cleaner if there were some other way
4158 to do this, but nothing simple comes to mind. */
4159 if ((bfd_get_flavour (output_bfd
) == bfd_target_ecoff_flavour
4160 || bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
4161 && (os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
4165 if (os
->children
.head
== NULL
4166 || os
->children
.head
->header
.next
!= NULL
4167 || (os
->children
.head
->header
.type
4168 != lang_input_section_enum
))
4169 einfo (_("%P%X: Internal error on COFF shared library"
4170 " section %s\n"), os
->name
);
4172 input
= os
->children
.head
->input_section
.section
;
4173 bfd_set_section_vma (os
->bfd_section
->owner
,
4175 bfd_section_vma (input
->owner
, input
));
4176 os
->bfd_section
->size
= input
->size
;
4181 if (bfd_is_abs_section (os
->bfd_section
))
4183 /* No matter what happens, an abs section starts at zero. */
4184 ASSERT (os
->bfd_section
->vma
== 0);
4190 if (os
->addr_tree
== NULL
)
4192 /* No address specified for this section, get one
4193 from the region specification. */
4194 if (os
->region
== NULL
4195 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
4196 && os
->region
->name
[0] == '*'
4197 && strcmp (os
->region
->name
,
4198 DEFAULT_MEMORY_REGION
) == 0))
4200 os
->region
= lang_memory_default (os
->bfd_section
);
4203 /* If a loadable section is using the default memory
4204 region, and some non default memory regions were
4205 defined, issue an error message. */
4207 && !IGNORE_SECTION (os
->bfd_section
)
4208 && ! link_info
.relocatable
4210 && strcmp (os
->region
->name
,
4211 DEFAULT_MEMORY_REGION
) == 0
4212 && lang_memory_region_list
!= NULL
4213 && (strcmp (lang_memory_region_list
->name
,
4214 DEFAULT_MEMORY_REGION
) != 0
4215 || lang_memory_region_list
->next
!= NULL
)
4216 && expld
.phase
!= lang_mark_phase_enum
)
4218 /* By default this is an error rather than just a
4219 warning because if we allocate the section to the
4220 default memory region we can end up creating an
4221 excessively large binary, or even seg faulting when
4222 attempting to perform a negative seek. See
4223 sources.redhat.com/ml/binutils/2003-04/msg00423.html
4224 for an example of this. This behaviour can be
4225 overridden by the using the --no-check-sections
4227 if (command_line
.check_section_addresses
)
4228 einfo (_("%P%F: error: no memory region specified"
4229 " for loadable section `%s'\n"),
4230 bfd_get_section_name (output_bfd
,
4233 einfo (_("%P: warning: no memory region specified"
4234 " for loadable section `%s'\n"),
4235 bfd_get_section_name (output_bfd
,
4239 newdot
= os
->region
->current
;
4240 align
= os
->bfd_section
->alignment_power
;
4243 align
= os
->section_alignment
;
4245 /* Align to what the section needs. */
4248 bfd_vma savedot
= newdot
;
4249 newdot
= align_power (newdot
, align
);
4251 if (newdot
!= savedot
4252 && (config
.warn_section_align
4253 || os
->addr_tree
!= NULL
)
4254 && expld
.phase
!= lang_mark_phase_enum
)
4255 einfo (_("%P: warning: changing start of section"
4256 " %s by %lu bytes\n"),
4257 os
->name
, (unsigned long) (newdot
- savedot
));
4260 bfd_set_section_vma (0, os
->bfd_section
, newdot
);
4262 os
->bfd_section
->output_offset
= 0;
4265 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
4266 os
->fill
, newdot
, relax
, check_regions
);
4268 os
->processed
= TRUE
;
4270 if (bfd_is_abs_section (os
->bfd_section
) || os
->ignored
)
4272 ASSERT (os
->bfd_section
->size
== 0);
4276 dot
= os
->bfd_section
->vma
;
4278 /* Put the section within the requested block size, or
4279 align at the block boundary. */
4281 + TO_ADDR (os
->bfd_section
->size
)
4282 + os
->block_value
- 1)
4283 & - (bfd_vma
) os
->block_value
);
4285 os
->bfd_section
->size
= TO_SIZE (after
- os
->bfd_section
->vma
);
4287 /* .tbss sections effectively have zero size. */
4288 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4289 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4290 || link_info
.relocatable
)
4291 dot
+= TO_ADDR (os
->bfd_section
->size
);
4293 if (os
->update_dot_tree
!= 0)
4294 exp_fold_tree (os
->update_dot_tree
, bfd_abs_section_ptr
, &dot
);
4296 /* Update dot in the region ?
4297 We only do this if the section is going to be allocated,
4298 since unallocated sections do not contribute to the region's
4299 overall size in memory.
4301 If the SEC_NEVER_LOAD bit is not set, it will affect the
4302 addresses of sections after it. We have to update
4304 if (os
->region
!= NULL
4305 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
4306 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
4308 os
->region
->current
= dot
;
4311 /* Make sure the new address is within the region. */
4312 os_region_check (os
, os
->region
, os
->addr_tree
,
4313 os
->bfd_section
->vma
);
4315 /* If there's no load address specified, use the run
4316 region as the load region. */
4317 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
4318 os
->lma_region
= os
->region
;
4320 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
4322 /* Set load_base, which will be handled later. */
4323 os
->load_base
= exp_intop (os
->lma_region
->current
);
4324 os
->lma_region
->current
+=
4325 TO_ADDR (os
->bfd_section
->size
);
4327 os_region_check (os
, os
->lma_region
, NULL
,
4328 os
->bfd_section
->lma
);
4334 case lang_constructors_statement_enum
:
4335 dot
= lang_size_sections_1 (constructor_list
.head
,
4336 output_section_statement
,
4337 &s
->wild_statement
.children
.head
,
4338 fill
, dot
, relax
, check_regions
);
4341 case lang_data_statement_enum
:
4343 unsigned int size
= 0;
4345 s
->data_statement
.output_offset
=
4346 dot
- output_section_statement
->bfd_section
->vma
;
4347 s
->data_statement
.output_section
=
4348 output_section_statement
->bfd_section
;
4350 /* We might refer to provided symbols in the expression, and
4351 need to mark them as needed. */
4352 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4354 switch (s
->data_statement
.type
)
4372 if (size
< TO_SIZE ((unsigned) 1))
4373 size
= TO_SIZE ((unsigned) 1);
4374 dot
+= TO_ADDR (size
);
4375 output_section_statement
->bfd_section
->size
+= size
;
4379 case lang_reloc_statement_enum
:
4383 s
->reloc_statement
.output_offset
=
4384 dot
- output_section_statement
->bfd_section
->vma
;
4385 s
->reloc_statement
.output_section
=
4386 output_section_statement
->bfd_section
;
4387 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
4388 dot
+= TO_ADDR (size
);
4389 output_section_statement
->bfd_section
->size
+= size
;
4393 case lang_wild_statement_enum
:
4394 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
4395 output_section_statement
,
4396 &s
->wild_statement
.children
.head
,
4397 fill
, dot
, relax
, check_regions
);
4400 case lang_object_symbols_statement_enum
:
4401 link_info
.create_object_symbols_section
=
4402 output_section_statement
->bfd_section
;
4405 case lang_output_statement_enum
:
4406 case lang_target_statement_enum
:
4409 case lang_input_section_enum
:
4413 i
= (*prev
)->input_section
.section
;
4418 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
4419 einfo (_("%P%F: can't relax section: %E\n"));
4423 dot
= size_input_section (prev
, output_section_statement
,
4424 output_section_statement
->fill
, dot
);
4428 case lang_input_statement_enum
:
4431 case lang_fill_statement_enum
:
4432 s
->fill_statement
.output_section
=
4433 output_section_statement
->bfd_section
;
4435 fill
= s
->fill_statement
.fill
;
4438 case lang_assignment_statement_enum
:
4440 bfd_vma newdot
= dot
;
4442 exp_fold_tree (s
->assignment_statement
.exp
,
4443 output_section_statement
->bfd_section
,
4446 if (newdot
!= dot
&& !output_section_statement
->ignored
)
4448 if (output_section_statement
== abs_output_section
)
4450 /* If we don't have an output section, then just adjust
4451 the default memory address. */
4452 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
4453 FALSE
)->current
= newdot
;
4457 /* Insert a pad after this statement. We can't
4458 put the pad before when relaxing, in case the
4459 assignment references dot. */
4460 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
4461 output_section_statement
->bfd_section
, dot
);
4463 /* Don't neuter the pad below when relaxing. */
4466 /* If dot is advanced, this implies that the section
4467 should have space allocated to it, unless the
4468 user has explicitly stated that the section
4469 should never be loaded. */
4470 if (!(output_section_statement
->flags
4471 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
4472 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
4479 case lang_padding_statement_enum
:
4480 /* If this is the first time lang_size_sections is called,
4481 we won't have any padding statements. If this is the
4482 second or later passes when relaxing, we should allow
4483 padding to shrink. If padding is needed on this pass, it
4484 will be added back in. */
4485 s
->padding_statement
.size
= 0;
4487 /* Make sure output_offset is valid. If relaxation shrinks
4488 the section and this pad isn't needed, it's possible to
4489 have output_offset larger than the final size of the
4490 section. bfd_set_section_contents will complain even for
4491 a pad size of zero. */
4492 s
->padding_statement
.output_offset
4493 = dot
- output_section_statement
->bfd_section
->vma
;
4496 case lang_group_statement_enum
:
4497 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
4498 output_section_statement
,
4499 &s
->group_statement
.children
.head
,
4500 fill
, dot
, relax
, check_regions
);
4507 /* We can only get here when relaxing is turned on. */
4508 case lang_address_statement_enum
:
4511 prev
= &s
->header
.next
;
4517 one_lang_size_sections_pass (bfd_boolean
*relax
, bfd_boolean check_regions
)
4519 lang_statement_iteration
++;
4520 lang_size_sections_1 (statement_list
.head
, abs_output_section
,
4521 &statement_list
.head
, 0, 0, relax
, check_regions
);
4525 lang_size_sections (bfd_boolean
*relax
, bfd_boolean check_regions
)
4527 expld
.phase
= lang_allocating_phase_enum
;
4528 expld
.dataseg
.phase
= exp_dataseg_none
;
4530 one_lang_size_sections_pass (relax
, check_regions
);
4531 if (expld
.dataseg
.phase
== exp_dataseg_end_seen
4532 && link_info
.relro
&& expld
.dataseg
.relro_end
)
4534 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
4535 to put expld.dataseg.relro on a (common) page boundary. */
4536 bfd_vma old_min_base
, relro_end
, maxpage
;
4538 expld
.dataseg
.phase
= exp_dataseg_relro_adjust
;
4539 old_min_base
= expld
.dataseg
.min_base
;
4540 maxpage
= expld
.dataseg
.maxpagesize
;
4541 expld
.dataseg
.base
+= (-expld
.dataseg
.relro_end
4542 & (expld
.dataseg
.pagesize
- 1));
4543 /* Compute the expected PT_GNU_RELRO segment end. */
4544 relro_end
= (expld
.dataseg
.relro_end
+ expld
.dataseg
.pagesize
- 1)
4545 & ~(expld
.dataseg
.pagesize
- 1);
4546 if (old_min_base
+ maxpage
< expld
.dataseg
.base
)
4548 expld
.dataseg
.base
-= maxpage
;
4549 relro_end
-= maxpage
;
4551 lang_reset_memory_regions ();
4552 one_lang_size_sections_pass (relax
, check_regions
);
4553 if (expld
.dataseg
.relro_end
> relro_end
)
4555 /* The alignment of sections between DATA_SEGMENT_ALIGN
4556 and DATA_SEGMENT_RELRO_END caused huge padding to be
4557 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
4559 unsigned int max_alignment_power
= 0;
4561 /* Find maximum alignment power of sections between
4562 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
4563 for (sec
= output_bfd
->sections
; sec
; sec
= sec
->next
)
4564 if (sec
->vma
>= expld
.dataseg
.base
4565 && sec
->vma
< expld
.dataseg
.relro_end
4566 && sec
->alignment_power
> max_alignment_power
)
4567 max_alignment_power
= sec
->alignment_power
;
4569 if (((bfd_vma
) 1 << max_alignment_power
) < expld
.dataseg
.pagesize
)
4571 if (expld
.dataseg
.base
- (1 << max_alignment_power
)
4573 expld
.dataseg
.base
+= expld
.dataseg
.pagesize
;
4574 expld
.dataseg
.base
-= (1 << max_alignment_power
);
4575 lang_reset_memory_regions ();
4576 one_lang_size_sections_pass (relax
, check_regions
);
4579 link_info
.relro_start
= expld
.dataseg
.base
;
4580 link_info
.relro_end
= expld
.dataseg
.relro_end
;
4582 else if (expld
.dataseg
.phase
== exp_dataseg_end_seen
)
4584 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
4585 a page could be saved in the data segment. */
4586 bfd_vma first
, last
;
4588 first
= -expld
.dataseg
.base
& (expld
.dataseg
.pagesize
- 1);
4589 last
= expld
.dataseg
.end
& (expld
.dataseg
.pagesize
- 1);
4591 && ((expld
.dataseg
.base
& ~(expld
.dataseg
.pagesize
- 1))
4592 != (expld
.dataseg
.end
& ~(expld
.dataseg
.pagesize
- 1)))
4593 && first
+ last
<= expld
.dataseg
.pagesize
)
4595 expld
.dataseg
.phase
= exp_dataseg_adjust
;
4596 lang_reset_memory_regions ();
4597 one_lang_size_sections_pass (relax
, check_regions
);
4601 expld
.phase
= lang_final_phase_enum
;
4604 /* Worker function for lang_do_assignments. Recursiveness goes here. */
4607 lang_do_assignments_1
4608 (lang_statement_union_type
*s
,
4609 lang_output_section_statement_type
*output_section_statement
,
4613 for (; s
!= NULL
; s
= s
->header
.next
)
4615 switch (s
->header
.type
)
4617 case lang_constructors_statement_enum
:
4618 dot
= lang_do_assignments_1 (constructor_list
.head
,
4619 output_section_statement
,
4624 case lang_output_section_statement_enum
:
4626 lang_output_section_statement_type
*os
;
4628 os
= &(s
->output_section_statement
);
4629 if (os
->bfd_section
!= NULL
&& !os
->ignored
)
4631 dot
= os
->bfd_section
->vma
;
4632 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
4633 /* .tbss sections effectively have zero size. */
4634 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
4635 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
4636 || link_info
.relocatable
)
4637 dot
+= TO_ADDR (os
->bfd_section
->size
);
4641 /* If nothing has been placed into the output section then
4642 it won't have a bfd_section. */
4643 if (os
->bfd_section
&& !os
->ignored
)
4645 os
->bfd_section
->lma
4646 = exp_get_abs_int (os
->load_base
, 0, "load base");
4652 case lang_wild_statement_enum
:
4654 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
4655 output_section_statement
,
4659 case lang_object_symbols_statement_enum
:
4660 case lang_output_statement_enum
:
4661 case lang_target_statement_enum
:
4664 case lang_data_statement_enum
:
4665 exp_fold_tree (s
->data_statement
.exp
, bfd_abs_section_ptr
, &dot
);
4666 if (expld
.result
.valid_p
)
4667 s
->data_statement
.value
= (expld
.result
.value
4668 + expld
.result
.section
->vma
);
4670 einfo (_("%F%P: invalid data statement\n"));
4673 switch (s
->data_statement
.type
)
4691 if (size
< TO_SIZE ((unsigned) 1))
4692 size
= TO_SIZE ((unsigned) 1);
4693 dot
+= TO_ADDR (size
);
4697 case lang_reloc_statement_enum
:
4698 exp_fold_tree (s
->reloc_statement
.addend_exp
,
4699 bfd_abs_section_ptr
, &dot
);
4700 if (expld
.result
.valid_p
)
4701 s
->reloc_statement
.addend_value
= expld
.result
.value
;
4703 einfo (_("%F%P: invalid reloc statement\n"));
4704 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
4707 case lang_input_section_enum
:
4709 asection
*in
= s
->input_section
.section
;
4711 if ((in
->flags
& SEC_EXCLUDE
) == 0)
4712 dot
+= TO_ADDR (in
->size
);
4716 case lang_input_statement_enum
:
4719 case lang_fill_statement_enum
:
4720 fill
= s
->fill_statement
.fill
;
4723 case lang_assignment_statement_enum
:
4724 exp_fold_tree (s
->assignment_statement
.exp
,
4725 output_section_statement
->bfd_section
,
4729 case lang_padding_statement_enum
:
4730 dot
+= TO_ADDR (s
->padding_statement
.size
);
4733 case lang_group_statement_enum
:
4734 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
4735 output_section_statement
,
4743 case lang_address_statement_enum
:
4751 lang_do_assignments (void)
4753 lang_statement_iteration
++;
4754 lang_do_assignments_1 (statement_list
.head
, abs_output_section
, NULL
, 0);
4757 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4758 operator .startof. (section_name), it produces an undefined symbol
4759 .startof.section_name. Similarly, when it sees
4760 .sizeof. (section_name), it produces an undefined symbol
4761 .sizeof.section_name. For all the output sections, we look for
4762 such symbols, and set them to the correct value. */
4765 lang_set_startof (void)
4769 if (link_info
.relocatable
)
4772 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
4774 const char *secname
;
4776 struct bfd_link_hash_entry
*h
;
4778 secname
= bfd_get_section_name (output_bfd
, s
);
4779 buf
= xmalloc (10 + strlen (secname
));
4781 sprintf (buf
, ".startof.%s", secname
);
4782 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4783 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4785 h
->type
= bfd_link_hash_defined
;
4786 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
4787 h
->u
.def
.section
= bfd_abs_section_ptr
;
4790 sprintf (buf
, ".sizeof.%s", secname
);
4791 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
4792 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
4794 h
->type
= bfd_link_hash_defined
;
4795 h
->u
.def
.value
= TO_ADDR (s
->size
);
4796 h
->u
.def
.section
= bfd_abs_section_ptr
;
4806 struct bfd_link_hash_entry
*h
;
4809 if (link_info
.relocatable
|| link_info
.shared
)
4814 if (entry_symbol
.name
== NULL
)
4816 /* No entry has been specified. Look for the default entry, but
4817 don't warn if we don't find it. */
4818 entry_symbol
.name
= entry_symbol_default
;
4822 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
4823 FALSE
, FALSE
, TRUE
);
4825 && (h
->type
== bfd_link_hash_defined
4826 || h
->type
== bfd_link_hash_defweak
)
4827 && h
->u
.def
.section
->output_section
!= NULL
)
4831 val
= (h
->u
.def
.value
4832 + bfd_get_section_vma (output_bfd
,
4833 h
->u
.def
.section
->output_section
)
4834 + h
->u
.def
.section
->output_offset
);
4835 if (! bfd_set_start_address (output_bfd
, val
))
4836 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
4843 /* We couldn't find the entry symbol. Try parsing it as a
4845 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
4848 if (! bfd_set_start_address (output_bfd
, val
))
4849 einfo (_("%P%F: can't set start address\n"));
4855 /* Can't find the entry symbol, and it's not a number. Use
4856 the first address in the text section. */
4857 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
4861 einfo (_("%P: warning: cannot find entry symbol %s;"
4862 " defaulting to %V\n"),
4864 bfd_get_section_vma (output_bfd
, ts
));
4865 if (! bfd_set_start_address (output_bfd
,
4866 bfd_get_section_vma (output_bfd
,
4868 einfo (_("%P%F: can't set start address\n"));
4873 einfo (_("%P: warning: cannot find entry symbol %s;"
4874 " not setting start address\n"),
4880 /* Don't bfd_hash_table_free (&lang_definedness_table);
4881 map file output may result in a call of lang_track_definedness. */
4884 /* This is a small function used when we want to ignore errors from
4888 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
4890 /* Don't do anything. */
4893 /* Check that the architecture of all the input files is compatible
4894 with the output file. Also call the backend to let it do any
4895 other checking that is needed. */
4900 lang_statement_union_type
*file
;
4902 const bfd_arch_info_type
*compatible
;
4904 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
4906 input_bfd
= file
->input_statement
.the_bfd
;
4908 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
4909 command_line
.accept_unknown_input_arch
);
4911 /* In general it is not possible to perform a relocatable
4912 link between differing object formats when the input
4913 file has relocations, because the relocations in the
4914 input format may not have equivalent representations in
4915 the output format (and besides BFD does not translate
4916 relocs for other link purposes than a final link). */
4917 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
4918 && (compatible
== NULL
4919 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
4920 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
4922 einfo (_("%P%F: Relocatable linking with relocations from"
4923 " format %s (%B) to format %s (%B) is not supported\n"),
4924 bfd_get_target (input_bfd
), input_bfd
,
4925 bfd_get_target (output_bfd
), output_bfd
);
4926 /* einfo with %F exits. */
4929 if (compatible
== NULL
)
4931 if (command_line
.warn_mismatch
)
4932 einfo (_("%P: warning: %s architecture of input file `%B'"
4933 " is incompatible with %s output\n"),
4934 bfd_printable_name (input_bfd
), input_bfd
,
4935 bfd_printable_name (output_bfd
));
4937 else if (bfd_count_sections (input_bfd
))
4939 /* If the input bfd has no contents, it shouldn't set the
4940 private data of the output bfd. */
4942 bfd_error_handler_type pfn
= NULL
;
4944 /* If we aren't supposed to warn about mismatched input
4945 files, temporarily set the BFD error handler to a
4946 function which will do nothing. We still want to call
4947 bfd_merge_private_bfd_data, since it may set up
4948 information which is needed in the output file. */
4949 if (! command_line
.warn_mismatch
)
4950 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
4951 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
4953 if (command_line
.warn_mismatch
)
4954 einfo (_("%P%X: failed to merge target specific data"
4955 " of file %B\n"), input_bfd
);
4957 if (! command_line
.warn_mismatch
)
4958 bfd_set_error_handler (pfn
);
4963 /* Look through all the global common symbols and attach them to the
4964 correct section. The -sort-common command line switch may be used
4965 to roughly sort the entries by size. */
4970 if (command_line
.inhibit_common_definition
)
4972 if (link_info
.relocatable
4973 && ! command_line
.force_common_definition
)
4976 if (! config
.sort_common
)
4977 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
4982 for (power
= 4; power
>= 0; power
--)
4983 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
4987 /* Place one common symbol in the correct section. */
4990 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
4992 unsigned int power_of_two
;
4996 if (h
->type
!= bfd_link_hash_common
)
5000 power_of_two
= h
->u
.c
.p
->alignment_power
;
5002 if (config
.sort_common
5003 && power_of_two
< (unsigned int) *(int *) info
)
5006 section
= h
->u
.c
.p
->section
;
5008 /* Increase the size of the section to align the common sym. */
5009 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
5010 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
5012 /* Adjust the alignment if necessary. */
5013 if (power_of_two
> section
->alignment_power
)
5014 section
->alignment_power
= power_of_two
;
5016 /* Change the symbol from common to defined. */
5017 h
->type
= bfd_link_hash_defined
;
5018 h
->u
.def
.section
= section
;
5019 h
->u
.def
.value
= section
->size
;
5021 /* Increase the size of the section. */
5022 section
->size
+= size
;
5024 /* Make sure the section is allocated in memory, and make sure that
5025 it is no longer a common section. */
5026 section
->flags
|= SEC_ALLOC
;
5027 section
->flags
&= ~SEC_IS_COMMON
;
5029 if (config
.map_file
!= NULL
)
5031 static bfd_boolean header_printed
;
5036 if (! header_printed
)
5038 minfo (_("\nAllocating common symbols\n"));
5039 minfo (_("Common symbol size file\n\n"));
5040 header_printed
= TRUE
;
5043 name
= demangle (h
->root
.string
);
5045 len
= strlen (name
);
5060 if (size
<= 0xffffffff)
5061 sprintf (buf
, "%lx", (unsigned long) size
);
5063 sprintf_vma (buf
, size
);
5073 minfo ("%B\n", section
->owner
);
5079 /* Run through the input files and ensure that every input section has
5080 somewhere to go. If one is found without a destination then create
5081 an input request and place it into the statement tree. */
5084 lang_place_orphans (void)
5086 LANG_FOR_EACH_INPUT_STATEMENT (file
)
5090 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
5092 if (s
->output_section
== NULL
)
5094 /* This section of the file is not attached, root
5095 around for a sensible place for it to go. */
5097 if (file
->just_syms_flag
)
5098 bfd_link_just_syms (file
->the_bfd
, s
, &link_info
);
5099 else if ((s
->flags
& SEC_EXCLUDE
) != 0)
5100 s
->output_section
= bfd_abs_section_ptr
;
5101 else if (strcmp (s
->name
, "COMMON") == 0)
5103 /* This is a lonely common section which must have
5104 come from an archive. We attach to the section
5105 with the wildcard. */
5106 if (! link_info
.relocatable
5107 || command_line
.force_common_definition
)
5109 if (default_common_section
== NULL
)
5111 default_common_section
=
5112 lang_output_section_statement_lookup (".bss");
5115 lang_add_section (&default_common_section
->children
, s
,
5116 default_common_section
);
5119 else if (ldemul_place_orphan (s
))
5123 lang_output_section_statement_type
*os
;
5125 os
= lang_output_section_statement_lookup (s
->name
);
5126 lang_add_section (&os
->children
, s
, os
);
5134 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
5136 flagword
*ptr_flags
;
5138 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
5144 *ptr_flags
|= SEC_ALLOC
;
5148 *ptr_flags
|= SEC_READONLY
;
5152 *ptr_flags
|= SEC_DATA
;
5156 *ptr_flags
|= SEC_CODE
;
5161 *ptr_flags
|= SEC_LOAD
;
5165 einfo (_("%P%F: invalid syntax in flags\n"));
5172 /* Call a function on each input file. This function will be called
5173 on an archive, but not on the elements. */
5176 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
5178 lang_input_statement_type
*f
;
5180 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
5182 f
= (lang_input_statement_type
*) f
->next_real_file
)
5186 /* Call a function on each file. The function will be called on all
5187 the elements of an archive which are included in the link, but will
5188 not be called on the archive file itself. */
5191 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
5193 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5200 ldlang_add_file (lang_input_statement_type
*entry
)
5204 lang_statement_append (&file_chain
,
5205 (lang_statement_union_type
*) entry
,
5208 /* The BFD linker needs to have a list of all input BFDs involved in
5210 ASSERT (entry
->the_bfd
->link_next
== NULL
);
5211 ASSERT (entry
->the_bfd
!= output_bfd
);
5212 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
5214 *pp
= entry
->the_bfd
;
5215 entry
->the_bfd
->usrdata
= entry
;
5216 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
5218 /* Look through the sections and check for any which should not be
5219 included in the link. We need to do this now, so that we can
5220 notice when the backend linker tries to report multiple
5221 definition errors for symbols which are in sections we aren't
5222 going to link. FIXME: It might be better to entirely ignore
5223 symbols which are defined in sections which are going to be
5224 discarded. This would require modifying the backend linker for
5225 each backend which might set the SEC_LINK_ONCE flag. If we do
5226 this, we should probably handle SEC_EXCLUDE in the same way. */
5228 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
5232 lang_add_output (const char *name
, int from_script
)
5234 /* Make -o on command line override OUTPUT in script. */
5235 if (!had_output_filename
|| !from_script
)
5237 output_filename
= name
;
5238 had_output_filename
= TRUE
;
5242 static lang_output_section_statement_type
*current_section
;
5253 for (l
= 0; l
< 32; l
++)
5255 if (i
>= (unsigned int) x
)
5263 lang_output_section_statement_type
*
5264 lang_enter_output_section_statement (const char *output_section_statement_name
,
5265 etree_type
*address_exp
,
5266 enum section_type sectype
,
5268 etree_type
*subalign
,
5272 lang_output_section_statement_type
*os
;
5274 os
= lang_output_section_statement_lookup_1 (output_section_statement_name
,
5276 current_section
= os
;
5278 /* Make next things chain into subchain of this. */
5280 if (os
->addr_tree
== NULL
)
5282 os
->addr_tree
= address_exp
;
5284 os
->sectype
= sectype
;
5285 if (sectype
!= noload_section
)
5286 os
->flags
= SEC_NO_FLAGS
;
5288 os
->flags
= SEC_NEVER_LOAD
;
5289 os
->block_value
= 1;
5290 stat_ptr
= &os
->children
;
5292 os
->subsection_alignment
=
5293 topower (exp_get_value_int (subalign
, -1, "subsection alignment"));
5294 os
->section_alignment
=
5295 topower (exp_get_value_int (align
, -1, "section alignment"));
5297 os
->load_base
= ebase
;
5304 lang_output_statement_type
*new;
5306 new = new_stat (lang_output_statement
, stat_ptr
);
5307 new->name
= output_filename
;
5310 /* Reset the current counters in the regions. */
5313 lang_reset_memory_regions (void)
5315 lang_memory_region_type
*p
= lang_memory_region_list
;
5317 lang_output_section_statement_type
*os
;
5319 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
5321 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
5322 p
->current
= p
->origin
;
5325 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
5328 os
->processed
= FALSE
;
5330 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5332 /* Save the last size for possible use by bfd_relax_section. */
5333 o
->rawsize
= o
->size
;
5338 /* Worker for lang_gc_sections_1. */
5341 gc_section_callback (lang_wild_statement_type
*ptr
,
5342 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
5344 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
5345 void *data ATTRIBUTE_UNUSED
)
5347 /* If the wild pattern was marked KEEP, the member sections
5348 should be as well. */
5349 if (ptr
->keep_sections
)
5350 section
->flags
|= SEC_KEEP
;
5353 /* Iterate over sections marking them against GC. */
5356 lang_gc_sections_1 (lang_statement_union_type
*s
)
5358 for (; s
!= NULL
; s
= s
->header
.next
)
5360 switch (s
->header
.type
)
5362 case lang_wild_statement_enum
:
5363 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
5365 case lang_constructors_statement_enum
:
5366 lang_gc_sections_1 (constructor_list
.head
);
5368 case lang_output_section_statement_enum
:
5369 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
5371 case lang_group_statement_enum
:
5372 lang_gc_sections_1 (s
->group_statement
.children
.head
);
5381 lang_gc_sections (void)
5383 struct bfd_link_hash_entry
*h
;
5384 ldlang_undef_chain_list_type
*ulist
;
5386 /* Keep all sections so marked in the link script. */
5388 lang_gc_sections_1 (statement_list
.head
);
5390 /* Keep all sections containing symbols undefined on the command-line,
5391 and the section containing the entry symbol. */
5393 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
5395 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
5396 FALSE
, FALSE
, FALSE
);
5399 && (h
->type
== bfd_link_hash_defined
5400 || h
->type
== bfd_link_hash_defweak
)
5401 && ! bfd_is_abs_section (h
->u
.def
.section
))
5403 h
->u
.def
.section
->flags
|= SEC_KEEP
;
5407 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
5408 the special case of debug info. (See bfd/stabs.c)
5409 Twiddle the flag here, to simplify later linker code. */
5410 if (link_info
.relocatable
)
5412 LANG_FOR_EACH_INPUT_STATEMENT (f
)
5415 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
5416 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
5417 sec
->flags
&= ~SEC_EXCLUDE
;
5421 if (link_info
.gc_sections
)
5422 bfd_gc_sections (output_bfd
, &link_info
);
5425 /* Relax all sections until bfd_relax_section gives up. */
5428 relax_sections (void)
5430 /* Keep relaxing until bfd_relax_section gives up. */
5431 bfd_boolean relax_again
;
5435 relax_again
= FALSE
;
5437 /* Note: pe-dll.c does something like this also. If you find
5438 you need to change this code, you probably need to change
5439 pe-dll.c also. DJ */
5441 /* Do all the assignments with our current guesses as to
5443 lang_do_assignments ();
5445 /* We must do this after lang_do_assignments, because it uses
5447 lang_reset_memory_regions ();
5449 /* Perform another relax pass - this time we know where the
5450 globals are, so can make a better guess. */
5451 lang_size_sections (&relax_again
, FALSE
);
5453 while (relax_again
);
5459 current_target
= default_target
;
5461 /* Open the output file. */
5462 lang_for_each_statement (ldlang_open_output
);
5465 ldemul_create_output_section_statements ();
5467 /* Add to the hash table all undefineds on the command line. */
5468 lang_place_undefineds ();
5470 if (!bfd_section_already_linked_table_init ())
5471 einfo (_("%P%F: Failed to create hash table\n"));
5473 /* Create a bfd for each input file. */
5474 current_target
= default_target
;
5475 open_input_bfds (statement_list
.head
, FALSE
);
5477 link_info
.gc_sym_list
= &entry_symbol
;
5478 if (entry_symbol
.name
== NULL
)
5479 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
5481 ldemul_after_open ();
5483 bfd_section_already_linked_table_free ();
5485 /* Make sure that we're not mixing architectures. We call this
5486 after all the input files have been opened, but before we do any
5487 other processing, so that any operations merge_private_bfd_data
5488 does on the output file will be known during the rest of the
5492 /* Handle .exports instead of a version script if we're told to do so. */
5493 if (command_line
.version_exports_section
)
5494 lang_do_version_exports_section ();
5496 /* Build all sets based on the information gathered from the input
5498 ldctor_build_sets ();
5500 /* Remove unreferenced sections if asked to. */
5501 lang_gc_sections ();
5503 /* Size up the common data. */
5506 /* Update wild statements. */
5507 update_wild_statements (statement_list
.head
);
5509 /* Run through the contours of the script and attach input sections
5510 to the correct output sections. */
5511 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
5513 /* Find any sections not attached explicitly and handle them. */
5514 lang_place_orphans ();
5516 if (! link_info
.relocatable
)
5520 /* Merge SEC_MERGE sections. This has to be done after GC of
5521 sections, so that GCed sections are not merged, but before
5522 assigning dynamic symbols, since removing whole input sections
5524 bfd_merge_sections (output_bfd
, &link_info
);
5526 /* Look for a text section and set the readonly attribute in it. */
5527 found
= bfd_get_section_by_name (output_bfd
, ".text");
5531 if (config
.text_read_only
)
5532 found
->flags
|= SEC_READONLY
;
5534 found
->flags
&= ~SEC_READONLY
;
5538 /* Do anything special before sizing sections. This is where ELF
5539 and other back-ends size dynamic sections. */
5540 ldemul_before_allocation ();
5542 /* We must record the program headers before we try to fix the
5543 section positions, since they will affect SIZEOF_HEADERS. */
5544 lang_record_phdrs ();
5546 /* Size up the sections. */
5547 lang_size_sections (NULL
, !command_line
.relax
);
5549 /* Now run around and relax if we can. */
5550 if (command_line
.relax
)
5552 /* We may need more than one relaxation pass. */
5553 int i
= link_info
.relax_pass
;
5555 /* The backend can use it to determine the current pass. */
5556 link_info
.relax_pass
= 0;
5561 link_info
.relax_pass
++;
5564 /* Final extra sizing to report errors. */
5565 lang_do_assignments ();
5566 lang_reset_memory_regions ();
5567 lang_size_sections (NULL
, TRUE
);
5570 /* See if anything special should be done now we know how big
5572 ldemul_after_allocation ();
5574 /* Fix any .startof. or .sizeof. symbols. */
5575 lang_set_startof ();
5577 /* Do all the assignments, now that we know the final resting places
5578 of all the symbols. */
5580 lang_do_assignments ();
5584 /* Make sure that the section addresses make sense. */
5585 if (! link_info
.relocatable
5586 && command_line
.check_section_addresses
)
5587 lang_check_section_addresses ();
5592 /* EXPORTED TO YACC */
5595 lang_add_wild (struct wildcard_spec
*filespec
,
5596 struct wildcard_list
*section_list
,
5597 bfd_boolean keep_sections
)
5599 struct wildcard_list
*curr
, *next
;
5600 lang_wild_statement_type
*new;
5602 /* Reverse the list as the parser puts it back to front. */
5603 for (curr
= section_list
, section_list
= NULL
;
5605 section_list
= curr
, curr
= next
)
5607 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
5608 placed_commons
= TRUE
;
5611 curr
->next
= section_list
;
5614 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
5616 if (strcmp (filespec
->name
, "*") == 0)
5617 filespec
->name
= NULL
;
5618 else if (! wildcardp (filespec
->name
))
5619 lang_has_input_file
= TRUE
;
5622 new = new_stat (lang_wild_statement
, stat_ptr
);
5623 new->filename
= NULL
;
5624 new->filenames_sorted
= FALSE
;
5625 if (filespec
!= NULL
)
5627 new->filename
= filespec
->name
;
5628 new->filenames_sorted
= filespec
->sorted
== by_name
;
5630 new->section_list
= section_list
;
5631 new->keep_sections
= keep_sections
;
5632 lang_list_init (&new->children
);
5633 analyze_walk_wild_section_handler (new);
5637 lang_section_start (const char *name
, etree_type
*address
,
5638 const segment_type
*segment
)
5640 lang_address_statement_type
*ad
;
5642 ad
= new_stat (lang_address_statement
, stat_ptr
);
5643 ad
->section_name
= name
;
5644 ad
->address
= address
;
5645 ad
->segment
= segment
;
5648 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5649 because of a -e argument on the command line, or zero if this is
5650 called by ENTRY in a linker script. Command line arguments take
5654 lang_add_entry (const char *name
, bfd_boolean cmdline
)
5656 if (entry_symbol
.name
== NULL
5658 || ! entry_from_cmdline
)
5660 entry_symbol
.name
= name
;
5661 entry_from_cmdline
= cmdline
;
5665 /* Set the default start symbol to NAME. .em files should use this,
5666 not lang_add_entry, to override the use of "start" if neither the
5667 linker script nor the command line specifies an entry point. NAME
5668 must be permanently allocated. */
5670 lang_default_entry (const char *name
)
5672 entry_symbol_default
= name
;
5676 lang_add_target (const char *name
)
5678 lang_target_statement_type
*new;
5680 new = new_stat (lang_target_statement
, stat_ptr
);
5685 lang_add_map (const char *name
)
5692 map_option_f
= TRUE
;
5700 lang_add_fill (fill_type
*fill
)
5702 lang_fill_statement_type
*new;
5704 new = new_stat (lang_fill_statement
, stat_ptr
);
5709 lang_add_data (int type
, union etree_union
*exp
)
5711 lang_data_statement_type
*new;
5713 new = new_stat (lang_data_statement
, stat_ptr
);
5718 /* Create a new reloc statement. RELOC is the BFD relocation type to
5719 generate. HOWTO is the corresponding howto structure (we could
5720 look this up, but the caller has already done so). SECTION is the
5721 section to generate a reloc against, or NAME is the name of the
5722 symbol to generate a reloc against. Exactly one of SECTION and
5723 NAME must be NULL. ADDEND is an expression for the addend. */
5726 lang_add_reloc (bfd_reloc_code_real_type reloc
,
5727 reloc_howto_type
*howto
,
5730 union etree_union
*addend
)
5732 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
5736 p
->section
= section
;
5738 p
->addend_exp
= addend
;
5740 p
->addend_value
= 0;
5741 p
->output_section
= NULL
;
5742 p
->output_offset
= 0;
5745 lang_assignment_statement_type
*
5746 lang_add_assignment (etree_type
*exp
)
5748 lang_assignment_statement_type
*new;
5750 new = new_stat (lang_assignment_statement
, stat_ptr
);
5756 lang_add_attribute (enum statement_enum attribute
)
5758 new_statement (attribute
, sizeof (lang_statement_header_type
), stat_ptr
);
5762 lang_startup (const char *name
)
5764 if (startup_file
!= NULL
)
5766 einfo (_("%P%F: multiple STARTUP files\n"));
5768 first_file
->filename
= name
;
5769 first_file
->local_sym_name
= name
;
5770 first_file
->real
= TRUE
;
5772 startup_file
= name
;
5776 lang_float (bfd_boolean maybe
)
5778 lang_float_flag
= maybe
;
5782 /* Work out the load- and run-time regions from a script statement, and
5783 store them in *LMA_REGION and *REGION respectively.
5785 MEMSPEC is the name of the run-time region, or the value of
5786 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5787 LMA_MEMSPEC is the name of the load-time region, or null if the
5788 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5789 had an explicit load address.
5791 It is an error to specify both a load region and a load address. */
5794 lang_get_regions (lang_memory_region_type
**region
,
5795 lang_memory_region_type
**lma_region
,
5796 const char *memspec
,
5797 const char *lma_memspec
,
5798 bfd_boolean have_lma
,
5799 bfd_boolean have_vma
)
5801 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
5803 /* If no runtime region or VMA has been specified, but the load region
5804 has been specified, then use the load region for the runtime region
5806 if (lma_memspec
!= NULL
5808 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
5809 *region
= *lma_region
;
5811 *region
= lang_memory_region_lookup (memspec
, FALSE
);
5813 if (have_lma
&& lma_memspec
!= 0)
5814 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5818 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
5819 lang_output_section_phdr_list
*phdrs
,
5820 const char *lma_memspec
)
5822 lang_get_regions (¤t_section
->region
,
5823 ¤t_section
->lma_region
,
5824 memspec
, lma_memspec
,
5825 current_section
->load_base
!= NULL
,
5826 current_section
->addr_tree
!= NULL
);
5827 current_section
->fill
= fill
;
5828 current_section
->phdrs
= phdrs
;
5829 stat_ptr
= &statement_list
;
5832 /* Create an absolute symbol with the given name with the value of the
5833 address of first byte of the section named.
5835 If the symbol already exists, then do nothing. */
5838 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
5840 struct bfd_link_hash_entry
*h
;
5842 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5844 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5846 if (h
->type
== bfd_link_hash_new
5847 || h
->type
== bfd_link_hash_undefined
)
5851 h
->type
= bfd_link_hash_defined
;
5853 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5857 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
5859 h
->u
.def
.section
= bfd_abs_section_ptr
;
5863 /* Create an absolute symbol with the given name with the value of the
5864 address of the first byte after the end of the section named.
5866 If the symbol already exists, then do nothing. */
5869 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
5871 struct bfd_link_hash_entry
*h
;
5873 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
5875 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5877 if (h
->type
== bfd_link_hash_new
5878 || h
->type
== bfd_link_hash_undefined
)
5882 h
->type
= bfd_link_hash_defined
;
5884 sec
= bfd_get_section_by_name (output_bfd
, secname
);
5888 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
5889 + TO_ADDR (sec
->size
));
5891 h
->u
.def
.section
= bfd_abs_section_ptr
;
5896 lang_statement_append (lang_statement_list_type
*list
,
5897 lang_statement_union_type
*element
,
5898 lang_statement_union_type
**field
)
5900 *(list
->tail
) = element
;
5904 /* Set the output format type. -oformat overrides scripts. */
5907 lang_add_output_format (const char *format
,
5912 if (output_target
== NULL
|| !from_script
)
5914 if (command_line
.endian
== ENDIAN_BIG
5917 else if (command_line
.endian
== ENDIAN_LITTLE
5921 output_target
= format
;
5925 /* Enter a group. This creates a new lang_group_statement, and sets
5926 stat_ptr to build new statements within the group. */
5929 lang_enter_group (void)
5931 lang_group_statement_type
*g
;
5933 g
= new_stat (lang_group_statement
, stat_ptr
);
5934 lang_list_init (&g
->children
);
5935 stat_ptr
= &g
->children
;
5938 /* Leave a group. This just resets stat_ptr to start writing to the
5939 regular list of statements again. Note that this will not work if
5940 groups can occur inside anything else which can adjust stat_ptr,
5941 but currently they can't. */
5944 lang_leave_group (void)
5946 stat_ptr
= &statement_list
;
5949 /* Add a new program header. This is called for each entry in a PHDRS
5950 command in a linker script. */
5953 lang_new_phdr (const char *name
,
5955 bfd_boolean filehdr
,
5960 struct lang_phdr
*n
, **pp
;
5962 n
= stat_alloc (sizeof (struct lang_phdr
));
5965 n
->type
= exp_get_value_int (type
, 0, "program header type");
5966 n
->filehdr
= filehdr
;
5971 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5976 /* Record the program header information in the output BFD. FIXME: We
5977 should not be calling an ELF specific function here. */
5980 lang_record_phdrs (void)
5984 lang_output_section_phdr_list
*last
;
5985 struct lang_phdr
*l
;
5986 lang_output_section_statement_type
*os
;
5989 secs
= xmalloc (alc
* sizeof (asection
*));
5991 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
5998 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6002 lang_output_section_phdr_list
*pl
;
6004 if (os
->constraint
== -1)
6012 if (os
->sectype
== noload_section
6013 || os
->bfd_section
== NULL
6014 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
6019 if (os
->bfd_section
== NULL
)
6022 for (; pl
!= NULL
; pl
= pl
->next
)
6024 if (strcmp (pl
->name
, l
->name
) == 0)
6029 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
6031 secs
[c
] = os
->bfd_section
;
6038 if (l
->flags
== NULL
)
6041 flags
= exp_get_vma (l
->flags
, 0, "phdr flags");
6046 at
= exp_get_vma (l
->at
, 0, "phdr load address");
6048 if (! bfd_record_phdr (output_bfd
, l
->type
,
6049 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
6050 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
6051 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
6056 /* Make sure all the phdr assignments succeeded. */
6057 for (os
= &lang_output_section_statement
.head
->output_section_statement
;
6061 lang_output_section_phdr_list
*pl
;
6063 if (os
->constraint
== -1
6064 || os
->bfd_section
== NULL
)
6067 for (pl
= os
->phdrs
;
6070 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
6071 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
6072 os
->name
, pl
->name
);
6076 /* Record a list of sections which may not be cross referenced. */
6079 lang_add_nocrossref (lang_nocrossref_type
*l
)
6081 struct lang_nocrossrefs
*n
;
6083 n
= xmalloc (sizeof *n
);
6084 n
->next
= nocrossref_list
;
6086 nocrossref_list
= n
;
6088 /* Set notice_all so that we get informed about all symbols. */
6089 link_info
.notice_all
= TRUE
;
6092 /* Overlay handling. We handle overlays with some static variables. */
6094 /* The overlay virtual address. */
6095 static etree_type
*overlay_vma
;
6096 /* And subsection alignment. */
6097 static etree_type
*overlay_subalign
;
6099 /* An expression for the maximum section size seen so far. */
6100 static etree_type
*overlay_max
;
6102 /* A list of all the sections in this overlay. */
6104 struct overlay_list
{
6105 struct overlay_list
*next
;
6106 lang_output_section_statement_type
*os
;
6109 static struct overlay_list
*overlay_list
;
6111 /* Start handling an overlay. */
6114 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
6116 /* The grammar should prevent nested overlays from occurring. */
6117 ASSERT (overlay_vma
== NULL
6118 && overlay_subalign
== NULL
6119 && overlay_max
== NULL
);
6121 overlay_vma
= vma_expr
;
6122 overlay_subalign
= subalign
;
6125 /* Start a section in an overlay. We handle this by calling
6126 lang_enter_output_section_statement with the correct VMA.
6127 lang_leave_overlay sets up the LMA and memory regions. */
6130 lang_enter_overlay_section (const char *name
)
6132 struct overlay_list
*n
;
6135 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
6136 0, overlay_subalign
, 0, 0);
6138 /* If this is the first section, then base the VMA of future
6139 sections on this one. This will work correctly even if `.' is
6140 used in the addresses. */
6141 if (overlay_list
== NULL
)
6142 overlay_vma
= exp_nameop (ADDR
, name
);
6144 /* Remember the section. */
6145 n
= xmalloc (sizeof *n
);
6146 n
->os
= current_section
;
6147 n
->next
= overlay_list
;
6150 size
= exp_nameop (SIZEOF
, name
);
6152 /* Arrange to work out the maximum section end address. */
6153 if (overlay_max
== NULL
)
6156 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
6159 /* Finish a section in an overlay. There isn't any special to do
6163 lang_leave_overlay_section (fill_type
*fill
,
6164 lang_output_section_phdr_list
*phdrs
)
6171 name
= current_section
->name
;
6173 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
6174 region and that no load-time region has been specified. It doesn't
6175 really matter what we say here, since lang_leave_overlay will
6177 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
6179 /* Define the magic symbols. */
6181 clean
= xmalloc (strlen (name
) + 1);
6183 for (s1
= name
; *s1
!= '\0'; s1
++)
6184 if (ISALNUM (*s1
) || *s1
== '_')
6188 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
6189 sprintf (buf
, "__load_start_%s", clean
);
6190 lang_add_assignment (exp_assop ('=', buf
,
6191 exp_nameop (LOADADDR
, name
)));
6193 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
6194 sprintf (buf
, "__load_stop_%s", clean
);
6195 lang_add_assignment (exp_assop ('=', buf
,
6197 exp_nameop (LOADADDR
, name
),
6198 exp_nameop (SIZEOF
, name
))));
6203 /* Finish an overlay. If there are any overlay wide settings, this
6204 looks through all the sections in the overlay and sets them. */
6207 lang_leave_overlay (etree_type
*lma_expr
,
6210 const char *memspec
,
6211 lang_output_section_phdr_list
*phdrs
,
6212 const char *lma_memspec
)
6214 lang_memory_region_type
*region
;
6215 lang_memory_region_type
*lma_region
;
6216 struct overlay_list
*l
;
6217 lang_nocrossref_type
*nocrossref
;
6219 lang_get_regions (®ion
, &lma_region
,
6220 memspec
, lma_memspec
,
6221 lma_expr
!= NULL
, FALSE
);
6225 /* After setting the size of the last section, set '.' to end of the
6227 if (overlay_list
!= NULL
)
6228 overlay_list
->os
->update_dot_tree
6229 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
6234 struct overlay_list
*next
;
6236 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
6239 l
->os
->region
= region
;
6240 l
->os
->lma_region
= lma_region
;
6242 /* The first section has the load address specified in the
6243 OVERLAY statement. The rest are worked out from that.
6244 The base address is not needed (and should be null) if
6245 an LMA region was specified. */
6247 l
->os
->load_base
= lma_expr
;
6248 else if (lma_region
== 0)
6249 l
->os
->load_base
= exp_binop ('+',
6250 exp_nameop (LOADADDR
, l
->next
->os
->name
),
6251 exp_nameop (SIZEOF
, l
->next
->os
->name
));
6253 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
6254 l
->os
->phdrs
= phdrs
;
6258 lang_nocrossref_type
*nc
;
6260 nc
= xmalloc (sizeof *nc
);
6261 nc
->name
= l
->os
->name
;
6262 nc
->next
= nocrossref
;
6271 if (nocrossref
!= NULL
)
6272 lang_add_nocrossref (nocrossref
);
6275 overlay_list
= NULL
;
6279 /* Version handling. This is only useful for ELF. */
6281 /* This global variable holds the version tree that we build. */
6283 struct bfd_elf_version_tree
*lang_elf_version_info
;
6285 /* If PREV is NULL, return first version pattern matching particular symbol.
6286 If PREV is non-NULL, return first version pattern matching particular
6287 symbol after PREV (previously returned by lang_vers_match). */
6289 static struct bfd_elf_version_expr
*
6290 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
6291 struct bfd_elf_version_expr
*prev
,
6294 const char *cxx_sym
= sym
;
6295 const char *java_sym
= sym
;
6296 struct bfd_elf_version_expr
*expr
= NULL
;
6298 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6300 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
6304 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6306 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
6311 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
6313 struct bfd_elf_version_expr e
;
6315 switch (prev
? prev
->mask
: 0)
6318 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
6321 expr
= htab_find (head
->htab
, &e
);
6322 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
6323 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
6329 case BFD_ELF_VERSION_C_TYPE
:
6330 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
6333 expr
= htab_find (head
->htab
, &e
);
6334 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
6335 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6341 case BFD_ELF_VERSION_CXX_TYPE
:
6342 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
6344 e
.symbol
= java_sym
;
6345 expr
= htab_find (head
->htab
, &e
);
6346 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
6347 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6358 /* Finally, try the wildcards. */
6359 if (prev
== NULL
|| prev
->symbol
)
6360 expr
= head
->remaining
;
6363 for (; expr
; expr
= expr
->next
)
6370 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
6373 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
6375 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
6379 if (fnmatch (expr
->pattern
, s
, 0) == 0)
6385 free ((char *) cxx_sym
);
6386 if (java_sym
!= sym
)
6387 free ((char *) java_sym
);
6391 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
6392 return a string pointing to the symbol name. */
6395 realsymbol (const char *pattern
)
6398 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
6399 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
6401 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
6403 /* It is a glob pattern only if there is no preceding
6405 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
6413 /* Remove the preceding backslash. */
6420 backslash
= *p
== '\\';
6435 /* This is called for each variable name or match expression. NEW is
6436 the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
6437 pattern to be matched against symbol names. */
6439 struct bfd_elf_version_expr
*
6440 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
6443 bfd_boolean literal_p
)
6445 struct bfd_elf_version_expr
*ret
;
6447 ret
= xmalloc (sizeof *ret
);
6449 ret
->pattern
= literal_p
? NULL
: new;
6452 ret
->symbol
= literal_p
? new : realsymbol (new);
6454 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
6455 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6456 else if (strcasecmp (lang
, "C++") == 0)
6457 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
6458 else if (strcasecmp (lang
, "Java") == 0)
6459 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
6462 einfo (_("%X%P: unknown language `%s' in version information\n"),
6464 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
6467 return ldemul_new_vers_pattern (ret
);
6470 /* This is called for each set of variable names and match
6473 struct bfd_elf_version_tree
*
6474 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
6475 struct bfd_elf_version_expr
*locals
)
6477 struct bfd_elf_version_tree
*ret
;
6479 ret
= xcalloc (1, sizeof *ret
);
6480 ret
->globals
.list
= globals
;
6481 ret
->locals
.list
= locals
;
6482 ret
->match
= lang_vers_match
;
6483 ret
->name_indx
= (unsigned int) -1;
6487 /* This static variable keeps track of version indices. */
6489 static int version_index
;
6492 version_expr_head_hash (const void *p
)
6494 const struct bfd_elf_version_expr
*e
= p
;
6496 return htab_hash_string (e
->symbol
);
6500 version_expr_head_eq (const void *p1
, const void *p2
)
6502 const struct bfd_elf_version_expr
*e1
= p1
;
6503 const struct bfd_elf_version_expr
*e2
= p2
;
6505 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
6509 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
6512 struct bfd_elf_version_expr
*e
, *next
;
6513 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
6515 for (e
= head
->list
; e
; e
= e
->next
)
6519 head
->mask
|= e
->mask
;
6524 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
6525 version_expr_head_eq
, NULL
);
6526 list_loc
= &head
->list
;
6527 remaining_loc
= &head
->remaining
;
6528 for (e
= head
->list
; e
; e
= next
)
6534 remaining_loc
= &e
->next
;
6538 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
6542 struct bfd_elf_version_expr
*e1
, *last
;
6548 if (e1
->mask
== e
->mask
)
6556 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
6560 /* This is a duplicate. */
6561 /* FIXME: Memory leak. Sometimes pattern is not
6562 xmalloced alone, but in larger chunk of memory. */
6563 /* free (e->symbol); */
6568 e
->next
= last
->next
;
6576 list_loc
= &e
->next
;
6580 *remaining_loc
= NULL
;
6581 *list_loc
= head
->remaining
;
6584 head
->remaining
= head
->list
;
6587 /* This is called when we know the name and dependencies of the
6591 lang_register_vers_node (const char *name
,
6592 struct bfd_elf_version_tree
*version
,
6593 struct bfd_elf_version_deps
*deps
)
6595 struct bfd_elf_version_tree
*t
, **pp
;
6596 struct bfd_elf_version_expr
*e1
;
6601 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
6602 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
6604 einfo (_("%X%P: anonymous version tag cannot be combined"
6605 " with other version tags\n"));
6610 /* Make sure this node has a unique name. */
6611 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6612 if (strcmp (t
->name
, name
) == 0)
6613 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
6615 lang_finalize_version_expr_head (&version
->globals
);
6616 lang_finalize_version_expr_head (&version
->locals
);
6618 /* Check the global and local match names, and make sure there
6619 aren't any duplicates. */
6621 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
6623 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6625 struct bfd_elf_version_expr
*e2
;
6627 if (t
->locals
.htab
&& e1
->symbol
)
6629 e2
= htab_find (t
->locals
.htab
, e1
);
6630 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6632 if (e1
->mask
== e2
->mask
)
6633 einfo (_("%X%P: duplicate expression `%s'"
6634 " in version information\n"), e1
->symbol
);
6638 else if (!e1
->symbol
)
6639 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6640 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6641 && e1
->mask
== e2
->mask
)
6642 einfo (_("%X%P: duplicate expression `%s'"
6643 " in version information\n"), e1
->pattern
);
6647 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
6649 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6651 struct bfd_elf_version_expr
*e2
;
6653 if (t
->globals
.htab
&& e1
->symbol
)
6655 e2
= htab_find (t
->globals
.htab
, e1
);
6656 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
6658 if (e1
->mask
== e2
->mask
)
6659 einfo (_("%X%P: duplicate expression `%s'"
6660 " in version information\n"),
6665 else if (!e1
->symbol
)
6666 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
6667 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
6668 && e1
->mask
== e2
->mask
)
6669 einfo (_("%X%P: duplicate expression `%s'"
6670 " in version information\n"), e1
->pattern
);
6674 version
->deps
= deps
;
6675 version
->name
= name
;
6676 if (name
[0] != '\0')
6679 version
->vernum
= version_index
;
6682 version
->vernum
= 0;
6684 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
6689 /* This is called when we see a version dependency. */
6691 struct bfd_elf_version_deps
*
6692 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
6694 struct bfd_elf_version_deps
*ret
;
6695 struct bfd_elf_version_tree
*t
;
6697 ret
= xmalloc (sizeof *ret
);
6700 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
6702 if (strcmp (t
->name
, name
) == 0)
6704 ret
->version_needed
= t
;
6709 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
6715 lang_do_version_exports_section (void)
6717 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
6719 LANG_FOR_EACH_INPUT_STATEMENT (is
)
6721 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
6729 contents
= xmalloc (len
);
6730 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
6731 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
6734 while (p
< contents
+ len
)
6736 greg
= lang_new_vers_pattern (greg
, p
, NULL
, FALSE
);
6737 p
= strchr (p
, '\0') + 1;
6740 /* Do not free the contents, as we used them creating the regex. */
6742 /* Do not include this section in the link. */
6743 sec
->flags
|= SEC_EXCLUDE
;
6746 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
, FALSE
);
6747 lang_register_vers_node (command_line
.version_exports_section
,
6748 lang_new_vers_node (greg
, lreg
), NULL
);
6752 lang_add_unique (const char *name
)
6754 struct unique_sections
*ent
;
6756 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
6757 if (strcmp (ent
->name
, name
) == 0)
6760 ent
= xmalloc (sizeof *ent
);
6761 ent
->name
= xstrdup (name
);
6762 ent
->next
= unique_section_list
;
6763 unique_section_list
= ent
;