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
->as_needed
= as_needed
;
449 p
->whole_archive
= whole_archive
;
451 lang_statement_append (&input_file_chain
,
452 (lang_statement_union_type
*) p
,
457 lang_input_statement_type
*
458 lang_add_input_file (const char *name
,
459 lang_input_file_enum_type file_type
,
462 lang_has_input_file
= TRUE
;
463 return new_afile (name
, file_type
, target
, TRUE
);
466 /* Build enough state so that the parser can build its tree. */
471 obstack_begin (&stat_obstack
, 1000);
473 stat_ptr
= &statement_list
;
475 lang_list_init (stat_ptr
);
477 lang_list_init (&input_file_chain
);
478 lang_list_init (&lang_output_section_statement
);
479 lang_list_init (&file_chain
);
480 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
483 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
485 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
487 /* The value "3" is ad-hoc, somewhat related to the expected number of
488 DEFINED expressions in a linker script. For most default linker
489 scripts, there are none. Why a hash table then? Well, it's somewhat
490 simpler to re-use working machinery than using a linked list in terms
491 of code-complexity here in ld, besides the initialization which just
492 looks like other code here. */
493 if (bfd_hash_table_init_n (&lang_definedness_table
,
494 lang_definedness_newfunc
, 3) != TRUE
)
495 einfo (_("%P%F: out of memory during initialization"));
497 /* Callers of exp_fold_tree need to increment this. */
498 lang_statement_iteration
= 0;
501 /*----------------------------------------------------------------------
502 A region is an area of memory declared with the
503 MEMORY { name:org=exp, len=exp ... }
506 We maintain a list of all the regions here.
508 If no regions are specified in the script, then the default is used
509 which is created when looked up to be the entire data space.
511 If create is true we are creating a region inside a MEMORY block.
512 In this case it is probably an error to create a region that has
513 already been created. If we are not inside a MEMORY block it is
514 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
515 and so we issue a warning. */
517 static lang_memory_region_type
*lang_memory_region_list
;
518 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
520 lang_memory_region_type
*
521 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
523 lang_memory_region_type
*p
;
524 lang_memory_region_type
*new;
526 /* NAME is NULL for LMA memspecs if no region was specified. */
530 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
531 if (strcmp (p
->name
, name
) == 0)
534 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name
);
539 /* This code used to always use the first region in the list as the
540 default region. I changed it to instead use a region
541 encompassing all of memory as the default region. This permits
542 NOLOAD sections to work reasonably without requiring a region.
543 People should specify what region they mean, if they really want
545 if (strcmp (name
, DEFAULT_MEMORY_REGION
) == 0)
547 if (lang_memory_region_list
!= NULL
)
548 return lang_memory_region_list
;
552 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
553 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
555 new = stat_alloc (sizeof (lang_memory_region_type
));
557 new->name
= xstrdup (name
);
560 *lang_memory_region_list_tail
= new;
561 lang_memory_region_list_tail
= &new->next
;
565 new->length
= ~(bfd_size_type
) 0;
567 new->had_full_message
= FALSE
;
572 static lang_memory_region_type
*
573 lang_memory_default (asection
*section
)
575 lang_memory_region_type
*p
;
577 flagword sec_flags
= section
->flags
;
579 /* Override SEC_DATA to mean a writable section. */
580 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
581 sec_flags
|= SEC_DATA
;
583 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
585 if ((p
->flags
& sec_flags
) != 0
586 && (p
->not_flags
& sec_flags
) == 0)
591 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
594 lang_output_section_statement_type
*
595 lang_output_section_find (const char *const name
)
597 lang_statement_union_type
*u
;
598 lang_output_section_statement_type
*lookup
;
600 for (u
= lang_output_section_statement
.head
; u
!= NULL
; u
= lookup
->next
)
602 lookup
= &u
->output_section_statement
;
603 if (strcmp (name
, lookup
->name
) == 0)
609 lang_output_section_statement_type
*
610 lang_output_section_statement_lookup (const char *const name
)
612 lang_output_section_statement_type
*lookup
;
614 lookup
= lang_output_section_find (name
);
617 lookup
= new_stat (lang_output_section_statement
, stat_ptr
);
618 lookup
->region
= NULL
;
619 lookup
->lma_region
= NULL
;
621 lookup
->block_value
= 1;
625 lookup
->bfd_section
= NULL
;
626 lookup
->processed
= 0;
627 lookup
->sectype
= normal_section
;
628 lookup
->addr_tree
= NULL
;
629 lang_list_init (&lookup
->children
);
631 lookup
->memspec
= NULL
;
633 lookup
->subsection_alignment
= -1;
634 lookup
->section_alignment
= -1;
635 lookup
->load_base
= NULL
;
636 lookup
->update_dot_tree
= NULL
;
637 lookup
->phdrs
= NULL
;
639 lang_statement_append (&lang_output_section_statement
,
640 (lang_statement_union_type
*) lookup
,
647 lang_map_flags (flagword flag
)
649 if (flag
& SEC_ALLOC
)
655 if (flag
& SEC_READONLY
)
668 lang_memory_region_type
*m
;
670 minfo (_("\nMemory Configuration\n\n"));
671 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
672 _("Name"), _("Origin"), _("Length"), _("Attributes"));
674 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
679 fprintf (config
.map_file
, "%-16s ", m
->name
);
681 sprintf_vma (buf
, m
->origin
);
682 minfo ("0x%s ", buf
);
690 minfo ("0x%V", m
->length
);
691 if (m
->flags
|| m
->not_flags
)
699 lang_map_flags (m
->flags
);
705 lang_map_flags (m
->not_flags
);
712 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
717 /* Initialize an output section. */
720 init_os (lang_output_section_statement_type
*s
)
722 section_userdata_type
*new;
724 if (s
->bfd_section
!= NULL
)
727 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
728 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
730 new = stat_alloc (sizeof (section_userdata_type
));
732 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
733 if (s
->bfd_section
== NULL
)
734 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
735 if (s
->bfd_section
== NULL
)
737 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
738 output_bfd
->xvec
->name
, s
->name
);
740 s
->bfd_section
->output_section
= s
->bfd_section
;
742 /* We initialize an output sections output offset to minus its own
743 vma to allow us to output a section through itself. */
744 s
->bfd_section
->output_offset
= 0;
745 get_userdata (s
->bfd_section
) = new;
747 /* If there is a base address, make sure that any sections it might
748 mention are initialized. */
749 if (s
->addr_tree
!= NULL
)
750 exp_init_os (s
->addr_tree
);
752 if (s
->load_base
!= NULL
)
753 exp_init_os (s
->load_base
);
756 /* Make sure that all output sections mentioned in an expression are
760 exp_init_os (etree_type
*exp
)
762 switch (exp
->type
.node_class
)
765 exp_init_os (exp
->assign
.src
);
769 exp_init_os (exp
->binary
.lhs
);
770 exp_init_os (exp
->binary
.rhs
);
774 exp_init_os (exp
->trinary
.cond
);
775 exp_init_os (exp
->trinary
.lhs
);
776 exp_init_os (exp
->trinary
.rhs
);
780 exp_init_os (exp
->assert_s
.child
);
784 exp_init_os (exp
->unary
.child
);
788 switch (exp
->type
.node_code
)
794 lang_output_section_statement_type
*os
;
796 os
= lang_output_section_find (exp
->name
.name
);
797 if (os
!= NULL
&& os
->bfd_section
== NULL
)
808 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
809 once into the output. This routine checks each section, and
810 arrange to discard it if a section of the same name has already
811 been linked. If the section has COMDAT information, then it uses
812 that to decide whether the section should be included. This code
813 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
814 that is, it does not depend solely upon the section name.
815 section_already_linked is called via bfd_map_over_sections. */
817 /* This is the shape of the elements inside the already_linked hash
818 table. It maps a name onto a list of already_linked elements with
819 the same name. It's possible to get more than one element in a
820 list if the COMDAT sections have different names. */
822 struct already_linked_hash_entry
824 struct bfd_hash_entry root
;
825 struct already_linked
*entry
;
828 struct already_linked
830 struct already_linked
*next
;
834 /* The hash table. */
836 static struct bfd_hash_table already_linked_table
;
839 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
841 lang_input_statement_type
*entry
= data
;
844 struct already_linked
*l
;
845 struct already_linked_hash_entry
*already_linked_list
;
847 /* If we are only reading symbols from this object, then we want to
848 discard all sections. */
849 if (entry
->just_syms_flag
)
851 bfd_link_just_syms (sec
, &link_info
);
855 flags
= bfd_get_section_flags (abfd
, sec
);
857 if ((flags
& SEC_LINK_ONCE
) == 0)
860 /* FIXME: When doing a relocatable link, we may have trouble
861 copying relocations in other sections that refer to local symbols
862 in the section being discarded. Those relocations will have to
863 be converted somehow; as of this writing I'm not sure that any of
864 the backends handle that correctly.
866 It is tempting to instead not discard link once sections when
867 doing a relocatable link (technically, they should be discarded
868 whenever we are building constructors). However, that fails,
869 because the linker winds up combining all the link once sections
870 into a single large link once section, which defeats the purpose
871 of having link once sections in the first place.
873 Also, not merging link once sections in a relocatable link
874 causes trouble for MIPS ELF, which relies on link once semantics
875 to handle the .reginfo section correctly. */
877 name
= bfd_get_section_name (abfd
, sec
);
879 already_linked_list
=
880 ((struct already_linked_hash_entry
*)
881 bfd_hash_lookup (&already_linked_table
, name
, TRUE
, FALSE
));
883 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
885 if (sec
->comdat
== NULL
886 || l
->sec
->comdat
== NULL
887 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
889 /* The section has already been linked. See if we should
891 switch (flags
& SEC_LINK_DUPLICATES
)
896 case SEC_LINK_DUPLICATES_DISCARD
:
899 case SEC_LINK_DUPLICATES_ONE_ONLY
:
900 if (sec
->comdat
== NULL
)
901 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
904 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
905 abfd
, name
, sec
->comdat
->name
);
908 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
909 /* FIXME: We should really dig out the contents of both
910 sections and memcmp them. The COFF/PE spec says that
911 the Microsoft linker does not implement this
912 correctly, so I'm not going to bother doing it
915 case SEC_LINK_DUPLICATES_SAME_SIZE
:
916 if (bfd_section_size (abfd
, sec
)
917 != bfd_section_size (l
->sec
->owner
, l
->sec
))
918 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
923 /* Set the output_section field so that lang_add_section
924 does not create a lang_input_section structure for this
925 section. Since there might be a symbol in the section
926 being discarded, we must retain a pointer to the section
927 which we are really going to use. */
928 sec
->output_section
= bfd_abs_section_ptr
;
929 sec
->kept_section
= l
->sec
;
931 if (flags
& SEC_GROUP
)
932 bfd_discard_group (abfd
, sec
);
938 /* This is the first section with this name. Record it. Allocate
939 the memory from the same obstack as the hash table is kept in. */
941 l
= bfd_hash_allocate (&already_linked_table
, sizeof *l
);
944 l
->next
= already_linked_list
->entry
;
945 already_linked_list
->entry
= l
;
948 /* Support routines for the hash table used by section_already_linked,
949 initialize the table, fill in an entry and remove the table. */
951 static struct bfd_hash_entry
*
952 already_linked_newfunc (struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
,
953 struct bfd_hash_table
*table
,
954 const char *string ATTRIBUTE_UNUSED
)
956 struct already_linked_hash_entry
*ret
=
957 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
965 already_linked_table_init (void)
967 if (! bfd_hash_table_init_n (&already_linked_table
,
968 already_linked_newfunc
,
970 einfo (_("%P%F: Failed to create hash table\n"));
974 already_linked_table_free (void)
976 bfd_hash_table_free (&already_linked_table
);
979 /* The wild routines.
981 These expand statements like *(.text) and foo.o to a list of
982 explicit actions, like foo.o(.text), bar.o(.text) and
983 foo.o(.text, .data). */
985 /* Return TRUE if the PATTERN argument is a wildcard pattern.
986 Although backslashes are treated specially if a pattern contains
987 wildcards, we do not consider the mere presence of a backslash to
988 be enough to cause the pattern to be treated as a wildcard.
989 That lets us handle DOS filenames more naturally. */
992 wildcardp (const char *pattern
)
996 for (s
= pattern
; *s
!= '\0'; ++s
)
1004 /* Add SECTION to the output section OUTPUT. Do this by creating a
1005 lang_input_section statement which is placed at PTR. FILE is the
1006 input file which holds SECTION. */
1009 lang_add_section (lang_statement_list_type
*ptr
,
1011 lang_output_section_statement_type
*output
,
1012 lang_input_statement_type
*file
)
1015 bfd_boolean discard
;
1017 flags
= bfd_get_section_flags (section
->owner
, section
);
1021 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1022 link. Discard debugging sections marked with SEC_EXCLUDE on a
1023 relocatable link too. */
1024 if ((flags
& SEC_EXCLUDE
) != 0
1025 && ((flags
& SEC_DEBUGGING
) != 0 || !link_info
.relocatable
))
1028 /* Discard input sections which are assigned to a section named
1029 DISCARD_SECTION_NAME. */
1030 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1033 /* Discard debugging sections if we are stripping debugging
1035 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1036 && (flags
& SEC_DEBUGGING
) != 0)
1041 if (section
->output_section
== NULL
)
1043 /* This prevents future calls from assigning this section. */
1044 section
->output_section
= bfd_abs_section_ptr
;
1049 if (section
->output_section
== NULL
)
1052 lang_input_section_type
*new;
1055 if (output
->bfd_section
== NULL
)
1058 first
= ! output
->bfd_section
->linker_has_input
;
1059 output
->bfd_section
->linker_has_input
= 1;
1061 /* Add a section reference to the list. */
1062 new = new_stat (lang_input_section
, ptr
);
1064 new->section
= section
;
1066 section
->output_section
= output
->bfd_section
;
1068 flags
= section
->flags
;
1070 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1071 to an output section, because we want to be able to include a
1072 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1073 section (I don't know why we want to do this, but we do).
1074 build_link_order in ldwrite.c handles this case by turning
1075 the embedded SEC_NEVER_LOAD section into a fill. */
1077 flags
&= ~ SEC_NEVER_LOAD
;
1079 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1080 already been processed. One reason to do this is that on pe
1081 format targets, .text$foo sections go into .text and it's odd
1082 to see .text with SEC_LINK_ONCE set. */
1084 if (! link_info
.relocatable
)
1085 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1087 /* If this is not the first input section, and the SEC_READONLY
1088 flag is not currently set, then don't set it just because the
1089 input section has it set. */
1091 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1092 flags
&= ~ SEC_READONLY
;
1094 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1096 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1097 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1098 || ((flags
& SEC_MERGE
)
1099 && section
->output_section
->entsize
!= section
->entsize
)))
1101 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1102 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1105 section
->output_section
->flags
|= flags
;
1107 if (flags
& SEC_MERGE
)
1108 section
->output_section
->entsize
= section
->entsize
;
1110 /* If SEC_READONLY is not set in the input section, then clear
1111 it from the output section. */
1112 if ((section
->flags
& SEC_READONLY
) == 0)
1113 section
->output_section
->flags
&= ~SEC_READONLY
;
1115 switch (output
->sectype
)
1117 case normal_section
:
1122 case overlay_section
:
1123 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1125 case noload_section
:
1126 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1127 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1131 /* Copy over SEC_SMALL_DATA. */
1132 if (section
->flags
& SEC_SMALL_DATA
)
1133 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1135 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1136 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1138 /* If supplied an alignment, then force it. */
1139 if (output
->section_alignment
!= -1)
1140 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1142 if (section
->flags
& SEC_BLOCK
)
1144 section
->output_section
->flags
|= SEC_BLOCK
;
1145 /* FIXME: This value should really be obtained from the bfd... */
1146 output
->block_value
= 128;
1151 /* Handle wildcard sorting. This returns the lang_input_section which
1152 should follow the one we are going to create for SECTION and FILE,
1153 based on the sorting requirements of WILD. It returns NULL if the
1154 new section should just go at the end of the current list. */
1156 static lang_statement_union_type
*
1157 wild_sort (lang_wild_statement_type
*wild
,
1158 struct wildcard_list
*sec
,
1159 lang_input_statement_type
*file
,
1162 const char *section_name
;
1163 lang_statement_union_type
*l
;
1165 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1168 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1169 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1171 lang_input_section_type
*ls
;
1173 if (l
->header
.type
!= lang_input_section_enum
)
1175 ls
= &l
->input_section
;
1177 /* Sorting by filename takes precedence over sorting by section
1180 if (wild
->filenames_sorted
)
1182 const char *fn
, *ln
;
1186 /* The PE support for the .idata section as generated by
1187 dlltool assumes that files will be sorted by the name of
1188 the archive and then the name of the file within the
1191 if (file
->the_bfd
!= NULL
1192 && bfd_my_archive (file
->the_bfd
) != NULL
)
1194 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1199 fn
= file
->filename
;
1203 if (ls
->ifile
->the_bfd
!= NULL
1204 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1206 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1211 ln
= ls
->ifile
->filename
;
1215 i
= strcmp (fn
, ln
);
1224 fn
= file
->filename
;
1226 ln
= ls
->ifile
->filename
;
1228 i
= strcmp (fn
, ln
);
1236 /* Here either the files are not sorted by name, or we are
1237 looking at the sections for this file. */
1239 if (sec
!= NULL
&& sec
->spec
.sorted
)
1241 if (strcmp (section_name
,
1242 bfd_get_section_name (ls
->ifile
->the_bfd
,
1252 /* Expand a wild statement for a particular FILE. SECTION may be
1253 NULL, in which case it is a wild card. */
1256 output_section_callback (lang_wild_statement_type
*ptr
,
1257 struct wildcard_list
*sec
,
1259 lang_input_statement_type
*file
,
1262 lang_statement_union_type
*before
;
1264 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1265 if (unique_section_p (bfd_get_section_name (file
->the_bfd
, section
)))
1268 /* If the wild pattern was marked KEEP, the member sections
1269 should be as well. */
1270 if (ptr
->keep_sections
)
1271 section
->flags
|= SEC_KEEP
;
1273 before
= wild_sort (ptr
, sec
, file
, section
);
1275 /* Here BEFORE points to the lang_input_section which
1276 should follow the one we are about to add. If BEFORE
1277 is NULL, then the section should just go at the end
1278 of the current list. */
1281 lang_add_section (&ptr
->children
, section
,
1282 (lang_output_section_statement_type
*) output
,
1286 lang_statement_list_type list
;
1287 lang_statement_union_type
**pp
;
1289 lang_list_init (&list
);
1290 lang_add_section (&list
, section
,
1291 (lang_output_section_statement_type
*) output
,
1294 /* If we are discarding the section, LIST.HEAD will
1296 if (list
.head
!= NULL
)
1298 ASSERT (list
.head
->header
.next
== NULL
);
1300 for (pp
= &ptr
->children
.head
;
1302 pp
= &(*pp
)->header
.next
)
1303 ASSERT (*pp
!= NULL
);
1305 list
.head
->header
.next
= *pp
;
1311 /* This is passed a file name which must have been seen already and
1312 added to the statement tree. We will see if it has been opened
1313 already and had its symbols read. If not then we'll read it. */
1315 static lang_input_statement_type
*
1316 lookup_name (const char *name
)
1318 lang_input_statement_type
*search
;
1320 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1322 search
= (lang_input_statement_type
*) search
->next_real_file
)
1324 /* Use the local_sym_name as the name of the file that has
1325 already been loaded as filename might have been transformed
1326 via the search directory lookup mechanism. */
1327 const char * filename
= search
->local_sym_name
;
1329 if (filename
== NULL
&& name
== NULL
)
1331 if (filename
!= NULL
1333 && strcmp (filename
, name
) == 0)
1338 search
= new_afile (name
, lang_input_file_is_search_file_enum
, default_target
,
1341 /* If we have already added this file, or this file is not real
1342 (FIXME: can that ever actually happen?) or the name is NULL
1343 (FIXME: can that ever actually happen?) don't add this file. */
1346 || search
->filename
== NULL
)
1349 if (! load_symbols (search
, NULL
))
1355 /* Get the symbols for an input file. */
1358 load_symbols (lang_input_statement_type
*entry
,
1359 lang_statement_list_type
*place
)
1366 ldfile_open_file (entry
);
1368 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1369 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1372 lang_statement_list_type
*hold
;
1373 bfd_boolean bad_load
= TRUE
;
1374 bfd_boolean save_ldlang_sysrooted_script
;
1376 err
= bfd_get_error ();
1378 /* See if the emulation has some special knowledge. */
1379 if (ldemul_unrecognized_file (entry
))
1382 if (err
== bfd_error_file_ambiguously_recognized
)
1386 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1387 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1388 for (p
= matching
; *p
!= NULL
; p
++)
1392 else if (err
!= bfd_error_file_not_recognized
1394 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1398 bfd_close (entry
->the_bfd
);
1399 entry
->the_bfd
= NULL
;
1401 /* Try to interpret the file as a linker script. */
1402 ldfile_open_command_file (entry
->filename
);
1406 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1407 ldlang_sysrooted_script
= entry
->sysrooted
;
1409 ldfile_assumed_script
= TRUE
;
1410 parser_input
= input_script
;
1412 ldfile_assumed_script
= FALSE
;
1414 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1420 if (ldemul_recognized_file (entry
))
1423 /* We don't call ldlang_add_file for an archive. Instead, the
1424 add_symbols entry point will call ldlang_add_file, via the
1425 add_archive_element callback, for each element of the archive
1427 switch (bfd_get_format (entry
->the_bfd
))
1433 ldlang_add_file (entry
);
1434 if (trace_files
|| trace_file_tries
)
1435 info_msg ("%I\n", entry
);
1439 if (entry
->whole_archive
)
1442 bfd_boolean loaded
= TRUE
;
1446 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1451 if (! bfd_check_format (member
, bfd_object
))
1453 einfo (_("%F%B: member %B in archive is not an object\n"),
1454 entry
->the_bfd
, member
);
1458 if (! ((*link_info
.callbacks
->add_archive_element
)
1459 (&link_info
, member
, "--whole-archive")))
1462 if (! bfd_link_add_symbols (member
, &link_info
))
1464 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1469 entry
->loaded
= loaded
;
1475 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1476 entry
->loaded
= TRUE
;
1478 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1480 return entry
->loaded
;
1483 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1484 may be NULL, indicating that it is a wildcard. Separate
1485 lang_input_section statements are created for each part of the
1486 expansion; they are added after the wild statement S. OUTPUT is
1487 the output section. */
1490 wild (lang_wild_statement_type
*s
,
1491 const char *target ATTRIBUTE_UNUSED
,
1492 lang_output_section_statement_type
*output
)
1494 struct wildcard_list
*sec
;
1496 walk_wild (s
, output_section_callback
, output
);
1498 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1500 if (default_common_section
!= NULL
)
1502 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1504 /* Remember the section that common is going to in case we
1505 later get something which doesn't know where to put it. */
1506 default_common_section
= output
;
1511 /* Return TRUE iff target is the sought target. */
1514 get_target (const bfd_target
*target
, void *data
)
1516 const char *sought
= data
;
1518 return strcmp (target
->name
, sought
) == 0;
1521 /* Like strcpy() but convert to lower case as well. */
1524 stricpy (char *dest
, char *src
)
1528 while ((c
= *src
++) != 0)
1529 *dest
++ = TOLOWER (c
);
1534 /* Remove the first occurrence of needle (if any) in haystack
1538 strcut (char *haystack
, char *needle
)
1540 haystack
= strstr (haystack
, needle
);
1546 for (src
= haystack
+ strlen (needle
); *src
;)
1547 *haystack
++ = *src
++;
1553 /* Compare two target format name strings.
1554 Return a value indicating how "similar" they are. */
1557 name_compare (char *first
, char *second
)
1563 copy1
= xmalloc (strlen (first
) + 1);
1564 copy2
= xmalloc (strlen (second
) + 1);
1566 /* Convert the names to lower case. */
1567 stricpy (copy1
, first
);
1568 stricpy (copy2
, second
);
1570 /* Remove size and endian strings from the name. */
1571 strcut (copy1
, "big");
1572 strcut (copy1
, "little");
1573 strcut (copy2
, "big");
1574 strcut (copy2
, "little");
1576 /* Return a value based on how many characters match,
1577 starting from the beginning. If both strings are
1578 the same then return 10 * their length. */
1579 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1580 if (copy1
[result
] == 0)
1592 /* Set by closest_target_match() below. */
1593 static const bfd_target
*winner
;
1595 /* Scan all the valid bfd targets looking for one that has the endianness
1596 requirement that was specified on the command line, and is the nearest
1597 match to the original output target. */
1600 closest_target_match (const bfd_target
*target
, void *data
)
1602 const bfd_target
*original
= data
;
1604 if (command_line
.endian
== ENDIAN_BIG
1605 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1608 if (command_line
.endian
== ENDIAN_LITTLE
1609 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1612 /* Must be the same flavour. */
1613 if (target
->flavour
!= original
->flavour
)
1616 /* If we have not found a potential winner yet, then record this one. */
1623 /* Oh dear, we now have two potential candidates for a successful match.
1624 Compare their names and choose the better one. */
1625 if (name_compare (target
->name
, original
->name
)
1626 > name_compare (winner
->name
, original
->name
))
1629 /* Keep on searching until wqe have checked them all. */
1633 /* Return the BFD target format of the first input file. */
1636 get_first_input_target (void)
1638 char *target
= NULL
;
1640 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1642 if (s
->header
.type
== lang_input_statement_enum
1645 ldfile_open_file (s
);
1647 if (s
->the_bfd
!= NULL
1648 && bfd_check_format (s
->the_bfd
, bfd_object
))
1650 target
= bfd_get_target (s
->the_bfd
);
1662 lang_get_output_target (void)
1666 /* Has the user told us which output format to use? */
1667 if (output_target
!= NULL
)
1668 return output_target
;
1670 /* No - has the current target been set to something other than
1672 if (current_target
!= default_target
)
1673 return current_target
;
1675 /* No - can we determine the format of the first input file? */
1676 target
= get_first_input_target ();
1680 /* Failed - use the default output target. */
1681 return default_target
;
1684 /* Open the output file. */
1687 open_output (const char *name
)
1691 output_target
= lang_get_output_target ();
1693 /* Has the user requested a particular endianness on the command
1695 if (command_line
.endian
!= ENDIAN_UNSET
)
1697 const bfd_target
*target
;
1698 enum bfd_endian desired_endian
;
1700 /* Get the chosen target. */
1701 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1703 /* If the target is not supported, we cannot do anything. */
1706 if (command_line
.endian
== ENDIAN_BIG
)
1707 desired_endian
= BFD_ENDIAN_BIG
;
1709 desired_endian
= BFD_ENDIAN_LITTLE
;
1711 /* See if the target has the wrong endianness. This should
1712 not happen if the linker script has provided big and
1713 little endian alternatives, but some scrips don't do
1715 if (target
->byteorder
!= desired_endian
)
1717 /* If it does, then see if the target provides
1718 an alternative with the correct endianness. */
1719 if (target
->alternative_target
!= NULL
1720 && (target
->alternative_target
->byteorder
== desired_endian
))
1721 output_target
= target
->alternative_target
->name
;
1724 /* Try to find a target as similar as possible to
1725 the default target, but which has the desired
1726 endian characteristic. */
1727 bfd_search_for_target (closest_target_match
,
1730 /* Oh dear - we could not find any targets that
1731 satisfy our requirements. */
1733 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1735 output_target
= winner
->name
;
1741 output
= bfd_openw (name
, output_target
);
1745 if (bfd_get_error () == bfd_error_invalid_target
)
1746 einfo (_("%P%F: target %s not found\n"), output_target
);
1748 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1751 delete_output_file_on_failure
= TRUE
;
1754 output
->flags
|= D_PAGED
;
1757 if (! bfd_set_format (output
, bfd_object
))
1758 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1759 if (! bfd_set_arch_mach (output
,
1760 ldfile_output_architecture
,
1761 ldfile_output_machine
))
1762 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1764 link_info
.hash
= bfd_link_hash_table_create (output
);
1765 if (link_info
.hash
== NULL
)
1766 einfo (_("%P%F: can not create link hash table: %E\n"));
1768 bfd_set_gp_size (output
, g_switch_value
);
1773 ldlang_open_output (lang_statement_union_type
*statement
)
1775 switch (statement
->header
.type
)
1777 case lang_output_statement_enum
:
1778 ASSERT (output_bfd
== NULL
);
1779 output_bfd
= open_output (statement
->output_statement
.name
);
1780 ldemul_set_output_arch ();
1781 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
1782 output_bfd
->flags
|= D_PAGED
;
1784 output_bfd
->flags
&= ~D_PAGED
;
1785 if (config
.text_read_only
)
1786 output_bfd
->flags
|= WP_TEXT
;
1788 output_bfd
->flags
&= ~WP_TEXT
;
1789 if (link_info
.traditional_format
)
1790 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1792 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1795 case lang_target_statement_enum
:
1796 current_target
= statement
->target_statement
.target
;
1803 /* Convert between addresses in bytes and sizes in octets.
1804 For currently supported targets, octets_per_byte is always a power
1805 of two, so we can use shifts. */
1806 #define TO_ADDR(X) ((X) >> opb_shift)
1807 #define TO_SIZE(X) ((X) << opb_shift)
1809 /* Support the above. */
1810 static unsigned int opb_shift
= 0;
1815 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
1816 ldfile_output_machine
);
1819 while ((x
& 1) == 0)
1827 /* Open all the input files. */
1830 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
1832 for (; s
!= NULL
; s
= s
->header
.next
)
1834 switch (s
->header
.type
)
1836 case lang_constructors_statement_enum
:
1837 open_input_bfds (constructor_list
.head
, force
);
1839 case lang_output_section_statement_enum
:
1840 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1842 case lang_wild_statement_enum
:
1843 /* Maybe we should load the file's symbols. */
1844 if (s
->wild_statement
.filename
1845 && ! wildcardp (s
->wild_statement
.filename
))
1846 lookup_name (s
->wild_statement
.filename
);
1847 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1849 case lang_group_statement_enum
:
1851 struct bfd_link_hash_entry
*undefs
;
1853 /* We must continually search the entries in the group
1854 until no new symbols are added to the list of undefined
1859 undefs
= link_info
.hash
->undefs_tail
;
1860 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
1862 while (undefs
!= link_info
.hash
->undefs_tail
);
1865 case lang_target_statement_enum
:
1866 current_target
= s
->target_statement
.target
;
1868 case lang_input_statement_enum
:
1869 if (s
->input_statement
.real
)
1871 lang_statement_list_type add
;
1873 s
->input_statement
.target
= current_target
;
1875 /* If we are being called from within a group, and this
1876 is an archive which has already been searched, then
1877 force it to be researched unless the whole archive
1878 has been loaded already. */
1880 && !s
->input_statement
.whole_archive
1881 && s
->input_statement
.loaded
1882 && bfd_check_format (s
->input_statement
.the_bfd
,
1884 s
->input_statement
.loaded
= FALSE
;
1886 lang_list_init (&add
);
1888 if (! load_symbols (&s
->input_statement
, &add
))
1889 config
.make_executable
= FALSE
;
1891 if (add
.head
!= NULL
)
1893 *add
.tail
= s
->header
.next
;
1894 s
->header
.next
= add
.head
;
1904 /* If there are [COMMONS] statements, put a wild one into the bss
1908 lang_reasonable_defaults (void)
1911 lang_output_section_statement_lookup (".text");
1912 lang_output_section_statement_lookup (".data");
1914 default_common_section
= lang_output_section_statement_lookup (".bss");
1916 if (!placed_commons
)
1918 lang_wild_statement_type
*new =
1919 new_stat (lang_wild_statement
,
1920 &default_common_section
->children
);
1922 new->section_name
= "COMMON";
1923 new->filename
= NULL
;
1924 lang_list_init (&new->children
);
1929 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1932 lang_track_definedness (const char *name
)
1934 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
1935 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
1938 /* New-function for the definedness hash table. */
1940 static struct bfd_hash_entry
*
1941 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
1942 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
1943 const char *name ATTRIBUTE_UNUSED
)
1945 struct lang_definedness_hash_entry
*ret
1946 = (struct lang_definedness_hash_entry
*) entry
;
1949 ret
= (struct lang_definedness_hash_entry
*)
1950 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
1953 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
1955 ret
->iteration
= -1;
1959 /* Return the iteration when the definition of NAME was last updated. A
1960 value of -1 means that the symbol is not defined in the linker script
1961 or the command line, but may be defined in the linker symbol table. */
1964 lang_symbol_definition_iteration (const char *name
)
1966 struct lang_definedness_hash_entry
*defentry
1967 = (struct lang_definedness_hash_entry
*)
1968 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1970 /* We've already created this one on the presence of DEFINED in the
1971 script, so it can't be NULL unless something is borked elsewhere in
1973 if (defentry
== NULL
)
1976 return defentry
->iteration
;
1979 /* Update the definedness state of NAME. */
1982 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
1984 struct lang_definedness_hash_entry
*defentry
1985 = (struct lang_definedness_hash_entry
*)
1986 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1988 /* We don't keep track of symbols not tested with DEFINED. */
1989 if (defentry
== NULL
)
1992 /* If the symbol was already defined, and not from an earlier statement
1993 iteration, don't update the definedness iteration, because that'd
1994 make the symbol seem defined in the linker script at this point, and
1995 it wasn't; it was defined in some object. If we do anyway, DEFINED
1996 would start to yield false before this point and the construct "sym =
1997 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1999 if (h
->type
!= bfd_link_hash_undefined
2000 && h
->type
!= bfd_link_hash_common
2001 && h
->type
!= bfd_link_hash_new
2002 && defentry
->iteration
== -1)
2005 defentry
->iteration
= lang_statement_iteration
;
2008 /* Add the supplied name to the symbol table as an undefined reference.
2009 This is a two step process as the symbol table doesn't even exist at
2010 the time the ld command line is processed. First we put the name
2011 on a list, then, once the output file has been opened, transfer the
2012 name to the symbol table. */
2014 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2016 #define ldlang_undef_chain_list_head entry_symbol.next
2019 ldlang_add_undef (const char *const name
)
2021 ldlang_undef_chain_list_type
*new =
2022 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
2024 new->next
= ldlang_undef_chain_list_head
;
2025 ldlang_undef_chain_list_head
= new;
2027 new->name
= xstrdup (name
);
2029 if (output_bfd
!= NULL
)
2030 insert_undefined (new->name
);
2033 /* Insert NAME as undefined in the symbol table. */
2036 insert_undefined (const char *name
)
2038 struct bfd_link_hash_entry
*h
;
2040 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2042 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2043 if (h
->type
== bfd_link_hash_new
)
2045 h
->type
= bfd_link_hash_undefined
;
2046 h
->u
.undef
.abfd
= NULL
;
2047 bfd_link_add_undef (link_info
.hash
, h
);
2051 /* Run through the list of undefineds created above and place them
2052 into the linker hash table as undefined symbols belonging to the
2056 lang_place_undefineds (void)
2058 ldlang_undef_chain_list_type
*ptr
;
2060 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
2061 insert_undefined (ptr
->name
);
2064 /* Open input files and attach to output sections. */
2067 map_input_to_output_sections
2068 (lang_statement_union_type
*s
, const char *target
,
2069 lang_output_section_statement_type
*output_section_statement
)
2071 for (; s
!= NULL
; s
= s
->header
.next
)
2073 switch (s
->header
.type
)
2075 case lang_wild_statement_enum
:
2076 wild (&s
->wild_statement
, target
, output_section_statement
);
2078 case lang_constructors_statement_enum
:
2079 map_input_to_output_sections (constructor_list
.head
,
2081 output_section_statement
);
2083 case lang_output_section_statement_enum
:
2084 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2086 &s
->output_section_statement
);
2088 case lang_output_statement_enum
:
2090 case lang_target_statement_enum
:
2091 target
= s
->target_statement
.target
;
2093 case lang_group_statement_enum
:
2094 map_input_to_output_sections (s
->group_statement
.children
.head
,
2096 output_section_statement
);
2098 case lang_data_statement_enum
:
2099 /* Make sure that any sections mentioned in the expression
2101 exp_init_os (s
->data_statement
.exp
);
2103 case lang_fill_statement_enum
:
2104 case lang_input_section_enum
:
2105 case lang_object_symbols_statement_enum
:
2106 case lang_reloc_statement_enum
:
2107 case lang_padding_statement_enum
:
2108 case lang_input_statement_enum
:
2109 if (output_section_statement
!= NULL
2110 && output_section_statement
->bfd_section
== NULL
)
2111 init_os (output_section_statement
);
2113 case lang_assignment_statement_enum
:
2114 if (output_section_statement
!= NULL
2115 && output_section_statement
->bfd_section
== NULL
)
2116 init_os (output_section_statement
);
2118 /* Make sure that any sections mentioned in the assignment
2120 exp_init_os (s
->assignment_statement
.exp
);
2122 case lang_afile_asection_pair_statement_enum
:
2125 case lang_address_statement_enum
:
2126 /* Mark the specified section with the supplied address. */
2128 lang_output_section_statement_type
*os
=
2129 lang_output_section_statement_lookup
2130 (s
->address_statement
.section_name
);
2132 if (os
->bfd_section
== NULL
)
2134 os
->addr_tree
= s
->address_statement
.address
;
2141 /* An output section might have been removed after its statement was
2142 added. For example, ldemul_before_allocation can remove dynamic
2143 sections if they turn out to be not needed. Clean them up here. */
2146 strip_excluded_output_sections (void)
2148 lang_statement_union_type
*u
;
2150 for (u
= lang_output_section_statement
.head
;
2152 u
= u
->output_section_statement
.next
)
2154 lang_output_section_statement_type
*os
;
2157 os
= &u
->output_section_statement
;
2158 s
= os
->bfd_section
;
2159 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2163 os
->bfd_section
= NULL
;
2165 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2168 bfd_section_list_remove (output_bfd
, p
);
2169 output_bfd
->section_count
--;
2177 print_output_section_statement
2178 (lang_output_section_statement_type
*output_section_statement
)
2180 asection
*section
= output_section_statement
->bfd_section
;
2183 if (output_section_statement
!= abs_output_section
)
2185 minfo ("\n%s", output_section_statement
->name
);
2187 if (section
!= NULL
)
2189 print_dot
= section
->vma
;
2191 len
= strlen (output_section_statement
->name
);
2192 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2197 while (len
< SECTION_NAME_MAP_LENGTH
)
2203 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2205 if (output_section_statement
->load_base
!= NULL
)
2209 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2210 "load base", lang_final_phase_enum
);
2211 minfo (_(" load address 0x%V"), addr
);
2218 print_statement_list (output_section_statement
->children
.head
,
2219 output_section_statement
);
2223 print_assignment (lang_assignment_statement_type
*assignment
,
2224 lang_output_section_statement_type
*output_section
)
2227 etree_value_type result
;
2229 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2232 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2233 lang_final_phase_enum
, print_dot
, &print_dot
);
2239 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2240 dst
= assignment
->exp
->assign
.dst
;
2242 minfo ("0x%V", value
);
2243 if (dst
[0] == '.' && dst
[1] == 0)
2256 exp_print_tree (assignment
->exp
);
2262 print_input_statement (lang_input_statement_type
*statm
)
2264 if (statm
->filename
!= NULL
)
2266 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2270 /* Print all symbols defined in a particular section. This is called
2271 via bfd_link_hash_traverse. */
2274 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
2276 asection
*sec
= ptr
;
2278 if ((hash_entry
->type
== bfd_link_hash_defined
2279 || hash_entry
->type
== bfd_link_hash_defweak
)
2280 && sec
== hash_entry
->u
.def
.section
)
2284 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2287 (hash_entry
->u
.def
.value
2288 + hash_entry
->u
.def
.section
->output_offset
2289 + hash_entry
->u
.def
.section
->output_section
->vma
));
2291 minfo (" %T\n", hash_entry
->root
.string
);
2297 /* Print information about an input section to the map file. */
2300 print_input_section (lang_input_section_type
*in
)
2302 asection
*i
= in
->section
;
2303 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2310 minfo ("%s", i
->name
);
2312 if (i
->output_section
!= NULL
)
2316 len
= 1 + strlen (i
->name
);
2317 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2322 while (len
< SECTION_NAME_MAP_LENGTH
)
2328 minfo ("0x%V %W %B\n",
2329 i
->output_section
->vma
+ i
->output_offset
, TO_ADDR (size
),
2332 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2334 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2346 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2349 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
2351 print_dot
= (i
->output_section
->vma
+ i
->output_offset
2358 print_fill_statement (lang_fill_statement_type
*fill
)
2362 fputs (" FILL mask 0x", config
.map_file
);
2363 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2364 fprintf (config
.map_file
, "%02x", *p
);
2365 fputs ("\n", config
.map_file
);
2369 print_data_statement (lang_data_statement_type
*data
)
2377 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2380 addr
= data
->output_vma
;
2381 if (data
->output_section
!= NULL
)
2382 addr
+= data
->output_section
->vma
;
2410 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2412 if (data
->exp
->type
.node_class
!= etree_value
)
2415 exp_print_tree (data
->exp
);
2420 print_dot
= addr
+ TO_ADDR (size
);
2423 /* Print an address statement. These are generated by options like
2427 print_address_statement (lang_address_statement_type
*address
)
2429 minfo (_("Address of section %s set to "), address
->section_name
);
2430 exp_print_tree (address
->address
);
2434 /* Print a reloc statement. */
2437 print_reloc_statement (lang_reloc_statement_type
*reloc
)
2444 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2447 addr
= reloc
->output_vma
;
2448 if (reloc
->output_section
!= NULL
)
2449 addr
+= reloc
->output_section
->vma
;
2451 size
= bfd_get_reloc_size (reloc
->howto
);
2453 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2455 if (reloc
->name
!= NULL
)
2456 minfo ("%s+", reloc
->name
);
2458 minfo ("%s+", reloc
->section
->name
);
2460 exp_print_tree (reloc
->addend_exp
);
2464 print_dot
= addr
+ TO_ADDR (size
);
2468 print_padding_statement (lang_padding_statement_type
*s
)
2476 len
= sizeof " *fill*" - 1;
2477 while (len
< SECTION_NAME_MAP_LENGTH
)
2483 addr
= s
->output_offset
;
2484 if (s
->output_section
!= NULL
)
2485 addr
+= s
->output_section
->vma
;
2486 minfo ("0x%V %W ", addr
, s
->size
);
2488 if (s
->fill
->size
!= 0)
2492 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2493 fprintf (config
.map_file
, "%02x", *p
);
2498 print_dot
= addr
+ TO_ADDR (s
->size
);
2502 print_wild_statement (lang_wild_statement_type
*w
,
2503 lang_output_section_statement_type
*os
)
2505 struct wildcard_list
*sec
;
2509 if (w
->filenames_sorted
)
2511 if (w
->filename
!= NULL
)
2512 minfo ("%s", w
->filename
);
2515 if (w
->filenames_sorted
)
2519 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2521 if (sec
->spec
.sorted
)
2523 if (sec
->spec
.exclude_name_list
!= NULL
)
2526 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2527 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2528 minfo (" %s", tmp
->name
);
2531 if (sec
->spec
.name
!= NULL
)
2532 minfo ("%s", sec
->spec
.name
);
2535 if (sec
->spec
.sorted
)
2544 print_statement_list (w
->children
.head
, os
);
2547 /* Print a group statement. */
2550 print_group (lang_group_statement_type
*s
,
2551 lang_output_section_statement_type
*os
)
2553 fprintf (config
.map_file
, "START GROUP\n");
2554 print_statement_list (s
->children
.head
, os
);
2555 fprintf (config
.map_file
, "END GROUP\n");
2558 /* Print the list of statements in S.
2559 This can be called for any statement type. */
2562 print_statement_list (lang_statement_union_type
*s
,
2563 lang_output_section_statement_type
*os
)
2567 print_statement (s
, os
);
2572 /* Print the first statement in statement list S.
2573 This can be called for any statement type. */
2576 print_statement (lang_statement_union_type
*s
,
2577 lang_output_section_statement_type
*os
)
2579 switch (s
->header
.type
)
2582 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2585 case lang_constructors_statement_enum
:
2586 if (constructor_list
.head
!= NULL
)
2588 if (constructors_sorted
)
2589 minfo (" SORT (CONSTRUCTORS)\n");
2591 minfo (" CONSTRUCTORS\n");
2592 print_statement_list (constructor_list
.head
, os
);
2595 case lang_wild_statement_enum
:
2596 print_wild_statement (&s
->wild_statement
, os
);
2598 case lang_address_statement_enum
:
2599 print_address_statement (&s
->address_statement
);
2601 case lang_object_symbols_statement_enum
:
2602 minfo (" CREATE_OBJECT_SYMBOLS\n");
2604 case lang_fill_statement_enum
:
2605 print_fill_statement (&s
->fill_statement
);
2607 case lang_data_statement_enum
:
2608 print_data_statement (&s
->data_statement
);
2610 case lang_reloc_statement_enum
:
2611 print_reloc_statement (&s
->reloc_statement
);
2613 case lang_input_section_enum
:
2614 print_input_section (&s
->input_section
);
2616 case lang_padding_statement_enum
:
2617 print_padding_statement (&s
->padding_statement
);
2619 case lang_output_section_statement_enum
:
2620 print_output_section_statement (&s
->output_section_statement
);
2622 case lang_assignment_statement_enum
:
2623 print_assignment (&s
->assignment_statement
, os
);
2625 case lang_target_statement_enum
:
2626 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2628 case lang_output_statement_enum
:
2629 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2630 if (output_target
!= NULL
)
2631 minfo (" %s", output_target
);
2634 case lang_input_statement_enum
:
2635 print_input_statement (&s
->input_statement
);
2637 case lang_group_statement_enum
:
2638 print_group (&s
->group_statement
, os
);
2640 case lang_afile_asection_pair_statement_enum
:
2647 print_statements (void)
2649 print_statement_list (statement_list
.head
, abs_output_section
);
2652 /* Print the first N statements in statement list S to STDERR.
2653 If N == 0, nothing is printed.
2654 If N < 0, the entire list is printed.
2655 Intended to be called from GDB. */
2658 dprint_statement (lang_statement_union_type
*s
, int n
)
2660 FILE *map_save
= config
.map_file
;
2662 config
.map_file
= stderr
;
2665 print_statement_list (s
, abs_output_section
);
2668 while (s
&& --n
>= 0)
2670 print_statement (s
, abs_output_section
);
2675 config
.map_file
= map_save
;
2679 insert_pad (lang_statement_union_type
**ptr
,
2681 unsigned int alignment_needed
,
2682 asection
*output_section
,
2685 static fill_type zero_fill
= { 1, { 0 } };
2686 lang_statement_union_type
*pad
;
2688 pad
= ((lang_statement_union_type
*)
2689 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2690 if (ptr
!= &statement_list
.head
2691 && pad
->header
.type
== lang_padding_statement_enum
2692 && pad
->padding_statement
.output_section
== output_section
)
2694 /* Use the existing pad statement. The above test on output
2695 section is probably redundant, but it doesn't hurt to check. */
2699 /* Make a new padding statement, linked into existing chain. */
2700 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
2701 pad
->header
.next
= *ptr
;
2703 pad
->header
.type
= lang_padding_statement_enum
;
2704 pad
->padding_statement
.output_section
= output_section
;
2707 pad
->padding_statement
.fill
= fill
;
2709 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2710 pad
->padding_statement
.size
= alignment_needed
;
2711 output_section
->_raw_size
+= alignment_needed
;
2714 /* Work out how much this section will move the dot point. */
2717 size_input_section (lang_statement_union_type
**this_ptr
,
2718 lang_output_section_statement_type
*output_section_statement
,
2722 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2723 asection
*i
= is
->section
;
2725 if (!is
->ifile
->just_syms_flag
)
2727 unsigned int alignment_needed
;
2730 /* Align this section first to the input sections requirement,
2731 then to the output section's requirement. If this alignment
2732 is greater than any seen before, then record it too. Perform
2733 the alignment by inserting a magic 'padding' statement. */
2735 if (output_section_statement
->subsection_alignment
!= -1)
2736 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2738 o
= output_section_statement
->bfd_section
;
2739 if (o
->alignment_power
< i
->alignment_power
)
2740 o
->alignment_power
= i
->alignment_power
;
2742 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2744 if (alignment_needed
!= 0)
2746 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
2747 dot
+= alignment_needed
;
2750 /* Remember where in the output section this input section goes. */
2752 i
->output_offset
= dot
- o
->vma
;
2754 /* Mark how big the output section must be to contain this now. */
2755 if (i
->_cooked_size
!= 0)
2756 dot
+= TO_ADDR (i
->_cooked_size
);
2758 dot
+= TO_ADDR (i
->_raw_size
);
2759 o
->_raw_size
= TO_SIZE (dot
- o
->vma
);
2763 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2769 #define IGNORE_SECTION(bfd, s) \
2770 (((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2771 ? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2773 : ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2775 || bfd_section_size (bfd, s) == 0)
2777 /* Check to see if any allocated sections overlap with other allocated
2778 sections. This can happen when the linker script specifically specifies
2779 the output section addresses of the two sections. */
2782 lang_check_section_addresses (void)
2786 /* Scan all sections in the output list. */
2787 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2791 /* Ignore sections which are not loaded or which have no contents. */
2792 if (IGNORE_SECTION (output_bfd
, s
))
2795 /* Once we reach section 's' stop our seach. This prevents two
2796 warning messages from being produced, one for 'section A overlaps
2797 section B' and one for 'section B overlaps section A'. */
2798 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2805 /* Only consider loadable sections with real contents. */
2806 if (IGNORE_SECTION (output_bfd
, os
))
2809 /* We must check the sections' LMA addresses not their
2810 VMA addresses because overlay sections can have
2811 overlapping VMAs but they must have distinct LMAs. */
2812 s_start
= bfd_section_lma (output_bfd
, s
);
2813 os_start
= bfd_section_lma (output_bfd
, os
);
2814 s_end
= s_start
+ TO_ADDR (bfd_section_size (output_bfd
, s
)) - 1;
2815 os_end
= os_start
+ TO_ADDR (bfd_section_size (output_bfd
, os
)) - 1;
2817 /* Look for an overlap. */
2818 if ((s_end
< os_start
) || (s_start
> os_end
))
2822 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2823 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2825 /* Once we have found one overlap for this section,
2826 stop looking for others. */
2832 /* Make sure the new address is within the region. We explicitly permit the
2833 current address to be at the exact end of the region when the address is
2834 non-zero, in case the region is at the end of addressable memory and the
2835 calculation wraps around. */
2838 os_region_check (lang_output_section_statement_type
*os
,
2839 lang_memory_region_type
*region
,
2843 if ((region
->current
< region
->origin
2844 || (region
->current
- region
->origin
> region
->length
))
2845 && ((region
->current
!= region
->origin
+ region
->length
)
2850 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2852 os
->bfd_section
->owner
,
2853 os
->bfd_section
->name
,
2858 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2860 os
->bfd_section
->owner
,
2861 os
->bfd_section
->name
);
2863 /* Reset the region pointer. */
2864 region
->current
= region
->origin
;
2868 /* Set the sizes for all the output sections. */
2871 lang_size_sections_1
2872 (lang_statement_union_type
*s
,
2873 lang_output_section_statement_type
*output_section_statement
,
2874 lang_statement_union_type
**prev
,
2878 bfd_boolean check_regions
)
2880 /* Size up the sections from their constituent parts. */
2881 for (; s
!= NULL
; s
= s
->header
.next
)
2883 switch (s
->header
.type
)
2885 case lang_output_section_statement_enum
:
2888 lang_output_section_statement_type
*os
;
2890 os
= &s
->output_section_statement
;
2891 if (os
->bfd_section
== NULL
)
2892 /* This section was never actually created. */
2895 /* If this is a COFF shared library section, use the size and
2896 address from the input section. FIXME: This is COFF
2897 specific; it would be cleaner if there were some other way
2898 to do this, but nothing simple comes to mind. */
2899 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2903 if (os
->children
.head
== NULL
2904 || os
->children
.head
->header
.next
!= NULL
2905 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2906 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2909 input
= os
->children
.head
->input_section
.section
;
2910 bfd_set_section_vma (os
->bfd_section
->owner
,
2912 bfd_section_vma (input
->owner
, input
));
2913 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2917 if (bfd_is_abs_section (os
->bfd_section
))
2919 /* No matter what happens, an abs section starts at zero. */
2920 ASSERT (os
->bfd_section
->vma
== 0);
2924 if (os
->addr_tree
== NULL
)
2926 /* No address specified for this section, get one
2927 from the region specification. */
2928 if (os
->region
== NULL
2929 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2930 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2931 && os
->region
->name
[0] == '*'
2932 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0))
2934 os
->region
= lang_memory_default (os
->bfd_section
);
2937 /* If a loadable section is using the default memory
2938 region, and some non default memory regions were
2939 defined, issue an error message. */
2940 if (!IGNORE_SECTION (output_bfd
, os
->bfd_section
)
2941 && ! link_info
.relocatable
2943 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0
2944 && lang_memory_region_list
!= NULL
2945 && (strcmp (lang_memory_region_list
->name
,
2946 DEFAULT_MEMORY_REGION
) != 0
2947 || lang_memory_region_list
->next
!= NULL
))
2949 /* By default this is an error rather than just a
2950 warning because if we allocate the section to the
2951 default memory region we can end up creating an
2952 excessively large binary, or even seg faulting when
2953 attempting to perform a negative seek. See
2954 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2955 for an example of this. This behaviour can be
2956 overridden by the using the --no-check-sections
2958 if (command_line
.check_section_addresses
)
2959 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2960 bfd_get_section_name (output_bfd
,
2963 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2964 bfd_get_section_name (output_bfd
,
2968 dot
= os
->region
->current
;
2970 if (os
->section_alignment
== -1)
2975 dot
= align_power (dot
,
2976 os
->bfd_section
->alignment_power
);
2978 if (dot
!= olddot
&& config
.warn_section_align
)
2979 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2980 os
->name
, (unsigned int) (dot
- olddot
));
2988 r
= exp_fold_tree (os
->addr_tree
,
2990 lang_allocating_phase_enum
,
2995 einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
2998 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
3001 /* The section starts here.
3002 First, align to what the section needs. */
3004 if (os
->section_alignment
!= -1)
3005 dot
= align_power (dot
, os
->section_alignment
);
3007 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3009 os
->bfd_section
->output_offset
= 0;
3012 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3013 os
->fill
, dot
, relax
, check_regions
);
3015 /* Put the section within the requested block size, or
3016 align at the block boundary. */
3017 after
= ((os
->bfd_section
->vma
3018 + TO_ADDR (os
->bfd_section
->_raw_size
)
3019 + os
->block_value
- 1)
3020 & - (bfd_vma
) os
->block_value
);
3022 if (bfd_is_abs_section (os
->bfd_section
))
3023 ASSERT (after
== os
->bfd_section
->vma
);
3025 os
->bfd_section
->_raw_size
3026 = TO_SIZE (after
- os
->bfd_section
->vma
);
3028 dot
= os
->bfd_section
->vma
;
3029 /* .tbss sections effectively have zero size. */
3030 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3031 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3032 || link_info
.relocatable
)
3033 dot
+= TO_ADDR (os
->bfd_section
->_raw_size
);
3037 if (os
->update_dot_tree
!= 0)
3038 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3039 lang_allocating_phase_enum
, dot
, &dot
);
3041 /* Update dot in the region ?
3042 We only do this if the section is going to be allocated,
3043 since unallocated sections do not contribute to the region's
3044 overall size in memory.
3046 If the SEC_NEVER_LOAD bit is not set, it will affect the
3047 addresses of sections after it. We have to update
3049 if (os
->region
!= NULL
3050 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3051 & SEC_NEVER_LOAD
) == 0
3052 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3053 & (SEC_ALLOC
| SEC_LOAD
))))
3055 os
->region
->current
= dot
;
3058 /* Make sure the new address is within the region. */
3059 os_region_check (os
, os
->region
, os
->addr_tree
,
3060 os
->bfd_section
->vma
);
3062 /* If there's no load address specified, use the run
3063 region as the load region. */
3064 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3065 os
->lma_region
= os
->region
;
3067 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3069 /* Set load_base, which will be handled later. */
3070 os
->load_base
= exp_intop (os
->lma_region
->current
);
3071 os
->lma_region
->current
+=
3072 TO_ADDR (os
->bfd_section
->_raw_size
);
3074 os_region_check (os
, os
->lma_region
, NULL
,
3075 os
->bfd_section
->lma
);
3081 case lang_constructors_statement_enum
:
3082 dot
= lang_size_sections_1 (constructor_list
.head
,
3083 output_section_statement
,
3084 &s
->wild_statement
.children
.head
,
3085 fill
, dot
, relax
, check_regions
);
3088 case lang_data_statement_enum
:
3090 unsigned int size
= 0;
3092 s
->data_statement
.output_vma
=
3093 dot
- output_section_statement
->bfd_section
->vma
;
3094 s
->data_statement
.output_section
=
3095 output_section_statement
->bfd_section
;
3097 /* We might refer to provided symbols in the expression, and
3098 need to mark them as needed. */
3099 exp_fold_tree (s
->data_statement
.exp
, abs_output_section
,
3100 lang_allocating_phase_enum
, dot
, &dot
);
3102 switch (s
->data_statement
.type
)
3120 if (size
< TO_SIZE ((unsigned) 1))
3121 size
= TO_SIZE ((unsigned) 1);
3122 dot
+= TO_ADDR (size
);
3123 output_section_statement
->bfd_section
->_raw_size
+= size
;
3124 /* The output section gets contents, and then we inspect for
3125 any flags set in the input script which override any ALLOC. */
3126 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3127 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3129 output_section_statement
->bfd_section
->flags
|=
3130 SEC_ALLOC
| SEC_LOAD
;
3135 case lang_reloc_statement_enum
:
3139 s
->reloc_statement
.output_vma
=
3140 dot
- output_section_statement
->bfd_section
->vma
;
3141 s
->reloc_statement
.output_section
=
3142 output_section_statement
->bfd_section
;
3143 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3144 dot
+= TO_ADDR (size
);
3145 output_section_statement
->bfd_section
->_raw_size
+= size
;
3149 case lang_wild_statement_enum
:
3151 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3152 output_section_statement
,
3153 &s
->wild_statement
.children
.head
,
3154 fill
, dot
, relax
, check_regions
);
3158 case lang_object_symbols_statement_enum
:
3159 link_info
.create_object_symbols_section
=
3160 output_section_statement
->bfd_section
;
3162 case lang_output_statement_enum
:
3163 case lang_target_statement_enum
:
3165 case lang_input_section_enum
:
3169 i
= (*prev
)->input_section
.section
;
3172 if (i
->_cooked_size
== 0)
3173 i
->_cooked_size
= i
->_raw_size
;
3179 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3180 einfo (_("%P%F: can't relax section: %E\n"));
3184 dot
= size_input_section (prev
, output_section_statement
,
3185 output_section_statement
->fill
, dot
);
3188 case lang_input_statement_enum
:
3190 case lang_fill_statement_enum
:
3191 s
->fill_statement
.output_section
=
3192 output_section_statement
->bfd_section
;
3194 fill
= s
->fill_statement
.fill
;
3196 case lang_assignment_statement_enum
:
3198 bfd_vma newdot
= dot
;
3200 exp_fold_tree (s
->assignment_statement
.exp
,
3201 output_section_statement
,
3202 lang_allocating_phase_enum
,
3208 if (output_section_statement
== abs_output_section
)
3210 /* If we don't have an output section, then just adjust
3211 the default memory address. */
3212 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
)->current
= newdot
;
3216 /* Insert a pad after this statement. We can't
3217 put the pad before when relaxing, in case the
3218 assignment references dot. */
3219 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
3220 output_section_statement
->bfd_section
, dot
);
3222 /* Don't neuter the pad below when relaxing. */
3226 /* If dot is advanced, this implies that the section should
3227 have space allocated to it, unless the user has explicitly
3228 stated that the section should never be loaded. */
3229 if (!(output_section_statement
->flags
& (SEC_NEVER_LOAD
| SEC_ALLOC
)))
3230 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
3237 case lang_padding_statement_enum
:
3238 /* If this is the first time lang_size_sections is called,
3239 we won't have any padding statements. If this is the
3240 second or later passes when relaxing, we should allow
3241 padding to shrink. If padding is needed on this pass, it
3242 will be added back in. */
3243 s
->padding_statement
.size
= 0;
3245 /* Make sure output_offset is valid. If relaxation shrinks
3246 the section and this pad isn't needed, it's possible to
3247 have output_offset larger than the final size of the
3248 section. bfd_set_section_contents will complain even for
3249 a pad size of zero. */
3250 s
->padding_statement
.output_offset
3251 = dot
- output_section_statement
->bfd_section
->vma
;
3254 case lang_group_statement_enum
:
3255 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3256 output_section_statement
,
3257 &s
->group_statement
.children
.head
,
3258 fill
, dot
, relax
, check_regions
);
3265 /* We can only get here when relaxing is turned on. */
3266 case lang_address_statement_enum
:
3269 prev
= &s
->header
.next
;
3276 (lang_statement_union_type
*s
,
3277 lang_output_section_statement_type
*output_section_statement
,
3278 lang_statement_union_type
**prev
,
3282 bfd_boolean check_regions
)
3287 /* Callers of exp_fold_tree need to increment this. */
3288 lang_statement_iteration
++;
3290 exp_data_seg
.phase
= exp_dataseg_none
;
3291 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3292 dot
, relax
, check_regions
);
3293 if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3295 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3296 a page could be saved in the data segment. */
3297 bfd_vma first
, last
;
3299 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3300 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3302 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3303 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3304 && first
+ last
<= exp_data_seg
.pagesize
)
3306 exp_data_seg
.phase
= exp_dataseg_adjust
;
3307 lang_statement_iteration
++;
3308 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3309 fill
, dot
, relax
, check_regions
);
3313 /* Some backend relaxers want to refer to the output section size. Give
3314 them a section size that does not change on the next call while they
3315 relax. We can't set this at top because lang_reset_memory_regions
3316 which is called before we get here, sets _raw_size to 0 on relaxing
3318 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3319 o
->_cooked_size
= o
->_raw_size
;
3324 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3327 lang_do_assignments_1
3328 (lang_statement_union_type
*s
,
3329 lang_output_section_statement_type
*output_section_statement
,
3333 for (; s
!= NULL
; s
= s
->header
.next
)
3335 switch (s
->header
.type
)
3337 case lang_constructors_statement_enum
:
3338 dot
= lang_do_assignments_1 (constructor_list
.head
,
3339 output_section_statement
,
3344 case lang_output_section_statement_enum
:
3346 lang_output_section_statement_type
*os
;
3348 os
= &(s
->output_section_statement
);
3349 if (os
->bfd_section
!= NULL
)
3351 dot
= os
->bfd_section
->vma
;
3352 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
3353 /* .tbss sections effectively have zero size. */
3354 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3355 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3356 || link_info
.relocatable
)
3357 dot
+= TO_ADDR (os
->bfd_section
->_raw_size
);
3361 /* If nothing has been placed into the output section then
3362 it won't have a bfd_section. */
3363 if (os
->bfd_section
)
3365 os
->bfd_section
->lma
3366 = exp_get_abs_int (os
->load_base
, 0, "load base",
3367 lang_final_phase_enum
);
3372 case lang_wild_statement_enum
:
3374 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
3375 output_section_statement
,
3380 case lang_object_symbols_statement_enum
:
3381 case lang_output_statement_enum
:
3382 case lang_target_statement_enum
:
3384 case lang_common_statement_enum
:
3387 case lang_data_statement_enum
:
3389 etree_value_type value
;
3391 value
= exp_fold_tree (s
->data_statement
.exp
,
3393 lang_final_phase_enum
, dot
, &dot
);
3395 einfo (_("%F%P: invalid data statement\n"));
3396 s
->data_statement
.value
3397 = value
.value
+ value
.section
->bfd_section
->vma
;
3401 switch (s
->data_statement
.type
)
3419 if (size
< TO_SIZE ((unsigned) 1))
3420 size
= TO_SIZE ((unsigned) 1);
3421 dot
+= TO_ADDR (size
);
3425 case lang_reloc_statement_enum
:
3427 etree_value_type value
;
3429 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3431 lang_final_phase_enum
, dot
, &dot
);
3432 s
->reloc_statement
.addend_value
= value
.value
;
3434 einfo (_("%F%P: invalid reloc statement\n"));
3436 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
3439 case lang_input_section_enum
:
3441 asection
*in
= s
->input_section
.section
;
3443 if (in
->_cooked_size
!= 0)
3444 dot
+= TO_ADDR (in
->_cooked_size
);
3446 dot
+= TO_ADDR (in
->_raw_size
);
3450 case lang_input_statement_enum
:
3452 case lang_fill_statement_enum
:
3453 fill
= s
->fill_statement
.fill
;
3455 case lang_assignment_statement_enum
:
3457 exp_fold_tree (s
->assignment_statement
.exp
,
3458 output_section_statement
,
3459 lang_final_phase_enum
,
3465 case lang_padding_statement_enum
:
3466 dot
+= TO_ADDR (s
->padding_statement
.size
);
3469 case lang_group_statement_enum
:
3470 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
3471 output_section_statement
,
3479 case lang_address_statement_enum
:
3488 lang_do_assignments (lang_statement_union_type
*s
,
3489 lang_output_section_statement_type
*output_section_statement
,
3493 /* Callers of exp_fold_tree need to increment this. */
3494 lang_statement_iteration
++;
3495 lang_do_assignments_1 (s
, output_section_statement
, fill
, dot
);
3498 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3499 operator .startof. (section_name), it produces an undefined symbol
3500 .startof.section_name. Similarly, when it sees
3501 .sizeof. (section_name), it produces an undefined symbol
3502 .sizeof.section_name. For all the output sections, we look for
3503 such symbols, and set them to the correct value. */
3506 lang_set_startof (void)
3510 if (link_info
.relocatable
)
3513 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3515 const char *secname
;
3517 struct bfd_link_hash_entry
*h
;
3519 secname
= bfd_get_section_name (output_bfd
, s
);
3520 buf
= xmalloc (10 + strlen (secname
));
3522 sprintf (buf
, ".startof.%s", secname
);
3523 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3524 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3526 h
->type
= bfd_link_hash_defined
;
3527 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3528 h
->u
.def
.section
= bfd_abs_section_ptr
;
3531 sprintf (buf
, ".sizeof.%s", secname
);
3532 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3533 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3535 h
->type
= bfd_link_hash_defined
;
3536 if (s
->_cooked_size
!= 0)
3537 h
->u
.def
.value
= TO_ADDR (s
->_cooked_size
);
3539 h
->u
.def
.value
= TO_ADDR (s
->_raw_size
);
3540 h
->u
.def
.section
= bfd_abs_section_ptr
;
3550 struct bfd_link_hash_entry
*h
;
3553 if (link_info
.relocatable
|| link_info
.shared
)
3558 if (entry_symbol
.name
== NULL
)
3560 /* No entry has been specified. Look for start, but don't warn
3561 if we don't find it. */
3562 entry_symbol
.name
= "start";
3566 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3567 FALSE
, FALSE
, TRUE
);
3569 && (h
->type
== bfd_link_hash_defined
3570 || h
->type
== bfd_link_hash_defweak
)
3571 && h
->u
.def
.section
->output_section
!= NULL
)
3575 val
= (h
->u
.def
.value
3576 + bfd_get_section_vma (output_bfd
,
3577 h
->u
.def
.section
->output_section
)
3578 + h
->u
.def
.section
->output_offset
);
3579 if (! bfd_set_start_address (output_bfd
, val
))
3580 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3587 /* We couldn't find the entry symbol. Try parsing it as a
3589 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3592 if (! bfd_set_start_address (output_bfd
, val
))
3593 einfo (_("%P%F: can't set start address\n"));
3599 /* Can't find the entry symbol, and it's not a number. Use
3600 the first address in the text section. */
3601 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3605 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3607 bfd_get_section_vma (output_bfd
, ts
));
3608 if (! bfd_set_start_address (output_bfd
,
3609 bfd_get_section_vma (output_bfd
,
3611 einfo (_("%P%F: can't set start address\n"));
3616 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3622 bfd_hash_table_free (&lang_definedness_table
);
3625 /* This is a small function used when we want to ignore errors from
3629 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3631 /* Don't do anything. */
3634 /* Check that the architecture of all the input files is compatible
3635 with the output file. Also call the backend to let it do any
3636 other checking that is needed. */
3641 lang_statement_union_type
*file
;
3643 const bfd_arch_info_type
*compatible
;
3645 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
3647 input_bfd
= file
->input_statement
.the_bfd
;
3648 compatible
= bfd_arch_get_compatible (input_bfd
, output_bfd
,
3649 command_line
.accept_unknown_input_arch
);
3651 /* In general it is not possible to perform a relocatable
3652 link between differing object formats when the input
3653 file has relocations, because the relocations in the
3654 input format may not have equivalent representations in
3655 the output format (and besides BFD does not translate
3656 relocs for other link purposes than a final link). */
3657 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
3658 && (compatible
== NULL
3659 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3660 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3662 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3663 bfd_get_target (input_bfd
), input_bfd
,
3664 bfd_get_target (output_bfd
), output_bfd
);
3665 /* einfo with %F exits. */
3668 if (compatible
== NULL
)
3670 if (command_line
.warn_mismatch
)
3671 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3672 bfd_printable_name (input_bfd
), input_bfd
,
3673 bfd_printable_name (output_bfd
));
3675 else if (bfd_count_sections (input_bfd
))
3677 /* If the input bfd has no contents, it shouldn't set the
3678 private data of the output bfd. */
3680 bfd_error_handler_type pfn
= NULL
;
3682 /* If we aren't supposed to warn about mismatched input
3683 files, temporarily set the BFD error handler to a
3684 function which will do nothing. We still want to call
3685 bfd_merge_private_bfd_data, since it may set up
3686 information which is needed in the output file. */
3687 if (! command_line
.warn_mismatch
)
3688 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3689 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3691 if (command_line
.warn_mismatch
)
3692 einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3695 if (! command_line
.warn_mismatch
)
3696 bfd_set_error_handler (pfn
);
3701 /* Look through all the global common symbols and attach them to the
3702 correct section. The -sort-common command line switch may be used
3703 to roughly sort the entries by size. */
3708 if (command_line
.inhibit_common_definition
)
3710 if (link_info
.relocatable
3711 && ! command_line
.force_common_definition
)
3714 if (! config
.sort_common
)
3715 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
3720 for (power
= 4; power
>= 0; power
--)
3721 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
3725 /* Place one common symbol in the correct section. */
3728 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
3730 unsigned int power_of_two
;
3734 if (h
->type
!= bfd_link_hash_common
)
3738 power_of_two
= h
->u
.c
.p
->alignment_power
;
3740 if (config
.sort_common
3741 && power_of_two
< (unsigned int) *(int *) info
)
3744 section
= h
->u
.c
.p
->section
;
3746 /* Increase the size of the section to align the common sym. */
3747 section
->_cooked_size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
3748 section
->_cooked_size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
3750 /* Adjust the alignment if necessary. */
3751 if (power_of_two
> section
->alignment_power
)
3752 section
->alignment_power
= power_of_two
;
3754 /* Change the symbol from common to defined. */
3755 h
->type
= bfd_link_hash_defined
;
3756 h
->u
.def
.section
= section
;
3757 h
->u
.def
.value
= section
->_cooked_size
;
3759 /* Increase the size of the section. */
3760 section
->_cooked_size
+= size
;
3762 /* Make sure the section is allocated in memory, and make sure that
3763 it is no longer a common section. */
3764 section
->flags
|= SEC_ALLOC
;
3765 section
->flags
&= ~SEC_IS_COMMON
;
3767 if (config
.map_file
!= NULL
)
3769 static bfd_boolean header_printed
;
3774 if (! header_printed
)
3776 minfo (_("\nAllocating common symbols\n"));
3777 minfo (_("Common symbol size file\n\n"));
3778 header_printed
= TRUE
;
3781 name
= demangle (h
->root
.string
);
3783 len
= strlen (name
);
3798 if (size
<= 0xffffffff)
3799 sprintf (buf
, "%lx", (unsigned long) size
);
3801 sprintf_vma (buf
, size
);
3811 minfo ("%B\n", section
->owner
);
3817 /* Run through the input files and ensure that every input section has
3818 somewhere to go. If one is found without a destination then create
3819 an input request and place it into the statement tree. */
3822 lang_place_orphans (void)
3824 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3828 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3830 if (s
->output_section
== NULL
)
3832 /* This section of the file is not attached, root
3833 around for a sensible place for it to go. */
3835 if (file
->just_syms_flag
)
3839 else if (strcmp (s
->name
, "COMMON") == 0)
3841 /* This is a lonely common section which must have
3842 come from an archive. We attach to the section
3843 with the wildcard. */
3844 if (! link_info
.relocatable
3845 || command_line
.force_common_definition
)
3847 if (default_common_section
== NULL
)
3850 /* This message happens when using the
3851 svr3.ifile linker script, so I have
3853 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3855 default_common_section
=
3856 lang_output_section_statement_lookup (".bss");
3859 lang_add_section (&default_common_section
->children
, s
,
3860 default_common_section
, file
);
3863 else if (ldemul_place_orphan (file
, s
))
3867 lang_output_section_statement_type
*os
;
3869 os
= lang_output_section_statement_lookup (s
->name
);
3870 lang_add_section (&os
->children
, s
, os
, file
);
3878 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
3880 flagword
*ptr_flags
;
3882 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3888 *ptr_flags
|= SEC_ALLOC
;
3892 *ptr_flags
|= SEC_READONLY
;
3896 *ptr_flags
|= SEC_DATA
;
3900 *ptr_flags
|= SEC_CODE
;
3905 *ptr_flags
|= SEC_LOAD
;
3909 einfo (_("%P%F: invalid syntax in flags\n"));
3916 /* Call a function on each input file. This function will be called
3917 on an archive, but not on the elements. */
3920 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
3922 lang_input_statement_type
*f
;
3924 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3926 f
= (lang_input_statement_type
*) f
->next_real_file
)
3930 /* Call a function on each file. The function will be called on all
3931 the elements of an archive which are included in the link, but will
3932 not be called on the archive file itself. */
3935 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
3937 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3944 ldlang_add_file (lang_input_statement_type
*entry
)
3948 lang_statement_append (&file_chain
,
3949 (lang_statement_union_type
*) entry
,
3952 /* The BFD linker needs to have a list of all input BFDs involved in
3954 ASSERT (entry
->the_bfd
->link_next
== NULL
);
3955 ASSERT (entry
->the_bfd
!= output_bfd
);
3956 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
3958 *pp
= entry
->the_bfd
;
3959 entry
->the_bfd
->usrdata
= entry
;
3960 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3962 /* Look through the sections and check for any which should not be
3963 included in the link. We need to do this now, so that we can
3964 notice when the backend linker tries to report multiple
3965 definition errors for symbols which are in sections we aren't
3966 going to link. FIXME: It might be better to entirely ignore
3967 symbols which are defined in sections which are going to be
3968 discarded. This would require modifying the backend linker for
3969 each backend which might set the SEC_LINK_ONCE flag. If we do
3970 this, we should probably handle SEC_EXCLUDE in the same way. */
3972 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
3976 lang_add_output (const char *name
, int from_script
)
3978 /* Make -o on command line override OUTPUT in script. */
3979 if (!had_output_filename
|| !from_script
)
3981 output_filename
= name
;
3982 had_output_filename
= TRUE
;
3986 static lang_output_section_statement_type
*current_section
;
3997 for (l
= 0; l
< 32; l
++)
3999 if (i
>= (unsigned int) x
)
4007 lang_output_section_statement_type
*
4008 lang_enter_output_section_statement (const char *output_section_statement_name
,
4009 etree_type
*address_exp
,
4010 enum section_type sectype
,
4012 etree_type
*subalign
,
4015 lang_output_section_statement_type
*os
;
4019 lang_output_section_statement_lookup (output_section_statement_name
);
4021 /* Add this statement to tree. */
4023 add_statement (lang_output_section_statement_enum
,
4024 output_section_statement
);
4026 /* Make next things chain into subchain of this. */
4028 if (os
->addr_tree
== NULL
)
4030 os
->addr_tree
= address_exp
;
4032 os
->sectype
= sectype
;
4033 if (sectype
!= noload_section
)
4034 os
->flags
= SEC_NO_FLAGS
;
4036 os
->flags
= SEC_NEVER_LOAD
;
4037 os
->block_value
= 1;
4038 stat_ptr
= &os
->children
;
4040 os
->subsection_alignment
=
4041 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4042 os
->section_alignment
=
4043 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4045 os
->load_base
= ebase
;
4052 lang_output_statement_type
*new =
4053 new_stat (lang_output_statement
, stat_ptr
);
4055 new->name
= output_filename
;
4058 /* Reset the current counters in the regions. */
4061 lang_reset_memory_regions (void)
4063 lang_memory_region_type
*p
= lang_memory_region_list
;
4066 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
4068 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4069 p
->current
= p
->origin
;
4072 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4076 /* If the wild pattern was marked KEEP, the member sections
4077 should be as well. */
4080 gc_section_callback (lang_wild_statement_type
*ptr
,
4081 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
4083 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
4084 void *data ATTRIBUTE_UNUSED
)
4086 if (ptr
->keep_sections
)
4087 section
->flags
|= SEC_KEEP
;
4090 /* Handle a wild statement, marking it against GC. */
4093 lang_gc_wild (lang_wild_statement_type
*s
)
4095 walk_wild (s
, gc_section_callback
, NULL
);
4098 /* Iterate over sections marking them against GC. */
4101 lang_gc_sections_1 (lang_statement_union_type
*s
)
4103 for (; s
!= NULL
; s
= s
->header
.next
)
4105 switch (s
->header
.type
)
4107 case lang_wild_statement_enum
:
4108 lang_gc_wild (&s
->wild_statement
);
4110 case lang_constructors_statement_enum
:
4111 lang_gc_sections_1 (constructor_list
.head
);
4113 case lang_output_section_statement_enum
:
4114 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4116 case lang_group_statement_enum
:
4117 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4126 lang_gc_sections (void)
4128 struct bfd_link_hash_entry
*h
;
4129 ldlang_undef_chain_list_type
*ulist
;
4131 /* Keep all sections so marked in the link script. */
4133 lang_gc_sections_1 (statement_list
.head
);
4135 /* Keep all sections containing symbols undefined on the command-line,
4136 and the section containing the entry symbol. */
4138 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4140 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4141 FALSE
, FALSE
, FALSE
);
4144 && (h
->type
== bfd_link_hash_defined
4145 || h
->type
== bfd_link_hash_defweak
)
4146 && ! bfd_is_abs_section (h
->u
.def
.section
))
4148 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4152 bfd_gc_sections (output_bfd
, &link_info
);
4158 lang_reasonable_defaults ();
4159 current_target
= default_target
;
4161 /* Open the output file. */
4162 lang_for_each_statement (ldlang_open_output
);
4165 ldemul_create_output_section_statements ();
4167 /* Add to the hash table all undefineds on the command line. */
4168 lang_place_undefineds ();
4170 already_linked_table_init ();
4172 /* Create a bfd for each input file. */
4173 current_target
= default_target
;
4174 open_input_bfds (statement_list
.head
, FALSE
);
4176 link_info
.gc_sym_list
= &entry_symbol
;
4177 if (entry_symbol
.name
== NULL
)
4178 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4180 ldemul_after_open ();
4182 already_linked_table_free ();
4184 /* Make sure that we're not mixing architectures. We call this
4185 after all the input files have been opened, but before we do any
4186 other processing, so that any operations merge_private_bfd_data
4187 does on the output file will be known during the rest of the
4191 /* Handle .exports instead of a version script if we're told to do so. */
4192 if (command_line
.version_exports_section
)
4193 lang_do_version_exports_section ();
4195 /* Build all sets based on the information gathered from the input
4197 ldctor_build_sets ();
4199 /* Remove unreferenced sections if asked to. */
4200 if (command_line
.gc_sections
)
4201 lang_gc_sections ();
4203 /* If there were any SEC_MERGE sections, finish their merging, so that
4204 section sizes can be computed. This has to be done after GC of sections,
4205 so that GCed sections are not merged, but before assigning output
4206 sections, since removing whole input sections is hard then. */
4207 bfd_merge_sections (output_bfd
, &link_info
);
4209 /* Size up the common data. */
4212 /* Run through the contours of the script and attach input sections
4213 to the correct output sections. */
4214 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
4216 /* Find any sections not attached explicitly and handle them. */
4217 lang_place_orphans ();
4219 if (! link_info
.relocatable
)
4221 /* Look for a text section and set the readonly attribute in it. */
4222 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
4226 if (config
.text_read_only
)
4227 found
->flags
|= SEC_READONLY
;
4229 found
->flags
&= ~SEC_READONLY
;
4233 /* Do anything special before sizing sections. This is where ELF
4234 and other back-ends size dynamic sections. */
4235 ldemul_before_allocation ();
4237 if (!link_info
.relocatable
)
4238 strip_excluded_output_sections ();
4240 /* We must record the program headers before we try to fix the
4241 section positions, since they will affect SIZEOF_HEADERS. */
4242 lang_record_phdrs ();
4244 /* Size up the sections. */
4245 lang_size_sections (statement_list
.head
, abs_output_section
,
4246 &statement_list
.head
, 0, 0, NULL
,
4247 command_line
.relax
? FALSE
: TRUE
);
4249 /* Now run around and relax if we can. */
4250 if (command_line
.relax
)
4252 /* Keep relaxing until bfd_relax_section gives up. */
4253 bfd_boolean relax_again
;
4257 relax_again
= FALSE
;
4259 /* Note: pe-dll.c does something like this also. If you find
4260 you need to change this code, you probably need to change
4261 pe-dll.c also. DJ */
4263 /* Do all the assignments with our current guesses as to
4265 lang_do_assignments (statement_list
.head
, abs_output_section
,
4268 /* We must do this after lang_do_assignments, because it uses
4270 lang_reset_memory_regions ();
4272 /* Perform another relax pass - this time we know where the
4273 globals are, so can make a better guess. */
4274 lang_size_sections (statement_list
.head
, abs_output_section
,
4275 &statement_list
.head
, 0, 0, &relax_again
, FALSE
);
4277 /* If the normal relax is done and the relax finalize pass
4278 is not performed yet, we perform another relax pass. */
4279 if (!relax_again
&& link_info
.need_relax_finalize
)
4281 link_info
.need_relax_finalize
= FALSE
;
4285 while (relax_again
);
4287 /* Final extra sizing to report errors. */
4288 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4289 lang_reset_memory_regions ();
4290 lang_size_sections (statement_list
.head
, abs_output_section
,
4291 &statement_list
.head
, 0, 0, NULL
, TRUE
);
4294 /* See if anything special should be done now we know how big
4296 ldemul_after_allocation ();
4298 /* Fix any .startof. or .sizeof. symbols. */
4299 lang_set_startof ();
4301 /* Do all the assignments, now that we know the final resting places
4302 of all the symbols. */
4304 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4306 /* Make sure that the section addresses make sense. */
4307 if (! link_info
.relocatable
4308 && command_line
.check_section_addresses
)
4309 lang_check_section_addresses ();
4317 /* EXPORTED TO YACC */
4320 lang_add_wild (struct wildcard_spec
*filespec
,
4321 struct wildcard_list
*section_list
,
4322 bfd_boolean keep_sections
)
4324 struct wildcard_list
*curr
, *next
;
4325 lang_wild_statement_type
*new;
4327 /* Reverse the list as the parser puts it back to front. */
4328 for (curr
= section_list
, section_list
= NULL
;
4330 section_list
= curr
, curr
= next
)
4332 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4333 placed_commons
= TRUE
;
4336 curr
->next
= section_list
;
4339 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4341 if (strcmp (filespec
->name
, "*") == 0)
4342 filespec
->name
= NULL
;
4343 else if (! wildcardp (filespec
->name
))
4344 lang_has_input_file
= TRUE
;
4347 new = new_stat (lang_wild_statement
, stat_ptr
);
4348 new->filename
= NULL
;
4349 new->filenames_sorted
= FALSE
;
4350 if (filespec
!= NULL
)
4352 new->filename
= filespec
->name
;
4353 new->filenames_sorted
= filespec
->sorted
;
4355 new->section_list
= section_list
;
4356 new->keep_sections
= keep_sections
;
4357 lang_list_init (&new->children
);
4361 lang_section_start (const char *name
, etree_type
*address
)
4363 lang_address_statement_type
*ad
;
4365 ad
= new_stat (lang_address_statement
, stat_ptr
);
4366 ad
->section_name
= name
;
4367 ad
->address
= address
;
4370 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4371 because of a -e argument on the command line, or zero if this is
4372 called by ENTRY in a linker script. Command line arguments take
4376 lang_add_entry (const char *name
, bfd_boolean cmdline
)
4378 if (entry_symbol
.name
== NULL
4380 || ! entry_from_cmdline
)
4382 entry_symbol
.name
= name
;
4383 entry_from_cmdline
= cmdline
;
4388 lang_add_target (const char *name
)
4390 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4398 lang_add_map (const char *name
)
4405 map_option_f
= TRUE
;
4413 lang_add_fill (fill_type
*fill
)
4415 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4422 lang_add_data (int type
, union etree_union
*exp
)
4425 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4433 /* Create a new reloc statement. RELOC is the BFD relocation type to
4434 generate. HOWTO is the corresponding howto structure (we could
4435 look this up, but the caller has already done so). SECTION is the
4436 section to generate a reloc against, or NAME is the name of the
4437 symbol to generate a reloc against. Exactly one of SECTION and
4438 NAME must be NULL. ADDEND is an expression for the addend. */
4441 lang_add_reloc (bfd_reloc_code_real_type reloc
,
4442 reloc_howto_type
*howto
,
4445 union etree_union
*addend
)
4447 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4451 p
->section
= section
;
4453 p
->addend_exp
= addend
;
4455 p
->addend_value
= 0;
4456 p
->output_section
= NULL
;
4460 lang_assignment_statement_type
*
4461 lang_add_assignment (etree_type
*exp
)
4463 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4471 lang_add_attribute (enum statement_enum attribute
)
4473 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4477 lang_startup (const char *name
)
4479 if (startup_file
!= NULL
)
4481 einfo (_("%P%Fmultiple STARTUP files\n"));
4483 first_file
->filename
= name
;
4484 first_file
->local_sym_name
= name
;
4485 first_file
->real
= TRUE
;
4487 startup_file
= name
;
4491 lang_float (bfd_boolean maybe
)
4493 lang_float_flag
= maybe
;
4497 /* Work out the load- and run-time regions from a script statement, and
4498 store them in *LMA_REGION and *REGION respectively.
4500 MEMSPEC is the name of the run-time region, or the value of
4501 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4502 LMA_MEMSPEC is the name of the load-time region, or null if the
4503 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4504 had an explicit load address.
4506 It is an error to specify both a load region and a load address. */
4509 lang_get_regions (lang_memory_region_type
**region
,
4510 lang_memory_region_type
**lma_region
,
4511 const char *memspec
,
4512 const char *lma_memspec
,
4513 bfd_boolean have_lma
,
4514 bfd_boolean have_vma
)
4516 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
4518 /* If no runtime region or VMA has been specified, but the load region has
4519 been specified, then use the load region for the runtime region as well. */
4520 if (lma_memspec
!= NULL
4522 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
4523 *region
= *lma_region
;
4525 *region
= lang_memory_region_lookup (memspec
, FALSE
);
4527 if (have_lma
&& lma_memspec
!= 0)
4528 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4532 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
4533 lang_output_section_phdr_list
*phdrs
,
4534 const char *lma_memspec
)
4536 lang_get_regions (¤t_section
->region
,
4537 ¤t_section
->lma_region
,
4538 memspec
, lma_memspec
,
4539 current_section
->load_base
!= NULL
,
4540 current_section
->addr_tree
!= NULL
);
4541 current_section
->fill
= fill
;
4542 current_section
->phdrs
= phdrs
;
4543 stat_ptr
= &statement_list
;
4546 /* Create an absolute symbol with the given name with the value of the
4547 address of first byte of the section named.
4549 If the symbol already exists, then do nothing. */
4552 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
4554 struct bfd_link_hash_entry
*h
;
4556 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4558 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4560 if (h
->type
== bfd_link_hash_new
4561 || h
->type
== bfd_link_hash_undefined
)
4565 h
->type
= bfd_link_hash_defined
;
4567 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4571 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4573 h
->u
.def
.section
= bfd_abs_section_ptr
;
4577 /* Create an absolute symbol with the given name with the value of the
4578 address of the first byte after the end of the section named.
4580 If the symbol already exists, then do nothing. */
4583 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
4585 struct bfd_link_hash_entry
*h
;
4587 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4589 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4591 if (h
->type
== bfd_link_hash_new
4592 || h
->type
== bfd_link_hash_undefined
)
4596 h
->type
= bfd_link_hash_defined
;
4598 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4602 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4603 + TO_ADDR (bfd_section_size (output_bfd
, sec
)));
4605 h
->u
.def
.section
= bfd_abs_section_ptr
;
4610 lang_statement_append (lang_statement_list_type
*list
,
4611 lang_statement_union_type
*element
,
4612 lang_statement_union_type
**field
)
4614 *(list
->tail
) = element
;
4618 /* Set the output format type. -oformat overrides scripts. */
4621 lang_add_output_format (const char *format
,
4626 if (output_target
== NULL
|| !from_script
)
4628 if (command_line
.endian
== ENDIAN_BIG
4631 else if (command_line
.endian
== ENDIAN_LITTLE
4635 output_target
= format
;
4639 /* Enter a group. This creates a new lang_group_statement, and sets
4640 stat_ptr to build new statements within the group. */
4643 lang_enter_group (void)
4645 lang_group_statement_type
*g
;
4647 g
= new_stat (lang_group_statement
, stat_ptr
);
4648 lang_list_init (&g
->children
);
4649 stat_ptr
= &g
->children
;
4652 /* Leave a group. This just resets stat_ptr to start writing to the
4653 regular list of statements again. Note that this will not work if
4654 groups can occur inside anything else which can adjust stat_ptr,
4655 but currently they can't. */
4658 lang_leave_group (void)
4660 stat_ptr
= &statement_list
;
4663 /* Add a new program header. This is called for each entry in a PHDRS
4664 command in a linker script. */
4667 lang_new_phdr (const char *name
,
4669 bfd_boolean filehdr
,
4674 struct lang_phdr
*n
, **pp
;
4676 n
= stat_alloc (sizeof (struct lang_phdr
));
4679 n
->type
= exp_get_value_int (type
, 0, "program header type",
4680 lang_final_phase_enum
);
4681 n
->filehdr
= filehdr
;
4686 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4691 /* Record the program header information in the output BFD. FIXME: We
4692 should not be calling an ELF specific function here. */
4695 lang_record_phdrs (void)
4699 lang_output_section_phdr_list
*last
;
4700 struct lang_phdr
*l
;
4701 lang_statement_union_type
*u
;
4704 secs
= xmalloc (alc
* sizeof (asection
*));
4706 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4713 for (u
= lang_output_section_statement
.head
;
4715 u
= u
->output_section_statement
.next
)
4717 lang_output_section_statement_type
*os
;
4718 lang_output_section_phdr_list
*pl
;
4720 os
= &u
->output_section_statement
;
4727 if (os
->sectype
== noload_section
4728 || os
->bfd_section
== NULL
4729 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4734 if (os
->bfd_section
== NULL
)
4737 for (; pl
!= NULL
; pl
= pl
->next
)
4739 if (strcmp (pl
->name
, l
->name
) == 0)
4744 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
4746 secs
[c
] = os
->bfd_section
;
4753 if (l
->flags
== NULL
)
4756 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4757 lang_final_phase_enum
);
4762 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4763 lang_final_phase_enum
);
4765 if (! bfd_record_phdr (output_bfd
, l
->type
,
4766 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4767 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4768 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4773 /* Make sure all the phdr assignments succeeded. */
4774 for (u
= lang_output_section_statement
.head
;
4776 u
= u
->output_section_statement
.next
)
4778 lang_output_section_phdr_list
*pl
;
4780 if (u
->output_section_statement
.bfd_section
== NULL
)
4783 for (pl
= u
->output_section_statement
.phdrs
;
4786 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4787 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4788 u
->output_section_statement
.name
, pl
->name
);
4792 /* Record a list of sections which may not be cross referenced. */
4795 lang_add_nocrossref (lang_nocrossref_type
*l
)
4797 struct lang_nocrossrefs
*n
;
4799 n
= xmalloc (sizeof *n
);
4800 n
->next
= nocrossref_list
;
4802 nocrossref_list
= n
;
4804 /* Set notice_all so that we get informed about all symbols. */
4805 link_info
.notice_all
= TRUE
;
4808 /* Overlay handling. We handle overlays with some static variables. */
4810 /* The overlay virtual address. */
4811 static etree_type
*overlay_vma
;
4812 /* And subsection alignment. */
4813 static etree_type
*overlay_subalign
;
4815 /* An expression for the maximum section size seen so far. */
4816 static etree_type
*overlay_max
;
4818 /* A list of all the sections in this overlay. */
4820 struct overlay_list
{
4821 struct overlay_list
*next
;
4822 lang_output_section_statement_type
*os
;
4825 static struct overlay_list
*overlay_list
;
4827 /* Start handling an overlay. */
4830 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
4832 /* The grammar should prevent nested overlays from occurring. */
4833 ASSERT (overlay_vma
== NULL
4834 && overlay_subalign
== NULL
4835 && overlay_max
== NULL
);
4837 overlay_vma
= vma_expr
;
4838 overlay_subalign
= subalign
;
4841 /* Start a section in an overlay. We handle this by calling
4842 lang_enter_output_section_statement with the correct VMA.
4843 lang_leave_overlay sets up the LMA and memory regions. */
4846 lang_enter_overlay_section (const char *name
)
4848 struct overlay_list
*n
;
4851 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4852 0, overlay_subalign
, 0);
4854 /* If this is the first section, then base the VMA of future
4855 sections on this one. This will work correctly even if `.' is
4856 used in the addresses. */
4857 if (overlay_list
== NULL
)
4858 overlay_vma
= exp_nameop (ADDR
, name
);
4860 /* Remember the section. */
4861 n
= xmalloc (sizeof *n
);
4862 n
->os
= current_section
;
4863 n
->next
= overlay_list
;
4866 size
= exp_nameop (SIZEOF
, name
);
4868 /* Arrange to work out the maximum section end address. */
4869 if (overlay_max
== NULL
)
4872 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4875 /* Finish a section in an overlay. There isn't any special to do
4879 lang_leave_overlay_section (fill_type
*fill
,
4880 lang_output_section_phdr_list
*phdrs
)
4887 name
= current_section
->name
;
4889 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4890 region and that no load-time region has been specified. It doesn't
4891 really matter what we say here, since lang_leave_overlay will
4893 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
4895 /* Define the magic symbols. */
4897 clean
= xmalloc (strlen (name
) + 1);
4899 for (s1
= name
; *s1
!= '\0'; s1
++)
4900 if (ISALNUM (*s1
) || *s1
== '_')
4904 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4905 sprintf (buf
, "__load_start_%s", clean
);
4906 lang_add_assignment (exp_assop ('=', buf
,
4907 exp_nameop (LOADADDR
, name
)));
4909 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4910 sprintf (buf
, "__load_stop_%s", clean
);
4911 lang_add_assignment (exp_assop ('=', buf
,
4913 exp_nameop (LOADADDR
, name
),
4914 exp_nameop (SIZEOF
, name
))));
4919 /* Finish an overlay. If there are any overlay wide settings, this
4920 looks through all the sections in the overlay and sets them. */
4923 lang_leave_overlay (etree_type
*lma_expr
,
4926 const char *memspec
,
4927 lang_output_section_phdr_list
*phdrs
,
4928 const char *lma_memspec
)
4930 lang_memory_region_type
*region
;
4931 lang_memory_region_type
*lma_region
;
4932 struct overlay_list
*l
;
4933 lang_nocrossref_type
*nocrossref
;
4935 lang_get_regions (®ion
, &lma_region
,
4936 memspec
, lma_memspec
,
4937 lma_expr
!= NULL
, FALSE
);
4941 /* After setting the size of the last section, set '.' to end of the
4943 if (overlay_list
!= NULL
)
4944 overlay_list
->os
->update_dot_tree
4945 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
4950 struct overlay_list
*next
;
4952 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
4955 l
->os
->region
= region
;
4956 l
->os
->lma_region
= lma_region
;
4958 /* The first section has the load address specified in the
4959 OVERLAY statement. The rest are worked out from that.
4960 The base address is not needed (and should be null) if
4961 an LMA region was specified. */
4963 l
->os
->load_base
= lma_expr
;
4964 else if (lma_region
== 0)
4965 l
->os
->load_base
= exp_binop ('+',
4966 exp_nameop (LOADADDR
, l
->next
->os
->name
),
4967 exp_nameop (SIZEOF
, l
->next
->os
->name
));
4969 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4970 l
->os
->phdrs
= phdrs
;
4974 lang_nocrossref_type
*nc
;
4976 nc
= xmalloc (sizeof *nc
);
4977 nc
->name
= l
->os
->name
;
4978 nc
->next
= nocrossref
;
4987 if (nocrossref
!= NULL
)
4988 lang_add_nocrossref (nocrossref
);
4991 overlay_list
= NULL
;
4995 /* Version handling. This is only useful for ELF. */
4997 /* This global variable holds the version tree that we build. */
4999 struct bfd_elf_version_tree
*lang_elf_version_info
;
5001 /* If PREV is NULL, return first version pattern matching particular symbol.
5002 If PREV is non-NULL, return first version pattern matching particular
5003 symbol after PREV (previously returned by lang_vers_match). */
5005 static struct bfd_elf_version_expr
*
5006 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
5007 struct bfd_elf_version_expr
*prev
,
5010 const char *cxx_sym
= sym
;
5011 const char *java_sym
= sym
;
5012 struct bfd_elf_version_expr
*expr
= NULL
;
5014 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5016 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
5020 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5022 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5027 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
5029 struct bfd_elf_version_expr e
;
5031 switch (prev
? prev
->mask
: 0)
5034 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
5037 expr
= htab_find (head
->htab
, &e
);
5038 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
5039 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
5045 case BFD_ELF_VERSION_C_TYPE
:
5046 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5049 expr
= htab_find (head
->htab
, &e
);
5050 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
5051 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5057 case BFD_ELF_VERSION_CXX_TYPE
:
5058 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5060 e
.symbol
= java_sym
;
5061 expr
= htab_find (head
->htab
, &e
);
5062 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
5063 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5074 /* Finally, try the wildcards. */
5075 if (prev
== NULL
|| prev
->symbol
)
5076 expr
= head
->remaining
;
5083 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5086 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5088 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5092 if (fnmatch (expr
->pattern
, s
, 0) == 0)
5099 free ((char *) cxx_sym
);
5100 if (java_sym
!= sym
)
5101 free ((char *) java_sym
);
5105 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5106 return a string pointing to the symbol name. */
5109 realsymbol (const char *pattern
)
5112 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
5113 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
5115 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
5117 /* It is a glob pattern only if there is no preceding
5119 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
5127 /* Remove the preceding backslash. */
5134 backslash
= *p
== '\\';
5149 /* This is called for each variable name or match expression. */
5151 struct bfd_elf_version_expr
*
5152 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
5156 struct bfd_elf_version_expr
*ret
;
5158 ret
= xmalloc (sizeof *ret
);
5163 ret
->symbol
= realsymbol (new);
5165 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5166 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5167 else if (strcasecmp (lang
, "C++") == 0)
5168 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
5169 else if (strcasecmp (lang
, "Java") == 0)
5170 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
5173 einfo (_("%X%P: unknown language `%s' in version information\n"),
5175 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5178 return ldemul_new_vers_pattern (ret
);
5181 /* This is called for each set of variable names and match
5184 struct bfd_elf_version_tree
*
5185 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
5186 struct bfd_elf_version_expr
*locals
)
5188 struct bfd_elf_version_tree
*ret
;
5190 ret
= xcalloc (1, sizeof *ret
);
5191 ret
->globals
.list
= globals
;
5192 ret
->locals
.list
= locals
;
5193 ret
->match
= lang_vers_match
;
5194 ret
->name_indx
= (unsigned int) -1;
5198 /* This static variable keeps track of version indices. */
5200 static int version_index
;
5203 version_expr_head_hash (const void *p
)
5205 const struct bfd_elf_version_expr
*e
= p
;
5207 return htab_hash_string (e
->symbol
);
5211 version_expr_head_eq (const void *p1
, const void *p2
)
5213 const struct bfd_elf_version_expr
*e1
= p1
;
5214 const struct bfd_elf_version_expr
*e2
= p2
;
5216 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
5220 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
5223 struct bfd_elf_version_expr
*e
, *next
;
5224 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
5226 for (e
= head
->list
; e
; e
= e
->next
)
5230 head
->mask
|= e
->mask
;
5235 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
5236 version_expr_head_eq
, NULL
);
5237 list_loc
= &head
->list
;
5238 remaining_loc
= &head
->remaining
;
5239 for (e
= head
->list
; e
; e
= next
)
5245 remaining_loc
= &e
->next
;
5249 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
5253 struct bfd_elf_version_expr
*e1
, *last
;
5259 if (e1
->mask
== e
->mask
)
5267 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
5271 /* This is a duplicate. */
5272 /* FIXME: Memory leak. Sometimes pattern is not
5273 xmalloced alone, but in larger chunk of memory. */
5274 /* free (e->symbol); */
5279 e
->next
= last
->next
;
5287 list_loc
= &e
->next
;
5291 *remaining_loc
= NULL
;
5292 *list_loc
= head
->remaining
;
5295 head
->remaining
= head
->list
;
5298 /* This is called when we know the name and dependencies of the
5302 lang_register_vers_node (const char *name
,
5303 struct bfd_elf_version_tree
*version
,
5304 struct bfd_elf_version_deps
*deps
)
5306 struct bfd_elf_version_tree
*t
, **pp
;
5307 struct bfd_elf_version_expr
*e1
;
5312 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5313 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5315 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5320 /* Make sure this node has a unique name. */
5321 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5322 if (strcmp (t
->name
, name
) == 0)
5323 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5325 lang_finalize_version_expr_head (&version
->globals
);
5326 lang_finalize_version_expr_head (&version
->locals
);
5328 /* Check the global and local match names, and make sure there
5329 aren't any duplicates. */
5331 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
5333 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5335 struct bfd_elf_version_expr
*e2
;
5337 if (t
->locals
.htab
&& e1
->symbol
)
5339 e2
= htab_find (t
->locals
.htab
, e1
);
5340 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5342 if (e1
->mask
== e2
->mask
)
5343 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5348 else if (!e1
->symbol
)
5349 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5350 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5351 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5356 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
5358 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5360 struct bfd_elf_version_expr
*e2
;
5362 if (t
->globals
.htab
&& e1
->symbol
)
5364 e2
= htab_find (t
->globals
.htab
, e1
);
5365 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5367 if (e1
->mask
== e2
->mask
)
5368 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5373 else if (!e1
->symbol
)
5374 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5375 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5376 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5381 version
->deps
= deps
;
5382 version
->name
= name
;
5383 if (name
[0] != '\0')
5386 version
->vernum
= version_index
;
5389 version
->vernum
= 0;
5391 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5396 /* This is called when we see a version dependency. */
5398 struct bfd_elf_version_deps
*
5399 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
5401 struct bfd_elf_version_deps
*ret
;
5402 struct bfd_elf_version_tree
*t
;
5404 ret
= xmalloc (sizeof *ret
);
5407 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5409 if (strcmp (t
->name
, name
) == 0)
5411 ret
->version_needed
= t
;
5416 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5422 lang_do_version_exports_section (void)
5424 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5426 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5428 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5435 len
= bfd_section_size (is
->the_bfd
, sec
);
5436 contents
= xmalloc (len
);
5437 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5438 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5441 while (p
< contents
+ len
)
5443 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5444 p
= strchr (p
, '\0') + 1;
5447 /* Do not free the contents, as we used them creating the regex. */
5449 /* Do not include this section in the link. */
5450 bfd_set_section_flags (is
->the_bfd
, sec
,
5451 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5454 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5455 lang_register_vers_node (command_line
.version_exports_section
,
5456 lang_new_vers_node (greg
, lreg
), NULL
);
5460 lang_add_unique (const char *name
)
5462 struct unique_sections
*ent
;
5464 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5465 if (strcmp (ent
->name
, name
) == 0)
5468 ent
= xmalloc (sizeof *ent
);
5469 ent
->name
= xstrdup (name
);
5470 ent
->next
= unique_section_list
;
5471 unique_section_list
= ent
;