1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
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, 59 Temple Place - Suite 330, 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
;
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static const char *startup_file
;
54 static lang_statement_list_type input_file_chain
;
55 static bfd_boolean placed_commons
= FALSE
;
56 static lang_output_section_statement_type
*default_common_section
;
57 static bfd_boolean map_option_f
;
58 static bfd_vma print_dot
;
59 static lang_input_statement_type
*first_file
;
60 static const char *current_target
;
61 static const char *output_target
;
62 static lang_statement_list_type statement_list
;
63 static struct lang_phdr
*lang_phdr_list
;
64 static struct bfd_hash_table lang_definedness_table
;
66 /* Forward declarations. */
67 static void exp_init_os (etree_type
*);
68 static bfd_boolean
wildcardp (const char *);
69 static lang_input_statement_type
*lookup_name (const char *);
70 static bfd_boolean
load_symbols (lang_input_statement_type
*,
71 lang_statement_list_type
*);
72 static struct bfd_hash_entry
*lang_definedness_newfunc
73 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
74 static void insert_undefined (const char *);
75 static void print_statement (lang_statement_union_type
*,
76 lang_output_section_statement_type
*);
77 static void print_statement_list (lang_statement_union_type
*,
78 lang_output_section_statement_type
*);
79 static void print_statements (void);
80 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
81 static void lang_record_phdrs (void);
82 static void lang_do_version_exports_section (void);
84 typedef void (*callback_t
) (lang_wild_statement_type
*, struct wildcard_list
*,
85 asection
*, lang_input_statement_type
*, 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 const char *entry_section
= ".text";
94 bfd_boolean entry_from_cmdline
;
95 bfd_boolean lang_has_input_file
= FALSE
;
96 bfd_boolean had_output_filename
= FALSE
;
97 bfd_boolean lang_float_flag
= FALSE
;
98 bfd_boolean delete_output_file_on_failure
= FALSE
;
99 struct lang_nocrossrefs
*nocrossref_list
;
100 struct unique_sections
*unique_section_list
;
101 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
102 int lang_statement_iteration
= 0;
104 etree_type
*base
; /* Relocation base - or null */
106 #define new_stat(x, y) \
107 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
109 #define outside_section_address(q) \
110 ((q)->output_offset + (q)->output_section->vma)
112 #define outside_symbol_address(q) \
113 ((q)->value + outside_section_address (q->section))
115 #define SECTION_NAME_MAP_LENGTH (16)
118 stat_alloc (size_t size
)
120 return obstack_alloc (&stat_obstack
, size
);
124 unique_section_p (const char *secnam
)
126 struct unique_sections
*unam
;
128 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
129 if (wildcardp (unam
->name
)
130 ? fnmatch (unam
->name
, secnam
, 0) == 0
131 : strcmp (unam
->name
, secnam
) == 0)
139 /* Generic traversal routines for finding matching sections. */
142 walk_wild_section (lang_wild_statement_type
*ptr
,
143 lang_input_statement_type
*file
,
149 if (file
->just_syms_flag
)
152 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
154 struct wildcard_list
*sec
;
156 sec
= ptr
->section_list
;
158 (*callback
) (ptr
, sec
, s
, file
, data
);
162 bfd_boolean skip
= FALSE
;
163 struct name_list
*list_tmp
;
165 /* Don't process sections from files which were
167 for (list_tmp
= sec
->spec
.exclude_name_list
;
169 list_tmp
= list_tmp
->next
)
171 if (wildcardp (list_tmp
->name
))
172 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
174 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
176 /* If this file is part of an archive, and the archive is
177 excluded, exclude this file. */
178 if (! skip
&& file
->the_bfd
!= NULL
179 && file
->the_bfd
->my_archive
!= NULL
180 && file
->the_bfd
->my_archive
->filename
!= NULL
)
182 if (wildcardp (list_tmp
->name
))
183 skip
= fnmatch (list_tmp
->name
,
184 file
->the_bfd
->my_archive
->filename
,
187 skip
= strcmp (list_tmp
->name
,
188 file
->the_bfd
->my_archive
->filename
) == 0;
195 if (!skip
&& sec
->spec
.name
!= NULL
)
197 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
199 if (wildcardp (sec
->spec
.name
))
200 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
202 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
206 (*callback
) (ptr
, sec
, s
, file
, data
);
213 /* Handle a wild statement for a single file F. */
216 walk_wild_file (lang_wild_statement_type
*s
,
217 lang_input_statement_type
*f
,
221 if (f
->the_bfd
== NULL
222 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
223 walk_wild_section (s
, f
, callback
, data
);
228 /* This is an archive file. We must map each member of the
229 archive separately. */
230 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
231 while (member
!= NULL
)
233 /* When lookup_name is called, it will call the add_symbols
234 entry point for the archive. For each element of the
235 archive which is included, BFD will call ldlang_add_file,
236 which will set the usrdata field of the member to the
237 lang_input_statement. */
238 if (member
->usrdata
!= NULL
)
240 walk_wild_section (s
, member
->usrdata
, callback
, data
);
243 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
249 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
251 const char *file_spec
= s
->filename
;
253 if (file_spec
== NULL
)
255 /* Perform the iteration over all files in the list. */
256 LANG_FOR_EACH_INPUT_STATEMENT (f
)
258 walk_wild_file (s
, f
, callback
, data
);
261 else if (wildcardp (file_spec
))
263 LANG_FOR_EACH_INPUT_STATEMENT (f
)
265 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
266 walk_wild_file (s
, f
, callback
, data
);
271 lang_input_statement_type
*f
;
273 /* Perform the iteration over a single file. */
274 f
= lookup_name (file_spec
);
276 walk_wild_file (s
, f
, callback
, data
);
280 /* lang_for_each_statement walks the parse tree and calls the provided
281 function for each node. */
284 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
285 lang_statement_union_type
*s
)
287 for (; s
!= NULL
; s
= s
->header
.next
)
291 switch (s
->header
.type
)
293 case lang_constructors_statement_enum
:
294 lang_for_each_statement_worker (func
, constructor_list
.head
);
296 case lang_output_section_statement_enum
:
297 lang_for_each_statement_worker
299 s
->output_section_statement
.children
.head
);
301 case lang_wild_statement_enum
:
302 lang_for_each_statement_worker
304 s
->wild_statement
.children
.head
);
306 case lang_group_statement_enum
:
307 lang_for_each_statement_worker (func
,
308 s
->group_statement
.children
.head
);
310 case lang_data_statement_enum
:
311 case lang_reloc_statement_enum
:
312 case lang_object_symbols_statement_enum
:
313 case lang_output_statement_enum
:
314 case lang_target_statement_enum
:
315 case lang_input_section_enum
:
316 case lang_input_statement_enum
:
317 case lang_assignment_statement_enum
:
318 case lang_padding_statement_enum
:
319 case lang_address_statement_enum
:
320 case lang_fill_statement_enum
:
330 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
332 lang_for_each_statement_worker (func
, statement_list
.head
);
335 /*----------------------------------------------------------------------*/
338 lang_list_init (lang_statement_list_type
*list
)
341 list
->tail
= &list
->head
;
344 /* Build a new statement node for the parse tree. */
346 static lang_statement_union_type
*
347 new_statement (enum statement_enum type
,
349 lang_statement_list_type
*list
)
351 lang_statement_union_type
*new;
353 new = stat_alloc (size
);
354 new->header
.type
= type
;
355 new->header
.next
= NULL
;
356 lang_statement_append (list
, new, &new->header
.next
);
360 /* Build a new input file node for the language. There are several
361 ways in which we treat an input file, eg, we only look at symbols,
362 or prefix it with a -l etc.
364 We can be supplied with requests for input files more than once;
365 they may, for example be split over several lines like foo.o(.text)
366 foo.o(.data) etc, so when asked for a file we check that we haven't
367 got it already so we don't duplicate the bfd. */
369 static lang_input_statement_type
*
370 new_afile (const char *name
,
371 lang_input_file_enum_type file_type
,
373 bfd_boolean add_to_list
)
375 lang_input_statement_type
*p
;
378 p
= new_stat (lang_input_statement
, stat_ptr
);
381 p
= stat_alloc (sizeof (lang_input_statement_type
));
382 p
->header
.next
= NULL
;
385 lang_has_input_file
= TRUE
;
387 p
->sysrooted
= FALSE
;
390 case lang_input_file_is_symbols_only_enum
:
392 p
->is_archive
= FALSE
;
394 p
->local_sym_name
= name
;
395 p
->just_syms_flag
= TRUE
;
396 p
->search_dirs_flag
= FALSE
;
398 case lang_input_file_is_fake_enum
:
400 p
->is_archive
= FALSE
;
402 p
->local_sym_name
= name
;
403 p
->just_syms_flag
= FALSE
;
404 p
->search_dirs_flag
= FALSE
;
406 case lang_input_file_is_l_enum
:
407 p
->is_archive
= TRUE
;
410 p
->local_sym_name
= concat ("-l", name
, NULL
);
411 p
->just_syms_flag
= FALSE
;
412 p
->search_dirs_flag
= TRUE
;
414 case lang_input_file_is_marker_enum
:
416 p
->is_archive
= FALSE
;
418 p
->local_sym_name
= name
;
419 p
->just_syms_flag
= FALSE
;
420 p
->search_dirs_flag
= TRUE
;
422 case lang_input_file_is_search_file_enum
:
423 p
->sysrooted
= ldlang_sysrooted_script
;
425 p
->is_archive
= FALSE
;
427 p
->local_sym_name
= name
;
428 p
->just_syms_flag
= FALSE
;
429 p
->search_dirs_flag
= TRUE
;
431 case lang_input_file_is_file_enum
:
433 p
->is_archive
= FALSE
;
435 p
->local_sym_name
= name
;
436 p
->just_syms_flag
= FALSE
;
437 p
->search_dirs_flag
= FALSE
;
444 p
->next_real_file
= NULL
;
447 p
->dynamic
= config
.dynamic_link
;
448 p
->whole_archive
= whole_archive
;
450 lang_statement_append (&input_file_chain
,
451 (lang_statement_union_type
*) p
,
456 lang_input_statement_type
*
457 lang_add_input_file (const char *name
,
458 lang_input_file_enum_type file_type
,
461 lang_has_input_file
= TRUE
;
462 return new_afile (name
, file_type
, target
, TRUE
);
465 /* Build enough state so that the parser can build its tree. */
470 obstack_begin (&stat_obstack
, 1000);
472 stat_ptr
= &statement_list
;
474 lang_list_init (stat_ptr
);
476 lang_list_init (&input_file_chain
);
477 lang_list_init (&lang_output_section_statement
);
478 lang_list_init (&file_chain
);
479 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
482 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
484 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
486 /* The value "3" is ad-hoc, somewhat related to the expected number of
487 DEFINED expressions in a linker script. For most default linker
488 scripts, there are none. Why a hash table then? Well, it's somewhat
489 simpler to re-use working machinery than using a linked list in terms
490 of code-complexity here in ld, besides the initialization which just
491 looks like other code here. */
492 if (bfd_hash_table_init_n (&lang_definedness_table
,
493 lang_definedness_newfunc
, 3) != TRUE
)
494 einfo (_("%P%F: out of memory during initialization"));
496 /* Callers of exp_fold_tree need to increment this. */
497 lang_statement_iteration
= 0;
500 /*----------------------------------------------------------------------
501 A region is an area of memory declared with the
502 MEMORY { name:org=exp, len=exp ... }
505 We maintain a list of all the regions here.
507 If no regions are specified in the script, then the default is used
508 which is created when looked up to be the entire data space.
510 If create is true we are creating a region inside a MEMORY block.
511 In this case it is probably an error to create a region that has
512 already been created. If we are not inside a MEMORY block it is
513 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
514 and so we issue a warning. */
516 static lang_memory_region_type
*lang_memory_region_list
;
517 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
519 lang_memory_region_type
*
520 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
522 lang_memory_region_type
*p
;
523 lang_memory_region_type
*new;
525 /* NAME is NULL for LMA memspecs if no region was specified. */
529 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
530 if (strcmp (p
->name
, name
) == 0)
533 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name
);
538 /* This code used to always use the first region in the list as the
539 default region. I changed it to instead use a region
540 encompassing all of memory as the default region. This permits
541 NOLOAD sections to work reasonably without requiring a region.
542 People should specify what region they mean, if they really want
544 if (strcmp (name
, DEFAULT_MEMORY_REGION
) == 0)
546 if (lang_memory_region_list
!= NULL
)
547 return lang_memory_region_list
;
551 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
552 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
554 new = stat_alloc (sizeof (lang_memory_region_type
));
556 new->name
= xstrdup (name
);
559 *lang_memory_region_list_tail
= new;
560 lang_memory_region_list_tail
= &new->next
;
564 new->length
= ~(bfd_size_type
) 0;
566 new->had_full_message
= FALSE
;
571 static lang_memory_region_type
*
572 lang_memory_default (asection
*section
)
574 lang_memory_region_type
*p
;
576 flagword sec_flags
= section
->flags
;
578 /* Override SEC_DATA to mean a writable section. */
579 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
580 sec_flags
|= SEC_DATA
;
582 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
584 if ((p
->flags
& sec_flags
) != 0
585 && (p
->not_flags
& sec_flags
) == 0)
590 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
593 lang_output_section_statement_type
*
594 lang_output_section_find (const char *const name
)
596 lang_statement_union_type
*u
;
597 lang_output_section_statement_type
*lookup
;
599 for (u
= lang_output_section_statement
.head
; u
!= NULL
; u
= lookup
->next
)
601 lookup
= &u
->output_section_statement
;
602 if (strcmp (name
, lookup
->name
) == 0)
608 lang_output_section_statement_type
*
609 lang_output_section_statement_lookup (const char *const name
)
611 lang_output_section_statement_type
*lookup
;
613 lookup
= lang_output_section_find (name
);
616 lookup
= new_stat (lang_output_section_statement
, stat_ptr
);
617 lookup
->region
= NULL
;
618 lookup
->lma_region
= NULL
;
620 lookup
->block_value
= 1;
624 lookup
->bfd_section
= NULL
;
625 lookup
->processed
= 0;
626 lookup
->sectype
= normal_section
;
627 lookup
->addr_tree
= NULL
;
628 lang_list_init (&lookup
->children
);
630 lookup
->memspec
= NULL
;
632 lookup
->subsection_alignment
= -1;
633 lookup
->section_alignment
= -1;
634 lookup
->load_base
= NULL
;
635 lookup
->update_dot_tree
= NULL
;
636 lookup
->phdrs
= NULL
;
638 lang_statement_append (&lang_output_section_statement
,
639 (lang_statement_union_type
*) lookup
,
646 lang_map_flags (flagword flag
)
648 if (flag
& SEC_ALLOC
)
654 if (flag
& SEC_READONLY
)
667 lang_memory_region_type
*m
;
669 minfo (_("\nMemory Configuration\n\n"));
670 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
671 _("Name"), _("Origin"), _("Length"), _("Attributes"));
673 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
678 fprintf (config
.map_file
, "%-16s ", m
->name
);
680 sprintf_vma (buf
, m
->origin
);
681 minfo ("0x%s ", buf
);
689 minfo ("0x%V", m
->length
);
690 if (m
->flags
|| m
->not_flags
)
698 lang_map_flags (m
->flags
);
704 lang_map_flags (m
->not_flags
);
711 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
716 /* Initialize an output section. */
719 init_os (lang_output_section_statement_type
*s
)
721 section_userdata_type
*new;
723 if (s
->bfd_section
!= NULL
)
726 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
727 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
729 new = stat_alloc (sizeof (section_userdata_type
));
731 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
732 if (s
->bfd_section
== NULL
)
733 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
734 if (s
->bfd_section
== NULL
)
736 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
737 output_bfd
->xvec
->name
, s
->name
);
739 s
->bfd_section
->output_section
= s
->bfd_section
;
741 /* We initialize an output sections output offset to minus its own
742 vma to allow us to output a section through itself. */
743 s
->bfd_section
->output_offset
= 0;
744 get_userdata (s
->bfd_section
) = new;
746 /* If there is a base address, make sure that any sections it might
747 mention are initialized. */
748 if (s
->addr_tree
!= NULL
)
749 exp_init_os (s
->addr_tree
);
751 if (s
->load_base
!= NULL
)
752 exp_init_os (s
->load_base
);
755 /* Make sure that all output sections mentioned in an expression are
759 exp_init_os (etree_type
*exp
)
761 switch (exp
->type
.node_class
)
764 exp_init_os (exp
->assign
.src
);
768 exp_init_os (exp
->binary
.lhs
);
769 exp_init_os (exp
->binary
.rhs
);
773 exp_init_os (exp
->trinary
.cond
);
774 exp_init_os (exp
->trinary
.lhs
);
775 exp_init_os (exp
->trinary
.rhs
);
779 exp_init_os (exp
->assert_s
.child
);
783 exp_init_os (exp
->unary
.child
);
787 switch (exp
->type
.node_code
)
793 lang_output_section_statement_type
*os
;
795 os
= lang_output_section_find (exp
->name
.name
);
796 if (os
!= NULL
&& os
->bfd_section
== NULL
)
807 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
808 once into the output. This routine checks each section, and
809 arrange to discard it if a section of the same name has already
810 been linked. If the section has COMDAT information, then it uses
811 that to decide whether the section should be included. This code
812 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
813 that is, it does not depend solely upon the section name.
814 section_already_linked is called via bfd_map_over_sections. */
816 /* This is the shape of the elements inside the already_linked hash
817 table. It maps a name onto a list of already_linked elements with
818 the same name. It's possible to get more than one element in a
819 list if the COMDAT sections have different names. */
821 struct already_linked_hash_entry
823 struct bfd_hash_entry root
;
824 struct already_linked
*entry
;
827 struct already_linked
829 struct already_linked
*next
;
833 /* The hash table. */
835 static struct bfd_hash_table already_linked_table
;
838 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
840 lang_input_statement_type
*entry
= data
;
843 struct already_linked
*l
;
844 struct already_linked_hash_entry
*already_linked_list
;
846 /* If we are only reading symbols from this object, then we want to
847 discard all sections. */
848 if (entry
->just_syms_flag
)
850 bfd_link_just_syms (sec
, &link_info
);
854 flags
= bfd_get_section_flags (abfd
, sec
);
856 if ((flags
& SEC_LINK_ONCE
) == 0)
859 /* FIXME: When doing a relocatable link, we may have trouble
860 copying relocations in other sections that refer to local symbols
861 in the section being discarded. Those relocations will have to
862 be converted somehow; as of this writing I'm not sure that any of
863 the backends handle that correctly.
865 It is tempting to instead not discard link once sections when
866 doing a relocatable link (technically, they should be discarded
867 whenever we are building constructors). However, that fails,
868 because the linker winds up combining all the link once sections
869 into a single large link once section, which defeats the purpose
870 of having link once sections in the first place.
872 Also, not merging link once sections in a relocatable link
873 causes trouble for MIPS ELF, which relies on link once semantics
874 to handle the .reginfo section correctly. */
876 name
= bfd_get_section_name (abfd
, sec
);
878 already_linked_list
=
879 ((struct already_linked_hash_entry
*)
880 bfd_hash_lookup (&already_linked_table
, name
, TRUE
, FALSE
));
882 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
884 if (sec
->comdat
== NULL
885 || l
->sec
->comdat
== NULL
886 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
888 /* The section has already been linked. See if we should
890 switch (flags
& SEC_LINK_DUPLICATES
)
895 case SEC_LINK_DUPLICATES_DISCARD
:
898 case SEC_LINK_DUPLICATES_ONE_ONLY
:
899 if (sec
->comdat
== NULL
)
900 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
903 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
904 abfd
, name
, sec
->comdat
->name
);
907 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
908 /* FIXME: We should really dig out the contents of both
909 sections and memcmp them. The COFF/PE spec says that
910 the Microsoft linker does not implement this
911 correctly, so I'm not going to bother doing it
914 case SEC_LINK_DUPLICATES_SAME_SIZE
:
915 if (bfd_section_size (abfd
, sec
)
916 != bfd_section_size (l
->sec
->owner
, l
->sec
))
917 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
922 /* Set the output_section field so that lang_add_section
923 does not create a lang_input_section structure for this
924 section. Since there might be a symbol in the section
925 being discarded, we must retain a pointer to the section
926 which we are really going to use. */
927 sec
->output_section
= bfd_abs_section_ptr
;
928 sec
->kept_section
= l
->sec
;
930 if (flags
& SEC_GROUP
)
931 bfd_discard_group (abfd
, sec
);
937 /* This is the first section with this name. Record it. Allocate
938 the memory from the same obstack as the hash table is kept in. */
940 l
= bfd_hash_allocate (&already_linked_table
, sizeof *l
);
943 l
->next
= already_linked_list
->entry
;
944 already_linked_list
->entry
= l
;
947 /* Support routines for the hash table used by section_already_linked,
948 initialize the table, fill in an entry and remove the table. */
950 static struct bfd_hash_entry
*
951 already_linked_newfunc (struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
,
952 struct bfd_hash_table
*table
,
953 const char *string ATTRIBUTE_UNUSED
)
955 struct already_linked_hash_entry
*ret
=
956 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
964 already_linked_table_init (void)
966 if (! bfd_hash_table_init_n (&already_linked_table
,
967 already_linked_newfunc
,
969 einfo (_("%P%F: Failed to create hash table\n"));
973 already_linked_table_free (void)
975 bfd_hash_table_free (&already_linked_table
);
978 /* The wild routines.
980 These expand statements like *(.text) and foo.o to a list of
981 explicit actions, like foo.o(.text), bar.o(.text) and
982 foo.o(.text, .data). */
984 /* Return TRUE if the PATTERN argument is a wildcard pattern.
985 Although backslashes are treated specially if a pattern contains
986 wildcards, we do not consider the mere presence of a backslash to
987 be enough to cause the pattern to be treated as a wildcard.
988 That lets us handle DOS filenames more naturally. */
991 wildcardp (const char *pattern
)
995 for (s
= pattern
; *s
!= '\0'; ++s
)
1003 /* Add SECTION to the output section OUTPUT. Do this by creating a
1004 lang_input_section statement which is placed at PTR. FILE is the
1005 input file which holds SECTION. */
1008 lang_add_section (lang_statement_list_type
*ptr
,
1010 lang_output_section_statement_type
*output
,
1011 lang_input_statement_type
*file
)
1014 bfd_boolean discard
;
1016 flags
= bfd_get_section_flags (section
->owner
, section
);
1020 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1021 link. Discard debugging sections marked with SEC_EXCLUDE on a
1022 relocatable link too. */
1023 if ((flags
& SEC_EXCLUDE
) != 0
1024 && ((flags
& SEC_DEBUGGING
) != 0 || !link_info
.relocatable
))
1027 /* Discard input sections which are assigned to a section named
1028 DISCARD_SECTION_NAME. */
1029 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1032 /* Discard debugging sections if we are stripping debugging
1034 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1035 && (flags
& SEC_DEBUGGING
) != 0)
1040 if (section
->output_section
== NULL
)
1042 /* This prevents future calls from assigning this section. */
1043 section
->output_section
= bfd_abs_section_ptr
;
1048 if (section
->output_section
== NULL
)
1051 lang_input_section_type
*new;
1054 if (output
->bfd_section
== NULL
)
1057 first
= ! output
->bfd_section
->linker_has_input
;
1058 output
->bfd_section
->linker_has_input
= 1;
1060 /* Add a section reference to the list. */
1061 new = new_stat (lang_input_section
, ptr
);
1063 new->section
= section
;
1065 section
->output_section
= output
->bfd_section
;
1067 flags
= section
->flags
;
1069 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1070 to an output section, because we want to be able to include a
1071 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1072 section (I don't know why we want to do this, but we do).
1073 build_link_order in ldwrite.c handles this case by turning
1074 the embedded SEC_NEVER_LOAD section into a fill. */
1076 flags
&= ~ SEC_NEVER_LOAD
;
1078 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1079 already been processed. One reason to do this is that on pe
1080 format targets, .text$foo sections go into .text and it's odd
1081 to see .text with SEC_LINK_ONCE set. */
1083 if (! link_info
.relocatable
)
1084 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1086 /* If this is not the first input section, and the SEC_READONLY
1087 flag is not currently set, then don't set it just because the
1088 input section has it set. */
1090 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1091 flags
&= ~ SEC_READONLY
;
1093 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1095 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1096 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1097 || ((flags
& SEC_MERGE
)
1098 && section
->output_section
->entsize
!= section
->entsize
)))
1100 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1101 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1104 section
->output_section
->flags
|= flags
;
1106 if (flags
& SEC_MERGE
)
1107 section
->output_section
->entsize
= section
->entsize
;
1109 /* If SEC_READONLY is not set in the input section, then clear
1110 it from the output section. */
1111 if ((section
->flags
& SEC_READONLY
) == 0)
1112 section
->output_section
->flags
&= ~SEC_READONLY
;
1114 switch (output
->sectype
)
1116 case normal_section
:
1121 case overlay_section
:
1122 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1124 case noload_section
:
1125 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1126 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1130 /* Copy over SEC_SMALL_DATA. */
1131 if (section
->flags
& SEC_SMALL_DATA
)
1132 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1134 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1135 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1137 /* If supplied an alignment, then force it. */
1138 if (output
->section_alignment
!= -1)
1139 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1141 if (section
->flags
& SEC_BLOCK
)
1143 section
->output_section
->flags
|= SEC_BLOCK
;
1144 /* FIXME: This value should really be obtained from the bfd... */
1145 output
->block_value
= 128;
1150 /* Handle wildcard sorting. This returns the lang_input_section which
1151 should follow the one we are going to create for SECTION and FILE,
1152 based on the sorting requirements of WILD. It returns NULL if the
1153 new section should just go at the end of the current list. */
1155 static lang_statement_union_type
*
1156 wild_sort (lang_wild_statement_type
*wild
,
1157 struct wildcard_list
*sec
,
1158 lang_input_statement_type
*file
,
1161 const char *section_name
;
1162 lang_statement_union_type
*l
;
1164 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1167 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1168 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1170 lang_input_section_type
*ls
;
1172 if (l
->header
.type
!= lang_input_section_enum
)
1174 ls
= &l
->input_section
;
1176 /* Sorting by filename takes precedence over sorting by section
1179 if (wild
->filenames_sorted
)
1181 const char *fn
, *ln
;
1185 /* The PE support for the .idata section as generated by
1186 dlltool assumes that files will be sorted by the name of
1187 the archive and then the name of the file within the
1190 if (file
->the_bfd
!= NULL
1191 && bfd_my_archive (file
->the_bfd
) != NULL
)
1193 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1198 fn
= file
->filename
;
1202 if (ls
->ifile
->the_bfd
!= NULL
1203 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1205 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1210 ln
= ls
->ifile
->filename
;
1214 i
= strcmp (fn
, ln
);
1223 fn
= file
->filename
;
1225 ln
= ls
->ifile
->filename
;
1227 i
= strcmp (fn
, ln
);
1235 /* Here either the files are not sorted by name, or we are
1236 looking at the sections for this file. */
1238 if (sec
!= NULL
&& sec
->spec
.sorted
)
1240 if (strcmp (section_name
,
1241 bfd_get_section_name (ls
->ifile
->the_bfd
,
1251 /* Expand a wild statement for a particular FILE. SECTION may be
1252 NULL, in which case it is a wild card. */
1255 output_section_callback (lang_wild_statement_type
*ptr
,
1256 struct wildcard_list
*sec
,
1258 lang_input_statement_type
*file
,
1261 lang_statement_union_type
*before
;
1263 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1264 if (unique_section_p (bfd_get_section_name (file
->the_bfd
, section
)))
1267 /* If the wild pattern was marked KEEP, the member sections
1268 should be as well. */
1269 if (ptr
->keep_sections
)
1270 section
->flags
|= SEC_KEEP
;
1272 before
= wild_sort (ptr
, sec
, file
, section
);
1274 /* Here BEFORE points to the lang_input_section which
1275 should follow the one we are about to add. If BEFORE
1276 is NULL, then the section should just go at the end
1277 of the current list. */
1280 lang_add_section (&ptr
->children
, section
,
1281 (lang_output_section_statement_type
*) output
,
1285 lang_statement_list_type list
;
1286 lang_statement_union_type
**pp
;
1288 lang_list_init (&list
);
1289 lang_add_section (&list
, section
,
1290 (lang_output_section_statement_type
*) output
,
1293 /* If we are discarding the section, LIST.HEAD will
1295 if (list
.head
!= NULL
)
1297 ASSERT (list
.head
->header
.next
== NULL
);
1299 for (pp
= &ptr
->children
.head
;
1301 pp
= &(*pp
)->header
.next
)
1302 ASSERT (*pp
!= NULL
);
1304 list
.head
->header
.next
= *pp
;
1310 /* This is passed a file name which must have been seen already and
1311 added to the statement tree. We will see if it has been opened
1312 already and had its symbols read. If not then we'll read it. */
1314 static lang_input_statement_type
*
1315 lookup_name (const char *name
)
1317 lang_input_statement_type
*search
;
1319 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1321 search
= (lang_input_statement_type
*) search
->next_real_file
)
1323 /* Use the local_sym_name as the name of the file that has
1324 already been loaded as filename might have been transformed
1325 via the search directory lookup mechanism. */
1326 const char * filename
= search
->local_sym_name
;
1328 if (filename
== NULL
&& name
== NULL
)
1330 if (filename
!= NULL
1332 && strcmp (filename
, name
) == 0)
1337 search
= new_afile (name
, lang_input_file_is_search_file_enum
, default_target
,
1340 /* If we have already added this file, or this file is not real
1341 (FIXME: can that ever actually happen?) or the name is NULL
1342 (FIXME: can that ever actually happen?) don't add this file. */
1345 || search
->filename
== NULL
)
1348 if (! load_symbols (search
, NULL
))
1354 /* Get the symbols for an input file. */
1357 load_symbols (lang_input_statement_type
*entry
,
1358 lang_statement_list_type
*place
)
1365 ldfile_open_file (entry
);
1367 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1368 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1371 lang_statement_list_type
*hold
;
1372 bfd_boolean bad_load
= TRUE
;
1373 bfd_boolean save_ldlang_sysrooted_script
;
1375 err
= bfd_get_error ();
1377 /* See if the emulation has some special knowledge. */
1378 if (ldemul_unrecognized_file (entry
))
1381 if (err
== bfd_error_file_ambiguously_recognized
)
1385 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1386 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1387 for (p
= matching
; *p
!= NULL
; p
++)
1391 else if (err
!= bfd_error_file_not_recognized
1393 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1397 bfd_close (entry
->the_bfd
);
1398 entry
->the_bfd
= NULL
;
1400 /* Try to interpret the file as a linker script. */
1401 ldfile_open_command_file (entry
->filename
);
1405 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1406 ldlang_sysrooted_script
= entry
->sysrooted
;
1408 ldfile_assumed_script
= TRUE
;
1409 parser_input
= input_script
;
1411 ldfile_assumed_script
= FALSE
;
1413 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1419 if (ldemul_recognized_file (entry
))
1422 /* We don't call ldlang_add_file for an archive. Instead, the
1423 add_symbols entry point will call ldlang_add_file, via the
1424 add_archive_element callback, for each element of the archive
1426 switch (bfd_get_format (entry
->the_bfd
))
1432 ldlang_add_file (entry
);
1433 if (trace_files
|| trace_file_tries
)
1434 info_msg ("%I\n", entry
);
1438 if (entry
->whole_archive
)
1441 bfd_boolean loaded
= TRUE
;
1445 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1450 if (! bfd_check_format (member
, bfd_object
))
1452 einfo (_("%F%B: member %B in archive is not an object\n"),
1453 entry
->the_bfd
, member
);
1457 if (! ((*link_info
.callbacks
->add_archive_element
)
1458 (&link_info
, member
, "--whole-archive")))
1461 if (! bfd_link_add_symbols (member
, &link_info
))
1463 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1468 entry
->loaded
= loaded
;
1474 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1475 entry
->loaded
= TRUE
;
1477 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1479 return entry
->loaded
;
1482 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1483 may be NULL, indicating that it is a wildcard. Separate
1484 lang_input_section statements are created for each part of the
1485 expansion; they are added after the wild statement S. OUTPUT is
1486 the output section. */
1489 wild (lang_wild_statement_type
*s
,
1490 const char *target ATTRIBUTE_UNUSED
,
1491 lang_output_section_statement_type
*output
)
1493 struct wildcard_list
*sec
;
1495 walk_wild (s
, output_section_callback
, output
);
1497 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1499 if (default_common_section
!= NULL
)
1501 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1503 /* Remember the section that common is going to in case we
1504 later get something which doesn't know where to put it. */
1505 default_common_section
= output
;
1510 /* Return TRUE iff target is the sought target. */
1513 get_target (const bfd_target
*target
, void *data
)
1515 const char *sought
= data
;
1517 return strcmp (target
->name
, sought
) == 0;
1520 /* Like strcpy() but convert to lower case as well. */
1523 stricpy (char *dest
, char *src
)
1527 while ((c
= *src
++) != 0)
1528 *dest
++ = TOLOWER (c
);
1533 /* Remove the first occurrence of needle (if any) in haystack
1537 strcut (char *haystack
, char *needle
)
1539 haystack
= strstr (haystack
, needle
);
1545 for (src
= haystack
+ strlen (needle
); *src
;)
1546 *haystack
++ = *src
++;
1552 /* Compare two target format name strings.
1553 Return a value indicating how "similar" they are. */
1556 name_compare (char *first
, char *second
)
1562 copy1
= xmalloc (strlen (first
) + 1);
1563 copy2
= xmalloc (strlen (second
) + 1);
1565 /* Convert the names to lower case. */
1566 stricpy (copy1
, first
);
1567 stricpy (copy2
, second
);
1569 /* Remove size and endian strings from the name. */
1570 strcut (copy1
, "big");
1571 strcut (copy1
, "little");
1572 strcut (copy2
, "big");
1573 strcut (copy2
, "little");
1575 /* Return a value based on how many characters match,
1576 starting from the beginning. If both strings are
1577 the same then return 10 * their length. */
1578 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1579 if (copy1
[result
] == 0)
1591 /* Set by closest_target_match() below. */
1592 static const bfd_target
*winner
;
1594 /* Scan all the valid bfd targets looking for one that has the endianness
1595 requirement that was specified on the command line, and is the nearest
1596 match to the original output target. */
1599 closest_target_match (const bfd_target
*target
, void *data
)
1601 const bfd_target
*original
= data
;
1603 if (command_line
.endian
== ENDIAN_BIG
1604 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1607 if (command_line
.endian
== ENDIAN_LITTLE
1608 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1611 /* Must be the same flavour. */
1612 if (target
->flavour
!= original
->flavour
)
1615 /* If we have not found a potential winner yet, then record this one. */
1622 /* Oh dear, we now have two potential candidates for a successful match.
1623 Compare their names and choose the better one. */
1624 if (name_compare (target
->name
, original
->name
)
1625 > name_compare (winner
->name
, original
->name
))
1628 /* Keep on searching until wqe have checked them all. */
1632 /* Return the BFD target format of the first input file. */
1635 get_first_input_target (void)
1637 char *target
= NULL
;
1639 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1641 if (s
->header
.type
== lang_input_statement_enum
1644 ldfile_open_file (s
);
1646 if (s
->the_bfd
!= NULL
1647 && bfd_check_format (s
->the_bfd
, bfd_object
))
1649 target
= bfd_get_target (s
->the_bfd
);
1661 lang_get_output_target (void)
1665 /* Has the user told us which output format to use? */
1666 if (output_target
!= NULL
)
1667 return output_target
;
1669 /* No - has the current target been set to something other than
1671 if (current_target
!= default_target
)
1672 return current_target
;
1674 /* No - can we determine the format of the first input file? */
1675 target
= get_first_input_target ();
1679 /* Failed - use the default output target. */
1680 return default_target
;
1683 /* Open the output file. */
1686 open_output (const char *name
)
1690 output_target
= lang_get_output_target ();
1692 /* Has the user requested a particular endianness on the command
1694 if (command_line
.endian
!= ENDIAN_UNSET
)
1696 const bfd_target
*target
;
1697 enum bfd_endian desired_endian
;
1699 /* Get the chosen target. */
1700 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1702 /* If the target is not supported, we cannot do anything. */
1705 if (command_line
.endian
== ENDIAN_BIG
)
1706 desired_endian
= BFD_ENDIAN_BIG
;
1708 desired_endian
= BFD_ENDIAN_LITTLE
;
1710 /* See if the target has the wrong endianness. This should
1711 not happen if the linker script has provided big and
1712 little endian alternatives, but some scrips don't do
1714 if (target
->byteorder
!= desired_endian
)
1716 /* If it does, then see if the target provides
1717 an alternative with the correct endianness. */
1718 if (target
->alternative_target
!= NULL
1719 && (target
->alternative_target
->byteorder
== desired_endian
))
1720 output_target
= target
->alternative_target
->name
;
1723 /* Try to find a target as similar as possible to
1724 the default target, but which has the desired
1725 endian characteristic. */
1726 bfd_search_for_target (closest_target_match
,
1729 /* Oh dear - we could not find any targets that
1730 satisfy our requirements. */
1732 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1734 output_target
= winner
->name
;
1740 output
= bfd_openw (name
, output_target
);
1744 if (bfd_get_error () == bfd_error_invalid_target
)
1745 einfo (_("%P%F: target %s not found\n"), output_target
);
1747 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1750 delete_output_file_on_failure
= TRUE
;
1753 output
->flags
|= D_PAGED
;
1756 if (! bfd_set_format (output
, bfd_object
))
1757 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1758 if (! bfd_set_arch_mach (output
,
1759 ldfile_output_architecture
,
1760 ldfile_output_machine
))
1761 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1763 link_info
.hash
= bfd_link_hash_table_create (output
);
1764 if (link_info
.hash
== NULL
)
1765 einfo (_("%P%F: can not create link hash table: %E\n"));
1767 bfd_set_gp_size (output
, g_switch_value
);
1772 ldlang_open_output (lang_statement_union_type
*statement
)
1774 switch (statement
->header
.type
)
1776 case lang_output_statement_enum
:
1777 ASSERT (output_bfd
== NULL
);
1778 output_bfd
= open_output (statement
->output_statement
.name
);
1779 ldemul_set_output_arch ();
1780 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
1781 output_bfd
->flags
|= D_PAGED
;
1783 output_bfd
->flags
&= ~D_PAGED
;
1784 if (config
.text_read_only
)
1785 output_bfd
->flags
|= WP_TEXT
;
1787 output_bfd
->flags
&= ~WP_TEXT
;
1788 if (link_info
.traditional_format
)
1789 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1791 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1794 case lang_target_statement_enum
:
1795 current_target
= statement
->target_statement
.target
;
1802 /* Convert between addresses in bytes and sizes in octets.
1803 For currently supported targets, octets_per_byte is always a power
1804 of two, so we can use shifts. */
1805 #define TO_ADDR(X) ((X) >> opb_shift)
1806 #define TO_SIZE(X) ((X) << opb_shift)
1808 /* Support the above. */
1809 static unsigned int opb_shift
= 0;
1814 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
1815 ldfile_output_machine
);
1818 while ((x
& 1) == 0)
1826 /* Open all the input files. */
1829 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
1831 for (; s
!= NULL
; s
= s
->header
.next
)
1833 switch (s
->header
.type
)
1835 case lang_constructors_statement_enum
:
1836 open_input_bfds (constructor_list
.head
, force
);
1838 case lang_output_section_statement_enum
:
1839 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1841 case lang_wild_statement_enum
:
1842 /* Maybe we should load the file's symbols. */
1843 if (s
->wild_statement
.filename
1844 && ! wildcardp (s
->wild_statement
.filename
))
1845 (void) lookup_name (s
->wild_statement
.filename
);
1846 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1848 case lang_group_statement_enum
:
1850 struct bfd_link_hash_entry
*undefs
;
1852 /* We must continually search the entries in the group
1853 until no new symbols are added to the list of undefined
1858 undefs
= link_info
.hash
->undefs_tail
;
1859 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
1861 while (undefs
!= link_info
.hash
->undefs_tail
);
1864 case lang_target_statement_enum
:
1865 current_target
= s
->target_statement
.target
;
1867 case lang_input_statement_enum
:
1868 if (s
->input_statement
.real
)
1870 lang_statement_list_type add
;
1872 s
->input_statement
.target
= current_target
;
1874 /* If we are being called from within a group, and this
1875 is an archive which has already been searched, then
1876 force it to be researched unless the whole archive
1877 has been loaded already. */
1879 && !s
->input_statement
.whole_archive
1880 && s
->input_statement
.loaded
1881 && bfd_check_format (s
->input_statement
.the_bfd
,
1883 s
->input_statement
.loaded
= FALSE
;
1885 lang_list_init (&add
);
1887 if (! load_symbols (&s
->input_statement
, &add
))
1888 config
.make_executable
= FALSE
;
1890 if (add
.head
!= NULL
)
1892 *add
.tail
= s
->header
.next
;
1893 s
->header
.next
= add
.head
;
1903 /* If there are [COMMONS] statements, put a wild one into the bss
1907 lang_reasonable_defaults (void)
1910 lang_output_section_statement_lookup (".text");
1911 lang_output_section_statement_lookup (".data");
1913 default_common_section
= lang_output_section_statement_lookup (".bss");
1915 if (!placed_commons
)
1917 lang_wild_statement_type
*new =
1918 new_stat (lang_wild_statement
,
1919 &default_common_section
->children
);
1921 new->section_name
= "COMMON";
1922 new->filename
= NULL
;
1923 lang_list_init (&new->children
);
1928 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1931 lang_track_definedness (const char *name
)
1933 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
1934 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
1937 /* New-function for the definedness hash table. */
1939 static struct bfd_hash_entry
*
1940 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
1941 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
1942 const char *name ATTRIBUTE_UNUSED
)
1944 struct lang_definedness_hash_entry
*ret
1945 = (struct lang_definedness_hash_entry
*) entry
;
1948 ret
= (struct lang_definedness_hash_entry
*)
1949 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
1952 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
1954 ret
->iteration
= -1;
1958 /* Return the iteration when the definition of NAME was last updated. A
1959 value of -1 means that the symbol is not defined in the linker script
1960 or the command line, but may be defined in the linker symbol table. */
1963 lang_symbol_definition_iteration (const char *name
)
1965 struct lang_definedness_hash_entry
*defentry
1966 = (struct lang_definedness_hash_entry
*)
1967 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1969 /* We've already created this one on the presence of DEFINED in the
1970 script, so it can't be NULL unless something is borked elsewhere in
1972 if (defentry
== NULL
)
1975 return defentry
->iteration
;
1978 /* Update the definedness state of NAME. */
1981 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
1983 struct lang_definedness_hash_entry
*defentry
1984 = (struct lang_definedness_hash_entry
*)
1985 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1987 /* We don't keep track of symbols not tested with DEFINED. */
1988 if (defentry
== NULL
)
1991 /* If the symbol was already defined, and not from an earlier statement
1992 iteration, don't update the definedness iteration, because that'd
1993 make the symbol seem defined in the linker script at this point, and
1994 it wasn't; it was defined in some object. If we do anyway, DEFINED
1995 would start to yield false before this point and the construct "sym =
1996 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1998 if (h
->type
!= bfd_link_hash_undefined
1999 && h
->type
!= bfd_link_hash_common
2000 && h
->type
!= bfd_link_hash_new
2001 && defentry
->iteration
== -1)
2004 defentry
->iteration
= lang_statement_iteration
;
2007 /* Add the supplied name to the symbol table as an undefined reference.
2008 This is a two step process as the symbol table doesn't even exist at
2009 the time the ld command line is processed. First we put the name
2010 on a list, then, once the output file has been opened, transfer the
2011 name to the symbol table. */
2013 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2015 #define ldlang_undef_chain_list_head entry_symbol.next
2018 ldlang_add_undef (const char *const name
)
2020 ldlang_undef_chain_list_type
*new =
2021 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
2023 new->next
= ldlang_undef_chain_list_head
;
2024 ldlang_undef_chain_list_head
= new;
2026 new->name
= xstrdup (name
);
2028 if (output_bfd
!= NULL
)
2029 insert_undefined (new->name
);
2032 /* Insert NAME as undefined in the symbol table. */
2035 insert_undefined (const char *name
)
2037 struct bfd_link_hash_entry
*h
;
2039 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2041 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2042 if (h
->type
== bfd_link_hash_new
)
2044 h
->type
= bfd_link_hash_undefined
;
2045 h
->u
.undef
.abfd
= NULL
;
2046 bfd_link_add_undef (link_info
.hash
, h
);
2050 /* Run through the list of undefineds created above and place them
2051 into the linker hash table as undefined symbols belonging to the
2055 lang_place_undefineds (void)
2057 ldlang_undef_chain_list_type
*ptr
;
2059 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
2060 insert_undefined (ptr
->name
);
2063 /* Open input files and attach to output sections. */
2066 map_input_to_output_sections
2067 (lang_statement_union_type
*s
, const char *target
,
2068 lang_output_section_statement_type
*output_section_statement
)
2070 for (; s
!= NULL
; s
= s
->header
.next
)
2072 switch (s
->header
.type
)
2074 case lang_wild_statement_enum
:
2075 wild (&s
->wild_statement
, target
, output_section_statement
);
2077 case lang_constructors_statement_enum
:
2078 map_input_to_output_sections (constructor_list
.head
,
2080 output_section_statement
);
2082 case lang_output_section_statement_enum
:
2083 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2085 &s
->output_section_statement
);
2087 case lang_output_statement_enum
:
2089 case lang_target_statement_enum
:
2090 target
= s
->target_statement
.target
;
2092 case lang_group_statement_enum
:
2093 map_input_to_output_sections (s
->group_statement
.children
.head
,
2095 output_section_statement
);
2097 case lang_data_statement_enum
:
2098 /* Make sure that any sections mentioned in the expression
2100 exp_init_os (s
->data_statement
.exp
);
2102 case lang_fill_statement_enum
:
2103 case lang_input_section_enum
:
2104 case lang_object_symbols_statement_enum
:
2105 case lang_reloc_statement_enum
:
2106 case lang_padding_statement_enum
:
2107 case lang_input_statement_enum
:
2108 if (output_section_statement
!= NULL
2109 && output_section_statement
->bfd_section
== NULL
)
2110 init_os (output_section_statement
);
2112 case lang_assignment_statement_enum
:
2113 if (output_section_statement
!= NULL
2114 && output_section_statement
->bfd_section
== NULL
)
2115 init_os (output_section_statement
);
2117 /* Make sure that any sections mentioned in the assignment
2119 exp_init_os (s
->assignment_statement
.exp
);
2121 case lang_afile_asection_pair_statement_enum
:
2124 case lang_address_statement_enum
:
2125 /* Mark the specified section with the supplied address. */
2127 lang_output_section_statement_type
*os
=
2128 lang_output_section_statement_lookup
2129 (s
->address_statement
.section_name
);
2131 if (os
->bfd_section
== NULL
)
2133 os
->addr_tree
= s
->address_statement
.address
;
2140 /* An output section might have been removed after its statement was
2141 added. For example, ldemul_before_allocation can remove dynamic
2142 sections if they turn out to be not needed. Clean them up here. */
2145 strip_excluded_output_sections (void)
2147 lang_statement_union_type
*u
;
2149 for (u
= lang_output_section_statement
.head
;
2151 u
= u
->output_section_statement
.next
)
2153 lang_output_section_statement_type
*os
;
2156 os
= &u
->output_section_statement
;
2157 s
= os
->bfd_section
;
2158 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2162 os
->bfd_section
= NULL
;
2164 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2167 bfd_section_list_remove (output_bfd
, p
);
2168 output_bfd
->section_count
--;
2176 print_output_section_statement
2177 (lang_output_section_statement_type
*output_section_statement
)
2179 asection
*section
= output_section_statement
->bfd_section
;
2182 if (output_section_statement
!= abs_output_section
)
2184 minfo ("\n%s", output_section_statement
->name
);
2186 if (section
!= NULL
)
2188 print_dot
= section
->vma
;
2190 len
= strlen (output_section_statement
->name
);
2191 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2196 while (len
< SECTION_NAME_MAP_LENGTH
)
2202 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2204 if (output_section_statement
->load_base
!= NULL
)
2208 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2209 "load base", lang_final_phase_enum
);
2210 minfo (_(" load address 0x%V"), addr
);
2217 print_statement_list (output_section_statement
->children
.head
,
2218 output_section_statement
);
2222 print_assignment (lang_assignment_statement_type
*assignment
,
2223 lang_output_section_statement_type
*output_section
)
2226 etree_value_type result
;
2228 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2231 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2232 lang_final_phase_enum
, print_dot
, &print_dot
);
2238 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2239 dst
= assignment
->exp
->assign
.dst
;
2241 minfo ("0x%V", value
);
2242 if (dst
[0] == '.' && dst
[1] == 0)
2255 exp_print_tree (assignment
->exp
);
2261 print_input_statement (lang_input_statement_type
*statm
)
2263 if (statm
->filename
!= NULL
)
2265 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2269 /* Print all symbols defined in a particular section. This is called
2270 via bfd_link_hash_traverse. */
2273 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
2275 asection
*sec
= ptr
;
2277 if ((hash_entry
->type
== bfd_link_hash_defined
2278 || hash_entry
->type
== bfd_link_hash_defweak
)
2279 && sec
== hash_entry
->u
.def
.section
)
2283 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2286 (hash_entry
->u
.def
.value
2287 + hash_entry
->u
.def
.section
->output_offset
2288 + hash_entry
->u
.def
.section
->output_section
->vma
));
2290 minfo (" %T\n", hash_entry
->root
.string
);
2296 /* Print information about an input section to the map file. */
2299 print_input_section (lang_input_section_type
*in
)
2301 asection
*i
= in
->section
;
2302 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2309 minfo ("%s", i
->name
);
2311 if (i
->output_section
!= NULL
)
2315 len
= 1 + strlen (i
->name
);
2316 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2321 while (len
< SECTION_NAME_MAP_LENGTH
)
2327 minfo ("0x%V %W %B\n",
2328 i
->output_section
->vma
+ i
->output_offset
, TO_ADDR (size
),
2331 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2333 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2345 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2348 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
2350 print_dot
= (i
->output_section
->vma
+ i
->output_offset
2357 print_fill_statement (lang_fill_statement_type
*fill
)
2361 fputs (" FILL mask 0x", config
.map_file
);
2362 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2363 fprintf (config
.map_file
, "%02x", *p
);
2364 fputs ("\n", config
.map_file
);
2368 print_data_statement (lang_data_statement_type
*data
)
2376 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2379 addr
= data
->output_vma
;
2380 if (data
->output_section
!= NULL
)
2381 addr
+= data
->output_section
->vma
;
2409 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2411 if (data
->exp
->type
.node_class
!= etree_value
)
2414 exp_print_tree (data
->exp
);
2419 print_dot
= addr
+ TO_ADDR (size
);
2422 /* Print an address statement. These are generated by options like
2426 print_address_statement (lang_address_statement_type
*address
)
2428 minfo (_("Address of section %s set to "), address
->section_name
);
2429 exp_print_tree (address
->address
);
2433 /* Print a reloc statement. */
2436 print_reloc_statement (lang_reloc_statement_type
*reloc
)
2443 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2446 addr
= reloc
->output_vma
;
2447 if (reloc
->output_section
!= NULL
)
2448 addr
+= reloc
->output_section
->vma
;
2450 size
= bfd_get_reloc_size (reloc
->howto
);
2452 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2454 if (reloc
->name
!= NULL
)
2455 minfo ("%s+", reloc
->name
);
2457 minfo ("%s+", reloc
->section
->name
);
2459 exp_print_tree (reloc
->addend_exp
);
2463 print_dot
= addr
+ TO_ADDR (size
);
2467 print_padding_statement (lang_padding_statement_type
*s
)
2475 len
= sizeof " *fill*" - 1;
2476 while (len
< SECTION_NAME_MAP_LENGTH
)
2482 addr
= s
->output_offset
;
2483 if (s
->output_section
!= NULL
)
2484 addr
+= s
->output_section
->vma
;
2485 minfo ("0x%V %W ", addr
, s
->size
);
2487 if (s
->fill
->size
!= 0)
2491 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2492 fprintf (config
.map_file
, "%02x", *p
);
2497 print_dot
= addr
+ TO_ADDR (s
->size
);
2501 print_wild_statement (lang_wild_statement_type
*w
,
2502 lang_output_section_statement_type
*os
)
2504 struct wildcard_list
*sec
;
2508 if (w
->filenames_sorted
)
2510 if (w
->filename
!= NULL
)
2511 minfo ("%s", w
->filename
);
2514 if (w
->filenames_sorted
)
2518 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2520 if (sec
->spec
.sorted
)
2522 if (sec
->spec
.exclude_name_list
!= NULL
)
2525 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2526 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2527 minfo (" %s", tmp
->name
);
2530 if (sec
->spec
.name
!= NULL
)
2531 minfo ("%s", sec
->spec
.name
);
2534 if (sec
->spec
.sorted
)
2543 print_statement_list (w
->children
.head
, os
);
2546 /* Print a group statement. */
2549 print_group (lang_group_statement_type
*s
,
2550 lang_output_section_statement_type
*os
)
2552 fprintf (config
.map_file
, "START GROUP\n");
2553 print_statement_list (s
->children
.head
, os
);
2554 fprintf (config
.map_file
, "END GROUP\n");
2557 /* Print the list of statements in S.
2558 This can be called for any statement type. */
2561 print_statement_list (lang_statement_union_type
*s
,
2562 lang_output_section_statement_type
*os
)
2566 print_statement (s
, os
);
2571 /* Print the first statement in statement list S.
2572 This can be called for any statement type. */
2575 print_statement (lang_statement_union_type
*s
,
2576 lang_output_section_statement_type
*os
)
2578 switch (s
->header
.type
)
2581 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2584 case lang_constructors_statement_enum
:
2585 if (constructor_list
.head
!= NULL
)
2587 if (constructors_sorted
)
2588 minfo (" SORT (CONSTRUCTORS)\n");
2590 minfo (" CONSTRUCTORS\n");
2591 print_statement_list (constructor_list
.head
, os
);
2594 case lang_wild_statement_enum
:
2595 print_wild_statement (&s
->wild_statement
, os
);
2597 case lang_address_statement_enum
:
2598 print_address_statement (&s
->address_statement
);
2600 case lang_object_symbols_statement_enum
:
2601 minfo (" CREATE_OBJECT_SYMBOLS\n");
2603 case lang_fill_statement_enum
:
2604 print_fill_statement (&s
->fill_statement
);
2606 case lang_data_statement_enum
:
2607 print_data_statement (&s
->data_statement
);
2609 case lang_reloc_statement_enum
:
2610 print_reloc_statement (&s
->reloc_statement
);
2612 case lang_input_section_enum
:
2613 print_input_section (&s
->input_section
);
2615 case lang_padding_statement_enum
:
2616 print_padding_statement (&s
->padding_statement
);
2618 case lang_output_section_statement_enum
:
2619 print_output_section_statement (&s
->output_section_statement
);
2621 case lang_assignment_statement_enum
:
2622 print_assignment (&s
->assignment_statement
, os
);
2624 case lang_target_statement_enum
:
2625 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2627 case lang_output_statement_enum
:
2628 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2629 if (output_target
!= NULL
)
2630 minfo (" %s", output_target
);
2633 case lang_input_statement_enum
:
2634 print_input_statement (&s
->input_statement
);
2636 case lang_group_statement_enum
:
2637 print_group (&s
->group_statement
, os
);
2639 case lang_afile_asection_pair_statement_enum
:
2646 print_statements (void)
2648 print_statement_list (statement_list
.head
, abs_output_section
);
2651 /* Print the first N statements in statement list S to STDERR.
2652 If N == 0, nothing is printed.
2653 If N < 0, the entire list is printed.
2654 Intended to be called from GDB. */
2657 dprint_statement (lang_statement_union_type
*s
, int n
)
2659 FILE *map_save
= config
.map_file
;
2661 config
.map_file
= stderr
;
2664 print_statement_list (s
, abs_output_section
);
2667 while (s
&& --n
>= 0)
2669 print_statement (s
, abs_output_section
);
2674 config
.map_file
= map_save
;
2678 insert_pad (lang_statement_union_type
**ptr
,
2680 unsigned int alignment_needed
,
2681 asection
*output_section
,
2684 static fill_type zero_fill
= { 1, { 0 } };
2685 lang_statement_union_type
*pad
;
2687 pad
= ((lang_statement_union_type
*)
2688 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2689 if (ptr
!= &statement_list
.head
2690 && pad
->header
.type
== lang_padding_statement_enum
2691 && pad
->padding_statement
.output_section
== output_section
)
2693 /* Use the existing pad statement. The above test on output
2694 section is probably redundant, but it doesn't hurt to check. */
2698 /* Make a new padding statement, linked into existing chain. */
2699 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
2700 pad
->header
.next
= *ptr
;
2702 pad
->header
.type
= lang_padding_statement_enum
;
2703 pad
->padding_statement
.output_section
= output_section
;
2706 pad
->padding_statement
.fill
= fill
;
2708 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2709 pad
->padding_statement
.size
= alignment_needed
;
2710 output_section
->_raw_size
+= alignment_needed
;
2713 /* Work out how much this section will move the dot point. */
2716 size_input_section (lang_statement_union_type
**this_ptr
,
2717 lang_output_section_statement_type
*output_section_statement
,
2721 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2722 asection
*i
= is
->section
;
2724 if (!is
->ifile
->just_syms_flag
)
2726 unsigned int alignment_needed
;
2729 /* Align this section first to the input sections requirement,
2730 then to the output section's requirement. If this alignment
2731 is greater than any seen before, then record it too. Perform
2732 the alignment by inserting a magic 'padding' statement. */
2734 if (output_section_statement
->subsection_alignment
!= -1)
2735 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2737 o
= output_section_statement
->bfd_section
;
2738 if (o
->alignment_power
< i
->alignment_power
)
2739 o
->alignment_power
= i
->alignment_power
;
2741 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2743 if (alignment_needed
!= 0)
2745 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
2746 dot
+= alignment_needed
;
2749 /* Remember where in the output section this input section goes. */
2751 i
->output_offset
= dot
- o
->vma
;
2753 /* Mark how big the output section must be to contain this now. */
2754 if (i
->_cooked_size
!= 0)
2755 dot
+= TO_ADDR (i
->_cooked_size
);
2757 dot
+= TO_ADDR (i
->_raw_size
);
2758 o
->_raw_size
= TO_SIZE (dot
- o
->vma
);
2762 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2768 #define IGNORE_SECTION(bfd, s) \
2769 (((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2770 ? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2772 : ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2774 || bfd_section_size (bfd, s) == 0)
2776 /* Check to see if any allocated sections overlap with other allocated
2777 sections. This can happen when the linker script specifically specifies
2778 the output section addresses of the two sections. */
2781 lang_check_section_addresses (void)
2785 /* Scan all sections in the output list. */
2786 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2790 /* Ignore sections which are not loaded or which have no contents. */
2791 if (IGNORE_SECTION (output_bfd
, s
))
2794 /* Once we reach section 's' stop our seach. This prevents two
2795 warning messages from being produced, one for 'section A overlaps
2796 section B' and one for 'section B overlaps section A'. */
2797 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2804 /* Only consider loadable sections with real contents. */
2805 if (IGNORE_SECTION (output_bfd
, os
))
2808 /* We must check the sections' LMA addresses not their
2809 VMA addresses because overlay sections can have
2810 overlapping VMAs but they must have distinct LMAs. */
2811 s_start
= bfd_section_lma (output_bfd
, s
);
2812 os_start
= bfd_section_lma (output_bfd
, os
);
2813 s_end
= s_start
+ TO_ADDR (bfd_section_size (output_bfd
, s
)) - 1;
2814 os_end
= os_start
+ TO_ADDR (bfd_section_size (output_bfd
, os
)) - 1;
2816 /* Look for an overlap. */
2817 if ((s_end
< os_start
) || (s_start
> os_end
))
2821 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2822 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2824 /* Once we have found one overlap for this section,
2825 stop looking for others. */
2831 /* Make sure the new address is within the region. We explicitly permit the
2832 current address to be at the exact end of the region when the address is
2833 non-zero, in case the region is at the end of addressable memory and the
2834 calculation wraps around. */
2837 os_region_check (lang_output_section_statement_type
*os
,
2838 lang_memory_region_type
*region
,
2842 if ((region
->current
< region
->origin
2843 || (region
->current
- region
->origin
> region
->length
))
2844 && ((region
->current
!= region
->origin
+ region
->length
)
2849 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2851 os
->bfd_section
->owner
,
2852 os
->bfd_section
->name
,
2857 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2859 os
->bfd_section
->owner
,
2860 os
->bfd_section
->name
);
2862 /* Reset the region pointer. */
2863 region
->current
= region
->origin
;
2867 /* Set the sizes for all the output sections. */
2870 lang_size_sections_1
2871 (lang_statement_union_type
*s
,
2872 lang_output_section_statement_type
*output_section_statement
,
2873 lang_statement_union_type
**prev
,
2877 bfd_boolean check_regions
)
2879 /* Size up the sections from their constituent parts. */
2880 for (; s
!= NULL
; s
= s
->header
.next
)
2882 switch (s
->header
.type
)
2884 case lang_output_section_statement_enum
:
2887 lang_output_section_statement_type
*os
;
2889 os
= &s
->output_section_statement
;
2890 if (os
->bfd_section
== NULL
)
2891 /* This section was never actually created. */
2894 /* If this is a COFF shared library section, use the size and
2895 address from the input section. FIXME: This is COFF
2896 specific; it would be cleaner if there were some other way
2897 to do this, but nothing simple comes to mind. */
2898 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2902 if (os
->children
.head
== NULL
2903 || os
->children
.head
->header
.next
!= NULL
2904 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2905 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2908 input
= os
->children
.head
->input_section
.section
;
2909 bfd_set_section_vma (os
->bfd_section
->owner
,
2911 bfd_section_vma (input
->owner
, input
));
2912 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2916 if (bfd_is_abs_section (os
->bfd_section
))
2918 /* No matter what happens, an abs section starts at zero. */
2919 ASSERT (os
->bfd_section
->vma
== 0);
2923 if (os
->addr_tree
== NULL
)
2925 /* No address specified for this section, get one
2926 from the region specification. */
2927 if (os
->region
== NULL
2928 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2929 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2930 && os
->region
->name
[0] == '*'
2931 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0))
2933 os
->region
= lang_memory_default (os
->bfd_section
);
2936 /* If a loadable section is using the default memory
2937 region, and some non default memory regions were
2938 defined, issue an error message. */
2939 if (!IGNORE_SECTION (output_bfd
, os
->bfd_section
)
2940 && ! link_info
.relocatable
2942 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0
2943 && lang_memory_region_list
!= NULL
2944 && (strcmp (lang_memory_region_list
->name
,
2945 DEFAULT_MEMORY_REGION
) != 0
2946 || lang_memory_region_list
->next
!= NULL
))
2948 /* By default this is an error rather than just a
2949 warning because if we allocate the section to the
2950 default memory region we can end up creating an
2951 excessively large binary, or even seg faulting when
2952 attempting to perform a negative seek. See
2953 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2954 for an example of this. This behaviour can be
2955 overridden by the using the --no-check-sections
2957 if (command_line
.check_section_addresses
)
2958 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2959 bfd_get_section_name (output_bfd
,
2962 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2963 bfd_get_section_name (output_bfd
,
2967 dot
= os
->region
->current
;
2969 if (os
->section_alignment
== -1)
2974 dot
= align_power (dot
,
2975 os
->bfd_section
->alignment_power
);
2977 if (dot
!= olddot
&& config
.warn_section_align
)
2978 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2979 os
->name
, (unsigned int) (dot
- olddot
));
2987 r
= exp_fold_tree (os
->addr_tree
,
2989 lang_allocating_phase_enum
,
2994 einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
2997 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
3000 /* The section starts here.
3001 First, align to what the section needs. */
3003 if (os
->section_alignment
!= -1)
3004 dot
= align_power (dot
, os
->section_alignment
);
3006 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3008 os
->bfd_section
->output_offset
= 0;
3011 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3012 os
->fill
, dot
, relax
, check_regions
);
3014 /* Put the section within the requested block size, or
3015 align at the block boundary. */
3016 after
= ((os
->bfd_section
->vma
3017 + TO_ADDR (os
->bfd_section
->_raw_size
)
3018 + os
->block_value
- 1)
3019 & - (bfd_vma
) os
->block_value
);
3021 if (bfd_is_abs_section (os
->bfd_section
))
3022 ASSERT (after
== os
->bfd_section
->vma
);
3024 os
->bfd_section
->_raw_size
3025 = TO_SIZE (after
- os
->bfd_section
->vma
);
3027 dot
= os
->bfd_section
->vma
;
3028 /* .tbss sections effectively have zero size. */
3029 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3030 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3031 || link_info
.relocatable
)
3032 dot
+= TO_ADDR (os
->bfd_section
->_raw_size
);
3036 if (os
->update_dot_tree
!= 0)
3037 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3038 lang_allocating_phase_enum
, dot
, &dot
);
3040 /* Update dot in the region ?
3041 We only do this if the section is going to be allocated,
3042 since unallocated sections do not contribute to the region's
3043 overall size in memory.
3045 If the SEC_NEVER_LOAD bit is not set, it will affect the
3046 addresses of sections after it. We have to update
3048 if (os
->region
!= NULL
3049 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3050 & SEC_NEVER_LOAD
) == 0
3051 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3052 & (SEC_ALLOC
| SEC_LOAD
))))
3054 os
->region
->current
= dot
;
3057 /* Make sure the new address is within the region. */
3058 os_region_check (os
, os
->region
, os
->addr_tree
,
3059 os
->bfd_section
->vma
);
3061 /* If there's no load address specified, use the run
3062 region as the load region. */
3063 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3064 os
->lma_region
= os
->region
;
3066 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3068 /* Set load_base, which will be handled later. */
3069 os
->load_base
= exp_intop (os
->lma_region
->current
);
3070 os
->lma_region
->current
+=
3071 TO_ADDR (os
->bfd_section
->_raw_size
);
3073 os_region_check (os
, os
->lma_region
, NULL
,
3074 os
->bfd_section
->lma
);
3080 case lang_constructors_statement_enum
:
3081 dot
= lang_size_sections_1 (constructor_list
.head
,
3082 output_section_statement
,
3083 &s
->wild_statement
.children
.head
,
3084 fill
, dot
, relax
, check_regions
);
3087 case lang_data_statement_enum
:
3089 unsigned int size
= 0;
3091 s
->data_statement
.output_vma
=
3092 dot
- output_section_statement
->bfd_section
->vma
;
3093 s
->data_statement
.output_section
=
3094 output_section_statement
->bfd_section
;
3096 /* We might refer to provided symbols in the expression, and
3097 need to mark them as needed. */
3098 exp_fold_tree (s
->data_statement
.exp
, abs_output_section
,
3099 lang_allocating_phase_enum
, dot
, &dot
);
3101 switch (s
->data_statement
.type
)
3119 if (size
< TO_SIZE ((unsigned) 1))
3120 size
= TO_SIZE ((unsigned) 1);
3121 dot
+= TO_ADDR (size
);
3122 output_section_statement
->bfd_section
->_raw_size
+= size
;
3123 /* The output section gets contents, and then we inspect for
3124 any flags set in the input script which override any ALLOC. */
3125 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3126 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3128 output_section_statement
->bfd_section
->flags
|=
3129 SEC_ALLOC
| SEC_LOAD
;
3134 case lang_reloc_statement_enum
:
3138 s
->reloc_statement
.output_vma
=
3139 dot
- output_section_statement
->bfd_section
->vma
;
3140 s
->reloc_statement
.output_section
=
3141 output_section_statement
->bfd_section
;
3142 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3143 dot
+= TO_ADDR (size
);
3144 output_section_statement
->bfd_section
->_raw_size
+= size
;
3148 case lang_wild_statement_enum
:
3150 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3151 output_section_statement
,
3152 &s
->wild_statement
.children
.head
,
3153 fill
, dot
, relax
, check_regions
);
3157 case lang_object_symbols_statement_enum
:
3158 link_info
.create_object_symbols_section
=
3159 output_section_statement
->bfd_section
;
3161 case lang_output_statement_enum
:
3162 case lang_target_statement_enum
:
3164 case lang_input_section_enum
:
3168 i
= (*prev
)->input_section
.section
;
3171 if (i
->_cooked_size
== 0)
3172 i
->_cooked_size
= i
->_raw_size
;
3178 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3179 einfo (_("%P%F: can't relax section: %E\n"));
3183 dot
= size_input_section (prev
, output_section_statement
,
3184 output_section_statement
->fill
, dot
);
3187 case lang_input_statement_enum
:
3189 case lang_fill_statement_enum
:
3190 s
->fill_statement
.output_section
=
3191 output_section_statement
->bfd_section
;
3193 fill
= s
->fill_statement
.fill
;
3195 case lang_assignment_statement_enum
:
3197 bfd_vma newdot
= dot
;
3199 exp_fold_tree (s
->assignment_statement
.exp
,
3200 output_section_statement
,
3201 lang_allocating_phase_enum
,
3207 if (output_section_statement
== abs_output_section
)
3209 /* If we don't have an output section, then just adjust
3210 the default memory address. */
3211 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
)->current
= newdot
;
3215 /* Insert a pad after this statement. We can't
3216 put the pad before when relaxing, in case the
3217 assignment references dot. */
3218 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
3219 output_section_statement
->bfd_section
, dot
);
3221 /* Don't neuter the pad below when relaxing. */
3225 /* If dot is advanced, this implies that the section should
3226 have space allocated to it, unless the user has explicitly
3227 stated that the section should never be loaded. */
3228 if (!(output_section_statement
->flags
& (SEC_NEVER_LOAD
| SEC_ALLOC
)))
3229 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
3236 case lang_padding_statement_enum
:
3237 /* If this is the first time lang_size_sections is called,
3238 we won't have any padding statements. If this is the
3239 second or later passes when relaxing, we should allow
3240 padding to shrink. If padding is needed on this pass, it
3241 will be added back in. */
3242 s
->padding_statement
.size
= 0;
3244 /* Make sure output_offset is valid. If relaxation shrinks
3245 the section and this pad isn't needed, it's possible to
3246 have output_offset larger than the final size of the
3247 section. bfd_set_section_contents will complain even for
3248 a pad size of zero. */
3249 s
->padding_statement
.output_offset
3250 = dot
- output_section_statement
->bfd_section
->vma
;
3253 case lang_group_statement_enum
:
3254 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3255 output_section_statement
,
3256 &s
->group_statement
.children
.head
,
3257 fill
, dot
, relax
, check_regions
);
3264 /* We can only get here when relaxing is turned on. */
3265 case lang_address_statement_enum
:
3268 prev
= &s
->header
.next
;
3275 (lang_statement_union_type
*s
,
3276 lang_output_section_statement_type
*output_section_statement
,
3277 lang_statement_union_type
**prev
,
3281 bfd_boolean check_regions
)
3286 /* Callers of exp_fold_tree need to increment this. */
3287 lang_statement_iteration
++;
3289 exp_data_seg
.phase
= exp_dataseg_none
;
3290 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3291 dot
, relax
, check_regions
);
3292 if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3294 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3295 a page could be saved in the data segment. */
3296 bfd_vma first
, last
;
3298 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3299 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3301 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3302 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3303 && first
+ last
<= exp_data_seg
.pagesize
)
3305 exp_data_seg
.phase
= exp_dataseg_adjust
;
3306 lang_statement_iteration
++;
3307 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3308 fill
, dot
, relax
, check_regions
);
3312 /* Some backend relaxers want to refer to the output section size. Give
3313 them a section size that does not change on the next call while they
3314 relax. We can't set this at top because lang_reset_memory_regions
3315 which is called before we get here, sets _raw_size to 0 on relaxing
3317 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3318 o
->_cooked_size
= o
->_raw_size
;
3323 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3326 lang_do_assignments_1
3327 (lang_statement_union_type
*s
,
3328 lang_output_section_statement_type
*output_section_statement
,
3332 for (; s
!= NULL
; s
= s
->header
.next
)
3334 switch (s
->header
.type
)
3336 case lang_constructors_statement_enum
:
3337 dot
= lang_do_assignments_1 (constructor_list
.head
,
3338 output_section_statement
,
3343 case lang_output_section_statement_enum
:
3345 lang_output_section_statement_type
*os
;
3347 os
= &(s
->output_section_statement
);
3348 if (os
->bfd_section
!= NULL
)
3350 dot
= os
->bfd_section
->vma
;
3351 (void) lang_do_assignments_1 (os
->children
.head
, os
,
3353 dot
= (os
->bfd_section
->vma
3354 + TO_ADDR (os
->bfd_section
->_raw_size
));
3359 /* If nothing has been placed into the output section then
3360 it won't have a bfd_section. */
3361 if (os
->bfd_section
)
3363 os
->bfd_section
->lma
3364 = exp_get_abs_int (os
->load_base
, 0, "load base",
3365 lang_final_phase_enum
);
3370 case lang_wild_statement_enum
:
3372 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
3373 output_section_statement
,
3378 case lang_object_symbols_statement_enum
:
3379 case lang_output_statement_enum
:
3380 case lang_target_statement_enum
:
3382 case lang_common_statement_enum
:
3385 case lang_data_statement_enum
:
3387 etree_value_type value
;
3389 value
= exp_fold_tree (s
->data_statement
.exp
,
3391 lang_final_phase_enum
, dot
, &dot
);
3393 einfo (_("%F%P: invalid data statement\n"));
3394 s
->data_statement
.value
3395 = value
.value
+ value
.section
->bfd_section
->vma
;
3399 switch (s
->data_statement
.type
)
3417 if (size
< TO_SIZE ((unsigned) 1))
3418 size
= TO_SIZE ((unsigned) 1);
3419 dot
+= TO_ADDR (size
);
3423 case lang_reloc_statement_enum
:
3425 etree_value_type value
;
3427 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3429 lang_final_phase_enum
, dot
, &dot
);
3430 s
->reloc_statement
.addend_value
= value
.value
;
3432 einfo (_("%F%P: invalid reloc statement\n"));
3434 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
3437 case lang_input_section_enum
:
3439 asection
*in
= s
->input_section
.section
;
3441 if (in
->_cooked_size
!= 0)
3442 dot
+= TO_ADDR (in
->_cooked_size
);
3444 dot
+= TO_ADDR (in
->_raw_size
);
3448 case lang_input_statement_enum
:
3450 case lang_fill_statement_enum
:
3451 fill
= s
->fill_statement
.fill
;
3453 case lang_assignment_statement_enum
:
3455 exp_fold_tree (s
->assignment_statement
.exp
,
3456 output_section_statement
,
3457 lang_final_phase_enum
,
3463 case lang_padding_statement_enum
:
3464 dot
+= TO_ADDR (s
->padding_statement
.size
);
3467 case lang_group_statement_enum
:
3468 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
3469 output_section_statement
,
3477 case lang_address_statement_enum
:
3486 lang_do_assignments (lang_statement_union_type
*s
,
3487 lang_output_section_statement_type
*output_section_statement
,
3491 /* Callers of exp_fold_tree need to increment this. */
3492 lang_statement_iteration
++;
3493 lang_do_assignments_1 (s
, output_section_statement
, fill
, dot
);
3496 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3497 operator .startof. (section_name), it produces an undefined symbol
3498 .startof.section_name. Similarly, when it sees
3499 .sizeof. (section_name), it produces an undefined symbol
3500 .sizeof.section_name. For all the output sections, we look for
3501 such symbols, and set them to the correct value. */
3504 lang_set_startof (void)
3508 if (link_info
.relocatable
)
3511 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3513 const char *secname
;
3515 struct bfd_link_hash_entry
*h
;
3517 secname
= bfd_get_section_name (output_bfd
, s
);
3518 buf
= xmalloc (10 + strlen (secname
));
3520 sprintf (buf
, ".startof.%s", secname
);
3521 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3522 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3524 h
->type
= bfd_link_hash_defined
;
3525 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3526 h
->u
.def
.section
= bfd_abs_section_ptr
;
3529 sprintf (buf
, ".sizeof.%s", secname
);
3530 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3531 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3533 h
->type
= bfd_link_hash_defined
;
3534 if (s
->_cooked_size
!= 0)
3535 h
->u
.def
.value
= TO_ADDR (s
->_cooked_size
);
3537 h
->u
.def
.value
= TO_ADDR (s
->_raw_size
);
3538 h
->u
.def
.section
= bfd_abs_section_ptr
;
3548 struct bfd_link_hash_entry
*h
;
3551 if (link_info
.relocatable
|| link_info
.shared
)
3556 if (entry_symbol
.name
== NULL
)
3558 /* No entry has been specified. Look for start, but don't warn
3559 if we don't find it. */
3560 entry_symbol
.name
= "start";
3564 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3565 FALSE
, FALSE
, TRUE
);
3567 && (h
->type
== bfd_link_hash_defined
3568 || h
->type
== bfd_link_hash_defweak
)
3569 && h
->u
.def
.section
->output_section
!= NULL
)
3573 val
= (h
->u
.def
.value
3574 + bfd_get_section_vma (output_bfd
,
3575 h
->u
.def
.section
->output_section
)
3576 + h
->u
.def
.section
->output_offset
);
3577 if (! bfd_set_start_address (output_bfd
, val
))
3578 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3585 /* We couldn't find the entry symbol. Try parsing it as a
3587 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3590 if (! bfd_set_start_address (output_bfd
, val
))
3591 einfo (_("%P%F: can't set start address\n"));
3597 /* Can't find the entry symbol, and it's not a number. Use
3598 the first address in the text section. */
3599 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3603 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3605 bfd_get_section_vma (output_bfd
, ts
));
3606 if (! bfd_set_start_address (output_bfd
,
3607 bfd_get_section_vma (output_bfd
,
3609 einfo (_("%P%F: can't set start address\n"));
3614 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3620 bfd_hash_table_free (&lang_definedness_table
);
3623 /* This is a small function used when we want to ignore errors from
3627 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3629 /* Don't do anything. */
3632 /* Check that the architecture of all the input files is compatible
3633 with the output file. Also call the backend to let it do any
3634 other checking that is needed. */
3639 lang_statement_union_type
*file
;
3641 const bfd_arch_info_type
*compatible
;
3643 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
3645 input_bfd
= file
->input_statement
.the_bfd
;
3646 compatible
= bfd_arch_get_compatible (input_bfd
, output_bfd
,
3647 command_line
.accept_unknown_input_arch
);
3649 /* In general it is not possible to perform a relocatable
3650 link between differing object formats when the input
3651 file has relocations, because the relocations in the
3652 input format may not have equivalent representations in
3653 the output format (and besides BFD does not translate
3654 relocs for other link purposes than a final link). */
3655 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
3656 && (compatible
== NULL
3657 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3658 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3660 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3661 bfd_get_target (input_bfd
), input_bfd
,
3662 bfd_get_target (output_bfd
), output_bfd
);
3663 /* einfo with %F exits. */
3666 if (compatible
== NULL
)
3668 if (command_line
.warn_mismatch
)
3669 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3670 bfd_printable_name (input_bfd
), input_bfd
,
3671 bfd_printable_name (output_bfd
));
3673 else if (bfd_count_sections (input_bfd
))
3675 /* If the input bfd has no contents, it shouldn't set the
3676 private data of the output bfd. */
3678 bfd_error_handler_type pfn
= NULL
;
3680 /* If we aren't supposed to warn about mismatched input
3681 files, temporarily set the BFD error handler to a
3682 function which will do nothing. We still want to call
3683 bfd_merge_private_bfd_data, since it may set up
3684 information which is needed in the output file. */
3685 if (! command_line
.warn_mismatch
)
3686 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3687 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3689 if (command_line
.warn_mismatch
)
3690 einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3693 if (! command_line
.warn_mismatch
)
3694 bfd_set_error_handler (pfn
);
3699 /* Look through all the global common symbols and attach them to the
3700 correct section. The -sort-common command line switch may be used
3701 to roughly sort the entries by size. */
3706 if (command_line
.inhibit_common_definition
)
3708 if (link_info
.relocatable
3709 && ! command_line
.force_common_definition
)
3712 if (! config
.sort_common
)
3713 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
3718 for (power
= 4; power
>= 0; power
--)
3719 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
3723 /* Place one common symbol in the correct section. */
3726 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
3728 unsigned int power_of_two
;
3732 if (h
->type
!= bfd_link_hash_common
)
3736 power_of_two
= h
->u
.c
.p
->alignment_power
;
3738 if (config
.sort_common
3739 && power_of_two
< (unsigned int) *(int *) info
)
3742 section
= h
->u
.c
.p
->section
;
3744 /* Increase the size of the section to align the common sym. */
3745 section
->_cooked_size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
3746 section
->_cooked_size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
3748 /* Adjust the alignment if necessary. */
3749 if (power_of_two
> section
->alignment_power
)
3750 section
->alignment_power
= power_of_two
;
3752 /* Change the symbol from common to defined. */
3753 h
->type
= bfd_link_hash_defined
;
3754 h
->u
.def
.section
= section
;
3755 h
->u
.def
.value
= section
->_cooked_size
;
3757 /* Increase the size of the section. */
3758 section
->_cooked_size
+= size
;
3760 /* Make sure the section is allocated in memory, and make sure that
3761 it is no longer a common section. */
3762 section
->flags
|= SEC_ALLOC
;
3763 section
->flags
&= ~SEC_IS_COMMON
;
3765 if (config
.map_file
!= NULL
)
3767 static bfd_boolean header_printed
;
3772 if (! header_printed
)
3774 minfo (_("\nAllocating common symbols\n"));
3775 minfo (_("Common symbol size file\n\n"));
3776 header_printed
= TRUE
;
3779 name
= demangle (h
->root
.string
);
3781 len
= strlen (name
);
3796 if (size
<= 0xffffffff)
3797 sprintf (buf
, "%lx", (unsigned long) size
);
3799 sprintf_vma (buf
, size
);
3809 minfo ("%B\n", section
->owner
);
3815 /* Run through the input files and ensure that every input section has
3816 somewhere to go. If one is found without a destination then create
3817 an input request and place it into the statement tree. */
3820 lang_place_orphans (void)
3822 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3826 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3828 if (s
->output_section
== NULL
)
3830 /* This section of the file is not attached, root
3831 around for a sensible place for it to go. */
3833 if (file
->just_syms_flag
)
3837 else if (strcmp (s
->name
, "COMMON") == 0)
3839 /* This is a lonely common section which must have
3840 come from an archive. We attach to the section
3841 with the wildcard. */
3842 if (! link_info
.relocatable
3843 || command_line
.force_common_definition
)
3845 if (default_common_section
== NULL
)
3848 /* This message happens when using the
3849 svr3.ifile linker script, so I have
3851 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3853 default_common_section
=
3854 lang_output_section_statement_lookup (".bss");
3857 lang_add_section (&default_common_section
->children
, s
,
3858 default_common_section
, file
);
3861 else if (ldemul_place_orphan (file
, s
))
3865 lang_output_section_statement_type
*os
;
3867 os
= lang_output_section_statement_lookup (s
->name
);
3868 lang_add_section (&os
->children
, s
, os
, file
);
3876 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
3878 flagword
*ptr_flags
;
3880 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3886 *ptr_flags
|= SEC_ALLOC
;
3890 *ptr_flags
|= SEC_READONLY
;
3894 *ptr_flags
|= SEC_DATA
;
3898 *ptr_flags
|= SEC_CODE
;
3903 *ptr_flags
|= SEC_LOAD
;
3907 einfo (_("%P%F: invalid syntax in flags\n"));
3914 /* Call a function on each input file. This function will be called
3915 on an archive, but not on the elements. */
3918 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
3920 lang_input_statement_type
*f
;
3922 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3924 f
= (lang_input_statement_type
*) f
->next_real_file
)
3928 /* Call a function on each file. The function will be called on all
3929 the elements of an archive which are included in the link, but will
3930 not be called on the archive file itself. */
3933 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
3935 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3946 lang_for_each_input_section (void (*func
) (bfd
*ab
, asection
*as
))
3948 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3952 for (s
= f
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3953 func (f
->the_bfd
, s
);
3960 ldlang_add_file (lang_input_statement_type
*entry
)
3964 lang_statement_append (&file_chain
,
3965 (lang_statement_union_type
*) entry
,
3968 /* The BFD linker needs to have a list of all input BFDs involved in
3970 ASSERT (entry
->the_bfd
->link_next
== NULL
);
3971 ASSERT (entry
->the_bfd
!= output_bfd
);
3972 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
3974 *pp
= entry
->the_bfd
;
3975 entry
->the_bfd
->usrdata
= entry
;
3976 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3978 /* Look through the sections and check for any which should not be
3979 included in the link. We need to do this now, so that we can
3980 notice when the backend linker tries to report multiple
3981 definition errors for symbols which are in sections we aren't
3982 going to link. FIXME: It might be better to entirely ignore
3983 symbols which are defined in sections which are going to be
3984 discarded. This would require modifying the backend linker for
3985 each backend which might set the SEC_LINK_ONCE flag. If we do
3986 this, we should probably handle SEC_EXCLUDE in the same way. */
3988 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
3992 lang_add_output (const char *name
, int from_script
)
3994 /* Make -o on command line override OUTPUT in script. */
3995 if (!had_output_filename
|| !from_script
)
3997 output_filename
= name
;
3998 had_output_filename
= TRUE
;
4002 static lang_output_section_statement_type
*current_section
;
4013 for (l
= 0; l
< 32; l
++)
4015 if (i
>= (unsigned int) x
)
4023 lang_output_section_statement_type
*
4024 lang_enter_output_section_statement (const char *output_section_statement_name
,
4025 etree_type
*address_exp
,
4026 enum section_type sectype
,
4028 etree_type
*subalign
,
4031 lang_output_section_statement_type
*os
;
4035 lang_output_section_statement_lookup (output_section_statement_name
);
4037 /* Add this statement to tree. */
4039 add_statement (lang_output_section_statement_enum
,
4040 output_section_statement
);
4042 /* Make next things chain into subchain of this. */
4044 if (os
->addr_tree
== NULL
)
4046 os
->addr_tree
= address_exp
;
4048 os
->sectype
= sectype
;
4049 if (sectype
!= noload_section
)
4050 os
->flags
= SEC_NO_FLAGS
;
4052 os
->flags
= SEC_NEVER_LOAD
;
4053 os
->block_value
= 1;
4054 stat_ptr
= &os
->children
;
4056 os
->subsection_alignment
=
4057 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4058 os
->section_alignment
=
4059 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4061 os
->load_base
= ebase
;
4068 lang_output_statement_type
*new =
4069 new_stat (lang_output_statement
, stat_ptr
);
4071 new->name
= output_filename
;
4074 /* Reset the current counters in the regions. */
4077 lang_reset_memory_regions (void)
4079 lang_memory_region_type
*p
= lang_memory_region_list
;
4082 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
4084 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4085 p
->current
= p
->origin
;
4088 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4092 /* If the wild pattern was marked KEEP, the member sections
4093 should be as well. */
4096 gc_section_callback (lang_wild_statement_type
*ptr
,
4097 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
4099 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
4100 void *data ATTRIBUTE_UNUSED
)
4102 if (ptr
->keep_sections
)
4103 section
->flags
|= SEC_KEEP
;
4106 /* Handle a wild statement, marking it against GC. */
4109 lang_gc_wild (lang_wild_statement_type
*s
)
4111 walk_wild (s
, gc_section_callback
, NULL
);
4114 /* Iterate over sections marking them against GC. */
4117 lang_gc_sections_1 (lang_statement_union_type
*s
)
4119 for (; s
!= NULL
; s
= s
->header
.next
)
4121 switch (s
->header
.type
)
4123 case lang_wild_statement_enum
:
4124 lang_gc_wild (&s
->wild_statement
);
4126 case lang_constructors_statement_enum
:
4127 lang_gc_sections_1 (constructor_list
.head
);
4129 case lang_output_section_statement_enum
:
4130 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4132 case lang_group_statement_enum
:
4133 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4142 lang_gc_sections (void)
4144 struct bfd_link_hash_entry
*h
;
4145 ldlang_undef_chain_list_type
*ulist
;
4147 /* Keep all sections so marked in the link script. */
4149 lang_gc_sections_1 (statement_list
.head
);
4151 /* Keep all sections containing symbols undefined on the command-line,
4152 and the section containing the entry symbol. */
4154 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4156 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4157 FALSE
, FALSE
, FALSE
);
4160 && (h
->type
== bfd_link_hash_defined
4161 || h
->type
== bfd_link_hash_defweak
)
4162 && ! bfd_is_abs_section (h
->u
.def
.section
))
4164 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4168 bfd_gc_sections (output_bfd
, &link_info
);
4174 lang_reasonable_defaults ();
4175 current_target
= default_target
;
4177 /* Open the output file. */
4178 lang_for_each_statement (ldlang_open_output
);
4181 ldemul_create_output_section_statements ();
4183 /* Add to the hash table all undefineds on the command line. */
4184 lang_place_undefineds ();
4186 already_linked_table_init ();
4188 /* Create a bfd for each input file. */
4189 current_target
= default_target
;
4190 open_input_bfds (statement_list
.head
, FALSE
);
4192 link_info
.gc_sym_list
= &entry_symbol
;
4193 if (entry_symbol
.name
== NULL
)
4194 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4196 ldemul_after_open ();
4198 already_linked_table_free ();
4200 /* Make sure that we're not mixing architectures. We call this
4201 after all the input files have been opened, but before we do any
4202 other processing, so that any operations merge_private_bfd_data
4203 does on the output file will be known during the rest of the
4207 /* Handle .exports instead of a version script if we're told to do so. */
4208 if (command_line
.version_exports_section
)
4209 lang_do_version_exports_section ();
4211 /* Build all sets based on the information gathered from the input
4213 ldctor_build_sets ();
4215 /* Remove unreferenced sections if asked to. */
4216 if (command_line
.gc_sections
)
4217 lang_gc_sections ();
4219 /* If there were any SEC_MERGE sections, finish their merging, so that
4220 section sizes can be computed. This has to be done after GC of sections,
4221 so that GCed sections are not merged, but before assigning output
4222 sections, since removing whole input sections is hard then. */
4223 bfd_merge_sections (output_bfd
, &link_info
);
4225 /* Size up the common data. */
4228 /* Run through the contours of the script and attach input sections
4229 to the correct output sections. */
4230 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
4232 /* Find any sections not attached explicitly and handle them. */
4233 lang_place_orphans ();
4235 if (! link_info
.relocatable
)
4237 /* Look for a text section and set the readonly attribute in it. */
4238 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
4242 if (config
.text_read_only
)
4243 found
->flags
|= SEC_READONLY
;
4245 found
->flags
&= ~SEC_READONLY
;
4249 /* Do anything special before sizing sections. This is where ELF
4250 and other back-ends size dynamic sections. */
4251 ldemul_before_allocation ();
4253 if (!link_info
.relocatable
)
4254 strip_excluded_output_sections ();
4256 /* We must record the program headers before we try to fix the
4257 section positions, since they will affect SIZEOF_HEADERS. */
4258 lang_record_phdrs ();
4260 /* Size up the sections. */
4261 lang_size_sections (statement_list
.head
, abs_output_section
,
4262 &statement_list
.head
, 0, 0, NULL
,
4263 command_line
.relax
? FALSE
: TRUE
);
4265 /* Now run around and relax if we can. */
4266 if (command_line
.relax
)
4268 /* Keep relaxing until bfd_relax_section gives up. */
4269 bfd_boolean relax_again
;
4273 relax_again
= FALSE
;
4275 /* Note: pe-dll.c does something like this also. If you find
4276 you need to change this code, you probably need to change
4277 pe-dll.c also. DJ */
4279 /* Do all the assignments with our current guesses as to
4281 lang_do_assignments (statement_list
.head
, abs_output_section
,
4284 /* We must do this after lang_do_assignments, because it uses
4286 lang_reset_memory_regions ();
4288 /* Perform another relax pass - this time we know where the
4289 globals are, so can make a better guess. */
4290 lang_size_sections (statement_list
.head
, abs_output_section
,
4291 &statement_list
.head
, 0, 0, &relax_again
, FALSE
);
4293 /* If the normal relax is done and the relax finalize pass
4294 is not performed yet, we perform another relax pass. */
4295 if (!relax_again
&& link_info
.need_relax_finalize
)
4297 link_info
.need_relax_finalize
= FALSE
;
4301 while (relax_again
);
4303 /* Final extra sizing to report errors. */
4304 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4305 lang_reset_memory_regions ();
4306 lang_size_sections (statement_list
.head
, abs_output_section
,
4307 &statement_list
.head
, 0, 0, NULL
, TRUE
);
4310 /* See if anything special should be done now we know how big
4312 ldemul_after_allocation ();
4314 /* Fix any .startof. or .sizeof. symbols. */
4315 lang_set_startof ();
4317 /* Do all the assignments, now that we know the final resting places
4318 of all the symbols. */
4320 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4322 /* Make sure that the section addresses make sense. */
4323 if (! link_info
.relocatable
4324 && command_line
.check_section_addresses
)
4325 lang_check_section_addresses ();
4333 /* EXPORTED TO YACC */
4336 lang_add_wild (struct wildcard_spec
*filespec
,
4337 struct wildcard_list
*section_list
,
4338 bfd_boolean keep_sections
)
4340 struct wildcard_list
*curr
, *next
;
4341 lang_wild_statement_type
*new;
4343 /* Reverse the list as the parser puts it back to front. */
4344 for (curr
= section_list
, section_list
= NULL
;
4346 section_list
= curr
, curr
= next
)
4348 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4349 placed_commons
= TRUE
;
4352 curr
->next
= section_list
;
4355 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4357 if (strcmp (filespec
->name
, "*") == 0)
4358 filespec
->name
= NULL
;
4359 else if (! wildcardp (filespec
->name
))
4360 lang_has_input_file
= TRUE
;
4363 new = new_stat (lang_wild_statement
, stat_ptr
);
4364 new->filename
= NULL
;
4365 new->filenames_sorted
= FALSE
;
4366 if (filespec
!= NULL
)
4368 new->filename
= filespec
->name
;
4369 new->filenames_sorted
= filespec
->sorted
;
4371 new->section_list
= section_list
;
4372 new->keep_sections
= keep_sections
;
4373 lang_list_init (&new->children
);
4377 lang_section_start (const char *name
, etree_type
*address
)
4379 lang_address_statement_type
*ad
;
4381 ad
= new_stat (lang_address_statement
, stat_ptr
);
4382 ad
->section_name
= name
;
4383 ad
->address
= address
;
4386 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4387 because of a -e argument on the command line, or zero if this is
4388 called by ENTRY in a linker script. Command line arguments take
4392 lang_add_entry (const char *name
, bfd_boolean cmdline
)
4394 if (entry_symbol
.name
== NULL
4396 || ! entry_from_cmdline
)
4398 entry_symbol
.name
= name
;
4399 entry_from_cmdline
= cmdline
;
4404 lang_add_target (const char *name
)
4406 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4414 lang_add_map (const char *name
)
4421 map_option_f
= TRUE
;
4429 lang_add_fill (fill_type
*fill
)
4431 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4438 lang_add_data (int type
, union etree_union
*exp
)
4441 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4449 /* Create a new reloc statement. RELOC is the BFD relocation type to
4450 generate. HOWTO is the corresponding howto structure (we could
4451 look this up, but the caller has already done so). SECTION is the
4452 section to generate a reloc against, or NAME is the name of the
4453 symbol to generate a reloc against. Exactly one of SECTION and
4454 NAME must be NULL. ADDEND is an expression for the addend. */
4457 lang_add_reloc (bfd_reloc_code_real_type reloc
,
4458 reloc_howto_type
*howto
,
4461 union etree_union
*addend
)
4463 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4467 p
->section
= section
;
4469 p
->addend_exp
= addend
;
4471 p
->addend_value
= 0;
4472 p
->output_section
= NULL
;
4476 lang_assignment_statement_type
*
4477 lang_add_assignment (etree_type
*exp
)
4479 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4487 lang_add_attribute (enum statement_enum attribute
)
4489 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4493 lang_startup (const char *name
)
4495 if (startup_file
!= NULL
)
4497 einfo (_("%P%Fmultiple STARTUP files\n"));
4499 first_file
->filename
= name
;
4500 first_file
->local_sym_name
= name
;
4501 first_file
->real
= TRUE
;
4503 startup_file
= name
;
4507 lang_float (bfd_boolean maybe
)
4509 lang_float_flag
= maybe
;
4513 /* Work out the load- and run-time regions from a script statement, and
4514 store them in *LMA_REGION and *REGION respectively.
4516 MEMSPEC is the name of the run-time region, or the value of
4517 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4518 LMA_MEMSPEC is the name of the load-time region, or null if the
4519 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4520 had an explicit load address.
4522 It is an error to specify both a load region and a load address. */
4525 lang_get_regions (lang_memory_region_type
**region
,
4526 lang_memory_region_type
**lma_region
,
4527 const char *memspec
,
4528 const char *lma_memspec
,
4529 bfd_boolean have_lma
,
4530 bfd_boolean have_vma
)
4532 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
4534 /* If no runtime region or VMA has been specified, but the load region has
4535 been specified, then use the load region for the runtime region as well. */
4536 if (lma_memspec
!= NULL
4538 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
4539 *region
= *lma_region
;
4541 *region
= lang_memory_region_lookup (memspec
, FALSE
);
4543 if (have_lma
&& lma_memspec
!= 0)
4544 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4548 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
4549 lang_output_section_phdr_list
*phdrs
,
4550 const char *lma_memspec
)
4552 lang_get_regions (¤t_section
->region
,
4553 ¤t_section
->lma_region
,
4554 memspec
, lma_memspec
,
4555 current_section
->load_base
!= NULL
,
4556 current_section
->addr_tree
!= NULL
);
4557 current_section
->fill
= fill
;
4558 current_section
->phdrs
= phdrs
;
4559 stat_ptr
= &statement_list
;
4562 /* Create an absolute symbol with the given name with the value of the
4563 address of first byte of the section named.
4565 If the symbol already exists, then do nothing. */
4568 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
4570 struct bfd_link_hash_entry
*h
;
4572 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4574 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4576 if (h
->type
== bfd_link_hash_new
4577 || h
->type
== bfd_link_hash_undefined
)
4581 h
->type
= bfd_link_hash_defined
;
4583 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4587 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4589 h
->u
.def
.section
= bfd_abs_section_ptr
;
4593 /* Create an absolute symbol with the given name with the value of the
4594 address of the first byte after the end of the section named.
4596 If the symbol already exists, then do nothing. */
4599 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
4601 struct bfd_link_hash_entry
*h
;
4603 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4605 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4607 if (h
->type
== bfd_link_hash_new
4608 || h
->type
== bfd_link_hash_undefined
)
4612 h
->type
= bfd_link_hash_defined
;
4614 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4618 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4619 + TO_ADDR (bfd_section_size (output_bfd
, sec
)));
4621 h
->u
.def
.section
= bfd_abs_section_ptr
;
4626 lang_statement_append (lang_statement_list_type
*list
,
4627 lang_statement_union_type
*element
,
4628 lang_statement_union_type
**field
)
4630 *(list
->tail
) = element
;
4634 /* Set the output format type. -oformat overrides scripts. */
4637 lang_add_output_format (const char *format
,
4642 if (output_target
== NULL
|| !from_script
)
4644 if (command_line
.endian
== ENDIAN_BIG
4647 else if (command_line
.endian
== ENDIAN_LITTLE
4651 output_target
= format
;
4655 /* Enter a group. This creates a new lang_group_statement, and sets
4656 stat_ptr to build new statements within the group. */
4659 lang_enter_group (void)
4661 lang_group_statement_type
*g
;
4663 g
= new_stat (lang_group_statement
, stat_ptr
);
4664 lang_list_init (&g
->children
);
4665 stat_ptr
= &g
->children
;
4668 /* Leave a group. This just resets stat_ptr to start writing to the
4669 regular list of statements again. Note that this will not work if
4670 groups can occur inside anything else which can adjust stat_ptr,
4671 but currently they can't. */
4674 lang_leave_group (void)
4676 stat_ptr
= &statement_list
;
4679 /* Add a new program header. This is called for each entry in a PHDRS
4680 command in a linker script. */
4683 lang_new_phdr (const char *name
,
4685 bfd_boolean filehdr
,
4690 struct lang_phdr
*n
, **pp
;
4692 n
= stat_alloc (sizeof (struct lang_phdr
));
4695 n
->type
= exp_get_value_int (type
, 0, "program header type",
4696 lang_final_phase_enum
);
4697 n
->filehdr
= filehdr
;
4702 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4707 /* Record the program header information in the output BFD. FIXME: We
4708 should not be calling an ELF specific function here. */
4711 lang_record_phdrs (void)
4715 lang_output_section_phdr_list
*last
;
4716 struct lang_phdr
*l
;
4717 lang_statement_union_type
*u
;
4720 secs
= xmalloc (alc
* sizeof (asection
*));
4722 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4729 for (u
= lang_output_section_statement
.head
;
4731 u
= u
->output_section_statement
.next
)
4733 lang_output_section_statement_type
*os
;
4734 lang_output_section_phdr_list
*pl
;
4736 os
= &u
->output_section_statement
;
4743 if (os
->sectype
== noload_section
4744 || os
->bfd_section
== NULL
4745 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4750 if (os
->bfd_section
== NULL
)
4753 for (; pl
!= NULL
; pl
= pl
->next
)
4755 if (strcmp (pl
->name
, l
->name
) == 0)
4760 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
4762 secs
[c
] = os
->bfd_section
;
4769 if (l
->flags
== NULL
)
4772 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4773 lang_final_phase_enum
);
4778 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4779 lang_final_phase_enum
);
4781 if (! bfd_record_phdr (output_bfd
, l
->type
,
4782 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4783 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4784 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4789 /* Make sure all the phdr assignments succeeded. */
4790 for (u
= lang_output_section_statement
.head
;
4792 u
= u
->output_section_statement
.next
)
4794 lang_output_section_phdr_list
*pl
;
4796 if (u
->output_section_statement
.bfd_section
== NULL
)
4799 for (pl
= u
->output_section_statement
.phdrs
;
4802 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4803 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4804 u
->output_section_statement
.name
, pl
->name
);
4808 /* Record a list of sections which may not be cross referenced. */
4811 lang_add_nocrossref (lang_nocrossref_type
*l
)
4813 struct lang_nocrossrefs
*n
;
4815 n
= xmalloc (sizeof *n
);
4816 n
->next
= nocrossref_list
;
4818 nocrossref_list
= n
;
4820 /* Set notice_all so that we get informed about all symbols. */
4821 link_info
.notice_all
= TRUE
;
4824 /* Overlay handling. We handle overlays with some static variables. */
4826 /* The overlay virtual address. */
4827 static etree_type
*overlay_vma
;
4828 /* And subsection alignment. */
4829 static etree_type
*overlay_subalign
;
4831 /* An expression for the maximum section size seen so far. */
4832 static etree_type
*overlay_max
;
4834 /* A list of all the sections in this overlay. */
4836 struct overlay_list
{
4837 struct overlay_list
*next
;
4838 lang_output_section_statement_type
*os
;
4841 static struct overlay_list
*overlay_list
;
4843 /* Start handling an overlay. */
4846 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
4848 /* The grammar should prevent nested overlays from occurring. */
4849 ASSERT (overlay_vma
== NULL
4850 && overlay_subalign
== NULL
4851 && overlay_max
== NULL
);
4853 overlay_vma
= vma_expr
;
4854 overlay_subalign
= subalign
;
4857 /* Start a section in an overlay. We handle this by calling
4858 lang_enter_output_section_statement with the correct VMA.
4859 lang_leave_overlay sets up the LMA and memory regions. */
4862 lang_enter_overlay_section (const char *name
)
4864 struct overlay_list
*n
;
4867 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4868 0, overlay_subalign
, 0);
4870 /* If this is the first section, then base the VMA of future
4871 sections on this one. This will work correctly even if `.' is
4872 used in the addresses. */
4873 if (overlay_list
== NULL
)
4874 overlay_vma
= exp_nameop (ADDR
, name
);
4876 /* Remember the section. */
4877 n
= xmalloc (sizeof *n
);
4878 n
->os
= current_section
;
4879 n
->next
= overlay_list
;
4882 size
= exp_nameop (SIZEOF
, name
);
4884 /* Arrange to work out the maximum section end address. */
4885 if (overlay_max
== NULL
)
4888 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4891 /* Finish a section in an overlay. There isn't any special to do
4895 lang_leave_overlay_section (fill_type
*fill
,
4896 lang_output_section_phdr_list
*phdrs
)
4903 name
= current_section
->name
;
4905 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4906 region and that no load-time region has been specified. It doesn't
4907 really matter what we say here, since lang_leave_overlay will
4909 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
4911 /* Define the magic symbols. */
4913 clean
= xmalloc (strlen (name
) + 1);
4915 for (s1
= name
; *s1
!= '\0'; s1
++)
4916 if (ISALNUM (*s1
) || *s1
== '_')
4920 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4921 sprintf (buf
, "__load_start_%s", clean
);
4922 lang_add_assignment (exp_assop ('=', buf
,
4923 exp_nameop (LOADADDR
, name
)));
4925 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4926 sprintf (buf
, "__load_stop_%s", clean
);
4927 lang_add_assignment (exp_assop ('=', buf
,
4929 exp_nameop (LOADADDR
, name
),
4930 exp_nameop (SIZEOF
, name
))));
4935 /* Finish an overlay. If there are any overlay wide settings, this
4936 looks through all the sections in the overlay and sets them. */
4939 lang_leave_overlay (etree_type
*lma_expr
,
4942 const char *memspec
,
4943 lang_output_section_phdr_list
*phdrs
,
4944 const char *lma_memspec
)
4946 lang_memory_region_type
*region
;
4947 lang_memory_region_type
*lma_region
;
4948 struct overlay_list
*l
;
4949 lang_nocrossref_type
*nocrossref
;
4951 lang_get_regions (®ion
, &lma_region
,
4952 memspec
, lma_memspec
,
4953 lma_expr
!= NULL
, FALSE
);
4957 /* After setting the size of the last section, set '.' to end of the
4959 if (overlay_list
!= NULL
)
4960 overlay_list
->os
->update_dot_tree
4961 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
4966 struct overlay_list
*next
;
4968 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
4971 l
->os
->region
= region
;
4972 l
->os
->lma_region
= lma_region
;
4974 /* The first section has the load address specified in the
4975 OVERLAY statement. The rest are worked out from that.
4976 The base address is not needed (and should be null) if
4977 an LMA region was specified. */
4979 l
->os
->load_base
= lma_expr
;
4980 else if (lma_region
== 0)
4981 l
->os
->load_base
= exp_binop ('+',
4982 exp_nameop (LOADADDR
, l
->next
->os
->name
),
4983 exp_nameop (SIZEOF
, l
->next
->os
->name
));
4985 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4986 l
->os
->phdrs
= phdrs
;
4990 lang_nocrossref_type
*nc
;
4992 nc
= xmalloc (sizeof *nc
);
4993 nc
->name
= l
->os
->name
;
4994 nc
->next
= nocrossref
;
5003 if (nocrossref
!= NULL
)
5004 lang_add_nocrossref (nocrossref
);
5007 overlay_list
= NULL
;
5011 /* Version handling. This is only useful for ELF. */
5013 /* This global variable holds the version tree that we build. */
5015 struct bfd_elf_version_tree
*lang_elf_version_info
;
5017 /* If PREV is NULL, return first version pattern matching particular symbol.
5018 If PREV is non-NULL, return first version pattern matching particular
5019 symbol after PREV (previously returned by lang_vers_match). */
5021 static struct bfd_elf_version_expr
*
5022 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
5023 struct bfd_elf_version_expr
*prev
,
5026 const char *cxx_sym
= sym
;
5027 const char *java_sym
= sym
;
5028 struct bfd_elf_version_expr
*expr
= NULL
;
5030 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5032 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
5036 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5038 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5043 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
5045 struct bfd_elf_version_expr e
;
5047 switch (prev
? prev
->mask
: 0)
5050 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
5053 expr
= htab_find (head
->htab
, &e
);
5054 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
5055 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
5061 case BFD_ELF_VERSION_C_TYPE
:
5062 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5065 expr
= htab_find (head
->htab
, &e
);
5066 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
5067 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5073 case BFD_ELF_VERSION_CXX_TYPE
:
5074 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5076 e
.symbol
= java_sym
;
5077 expr
= htab_find (head
->htab
, &e
);
5078 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
5079 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5090 /* Finally, try the wildcards. */
5091 if (prev
== NULL
|| prev
->symbol
)
5092 expr
= head
->remaining
;
5099 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5102 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5104 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5108 if (fnmatch (expr
->pattern
, s
, 0) == 0)
5115 free ((char *) cxx_sym
);
5116 if (java_sym
!= sym
)
5117 free ((char *) java_sym
);
5121 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5122 return a string pointing to the symbol name. */
5125 realsymbol (const char *pattern
)
5128 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
5129 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
5131 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
5133 /* It is a glob pattern only if there is no preceding
5135 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
5143 /* Remove the preceding backslash. */
5150 backslash
= *p
== '\\';
5165 /* This is called for each variable name or match expression. */
5167 struct bfd_elf_version_expr
*
5168 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
5172 struct bfd_elf_version_expr
*ret
;
5174 ret
= xmalloc (sizeof *ret
);
5179 ret
->symbol
= realsymbol (new);
5181 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5182 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5183 else if (strcasecmp (lang
, "C++") == 0)
5184 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
5185 else if (strcasecmp (lang
, "Java") == 0)
5186 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
5189 einfo (_("%X%P: unknown language `%s' in version information\n"),
5191 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5194 return ldemul_new_vers_pattern (ret
);
5197 /* This is called for each set of variable names and match
5200 struct bfd_elf_version_tree
*
5201 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
5202 struct bfd_elf_version_expr
*locals
)
5204 struct bfd_elf_version_tree
*ret
;
5206 ret
= xcalloc (1, sizeof *ret
);
5207 ret
->globals
.list
= globals
;
5208 ret
->locals
.list
= locals
;
5209 ret
->match
= lang_vers_match
;
5210 ret
->name_indx
= (unsigned int) -1;
5214 /* This static variable keeps track of version indices. */
5216 static int version_index
;
5219 version_expr_head_hash (const void *p
)
5221 const struct bfd_elf_version_expr
*e
= p
;
5223 return htab_hash_string (e
->symbol
);
5227 version_expr_head_eq (const void *p1
, const void *p2
)
5229 const struct bfd_elf_version_expr
*e1
= p1
;
5230 const struct bfd_elf_version_expr
*e2
= p2
;
5232 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
5236 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
5239 struct bfd_elf_version_expr
*e
, *next
;
5240 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
5242 for (e
= head
->list
; e
; e
= e
->next
)
5246 head
->mask
|= e
->mask
;
5251 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
5252 version_expr_head_eq
, NULL
);
5253 list_loc
= &head
->list
;
5254 remaining_loc
= &head
->remaining
;
5255 for (e
= head
->list
; e
; e
= next
)
5261 remaining_loc
= &e
->next
;
5265 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
5269 struct bfd_elf_version_expr
*e1
, *last
;
5275 if (e1
->mask
== e
->mask
)
5283 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
5287 /* This is a duplicate. */
5288 /* FIXME: Memory leak. Sometimes pattern is not
5289 xmalloced alone, but in larger chunk of memory. */
5290 /* free (e->symbol); */
5295 e
->next
= last
->next
;
5303 list_loc
= &e
->next
;
5307 *remaining_loc
= NULL
;
5308 *list_loc
= head
->remaining
;
5311 head
->remaining
= head
->list
;
5314 /* This is called when we know the name and dependencies of the
5318 lang_register_vers_node (const char *name
,
5319 struct bfd_elf_version_tree
*version
,
5320 struct bfd_elf_version_deps
*deps
)
5322 struct bfd_elf_version_tree
*t
, **pp
;
5323 struct bfd_elf_version_expr
*e1
;
5328 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5329 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5331 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5336 /* Make sure this node has a unique name. */
5337 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5338 if (strcmp (t
->name
, name
) == 0)
5339 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5341 lang_finalize_version_expr_head (&version
->globals
);
5342 lang_finalize_version_expr_head (&version
->locals
);
5344 /* Check the global and local match names, and make sure there
5345 aren't any duplicates. */
5347 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
5349 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5351 struct bfd_elf_version_expr
*e2
;
5353 if (t
->locals
.htab
&& e1
->symbol
)
5355 e2
= htab_find (t
->locals
.htab
, e1
);
5356 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5358 if (e1
->mask
== e2
->mask
)
5359 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5364 else if (!e1
->symbol
)
5365 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5366 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5367 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5372 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
5374 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5376 struct bfd_elf_version_expr
*e2
;
5378 if (t
->globals
.htab
&& e1
->symbol
)
5380 e2
= htab_find (t
->globals
.htab
, e1
);
5381 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5383 if (e1
->mask
== e2
->mask
)
5384 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5389 else if (!e1
->symbol
)
5390 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5391 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5392 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5397 version
->deps
= deps
;
5398 version
->name
= name
;
5399 if (name
[0] != '\0')
5402 version
->vernum
= version_index
;
5405 version
->vernum
= 0;
5407 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5412 /* This is called when we see a version dependency. */
5414 struct bfd_elf_version_deps
*
5415 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
5417 struct bfd_elf_version_deps
*ret
;
5418 struct bfd_elf_version_tree
*t
;
5420 ret
= xmalloc (sizeof *ret
);
5423 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5425 if (strcmp (t
->name
, name
) == 0)
5427 ret
->version_needed
= t
;
5432 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5438 lang_do_version_exports_section (void)
5440 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5442 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5444 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5451 len
= bfd_section_size (is
->the_bfd
, sec
);
5452 contents
= xmalloc (len
);
5453 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5454 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5457 while (p
< contents
+ len
)
5459 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5460 p
= strchr (p
, '\0') + 1;
5463 /* Do not free the contents, as we used them creating the regex. */
5465 /* Do not include this section in the link. */
5466 bfd_set_section_flags (is
->the_bfd
, sec
,
5467 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5470 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5471 lang_register_vers_node (command_line
.version_exports_section
,
5472 lang_new_vers_node (greg
, lreg
), NULL
);
5476 lang_add_unique (const char *name
)
5478 struct unique_sections
*ent
;
5480 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5481 if (strcmp (ent
->name
, name
) == 0)
5484 ent
= xmalloc (sizeof *ent
);
5485 ent
->name
= xstrdup (name
);
5486 ent
->next
= unique_section_list
;
5487 unique_section_list
= ent
;