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 asection
*sec
)
126 struct unique_sections
*unam
;
129 if (link_info
.relocatable
130 && sec
->owner
!= NULL
131 && bfd_is_group_section (sec
->owner
, sec
))
135 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
136 if (wildcardp (unam
->name
)
137 ? fnmatch (unam
->name
, secnam
, 0) == 0
138 : strcmp (unam
->name
, secnam
) == 0)
146 /* Generic traversal routines for finding matching sections. */
149 walk_wild_section (lang_wild_statement_type
*ptr
,
150 lang_input_statement_type
*file
,
156 if (file
->just_syms_flag
)
159 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
161 struct wildcard_list
*sec
;
163 sec
= ptr
->section_list
;
165 (*callback
) (ptr
, sec
, s
, file
, data
);
169 bfd_boolean skip
= FALSE
;
170 struct name_list
*list_tmp
;
172 /* Don't process sections from files which were
174 for (list_tmp
= sec
->spec
.exclude_name_list
;
176 list_tmp
= list_tmp
->next
)
178 if (wildcardp (list_tmp
->name
))
179 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
181 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
183 /* If this file is part of an archive, and the archive is
184 excluded, exclude this file. */
185 if (! skip
&& file
->the_bfd
!= NULL
186 && file
->the_bfd
->my_archive
!= NULL
187 && file
->the_bfd
->my_archive
->filename
!= NULL
)
189 if (wildcardp (list_tmp
->name
))
190 skip
= fnmatch (list_tmp
->name
,
191 file
->the_bfd
->my_archive
->filename
,
194 skip
= strcmp (list_tmp
->name
,
195 file
->the_bfd
->my_archive
->filename
) == 0;
202 if (!skip
&& sec
->spec
.name
!= NULL
)
204 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
206 if (wildcardp (sec
->spec
.name
))
207 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
209 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
213 (*callback
) (ptr
, sec
, s
, file
, data
);
220 /* Handle a wild statement for a single file F. */
223 walk_wild_file (lang_wild_statement_type
*s
,
224 lang_input_statement_type
*f
,
228 if (f
->the_bfd
== NULL
229 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
230 walk_wild_section (s
, f
, callback
, data
);
235 /* This is an archive file. We must map each member of the
236 archive separately. */
237 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
238 while (member
!= NULL
)
240 /* When lookup_name is called, it will call the add_symbols
241 entry point for the archive. For each element of the
242 archive which is included, BFD will call ldlang_add_file,
243 which will set the usrdata field of the member to the
244 lang_input_statement. */
245 if (member
->usrdata
!= NULL
)
247 walk_wild_section (s
, member
->usrdata
, callback
, data
);
250 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
256 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
258 const char *file_spec
= s
->filename
;
260 if (file_spec
== NULL
)
262 /* Perform the iteration over all files in the list. */
263 LANG_FOR_EACH_INPUT_STATEMENT (f
)
265 walk_wild_file (s
, f
, callback
, data
);
268 else if (wildcardp (file_spec
))
270 LANG_FOR_EACH_INPUT_STATEMENT (f
)
272 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
273 walk_wild_file (s
, f
, callback
, data
);
278 lang_input_statement_type
*f
;
280 /* Perform the iteration over a single file. */
281 f
= lookup_name (file_spec
);
283 walk_wild_file (s
, f
, callback
, data
);
287 /* lang_for_each_statement walks the parse tree and calls the provided
288 function for each node. */
291 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
292 lang_statement_union_type
*s
)
294 for (; s
!= NULL
; s
= s
->header
.next
)
298 switch (s
->header
.type
)
300 case lang_constructors_statement_enum
:
301 lang_for_each_statement_worker (func
, constructor_list
.head
);
303 case lang_output_section_statement_enum
:
304 lang_for_each_statement_worker
306 s
->output_section_statement
.children
.head
);
308 case lang_wild_statement_enum
:
309 lang_for_each_statement_worker
311 s
->wild_statement
.children
.head
);
313 case lang_group_statement_enum
:
314 lang_for_each_statement_worker (func
,
315 s
->group_statement
.children
.head
);
317 case lang_data_statement_enum
:
318 case lang_reloc_statement_enum
:
319 case lang_object_symbols_statement_enum
:
320 case lang_output_statement_enum
:
321 case lang_target_statement_enum
:
322 case lang_input_section_enum
:
323 case lang_input_statement_enum
:
324 case lang_assignment_statement_enum
:
325 case lang_padding_statement_enum
:
326 case lang_address_statement_enum
:
327 case lang_fill_statement_enum
:
337 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
339 lang_for_each_statement_worker (func
, statement_list
.head
);
342 /*----------------------------------------------------------------------*/
345 lang_list_init (lang_statement_list_type
*list
)
348 list
->tail
= &list
->head
;
351 /* Build a new statement node for the parse tree. */
353 static lang_statement_union_type
*
354 new_statement (enum statement_enum type
,
356 lang_statement_list_type
*list
)
358 lang_statement_union_type
*new;
360 new = stat_alloc (size
);
361 new->header
.type
= type
;
362 new->header
.next
= NULL
;
363 lang_statement_append (list
, new, &new->header
.next
);
367 /* Build a new input file node for the language. There are several
368 ways in which we treat an input file, eg, we only look at symbols,
369 or prefix it with a -l etc.
371 We can be supplied with requests for input files more than once;
372 they may, for example be split over several lines like foo.o(.text)
373 foo.o(.data) etc, so when asked for a file we check that we haven't
374 got it already so we don't duplicate the bfd. */
376 static lang_input_statement_type
*
377 new_afile (const char *name
,
378 lang_input_file_enum_type file_type
,
380 bfd_boolean add_to_list
)
382 lang_input_statement_type
*p
;
385 p
= new_stat (lang_input_statement
, stat_ptr
);
388 p
= stat_alloc (sizeof (lang_input_statement_type
));
389 p
->header
.next
= NULL
;
392 lang_has_input_file
= TRUE
;
394 p
->sysrooted
= FALSE
;
397 case lang_input_file_is_symbols_only_enum
:
399 p
->is_archive
= FALSE
;
401 p
->local_sym_name
= name
;
402 p
->just_syms_flag
= TRUE
;
403 p
->search_dirs_flag
= FALSE
;
405 case lang_input_file_is_fake_enum
:
407 p
->is_archive
= FALSE
;
409 p
->local_sym_name
= name
;
410 p
->just_syms_flag
= FALSE
;
411 p
->search_dirs_flag
= FALSE
;
413 case lang_input_file_is_l_enum
:
414 p
->is_archive
= TRUE
;
417 p
->local_sym_name
= concat ("-l", name
, NULL
);
418 p
->just_syms_flag
= FALSE
;
419 p
->search_dirs_flag
= TRUE
;
421 case lang_input_file_is_marker_enum
:
423 p
->is_archive
= FALSE
;
425 p
->local_sym_name
= name
;
426 p
->just_syms_flag
= FALSE
;
427 p
->search_dirs_flag
= TRUE
;
429 case lang_input_file_is_search_file_enum
:
430 p
->sysrooted
= ldlang_sysrooted_script
;
432 p
->is_archive
= FALSE
;
434 p
->local_sym_name
= name
;
435 p
->just_syms_flag
= FALSE
;
436 p
->search_dirs_flag
= TRUE
;
438 case lang_input_file_is_file_enum
:
440 p
->is_archive
= FALSE
;
442 p
->local_sym_name
= name
;
443 p
->just_syms_flag
= FALSE
;
444 p
->search_dirs_flag
= FALSE
;
451 p
->next_real_file
= NULL
;
454 p
->dynamic
= config
.dynamic_link
;
455 p
->as_needed
= as_needed
;
456 p
->whole_archive
= whole_archive
;
458 lang_statement_append (&input_file_chain
,
459 (lang_statement_union_type
*) p
,
464 lang_input_statement_type
*
465 lang_add_input_file (const char *name
,
466 lang_input_file_enum_type file_type
,
469 lang_has_input_file
= TRUE
;
470 return new_afile (name
, file_type
, target
, TRUE
);
473 /* Build enough state so that the parser can build its tree. */
478 obstack_begin (&stat_obstack
, 1000);
480 stat_ptr
= &statement_list
;
482 lang_list_init (stat_ptr
);
484 lang_list_init (&input_file_chain
);
485 lang_list_init (&lang_output_section_statement
);
486 lang_list_init (&file_chain
);
487 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
490 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
492 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
494 /* The value "3" is ad-hoc, somewhat related to the expected number of
495 DEFINED expressions in a linker script. For most default linker
496 scripts, there are none. Why a hash table then? Well, it's somewhat
497 simpler to re-use working machinery than using a linked list in terms
498 of code-complexity here in ld, besides the initialization which just
499 looks like other code here. */
500 if (bfd_hash_table_init_n (&lang_definedness_table
,
501 lang_definedness_newfunc
, 3) != TRUE
)
502 einfo (_("%P%F: out of memory during initialization"));
504 /* Callers of exp_fold_tree need to increment this. */
505 lang_statement_iteration
= 0;
508 /*----------------------------------------------------------------------
509 A region is an area of memory declared with the
510 MEMORY { name:org=exp, len=exp ... }
513 We maintain a list of all the regions here.
515 If no regions are specified in the script, then the default is used
516 which is created when looked up to be the entire data space.
518 If create is true we are creating a region inside a MEMORY block.
519 In this case it is probably an error to create a region that has
520 already been created. If we are not inside a MEMORY block it is
521 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
522 and so we issue a warning. */
524 static lang_memory_region_type
*lang_memory_region_list
;
525 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
527 lang_memory_region_type
*
528 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
530 lang_memory_region_type
*p
;
531 lang_memory_region_type
*new;
533 /* NAME is NULL for LMA memspecs if no region was specified. */
537 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
538 if (strcmp (p
->name
, name
) == 0)
541 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name
);
546 /* This code used to always use the first region in the list as the
547 default region. I changed it to instead use a region
548 encompassing all of memory as the default region. This permits
549 NOLOAD sections to work reasonably without requiring a region.
550 People should specify what region they mean, if they really want
552 if (strcmp (name
, DEFAULT_MEMORY_REGION
) == 0)
554 if (lang_memory_region_list
!= NULL
)
555 return lang_memory_region_list
;
559 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
560 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
562 new = stat_alloc (sizeof (lang_memory_region_type
));
564 new->name
= xstrdup (name
);
567 *lang_memory_region_list_tail
= new;
568 lang_memory_region_list_tail
= &new->next
;
572 new->length
= ~(bfd_size_type
) 0;
574 new->had_full_message
= FALSE
;
579 static lang_memory_region_type
*
580 lang_memory_default (asection
*section
)
582 lang_memory_region_type
*p
;
584 flagword sec_flags
= section
->flags
;
586 /* Override SEC_DATA to mean a writable section. */
587 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
588 sec_flags
|= SEC_DATA
;
590 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
592 if ((p
->flags
& sec_flags
) != 0
593 && (p
->not_flags
& sec_flags
) == 0)
598 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
601 lang_output_section_statement_type
*
602 lang_output_section_find (const char *const name
)
604 lang_statement_union_type
*u
;
605 lang_output_section_statement_type
*lookup
;
607 for (u
= lang_output_section_statement
.head
; u
!= NULL
; u
= lookup
->next
)
609 lookup
= &u
->output_section_statement
;
610 if (strcmp (name
, lookup
->name
) == 0)
616 lang_output_section_statement_type
*
617 lang_output_section_statement_lookup (const char *const name
)
619 lang_output_section_statement_type
*lookup
;
621 lookup
= lang_output_section_find (name
);
624 lookup
= new_stat (lang_output_section_statement
, stat_ptr
);
625 lookup
->region
= NULL
;
626 lookup
->lma_region
= NULL
;
628 lookup
->block_value
= 1;
632 lookup
->bfd_section
= NULL
;
633 lookup
->processed
= 0;
634 lookup
->sectype
= normal_section
;
635 lookup
->addr_tree
= NULL
;
636 lang_list_init (&lookup
->children
);
638 lookup
->memspec
= NULL
;
640 lookup
->subsection_alignment
= -1;
641 lookup
->section_alignment
= -1;
642 lookup
->load_base
= NULL
;
643 lookup
->update_dot_tree
= NULL
;
644 lookup
->phdrs
= NULL
;
646 lang_statement_append (&lang_output_section_statement
,
647 (lang_statement_union_type
*) lookup
,
654 lang_map_flags (flagword flag
)
656 if (flag
& SEC_ALLOC
)
662 if (flag
& SEC_READONLY
)
675 lang_memory_region_type
*m
;
677 minfo (_("\nMemory Configuration\n\n"));
678 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
679 _("Name"), _("Origin"), _("Length"), _("Attributes"));
681 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
686 fprintf (config
.map_file
, "%-16s ", m
->name
);
688 sprintf_vma (buf
, m
->origin
);
689 minfo ("0x%s ", buf
);
697 minfo ("0x%V", m
->length
);
698 if (m
->flags
|| m
->not_flags
)
706 lang_map_flags (m
->flags
);
712 lang_map_flags (m
->not_flags
);
719 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
724 /* Initialize an output section. */
727 init_os (lang_output_section_statement_type
*s
)
729 section_userdata_type
*new;
731 if (s
->bfd_section
!= NULL
)
734 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
735 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
737 new = stat_alloc (sizeof (section_userdata_type
));
739 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
740 if (s
->bfd_section
== NULL
)
741 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
742 if (s
->bfd_section
== NULL
)
744 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
745 output_bfd
->xvec
->name
, s
->name
);
747 s
->bfd_section
->output_section
= s
->bfd_section
;
749 /* We initialize an output sections output offset to minus its own
750 vma to allow us to output a section through itself. */
751 s
->bfd_section
->output_offset
= 0;
752 get_userdata (s
->bfd_section
) = new;
754 /* If there is a base address, make sure that any sections it might
755 mention are initialized. */
756 if (s
->addr_tree
!= NULL
)
757 exp_init_os (s
->addr_tree
);
759 if (s
->load_base
!= NULL
)
760 exp_init_os (s
->load_base
);
763 /* Make sure that all output sections mentioned in an expression are
767 exp_init_os (etree_type
*exp
)
769 switch (exp
->type
.node_class
)
772 exp_init_os (exp
->assign
.src
);
776 exp_init_os (exp
->binary
.lhs
);
777 exp_init_os (exp
->binary
.rhs
);
781 exp_init_os (exp
->trinary
.cond
);
782 exp_init_os (exp
->trinary
.lhs
);
783 exp_init_os (exp
->trinary
.rhs
);
787 exp_init_os (exp
->assert_s
.child
);
791 exp_init_os (exp
->unary
.child
);
795 switch (exp
->type
.node_code
)
801 lang_output_section_statement_type
*os
;
803 os
= lang_output_section_find (exp
->name
.name
);
804 if (os
!= NULL
&& os
->bfd_section
== NULL
)
815 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
816 once into the output. This routine checks each section, and
817 arrange to discard it if a section of the same name has already
818 been linked. If the section has COMDAT information, then it uses
819 that to decide whether the section should be included. This code
820 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
821 that is, it does not depend solely upon the section name.
822 section_already_linked is called via bfd_map_over_sections. */
824 /* This is the shape of the elements inside the already_linked hash
825 table. It maps a name onto a list of already_linked elements with
826 the same name. It's possible to get more than one element in a
827 list if the COMDAT sections have different names. */
829 struct already_linked_hash_entry
831 struct bfd_hash_entry root
;
832 struct already_linked
*entry
;
835 struct already_linked
837 struct already_linked
*next
;
841 /* The hash table. */
843 static struct bfd_hash_table already_linked_table
;
846 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
848 lang_input_statement_type
*entry
= data
;
851 struct already_linked
*l
;
852 struct already_linked_hash_entry
*already_linked_list
;
854 /* If we are only reading symbols from this object, then we want to
855 discard all sections. */
856 if (entry
->just_syms_flag
)
858 bfd_link_just_syms (sec
, &link_info
);
862 flags
= bfd_get_section_flags (abfd
, sec
);
864 if ((flags
& SEC_LINK_ONCE
) == 0)
867 /* FIXME: When doing a relocatable link, we may have trouble
868 copying relocations in other sections that refer to local symbols
869 in the section being discarded. Those relocations will have to
870 be converted somehow; as of this writing I'm not sure that any of
871 the backends handle that correctly.
873 It is tempting to instead not discard link once sections when
874 doing a relocatable link (technically, they should be discarded
875 whenever we are building constructors). However, that fails,
876 because the linker winds up combining all the link once sections
877 into a single large link once section, which defeats the purpose
878 of having link once sections in the first place.
880 Also, not merging link once sections in a relocatable link
881 causes trouble for MIPS ELF, which relies on link once semantics
882 to handle the .reginfo section correctly. */
884 name
= bfd_get_section_name (abfd
, sec
);
886 already_linked_list
=
887 ((struct already_linked_hash_entry
*)
888 bfd_hash_lookup (&already_linked_table
, name
, TRUE
, FALSE
));
890 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
892 if (sec
->comdat
== NULL
893 || l
->sec
->comdat
== NULL
894 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
896 /* The section has already been linked. See if we should
898 switch (flags
& SEC_LINK_DUPLICATES
)
903 case SEC_LINK_DUPLICATES_DISCARD
:
906 case SEC_LINK_DUPLICATES_ONE_ONLY
:
907 if (sec
->comdat
== NULL
)
908 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
911 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
912 abfd
, name
, sec
->comdat
->name
);
915 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
916 /* FIXME: We should really dig out the contents of both
917 sections and memcmp them. The COFF/PE spec says that
918 the Microsoft linker does not implement this
919 correctly, so I'm not going to bother doing it
922 case SEC_LINK_DUPLICATES_SAME_SIZE
:
923 if (bfd_section_size (abfd
, sec
)
924 != bfd_section_size (l
->sec
->owner
, l
->sec
))
925 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
930 /* Set the output_section field so that lang_add_section
931 does not create a lang_input_section structure for this
932 section. Since there might be a symbol in the section
933 being discarded, we must retain a pointer to the section
934 which we are really going to use. */
935 sec
->output_section
= bfd_abs_section_ptr
;
936 sec
->kept_section
= l
->sec
;
938 if (flags
& SEC_GROUP
)
939 bfd_discard_group (abfd
, sec
);
945 /* This is the first section with this name. Record it. Allocate
946 the memory from the same obstack as the hash table is kept in. */
948 l
= bfd_hash_allocate (&already_linked_table
, sizeof *l
);
951 l
->next
= already_linked_list
->entry
;
952 already_linked_list
->entry
= l
;
955 /* Support routines for the hash table used by section_already_linked,
956 initialize the table, fill in an entry and remove the table. */
958 static struct bfd_hash_entry
*
959 already_linked_newfunc (struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
,
960 struct bfd_hash_table
*table
,
961 const char *string ATTRIBUTE_UNUSED
)
963 struct already_linked_hash_entry
*ret
=
964 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
972 already_linked_table_init (void)
974 if (! bfd_hash_table_init_n (&already_linked_table
,
975 already_linked_newfunc
,
977 einfo (_("%P%F: Failed to create hash table\n"));
981 already_linked_table_free (void)
983 bfd_hash_table_free (&already_linked_table
);
986 /* The wild routines.
988 These expand statements like *(.text) and foo.o to a list of
989 explicit actions, like foo.o(.text), bar.o(.text) and
990 foo.o(.text, .data). */
992 /* Return TRUE if the PATTERN argument is a wildcard pattern.
993 Although backslashes are treated specially if a pattern contains
994 wildcards, we do not consider the mere presence of a backslash to
995 be enough to cause the pattern to be treated as a wildcard.
996 That lets us handle DOS filenames more naturally. */
999 wildcardp (const char *pattern
)
1003 for (s
= pattern
; *s
!= '\0'; ++s
)
1011 /* Add SECTION to the output section OUTPUT. Do this by creating a
1012 lang_input_section statement which is placed at PTR. FILE is the
1013 input file which holds SECTION. */
1016 lang_add_section (lang_statement_list_type
*ptr
,
1018 lang_output_section_statement_type
*output
,
1019 lang_input_statement_type
*file
)
1022 bfd_boolean discard
;
1024 flags
= bfd_get_section_flags (section
->owner
, section
);
1028 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1029 link. Discard debugging sections marked with SEC_EXCLUDE on a
1030 relocatable link too. */
1031 if ((flags
& SEC_EXCLUDE
) != 0
1032 && ((flags
& SEC_DEBUGGING
) != 0 || !link_info
.relocatable
))
1035 /* Discard input sections which are assigned to a section named
1036 DISCARD_SECTION_NAME. */
1037 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1040 /* Discard debugging sections if we are stripping debugging
1042 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1043 && (flags
& SEC_DEBUGGING
) != 0)
1048 if (section
->output_section
== NULL
)
1050 /* This prevents future calls from assigning this section. */
1051 section
->output_section
= bfd_abs_section_ptr
;
1056 if (section
->output_section
== NULL
)
1059 lang_input_section_type
*new;
1062 if (output
->bfd_section
== NULL
)
1065 first
= ! output
->bfd_section
->linker_has_input
;
1066 output
->bfd_section
->linker_has_input
= 1;
1068 /* Add a section reference to the list. */
1069 new = new_stat (lang_input_section
, ptr
);
1071 new->section
= section
;
1073 section
->output_section
= output
->bfd_section
;
1075 flags
= section
->flags
;
1077 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1078 to an output section, because we want to be able to include a
1079 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1080 section (I don't know why we want to do this, but we do).
1081 build_link_order in ldwrite.c handles this case by turning
1082 the embedded SEC_NEVER_LOAD section into a fill. */
1084 flags
&= ~ SEC_NEVER_LOAD
;
1086 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1087 already been processed. One reason to do this is that on pe
1088 format targets, .text$foo sections go into .text and it's odd
1089 to see .text with SEC_LINK_ONCE set. */
1091 if (! link_info
.relocatable
)
1092 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1094 /* If this is not the first input section, and the SEC_READONLY
1095 flag is not currently set, then don't set it just because the
1096 input section has it set. */
1098 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1099 flags
&= ~ SEC_READONLY
;
1101 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1103 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1104 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1105 || ((flags
& SEC_MERGE
)
1106 && section
->output_section
->entsize
!= section
->entsize
)))
1108 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1109 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1112 section
->output_section
->flags
|= flags
;
1114 if (flags
& SEC_MERGE
)
1115 section
->output_section
->entsize
= section
->entsize
;
1117 /* If SEC_READONLY is not set in the input section, then clear
1118 it from the output section. */
1119 if ((section
->flags
& SEC_READONLY
) == 0)
1120 section
->output_section
->flags
&= ~SEC_READONLY
;
1122 switch (output
->sectype
)
1124 case normal_section
:
1129 case overlay_section
:
1130 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1132 case noload_section
:
1133 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1134 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1138 /* Copy over SEC_SMALL_DATA. */
1139 if (section
->flags
& SEC_SMALL_DATA
)
1140 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1142 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1143 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1145 /* If supplied an alignment, then force it. */
1146 if (output
->section_alignment
!= -1)
1147 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1149 if (section
->flags
& SEC_BLOCK
)
1151 section
->output_section
->flags
|= SEC_BLOCK
;
1152 /* FIXME: This value should really be obtained from the bfd... */
1153 output
->block_value
= 128;
1158 /* Handle wildcard sorting. This returns the lang_input_section which
1159 should follow the one we are going to create for SECTION and FILE,
1160 based on the sorting requirements of WILD. It returns NULL if the
1161 new section should just go at the end of the current list. */
1163 static lang_statement_union_type
*
1164 wild_sort (lang_wild_statement_type
*wild
,
1165 struct wildcard_list
*sec
,
1166 lang_input_statement_type
*file
,
1169 const char *section_name
;
1170 lang_statement_union_type
*l
;
1172 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1175 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1176 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1178 lang_input_section_type
*ls
;
1180 if (l
->header
.type
!= lang_input_section_enum
)
1182 ls
= &l
->input_section
;
1184 /* Sorting by filename takes precedence over sorting by section
1187 if (wild
->filenames_sorted
)
1189 const char *fn
, *ln
;
1193 /* The PE support for the .idata section as generated by
1194 dlltool assumes that files will be sorted by the name of
1195 the archive and then the name of the file within the
1198 if (file
->the_bfd
!= NULL
1199 && bfd_my_archive (file
->the_bfd
) != NULL
)
1201 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1206 fn
= file
->filename
;
1210 if (ls
->ifile
->the_bfd
!= NULL
1211 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1213 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1218 ln
= ls
->ifile
->filename
;
1222 i
= strcmp (fn
, ln
);
1231 fn
= file
->filename
;
1233 ln
= ls
->ifile
->filename
;
1235 i
= strcmp (fn
, ln
);
1243 /* Here either the files are not sorted by name, or we are
1244 looking at the sections for this file. */
1246 if (sec
!= NULL
&& sec
->spec
.sorted
)
1248 if (strcmp (section_name
,
1249 bfd_get_section_name (ls
->ifile
->the_bfd
,
1259 /* Expand a wild statement for a particular FILE. SECTION may be
1260 NULL, in which case it is a wild card. */
1263 output_section_callback (lang_wild_statement_type
*ptr
,
1264 struct wildcard_list
*sec
,
1266 lang_input_statement_type
*file
,
1269 lang_statement_union_type
*before
;
1271 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1272 if (unique_section_p (section
))
1275 /* If the wild pattern was marked KEEP, the member sections
1276 should be as well. */
1277 if (ptr
->keep_sections
)
1278 section
->flags
|= SEC_KEEP
;
1280 before
= wild_sort (ptr
, sec
, file
, section
);
1282 /* Here BEFORE points to the lang_input_section which
1283 should follow the one we are about to add. If BEFORE
1284 is NULL, then the section should just go at the end
1285 of the current list. */
1288 lang_add_section (&ptr
->children
, section
,
1289 (lang_output_section_statement_type
*) output
,
1293 lang_statement_list_type list
;
1294 lang_statement_union_type
**pp
;
1296 lang_list_init (&list
);
1297 lang_add_section (&list
, section
,
1298 (lang_output_section_statement_type
*) output
,
1301 /* If we are discarding the section, LIST.HEAD will
1303 if (list
.head
!= NULL
)
1305 ASSERT (list
.head
->header
.next
== NULL
);
1307 for (pp
= &ptr
->children
.head
;
1309 pp
= &(*pp
)->header
.next
)
1310 ASSERT (*pp
!= NULL
);
1312 list
.head
->header
.next
= *pp
;
1318 /* This is passed a file name which must have been seen already and
1319 added to the statement tree. We will see if it has been opened
1320 already and had its symbols read. If not then we'll read it. */
1322 static lang_input_statement_type
*
1323 lookup_name (const char *name
)
1325 lang_input_statement_type
*search
;
1327 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1329 search
= (lang_input_statement_type
*) search
->next_real_file
)
1331 /* Use the local_sym_name as the name of the file that has
1332 already been loaded as filename might have been transformed
1333 via the search directory lookup mechanism. */
1334 const char * filename
= search
->local_sym_name
;
1336 if (filename
== NULL
&& name
== NULL
)
1338 if (filename
!= NULL
1340 && strcmp (filename
, name
) == 0)
1345 search
= new_afile (name
, lang_input_file_is_search_file_enum
, default_target
,
1348 /* If we have already added this file, or this file is not real
1349 (FIXME: can that ever actually happen?) or the name is NULL
1350 (FIXME: can that ever actually happen?) don't add this file. */
1353 || search
->filename
== NULL
)
1356 if (! load_symbols (search
, NULL
))
1362 /* Get the symbols for an input file. */
1365 load_symbols (lang_input_statement_type
*entry
,
1366 lang_statement_list_type
*place
)
1373 ldfile_open_file (entry
);
1375 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1376 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1379 lang_statement_list_type
*hold
;
1380 bfd_boolean bad_load
= TRUE
;
1381 bfd_boolean save_ldlang_sysrooted_script
;
1383 err
= bfd_get_error ();
1385 /* See if the emulation has some special knowledge. */
1386 if (ldemul_unrecognized_file (entry
))
1389 if (err
== bfd_error_file_ambiguously_recognized
)
1393 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1394 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1395 for (p
= matching
; *p
!= NULL
; p
++)
1399 else if (err
!= bfd_error_file_not_recognized
1401 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1405 bfd_close (entry
->the_bfd
);
1406 entry
->the_bfd
= NULL
;
1408 /* Try to interpret the file as a linker script. */
1409 ldfile_open_command_file (entry
->filename
);
1413 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1414 ldlang_sysrooted_script
= entry
->sysrooted
;
1416 ldfile_assumed_script
= TRUE
;
1417 parser_input
= input_script
;
1419 ldfile_assumed_script
= FALSE
;
1421 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1427 if (ldemul_recognized_file (entry
))
1430 /* We don't call ldlang_add_file for an archive. Instead, the
1431 add_symbols entry point will call ldlang_add_file, via the
1432 add_archive_element callback, for each element of the archive
1434 switch (bfd_get_format (entry
->the_bfd
))
1440 ldlang_add_file (entry
);
1441 if (trace_files
|| trace_file_tries
)
1442 info_msg ("%I\n", entry
);
1446 if (entry
->whole_archive
)
1449 bfd_boolean loaded
= TRUE
;
1453 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1458 if (! bfd_check_format (member
, bfd_object
))
1460 einfo (_("%F%B: member %B in archive is not an object\n"),
1461 entry
->the_bfd
, member
);
1465 if (! ((*link_info
.callbacks
->add_archive_element
)
1466 (&link_info
, member
, "--whole-archive")))
1469 if (! bfd_link_add_symbols (member
, &link_info
))
1471 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1476 entry
->loaded
= loaded
;
1482 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1483 entry
->loaded
= TRUE
;
1485 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1487 return entry
->loaded
;
1490 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1491 may be NULL, indicating that it is a wildcard. Separate
1492 lang_input_section statements are created for each part of the
1493 expansion; they are added after the wild statement S. OUTPUT is
1494 the output section. */
1497 wild (lang_wild_statement_type
*s
,
1498 const char *target ATTRIBUTE_UNUSED
,
1499 lang_output_section_statement_type
*output
)
1501 struct wildcard_list
*sec
;
1503 walk_wild (s
, output_section_callback
, output
);
1505 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1507 if (default_common_section
!= NULL
)
1509 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1511 /* Remember the section that common is going to in case we
1512 later get something which doesn't know where to put it. */
1513 default_common_section
= output
;
1518 /* Return TRUE iff target is the sought target. */
1521 get_target (const bfd_target
*target
, void *data
)
1523 const char *sought
= data
;
1525 return strcmp (target
->name
, sought
) == 0;
1528 /* Like strcpy() but convert to lower case as well. */
1531 stricpy (char *dest
, char *src
)
1535 while ((c
= *src
++) != 0)
1536 *dest
++ = TOLOWER (c
);
1541 /* Remove the first occurrence of needle (if any) in haystack
1545 strcut (char *haystack
, char *needle
)
1547 haystack
= strstr (haystack
, needle
);
1553 for (src
= haystack
+ strlen (needle
); *src
;)
1554 *haystack
++ = *src
++;
1560 /* Compare two target format name strings.
1561 Return a value indicating how "similar" they are. */
1564 name_compare (char *first
, char *second
)
1570 copy1
= xmalloc (strlen (first
) + 1);
1571 copy2
= xmalloc (strlen (second
) + 1);
1573 /* Convert the names to lower case. */
1574 stricpy (copy1
, first
);
1575 stricpy (copy2
, second
);
1577 /* Remove size and endian strings from the name. */
1578 strcut (copy1
, "big");
1579 strcut (copy1
, "little");
1580 strcut (copy2
, "big");
1581 strcut (copy2
, "little");
1583 /* Return a value based on how many characters match,
1584 starting from the beginning. If both strings are
1585 the same then return 10 * their length. */
1586 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1587 if (copy1
[result
] == 0)
1599 /* Set by closest_target_match() below. */
1600 static const bfd_target
*winner
;
1602 /* Scan all the valid bfd targets looking for one that has the endianness
1603 requirement that was specified on the command line, and is the nearest
1604 match to the original output target. */
1607 closest_target_match (const bfd_target
*target
, void *data
)
1609 const bfd_target
*original
= data
;
1611 if (command_line
.endian
== ENDIAN_BIG
1612 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1615 if (command_line
.endian
== ENDIAN_LITTLE
1616 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1619 /* Must be the same flavour. */
1620 if (target
->flavour
!= original
->flavour
)
1623 /* If we have not found a potential winner yet, then record this one. */
1630 /* Oh dear, we now have two potential candidates for a successful match.
1631 Compare their names and choose the better one. */
1632 if (name_compare (target
->name
, original
->name
)
1633 > name_compare (winner
->name
, original
->name
))
1636 /* Keep on searching until wqe have checked them all. */
1640 /* Return the BFD target format of the first input file. */
1643 get_first_input_target (void)
1645 char *target
= NULL
;
1647 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1649 if (s
->header
.type
== lang_input_statement_enum
1652 ldfile_open_file (s
);
1654 if (s
->the_bfd
!= NULL
1655 && bfd_check_format (s
->the_bfd
, bfd_object
))
1657 target
= bfd_get_target (s
->the_bfd
);
1669 lang_get_output_target (void)
1673 /* Has the user told us which output format to use? */
1674 if (output_target
!= NULL
)
1675 return output_target
;
1677 /* No - has the current target been set to something other than
1679 if (current_target
!= default_target
)
1680 return current_target
;
1682 /* No - can we determine the format of the first input file? */
1683 target
= get_first_input_target ();
1687 /* Failed - use the default output target. */
1688 return default_target
;
1691 /* Open the output file. */
1694 open_output (const char *name
)
1698 output_target
= lang_get_output_target ();
1700 /* Has the user requested a particular endianness on the command
1702 if (command_line
.endian
!= ENDIAN_UNSET
)
1704 const bfd_target
*target
;
1705 enum bfd_endian desired_endian
;
1707 /* Get the chosen target. */
1708 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1710 /* If the target is not supported, we cannot do anything. */
1713 if (command_line
.endian
== ENDIAN_BIG
)
1714 desired_endian
= BFD_ENDIAN_BIG
;
1716 desired_endian
= BFD_ENDIAN_LITTLE
;
1718 /* See if the target has the wrong endianness. This should
1719 not happen if the linker script has provided big and
1720 little endian alternatives, but some scrips don't do
1722 if (target
->byteorder
!= desired_endian
)
1724 /* If it does, then see if the target provides
1725 an alternative with the correct endianness. */
1726 if (target
->alternative_target
!= NULL
1727 && (target
->alternative_target
->byteorder
== desired_endian
))
1728 output_target
= target
->alternative_target
->name
;
1731 /* Try to find a target as similar as possible to
1732 the default target, but which has the desired
1733 endian characteristic. */
1734 bfd_search_for_target (closest_target_match
,
1737 /* Oh dear - we could not find any targets that
1738 satisfy our requirements. */
1740 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1742 output_target
= winner
->name
;
1748 output
= bfd_openw (name
, output_target
);
1752 if (bfd_get_error () == bfd_error_invalid_target
)
1753 einfo (_("%P%F: target %s not found\n"), output_target
);
1755 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1758 delete_output_file_on_failure
= TRUE
;
1761 output
->flags
|= D_PAGED
;
1764 if (! bfd_set_format (output
, bfd_object
))
1765 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1766 if (! bfd_set_arch_mach (output
,
1767 ldfile_output_architecture
,
1768 ldfile_output_machine
))
1769 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1771 link_info
.hash
= bfd_link_hash_table_create (output
);
1772 if (link_info
.hash
== NULL
)
1773 einfo (_("%P%F: can not create link hash table: %E\n"));
1775 bfd_set_gp_size (output
, g_switch_value
);
1780 ldlang_open_output (lang_statement_union_type
*statement
)
1782 switch (statement
->header
.type
)
1784 case lang_output_statement_enum
:
1785 ASSERT (output_bfd
== NULL
);
1786 output_bfd
= open_output (statement
->output_statement
.name
);
1787 ldemul_set_output_arch ();
1788 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
1789 output_bfd
->flags
|= D_PAGED
;
1791 output_bfd
->flags
&= ~D_PAGED
;
1792 if (config
.text_read_only
)
1793 output_bfd
->flags
|= WP_TEXT
;
1795 output_bfd
->flags
&= ~WP_TEXT
;
1796 if (link_info
.traditional_format
)
1797 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1799 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1802 case lang_target_statement_enum
:
1803 current_target
= statement
->target_statement
.target
;
1810 /* Convert between addresses in bytes and sizes in octets.
1811 For currently supported targets, octets_per_byte is always a power
1812 of two, so we can use shifts. */
1813 #define TO_ADDR(X) ((X) >> opb_shift)
1814 #define TO_SIZE(X) ((X) << opb_shift)
1816 /* Support the above. */
1817 static unsigned int opb_shift
= 0;
1822 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
1823 ldfile_output_machine
);
1826 while ((x
& 1) == 0)
1834 /* Open all the input files. */
1837 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
1839 for (; s
!= NULL
; s
= s
->header
.next
)
1841 switch (s
->header
.type
)
1843 case lang_constructors_statement_enum
:
1844 open_input_bfds (constructor_list
.head
, force
);
1846 case lang_output_section_statement_enum
:
1847 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1849 case lang_wild_statement_enum
:
1850 /* Maybe we should load the file's symbols. */
1851 if (s
->wild_statement
.filename
1852 && ! wildcardp (s
->wild_statement
.filename
))
1853 lookup_name (s
->wild_statement
.filename
);
1854 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1856 case lang_group_statement_enum
:
1858 struct bfd_link_hash_entry
*undefs
;
1860 /* We must continually search the entries in the group
1861 until no new symbols are added to the list of undefined
1866 undefs
= link_info
.hash
->undefs_tail
;
1867 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
1869 while (undefs
!= link_info
.hash
->undefs_tail
);
1872 case lang_target_statement_enum
:
1873 current_target
= s
->target_statement
.target
;
1875 case lang_input_statement_enum
:
1876 if (s
->input_statement
.real
)
1878 lang_statement_list_type add
;
1880 s
->input_statement
.target
= current_target
;
1882 /* If we are being called from within a group, and this
1883 is an archive which has already been searched, then
1884 force it to be researched unless the whole archive
1885 has been loaded already. */
1887 && !s
->input_statement
.whole_archive
1888 && s
->input_statement
.loaded
1889 && bfd_check_format (s
->input_statement
.the_bfd
,
1891 s
->input_statement
.loaded
= FALSE
;
1893 lang_list_init (&add
);
1895 if (! load_symbols (&s
->input_statement
, &add
))
1896 config
.make_executable
= FALSE
;
1898 if (add
.head
!= NULL
)
1900 *add
.tail
= s
->header
.next
;
1901 s
->header
.next
= add
.head
;
1911 /* If there are [COMMONS] statements, put a wild one into the bss
1915 lang_reasonable_defaults (void)
1918 lang_output_section_statement_lookup (".text");
1919 lang_output_section_statement_lookup (".data");
1921 default_common_section
= lang_output_section_statement_lookup (".bss");
1923 if (!placed_commons
)
1925 lang_wild_statement_type
*new =
1926 new_stat (lang_wild_statement
,
1927 &default_common_section
->children
);
1929 new->section_name
= "COMMON";
1930 new->filename
= NULL
;
1931 lang_list_init (&new->children
);
1936 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1939 lang_track_definedness (const char *name
)
1941 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
1942 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
1945 /* New-function for the definedness hash table. */
1947 static struct bfd_hash_entry
*
1948 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
1949 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
1950 const char *name ATTRIBUTE_UNUSED
)
1952 struct lang_definedness_hash_entry
*ret
1953 = (struct lang_definedness_hash_entry
*) entry
;
1956 ret
= (struct lang_definedness_hash_entry
*)
1957 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
1960 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
1962 ret
->iteration
= -1;
1966 /* Return the iteration when the definition of NAME was last updated. A
1967 value of -1 means that the symbol is not defined in the linker script
1968 or the command line, but may be defined in the linker symbol table. */
1971 lang_symbol_definition_iteration (const char *name
)
1973 struct lang_definedness_hash_entry
*defentry
1974 = (struct lang_definedness_hash_entry
*)
1975 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1977 /* We've already created this one on the presence of DEFINED in the
1978 script, so it can't be NULL unless something is borked elsewhere in
1980 if (defentry
== NULL
)
1983 return defentry
->iteration
;
1986 /* Update the definedness state of NAME. */
1989 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
1991 struct lang_definedness_hash_entry
*defentry
1992 = (struct lang_definedness_hash_entry
*)
1993 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1995 /* We don't keep track of symbols not tested with DEFINED. */
1996 if (defentry
== NULL
)
1999 /* If the symbol was already defined, and not from an earlier statement
2000 iteration, don't update the definedness iteration, because that'd
2001 make the symbol seem defined in the linker script at this point, and
2002 it wasn't; it was defined in some object. If we do anyway, DEFINED
2003 would start to yield false before this point and the construct "sym =
2004 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2006 if (h
->type
!= bfd_link_hash_undefined
2007 && h
->type
!= bfd_link_hash_common
2008 && h
->type
!= bfd_link_hash_new
2009 && defentry
->iteration
== -1)
2012 defentry
->iteration
= lang_statement_iteration
;
2015 /* Add the supplied name to the symbol table as an undefined reference.
2016 This is a two step process as the symbol table doesn't even exist at
2017 the time the ld command line is processed. First we put the name
2018 on a list, then, once the output file has been opened, transfer the
2019 name to the symbol table. */
2021 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2023 #define ldlang_undef_chain_list_head entry_symbol.next
2026 ldlang_add_undef (const char *const name
)
2028 ldlang_undef_chain_list_type
*new =
2029 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
2031 new->next
= ldlang_undef_chain_list_head
;
2032 ldlang_undef_chain_list_head
= new;
2034 new->name
= xstrdup (name
);
2036 if (output_bfd
!= NULL
)
2037 insert_undefined (new->name
);
2040 /* Insert NAME as undefined in the symbol table. */
2043 insert_undefined (const char *name
)
2045 struct bfd_link_hash_entry
*h
;
2047 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2049 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2050 if (h
->type
== bfd_link_hash_new
)
2052 h
->type
= bfd_link_hash_undefined
;
2053 h
->u
.undef
.abfd
= NULL
;
2054 bfd_link_add_undef (link_info
.hash
, h
);
2058 /* Run through the list of undefineds created above and place them
2059 into the linker hash table as undefined symbols belonging to the
2063 lang_place_undefineds (void)
2065 ldlang_undef_chain_list_type
*ptr
;
2067 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
2068 insert_undefined (ptr
->name
);
2071 /* Open input files and attach to output sections. */
2074 map_input_to_output_sections
2075 (lang_statement_union_type
*s
, const char *target
,
2076 lang_output_section_statement_type
*output_section_statement
)
2078 for (; s
!= NULL
; s
= s
->header
.next
)
2080 switch (s
->header
.type
)
2082 case lang_wild_statement_enum
:
2083 wild (&s
->wild_statement
, target
, output_section_statement
);
2085 case lang_constructors_statement_enum
:
2086 map_input_to_output_sections (constructor_list
.head
,
2088 output_section_statement
);
2090 case lang_output_section_statement_enum
:
2091 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2093 &s
->output_section_statement
);
2095 case lang_output_statement_enum
:
2097 case lang_target_statement_enum
:
2098 target
= s
->target_statement
.target
;
2100 case lang_group_statement_enum
:
2101 map_input_to_output_sections (s
->group_statement
.children
.head
,
2103 output_section_statement
);
2105 case lang_data_statement_enum
:
2106 /* Make sure that any sections mentioned in the expression
2108 exp_init_os (s
->data_statement
.exp
);
2110 case lang_fill_statement_enum
:
2111 case lang_input_section_enum
:
2112 case lang_object_symbols_statement_enum
:
2113 case lang_reloc_statement_enum
:
2114 case lang_padding_statement_enum
:
2115 case lang_input_statement_enum
:
2116 if (output_section_statement
!= NULL
2117 && output_section_statement
->bfd_section
== NULL
)
2118 init_os (output_section_statement
);
2120 case lang_assignment_statement_enum
:
2121 if (output_section_statement
!= NULL
2122 && output_section_statement
->bfd_section
== NULL
)
2123 init_os (output_section_statement
);
2125 /* Make sure that any sections mentioned in the assignment
2127 exp_init_os (s
->assignment_statement
.exp
);
2129 case lang_afile_asection_pair_statement_enum
:
2132 case lang_address_statement_enum
:
2133 /* Mark the specified section with the supplied address. */
2135 lang_output_section_statement_type
*os
=
2136 lang_output_section_statement_lookup
2137 (s
->address_statement
.section_name
);
2139 if (os
->bfd_section
== NULL
)
2141 os
->addr_tree
= s
->address_statement
.address
;
2148 /* An output section might have been removed after its statement was
2149 added. For example, ldemul_before_allocation can remove dynamic
2150 sections if they turn out to be not needed. Clean them up here. */
2153 strip_excluded_output_sections (void)
2155 lang_statement_union_type
*u
;
2157 for (u
= lang_output_section_statement
.head
;
2159 u
= u
->output_section_statement
.next
)
2161 lang_output_section_statement_type
*os
;
2164 os
= &u
->output_section_statement
;
2165 s
= os
->bfd_section
;
2166 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2170 os
->bfd_section
= NULL
;
2172 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2175 bfd_section_list_remove (output_bfd
, p
);
2176 output_bfd
->section_count
--;
2184 print_output_section_statement
2185 (lang_output_section_statement_type
*output_section_statement
)
2187 asection
*section
= output_section_statement
->bfd_section
;
2190 if (output_section_statement
!= abs_output_section
)
2192 minfo ("\n%s", output_section_statement
->name
);
2194 if (section
!= NULL
)
2196 print_dot
= section
->vma
;
2198 len
= strlen (output_section_statement
->name
);
2199 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2204 while (len
< SECTION_NAME_MAP_LENGTH
)
2210 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2212 if (output_section_statement
->load_base
!= NULL
)
2216 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2217 "load base", lang_final_phase_enum
);
2218 minfo (_(" load address 0x%V"), addr
);
2225 print_statement_list (output_section_statement
->children
.head
,
2226 output_section_statement
);
2230 print_assignment (lang_assignment_statement_type
*assignment
,
2231 lang_output_section_statement_type
*output_section
)
2234 etree_value_type result
;
2236 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2239 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2240 lang_final_phase_enum
, print_dot
, &print_dot
);
2246 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2247 dst
= assignment
->exp
->assign
.dst
;
2249 minfo ("0x%V", value
);
2250 if (dst
[0] == '.' && dst
[1] == 0)
2263 exp_print_tree (assignment
->exp
);
2269 print_input_statement (lang_input_statement_type
*statm
)
2271 if (statm
->filename
!= NULL
)
2273 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2277 /* Print all symbols defined in a particular section. This is called
2278 via bfd_link_hash_traverse. */
2281 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
2283 asection
*sec
= ptr
;
2285 if ((hash_entry
->type
== bfd_link_hash_defined
2286 || hash_entry
->type
== bfd_link_hash_defweak
)
2287 && sec
== hash_entry
->u
.def
.section
)
2291 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2294 (hash_entry
->u
.def
.value
2295 + hash_entry
->u
.def
.section
->output_offset
2296 + hash_entry
->u
.def
.section
->output_section
->vma
));
2298 minfo (" %T\n", hash_entry
->root
.string
);
2304 /* Print information about an input section to the map file. */
2307 print_input_section (lang_input_section_type
*in
)
2309 asection
*i
= in
->section
;
2310 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2317 minfo ("%s", i
->name
);
2319 if (i
->output_section
!= NULL
)
2323 len
= 1 + strlen (i
->name
);
2324 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2329 while (len
< SECTION_NAME_MAP_LENGTH
)
2335 minfo ("0x%V %W %B\n",
2336 i
->output_section
->vma
+ i
->output_offset
, TO_ADDR (size
),
2339 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2341 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2353 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2356 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
2358 print_dot
= (i
->output_section
->vma
+ i
->output_offset
2365 print_fill_statement (lang_fill_statement_type
*fill
)
2369 fputs (" FILL mask 0x", config
.map_file
);
2370 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2371 fprintf (config
.map_file
, "%02x", *p
);
2372 fputs ("\n", config
.map_file
);
2376 print_data_statement (lang_data_statement_type
*data
)
2384 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2387 addr
= data
->output_vma
;
2388 if (data
->output_section
!= NULL
)
2389 addr
+= data
->output_section
->vma
;
2417 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2419 if (data
->exp
->type
.node_class
!= etree_value
)
2422 exp_print_tree (data
->exp
);
2427 print_dot
= addr
+ TO_ADDR (size
);
2430 /* Print an address statement. These are generated by options like
2434 print_address_statement (lang_address_statement_type
*address
)
2436 minfo (_("Address of section %s set to "), address
->section_name
);
2437 exp_print_tree (address
->address
);
2441 /* Print a reloc statement. */
2444 print_reloc_statement (lang_reloc_statement_type
*reloc
)
2451 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2454 addr
= reloc
->output_vma
;
2455 if (reloc
->output_section
!= NULL
)
2456 addr
+= reloc
->output_section
->vma
;
2458 size
= bfd_get_reloc_size (reloc
->howto
);
2460 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2462 if (reloc
->name
!= NULL
)
2463 minfo ("%s+", reloc
->name
);
2465 minfo ("%s+", reloc
->section
->name
);
2467 exp_print_tree (reloc
->addend_exp
);
2471 print_dot
= addr
+ TO_ADDR (size
);
2475 print_padding_statement (lang_padding_statement_type
*s
)
2483 len
= sizeof " *fill*" - 1;
2484 while (len
< SECTION_NAME_MAP_LENGTH
)
2490 addr
= s
->output_offset
;
2491 if (s
->output_section
!= NULL
)
2492 addr
+= s
->output_section
->vma
;
2493 minfo ("0x%V %W ", addr
, s
->size
);
2495 if (s
->fill
->size
!= 0)
2499 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2500 fprintf (config
.map_file
, "%02x", *p
);
2505 print_dot
= addr
+ TO_ADDR (s
->size
);
2509 print_wild_statement (lang_wild_statement_type
*w
,
2510 lang_output_section_statement_type
*os
)
2512 struct wildcard_list
*sec
;
2516 if (w
->filenames_sorted
)
2518 if (w
->filename
!= NULL
)
2519 minfo ("%s", w
->filename
);
2522 if (w
->filenames_sorted
)
2526 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2528 if (sec
->spec
.sorted
)
2530 if (sec
->spec
.exclude_name_list
!= NULL
)
2533 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2534 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2535 minfo (" %s", tmp
->name
);
2538 if (sec
->spec
.name
!= NULL
)
2539 minfo ("%s", sec
->spec
.name
);
2542 if (sec
->spec
.sorted
)
2551 print_statement_list (w
->children
.head
, os
);
2554 /* Print a group statement. */
2557 print_group (lang_group_statement_type
*s
,
2558 lang_output_section_statement_type
*os
)
2560 fprintf (config
.map_file
, "START GROUP\n");
2561 print_statement_list (s
->children
.head
, os
);
2562 fprintf (config
.map_file
, "END GROUP\n");
2565 /* Print the list of statements in S.
2566 This can be called for any statement type. */
2569 print_statement_list (lang_statement_union_type
*s
,
2570 lang_output_section_statement_type
*os
)
2574 print_statement (s
, os
);
2579 /* Print the first statement in statement list S.
2580 This can be called for any statement type. */
2583 print_statement (lang_statement_union_type
*s
,
2584 lang_output_section_statement_type
*os
)
2586 switch (s
->header
.type
)
2589 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2592 case lang_constructors_statement_enum
:
2593 if (constructor_list
.head
!= NULL
)
2595 if (constructors_sorted
)
2596 minfo (" SORT (CONSTRUCTORS)\n");
2598 minfo (" CONSTRUCTORS\n");
2599 print_statement_list (constructor_list
.head
, os
);
2602 case lang_wild_statement_enum
:
2603 print_wild_statement (&s
->wild_statement
, os
);
2605 case lang_address_statement_enum
:
2606 print_address_statement (&s
->address_statement
);
2608 case lang_object_symbols_statement_enum
:
2609 minfo (" CREATE_OBJECT_SYMBOLS\n");
2611 case lang_fill_statement_enum
:
2612 print_fill_statement (&s
->fill_statement
);
2614 case lang_data_statement_enum
:
2615 print_data_statement (&s
->data_statement
);
2617 case lang_reloc_statement_enum
:
2618 print_reloc_statement (&s
->reloc_statement
);
2620 case lang_input_section_enum
:
2621 print_input_section (&s
->input_section
);
2623 case lang_padding_statement_enum
:
2624 print_padding_statement (&s
->padding_statement
);
2626 case lang_output_section_statement_enum
:
2627 print_output_section_statement (&s
->output_section_statement
);
2629 case lang_assignment_statement_enum
:
2630 print_assignment (&s
->assignment_statement
, os
);
2632 case lang_target_statement_enum
:
2633 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2635 case lang_output_statement_enum
:
2636 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2637 if (output_target
!= NULL
)
2638 minfo (" %s", output_target
);
2641 case lang_input_statement_enum
:
2642 print_input_statement (&s
->input_statement
);
2644 case lang_group_statement_enum
:
2645 print_group (&s
->group_statement
, os
);
2647 case lang_afile_asection_pair_statement_enum
:
2654 print_statements (void)
2656 print_statement_list (statement_list
.head
, abs_output_section
);
2659 /* Print the first N statements in statement list S to STDERR.
2660 If N == 0, nothing is printed.
2661 If N < 0, the entire list is printed.
2662 Intended to be called from GDB. */
2665 dprint_statement (lang_statement_union_type
*s
, int n
)
2667 FILE *map_save
= config
.map_file
;
2669 config
.map_file
= stderr
;
2672 print_statement_list (s
, abs_output_section
);
2675 while (s
&& --n
>= 0)
2677 print_statement (s
, abs_output_section
);
2682 config
.map_file
= map_save
;
2686 insert_pad (lang_statement_union_type
**ptr
,
2688 unsigned int alignment_needed
,
2689 asection
*output_section
,
2692 static fill_type zero_fill
= { 1, { 0 } };
2693 lang_statement_union_type
*pad
;
2695 pad
= ((lang_statement_union_type
*)
2696 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2697 if (ptr
!= &statement_list
.head
2698 && pad
->header
.type
== lang_padding_statement_enum
2699 && pad
->padding_statement
.output_section
== output_section
)
2701 /* Use the existing pad statement. The above test on output
2702 section is probably redundant, but it doesn't hurt to check. */
2706 /* Make a new padding statement, linked into existing chain. */
2707 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
2708 pad
->header
.next
= *ptr
;
2710 pad
->header
.type
= lang_padding_statement_enum
;
2711 pad
->padding_statement
.output_section
= output_section
;
2714 pad
->padding_statement
.fill
= fill
;
2716 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2717 pad
->padding_statement
.size
= alignment_needed
;
2718 output_section
->_raw_size
+= alignment_needed
;
2721 /* Work out how much this section will move the dot point. */
2724 size_input_section (lang_statement_union_type
**this_ptr
,
2725 lang_output_section_statement_type
*output_section_statement
,
2729 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2730 asection
*i
= is
->section
;
2732 if (!is
->ifile
->just_syms_flag
)
2734 unsigned int alignment_needed
;
2737 /* Align this section first to the input sections requirement,
2738 then to the output section's requirement. If this alignment
2739 is greater than any seen before, then record it too. Perform
2740 the alignment by inserting a magic 'padding' statement. */
2742 if (output_section_statement
->subsection_alignment
!= -1)
2743 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2745 o
= output_section_statement
->bfd_section
;
2746 if (o
->alignment_power
< i
->alignment_power
)
2747 o
->alignment_power
= i
->alignment_power
;
2749 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2751 if (alignment_needed
!= 0)
2753 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
2754 dot
+= alignment_needed
;
2757 /* Remember where in the output section this input section goes. */
2759 i
->output_offset
= dot
- o
->vma
;
2761 /* Mark how big the output section must be to contain this now. */
2762 if (i
->_cooked_size
!= 0)
2763 dot
+= TO_ADDR (i
->_cooked_size
);
2765 dot
+= TO_ADDR (i
->_raw_size
);
2766 o
->_raw_size
= TO_SIZE (dot
- o
->vma
);
2770 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2776 #define IGNORE_SECTION(bfd, s) \
2777 (((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2778 ? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2780 : ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2782 || bfd_section_size (bfd, s) == 0)
2784 /* Check to see if any allocated sections overlap with other allocated
2785 sections. This can happen when the linker script specifically specifies
2786 the output section addresses of the two sections. */
2789 lang_check_section_addresses (void)
2793 /* Scan all sections in the output list. */
2794 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2798 /* Ignore sections which are not loaded or which have no contents. */
2799 if (IGNORE_SECTION (output_bfd
, s
))
2802 /* Once we reach section 's' stop our seach. This prevents two
2803 warning messages from being produced, one for 'section A overlaps
2804 section B' and one for 'section B overlaps section A'. */
2805 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2812 /* Only consider loadable sections with real contents. */
2813 if (IGNORE_SECTION (output_bfd
, os
))
2816 /* We must check the sections' LMA addresses not their
2817 VMA addresses because overlay sections can have
2818 overlapping VMAs but they must have distinct LMAs. */
2819 s_start
= bfd_section_lma (output_bfd
, s
);
2820 os_start
= bfd_section_lma (output_bfd
, os
);
2821 s_end
= s_start
+ TO_ADDR (bfd_section_size (output_bfd
, s
)) - 1;
2822 os_end
= os_start
+ TO_ADDR (bfd_section_size (output_bfd
, os
)) - 1;
2824 /* Look for an overlap. */
2825 if ((s_end
< os_start
) || (s_start
> os_end
))
2829 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2830 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2832 /* Once we have found one overlap for this section,
2833 stop looking for others. */
2839 /* Make sure the new address is within the region. We explicitly permit the
2840 current address to be at the exact end of the region when the address is
2841 non-zero, in case the region is at the end of addressable memory and the
2842 calculation wraps around. */
2845 os_region_check (lang_output_section_statement_type
*os
,
2846 lang_memory_region_type
*region
,
2850 if ((region
->current
< region
->origin
2851 || (region
->current
- region
->origin
> region
->length
))
2852 && ((region
->current
!= region
->origin
+ region
->length
)
2857 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2859 os
->bfd_section
->owner
,
2860 os
->bfd_section
->name
,
2865 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2867 os
->bfd_section
->owner
,
2868 os
->bfd_section
->name
);
2870 /* Reset the region pointer. */
2871 region
->current
= region
->origin
;
2875 /* Set the sizes for all the output sections. */
2878 lang_size_sections_1
2879 (lang_statement_union_type
*s
,
2880 lang_output_section_statement_type
*output_section_statement
,
2881 lang_statement_union_type
**prev
,
2885 bfd_boolean check_regions
)
2887 /* Size up the sections from their constituent parts. */
2888 for (; s
!= NULL
; s
= s
->header
.next
)
2890 switch (s
->header
.type
)
2892 case lang_output_section_statement_enum
:
2895 lang_output_section_statement_type
*os
;
2897 os
= &s
->output_section_statement
;
2898 if (os
->bfd_section
== NULL
)
2899 /* This section was never actually created. */
2902 /* If this is a COFF shared library section, use the size and
2903 address from the input section. FIXME: This is COFF
2904 specific; it would be cleaner if there were some other way
2905 to do this, but nothing simple comes to mind. */
2906 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2910 if (os
->children
.head
== NULL
2911 || os
->children
.head
->header
.next
!= NULL
2912 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2913 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2916 input
= os
->children
.head
->input_section
.section
;
2917 bfd_set_section_vma (os
->bfd_section
->owner
,
2919 bfd_section_vma (input
->owner
, input
));
2920 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2924 if (bfd_is_abs_section (os
->bfd_section
))
2926 /* No matter what happens, an abs section starts at zero. */
2927 ASSERT (os
->bfd_section
->vma
== 0);
2931 if (os
->addr_tree
== NULL
)
2933 /* No address specified for this section, get one
2934 from the region specification. */
2935 if (os
->region
== NULL
2936 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2937 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2938 && os
->region
->name
[0] == '*'
2939 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0))
2941 os
->region
= lang_memory_default (os
->bfd_section
);
2944 /* If a loadable section is using the default memory
2945 region, and some non default memory regions were
2946 defined, issue an error message. */
2947 if (!IGNORE_SECTION (output_bfd
, os
->bfd_section
)
2948 && ! link_info
.relocatable
2950 && strcmp (os
->region
->name
, DEFAULT_MEMORY_REGION
) == 0
2951 && lang_memory_region_list
!= NULL
2952 && (strcmp (lang_memory_region_list
->name
,
2953 DEFAULT_MEMORY_REGION
) != 0
2954 || lang_memory_region_list
->next
!= NULL
))
2956 /* By default this is an error rather than just a
2957 warning because if we allocate the section to the
2958 default memory region we can end up creating an
2959 excessively large binary, or even seg faulting when
2960 attempting to perform a negative seek. See
2961 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2962 for an example of this. This behaviour can be
2963 overridden by the using the --no-check-sections
2965 if (command_line
.check_section_addresses
)
2966 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2967 bfd_get_section_name (output_bfd
,
2970 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2971 bfd_get_section_name (output_bfd
,
2975 dot
= os
->region
->current
;
2977 if (os
->section_alignment
== -1)
2982 dot
= align_power (dot
,
2983 os
->bfd_section
->alignment_power
);
2985 if (dot
!= olddot
&& config
.warn_section_align
)
2986 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2987 os
->name
, (unsigned int) (dot
- olddot
));
2995 r
= exp_fold_tree (os
->addr_tree
,
2997 lang_allocating_phase_enum
,
3002 einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
3005 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
3008 /* The section starts here.
3009 First, align to what the section needs. */
3011 if (os
->section_alignment
!= -1)
3012 dot
= align_power (dot
, os
->section_alignment
);
3014 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3016 os
->bfd_section
->output_offset
= 0;
3019 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3020 os
->fill
, dot
, relax
, check_regions
);
3022 /* Put the section within the requested block size, or
3023 align at the block boundary. */
3024 after
= ((os
->bfd_section
->vma
3025 + TO_ADDR (os
->bfd_section
->_raw_size
)
3026 + os
->block_value
- 1)
3027 & - (bfd_vma
) os
->block_value
);
3029 if (bfd_is_abs_section (os
->bfd_section
))
3030 ASSERT (after
== os
->bfd_section
->vma
);
3032 os
->bfd_section
->_raw_size
3033 = TO_SIZE (after
- os
->bfd_section
->vma
);
3035 dot
= os
->bfd_section
->vma
;
3036 /* .tbss sections effectively have zero size. */
3037 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3038 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3039 || link_info
.relocatable
)
3040 dot
+= TO_ADDR (os
->bfd_section
->_raw_size
);
3044 if (os
->update_dot_tree
!= 0)
3045 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3046 lang_allocating_phase_enum
, dot
, &dot
);
3048 /* Update dot in the region ?
3049 We only do this if the section is going to be allocated,
3050 since unallocated sections do not contribute to the region's
3051 overall size in memory.
3053 If the SEC_NEVER_LOAD bit is not set, it will affect the
3054 addresses of sections after it. We have to update
3056 if (os
->region
!= NULL
3057 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3058 & SEC_NEVER_LOAD
) == 0
3059 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3060 & (SEC_ALLOC
| SEC_LOAD
))))
3062 os
->region
->current
= dot
;
3065 /* Make sure the new address is within the region. */
3066 os_region_check (os
, os
->region
, os
->addr_tree
,
3067 os
->bfd_section
->vma
);
3069 /* If there's no load address specified, use the run
3070 region as the load region. */
3071 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3072 os
->lma_region
= os
->region
;
3074 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3076 /* Set load_base, which will be handled later. */
3077 os
->load_base
= exp_intop (os
->lma_region
->current
);
3078 os
->lma_region
->current
+=
3079 TO_ADDR (os
->bfd_section
->_raw_size
);
3081 os_region_check (os
, os
->lma_region
, NULL
,
3082 os
->bfd_section
->lma
);
3088 case lang_constructors_statement_enum
:
3089 dot
= lang_size_sections_1 (constructor_list
.head
,
3090 output_section_statement
,
3091 &s
->wild_statement
.children
.head
,
3092 fill
, dot
, relax
, check_regions
);
3095 case lang_data_statement_enum
:
3097 unsigned int size
= 0;
3099 s
->data_statement
.output_vma
=
3100 dot
- output_section_statement
->bfd_section
->vma
;
3101 s
->data_statement
.output_section
=
3102 output_section_statement
->bfd_section
;
3104 /* We might refer to provided symbols in the expression, and
3105 need to mark them as needed. */
3106 exp_fold_tree (s
->data_statement
.exp
, abs_output_section
,
3107 lang_allocating_phase_enum
, dot
, &dot
);
3109 switch (s
->data_statement
.type
)
3127 if (size
< TO_SIZE ((unsigned) 1))
3128 size
= TO_SIZE ((unsigned) 1);
3129 dot
+= TO_ADDR (size
);
3130 output_section_statement
->bfd_section
->_raw_size
+= size
;
3131 /* The output section gets contents, and then we inspect for
3132 any flags set in the input script which override any ALLOC. */
3133 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3134 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3136 output_section_statement
->bfd_section
->flags
|=
3137 SEC_ALLOC
| SEC_LOAD
;
3142 case lang_reloc_statement_enum
:
3146 s
->reloc_statement
.output_vma
=
3147 dot
- output_section_statement
->bfd_section
->vma
;
3148 s
->reloc_statement
.output_section
=
3149 output_section_statement
->bfd_section
;
3150 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3151 dot
+= TO_ADDR (size
);
3152 output_section_statement
->bfd_section
->_raw_size
+= size
;
3156 case lang_wild_statement_enum
:
3158 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3159 output_section_statement
,
3160 &s
->wild_statement
.children
.head
,
3161 fill
, dot
, relax
, check_regions
);
3165 case lang_object_symbols_statement_enum
:
3166 link_info
.create_object_symbols_section
=
3167 output_section_statement
->bfd_section
;
3169 case lang_output_statement_enum
:
3170 case lang_target_statement_enum
:
3172 case lang_input_section_enum
:
3176 i
= (*prev
)->input_section
.section
;
3179 if (i
->_cooked_size
== 0)
3180 i
->_cooked_size
= i
->_raw_size
;
3186 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3187 einfo (_("%P%F: can't relax section: %E\n"));
3191 dot
= size_input_section (prev
, output_section_statement
,
3192 output_section_statement
->fill
, dot
);
3195 case lang_input_statement_enum
:
3197 case lang_fill_statement_enum
:
3198 s
->fill_statement
.output_section
=
3199 output_section_statement
->bfd_section
;
3201 fill
= s
->fill_statement
.fill
;
3203 case lang_assignment_statement_enum
:
3205 bfd_vma newdot
= dot
;
3207 exp_fold_tree (s
->assignment_statement
.exp
,
3208 output_section_statement
,
3209 lang_allocating_phase_enum
,
3215 if (output_section_statement
== abs_output_section
)
3217 /* If we don't have an output section, then just adjust
3218 the default memory address. */
3219 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
)->current
= newdot
;
3223 /* Insert a pad after this statement. We can't
3224 put the pad before when relaxing, in case the
3225 assignment references dot. */
3226 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
3227 output_section_statement
->bfd_section
, dot
);
3229 /* Don't neuter the pad below when relaxing. */
3233 /* If dot is advanced, this implies that the section should
3234 have space allocated to it, unless the user has explicitly
3235 stated that the section should never be loaded. */
3236 if (!(output_section_statement
->flags
& (SEC_NEVER_LOAD
| SEC_ALLOC
)))
3237 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
3244 case lang_padding_statement_enum
:
3245 /* If this is the first time lang_size_sections is called,
3246 we won't have any padding statements. If this is the
3247 second or later passes when relaxing, we should allow
3248 padding to shrink. If padding is needed on this pass, it
3249 will be added back in. */
3250 s
->padding_statement
.size
= 0;
3252 /* Make sure output_offset is valid. If relaxation shrinks
3253 the section and this pad isn't needed, it's possible to
3254 have output_offset larger than the final size of the
3255 section. bfd_set_section_contents will complain even for
3256 a pad size of zero. */
3257 s
->padding_statement
.output_offset
3258 = dot
- output_section_statement
->bfd_section
->vma
;
3261 case lang_group_statement_enum
:
3262 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3263 output_section_statement
,
3264 &s
->group_statement
.children
.head
,
3265 fill
, dot
, relax
, check_regions
);
3272 /* We can only get here when relaxing is turned on. */
3273 case lang_address_statement_enum
:
3276 prev
= &s
->header
.next
;
3283 (lang_statement_union_type
*s
,
3284 lang_output_section_statement_type
*output_section_statement
,
3285 lang_statement_union_type
**prev
,
3289 bfd_boolean check_regions
)
3294 /* Callers of exp_fold_tree need to increment this. */
3295 lang_statement_iteration
++;
3297 exp_data_seg
.phase
= exp_dataseg_none
;
3298 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3299 dot
, relax
, check_regions
);
3300 if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3302 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3303 a page could be saved in the data segment. */
3304 bfd_vma first
, last
;
3306 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3307 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3309 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3310 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3311 && first
+ last
<= exp_data_seg
.pagesize
)
3313 exp_data_seg
.phase
= exp_dataseg_adjust
;
3314 lang_statement_iteration
++;
3315 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3316 fill
, dot
, relax
, check_regions
);
3320 /* Some backend relaxers want to refer to the output section size. Give
3321 them a section size that does not change on the next call while they
3322 relax. We can't set this at top because lang_reset_memory_regions
3323 which is called before we get here, sets _raw_size to 0 on relaxing
3325 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3326 o
->_cooked_size
= o
->_raw_size
;
3331 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3334 lang_do_assignments_1
3335 (lang_statement_union_type
*s
,
3336 lang_output_section_statement_type
*output_section_statement
,
3340 for (; s
!= NULL
; s
= s
->header
.next
)
3342 switch (s
->header
.type
)
3344 case lang_constructors_statement_enum
:
3345 dot
= lang_do_assignments_1 (constructor_list
.head
,
3346 output_section_statement
,
3351 case lang_output_section_statement_enum
:
3353 lang_output_section_statement_type
*os
;
3355 os
= &(s
->output_section_statement
);
3356 if (os
->bfd_section
!= NULL
)
3358 dot
= os
->bfd_section
->vma
;
3359 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
3360 /* .tbss sections effectively have zero size. */
3361 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3362 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3363 || link_info
.relocatable
)
3364 dot
+= TO_ADDR (os
->bfd_section
->_raw_size
);
3368 /* If nothing has been placed into the output section then
3369 it won't have a bfd_section. */
3370 if (os
->bfd_section
)
3372 os
->bfd_section
->lma
3373 = exp_get_abs_int (os
->load_base
, 0, "load base",
3374 lang_final_phase_enum
);
3379 case lang_wild_statement_enum
:
3381 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
3382 output_section_statement
,
3387 case lang_object_symbols_statement_enum
:
3388 case lang_output_statement_enum
:
3389 case lang_target_statement_enum
:
3391 case lang_common_statement_enum
:
3394 case lang_data_statement_enum
:
3396 etree_value_type value
;
3398 value
= exp_fold_tree (s
->data_statement
.exp
,
3400 lang_final_phase_enum
, dot
, &dot
);
3402 einfo (_("%F%P: invalid data statement\n"));
3403 s
->data_statement
.value
3404 = value
.value
+ value
.section
->bfd_section
->vma
;
3408 switch (s
->data_statement
.type
)
3426 if (size
< TO_SIZE ((unsigned) 1))
3427 size
= TO_SIZE ((unsigned) 1);
3428 dot
+= TO_ADDR (size
);
3432 case lang_reloc_statement_enum
:
3434 etree_value_type value
;
3436 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3438 lang_final_phase_enum
, dot
, &dot
);
3439 s
->reloc_statement
.addend_value
= value
.value
;
3441 einfo (_("%F%P: invalid reloc statement\n"));
3443 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
3446 case lang_input_section_enum
:
3448 asection
*in
= s
->input_section
.section
;
3450 if (in
->_cooked_size
!= 0)
3451 dot
+= TO_ADDR (in
->_cooked_size
);
3453 dot
+= TO_ADDR (in
->_raw_size
);
3457 case lang_input_statement_enum
:
3459 case lang_fill_statement_enum
:
3460 fill
= s
->fill_statement
.fill
;
3462 case lang_assignment_statement_enum
:
3464 exp_fold_tree (s
->assignment_statement
.exp
,
3465 output_section_statement
,
3466 lang_final_phase_enum
,
3472 case lang_padding_statement_enum
:
3473 dot
+= TO_ADDR (s
->padding_statement
.size
);
3476 case lang_group_statement_enum
:
3477 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
3478 output_section_statement
,
3486 case lang_address_statement_enum
:
3495 lang_do_assignments (lang_statement_union_type
*s
,
3496 lang_output_section_statement_type
*output_section_statement
,
3500 /* Callers of exp_fold_tree need to increment this. */
3501 lang_statement_iteration
++;
3502 lang_do_assignments_1 (s
, output_section_statement
, fill
, dot
);
3505 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3506 operator .startof. (section_name), it produces an undefined symbol
3507 .startof.section_name. Similarly, when it sees
3508 .sizeof. (section_name), it produces an undefined symbol
3509 .sizeof.section_name. For all the output sections, we look for
3510 such symbols, and set them to the correct value. */
3513 lang_set_startof (void)
3517 if (link_info
.relocatable
)
3520 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3522 const char *secname
;
3524 struct bfd_link_hash_entry
*h
;
3526 secname
= bfd_get_section_name (output_bfd
, s
);
3527 buf
= xmalloc (10 + strlen (secname
));
3529 sprintf (buf
, ".startof.%s", secname
);
3530 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3531 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3533 h
->type
= bfd_link_hash_defined
;
3534 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3535 h
->u
.def
.section
= bfd_abs_section_ptr
;
3538 sprintf (buf
, ".sizeof.%s", secname
);
3539 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3540 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3542 h
->type
= bfd_link_hash_defined
;
3543 if (s
->_cooked_size
!= 0)
3544 h
->u
.def
.value
= TO_ADDR (s
->_cooked_size
);
3546 h
->u
.def
.value
= TO_ADDR (s
->_raw_size
);
3547 h
->u
.def
.section
= bfd_abs_section_ptr
;
3557 struct bfd_link_hash_entry
*h
;
3560 if (link_info
.relocatable
|| link_info
.shared
)
3565 if (entry_symbol
.name
== NULL
)
3567 /* No entry has been specified. Look for start, but don't warn
3568 if we don't find it. */
3569 entry_symbol
.name
= "start";
3573 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3574 FALSE
, FALSE
, TRUE
);
3576 && (h
->type
== bfd_link_hash_defined
3577 || h
->type
== bfd_link_hash_defweak
)
3578 && h
->u
.def
.section
->output_section
!= NULL
)
3582 val
= (h
->u
.def
.value
3583 + bfd_get_section_vma (output_bfd
,
3584 h
->u
.def
.section
->output_section
)
3585 + h
->u
.def
.section
->output_offset
);
3586 if (! bfd_set_start_address (output_bfd
, val
))
3587 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3594 /* We couldn't find the entry symbol. Try parsing it as a
3596 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3599 if (! bfd_set_start_address (output_bfd
, val
))
3600 einfo (_("%P%F: can't set start address\n"));
3606 /* Can't find the entry symbol, and it's not a number. Use
3607 the first address in the text section. */
3608 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3612 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3614 bfd_get_section_vma (output_bfd
, ts
));
3615 if (! bfd_set_start_address (output_bfd
,
3616 bfd_get_section_vma (output_bfd
,
3618 einfo (_("%P%F: can't set start address\n"));
3623 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3629 bfd_hash_table_free (&lang_definedness_table
);
3632 /* This is a small function used when we want to ignore errors from
3636 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3638 /* Don't do anything. */
3641 /* Check that the architecture of all the input files is compatible
3642 with the output file. Also call the backend to let it do any
3643 other checking that is needed. */
3648 lang_statement_union_type
*file
;
3650 const bfd_arch_info_type
*compatible
;
3652 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
3654 input_bfd
= file
->input_statement
.the_bfd
;
3655 compatible
= bfd_arch_get_compatible (input_bfd
, output_bfd
,
3656 command_line
.accept_unknown_input_arch
);
3658 /* In general it is not possible to perform a relocatable
3659 link between differing object formats when the input
3660 file has relocations, because the relocations in the
3661 input format may not have equivalent representations in
3662 the output format (and besides BFD does not translate
3663 relocs for other link purposes than a final link). */
3664 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
3665 && (compatible
== NULL
3666 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3667 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3669 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3670 bfd_get_target (input_bfd
), input_bfd
,
3671 bfd_get_target (output_bfd
), output_bfd
);
3672 /* einfo with %F exits. */
3675 if (compatible
== NULL
)
3677 if (command_line
.warn_mismatch
)
3678 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3679 bfd_printable_name (input_bfd
), input_bfd
,
3680 bfd_printable_name (output_bfd
));
3682 else if (bfd_count_sections (input_bfd
))
3684 /* If the input bfd has no contents, it shouldn't set the
3685 private data of the output bfd. */
3687 bfd_error_handler_type pfn
= NULL
;
3689 /* If we aren't supposed to warn about mismatched input
3690 files, temporarily set the BFD error handler to a
3691 function which will do nothing. We still want to call
3692 bfd_merge_private_bfd_data, since it may set up
3693 information which is needed in the output file. */
3694 if (! command_line
.warn_mismatch
)
3695 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3696 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3698 if (command_line
.warn_mismatch
)
3699 einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3702 if (! command_line
.warn_mismatch
)
3703 bfd_set_error_handler (pfn
);
3708 /* Look through all the global common symbols and attach them to the
3709 correct section. The -sort-common command line switch may be used
3710 to roughly sort the entries by size. */
3715 if (command_line
.inhibit_common_definition
)
3717 if (link_info
.relocatable
3718 && ! command_line
.force_common_definition
)
3721 if (! config
.sort_common
)
3722 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
3727 for (power
= 4; power
>= 0; power
--)
3728 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
3732 /* Place one common symbol in the correct section. */
3735 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
3737 unsigned int power_of_two
;
3741 if (h
->type
!= bfd_link_hash_common
)
3745 power_of_two
= h
->u
.c
.p
->alignment_power
;
3747 if (config
.sort_common
3748 && power_of_two
< (unsigned int) *(int *) info
)
3751 section
= h
->u
.c
.p
->section
;
3753 /* Increase the size of the section to align the common sym. */
3754 section
->_cooked_size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
3755 section
->_cooked_size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
3757 /* Adjust the alignment if necessary. */
3758 if (power_of_two
> section
->alignment_power
)
3759 section
->alignment_power
= power_of_two
;
3761 /* Change the symbol from common to defined. */
3762 h
->type
= bfd_link_hash_defined
;
3763 h
->u
.def
.section
= section
;
3764 h
->u
.def
.value
= section
->_cooked_size
;
3766 /* Increase the size of the section. */
3767 section
->_cooked_size
+= size
;
3769 /* Make sure the section is allocated in memory, and make sure that
3770 it is no longer a common section. */
3771 section
->flags
|= SEC_ALLOC
;
3772 section
->flags
&= ~SEC_IS_COMMON
;
3774 if (config
.map_file
!= NULL
)
3776 static bfd_boolean header_printed
;
3781 if (! header_printed
)
3783 minfo (_("\nAllocating common symbols\n"));
3784 minfo (_("Common symbol size file\n\n"));
3785 header_printed
= TRUE
;
3788 name
= demangle (h
->root
.string
);
3790 len
= strlen (name
);
3805 if (size
<= 0xffffffff)
3806 sprintf (buf
, "%lx", (unsigned long) size
);
3808 sprintf_vma (buf
, size
);
3818 minfo ("%B\n", section
->owner
);
3824 /* Run through the input files and ensure that every input section has
3825 somewhere to go. If one is found without a destination then create
3826 an input request and place it into the statement tree. */
3829 lang_place_orphans (void)
3831 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3835 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3837 if (s
->output_section
== NULL
)
3839 /* This section of the file is not attached, root
3840 around for a sensible place for it to go. */
3842 if (file
->just_syms_flag
)
3846 else if (strcmp (s
->name
, "COMMON") == 0)
3848 /* This is a lonely common section which must have
3849 come from an archive. We attach to the section
3850 with the wildcard. */
3851 if (! link_info
.relocatable
3852 || command_line
.force_common_definition
)
3854 if (default_common_section
== NULL
)
3857 /* This message happens when using the
3858 svr3.ifile linker script, so I have
3860 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3862 default_common_section
=
3863 lang_output_section_statement_lookup (".bss");
3866 lang_add_section (&default_common_section
->children
, s
,
3867 default_common_section
, file
);
3870 else if (ldemul_place_orphan (file
, s
))
3874 lang_output_section_statement_type
*os
;
3876 os
= lang_output_section_statement_lookup (s
->name
);
3877 lang_add_section (&os
->children
, s
, os
, file
);
3885 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
3887 flagword
*ptr_flags
;
3889 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3895 *ptr_flags
|= SEC_ALLOC
;
3899 *ptr_flags
|= SEC_READONLY
;
3903 *ptr_flags
|= SEC_DATA
;
3907 *ptr_flags
|= SEC_CODE
;
3912 *ptr_flags
|= SEC_LOAD
;
3916 einfo (_("%P%F: invalid syntax in flags\n"));
3923 /* Call a function on each input file. This function will be called
3924 on an archive, but not on the elements. */
3927 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
3929 lang_input_statement_type
*f
;
3931 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3933 f
= (lang_input_statement_type
*) f
->next_real_file
)
3937 /* Call a function on each file. The function will be called on all
3938 the elements of an archive which are included in the link, but will
3939 not be called on the archive file itself. */
3942 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
3944 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3951 ldlang_add_file (lang_input_statement_type
*entry
)
3955 lang_statement_append (&file_chain
,
3956 (lang_statement_union_type
*) entry
,
3959 /* The BFD linker needs to have a list of all input BFDs involved in
3961 ASSERT (entry
->the_bfd
->link_next
== NULL
);
3962 ASSERT (entry
->the_bfd
!= output_bfd
);
3963 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
3965 *pp
= entry
->the_bfd
;
3966 entry
->the_bfd
->usrdata
= entry
;
3967 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3969 /* Look through the sections and check for any which should not be
3970 included in the link. We need to do this now, so that we can
3971 notice when the backend linker tries to report multiple
3972 definition errors for symbols which are in sections we aren't
3973 going to link. FIXME: It might be better to entirely ignore
3974 symbols which are defined in sections which are going to be
3975 discarded. This would require modifying the backend linker for
3976 each backend which might set the SEC_LINK_ONCE flag. If we do
3977 this, we should probably handle SEC_EXCLUDE in the same way. */
3979 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
3983 lang_add_output (const char *name
, int from_script
)
3985 /* Make -o on command line override OUTPUT in script. */
3986 if (!had_output_filename
|| !from_script
)
3988 output_filename
= name
;
3989 had_output_filename
= TRUE
;
3993 static lang_output_section_statement_type
*current_section
;
4004 for (l
= 0; l
< 32; l
++)
4006 if (i
>= (unsigned int) x
)
4014 lang_output_section_statement_type
*
4015 lang_enter_output_section_statement (const char *output_section_statement_name
,
4016 etree_type
*address_exp
,
4017 enum section_type sectype
,
4019 etree_type
*subalign
,
4022 lang_output_section_statement_type
*os
;
4026 lang_output_section_statement_lookup (output_section_statement_name
);
4028 /* Add this statement to tree. */
4030 add_statement (lang_output_section_statement_enum
,
4031 output_section_statement
);
4033 /* Make next things chain into subchain of this. */
4035 if (os
->addr_tree
== NULL
)
4037 os
->addr_tree
= address_exp
;
4039 os
->sectype
= sectype
;
4040 if (sectype
!= noload_section
)
4041 os
->flags
= SEC_NO_FLAGS
;
4043 os
->flags
= SEC_NEVER_LOAD
;
4044 os
->block_value
= 1;
4045 stat_ptr
= &os
->children
;
4047 os
->subsection_alignment
=
4048 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4049 os
->section_alignment
=
4050 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4052 os
->load_base
= ebase
;
4059 lang_output_statement_type
*new =
4060 new_stat (lang_output_statement
, stat_ptr
);
4062 new->name
= output_filename
;
4065 /* Reset the current counters in the regions. */
4068 lang_reset_memory_regions (void)
4070 lang_memory_region_type
*p
= lang_memory_region_list
;
4073 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
4075 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4076 p
->current
= p
->origin
;
4079 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4083 /* If the wild pattern was marked KEEP, the member sections
4084 should be as well. */
4087 gc_section_callback (lang_wild_statement_type
*ptr
,
4088 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
4090 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
4091 void *data ATTRIBUTE_UNUSED
)
4093 if (ptr
->keep_sections
)
4094 section
->flags
|= SEC_KEEP
;
4097 /* Handle a wild statement, marking it against GC. */
4100 lang_gc_wild (lang_wild_statement_type
*s
)
4102 walk_wild (s
, gc_section_callback
, NULL
);
4105 /* Iterate over sections marking them against GC. */
4108 lang_gc_sections_1 (lang_statement_union_type
*s
)
4110 for (; s
!= NULL
; s
= s
->header
.next
)
4112 switch (s
->header
.type
)
4114 case lang_wild_statement_enum
:
4115 lang_gc_wild (&s
->wild_statement
);
4117 case lang_constructors_statement_enum
:
4118 lang_gc_sections_1 (constructor_list
.head
);
4120 case lang_output_section_statement_enum
:
4121 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4123 case lang_group_statement_enum
:
4124 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4133 lang_gc_sections (void)
4135 struct bfd_link_hash_entry
*h
;
4136 ldlang_undef_chain_list_type
*ulist
;
4138 /* Keep all sections so marked in the link script. */
4140 lang_gc_sections_1 (statement_list
.head
);
4142 /* Keep all sections containing symbols undefined on the command-line,
4143 and the section containing the entry symbol. */
4145 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4147 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4148 FALSE
, FALSE
, FALSE
);
4151 && (h
->type
== bfd_link_hash_defined
4152 || h
->type
== bfd_link_hash_defweak
)
4153 && ! bfd_is_abs_section (h
->u
.def
.section
))
4155 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4159 bfd_gc_sections (output_bfd
, &link_info
);
4165 lang_reasonable_defaults ();
4166 current_target
= default_target
;
4168 /* Open the output file. */
4169 lang_for_each_statement (ldlang_open_output
);
4172 ldemul_create_output_section_statements ();
4174 /* Add to the hash table all undefineds on the command line. */
4175 lang_place_undefineds ();
4177 already_linked_table_init ();
4179 /* Create a bfd for each input file. */
4180 current_target
= default_target
;
4181 open_input_bfds (statement_list
.head
, FALSE
);
4183 link_info
.gc_sym_list
= &entry_symbol
;
4184 if (entry_symbol
.name
== NULL
)
4185 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4187 ldemul_after_open ();
4189 already_linked_table_free ();
4191 /* Make sure that we're not mixing architectures. We call this
4192 after all the input files have been opened, but before we do any
4193 other processing, so that any operations merge_private_bfd_data
4194 does on the output file will be known during the rest of the
4198 /* Handle .exports instead of a version script if we're told to do so. */
4199 if (command_line
.version_exports_section
)
4200 lang_do_version_exports_section ();
4202 /* Build all sets based on the information gathered from the input
4204 ldctor_build_sets ();
4206 /* Remove unreferenced sections if asked to. */
4207 if (command_line
.gc_sections
)
4208 lang_gc_sections ();
4210 /* If there were any SEC_MERGE sections, finish their merging, so that
4211 section sizes can be computed. This has to be done after GC of sections,
4212 so that GCed sections are not merged, but before assigning output
4213 sections, since removing whole input sections is hard then. */
4214 bfd_merge_sections (output_bfd
, &link_info
);
4216 /* Size up the common data. */
4219 /* Run through the contours of the script and attach input sections
4220 to the correct output sections. */
4221 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
4223 /* Find any sections not attached explicitly and handle them. */
4224 lang_place_orphans ();
4226 if (! link_info
.relocatable
)
4228 /* Look for a text section and set the readonly attribute in it. */
4229 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
4233 if (config
.text_read_only
)
4234 found
->flags
|= SEC_READONLY
;
4236 found
->flags
&= ~SEC_READONLY
;
4240 /* Do anything special before sizing sections. This is where ELF
4241 and other back-ends size dynamic sections. */
4242 ldemul_before_allocation ();
4244 if (!link_info
.relocatable
)
4245 strip_excluded_output_sections ();
4247 /* We must record the program headers before we try to fix the
4248 section positions, since they will affect SIZEOF_HEADERS. */
4249 lang_record_phdrs ();
4251 /* Size up the sections. */
4252 lang_size_sections (statement_list
.head
, abs_output_section
,
4253 &statement_list
.head
, 0, 0, NULL
,
4254 command_line
.relax
? FALSE
: TRUE
);
4256 /* Now run around and relax if we can. */
4257 if (command_line
.relax
)
4259 /* Keep relaxing until bfd_relax_section gives up. */
4260 bfd_boolean relax_again
;
4264 relax_again
= FALSE
;
4266 /* Note: pe-dll.c does something like this also. If you find
4267 you need to change this code, you probably need to change
4268 pe-dll.c also. DJ */
4270 /* Do all the assignments with our current guesses as to
4272 lang_do_assignments (statement_list
.head
, abs_output_section
,
4275 /* We must do this after lang_do_assignments, because it uses
4277 lang_reset_memory_regions ();
4279 /* Perform another relax pass - this time we know where the
4280 globals are, so can make a better guess. */
4281 lang_size_sections (statement_list
.head
, abs_output_section
,
4282 &statement_list
.head
, 0, 0, &relax_again
, FALSE
);
4284 /* If the normal relax is done and the relax finalize pass
4285 is not performed yet, we perform another relax pass. */
4286 if (!relax_again
&& link_info
.need_relax_finalize
)
4288 link_info
.need_relax_finalize
= FALSE
;
4292 while (relax_again
);
4294 /* Final extra sizing to report errors. */
4295 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4296 lang_reset_memory_regions ();
4297 lang_size_sections (statement_list
.head
, abs_output_section
,
4298 &statement_list
.head
, 0, 0, NULL
, TRUE
);
4301 /* See if anything special should be done now we know how big
4303 ldemul_after_allocation ();
4305 /* Fix any .startof. or .sizeof. symbols. */
4306 lang_set_startof ();
4308 /* Do all the assignments, now that we know the final resting places
4309 of all the symbols. */
4311 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4313 /* Make sure that the section addresses make sense. */
4314 if (! link_info
.relocatable
4315 && command_line
.check_section_addresses
)
4316 lang_check_section_addresses ();
4324 /* EXPORTED TO YACC */
4327 lang_add_wild (struct wildcard_spec
*filespec
,
4328 struct wildcard_list
*section_list
,
4329 bfd_boolean keep_sections
)
4331 struct wildcard_list
*curr
, *next
;
4332 lang_wild_statement_type
*new;
4334 /* Reverse the list as the parser puts it back to front. */
4335 for (curr
= section_list
, section_list
= NULL
;
4337 section_list
= curr
, curr
= next
)
4339 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4340 placed_commons
= TRUE
;
4343 curr
->next
= section_list
;
4346 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4348 if (strcmp (filespec
->name
, "*") == 0)
4349 filespec
->name
= NULL
;
4350 else if (! wildcardp (filespec
->name
))
4351 lang_has_input_file
= TRUE
;
4354 new = new_stat (lang_wild_statement
, stat_ptr
);
4355 new->filename
= NULL
;
4356 new->filenames_sorted
= FALSE
;
4357 if (filespec
!= NULL
)
4359 new->filename
= filespec
->name
;
4360 new->filenames_sorted
= filespec
->sorted
;
4362 new->section_list
= section_list
;
4363 new->keep_sections
= keep_sections
;
4364 lang_list_init (&new->children
);
4368 lang_section_start (const char *name
, etree_type
*address
)
4370 lang_address_statement_type
*ad
;
4372 ad
= new_stat (lang_address_statement
, stat_ptr
);
4373 ad
->section_name
= name
;
4374 ad
->address
= address
;
4377 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4378 because of a -e argument on the command line, or zero if this is
4379 called by ENTRY in a linker script. Command line arguments take
4383 lang_add_entry (const char *name
, bfd_boolean cmdline
)
4385 if (entry_symbol
.name
== NULL
4387 || ! entry_from_cmdline
)
4389 entry_symbol
.name
= name
;
4390 entry_from_cmdline
= cmdline
;
4395 lang_add_target (const char *name
)
4397 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4405 lang_add_map (const char *name
)
4412 map_option_f
= TRUE
;
4420 lang_add_fill (fill_type
*fill
)
4422 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4429 lang_add_data (int type
, union etree_union
*exp
)
4432 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4440 /* Create a new reloc statement. RELOC is the BFD relocation type to
4441 generate. HOWTO is the corresponding howto structure (we could
4442 look this up, but the caller has already done so). SECTION is the
4443 section to generate a reloc against, or NAME is the name of the
4444 symbol to generate a reloc against. Exactly one of SECTION and
4445 NAME must be NULL. ADDEND is an expression for the addend. */
4448 lang_add_reloc (bfd_reloc_code_real_type reloc
,
4449 reloc_howto_type
*howto
,
4452 union etree_union
*addend
)
4454 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4458 p
->section
= section
;
4460 p
->addend_exp
= addend
;
4462 p
->addend_value
= 0;
4463 p
->output_section
= NULL
;
4467 lang_assignment_statement_type
*
4468 lang_add_assignment (etree_type
*exp
)
4470 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4478 lang_add_attribute (enum statement_enum attribute
)
4480 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4484 lang_startup (const char *name
)
4486 if (startup_file
!= NULL
)
4488 einfo (_("%P%Fmultiple STARTUP files\n"));
4490 first_file
->filename
= name
;
4491 first_file
->local_sym_name
= name
;
4492 first_file
->real
= TRUE
;
4494 startup_file
= name
;
4498 lang_float (bfd_boolean maybe
)
4500 lang_float_flag
= maybe
;
4504 /* Work out the load- and run-time regions from a script statement, and
4505 store them in *LMA_REGION and *REGION respectively.
4507 MEMSPEC is the name of the run-time region, or the value of
4508 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4509 LMA_MEMSPEC is the name of the load-time region, or null if the
4510 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4511 had an explicit load address.
4513 It is an error to specify both a load region and a load address. */
4516 lang_get_regions (lang_memory_region_type
**region
,
4517 lang_memory_region_type
**lma_region
,
4518 const char *memspec
,
4519 const char *lma_memspec
,
4520 bfd_boolean have_lma
,
4521 bfd_boolean have_vma
)
4523 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
4525 /* If no runtime region or VMA has been specified, but the load region has
4526 been specified, then use the load region for the runtime region as well. */
4527 if (lma_memspec
!= NULL
4529 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
4530 *region
= *lma_region
;
4532 *region
= lang_memory_region_lookup (memspec
, FALSE
);
4534 if (have_lma
&& lma_memspec
!= 0)
4535 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4539 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
4540 lang_output_section_phdr_list
*phdrs
,
4541 const char *lma_memspec
)
4543 lang_get_regions (¤t_section
->region
,
4544 ¤t_section
->lma_region
,
4545 memspec
, lma_memspec
,
4546 current_section
->load_base
!= NULL
,
4547 current_section
->addr_tree
!= NULL
);
4548 current_section
->fill
= fill
;
4549 current_section
->phdrs
= phdrs
;
4550 stat_ptr
= &statement_list
;
4553 /* Create an absolute symbol with the given name with the value of the
4554 address of first byte of the section named.
4556 If the symbol already exists, then do nothing. */
4559 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
4561 struct bfd_link_hash_entry
*h
;
4563 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4565 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4567 if (h
->type
== bfd_link_hash_new
4568 || h
->type
== bfd_link_hash_undefined
)
4572 h
->type
= bfd_link_hash_defined
;
4574 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4578 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4580 h
->u
.def
.section
= bfd_abs_section_ptr
;
4584 /* Create an absolute symbol with the given name with the value of the
4585 address of the first byte after the end of the section named.
4587 If the symbol already exists, then do nothing. */
4590 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
4592 struct bfd_link_hash_entry
*h
;
4594 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4596 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4598 if (h
->type
== bfd_link_hash_new
4599 || h
->type
== bfd_link_hash_undefined
)
4603 h
->type
= bfd_link_hash_defined
;
4605 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4609 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4610 + TO_ADDR (bfd_section_size (output_bfd
, sec
)));
4612 h
->u
.def
.section
= bfd_abs_section_ptr
;
4617 lang_statement_append (lang_statement_list_type
*list
,
4618 lang_statement_union_type
*element
,
4619 lang_statement_union_type
**field
)
4621 *(list
->tail
) = element
;
4625 /* Set the output format type. -oformat overrides scripts. */
4628 lang_add_output_format (const char *format
,
4633 if (output_target
== NULL
|| !from_script
)
4635 if (command_line
.endian
== ENDIAN_BIG
4638 else if (command_line
.endian
== ENDIAN_LITTLE
4642 output_target
= format
;
4646 /* Enter a group. This creates a new lang_group_statement, and sets
4647 stat_ptr to build new statements within the group. */
4650 lang_enter_group (void)
4652 lang_group_statement_type
*g
;
4654 g
= new_stat (lang_group_statement
, stat_ptr
);
4655 lang_list_init (&g
->children
);
4656 stat_ptr
= &g
->children
;
4659 /* Leave a group. This just resets stat_ptr to start writing to the
4660 regular list of statements again. Note that this will not work if
4661 groups can occur inside anything else which can adjust stat_ptr,
4662 but currently they can't. */
4665 lang_leave_group (void)
4667 stat_ptr
= &statement_list
;
4670 /* Add a new program header. This is called for each entry in a PHDRS
4671 command in a linker script. */
4674 lang_new_phdr (const char *name
,
4676 bfd_boolean filehdr
,
4681 struct lang_phdr
*n
, **pp
;
4683 n
= stat_alloc (sizeof (struct lang_phdr
));
4686 n
->type
= exp_get_value_int (type
, 0, "program header type",
4687 lang_final_phase_enum
);
4688 n
->filehdr
= filehdr
;
4693 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4698 /* Record the program header information in the output BFD. FIXME: We
4699 should not be calling an ELF specific function here. */
4702 lang_record_phdrs (void)
4706 lang_output_section_phdr_list
*last
;
4707 struct lang_phdr
*l
;
4708 lang_statement_union_type
*u
;
4711 secs
= xmalloc (alc
* sizeof (asection
*));
4713 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4720 for (u
= lang_output_section_statement
.head
;
4722 u
= u
->output_section_statement
.next
)
4724 lang_output_section_statement_type
*os
;
4725 lang_output_section_phdr_list
*pl
;
4727 os
= &u
->output_section_statement
;
4734 if (os
->sectype
== noload_section
4735 || os
->bfd_section
== NULL
4736 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4741 if (os
->bfd_section
== NULL
)
4744 for (; pl
!= NULL
; pl
= pl
->next
)
4746 if (strcmp (pl
->name
, l
->name
) == 0)
4751 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
4753 secs
[c
] = os
->bfd_section
;
4760 if (l
->flags
== NULL
)
4763 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4764 lang_final_phase_enum
);
4769 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4770 lang_final_phase_enum
);
4772 if (! bfd_record_phdr (output_bfd
, l
->type
,
4773 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4774 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4775 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4780 /* Make sure all the phdr assignments succeeded. */
4781 for (u
= lang_output_section_statement
.head
;
4783 u
= u
->output_section_statement
.next
)
4785 lang_output_section_phdr_list
*pl
;
4787 if (u
->output_section_statement
.bfd_section
== NULL
)
4790 for (pl
= u
->output_section_statement
.phdrs
;
4793 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4794 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4795 u
->output_section_statement
.name
, pl
->name
);
4799 /* Record a list of sections which may not be cross referenced. */
4802 lang_add_nocrossref (lang_nocrossref_type
*l
)
4804 struct lang_nocrossrefs
*n
;
4806 n
= xmalloc (sizeof *n
);
4807 n
->next
= nocrossref_list
;
4809 nocrossref_list
= n
;
4811 /* Set notice_all so that we get informed about all symbols. */
4812 link_info
.notice_all
= TRUE
;
4815 /* Overlay handling. We handle overlays with some static variables. */
4817 /* The overlay virtual address. */
4818 static etree_type
*overlay_vma
;
4819 /* And subsection alignment. */
4820 static etree_type
*overlay_subalign
;
4822 /* An expression for the maximum section size seen so far. */
4823 static etree_type
*overlay_max
;
4825 /* A list of all the sections in this overlay. */
4827 struct overlay_list
{
4828 struct overlay_list
*next
;
4829 lang_output_section_statement_type
*os
;
4832 static struct overlay_list
*overlay_list
;
4834 /* Start handling an overlay. */
4837 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
4839 /* The grammar should prevent nested overlays from occurring. */
4840 ASSERT (overlay_vma
== NULL
4841 && overlay_subalign
== NULL
4842 && overlay_max
== NULL
);
4844 overlay_vma
= vma_expr
;
4845 overlay_subalign
= subalign
;
4848 /* Start a section in an overlay. We handle this by calling
4849 lang_enter_output_section_statement with the correct VMA.
4850 lang_leave_overlay sets up the LMA and memory regions. */
4853 lang_enter_overlay_section (const char *name
)
4855 struct overlay_list
*n
;
4858 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4859 0, overlay_subalign
, 0);
4861 /* If this is the first section, then base the VMA of future
4862 sections on this one. This will work correctly even if `.' is
4863 used in the addresses. */
4864 if (overlay_list
== NULL
)
4865 overlay_vma
= exp_nameop (ADDR
, name
);
4867 /* Remember the section. */
4868 n
= xmalloc (sizeof *n
);
4869 n
->os
= current_section
;
4870 n
->next
= overlay_list
;
4873 size
= exp_nameop (SIZEOF
, name
);
4875 /* Arrange to work out the maximum section end address. */
4876 if (overlay_max
== NULL
)
4879 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4882 /* Finish a section in an overlay. There isn't any special to do
4886 lang_leave_overlay_section (fill_type
*fill
,
4887 lang_output_section_phdr_list
*phdrs
)
4894 name
= current_section
->name
;
4896 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4897 region and that no load-time region has been specified. It doesn't
4898 really matter what we say here, since lang_leave_overlay will
4900 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
4902 /* Define the magic symbols. */
4904 clean
= xmalloc (strlen (name
) + 1);
4906 for (s1
= name
; *s1
!= '\0'; s1
++)
4907 if (ISALNUM (*s1
) || *s1
== '_')
4911 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4912 sprintf (buf
, "__load_start_%s", clean
);
4913 lang_add_assignment (exp_assop ('=', buf
,
4914 exp_nameop (LOADADDR
, name
)));
4916 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4917 sprintf (buf
, "__load_stop_%s", clean
);
4918 lang_add_assignment (exp_assop ('=', buf
,
4920 exp_nameop (LOADADDR
, name
),
4921 exp_nameop (SIZEOF
, name
))));
4926 /* Finish an overlay. If there are any overlay wide settings, this
4927 looks through all the sections in the overlay and sets them. */
4930 lang_leave_overlay (etree_type
*lma_expr
,
4933 const char *memspec
,
4934 lang_output_section_phdr_list
*phdrs
,
4935 const char *lma_memspec
)
4937 lang_memory_region_type
*region
;
4938 lang_memory_region_type
*lma_region
;
4939 struct overlay_list
*l
;
4940 lang_nocrossref_type
*nocrossref
;
4942 lang_get_regions (®ion
, &lma_region
,
4943 memspec
, lma_memspec
,
4944 lma_expr
!= NULL
, FALSE
);
4948 /* After setting the size of the last section, set '.' to end of the
4950 if (overlay_list
!= NULL
)
4951 overlay_list
->os
->update_dot_tree
4952 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
4957 struct overlay_list
*next
;
4959 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
4962 l
->os
->region
= region
;
4963 l
->os
->lma_region
= lma_region
;
4965 /* The first section has the load address specified in the
4966 OVERLAY statement. The rest are worked out from that.
4967 The base address is not needed (and should be null) if
4968 an LMA region was specified. */
4970 l
->os
->load_base
= lma_expr
;
4971 else if (lma_region
== 0)
4972 l
->os
->load_base
= exp_binop ('+',
4973 exp_nameop (LOADADDR
, l
->next
->os
->name
),
4974 exp_nameop (SIZEOF
, l
->next
->os
->name
));
4976 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4977 l
->os
->phdrs
= phdrs
;
4981 lang_nocrossref_type
*nc
;
4983 nc
= xmalloc (sizeof *nc
);
4984 nc
->name
= l
->os
->name
;
4985 nc
->next
= nocrossref
;
4994 if (nocrossref
!= NULL
)
4995 lang_add_nocrossref (nocrossref
);
4998 overlay_list
= NULL
;
5002 /* Version handling. This is only useful for ELF. */
5004 /* This global variable holds the version tree that we build. */
5006 struct bfd_elf_version_tree
*lang_elf_version_info
;
5008 /* If PREV is NULL, return first version pattern matching particular symbol.
5009 If PREV is non-NULL, return first version pattern matching particular
5010 symbol after PREV (previously returned by lang_vers_match). */
5012 static struct bfd_elf_version_expr
*
5013 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
5014 struct bfd_elf_version_expr
*prev
,
5017 const char *cxx_sym
= sym
;
5018 const char *java_sym
= sym
;
5019 struct bfd_elf_version_expr
*expr
= NULL
;
5021 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5023 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
5027 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5029 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5034 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
5036 struct bfd_elf_version_expr e
;
5038 switch (prev
? prev
->mask
: 0)
5041 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
5044 expr
= htab_find (head
->htab
, &e
);
5045 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
5046 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
5052 case BFD_ELF_VERSION_C_TYPE
:
5053 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5056 expr
= htab_find (head
->htab
, &e
);
5057 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
5058 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5064 case BFD_ELF_VERSION_CXX_TYPE
:
5065 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5067 e
.symbol
= java_sym
;
5068 expr
= htab_find (head
->htab
, &e
);
5069 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
5070 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5081 /* Finally, try the wildcards. */
5082 if (prev
== NULL
|| prev
->symbol
)
5083 expr
= head
->remaining
;
5090 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5093 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5095 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5099 if (fnmatch (expr
->pattern
, s
, 0) == 0)
5106 free ((char *) cxx_sym
);
5107 if (java_sym
!= sym
)
5108 free ((char *) java_sym
);
5112 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5113 return a string pointing to the symbol name. */
5116 realsymbol (const char *pattern
)
5119 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
5120 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
5122 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
5124 /* It is a glob pattern only if there is no preceding
5126 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
5134 /* Remove the preceding backslash. */
5141 backslash
= *p
== '\\';
5156 /* This is called for each variable name or match expression. */
5158 struct bfd_elf_version_expr
*
5159 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
5163 struct bfd_elf_version_expr
*ret
;
5165 ret
= xmalloc (sizeof *ret
);
5170 ret
->symbol
= realsymbol (new);
5172 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5173 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5174 else if (strcasecmp (lang
, "C++") == 0)
5175 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
5176 else if (strcasecmp (lang
, "Java") == 0)
5177 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
5180 einfo (_("%X%P: unknown language `%s' in version information\n"),
5182 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5185 return ldemul_new_vers_pattern (ret
);
5188 /* This is called for each set of variable names and match
5191 struct bfd_elf_version_tree
*
5192 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
5193 struct bfd_elf_version_expr
*locals
)
5195 struct bfd_elf_version_tree
*ret
;
5197 ret
= xcalloc (1, sizeof *ret
);
5198 ret
->globals
.list
= globals
;
5199 ret
->locals
.list
= locals
;
5200 ret
->match
= lang_vers_match
;
5201 ret
->name_indx
= (unsigned int) -1;
5205 /* This static variable keeps track of version indices. */
5207 static int version_index
;
5210 version_expr_head_hash (const void *p
)
5212 const struct bfd_elf_version_expr
*e
= p
;
5214 return htab_hash_string (e
->symbol
);
5218 version_expr_head_eq (const void *p1
, const void *p2
)
5220 const struct bfd_elf_version_expr
*e1
= p1
;
5221 const struct bfd_elf_version_expr
*e2
= p2
;
5223 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
5227 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
5230 struct bfd_elf_version_expr
*e
, *next
;
5231 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
5233 for (e
= head
->list
; e
; e
= e
->next
)
5237 head
->mask
|= e
->mask
;
5242 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
5243 version_expr_head_eq
, NULL
);
5244 list_loc
= &head
->list
;
5245 remaining_loc
= &head
->remaining
;
5246 for (e
= head
->list
; e
; e
= next
)
5252 remaining_loc
= &e
->next
;
5256 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
5260 struct bfd_elf_version_expr
*e1
, *last
;
5266 if (e1
->mask
== e
->mask
)
5274 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
5278 /* This is a duplicate. */
5279 /* FIXME: Memory leak. Sometimes pattern is not
5280 xmalloced alone, but in larger chunk of memory. */
5281 /* free (e->symbol); */
5286 e
->next
= last
->next
;
5294 list_loc
= &e
->next
;
5298 *remaining_loc
= NULL
;
5299 *list_loc
= head
->remaining
;
5302 head
->remaining
= head
->list
;
5305 /* This is called when we know the name and dependencies of the
5309 lang_register_vers_node (const char *name
,
5310 struct bfd_elf_version_tree
*version
,
5311 struct bfd_elf_version_deps
*deps
)
5313 struct bfd_elf_version_tree
*t
, **pp
;
5314 struct bfd_elf_version_expr
*e1
;
5319 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5320 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5322 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5327 /* Make sure this node has a unique name. */
5328 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5329 if (strcmp (t
->name
, name
) == 0)
5330 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5332 lang_finalize_version_expr_head (&version
->globals
);
5333 lang_finalize_version_expr_head (&version
->locals
);
5335 /* Check the global and local match names, and make sure there
5336 aren't any duplicates. */
5338 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
5340 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5342 struct bfd_elf_version_expr
*e2
;
5344 if (t
->locals
.htab
&& e1
->symbol
)
5346 e2
= htab_find (t
->locals
.htab
, e1
);
5347 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5349 if (e1
->mask
== e2
->mask
)
5350 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5355 else if (!e1
->symbol
)
5356 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5357 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5358 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5363 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
5365 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5367 struct bfd_elf_version_expr
*e2
;
5369 if (t
->globals
.htab
&& e1
->symbol
)
5371 e2
= htab_find (t
->globals
.htab
, e1
);
5372 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5374 if (e1
->mask
== e2
->mask
)
5375 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5380 else if (!e1
->symbol
)
5381 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5382 if (strcmp (e1
->pattern
, e2
->pattern
) == 0 && e1
->mask
== e2
->mask
)
5383 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5388 version
->deps
= deps
;
5389 version
->name
= name
;
5390 if (name
[0] != '\0')
5393 version
->vernum
= version_index
;
5396 version
->vernum
= 0;
5398 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5403 /* This is called when we see a version dependency. */
5405 struct bfd_elf_version_deps
*
5406 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
5408 struct bfd_elf_version_deps
*ret
;
5409 struct bfd_elf_version_tree
*t
;
5411 ret
= xmalloc (sizeof *ret
);
5414 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5416 if (strcmp (t
->name
, name
) == 0)
5418 ret
->version_needed
= t
;
5423 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5429 lang_do_version_exports_section (void)
5431 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5433 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5435 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5442 len
= bfd_section_size (is
->the_bfd
, sec
);
5443 contents
= xmalloc (len
);
5444 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5445 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5448 while (p
< contents
+ len
)
5450 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5451 p
= strchr (p
, '\0') + 1;
5454 /* Do not free the contents, as we used them creating the regex. */
5456 /* Do not include this section in the link. */
5457 bfd_set_section_flags (is
->the_bfd
, sec
,
5458 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5461 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5462 lang_register_vers_node (command_line
.version_exports_section
,
5463 lang_new_vers_node (greg
, lreg
), NULL
);
5467 lang_add_unique (const char *name
)
5469 struct unique_sections
*ent
;
5471 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5472 if (strcmp (ent
->name
, name
) == 0)
5475 ent
= xmalloc (sizeof *ent
);
5476 ent
->name
= xstrdup (name
);
5477 ent
->next
= unique_section_list
;
5478 unique_section_list
= ent
;