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
;
50 static struct obstack map_obstack
;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static const char *startup_file
;
55 static lang_statement_list_type input_file_chain
;
56 static bfd_boolean placed_commons
= FALSE
;
57 static lang_output_section_statement_type
*default_common_section
;
58 static bfd_boolean map_option_f
;
59 static bfd_vma print_dot
;
60 static lang_input_statement_type
*first_file
;
61 static const char *current_target
;
62 static const char *output_target
;
63 static lang_statement_list_type statement_list
;
64 static struct lang_phdr
*lang_phdr_list
;
65 static struct bfd_hash_table lang_definedness_table
;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type
*);
69 static void init_map_userdata (bfd
*, asection
*, void *);
70 static lang_input_statement_type
*lookup_name (const char *);
71 static bfd_boolean
load_symbols (lang_input_statement_type
*,
72 lang_statement_list_type
*);
73 static struct bfd_hash_entry
*lang_definedness_newfunc
74 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection
*);
77 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
78 static void print_statement (lang_statement_union_type
*,
79 lang_output_section_statement_type
*);
80 static void print_statement_list (lang_statement_union_type
*,
81 lang_output_section_statement_type
*);
82 static void print_statements (void);
83 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 typedef void (*callback_t
) (lang_wild_statement_type
*, struct wildcard_list
*,
88 asection
*, lang_input_statement_type
*, void *);
90 /* Exported variables. */
91 lang_output_section_statement_type
*abs_output_section
;
92 lang_statement_list_type lang_output_section_statement
;
93 lang_statement_list_type
*stat_ptr
= &statement_list
;
94 lang_statement_list_type file_chain
= { NULL
, NULL
};
95 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
96 const char *entry_section
= ".text";
97 bfd_boolean entry_from_cmdline
;
98 bfd_boolean lang_has_input_file
= FALSE
;
99 bfd_boolean had_output_filename
= FALSE
;
100 bfd_boolean lang_float_flag
= FALSE
;
101 bfd_boolean delete_output_file_on_failure
= FALSE
;
102 struct lang_nocrossrefs
*nocrossref_list
;
103 struct unique_sections
*unique_section_list
;
104 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
105 int lang_statement_iteration
= 0;
107 etree_type
*base
; /* Relocation base - or null */
109 /* Return TRUE if the PATTERN argument is a wildcard pattern.
110 Although backslashes are treated specially if a pattern contains
111 wildcards, we do not consider the mere presence of a backslash to
112 be enough to cause the pattern to be treated as a wildcard.
113 That lets us handle DOS filenames more naturally. */
114 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
116 #define new_stat(x, y) \
117 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
119 #define outside_section_address(q) \
120 ((q)->output_offset + (q)->output_section->vma)
122 #define outside_symbol_address(q) \
123 ((q)->value + outside_section_address (q->section))
125 #define SECTION_NAME_MAP_LENGTH (16)
128 stat_alloc (size_t size
)
130 return obstack_alloc (&stat_obstack
, size
);
134 unique_section_p (const asection
*sec
)
136 struct unique_sections
*unam
;
139 if (link_info
.relocatable
140 && sec
->owner
!= NULL
141 && bfd_is_group_section (sec
->owner
, sec
))
145 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
146 if (wildcardp (unam
->name
)
147 ? fnmatch (unam
->name
, secnam
, 0) == 0
148 : strcmp (unam
->name
, secnam
) == 0)
156 /* Generic traversal routines for finding matching sections. */
159 walk_wild_section (lang_wild_statement_type
*ptr
,
160 lang_input_statement_type
*file
,
166 if (file
->just_syms_flag
)
169 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
171 struct wildcard_list
*sec
;
173 sec
= ptr
->section_list
;
175 (*callback
) (ptr
, sec
, s
, file
, data
);
179 bfd_boolean skip
= FALSE
;
180 struct name_list
*list_tmp
;
182 /* Don't process sections from files which were
184 for (list_tmp
= sec
->spec
.exclude_name_list
;
186 list_tmp
= list_tmp
->next
)
188 if (wildcardp (list_tmp
->name
))
189 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
191 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
193 /* If this file is part of an archive, and the archive is
194 excluded, exclude this file. */
195 if (! skip
&& file
->the_bfd
!= NULL
196 && file
->the_bfd
->my_archive
!= NULL
197 && file
->the_bfd
->my_archive
->filename
!= NULL
)
199 if (wildcardp (list_tmp
->name
))
200 skip
= fnmatch (list_tmp
->name
,
201 file
->the_bfd
->my_archive
->filename
,
204 skip
= strcmp (list_tmp
->name
,
205 file
->the_bfd
->my_archive
->filename
) == 0;
212 if (!skip
&& sec
->spec
.name
!= NULL
)
214 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
216 if (wildcardp (sec
->spec
.name
))
217 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
219 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
223 (*callback
) (ptr
, sec
, s
, file
, data
);
230 /* Handle a wild statement for a single file F. */
233 walk_wild_file (lang_wild_statement_type
*s
,
234 lang_input_statement_type
*f
,
238 if (f
->the_bfd
== NULL
239 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
240 walk_wild_section (s
, f
, callback
, data
);
245 /* This is an archive file. We must map each member of the
246 archive separately. */
247 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
248 while (member
!= NULL
)
250 /* When lookup_name is called, it will call the add_symbols
251 entry point for the archive. For each element of the
252 archive which is included, BFD will call ldlang_add_file,
253 which will set the usrdata field of the member to the
254 lang_input_statement. */
255 if (member
->usrdata
!= NULL
)
257 walk_wild_section (s
, member
->usrdata
, callback
, data
);
260 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
266 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
268 const char *file_spec
= s
->filename
;
270 if (file_spec
== NULL
)
272 /* Perform the iteration over all files in the list. */
273 LANG_FOR_EACH_INPUT_STATEMENT (f
)
275 walk_wild_file (s
, f
, callback
, data
);
278 else if (wildcardp (file_spec
))
280 LANG_FOR_EACH_INPUT_STATEMENT (f
)
282 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
283 walk_wild_file (s
, f
, callback
, data
);
288 lang_input_statement_type
*f
;
290 /* Perform the iteration over a single file. */
291 f
= lookup_name (file_spec
);
293 walk_wild_file (s
, f
, callback
, data
);
297 /* lang_for_each_statement walks the parse tree and calls the provided
298 function for each node. */
301 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
302 lang_statement_union_type
*s
)
304 for (; s
!= NULL
; s
= s
->header
.next
)
308 switch (s
->header
.type
)
310 case lang_constructors_statement_enum
:
311 lang_for_each_statement_worker (func
, constructor_list
.head
);
313 case lang_output_section_statement_enum
:
314 lang_for_each_statement_worker
315 (func
, s
->output_section_statement
.children
.head
);
317 case lang_wild_statement_enum
:
318 lang_for_each_statement_worker (func
,
319 s
->wild_statement
.children
.head
);
321 case lang_group_statement_enum
:
322 lang_for_each_statement_worker (func
,
323 s
->group_statement
.children
.head
);
325 case lang_data_statement_enum
:
326 case lang_reloc_statement_enum
:
327 case lang_object_symbols_statement_enum
:
328 case lang_output_statement_enum
:
329 case lang_target_statement_enum
:
330 case lang_input_section_enum
:
331 case lang_input_statement_enum
:
332 case lang_assignment_statement_enum
:
333 case lang_padding_statement_enum
:
334 case lang_address_statement_enum
:
335 case lang_fill_statement_enum
:
345 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
347 lang_for_each_statement_worker (func
, statement_list
.head
);
350 /*----------------------------------------------------------------------*/
353 lang_list_init (lang_statement_list_type
*list
)
356 list
->tail
= &list
->head
;
359 /* Build a new statement node for the parse tree. */
361 static lang_statement_union_type
*
362 new_statement (enum statement_enum type
,
364 lang_statement_list_type
*list
)
366 lang_statement_union_type
*new;
368 new = stat_alloc (size
);
369 new->header
.type
= type
;
370 new->header
.next
= NULL
;
371 lang_statement_append (list
, new, &new->header
.next
);
375 /* Build a new input file node for the language. There are several
376 ways in which we treat an input file, eg, we only look at symbols,
377 or prefix it with a -l etc.
379 We can be supplied with requests for input files more than once;
380 they may, for example be split over several lines like foo.o(.text)
381 foo.o(.data) etc, so when asked for a file we check that we haven't
382 got it already so we don't duplicate the bfd. */
384 static lang_input_statement_type
*
385 new_afile (const char *name
,
386 lang_input_file_enum_type file_type
,
388 bfd_boolean add_to_list
)
390 lang_input_statement_type
*p
;
393 p
= new_stat (lang_input_statement
, stat_ptr
);
396 p
= stat_alloc (sizeof (lang_input_statement_type
));
397 p
->header
.next
= NULL
;
400 lang_has_input_file
= TRUE
;
402 p
->sysrooted
= FALSE
;
405 case lang_input_file_is_symbols_only_enum
:
407 p
->is_archive
= FALSE
;
409 p
->local_sym_name
= name
;
410 p
->just_syms_flag
= TRUE
;
411 p
->search_dirs_flag
= FALSE
;
413 case lang_input_file_is_fake_enum
:
415 p
->is_archive
= FALSE
;
417 p
->local_sym_name
= name
;
418 p
->just_syms_flag
= FALSE
;
419 p
->search_dirs_flag
= FALSE
;
421 case lang_input_file_is_l_enum
:
422 p
->is_archive
= TRUE
;
425 p
->local_sym_name
= concat ("-l", name
, NULL
);
426 p
->just_syms_flag
= FALSE
;
427 p
->search_dirs_flag
= TRUE
;
429 case lang_input_file_is_marker_enum
:
431 p
->is_archive
= FALSE
;
433 p
->local_sym_name
= name
;
434 p
->just_syms_flag
= FALSE
;
435 p
->search_dirs_flag
= TRUE
;
437 case lang_input_file_is_search_file_enum
:
438 p
->sysrooted
= ldlang_sysrooted_script
;
440 p
->is_archive
= FALSE
;
442 p
->local_sym_name
= name
;
443 p
->just_syms_flag
= FALSE
;
444 p
->search_dirs_flag
= TRUE
;
446 case lang_input_file_is_file_enum
:
448 p
->is_archive
= FALSE
;
450 p
->local_sym_name
= name
;
451 p
->just_syms_flag
= FALSE
;
452 p
->search_dirs_flag
= FALSE
;
459 p
->next_real_file
= NULL
;
462 p
->dynamic
= config
.dynamic_link
;
463 p
->add_needed
= add_needed
;
464 p
->as_needed
= as_needed
;
465 p
->whole_archive
= whole_archive
;
467 lang_statement_append (&input_file_chain
,
468 (lang_statement_union_type
*) p
,
473 lang_input_statement_type
*
474 lang_add_input_file (const char *name
,
475 lang_input_file_enum_type file_type
,
478 lang_has_input_file
= TRUE
;
479 return new_afile (name
, file_type
, target
, TRUE
);
482 /* Build enough state so that the parser can build its tree. */
487 obstack_begin (&stat_obstack
, 1000);
489 stat_ptr
= &statement_list
;
491 lang_list_init (stat_ptr
);
493 lang_list_init (&input_file_chain
);
494 lang_list_init (&lang_output_section_statement
);
495 lang_list_init (&file_chain
);
496 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
499 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
501 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
503 /* The value "3" is ad-hoc, somewhat related to the expected number of
504 DEFINED expressions in a linker script. For most default linker
505 scripts, there are none. Why a hash table then? Well, it's somewhat
506 simpler to re-use working machinery than using a linked list in terms
507 of code-complexity here in ld, besides the initialization which just
508 looks like other code here. */
509 if (!bfd_hash_table_init_n (&lang_definedness_table
,
510 lang_definedness_newfunc
, 3))
511 einfo (_("%P%F: out of memory during initialization"));
513 /* Callers of exp_fold_tree need to increment this. */
514 lang_statement_iteration
= 0;
517 /*----------------------------------------------------------------------
518 A region is an area of memory declared with the
519 MEMORY { name:org=exp, len=exp ... }
522 We maintain a list of all the regions here.
524 If no regions are specified in the script, then the default is used
525 which is created when looked up to be the entire data space.
527 If create is true we are creating a region inside a MEMORY block.
528 In this case it is probably an error to create a region that has
529 already been created. If we are not inside a MEMORY block it is
530 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
531 and so we issue a warning. */
533 static lang_memory_region_type
*lang_memory_region_list
;
534 static lang_memory_region_type
**lang_memory_region_list_tail
535 = &lang_memory_region_list
;
537 lang_memory_region_type
*
538 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
540 lang_memory_region_type
*p
;
541 lang_memory_region_type
*new;
543 /* NAME is NULL for LMA memspecs if no region was specified. */
547 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
548 if (strcmp (p
->name
, name
) == 0)
551 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
557 /* This code used to always use the first region in the list as the
558 default region. I changed it to instead use a region
559 encompassing all of memory as the default region. This permits
560 NOLOAD sections to work reasonably without requiring a region.
561 People should specify what region they mean, if they really want
563 if (strcmp (name
, DEFAULT_MEMORY_REGION
) == 0)
565 if (lang_memory_region_list
!= NULL
)
566 return lang_memory_region_list
;
570 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
571 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
573 new = stat_alloc (sizeof (lang_memory_region_type
));
575 new->name
= xstrdup (name
);
578 *lang_memory_region_list_tail
= new;
579 lang_memory_region_list_tail
= &new->next
;
583 new->length
= ~(bfd_size_type
) 0;
585 new->had_full_message
= FALSE
;
590 static lang_memory_region_type
*
591 lang_memory_default (asection
*section
)
593 lang_memory_region_type
*p
;
595 flagword sec_flags
= section
->flags
;
597 /* Override SEC_DATA to mean a writable section. */
598 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
599 sec_flags
|= SEC_DATA
;
601 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
603 if ((p
->flags
& sec_flags
) != 0
604 && (p
->not_flags
& sec_flags
) == 0)
609 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
612 static lang_output_section_statement_type
*
613 lang_output_section_find_1 (const char *const name
, int constraint
)
615 lang_statement_union_type
*u
;
616 lang_output_section_statement_type
*lookup
;
618 for (u
= lang_output_section_statement
.head
; u
!= NULL
; u
= lookup
->next
)
620 lookup
= &u
->output_section_statement
;
622 if (strcmp (name
, lookup
->name
) == 0
623 && lookup
->constraint
!= -1
624 && (constraint
== 0 || constraint
== lookup
->constraint
))
630 lang_output_section_statement_type
*
631 lang_output_section_find (const char *const name
)
633 return lang_output_section_find_1 (name
, 0);
636 static lang_output_section_statement_type
*
637 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
639 lang_output_section_statement_type
*lookup
;
641 lookup
= lang_output_section_find_1 (name
, constraint
);
644 lookup
= new_stat (lang_output_section_statement
, stat_ptr
);
645 lookup
->region
= NULL
;
646 lookup
->lma_region
= NULL
;
648 lookup
->block_value
= 1;
652 lookup
->bfd_section
= NULL
;
653 lookup
->processed
= 0;
654 lookup
->constraint
= constraint
;
655 lookup
->sectype
= normal_section
;
656 lookup
->addr_tree
= NULL
;
657 lang_list_init (&lookup
->children
);
659 lookup
->memspec
= NULL
;
661 lookup
->subsection_alignment
= -1;
662 lookup
->section_alignment
= -1;
663 lookup
->load_base
= NULL
;
664 lookup
->update_dot_tree
= NULL
;
665 lookup
->phdrs
= NULL
;
667 lang_statement_append (&lang_output_section_statement
,
668 (lang_statement_union_type
*) lookup
,
674 lang_output_section_statement_type
*
675 lang_output_section_statement_lookup (const char *const name
)
677 return lang_output_section_statement_lookup_1 (name
, 0);
681 lang_map_flags (flagword flag
)
683 if (flag
& SEC_ALLOC
)
689 if (flag
& SEC_READONLY
)
702 lang_memory_region_type
*m
;
705 minfo (_("\nMemory Configuration\n\n"));
706 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
707 _("Name"), _("Origin"), _("Length"), _("Attributes"));
709 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
714 fprintf (config
.map_file
, "%-16s ", m
->name
);
716 sprintf_vma (buf
, m
->origin
);
717 minfo ("0x%s ", buf
);
725 minfo ("0x%V", m
->length
);
726 if (m
->flags
|| m
->not_flags
)
734 lang_map_flags (m
->flags
);
740 lang_map_flags (m
->not_flags
);
747 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
749 if (! command_line
.reduce_memory_overheads
)
751 obstack_begin (&map_obstack
, 1000);
752 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
753 bfd_map_over_sections (p
, init_map_userdata
, 0);
754 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
760 init_map_userdata (abfd
, sec
, data
)
761 bfd
*abfd ATTRIBUTE_UNUSED
;
763 void *data ATTRIBUTE_UNUSED
;
765 fat_section_userdata_type
*new_data
766 = ((fat_section_userdata_type
*) (stat_alloc
767 (sizeof (fat_section_userdata_type
))));
769 ASSERT (get_userdata (sec
) == NULL
);
770 get_userdata (sec
) = new_data
;
771 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
775 sort_def_symbol (hash_entry
, info
)
776 struct bfd_link_hash_entry
*hash_entry
;
777 void *info ATTRIBUTE_UNUSED
;
779 if (hash_entry
->type
== bfd_link_hash_defined
780 || hash_entry
->type
== bfd_link_hash_defweak
)
782 struct fat_user_section_struct
*ud
;
783 struct map_symbol_def
*def
;
785 ud
= get_userdata (hash_entry
->u
.def
.section
);
788 /* ??? What do we have to do to initialize this beforehand? */
789 /* The first time we get here is bfd_abs_section... */
790 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
791 ud
= get_userdata (hash_entry
->u
.def
.section
);
793 else if (!ud
->map_symbol_def_tail
)
794 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
796 def
= obstack_alloc (&map_obstack
, sizeof *def
);
797 def
->entry
= hash_entry
;
798 *(ud
->map_symbol_def_tail
) = def
;
799 ud
->map_symbol_def_tail
= &def
->next
;
804 /* Initialize an output section. */
807 init_os (lang_output_section_statement_type
*s
)
809 lean_section_userdata_type
*new;
811 if (s
->bfd_section
!= NULL
)
814 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
815 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
817 new = stat_alloc (SECTION_USERDATA_SIZE
);
818 memset (new, 0, SECTION_USERDATA_SIZE
);
820 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
821 if (s
->bfd_section
== NULL
)
822 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
823 if (s
->bfd_section
== NULL
)
825 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
826 output_bfd
->xvec
->name
, s
->name
);
828 s
->bfd_section
->output_section
= s
->bfd_section
;
830 /* We initialize an output sections output offset to minus its own
831 vma to allow us to output a section through itself. */
832 s
->bfd_section
->output_offset
= 0;
833 get_userdata (s
->bfd_section
) = new;
835 /* If there is a base address, make sure that any sections it might
836 mention are initialized. */
837 if (s
->addr_tree
!= NULL
)
838 exp_init_os (s
->addr_tree
);
840 if (s
->load_base
!= NULL
)
841 exp_init_os (s
->load_base
);
844 /* Make sure that all output sections mentioned in an expression are
848 exp_init_os (etree_type
*exp
)
850 switch (exp
->type
.node_class
)
853 exp_init_os (exp
->assign
.src
);
857 exp_init_os (exp
->binary
.lhs
);
858 exp_init_os (exp
->binary
.rhs
);
862 exp_init_os (exp
->trinary
.cond
);
863 exp_init_os (exp
->trinary
.lhs
);
864 exp_init_os (exp
->trinary
.rhs
);
868 exp_init_os (exp
->assert_s
.child
);
872 exp_init_os (exp
->unary
.child
);
876 switch (exp
->type
.node_code
)
882 lang_output_section_statement_type
*os
;
884 os
= lang_output_section_find (exp
->name
.name
);
885 if (os
!= NULL
&& os
->bfd_section
== NULL
)
897 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
899 lang_input_statement_type
*entry
= data
;
901 /* If we are only reading symbols from this object, then we want to
902 discard all sections. */
903 if (entry
->just_syms_flag
)
905 bfd_link_just_syms (sec
, &link_info
);
909 bfd_section_already_linked (abfd
, sec
);
912 /* The wild routines.
914 These expand statements like *(.text) and foo.o to a list of
915 explicit actions, like foo.o(.text), bar.o(.text) and
916 foo.o(.text, .data). */
918 /* Add SECTION to the output section OUTPUT. Do this by creating a
919 lang_input_section statement which is placed at PTR. FILE is the
920 input file which holds SECTION. */
923 lang_add_section (lang_statement_list_type
*ptr
,
925 lang_output_section_statement_type
*output
,
926 lang_input_statement_type
*file
)
928 flagword flags
= section
->flags
;
931 /* Discard sections marked with SEC_EXCLUDE. */
932 discard
= (flags
& SEC_EXCLUDE
) != 0;
934 /* Discard input sections which are assigned to a section named
935 DISCARD_SECTION_NAME. */
936 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
939 /* Discard debugging sections if we are stripping debugging
941 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
942 && (flags
& SEC_DEBUGGING
) != 0)
947 if (section
->output_section
== NULL
)
949 /* This prevents future calls from assigning this section. */
950 section
->output_section
= bfd_abs_section_ptr
;
955 if (section
->output_section
== NULL
)
958 lang_input_section_type
*new;
961 if (output
->bfd_section
== NULL
)
964 first
= ! output
->bfd_section
->linker_has_input
;
965 output
->bfd_section
->linker_has_input
= 1;
967 /* Add a section reference to the list. */
968 new = new_stat (lang_input_section
, ptr
);
970 new->section
= section
;
972 section
->output_section
= output
->bfd_section
;
974 flags
= section
->flags
;
976 /* We don't copy the SEC_NEVER_LOAD flag from an input section
977 to an output section, because we want to be able to include a
978 SEC_NEVER_LOAD section in the middle of an otherwise loaded
979 section (I don't know why we want to do this, but we do).
980 build_link_order in ldwrite.c handles this case by turning
981 the embedded SEC_NEVER_LOAD section into a fill. */
983 flags
&= ~ SEC_NEVER_LOAD
;
985 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
986 already been processed. One reason to do this is that on pe
987 format targets, .text$foo sections go into .text and it's odd
988 to see .text with SEC_LINK_ONCE set. */
990 if (! link_info
.relocatable
)
991 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
993 /* If this is not the first input section, and the SEC_READONLY
994 flag is not currently set, then don't set it just because the
995 input section has it set. */
997 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
998 flags
&= ~ SEC_READONLY
;
1000 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1002 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1003 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1004 || ((flags
& SEC_MERGE
)
1005 && section
->output_section
->entsize
!= section
->entsize
)))
1007 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1008 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1011 section
->output_section
->flags
|= flags
;
1013 if (flags
& SEC_MERGE
)
1014 section
->output_section
->entsize
= section
->entsize
;
1016 /* If SEC_READONLY is not set in the input section, then clear
1017 it from the output section. */
1018 if ((section
->flags
& SEC_READONLY
) == 0)
1019 section
->output_section
->flags
&= ~SEC_READONLY
;
1021 switch (output
->sectype
)
1023 case normal_section
:
1028 case overlay_section
:
1029 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1031 case noload_section
:
1032 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1033 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1037 /* Copy over SEC_SMALL_DATA. */
1038 if (section
->flags
& SEC_SMALL_DATA
)
1039 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1041 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1042 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1044 /* If supplied an alignment, then force it. */
1045 if (output
->section_alignment
!= -1)
1046 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1048 if (section
->flags
& SEC_BLOCK
)
1050 section
->output_section
->flags
|= SEC_BLOCK
;
1051 /* FIXME: This value should really be obtained from the bfd... */
1052 output
->block_value
= 128;
1057 /* Handle wildcard sorting. This returns the lang_input_section which
1058 should follow the one we are going to create for SECTION and FILE,
1059 based on the sorting requirements of WILD. It returns NULL if the
1060 new section should just go at the end of the current list. */
1062 static lang_statement_union_type
*
1063 wild_sort (lang_wild_statement_type
*wild
,
1064 struct wildcard_list
*sec
,
1065 lang_input_statement_type
*file
,
1068 const char *section_name
;
1069 lang_statement_union_type
*l
;
1071 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1074 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1075 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1077 lang_input_section_type
*ls
;
1079 if (l
->header
.type
!= lang_input_section_enum
)
1081 ls
= &l
->input_section
;
1083 /* Sorting by filename takes precedence over sorting by section
1086 if (wild
->filenames_sorted
)
1088 const char *fn
, *ln
;
1092 /* The PE support for the .idata section as generated by
1093 dlltool assumes that files will be sorted by the name of
1094 the archive and then the name of the file within the
1097 if (file
->the_bfd
!= NULL
1098 && bfd_my_archive (file
->the_bfd
) != NULL
)
1100 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1105 fn
= file
->filename
;
1109 if (ls
->ifile
->the_bfd
!= NULL
1110 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1112 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1117 ln
= ls
->ifile
->filename
;
1121 i
= strcmp (fn
, ln
);
1130 fn
= file
->filename
;
1132 ln
= ls
->ifile
->filename
;
1134 i
= strcmp (fn
, ln
);
1142 /* Here either the files are not sorted by name, or we are
1143 looking at the sections for this file. */
1145 if (sec
!= NULL
&& sec
->spec
.sorted
)
1147 if (strcmp (section_name
,
1148 bfd_get_section_name (ls
->ifile
->the_bfd
,
1158 /* Expand a wild statement for a particular FILE. SECTION may be
1159 NULL, in which case it is a wild card. */
1162 output_section_callback (lang_wild_statement_type
*ptr
,
1163 struct wildcard_list
*sec
,
1165 lang_input_statement_type
*file
,
1168 lang_statement_union_type
*before
;
1170 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1171 if (unique_section_p (section
))
1174 before
= wild_sort (ptr
, sec
, file
, section
);
1176 /* Here BEFORE points to the lang_input_section which
1177 should follow the one we are about to add. If BEFORE
1178 is NULL, then the section should just go at the end
1179 of the current list. */
1182 lang_add_section (&ptr
->children
, section
,
1183 (lang_output_section_statement_type
*) output
,
1187 lang_statement_list_type list
;
1188 lang_statement_union_type
**pp
;
1190 lang_list_init (&list
);
1191 lang_add_section (&list
, section
,
1192 (lang_output_section_statement_type
*) output
,
1195 /* If we are discarding the section, LIST.HEAD will
1197 if (list
.head
!= NULL
)
1199 ASSERT (list
.head
->header
.next
== NULL
);
1201 for (pp
= &ptr
->children
.head
;
1203 pp
= &(*pp
)->header
.next
)
1204 ASSERT (*pp
!= NULL
);
1206 list
.head
->header
.next
= *pp
;
1212 /* Check if all sections in a wild statement for a particular FILE
1216 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
1217 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
1219 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
1222 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1223 if (unique_section_p (section
))
1226 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
1227 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
1230 /* This is passed a file name which must have been seen already and
1231 added to the statement tree. We will see if it has been opened
1232 already and had its symbols read. If not then we'll read it. */
1234 static lang_input_statement_type
*
1235 lookup_name (const char *name
)
1237 lang_input_statement_type
*search
;
1239 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1241 search
= (lang_input_statement_type
*) search
->next_real_file
)
1243 /* Use the local_sym_name as the name of the file that has
1244 already been loaded as filename might have been transformed
1245 via the search directory lookup mechanism. */
1246 const char * filename
= search
->local_sym_name
;
1248 if (filename
== NULL
&& name
== NULL
)
1250 if (filename
!= NULL
1252 && strcmp (filename
, name
) == 0)
1257 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
1258 default_target
, FALSE
);
1260 /* If we have already added this file, or this file is not real
1261 (FIXME: can that ever actually happen?) or the name is NULL
1262 (FIXME: can that ever actually happen?) don't add this file. */
1265 || search
->filename
== NULL
)
1268 if (! load_symbols (search
, NULL
))
1274 /* Get the symbols for an input file. */
1277 load_symbols (lang_input_statement_type
*entry
,
1278 lang_statement_list_type
*place
)
1285 ldfile_open_file (entry
);
1287 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1288 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1291 lang_statement_list_type
*hold
;
1292 bfd_boolean bad_load
= TRUE
;
1293 bfd_boolean save_ldlang_sysrooted_script
;
1295 err
= bfd_get_error ();
1297 /* See if the emulation has some special knowledge. */
1298 if (ldemul_unrecognized_file (entry
))
1301 if (err
== bfd_error_file_ambiguously_recognized
)
1305 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1306 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1307 for (p
= matching
; *p
!= NULL
; p
++)
1311 else if (err
!= bfd_error_file_not_recognized
1313 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1317 bfd_close (entry
->the_bfd
);
1318 entry
->the_bfd
= NULL
;
1320 /* Try to interpret the file as a linker script. */
1321 ldfile_open_command_file (entry
->filename
);
1325 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1326 ldlang_sysrooted_script
= entry
->sysrooted
;
1328 ldfile_assumed_script
= TRUE
;
1329 parser_input
= input_script
;
1331 ldfile_assumed_script
= FALSE
;
1333 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1339 if (ldemul_recognized_file (entry
))
1342 /* We don't call ldlang_add_file for an archive. Instead, the
1343 add_symbols entry point will call ldlang_add_file, via the
1344 add_archive_element callback, for each element of the archive
1346 switch (bfd_get_format (entry
->the_bfd
))
1352 ldlang_add_file (entry
);
1353 if (trace_files
|| trace_file_tries
)
1354 info_msg ("%I\n", entry
);
1358 if (entry
->whole_archive
)
1361 bfd_boolean loaded
= TRUE
;
1365 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1370 if (! bfd_check_format (member
, bfd_object
))
1372 einfo (_("%F%B: member %B in archive is not an object\n"),
1373 entry
->the_bfd
, member
);
1377 if (! ((*link_info
.callbacks
->add_archive_element
)
1378 (&link_info
, member
, "--whole-archive")))
1381 if (! bfd_link_add_symbols (member
, &link_info
))
1383 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1388 entry
->loaded
= loaded
;
1394 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1395 entry
->loaded
= TRUE
;
1397 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1399 return entry
->loaded
;
1402 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1403 may be NULL, indicating that it is a wildcard. Separate
1404 lang_input_section statements are created for each part of the
1405 expansion; they are added after the wild statement S. OUTPUT is
1406 the output section. */
1409 wild (lang_wild_statement_type
*s
,
1410 const char *target ATTRIBUTE_UNUSED
,
1411 lang_output_section_statement_type
*output
)
1413 struct wildcard_list
*sec
;
1415 walk_wild (s
, output_section_callback
, output
);
1417 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1419 if (default_common_section
!= NULL
)
1421 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1423 /* Remember the section that common is going to in case we
1424 later get something which doesn't know where to put it. */
1425 default_common_section
= output
;
1430 /* Return TRUE iff target is the sought target. */
1433 get_target (const bfd_target
*target
, void *data
)
1435 const char *sought
= data
;
1437 return strcmp (target
->name
, sought
) == 0;
1440 /* Like strcpy() but convert to lower case as well. */
1443 stricpy (char *dest
, char *src
)
1447 while ((c
= *src
++) != 0)
1448 *dest
++ = TOLOWER (c
);
1453 /* Remove the first occurrence of needle (if any) in haystack
1457 strcut (char *haystack
, char *needle
)
1459 haystack
= strstr (haystack
, needle
);
1465 for (src
= haystack
+ strlen (needle
); *src
;)
1466 *haystack
++ = *src
++;
1472 /* Compare two target format name strings.
1473 Return a value indicating how "similar" they are. */
1476 name_compare (char *first
, char *second
)
1482 copy1
= xmalloc (strlen (first
) + 1);
1483 copy2
= xmalloc (strlen (second
) + 1);
1485 /* Convert the names to lower case. */
1486 stricpy (copy1
, first
);
1487 stricpy (copy2
, second
);
1489 /* Remove size and endian strings from the name. */
1490 strcut (copy1
, "big");
1491 strcut (copy1
, "little");
1492 strcut (copy2
, "big");
1493 strcut (copy2
, "little");
1495 /* Return a value based on how many characters match,
1496 starting from the beginning. If both strings are
1497 the same then return 10 * their length. */
1498 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1499 if (copy1
[result
] == 0)
1511 /* Set by closest_target_match() below. */
1512 static const bfd_target
*winner
;
1514 /* Scan all the valid bfd targets looking for one that has the endianness
1515 requirement that was specified on the command line, and is the nearest
1516 match to the original output target. */
1519 closest_target_match (const bfd_target
*target
, void *data
)
1521 const bfd_target
*original
= data
;
1523 if (command_line
.endian
== ENDIAN_BIG
1524 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1527 if (command_line
.endian
== ENDIAN_LITTLE
1528 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1531 /* Must be the same flavour. */
1532 if (target
->flavour
!= original
->flavour
)
1535 /* If we have not found a potential winner yet, then record this one. */
1542 /* Oh dear, we now have two potential candidates for a successful match.
1543 Compare their names and choose the better one. */
1544 if (name_compare (target
->name
, original
->name
)
1545 > name_compare (winner
->name
, original
->name
))
1548 /* Keep on searching until wqe have checked them all. */
1552 /* Return the BFD target format of the first input file. */
1555 get_first_input_target (void)
1557 char *target
= NULL
;
1559 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1561 if (s
->header
.type
== lang_input_statement_enum
1564 ldfile_open_file (s
);
1566 if (s
->the_bfd
!= NULL
1567 && bfd_check_format (s
->the_bfd
, bfd_object
))
1569 target
= bfd_get_target (s
->the_bfd
);
1581 lang_get_output_target (void)
1585 /* Has the user told us which output format to use? */
1586 if (output_target
!= NULL
)
1587 return output_target
;
1589 /* No - has the current target been set to something other than
1591 if (current_target
!= default_target
)
1592 return current_target
;
1594 /* No - can we determine the format of the first input file? */
1595 target
= get_first_input_target ();
1599 /* Failed - use the default output target. */
1600 return default_target
;
1603 /* Open the output file. */
1606 open_output (const char *name
)
1610 output_target
= lang_get_output_target ();
1612 /* Has the user requested a particular endianness on the command
1614 if (command_line
.endian
!= ENDIAN_UNSET
)
1616 const bfd_target
*target
;
1617 enum bfd_endian desired_endian
;
1619 /* Get the chosen target. */
1620 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1622 /* If the target is not supported, we cannot do anything. */
1625 if (command_line
.endian
== ENDIAN_BIG
)
1626 desired_endian
= BFD_ENDIAN_BIG
;
1628 desired_endian
= BFD_ENDIAN_LITTLE
;
1630 /* See if the target has the wrong endianness. This should
1631 not happen if the linker script has provided big and
1632 little endian alternatives, but some scrips don't do
1634 if (target
->byteorder
!= desired_endian
)
1636 /* If it does, then see if the target provides
1637 an alternative with the correct endianness. */
1638 if (target
->alternative_target
!= NULL
1639 && (target
->alternative_target
->byteorder
== desired_endian
))
1640 output_target
= target
->alternative_target
->name
;
1643 /* Try to find a target as similar as possible to
1644 the default target, but which has the desired
1645 endian characteristic. */
1646 bfd_search_for_target (closest_target_match
,
1649 /* Oh dear - we could not find any targets that
1650 satisfy our requirements. */
1652 einfo (_("%P: warning: could not find any targets"
1653 " that match endianness requirement\n"));
1655 output_target
= winner
->name
;
1661 output
= bfd_openw (name
, output_target
);
1665 if (bfd_get_error () == bfd_error_invalid_target
)
1666 einfo (_("%P%F: target %s not found\n"), output_target
);
1668 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1671 delete_output_file_on_failure
= TRUE
;
1674 output
->flags
|= D_PAGED
;
1677 if (! bfd_set_format (output
, bfd_object
))
1678 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1679 if (! bfd_set_arch_mach (output
,
1680 ldfile_output_architecture
,
1681 ldfile_output_machine
))
1682 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1684 link_info
.hash
= bfd_link_hash_table_create (output
);
1685 if (link_info
.hash
== NULL
)
1686 einfo (_("%P%F: can not create link hash table: %E\n"));
1688 bfd_set_gp_size (output
, g_switch_value
);
1693 ldlang_open_output (lang_statement_union_type
*statement
)
1695 switch (statement
->header
.type
)
1697 case lang_output_statement_enum
:
1698 ASSERT (output_bfd
== NULL
);
1699 output_bfd
= open_output (statement
->output_statement
.name
);
1700 ldemul_set_output_arch ();
1701 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
1702 output_bfd
->flags
|= D_PAGED
;
1704 output_bfd
->flags
&= ~D_PAGED
;
1705 if (config
.text_read_only
)
1706 output_bfd
->flags
|= WP_TEXT
;
1708 output_bfd
->flags
&= ~WP_TEXT
;
1709 if (link_info
.traditional_format
)
1710 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1712 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1715 case lang_target_statement_enum
:
1716 current_target
= statement
->target_statement
.target
;
1723 /* Convert between addresses in bytes and sizes in octets.
1724 For currently supported targets, octets_per_byte is always a power
1725 of two, so we can use shifts. */
1726 #define TO_ADDR(X) ((X) >> opb_shift)
1727 #define TO_SIZE(X) ((X) << opb_shift)
1729 /* Support the above. */
1730 static unsigned int opb_shift
= 0;
1735 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
1736 ldfile_output_machine
);
1739 while ((x
& 1) == 0)
1747 /* Open all the input files. */
1750 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
1752 for (; s
!= NULL
; s
= s
->header
.next
)
1754 switch (s
->header
.type
)
1756 case lang_constructors_statement_enum
:
1757 open_input_bfds (constructor_list
.head
, force
);
1759 case lang_output_section_statement_enum
:
1760 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1762 case lang_wild_statement_enum
:
1763 /* Maybe we should load the file's symbols. */
1764 if (s
->wild_statement
.filename
1765 && ! wildcardp (s
->wild_statement
.filename
))
1766 lookup_name (s
->wild_statement
.filename
);
1767 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1769 case lang_group_statement_enum
:
1771 struct bfd_link_hash_entry
*undefs
;
1773 /* We must continually search the entries in the group
1774 until no new symbols are added to the list of undefined
1779 undefs
= link_info
.hash
->undefs_tail
;
1780 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
1782 while (undefs
!= link_info
.hash
->undefs_tail
);
1785 case lang_target_statement_enum
:
1786 current_target
= s
->target_statement
.target
;
1788 case lang_input_statement_enum
:
1789 if (s
->input_statement
.real
)
1791 lang_statement_list_type add
;
1793 s
->input_statement
.target
= current_target
;
1795 /* If we are being called from within a group, and this
1796 is an archive which has already been searched, then
1797 force it to be researched unless the whole archive
1798 has been loaded already. */
1800 && !s
->input_statement
.whole_archive
1801 && s
->input_statement
.loaded
1802 && bfd_check_format (s
->input_statement
.the_bfd
,
1804 s
->input_statement
.loaded
= FALSE
;
1806 lang_list_init (&add
);
1808 if (! load_symbols (&s
->input_statement
, &add
))
1809 config
.make_executable
= FALSE
;
1811 if (add
.head
!= NULL
)
1813 *add
.tail
= s
->header
.next
;
1814 s
->header
.next
= add
.head
;
1824 /* If there are [COMMONS] statements, put a wild one into the bss
1828 lang_reasonable_defaults (void)
1831 lang_output_section_statement_lookup (".text");
1832 lang_output_section_statement_lookup (".data");
1834 default_common_section
= lang_output_section_statement_lookup (".bss");
1836 if (!placed_commons
)
1838 lang_wild_statement_type
*new =
1839 new_stat (lang_wild_statement
,
1840 &default_common_section
->children
);
1842 new->section_name
= "COMMON";
1843 new->filename
= NULL
;
1844 lang_list_init (&new->children
);
1849 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1852 lang_track_definedness (const char *name
)
1854 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
1855 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
1858 /* New-function for the definedness hash table. */
1860 static struct bfd_hash_entry
*
1861 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
1862 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
1863 const char *name ATTRIBUTE_UNUSED
)
1865 struct lang_definedness_hash_entry
*ret
1866 = (struct lang_definedness_hash_entry
*) entry
;
1869 ret
= (struct lang_definedness_hash_entry
*)
1870 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
1873 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
1875 ret
->iteration
= -1;
1879 /* Return the iteration when the definition of NAME was last updated. A
1880 value of -1 means that the symbol is not defined in the linker script
1881 or the command line, but may be defined in the linker symbol table. */
1884 lang_symbol_definition_iteration (const char *name
)
1886 struct lang_definedness_hash_entry
*defentry
1887 = (struct lang_definedness_hash_entry
*)
1888 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1890 /* We've already created this one on the presence of DEFINED in the
1891 script, so it can't be NULL unless something is borked elsewhere in
1893 if (defentry
== NULL
)
1896 return defentry
->iteration
;
1899 /* Update the definedness state of NAME. */
1902 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
1904 struct lang_definedness_hash_entry
*defentry
1905 = (struct lang_definedness_hash_entry
*)
1906 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1908 /* We don't keep track of symbols not tested with DEFINED. */
1909 if (defentry
== NULL
)
1912 /* If the symbol was already defined, and not from an earlier statement
1913 iteration, don't update the definedness iteration, because that'd
1914 make the symbol seem defined in the linker script at this point, and
1915 it wasn't; it was defined in some object. If we do anyway, DEFINED
1916 would start to yield false before this point and the construct "sym =
1917 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1919 if (h
->type
!= bfd_link_hash_undefined
1920 && h
->type
!= bfd_link_hash_common
1921 && h
->type
!= bfd_link_hash_new
1922 && defentry
->iteration
== -1)
1925 defentry
->iteration
= lang_statement_iteration
;
1928 /* Add the supplied name to the symbol table as an undefined reference.
1929 This is a two step process as the symbol table doesn't even exist at
1930 the time the ld command line is processed. First we put the name
1931 on a list, then, once the output file has been opened, transfer the
1932 name to the symbol table. */
1934 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
1936 #define ldlang_undef_chain_list_head entry_symbol.next
1939 ldlang_add_undef (const char *const name
)
1941 ldlang_undef_chain_list_type
*new =
1942 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
1944 new->next
= ldlang_undef_chain_list_head
;
1945 ldlang_undef_chain_list_head
= new;
1947 new->name
= xstrdup (name
);
1949 if (output_bfd
!= NULL
)
1950 insert_undefined (new->name
);
1953 /* Insert NAME as undefined in the symbol table. */
1956 insert_undefined (const char *name
)
1958 struct bfd_link_hash_entry
*h
;
1960 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
1962 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1963 if (h
->type
== bfd_link_hash_new
)
1965 h
->type
= bfd_link_hash_undefined
;
1966 h
->u
.undef
.abfd
= NULL
;
1967 bfd_link_add_undef (link_info
.hash
, h
);
1971 /* Run through the list of undefineds created above and place them
1972 into the linker hash table as undefined symbols belonging to the
1976 lang_place_undefineds (void)
1978 ldlang_undef_chain_list_type
*ptr
;
1980 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
1981 insert_undefined (ptr
->name
);
1984 /* Check for all readonly or some readwrite sections. */
1987 check_input_sections
1988 (lang_statement_union_type
*s
,
1989 lang_output_section_statement_type
*output_section_statement
)
1991 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
1993 switch (s
->header
.type
)
1995 case lang_wild_statement_enum
:
1996 walk_wild (&s
->wild_statement
, check_section_callback
,
1997 output_section_statement
);
1998 if (! output_section_statement
->all_input_readonly
)
2001 case lang_constructors_statement_enum
:
2002 check_input_sections (constructor_list
.head
,
2003 output_section_statement
);
2004 if (! output_section_statement
->all_input_readonly
)
2007 case lang_group_statement_enum
:
2008 check_input_sections (s
->group_statement
.children
.head
,
2009 output_section_statement
);
2010 if (! output_section_statement
->all_input_readonly
)
2019 /* Open input files and attach to output sections. */
2022 map_input_to_output_sections
2023 (lang_statement_union_type
*s
, const char *target
,
2024 lang_output_section_statement_type
*output_section_statement
)
2026 for (; s
!= NULL
; s
= s
->header
.next
)
2028 switch (s
->header
.type
)
2030 case lang_wild_statement_enum
:
2031 wild (&s
->wild_statement
, target
, output_section_statement
);
2033 case lang_constructors_statement_enum
:
2034 map_input_to_output_sections (constructor_list
.head
,
2036 output_section_statement
);
2038 case lang_output_section_statement_enum
:
2039 if (s
->output_section_statement
.constraint
)
2041 if (s
->output_section_statement
.constraint
== -1)
2043 s
->output_section_statement
.all_input_readonly
= TRUE
;
2044 check_input_sections (s
->output_section_statement
.children
.head
,
2045 &s
->output_section_statement
);
2046 if ((s
->output_section_statement
.all_input_readonly
2047 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
2048 || (!s
->output_section_statement
.all_input_readonly
2049 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
2051 s
->output_section_statement
.constraint
= -1;
2056 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2058 &s
->output_section_statement
);
2060 case lang_output_statement_enum
:
2062 case lang_target_statement_enum
:
2063 target
= s
->target_statement
.target
;
2065 case lang_group_statement_enum
:
2066 map_input_to_output_sections (s
->group_statement
.children
.head
,
2068 output_section_statement
);
2070 case lang_data_statement_enum
:
2071 /* Make sure that any sections mentioned in the expression
2073 exp_init_os (s
->data_statement
.exp
);
2075 case lang_fill_statement_enum
:
2076 case lang_input_section_enum
:
2077 case lang_object_symbols_statement_enum
:
2078 case lang_reloc_statement_enum
:
2079 case lang_padding_statement_enum
:
2080 case lang_input_statement_enum
:
2081 if (output_section_statement
!= NULL
2082 && output_section_statement
->bfd_section
== NULL
)
2083 init_os (output_section_statement
);
2085 case lang_assignment_statement_enum
:
2086 if (output_section_statement
!= NULL
2087 && output_section_statement
->bfd_section
== NULL
)
2088 init_os (output_section_statement
);
2090 /* Make sure that any sections mentioned in the assignment
2092 exp_init_os (s
->assignment_statement
.exp
);
2094 case lang_afile_asection_pair_statement_enum
:
2097 case lang_address_statement_enum
:
2098 /* Mark the specified section with the supplied address. */
2100 lang_output_section_statement_type
*os
=
2101 lang_output_section_statement_lookup
2102 (s
->address_statement
.section_name
);
2104 if (os
->bfd_section
== NULL
)
2106 os
->addr_tree
= s
->address_statement
.address
;
2113 /* An output section might have been removed after its statement was
2114 added. For example, ldemul_before_allocation can remove dynamic
2115 sections if they turn out to be not needed. Clean them up here. */
2118 strip_excluded_output_sections (void)
2120 lang_statement_union_type
*u
;
2122 for (u
= lang_output_section_statement
.head
;
2124 u
= u
->output_section_statement
.next
)
2126 lang_output_section_statement_type
*os
;
2129 os
= &u
->output_section_statement
;
2130 if (os
->constraint
== -1)
2132 s
= os
->bfd_section
;
2133 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2137 os
->bfd_section
= NULL
;
2139 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2142 bfd_section_list_remove (output_bfd
, p
);
2143 output_bfd
->section_count
--;
2151 print_output_section_statement
2152 (lang_output_section_statement_type
*output_section_statement
)
2154 asection
*section
= output_section_statement
->bfd_section
;
2157 if (output_section_statement
!= abs_output_section
)
2159 minfo ("\n%s", output_section_statement
->name
);
2161 if (section
!= NULL
)
2163 print_dot
= section
->vma
;
2165 len
= strlen (output_section_statement
->name
);
2166 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2171 while (len
< SECTION_NAME_MAP_LENGTH
)
2177 minfo ("0x%V %W", section
->vma
, section
->size
);
2179 if (output_section_statement
->load_base
!= NULL
)
2183 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2184 "load base", lang_final_phase_enum
);
2185 minfo (_(" load address 0x%V"), addr
);
2192 print_statement_list (output_section_statement
->children
.head
,
2193 output_section_statement
);
2197 print_assignment (lang_assignment_statement_type
*assignment
,
2198 lang_output_section_statement_type
*output_section
)
2201 etree_value_type result
;
2203 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2206 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2207 lang_final_phase_enum
, print_dot
, &print_dot
);
2213 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2214 dst
= assignment
->exp
->assign
.dst
;
2216 minfo ("0x%V", value
);
2217 if (dst
[0] == '.' && dst
[1] == 0)
2230 exp_print_tree (assignment
->exp
);
2236 print_input_statement (lang_input_statement_type
*statm
)
2238 if (statm
->filename
!= NULL
)
2240 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2244 /* Print all symbols defined in a particular section. This is called
2245 via bfd_link_hash_traverse, or by print_all_symbols. */
2248 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
2250 asection
*sec
= ptr
;
2252 if ((hash_entry
->type
== bfd_link_hash_defined
2253 || hash_entry
->type
== bfd_link_hash_defweak
)
2254 && sec
== hash_entry
->u
.def
.section
)
2258 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2261 (hash_entry
->u
.def
.value
2262 + hash_entry
->u
.def
.section
->output_offset
2263 + hash_entry
->u
.def
.section
->output_section
->vma
));
2265 minfo (" %T\n", hash_entry
->root
.string
);
2272 print_all_symbols (sec
)
2275 struct fat_user_section_struct
*ud
= get_userdata (sec
);
2276 struct map_symbol_def
*def
;
2278 *ud
->map_symbol_def_tail
= 0;
2279 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
2280 print_one_symbol (def
->entry
, sec
);
2283 /* Print information about an input section to the map file. */
2286 print_input_section (lang_input_section_type
*in
)
2288 asection
*i
= in
->section
;
2289 bfd_size_type size
= i
->size
;
2298 minfo ("%s", i
->name
);
2300 len
= 1 + strlen (i
->name
);
2301 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2306 while (len
< SECTION_NAME_MAP_LENGTH
)
2312 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2313 addr
= i
->output_section
->vma
+ i
->output_offset
;
2320 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
2322 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
2324 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2336 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
2339 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2341 if (command_line
.reduce_memory_overheads
)
2342 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
2344 print_all_symbols (i
);
2346 print_dot
= addr
+ TO_ADDR (size
);
2352 print_fill_statement (lang_fill_statement_type
*fill
)
2356 fputs (" FILL mask 0x", config
.map_file
);
2357 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2358 fprintf (config
.map_file
, "%02x", *p
);
2359 fputs ("\n", config
.map_file
);
2363 print_data_statement (lang_data_statement_type
*data
)
2371 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2374 addr
= data
->output_vma
;
2375 if (data
->output_section
!= NULL
)
2376 addr
+= data
->output_section
->vma
;
2404 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2406 if (data
->exp
->type
.node_class
!= etree_value
)
2409 exp_print_tree (data
->exp
);
2414 print_dot
= addr
+ TO_ADDR (size
);
2417 /* Print an address statement. These are generated by options like
2421 print_address_statement (lang_address_statement_type
*address
)
2423 minfo (_("Address of section %s set to "), address
->section_name
);
2424 exp_print_tree (address
->address
);
2428 /* Print a reloc statement. */
2431 print_reloc_statement (lang_reloc_statement_type
*reloc
)
2438 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2441 addr
= reloc
->output_vma
;
2442 if (reloc
->output_section
!= NULL
)
2443 addr
+= reloc
->output_section
->vma
;
2445 size
= bfd_get_reloc_size (reloc
->howto
);
2447 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2449 if (reloc
->name
!= NULL
)
2450 minfo ("%s+", reloc
->name
);
2452 minfo ("%s+", reloc
->section
->name
);
2454 exp_print_tree (reloc
->addend_exp
);
2458 print_dot
= addr
+ TO_ADDR (size
);
2462 print_padding_statement (lang_padding_statement_type
*s
)
2470 len
= sizeof " *fill*" - 1;
2471 while (len
< SECTION_NAME_MAP_LENGTH
)
2477 addr
= s
->output_offset
;
2478 if (s
->output_section
!= NULL
)
2479 addr
+= s
->output_section
->vma
;
2480 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
2482 if (s
->fill
->size
!= 0)
2486 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2487 fprintf (config
.map_file
, "%02x", *p
);
2492 print_dot
= addr
+ TO_ADDR (s
->size
);
2496 print_wild_statement (lang_wild_statement_type
*w
,
2497 lang_output_section_statement_type
*os
)
2499 struct wildcard_list
*sec
;
2503 if (w
->filenames_sorted
)
2505 if (w
->filename
!= NULL
)
2506 minfo ("%s", w
->filename
);
2509 if (w
->filenames_sorted
)
2513 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2515 if (sec
->spec
.sorted
)
2517 if (sec
->spec
.exclude_name_list
!= NULL
)
2520 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2521 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2522 minfo (" %s", tmp
->name
);
2525 if (sec
->spec
.name
!= NULL
)
2526 minfo ("%s", sec
->spec
.name
);
2529 if (sec
->spec
.sorted
)
2538 print_statement_list (w
->children
.head
, os
);
2541 /* Print a group statement. */
2544 print_group (lang_group_statement_type
*s
,
2545 lang_output_section_statement_type
*os
)
2547 fprintf (config
.map_file
, "START GROUP\n");
2548 print_statement_list (s
->children
.head
, os
);
2549 fprintf (config
.map_file
, "END GROUP\n");
2552 /* Print the list of statements in S.
2553 This can be called for any statement type. */
2556 print_statement_list (lang_statement_union_type
*s
,
2557 lang_output_section_statement_type
*os
)
2561 print_statement (s
, os
);
2566 /* Print the first statement in statement list S.
2567 This can be called for any statement type. */
2570 print_statement (lang_statement_union_type
*s
,
2571 lang_output_section_statement_type
*os
)
2573 switch (s
->header
.type
)
2576 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2579 case lang_constructors_statement_enum
:
2580 if (constructor_list
.head
!= NULL
)
2582 if (constructors_sorted
)
2583 minfo (" SORT (CONSTRUCTORS)\n");
2585 minfo (" CONSTRUCTORS\n");
2586 print_statement_list (constructor_list
.head
, os
);
2589 case lang_wild_statement_enum
:
2590 print_wild_statement (&s
->wild_statement
, os
);
2592 case lang_address_statement_enum
:
2593 print_address_statement (&s
->address_statement
);
2595 case lang_object_symbols_statement_enum
:
2596 minfo (" CREATE_OBJECT_SYMBOLS\n");
2598 case lang_fill_statement_enum
:
2599 print_fill_statement (&s
->fill_statement
);
2601 case lang_data_statement_enum
:
2602 print_data_statement (&s
->data_statement
);
2604 case lang_reloc_statement_enum
:
2605 print_reloc_statement (&s
->reloc_statement
);
2607 case lang_input_section_enum
:
2608 print_input_section (&s
->input_section
);
2610 case lang_padding_statement_enum
:
2611 print_padding_statement (&s
->padding_statement
);
2613 case lang_output_section_statement_enum
:
2614 print_output_section_statement (&s
->output_section_statement
);
2616 case lang_assignment_statement_enum
:
2617 print_assignment (&s
->assignment_statement
, os
);
2619 case lang_target_statement_enum
:
2620 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2622 case lang_output_statement_enum
:
2623 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2624 if (output_target
!= NULL
)
2625 minfo (" %s", output_target
);
2628 case lang_input_statement_enum
:
2629 print_input_statement (&s
->input_statement
);
2631 case lang_group_statement_enum
:
2632 print_group (&s
->group_statement
, os
);
2634 case lang_afile_asection_pair_statement_enum
:
2641 print_statements (void)
2643 print_statement_list (statement_list
.head
, abs_output_section
);
2646 /* Print the first N statements in statement list S to STDERR.
2647 If N == 0, nothing is printed.
2648 If N < 0, the entire list is printed.
2649 Intended to be called from GDB. */
2652 dprint_statement (lang_statement_union_type
*s
, int n
)
2654 FILE *map_save
= config
.map_file
;
2656 config
.map_file
= stderr
;
2659 print_statement_list (s
, abs_output_section
);
2662 while (s
&& --n
>= 0)
2664 print_statement (s
, abs_output_section
);
2669 config
.map_file
= map_save
;
2673 insert_pad (lang_statement_union_type
**ptr
,
2675 unsigned int alignment_needed
,
2676 asection
*output_section
,
2679 static fill_type zero_fill
= { 1, { 0 } };
2680 lang_statement_union_type
*pad
;
2682 pad
= ((lang_statement_union_type
*)
2683 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2684 if (ptr
!= &statement_list
.head
2685 && pad
->header
.type
== lang_padding_statement_enum
2686 && pad
->padding_statement
.output_section
== output_section
)
2688 /* Use the existing pad statement. The above test on output
2689 section is probably redundant, but it doesn't hurt to check. */
2693 /* Make a new padding statement, linked into existing chain. */
2694 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
2695 pad
->header
.next
= *ptr
;
2697 pad
->header
.type
= lang_padding_statement_enum
;
2698 pad
->padding_statement
.output_section
= output_section
;
2701 pad
->padding_statement
.fill
= fill
;
2703 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2704 pad
->padding_statement
.size
= alignment_needed
;
2705 output_section
->size
+= alignment_needed
;
2708 /* Work out how much this section will move the dot point. */
2712 (lang_statement_union_type
**this_ptr
,
2713 lang_output_section_statement_type
*output_section_statement
,
2717 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2718 asection
*i
= is
->section
;
2720 if (!is
->ifile
->just_syms_flag
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2722 unsigned int alignment_needed
;
2725 /* Align this section first to the input sections requirement,
2726 then to the output section's requirement. If this alignment
2727 is greater than any seen before, then record it too. Perform
2728 the alignment by inserting a magic 'padding' statement. */
2730 if (output_section_statement
->subsection_alignment
!= -1)
2731 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2733 o
= output_section_statement
->bfd_section
;
2734 if (o
->alignment_power
< i
->alignment_power
)
2735 o
->alignment_power
= i
->alignment_power
;
2737 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2739 if (alignment_needed
!= 0)
2741 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
2742 dot
+= alignment_needed
;
2745 /* Remember where in the output section this input section goes. */
2747 i
->output_offset
= dot
- o
->vma
;
2749 /* Mark how big the output section must be to contain this now. */
2750 dot
+= TO_ADDR (i
->size
);
2751 o
->size
= TO_SIZE (dot
- o
->vma
);
2755 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2761 #define IGNORE_SECTION(s) \
2762 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2763 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2764 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2767 /* Check to see if any allocated sections overlap with other allocated
2768 sections. This can happen when the linker script specifically specifies
2769 the output section addresses of the two sections. */
2772 lang_check_section_addresses (void)
2776 /* Scan all sections in the output list. */
2777 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2781 /* Ignore sections which are not loaded or which have no contents. */
2782 if (IGNORE_SECTION (s
))
2785 /* Once we reach section 's' stop our seach. This prevents two
2786 warning messages from being produced, one for 'section A overlaps
2787 section B' and one for 'section B overlaps section A'. */
2788 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2795 /* Only consider loadable sections with real contents. */
2796 if (IGNORE_SECTION (os
))
2799 /* We must check the sections' LMA addresses not their
2800 VMA addresses because overlay sections can have
2801 overlapping VMAs but they must have distinct LMAs. */
2802 s_start
= bfd_section_lma (output_bfd
, s
);
2803 os_start
= bfd_section_lma (output_bfd
, os
);
2804 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
2805 os_end
= os_start
+ TO_ADDR (os
->size
) - 1;
2807 /* Look for an overlap. */
2808 if ((s_end
< os_start
) || (s_start
> os_end
))
2812 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2813 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2815 /* Once we have found one overlap for this section,
2816 stop looking for others. */
2822 /* Make sure the new address is within the region. We explicitly permit the
2823 current address to be at the exact end of the region when the address is
2824 non-zero, in case the region is at the end of addressable memory and the
2825 calculation wraps around. */
2828 os_region_check (lang_output_section_statement_type
*os
,
2829 lang_memory_region_type
*region
,
2833 if ((region
->current
< region
->origin
2834 || (region
->current
- region
->origin
> region
->length
))
2835 && ((region
->current
!= region
->origin
+ region
->length
)
2840 einfo (_("%X%P: address 0x%v of %B section %s"
2841 " is not within region %s\n"),
2843 os
->bfd_section
->owner
,
2844 os
->bfd_section
->name
,
2849 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2851 os
->bfd_section
->owner
,
2852 os
->bfd_section
->name
);
2854 /* Reset the region pointer. */
2855 region
->current
= region
->origin
;
2859 /* Set the sizes for all the output sections. */
2862 lang_size_sections_1
2863 (lang_statement_union_type
*s
,
2864 lang_output_section_statement_type
*output_section_statement
,
2865 lang_statement_union_type
**prev
,
2869 bfd_boolean check_regions
)
2871 /* Size up the sections from their constituent parts. */
2872 for (; s
!= NULL
; s
= s
->header
.next
)
2874 switch (s
->header
.type
)
2876 case lang_output_section_statement_enum
:
2879 lang_output_section_statement_type
*os
;
2881 os
= &s
->output_section_statement
;
2882 if (os
->bfd_section
== NULL
)
2883 /* This section was never actually created. */
2886 /* If this is a COFF shared library section, use the size and
2887 address from the input section. FIXME: This is COFF
2888 specific; it would be cleaner if there were some other way
2889 to do this, but nothing simple comes to mind. */
2890 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2894 if (os
->children
.head
== NULL
2895 || os
->children
.head
->header
.next
!= NULL
2896 || (os
->children
.head
->header
.type
2897 != lang_input_section_enum
))
2898 einfo (_("%P%X: Internal error on COFF shared library"
2899 " section %s\n"), os
->name
);
2901 input
= os
->children
.head
->input_section
.section
;
2902 bfd_set_section_vma (os
->bfd_section
->owner
,
2904 bfd_section_vma (input
->owner
, input
));
2905 os
->bfd_section
->size
= input
->size
;
2909 if (bfd_is_abs_section (os
->bfd_section
))
2911 /* No matter what happens, an abs section starts at zero. */
2912 ASSERT (os
->bfd_section
->vma
== 0);
2916 if (os
->addr_tree
== NULL
)
2918 /* No address specified for this section, get one
2919 from the region specification. */
2920 if (os
->region
== NULL
2921 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
2922 && os
->region
->name
[0] == '*'
2923 && strcmp (os
->region
->name
,
2924 DEFAULT_MEMORY_REGION
) == 0))
2926 os
->region
= lang_memory_default (os
->bfd_section
);
2929 /* If a loadable section is using the default memory
2930 region, and some non default memory regions were
2931 defined, issue an error message. */
2932 if (!IGNORE_SECTION (os
->bfd_section
)
2933 && ! link_info
.relocatable
2935 && strcmp (os
->region
->name
,
2936 DEFAULT_MEMORY_REGION
) == 0
2937 && lang_memory_region_list
!= NULL
2938 && (strcmp (lang_memory_region_list
->name
,
2939 DEFAULT_MEMORY_REGION
) != 0
2940 || lang_memory_region_list
->next
!= NULL
))
2942 /* By default this is an error rather than just a
2943 warning because if we allocate the section to the
2944 default memory region we can end up creating an
2945 excessively large binary, or even seg faulting when
2946 attempting to perform a negative seek. See
2947 sources.redhat.com/ml/binutils/2003-04/msg00423.html
2948 for an example of this. This behaviour can be
2949 overridden by the using the --no-check-sections
2951 if (command_line
.check_section_addresses
)
2952 einfo (_("%P%F: error: no memory region specified"
2953 " for loadable section `%s'\n"),
2954 bfd_get_section_name (output_bfd
,
2957 einfo (_("%P: warning: no memory region specified"
2958 " for loadable section `%s'\n"),
2959 bfd_get_section_name (output_bfd
,
2963 dot
= os
->region
->current
;
2965 if (os
->section_alignment
== -1)
2970 dot
= align_power (dot
,
2971 os
->bfd_section
->alignment_power
);
2973 if (dot
!= olddot
&& config
.warn_section_align
)
2974 einfo (_("%P: warning: changing start of section"
2975 " %s by %u bytes\n"),
2976 os
->name
, (unsigned int) (dot
- olddot
));
2984 r
= exp_fold_tree (os
->addr_tree
,
2986 lang_allocating_phase_enum
,
2991 einfo (_("%F%S: non constant or forward reference"
2992 " address expression for section %s\n"),
2995 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2998 /* The section starts here.
2999 First, align to what the section needs. */
3001 if (os
->section_alignment
!= -1)
3002 dot
= align_power (dot
, os
->section_alignment
);
3004 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3006 os
->bfd_section
->output_offset
= 0;
3009 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3010 os
->fill
, dot
, relax
, check_regions
);
3012 /* Put the section within the requested block size, or
3013 align at the block boundary. */
3014 after
= ((os
->bfd_section
->vma
3015 + TO_ADDR (os
->bfd_section
->size
)
3016 + os
->block_value
- 1)
3017 & - (bfd_vma
) os
->block_value
);
3019 if (bfd_is_abs_section (os
->bfd_section
))
3020 ASSERT (after
== os
->bfd_section
->vma
);
3022 os
->bfd_section
->size
3023 = TO_SIZE (after
- os
->bfd_section
->vma
);
3025 dot
= os
->bfd_section
->vma
;
3026 /* .tbss sections effectively have zero size. */
3027 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3028 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3029 || link_info
.relocatable
)
3030 dot
+= TO_ADDR (os
->bfd_section
->size
);
3034 if (os
->update_dot_tree
!= 0)
3035 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3036 lang_allocating_phase_enum
, dot
, &dot
);
3038 /* Update dot in the region ?
3039 We only do this if the section is going to be allocated,
3040 since unallocated sections do not contribute to the region's
3041 overall size in memory.
3043 If the SEC_NEVER_LOAD bit is not set, it will affect the
3044 addresses of sections after it. We have to update
3046 if (os
->region
!= NULL
3047 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
3048 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
3050 os
->region
->current
= dot
;
3053 /* Make sure the new address is within the region. */
3054 os_region_check (os
, os
->region
, os
->addr_tree
,
3055 os
->bfd_section
->vma
);
3057 /* If there's no load address specified, use the run
3058 region as the load region. */
3059 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3060 os
->lma_region
= os
->region
;
3062 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3064 /* Set load_base, which will be handled later. */
3065 os
->load_base
= exp_intop (os
->lma_region
->current
);
3066 os
->lma_region
->current
+=
3067 TO_ADDR (os
->bfd_section
->size
);
3069 os_region_check (os
, os
->lma_region
, NULL
,
3070 os
->bfd_section
->lma
);
3076 case lang_constructors_statement_enum
:
3077 dot
= lang_size_sections_1 (constructor_list
.head
,
3078 output_section_statement
,
3079 &s
->wild_statement
.children
.head
,
3080 fill
, dot
, relax
, check_regions
);
3083 case lang_data_statement_enum
:
3085 unsigned int size
= 0;
3087 s
->data_statement
.output_vma
=
3088 dot
- output_section_statement
->bfd_section
->vma
;
3089 s
->data_statement
.output_section
=
3090 output_section_statement
->bfd_section
;
3092 /* We might refer to provided symbols in the expression, and
3093 need to mark them as needed. */
3094 exp_fold_tree (s
->data_statement
.exp
, abs_output_section
,
3095 lang_allocating_phase_enum
, dot
, &dot
);
3097 switch (s
->data_statement
.type
)
3115 if (size
< TO_SIZE ((unsigned) 1))
3116 size
= TO_SIZE ((unsigned) 1);
3117 dot
+= TO_ADDR (size
);
3118 output_section_statement
->bfd_section
->size
+= size
;
3119 /* The output section gets contents, and then we inspect for
3120 any flags set in the input script which override any ALLOC. */
3121 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3122 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3124 output_section_statement
->bfd_section
->flags
|=
3125 SEC_ALLOC
| SEC_LOAD
;
3130 case lang_reloc_statement_enum
:
3134 s
->reloc_statement
.output_vma
=
3135 dot
- output_section_statement
->bfd_section
->vma
;
3136 s
->reloc_statement
.output_section
=
3137 output_section_statement
->bfd_section
;
3138 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3139 dot
+= TO_ADDR (size
);
3140 output_section_statement
->bfd_section
->size
+= size
;
3144 case lang_wild_statement_enum
:
3146 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3147 output_section_statement
,
3148 &s
->wild_statement
.children
.head
,
3149 fill
, dot
, relax
, check_regions
);
3153 case lang_object_symbols_statement_enum
:
3154 link_info
.create_object_symbols_section
=
3155 output_section_statement
->bfd_section
;
3157 case lang_output_statement_enum
:
3158 case lang_target_statement_enum
:
3160 case lang_input_section_enum
:
3164 i
= (*prev
)->input_section
.section
;
3169 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3170 einfo (_("%P%F: can't relax section: %E\n"));
3174 dot
= size_input_section (prev
, output_section_statement
,
3175 output_section_statement
->fill
, dot
);
3178 case lang_input_statement_enum
:
3180 case lang_fill_statement_enum
:
3181 s
->fill_statement
.output_section
=
3182 output_section_statement
->bfd_section
;
3184 fill
= s
->fill_statement
.fill
;
3186 case lang_assignment_statement_enum
:
3188 bfd_vma newdot
= dot
;
3190 exp_fold_tree (s
->assignment_statement
.exp
,
3191 output_section_statement
,
3192 lang_allocating_phase_enum
,
3198 if (output_section_statement
== abs_output_section
)
3200 /* If we don't have an output section, then just adjust
3201 the default memory address. */
3202 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
3203 FALSE
)->current
= newdot
;
3207 /* Insert a pad after this statement. We can't
3208 put the pad before when relaxing, in case the
3209 assignment references dot. */
3210 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
3211 output_section_statement
->bfd_section
, dot
);
3213 /* Don't neuter the pad below when relaxing. */
3217 /* If dot is advanced, this implies that the section should
3218 have space allocated to it, unless the user has explicitly
3219 stated that the section should never be loaded. */
3220 if (!(output_section_statement
->flags
3221 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
3222 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
3229 case lang_padding_statement_enum
:
3230 /* If this is the first time lang_size_sections is called,
3231 we won't have any padding statements. If this is the
3232 second or later passes when relaxing, we should allow
3233 padding to shrink. If padding is needed on this pass, it
3234 will be added back in. */
3235 s
->padding_statement
.size
= 0;
3237 /* Make sure output_offset is valid. If relaxation shrinks
3238 the section and this pad isn't needed, it's possible to
3239 have output_offset larger than the final size of the
3240 section. bfd_set_section_contents will complain even for
3241 a pad size of zero. */
3242 s
->padding_statement
.output_offset
3243 = dot
- output_section_statement
->bfd_section
->vma
;
3246 case lang_group_statement_enum
:
3247 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3248 output_section_statement
,
3249 &s
->group_statement
.children
.head
,
3250 fill
, dot
, relax
, check_regions
);
3257 /* We can only get here when relaxing is turned on. */
3258 case lang_address_statement_enum
:
3261 prev
= &s
->header
.next
;
3268 (lang_statement_union_type
*s
,
3269 lang_output_section_statement_type
*output_section_statement
,
3270 lang_statement_union_type
**prev
,
3274 bfd_boolean check_regions
)
3278 /* Callers of exp_fold_tree need to increment this. */
3279 lang_statement_iteration
++;
3281 exp_data_seg
.phase
= exp_dataseg_none
;
3282 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3283 dot
, relax
, check_regions
);
3284 if (exp_data_seg
.phase
== exp_dataseg_end_seen
3285 && link_info
.relro
&& exp_data_seg
.relro_end
)
3287 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3288 to put exp_data_seg.relro on a (common) page boundary. */
3290 exp_data_seg
.phase
= exp_dataseg_relro_adjust
;
3291 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3292 dot
, relax
, check_regions
);
3293 link_info
.relro_start
= exp_data_seg
.base
;
3294 link_info
.relro_end
= exp_data_seg
.relro_end
;
3296 else if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3298 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3299 a page could be saved in the data segment. */
3300 bfd_vma first
, last
;
3302 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3303 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3305 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3306 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3307 && first
+ last
<= exp_data_seg
.pagesize
)
3309 exp_data_seg
.phase
= exp_dataseg_adjust
;
3310 lang_statement_iteration
++;
3311 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3312 fill
, dot
, relax
, check_regions
);
3319 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3322 lang_do_assignments_1
3323 (lang_statement_union_type
*s
,
3324 lang_output_section_statement_type
*output_section_statement
,
3328 for (; s
!= NULL
; s
= s
->header
.next
)
3330 switch (s
->header
.type
)
3332 case lang_constructors_statement_enum
:
3333 dot
= lang_do_assignments_1 (constructor_list
.head
,
3334 output_section_statement
,
3339 case lang_output_section_statement_enum
:
3341 lang_output_section_statement_type
*os
;
3343 os
= &(s
->output_section_statement
);
3344 if (os
->bfd_section
!= NULL
)
3346 dot
= os
->bfd_section
->vma
;
3347 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
3348 /* .tbss sections effectively have zero size. */
3349 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3350 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3351 || link_info
.relocatable
)
3352 dot
+= TO_ADDR (os
->bfd_section
->size
);
3356 /* If nothing has been placed into the output section then
3357 it won't have a bfd_section. */
3358 if (os
->bfd_section
)
3360 os
->bfd_section
->lma
3361 = exp_get_abs_int (os
->load_base
, 0, "load base",
3362 lang_final_phase_enum
);
3367 case lang_wild_statement_enum
:
3369 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
3370 output_section_statement
,
3375 case lang_object_symbols_statement_enum
:
3376 case lang_output_statement_enum
:
3377 case lang_target_statement_enum
:
3379 case lang_common_statement_enum
:
3382 case lang_data_statement_enum
:
3384 etree_value_type value
;
3386 value
= exp_fold_tree (s
->data_statement
.exp
,
3388 lang_final_phase_enum
, dot
, &dot
);
3390 einfo (_("%F%P: invalid data statement\n"));
3391 s
->data_statement
.value
3392 = value
.value
+ value
.section
->bfd_section
->vma
;
3396 switch (s
->data_statement
.type
)
3414 if (size
< TO_SIZE ((unsigned) 1))
3415 size
= TO_SIZE ((unsigned) 1);
3416 dot
+= TO_ADDR (size
);
3420 case lang_reloc_statement_enum
:
3422 etree_value_type value
;
3424 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3426 lang_final_phase_enum
, dot
, &dot
);
3427 s
->reloc_statement
.addend_value
= value
.value
;
3429 einfo (_("%F%P: invalid reloc statement\n"));
3431 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
3434 case lang_input_section_enum
:
3436 asection
*in
= s
->input_section
.section
;
3438 if ((in
->flags
& SEC_EXCLUDE
) == 0)
3439 dot
+= TO_ADDR (in
->size
);
3443 case lang_input_statement_enum
:
3445 case lang_fill_statement_enum
:
3446 fill
= s
->fill_statement
.fill
;
3448 case lang_assignment_statement_enum
:
3450 exp_fold_tree (s
->assignment_statement
.exp
,
3451 output_section_statement
,
3452 lang_final_phase_enum
,
3458 case lang_padding_statement_enum
:
3459 dot
+= TO_ADDR (s
->padding_statement
.size
);
3462 case lang_group_statement_enum
:
3463 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
3464 output_section_statement
,
3472 case lang_address_statement_enum
:
3482 (lang_statement_union_type
*s
,
3483 lang_output_section_statement_type
*output_section_statement
,
3487 /* Callers of exp_fold_tree need to increment this. */
3488 lang_statement_iteration
++;
3489 lang_do_assignments_1 (s
, output_section_statement
, fill
, dot
);
3492 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3493 operator .startof. (section_name), it produces an undefined symbol
3494 .startof.section_name. Similarly, when it sees
3495 .sizeof. (section_name), it produces an undefined symbol
3496 .sizeof.section_name. For all the output sections, we look for
3497 such symbols, and set them to the correct value. */
3500 lang_set_startof (void)
3504 if (link_info
.relocatable
)
3507 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3509 const char *secname
;
3511 struct bfd_link_hash_entry
*h
;
3513 secname
= bfd_get_section_name (output_bfd
, s
);
3514 buf
= xmalloc (10 + strlen (secname
));
3516 sprintf (buf
, ".startof.%s", secname
);
3517 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3518 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3520 h
->type
= bfd_link_hash_defined
;
3521 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3522 h
->u
.def
.section
= bfd_abs_section_ptr
;
3525 sprintf (buf
, ".sizeof.%s", secname
);
3526 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3527 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3529 h
->type
= bfd_link_hash_defined
;
3530 h
->u
.def
.value
= TO_ADDR (s
->size
);
3531 h
->u
.def
.section
= bfd_abs_section_ptr
;
3541 struct bfd_link_hash_entry
*h
;
3544 if (link_info
.relocatable
|| link_info
.shared
)
3549 if (entry_symbol
.name
== NULL
)
3551 /* No entry has been specified. Look for start, but don't warn
3552 if we don't find it. */
3553 entry_symbol
.name
= "start";
3557 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3558 FALSE
, FALSE
, TRUE
);
3560 && (h
->type
== bfd_link_hash_defined
3561 || h
->type
== bfd_link_hash_defweak
)
3562 && h
->u
.def
.section
->output_section
!= NULL
)
3566 val
= (h
->u
.def
.value
3567 + bfd_get_section_vma (output_bfd
,
3568 h
->u
.def
.section
->output_section
)
3569 + h
->u
.def
.section
->output_offset
);
3570 if (! bfd_set_start_address (output_bfd
, val
))
3571 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3578 /* We couldn't find the entry symbol. Try parsing it as a
3580 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3583 if (! bfd_set_start_address (output_bfd
, val
))
3584 einfo (_("%P%F: can't set start address\n"));
3590 /* Can't find the entry symbol, and it's not a number. Use
3591 the first address in the text section. */
3592 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3596 einfo (_("%P: warning: cannot find entry symbol %s;"
3597 " defaulting to %V\n"),
3599 bfd_get_section_vma (output_bfd
, ts
));
3600 if (! bfd_set_start_address (output_bfd
,
3601 bfd_get_section_vma (output_bfd
,
3603 einfo (_("%P%F: can't set start address\n"));
3608 einfo (_("%P: warning: cannot find entry symbol %s;"
3609 " not setting start address\n"),
3615 /* Don't bfd_hash_table_free (&lang_definedness_table);
3616 map file output may result in a call of lang_track_definedness. */
3619 /* This is a small function used when we want to ignore errors from
3623 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3625 /* Don't do anything. */
3628 /* Check that the architecture of all the input files is compatible
3629 with the output file. Also call the backend to let it do any
3630 other checking that is needed. */
3635 lang_statement_union_type
*file
;
3637 const bfd_arch_info_type
*compatible
;
3639 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
3641 input_bfd
= file
->input_statement
.the_bfd
;
3643 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
3644 command_line
.accept_unknown_input_arch
);
3646 /* In general it is not possible to perform a relocatable
3647 link between differing object formats when the input
3648 file has relocations, because the relocations in the
3649 input format may not have equivalent representations in
3650 the output format (and besides BFD does not translate
3651 relocs for other link purposes than a final link). */
3652 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
3653 && (compatible
== NULL
3654 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3655 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3657 einfo (_("%P%F: Relocatable linking with relocations from"
3658 " format %s (%B) to format %s (%B) is not supported\n"),
3659 bfd_get_target (input_bfd
), input_bfd
,
3660 bfd_get_target (output_bfd
), output_bfd
);
3661 /* einfo with %F exits. */
3664 if (compatible
== NULL
)
3666 if (command_line
.warn_mismatch
)
3667 einfo (_("%P: warning: %s architecture of input file `%B'"
3668 " is incompatible with %s output\n"),
3669 bfd_printable_name (input_bfd
), input_bfd
,
3670 bfd_printable_name (output_bfd
));
3672 else if (bfd_count_sections (input_bfd
))
3674 /* If the input bfd has no contents, it shouldn't set the
3675 private data of the output bfd. */
3677 bfd_error_handler_type pfn
= NULL
;
3679 /* If we aren't supposed to warn about mismatched input
3680 files, temporarily set the BFD error handler to a
3681 function which will do nothing. We still want to call
3682 bfd_merge_private_bfd_data, since it may set up
3683 information which is needed in the output file. */
3684 if (! command_line
.warn_mismatch
)
3685 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3686 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3688 if (command_line
.warn_mismatch
)
3689 einfo (_("%P%X: failed to merge target specific data"
3690 " of file %B\n"), input_bfd
);
3692 if (! command_line
.warn_mismatch
)
3693 bfd_set_error_handler (pfn
);
3698 /* Look through all the global common symbols and attach them to the
3699 correct section. The -sort-common command line switch may be used
3700 to roughly sort the entries by size. */
3705 if (command_line
.inhibit_common_definition
)
3707 if (link_info
.relocatable
3708 && ! command_line
.force_common_definition
)
3711 if (! config
.sort_common
)
3712 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
3717 for (power
= 4; power
>= 0; power
--)
3718 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
3722 /* Place one common symbol in the correct section. */
3725 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
3727 unsigned int power_of_two
;
3731 if (h
->type
!= bfd_link_hash_common
)
3735 power_of_two
= h
->u
.c
.p
->alignment_power
;
3737 if (config
.sort_common
3738 && power_of_two
< (unsigned int) *(int *) info
)
3741 section
= h
->u
.c
.p
->section
;
3743 /* Increase the size of the section to align the common sym. */
3744 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
3745 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
3747 /* Adjust the alignment if necessary. */
3748 if (power_of_two
> section
->alignment_power
)
3749 section
->alignment_power
= power_of_two
;
3751 /* Change the symbol from common to defined. */
3752 h
->type
= bfd_link_hash_defined
;
3753 h
->u
.def
.section
= section
;
3754 h
->u
.def
.value
= section
->size
;
3756 /* Increase the size of the section. */
3757 section
->size
+= size
;
3759 /* Make sure the section is allocated in memory, and make sure that
3760 it is no longer a common section. */
3761 section
->flags
|= SEC_ALLOC
;
3762 section
->flags
&= ~SEC_IS_COMMON
;
3764 if (config
.map_file
!= NULL
)
3766 static bfd_boolean header_printed
;
3771 if (! header_printed
)
3773 minfo (_("\nAllocating common symbols\n"));
3774 minfo (_("Common symbol size file\n\n"));
3775 header_printed
= TRUE
;
3778 name
= demangle (h
->root
.string
);
3780 len
= strlen (name
);
3795 if (size
<= 0xffffffff)
3796 sprintf (buf
, "%lx", (unsigned long) size
);
3798 sprintf_vma (buf
, size
);
3808 minfo ("%B\n", section
->owner
);
3814 /* Run through the input files and ensure that every input section has
3815 somewhere to go. If one is found without a destination then create
3816 an input request and place it into the statement tree. */
3819 lang_place_orphans (void)
3821 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3825 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3827 if (s
->output_section
== NULL
)
3829 /* This section of the file is not attached, root
3830 around for a sensible place for it to go. */
3832 if (file
->just_syms_flag
)
3835 if ((s
->flags
& SEC_EXCLUDE
) != 0)
3836 s
->output_section
= bfd_abs_section_ptr
;
3837 else if (strcmp (s
->name
, "COMMON") == 0)
3839 /* This is a lonely common section which must have
3840 come from an archive. We attach to the section
3841 with the wildcard. */
3842 if (! link_info
.relocatable
3843 || command_line
.force_common_definition
)
3845 if (default_common_section
== NULL
)
3848 /* This message happens when using the
3849 svr3.ifile linker script, so I have
3851 info_msg (_("%P: no [COMMON] command,"
3852 " defaulting to .bss\n"));
3854 default_common_section
=
3855 lang_output_section_statement_lookup (".bss");
3858 lang_add_section (&default_common_section
->children
, s
,
3859 default_common_section
, file
);
3862 else if (ldemul_place_orphan (file
, s
))
3866 lang_output_section_statement_type
*os
;
3868 os
= lang_output_section_statement_lookup (s
->name
);
3869 lang_add_section (&os
->children
, s
, os
, file
);
3877 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
3879 flagword
*ptr_flags
;
3881 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3887 *ptr_flags
|= SEC_ALLOC
;
3891 *ptr_flags
|= SEC_READONLY
;
3895 *ptr_flags
|= SEC_DATA
;
3899 *ptr_flags
|= SEC_CODE
;
3904 *ptr_flags
|= SEC_LOAD
;
3908 einfo (_("%P%F: invalid syntax in flags\n"));
3915 /* Call a function on each input file. This function will be called
3916 on an archive, but not on the elements. */
3919 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
3921 lang_input_statement_type
*f
;
3923 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3925 f
= (lang_input_statement_type
*) f
->next_real_file
)
3929 /* Call a function on each file. The function will be called on all
3930 the elements of an archive which are included in the link, but will
3931 not be called on the archive file itself. */
3934 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
3936 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3943 ldlang_add_file (lang_input_statement_type
*entry
)
3947 lang_statement_append (&file_chain
,
3948 (lang_statement_union_type
*) entry
,
3951 /* The BFD linker needs to have a list of all input BFDs involved in
3953 ASSERT (entry
->the_bfd
->link_next
== NULL
);
3954 ASSERT (entry
->the_bfd
!= output_bfd
);
3955 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
3957 *pp
= entry
->the_bfd
;
3958 entry
->the_bfd
->usrdata
= entry
;
3959 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3961 /* Look through the sections and check for any which should not be
3962 included in the link. We need to do this now, so that we can
3963 notice when the backend linker tries to report multiple
3964 definition errors for symbols which are in sections we aren't
3965 going to link. FIXME: It might be better to entirely ignore
3966 symbols which are defined in sections which are going to be
3967 discarded. This would require modifying the backend linker for
3968 each backend which might set the SEC_LINK_ONCE flag. If we do
3969 this, we should probably handle SEC_EXCLUDE in the same way. */
3971 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
3975 lang_add_output (const char *name
, int from_script
)
3977 /* Make -o on command line override OUTPUT in script. */
3978 if (!had_output_filename
|| !from_script
)
3980 output_filename
= name
;
3981 had_output_filename
= TRUE
;
3985 static lang_output_section_statement_type
*current_section
;
3996 for (l
= 0; l
< 32; l
++)
3998 if (i
>= (unsigned int) x
)
4006 lang_output_section_statement_type
*
4007 lang_enter_output_section_statement (const char *output_section_statement_name
,
4008 etree_type
*address_exp
,
4009 enum section_type sectype
,
4011 etree_type
*subalign
,
4015 lang_output_section_statement_type
*os
;
4019 lang_output_section_statement_lookup_1 (output_section_statement_name
,
4022 /* Add this statement to tree. */
4024 add_statement (lang_output_section_statement_enum
,
4025 output_section_statement
);
4027 /* Make next things chain into subchain of this. */
4029 if (os
->addr_tree
== NULL
)
4031 os
->addr_tree
= address_exp
;
4033 os
->sectype
= sectype
;
4034 if (sectype
!= noload_section
)
4035 os
->flags
= SEC_NO_FLAGS
;
4037 os
->flags
= SEC_NEVER_LOAD
;
4038 os
->block_value
= 1;
4039 stat_ptr
= &os
->children
;
4041 os
->subsection_alignment
=
4042 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4043 os
->section_alignment
=
4044 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4046 os
->load_base
= ebase
;
4053 lang_output_statement_type
*new =
4054 new_stat (lang_output_statement
, stat_ptr
);
4056 new->name
= output_filename
;
4059 /* Reset the current counters in the regions. */
4062 lang_reset_memory_regions (void)
4064 lang_memory_region_type
*p
= lang_memory_region_list
;
4067 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
4069 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4070 p
->current
= p
->origin
;
4073 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4075 /* Save the last size for possible use by bfd_relax_section. */
4076 o
->rawsize
= o
->size
;
4081 /* Worker for lang_gc_sections_1. */
4084 gc_section_callback (lang_wild_statement_type
*ptr
,
4085 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
4087 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
4088 void *data ATTRIBUTE_UNUSED
)
4090 /* If the wild pattern was marked KEEP, the member sections
4091 should be as well. */
4092 if (ptr
->keep_sections
)
4093 section
->flags
|= SEC_KEEP
;
4096 /* Iterate over sections marking them against GC. */
4099 lang_gc_sections_1 (lang_statement_union_type
*s
)
4101 for (; s
!= NULL
; s
= s
->header
.next
)
4103 switch (s
->header
.type
)
4105 case lang_wild_statement_enum
:
4106 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
4108 case lang_constructors_statement_enum
:
4109 lang_gc_sections_1 (constructor_list
.head
);
4111 case lang_output_section_statement_enum
:
4112 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4114 case lang_group_statement_enum
:
4115 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4124 lang_gc_sections (void)
4126 struct bfd_link_hash_entry
*h
;
4127 ldlang_undef_chain_list_type
*ulist
;
4129 /* Keep all sections so marked in the link script. */
4131 lang_gc_sections_1 (statement_list
.head
);
4133 /* Keep all sections containing symbols undefined on the command-line,
4134 and the section containing the entry symbol. */
4136 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4138 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4139 FALSE
, FALSE
, FALSE
);
4142 && (h
->type
== bfd_link_hash_defined
4143 || h
->type
== bfd_link_hash_defweak
)
4144 && ! bfd_is_abs_section (h
->u
.def
.section
))
4146 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4150 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4151 the special case of debug info. (See bfd/stabs.c)
4152 Twiddle the flag here, to simplify later linker code. */
4153 if (link_info
.relocatable
)
4155 LANG_FOR_EACH_INPUT_STATEMENT (f
)
4158 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4159 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
4160 sec
->flags
&= ~SEC_EXCLUDE
;
4164 if (command_line
.gc_sections
)
4165 bfd_gc_sections (output_bfd
, &link_info
);
4171 lang_reasonable_defaults ();
4172 current_target
= default_target
;
4174 /* Open the output file. */
4175 lang_for_each_statement (ldlang_open_output
);
4178 ldemul_create_output_section_statements ();
4180 /* Add to the hash table all undefineds on the command line. */
4181 lang_place_undefineds ();
4183 if (!bfd_section_already_linked_table_init ())
4184 einfo (_("%P%F: Failed to create hash table\n"));
4186 /* Create a bfd for each input file. */
4187 current_target
= default_target
;
4188 open_input_bfds (statement_list
.head
, FALSE
);
4190 link_info
.gc_sym_list
= &entry_symbol
;
4191 if (entry_symbol
.name
== NULL
)
4192 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4194 ldemul_after_open ();
4196 bfd_section_already_linked_table_free ();
4198 /* Make sure that we're not mixing architectures. We call this
4199 after all the input files have been opened, but before we do any
4200 other processing, so that any operations merge_private_bfd_data
4201 does on the output file will be known during the rest of the
4205 /* Handle .exports instead of a version script if we're told to do so. */
4206 if (command_line
.version_exports_section
)
4207 lang_do_version_exports_section ();
4209 /* Build all sets based on the information gathered from the input
4211 ldctor_build_sets ();
4213 /* Remove unreferenced sections if asked to. */
4214 lang_gc_sections ();
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
)
4230 /* Merge SEC_MERGE sections. This has to be done after GC of
4231 sections, so that GCed sections are not merged, but before
4232 assigning dynamic symbols, since removing whole input sections
4234 bfd_merge_sections (output_bfd
, &link_info
);
4236 /* Look for a text section and set the readonly attribute in it. */
4237 found
= bfd_get_section_by_name (output_bfd
, ".text");
4241 if (config
.text_read_only
)
4242 found
->flags
|= SEC_READONLY
;
4244 found
->flags
&= ~SEC_READONLY
;
4248 /* Do anything special before sizing sections. This is where ELF
4249 and other back-ends size dynamic sections. */
4250 ldemul_before_allocation ();
4252 if (!link_info
.relocatable
)
4253 strip_excluded_output_sections ();
4255 /* We must record the program headers before we try to fix the
4256 section positions, since they will affect SIZEOF_HEADERS. */
4257 lang_record_phdrs ();
4259 /* Size up the sections. */
4260 lang_size_sections (statement_list
.head
, abs_output_section
,
4261 &statement_list
.head
, 0, 0, NULL
,
4262 command_line
.relax
? FALSE
: TRUE
);
4264 /* Now run around and relax if we can. */
4265 if (command_line
.relax
)
4267 /* Keep relaxing until bfd_relax_section gives up. */
4268 bfd_boolean relax_again
;
4272 relax_again
= FALSE
;
4274 /* Note: pe-dll.c does something like this also. If you find
4275 you need to change this code, you probably need to change
4276 pe-dll.c also. DJ */
4278 /* Do all the assignments with our current guesses as to
4280 lang_do_assignments (statement_list
.head
, abs_output_section
,
4283 /* We must do this after lang_do_assignments, because it uses
4285 lang_reset_memory_regions ();
4287 /* Perform another relax pass - this time we know where the
4288 globals are, so can make a better guess. */
4289 lang_size_sections (statement_list
.head
, abs_output_section
,
4290 &statement_list
.head
, 0, 0, &relax_again
, FALSE
);
4292 /* If the normal relax is done and the relax finalize pass
4293 is not performed yet, we perform another relax pass. */
4294 if (!relax_again
&& link_info
.need_relax_finalize
)
4296 link_info
.need_relax_finalize
= FALSE
;
4300 while (relax_again
);
4302 /* Final extra sizing to report errors. */
4303 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4304 lang_reset_memory_regions ();
4305 lang_size_sections (statement_list
.head
, abs_output_section
,
4306 &statement_list
.head
, 0, 0, NULL
, TRUE
);
4309 /* See if anything special should be done now we know how big
4311 ldemul_after_allocation ();
4313 /* Fix any .startof. or .sizeof. symbols. */
4314 lang_set_startof ();
4316 /* Do all the assignments, now that we know the final resting places
4317 of all the symbols. */
4319 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4321 /* Make sure that the section addresses make sense. */
4322 if (! link_info
.relocatable
4323 && command_line
.check_section_addresses
)
4324 lang_check_section_addresses ();
4332 /* EXPORTED TO YACC */
4335 lang_add_wild (struct wildcard_spec
*filespec
,
4336 struct wildcard_list
*section_list
,
4337 bfd_boolean keep_sections
)
4339 struct wildcard_list
*curr
, *next
;
4340 lang_wild_statement_type
*new;
4342 /* Reverse the list as the parser puts it back to front. */
4343 for (curr
= section_list
, section_list
= NULL
;
4345 section_list
= curr
, curr
= next
)
4347 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4348 placed_commons
= TRUE
;
4351 curr
->next
= section_list
;
4354 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4356 if (strcmp (filespec
->name
, "*") == 0)
4357 filespec
->name
= NULL
;
4358 else if (! wildcardp (filespec
->name
))
4359 lang_has_input_file
= TRUE
;
4362 new = new_stat (lang_wild_statement
, stat_ptr
);
4363 new->filename
= NULL
;
4364 new->filenames_sorted
= FALSE
;
4365 if (filespec
!= NULL
)
4367 new->filename
= filespec
->name
;
4368 new->filenames_sorted
= filespec
->sorted
;
4370 new->section_list
= section_list
;
4371 new->keep_sections
= keep_sections
;
4372 lang_list_init (&new->children
);
4376 lang_section_start (const char *name
, etree_type
*address
)
4378 lang_address_statement_type
*ad
;
4380 ad
= new_stat (lang_address_statement
, stat_ptr
);
4381 ad
->section_name
= name
;
4382 ad
->address
= address
;
4385 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4386 because of a -e argument on the command line, or zero if this is
4387 called by ENTRY in a linker script. Command line arguments take
4391 lang_add_entry (const char *name
, bfd_boolean cmdline
)
4393 if (entry_symbol
.name
== NULL
4395 || ! entry_from_cmdline
)
4397 entry_symbol
.name
= name
;
4398 entry_from_cmdline
= cmdline
;
4403 lang_add_target (const char *name
)
4405 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4413 lang_add_map (const char *name
)
4420 map_option_f
= TRUE
;
4428 lang_add_fill (fill_type
*fill
)
4430 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4437 lang_add_data (int type
, union etree_union
*exp
)
4440 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4448 /* Create a new reloc statement. RELOC is the BFD relocation type to
4449 generate. HOWTO is the corresponding howto structure (we could
4450 look this up, but the caller has already done so). SECTION is the
4451 section to generate a reloc against, or NAME is the name of the
4452 symbol to generate a reloc against. Exactly one of SECTION and
4453 NAME must be NULL. ADDEND is an expression for the addend. */
4456 lang_add_reloc (bfd_reloc_code_real_type reloc
,
4457 reloc_howto_type
*howto
,
4460 union etree_union
*addend
)
4462 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4466 p
->section
= section
;
4468 p
->addend_exp
= addend
;
4470 p
->addend_value
= 0;
4471 p
->output_section
= NULL
;
4475 lang_assignment_statement_type
*
4476 lang_add_assignment (etree_type
*exp
)
4478 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4486 lang_add_attribute (enum statement_enum attribute
)
4488 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4492 lang_startup (const char *name
)
4494 if (startup_file
!= NULL
)
4496 einfo (_("%P%Fmultiple STARTUP files\n"));
4498 first_file
->filename
= name
;
4499 first_file
->local_sym_name
= name
;
4500 first_file
->real
= TRUE
;
4502 startup_file
= name
;
4506 lang_float (bfd_boolean maybe
)
4508 lang_float_flag
= maybe
;
4512 /* Work out the load- and run-time regions from a script statement, and
4513 store them in *LMA_REGION and *REGION respectively.
4515 MEMSPEC is the name of the run-time region, or the value of
4516 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4517 LMA_MEMSPEC is the name of the load-time region, or null if the
4518 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4519 had an explicit load address.
4521 It is an error to specify both a load region and a load address. */
4524 lang_get_regions (lang_memory_region_type
**region
,
4525 lang_memory_region_type
**lma_region
,
4526 const char *memspec
,
4527 const char *lma_memspec
,
4528 bfd_boolean have_lma
,
4529 bfd_boolean have_vma
)
4531 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
4533 /* If no runtime region or VMA has been specified, but the load region
4534 has been specified, then use the load region for the runtime region
4536 if (lma_memspec
!= NULL
4538 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
4539 *region
= *lma_region
;
4541 *region
= lang_memory_region_lookup (memspec
, FALSE
);
4543 if (have_lma
&& lma_memspec
!= 0)
4544 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4548 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
4549 lang_output_section_phdr_list
*phdrs
,
4550 const char *lma_memspec
)
4552 lang_get_regions (¤t_section
->region
,
4553 ¤t_section
->lma_region
,
4554 memspec
, lma_memspec
,
4555 current_section
->load_base
!= NULL
,
4556 current_section
->addr_tree
!= NULL
);
4557 current_section
->fill
= fill
;
4558 current_section
->phdrs
= phdrs
;
4559 stat_ptr
= &statement_list
;
4562 /* Create an absolute symbol with the given name with the value of the
4563 address of first byte of the section named.
4565 If the symbol already exists, then do nothing. */
4568 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
4570 struct bfd_link_hash_entry
*h
;
4572 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4574 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4576 if (h
->type
== bfd_link_hash_new
4577 || h
->type
== bfd_link_hash_undefined
)
4581 h
->type
= bfd_link_hash_defined
;
4583 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4587 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4589 h
->u
.def
.section
= bfd_abs_section_ptr
;
4593 /* Create an absolute symbol with the given name with the value of the
4594 address of the first byte after the end of the section named.
4596 If the symbol already exists, then do nothing. */
4599 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
4601 struct bfd_link_hash_entry
*h
;
4603 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4605 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4607 if (h
->type
== bfd_link_hash_new
4608 || h
->type
== bfd_link_hash_undefined
)
4612 h
->type
= bfd_link_hash_defined
;
4614 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4618 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4619 + TO_ADDR (sec
->size
));
4621 h
->u
.def
.section
= bfd_abs_section_ptr
;
4626 lang_statement_append (lang_statement_list_type
*list
,
4627 lang_statement_union_type
*element
,
4628 lang_statement_union_type
**field
)
4630 *(list
->tail
) = element
;
4634 /* Set the output format type. -oformat overrides scripts. */
4637 lang_add_output_format (const char *format
,
4642 if (output_target
== NULL
|| !from_script
)
4644 if (command_line
.endian
== ENDIAN_BIG
4647 else if (command_line
.endian
== ENDIAN_LITTLE
4651 output_target
= format
;
4655 /* Enter a group. This creates a new lang_group_statement, and sets
4656 stat_ptr to build new statements within the group. */
4659 lang_enter_group (void)
4661 lang_group_statement_type
*g
;
4663 g
= new_stat (lang_group_statement
, stat_ptr
);
4664 lang_list_init (&g
->children
);
4665 stat_ptr
= &g
->children
;
4668 /* Leave a group. This just resets stat_ptr to start writing to the
4669 regular list of statements again. Note that this will not work if
4670 groups can occur inside anything else which can adjust stat_ptr,
4671 but currently they can't. */
4674 lang_leave_group (void)
4676 stat_ptr
= &statement_list
;
4679 /* Add a new program header. This is called for each entry in a PHDRS
4680 command in a linker script. */
4683 lang_new_phdr (const char *name
,
4685 bfd_boolean filehdr
,
4690 struct lang_phdr
*n
, **pp
;
4692 n
= stat_alloc (sizeof (struct lang_phdr
));
4695 n
->type
= exp_get_value_int (type
, 0, "program header type",
4696 lang_final_phase_enum
);
4697 n
->filehdr
= filehdr
;
4702 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4707 /* Record the program header information in the output BFD. FIXME: We
4708 should not be calling an ELF specific function here. */
4711 lang_record_phdrs (void)
4715 lang_output_section_phdr_list
*last
;
4716 struct lang_phdr
*l
;
4717 lang_statement_union_type
*u
;
4720 secs
= xmalloc (alc
* sizeof (asection
*));
4722 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4729 for (u
= lang_output_section_statement
.head
;
4731 u
= u
->output_section_statement
.next
)
4733 lang_output_section_statement_type
*os
;
4734 lang_output_section_phdr_list
*pl
;
4736 os
= &u
->output_section_statement
;
4737 if (os
->constraint
== -1)
4745 if (os
->sectype
== noload_section
4746 || os
->bfd_section
== NULL
4747 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4752 if (os
->bfd_section
== NULL
)
4755 for (; pl
!= NULL
; pl
= pl
->next
)
4757 if (strcmp (pl
->name
, l
->name
) == 0)
4762 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
4764 secs
[c
] = os
->bfd_section
;
4771 if (l
->flags
== NULL
)
4774 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4775 lang_final_phase_enum
);
4780 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4781 lang_final_phase_enum
);
4783 if (! bfd_record_phdr (output_bfd
, l
->type
,
4784 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4785 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4786 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4791 /* Make sure all the phdr assignments succeeded. */
4792 for (u
= lang_output_section_statement
.head
;
4794 u
= u
->output_section_statement
.next
)
4796 lang_output_section_phdr_list
*pl
;
4798 if (u
->output_section_statement
.constraint
== -1
4799 || u
->output_section_statement
.bfd_section
== NULL
)
4802 for (pl
= u
->output_section_statement
.phdrs
;
4805 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4806 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4807 u
->output_section_statement
.name
, pl
->name
);
4811 /* Record a list of sections which may not be cross referenced. */
4814 lang_add_nocrossref (lang_nocrossref_type
*l
)
4816 struct lang_nocrossrefs
*n
;
4818 n
= xmalloc (sizeof *n
);
4819 n
->next
= nocrossref_list
;
4821 nocrossref_list
= n
;
4823 /* Set notice_all so that we get informed about all symbols. */
4824 link_info
.notice_all
= TRUE
;
4827 /* Overlay handling. We handle overlays with some static variables. */
4829 /* The overlay virtual address. */
4830 static etree_type
*overlay_vma
;
4831 /* And subsection alignment. */
4832 static etree_type
*overlay_subalign
;
4834 /* An expression for the maximum section size seen so far. */
4835 static etree_type
*overlay_max
;
4837 /* A list of all the sections in this overlay. */
4839 struct overlay_list
{
4840 struct overlay_list
*next
;
4841 lang_output_section_statement_type
*os
;
4844 static struct overlay_list
*overlay_list
;
4846 /* Start handling an overlay. */
4849 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
4851 /* The grammar should prevent nested overlays from occurring. */
4852 ASSERT (overlay_vma
== NULL
4853 && overlay_subalign
== NULL
4854 && overlay_max
== NULL
);
4856 overlay_vma
= vma_expr
;
4857 overlay_subalign
= subalign
;
4860 /* Start a section in an overlay. We handle this by calling
4861 lang_enter_output_section_statement with the correct VMA.
4862 lang_leave_overlay sets up the LMA and memory regions. */
4865 lang_enter_overlay_section (const char *name
)
4867 struct overlay_list
*n
;
4870 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4871 0, overlay_subalign
, 0, 0);
4873 /* If this is the first section, then base the VMA of future
4874 sections on this one. This will work correctly even if `.' is
4875 used in the addresses. */
4876 if (overlay_list
== NULL
)
4877 overlay_vma
= exp_nameop (ADDR
, name
);
4879 /* Remember the section. */
4880 n
= xmalloc (sizeof *n
);
4881 n
->os
= current_section
;
4882 n
->next
= overlay_list
;
4885 size
= exp_nameop (SIZEOF
, name
);
4887 /* Arrange to work out the maximum section end address. */
4888 if (overlay_max
== NULL
)
4891 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4894 /* Finish a section in an overlay. There isn't any special to do
4898 lang_leave_overlay_section (fill_type
*fill
,
4899 lang_output_section_phdr_list
*phdrs
)
4906 name
= current_section
->name
;
4908 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4909 region and that no load-time region has been specified. It doesn't
4910 really matter what we say here, since lang_leave_overlay will
4912 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
4914 /* Define the magic symbols. */
4916 clean
= xmalloc (strlen (name
) + 1);
4918 for (s1
= name
; *s1
!= '\0'; s1
++)
4919 if (ISALNUM (*s1
) || *s1
== '_')
4923 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4924 sprintf (buf
, "__load_start_%s", clean
);
4925 lang_add_assignment (exp_assop ('=', buf
,
4926 exp_nameop (LOADADDR
, name
)));
4928 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4929 sprintf (buf
, "__load_stop_%s", clean
);
4930 lang_add_assignment (exp_assop ('=', buf
,
4932 exp_nameop (LOADADDR
, name
),
4933 exp_nameop (SIZEOF
, name
))));
4938 /* Finish an overlay. If there are any overlay wide settings, this
4939 looks through all the sections in the overlay and sets them. */
4942 lang_leave_overlay (etree_type
*lma_expr
,
4945 const char *memspec
,
4946 lang_output_section_phdr_list
*phdrs
,
4947 const char *lma_memspec
)
4949 lang_memory_region_type
*region
;
4950 lang_memory_region_type
*lma_region
;
4951 struct overlay_list
*l
;
4952 lang_nocrossref_type
*nocrossref
;
4954 lang_get_regions (®ion
, &lma_region
,
4955 memspec
, lma_memspec
,
4956 lma_expr
!= NULL
, FALSE
);
4960 /* After setting the size of the last section, set '.' to end of the
4962 if (overlay_list
!= NULL
)
4963 overlay_list
->os
->update_dot_tree
4964 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
4969 struct overlay_list
*next
;
4971 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
4974 l
->os
->region
= region
;
4975 l
->os
->lma_region
= lma_region
;
4977 /* The first section has the load address specified in the
4978 OVERLAY statement. The rest are worked out from that.
4979 The base address is not needed (and should be null) if
4980 an LMA region was specified. */
4982 l
->os
->load_base
= lma_expr
;
4983 else if (lma_region
== 0)
4984 l
->os
->load_base
= exp_binop ('+',
4985 exp_nameop (LOADADDR
, l
->next
->os
->name
),
4986 exp_nameop (SIZEOF
, l
->next
->os
->name
));
4988 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4989 l
->os
->phdrs
= phdrs
;
4993 lang_nocrossref_type
*nc
;
4995 nc
= xmalloc (sizeof *nc
);
4996 nc
->name
= l
->os
->name
;
4997 nc
->next
= nocrossref
;
5006 if (nocrossref
!= NULL
)
5007 lang_add_nocrossref (nocrossref
);
5010 overlay_list
= NULL
;
5014 /* Version handling. This is only useful for ELF. */
5016 /* This global variable holds the version tree that we build. */
5018 struct bfd_elf_version_tree
*lang_elf_version_info
;
5020 /* If PREV is NULL, return first version pattern matching particular symbol.
5021 If PREV is non-NULL, return first version pattern matching particular
5022 symbol after PREV (previously returned by lang_vers_match). */
5024 static struct bfd_elf_version_expr
*
5025 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
5026 struct bfd_elf_version_expr
*prev
,
5029 const char *cxx_sym
= sym
;
5030 const char *java_sym
= sym
;
5031 struct bfd_elf_version_expr
*expr
= NULL
;
5033 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5035 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
5039 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5041 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5046 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
5048 struct bfd_elf_version_expr e
;
5050 switch (prev
? prev
->mask
: 0)
5053 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
5056 expr
= htab_find (head
->htab
, &e
);
5057 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
5058 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
5064 case BFD_ELF_VERSION_C_TYPE
:
5065 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5068 expr
= htab_find (head
->htab
, &e
);
5069 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
5070 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5076 case BFD_ELF_VERSION_CXX_TYPE
:
5077 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5079 e
.symbol
= java_sym
;
5080 expr
= htab_find (head
->htab
, &e
);
5081 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
5082 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5093 /* Finally, try the wildcards. */
5094 if (prev
== NULL
|| prev
->symbol
)
5095 expr
= head
->remaining
;
5102 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5105 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5107 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5111 if (fnmatch (expr
->pattern
, s
, 0) == 0)
5118 free ((char *) cxx_sym
);
5119 if (java_sym
!= sym
)
5120 free ((char *) java_sym
);
5124 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5125 return a string pointing to the symbol name. */
5128 realsymbol (const char *pattern
)
5131 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
5132 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
5134 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
5136 /* It is a glob pattern only if there is no preceding
5138 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
5146 /* Remove the preceding backslash. */
5153 backslash
= *p
== '\\';
5168 /* This is called for each variable name or match expression. */
5170 struct bfd_elf_version_expr
*
5171 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
5175 struct bfd_elf_version_expr
*ret
;
5177 ret
= xmalloc (sizeof *ret
);
5182 ret
->symbol
= realsymbol (new);
5184 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5185 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5186 else if (strcasecmp (lang
, "C++") == 0)
5187 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
5188 else if (strcasecmp (lang
, "Java") == 0)
5189 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
5192 einfo (_("%X%P: unknown language `%s' in version information\n"),
5194 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5197 return ldemul_new_vers_pattern (ret
);
5200 /* This is called for each set of variable names and match
5203 struct bfd_elf_version_tree
*
5204 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
5205 struct bfd_elf_version_expr
*locals
)
5207 struct bfd_elf_version_tree
*ret
;
5209 ret
= xcalloc (1, sizeof *ret
);
5210 ret
->globals
.list
= globals
;
5211 ret
->locals
.list
= locals
;
5212 ret
->match
= lang_vers_match
;
5213 ret
->name_indx
= (unsigned int) -1;
5217 /* This static variable keeps track of version indices. */
5219 static int version_index
;
5222 version_expr_head_hash (const void *p
)
5224 const struct bfd_elf_version_expr
*e
= p
;
5226 return htab_hash_string (e
->symbol
);
5230 version_expr_head_eq (const void *p1
, const void *p2
)
5232 const struct bfd_elf_version_expr
*e1
= p1
;
5233 const struct bfd_elf_version_expr
*e2
= p2
;
5235 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
5239 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
5242 struct bfd_elf_version_expr
*e
, *next
;
5243 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
5245 for (e
= head
->list
; e
; e
= e
->next
)
5249 head
->mask
|= e
->mask
;
5254 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
5255 version_expr_head_eq
, NULL
);
5256 list_loc
= &head
->list
;
5257 remaining_loc
= &head
->remaining
;
5258 for (e
= head
->list
; e
; e
= next
)
5264 remaining_loc
= &e
->next
;
5268 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
5272 struct bfd_elf_version_expr
*e1
, *last
;
5278 if (e1
->mask
== e
->mask
)
5286 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
5290 /* This is a duplicate. */
5291 /* FIXME: Memory leak. Sometimes pattern is not
5292 xmalloced alone, but in larger chunk of memory. */
5293 /* free (e->symbol); */
5298 e
->next
= last
->next
;
5306 list_loc
= &e
->next
;
5310 *remaining_loc
= NULL
;
5311 *list_loc
= head
->remaining
;
5314 head
->remaining
= head
->list
;
5317 /* This is called when we know the name and dependencies of the
5321 lang_register_vers_node (const char *name
,
5322 struct bfd_elf_version_tree
*version
,
5323 struct bfd_elf_version_deps
*deps
)
5325 struct bfd_elf_version_tree
*t
, **pp
;
5326 struct bfd_elf_version_expr
*e1
;
5331 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5332 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5334 einfo (_("%X%P: anonymous version tag cannot be combined"
5335 " with other version tags\n"));
5340 /* Make sure this node has a unique name. */
5341 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5342 if (strcmp (t
->name
, name
) == 0)
5343 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5345 lang_finalize_version_expr_head (&version
->globals
);
5346 lang_finalize_version_expr_head (&version
->locals
);
5348 /* Check the global and local match names, and make sure there
5349 aren't any duplicates. */
5351 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
5353 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5355 struct bfd_elf_version_expr
*e2
;
5357 if (t
->locals
.htab
&& e1
->symbol
)
5359 e2
= htab_find (t
->locals
.htab
, e1
);
5360 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5362 if (e1
->mask
== e2
->mask
)
5363 einfo (_("%X%P: duplicate expression `%s'"
5364 " in version information\n"), e1
->symbol
);
5368 else if (!e1
->symbol
)
5369 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5370 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
5371 && e1
->mask
== e2
->mask
)
5372 einfo (_("%X%P: duplicate expression `%s'"
5373 " in version information\n"), e1
->pattern
);
5377 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
5379 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5381 struct bfd_elf_version_expr
*e2
;
5383 if (t
->globals
.htab
&& e1
->symbol
)
5385 e2
= htab_find (t
->globals
.htab
, e1
);
5386 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5388 if (e1
->mask
== e2
->mask
)
5389 einfo (_("%X%P: duplicate expression `%s'"
5390 " in version information\n"),
5395 else if (!e1
->symbol
)
5396 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5397 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
5398 && e1
->mask
== e2
->mask
)
5399 einfo (_("%X%P: duplicate expression `%s'"
5400 " in version information\n"), e1
->pattern
);
5404 version
->deps
= deps
;
5405 version
->name
= name
;
5406 if (name
[0] != '\0')
5409 version
->vernum
= version_index
;
5412 version
->vernum
= 0;
5414 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5419 /* This is called when we see a version dependency. */
5421 struct bfd_elf_version_deps
*
5422 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
5424 struct bfd_elf_version_deps
*ret
;
5425 struct bfd_elf_version_tree
*t
;
5427 ret
= xmalloc (sizeof *ret
);
5430 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5432 if (strcmp (t
->name
, name
) == 0)
5434 ret
->version_needed
= t
;
5439 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5445 lang_do_version_exports_section (void)
5447 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5449 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5451 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5459 contents
= xmalloc (len
);
5460 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5461 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5464 while (p
< contents
+ len
)
5466 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5467 p
= strchr (p
, '\0') + 1;
5470 /* Do not free the contents, as we used them creating the regex. */
5472 /* Do not include this section in the link. */
5473 sec
->flags
|= SEC_EXCLUDE
;
5476 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5477 lang_register_vers_node (command_line
.version_exports_section
,
5478 lang_new_vers_node (greg
, lreg
), NULL
);
5482 lang_add_unique (const char *name
)
5484 struct unique_sections
*ent
;
5486 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5487 if (strcmp (ent
->name
, name
) == 0)
5490 ent
= xmalloc (sizeof *ent
);
5491 ent
->name
= xstrdup (name
);
5492 ent
->next
= unique_section_list
;
5493 unique_section_list
= ent
;