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 bfd_boolean
wildcardp (const char *);
71 static lang_input_statement_type
*lookup_name (const char *);
72 static bfd_boolean
load_symbols (lang_input_statement_type
*,
73 lang_statement_list_type
*);
74 static struct bfd_hash_entry
*lang_definedness_newfunc
75 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
76 static void insert_undefined (const char *);
77 static void print_all_symbols (asection
*);
78 static bfd_boolean
sort_def_symbol (struct bfd_link_hash_entry
*, void *);
79 static void print_statement (lang_statement_union_type
*,
80 lang_output_section_statement_type
*);
81 static void print_statement_list (lang_statement_union_type
*,
82 lang_output_section_statement_type
*);
83 static void print_statements (void);
84 static bfd_boolean
lang_one_common (struct bfd_link_hash_entry
*, void *);
85 static void lang_record_phdrs (void);
86 static void lang_do_version_exports_section (void);
88 typedef void (*callback_t
) (lang_wild_statement_type
*, struct wildcard_list
*,
89 asection
*, lang_input_statement_type
*, void *);
91 /* Exported variables. */
92 lang_output_section_statement_type
*abs_output_section
;
93 lang_statement_list_type lang_output_section_statement
;
94 lang_statement_list_type
*stat_ptr
= &statement_list
;
95 lang_statement_list_type file_chain
= { NULL
, NULL
};
96 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
97 const char *entry_section
= ".text";
98 bfd_boolean entry_from_cmdline
;
99 bfd_boolean lang_has_input_file
= FALSE
;
100 bfd_boolean had_output_filename
= FALSE
;
101 bfd_boolean lang_float_flag
= FALSE
;
102 bfd_boolean delete_output_file_on_failure
= FALSE
;
103 struct lang_nocrossrefs
*nocrossref_list
;
104 struct unique_sections
*unique_section_list
;
105 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
106 int lang_statement_iteration
= 0;
108 etree_type
*base
; /* Relocation base - or null */
110 #define new_stat(x, y) \
111 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
113 #define outside_section_address(q) \
114 ((q)->output_offset + (q)->output_section->vma)
116 #define outside_symbol_address(q) \
117 ((q)->value + outside_section_address (q->section))
119 #define SECTION_NAME_MAP_LENGTH (16)
122 stat_alloc (size_t size
)
124 return obstack_alloc (&stat_obstack
, size
);
128 unique_section_p (const asection
*sec
)
130 struct unique_sections
*unam
;
133 if (link_info
.relocatable
134 && sec
->owner
!= NULL
135 && bfd_is_group_section (sec
->owner
, sec
))
139 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
140 if (wildcardp (unam
->name
)
141 ? fnmatch (unam
->name
, secnam
, 0) == 0
142 : strcmp (unam
->name
, secnam
) == 0)
150 /* Generic traversal routines for finding matching sections. */
153 walk_wild_section (lang_wild_statement_type
*ptr
,
154 lang_input_statement_type
*file
,
160 if (file
->just_syms_flag
)
163 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
165 struct wildcard_list
*sec
;
167 sec
= ptr
->section_list
;
169 (*callback
) (ptr
, sec
, s
, file
, data
);
173 bfd_boolean skip
= FALSE
;
174 struct name_list
*list_tmp
;
176 /* Don't process sections from files which were
178 for (list_tmp
= sec
->spec
.exclude_name_list
;
180 list_tmp
= list_tmp
->next
)
182 if (wildcardp (list_tmp
->name
))
183 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
185 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
187 /* If this file is part of an archive, and the archive is
188 excluded, exclude this file. */
189 if (! skip
&& file
->the_bfd
!= NULL
190 && file
->the_bfd
->my_archive
!= NULL
191 && file
->the_bfd
->my_archive
->filename
!= NULL
)
193 if (wildcardp (list_tmp
->name
))
194 skip
= fnmatch (list_tmp
->name
,
195 file
->the_bfd
->my_archive
->filename
,
198 skip
= strcmp (list_tmp
->name
,
199 file
->the_bfd
->my_archive
->filename
) == 0;
206 if (!skip
&& sec
->spec
.name
!= NULL
)
208 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
210 if (wildcardp (sec
->spec
.name
))
211 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
213 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
217 (*callback
) (ptr
, sec
, s
, file
, data
);
224 /* Handle a wild statement for a single file F. */
227 walk_wild_file (lang_wild_statement_type
*s
,
228 lang_input_statement_type
*f
,
232 if (f
->the_bfd
== NULL
233 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
234 walk_wild_section (s
, f
, callback
, data
);
239 /* This is an archive file. We must map each member of the
240 archive separately. */
241 member
= bfd_openr_next_archived_file (f
->the_bfd
, NULL
);
242 while (member
!= NULL
)
244 /* When lookup_name is called, it will call the add_symbols
245 entry point for the archive. For each element of the
246 archive which is included, BFD will call ldlang_add_file,
247 which will set the usrdata field of the member to the
248 lang_input_statement. */
249 if (member
->usrdata
!= NULL
)
251 walk_wild_section (s
, member
->usrdata
, callback
, data
);
254 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
260 walk_wild (lang_wild_statement_type
*s
, callback_t callback
, void *data
)
262 const char *file_spec
= s
->filename
;
264 if (file_spec
== NULL
)
266 /* Perform the iteration over all files in the list. */
267 LANG_FOR_EACH_INPUT_STATEMENT (f
)
269 walk_wild_file (s
, f
, callback
, data
);
272 else if (wildcardp (file_spec
))
274 LANG_FOR_EACH_INPUT_STATEMENT (f
)
276 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
277 walk_wild_file (s
, f
, callback
, data
);
282 lang_input_statement_type
*f
;
284 /* Perform the iteration over a single file. */
285 f
= lookup_name (file_spec
);
287 walk_wild_file (s
, f
, callback
, data
);
291 /* lang_for_each_statement walks the parse tree and calls the provided
292 function for each node. */
295 lang_for_each_statement_worker (void (*func
) (lang_statement_union_type
*),
296 lang_statement_union_type
*s
)
298 for (; s
!= NULL
; s
= s
->header
.next
)
302 switch (s
->header
.type
)
304 case lang_constructors_statement_enum
:
305 lang_for_each_statement_worker (func
, constructor_list
.head
);
307 case lang_output_section_statement_enum
:
308 lang_for_each_statement_worker
309 (func
, s
->output_section_statement
.children
.head
);
311 case lang_wild_statement_enum
:
312 lang_for_each_statement_worker (func
,
313 s
->wild_statement
.children
.head
);
315 case lang_group_statement_enum
:
316 lang_for_each_statement_worker (func
,
317 s
->group_statement
.children
.head
);
319 case lang_data_statement_enum
:
320 case lang_reloc_statement_enum
:
321 case lang_object_symbols_statement_enum
:
322 case lang_output_statement_enum
:
323 case lang_target_statement_enum
:
324 case lang_input_section_enum
:
325 case lang_input_statement_enum
:
326 case lang_assignment_statement_enum
:
327 case lang_padding_statement_enum
:
328 case lang_address_statement_enum
:
329 case lang_fill_statement_enum
:
339 lang_for_each_statement (void (*func
) (lang_statement_union_type
*))
341 lang_for_each_statement_worker (func
, statement_list
.head
);
344 /*----------------------------------------------------------------------*/
347 lang_list_init (lang_statement_list_type
*list
)
350 list
->tail
= &list
->head
;
353 /* Build a new statement node for the parse tree. */
355 static lang_statement_union_type
*
356 new_statement (enum statement_enum type
,
358 lang_statement_list_type
*list
)
360 lang_statement_union_type
*new;
362 new = stat_alloc (size
);
363 new->header
.type
= type
;
364 new->header
.next
= NULL
;
365 lang_statement_append (list
, new, &new->header
.next
);
369 /* Build a new input file node for the language. There are several
370 ways in which we treat an input file, eg, we only look at symbols,
371 or prefix it with a -l etc.
373 We can be supplied with requests for input files more than once;
374 they may, for example be split over several lines like foo.o(.text)
375 foo.o(.data) etc, so when asked for a file we check that we haven't
376 got it already so we don't duplicate the bfd. */
378 static lang_input_statement_type
*
379 new_afile (const char *name
,
380 lang_input_file_enum_type file_type
,
382 bfd_boolean add_to_list
)
384 lang_input_statement_type
*p
;
387 p
= new_stat (lang_input_statement
, stat_ptr
);
390 p
= stat_alloc (sizeof (lang_input_statement_type
));
391 p
->header
.next
= NULL
;
394 lang_has_input_file
= TRUE
;
396 p
->sysrooted
= FALSE
;
399 case lang_input_file_is_symbols_only_enum
:
401 p
->is_archive
= FALSE
;
403 p
->local_sym_name
= name
;
404 p
->just_syms_flag
= TRUE
;
405 p
->search_dirs_flag
= FALSE
;
407 case lang_input_file_is_fake_enum
:
409 p
->is_archive
= FALSE
;
411 p
->local_sym_name
= name
;
412 p
->just_syms_flag
= FALSE
;
413 p
->search_dirs_flag
= FALSE
;
415 case lang_input_file_is_l_enum
:
416 p
->is_archive
= TRUE
;
419 p
->local_sym_name
= concat ("-l", name
, NULL
);
420 p
->just_syms_flag
= FALSE
;
421 p
->search_dirs_flag
= TRUE
;
423 case lang_input_file_is_marker_enum
:
425 p
->is_archive
= FALSE
;
427 p
->local_sym_name
= name
;
428 p
->just_syms_flag
= FALSE
;
429 p
->search_dirs_flag
= TRUE
;
431 case lang_input_file_is_search_file_enum
:
432 p
->sysrooted
= ldlang_sysrooted_script
;
434 p
->is_archive
= FALSE
;
436 p
->local_sym_name
= name
;
437 p
->just_syms_flag
= FALSE
;
438 p
->search_dirs_flag
= TRUE
;
440 case lang_input_file_is_file_enum
:
442 p
->is_archive
= FALSE
;
444 p
->local_sym_name
= name
;
445 p
->just_syms_flag
= FALSE
;
446 p
->search_dirs_flag
= FALSE
;
453 p
->next_real_file
= NULL
;
456 p
->dynamic
= config
.dynamic_link
;
457 p
->add_needed
= add_needed
;
458 p
->as_needed
= as_needed
;
459 p
->whole_archive
= whole_archive
;
461 lang_statement_append (&input_file_chain
,
462 (lang_statement_union_type
*) p
,
467 lang_input_statement_type
*
468 lang_add_input_file (const char *name
,
469 lang_input_file_enum_type file_type
,
472 lang_has_input_file
= TRUE
;
473 return new_afile (name
, file_type
, target
, TRUE
);
476 /* Build enough state so that the parser can build its tree. */
481 obstack_begin (&stat_obstack
, 1000);
483 stat_ptr
= &statement_list
;
485 lang_list_init (stat_ptr
);
487 lang_list_init (&input_file_chain
);
488 lang_list_init (&lang_output_section_statement
);
489 lang_list_init (&file_chain
);
490 first_file
= lang_add_input_file (NULL
, lang_input_file_is_marker_enum
,
493 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
495 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
497 /* The value "3" is ad-hoc, somewhat related to the expected number of
498 DEFINED expressions in a linker script. For most default linker
499 scripts, there are none. Why a hash table then? Well, it's somewhat
500 simpler to re-use working machinery than using a linked list in terms
501 of code-complexity here in ld, besides the initialization which just
502 looks like other code here. */
503 if (bfd_hash_table_init_n (&lang_definedness_table
,
504 lang_definedness_newfunc
, 3) != TRUE
)
505 einfo (_("%P%F: out of memory during initialization"));
507 /* Callers of exp_fold_tree need to increment this. */
508 lang_statement_iteration
= 0;
511 /*----------------------------------------------------------------------
512 A region is an area of memory declared with the
513 MEMORY { name:org=exp, len=exp ... }
516 We maintain a list of all the regions here.
518 If no regions are specified in the script, then the default is used
519 which is created when looked up to be the entire data space.
521 If create is true we are creating a region inside a MEMORY block.
522 In this case it is probably an error to create a region that has
523 already been created. If we are not inside a MEMORY block it is
524 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
525 and so we issue a warning. */
527 static lang_memory_region_type
*lang_memory_region_list
;
528 static lang_memory_region_type
**lang_memory_region_list_tail
529 = &lang_memory_region_list
;
531 lang_memory_region_type
*
532 lang_memory_region_lookup (const char *const name
, bfd_boolean create
)
534 lang_memory_region_type
*p
;
535 lang_memory_region_type
*new;
537 /* NAME is NULL for LMA memspecs if no region was specified. */
541 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
542 if (strcmp (p
->name
, name
) == 0)
545 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
551 /* This code used to always use the first region in the list as the
552 default region. I changed it to instead use a region
553 encompassing all of memory as the default region. This permits
554 NOLOAD sections to work reasonably without requiring a region.
555 People should specify what region they mean, if they really want
557 if (strcmp (name
, DEFAULT_MEMORY_REGION
) == 0)
559 if (lang_memory_region_list
!= NULL
)
560 return lang_memory_region_list
;
564 if (!create
&& strcmp (name
, DEFAULT_MEMORY_REGION
))
565 einfo (_("%P:%S: warning: memory region %s not declared\n"), name
);
567 new = stat_alloc (sizeof (lang_memory_region_type
));
569 new->name
= xstrdup (name
);
572 *lang_memory_region_list_tail
= new;
573 lang_memory_region_list_tail
= &new->next
;
577 new->length
= ~(bfd_size_type
) 0;
579 new->had_full_message
= FALSE
;
584 static lang_memory_region_type
*
585 lang_memory_default (asection
*section
)
587 lang_memory_region_type
*p
;
589 flagword sec_flags
= section
->flags
;
591 /* Override SEC_DATA to mean a writable section. */
592 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
593 sec_flags
|= SEC_DATA
;
595 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
597 if ((p
->flags
& sec_flags
) != 0
598 && (p
->not_flags
& sec_flags
) == 0)
603 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION
, FALSE
);
606 static lang_output_section_statement_type
*
607 lang_output_section_find_1 (const char *const name
, int constraint
)
609 lang_statement_union_type
*u
;
610 lang_output_section_statement_type
*lookup
;
612 for (u
= lang_output_section_statement
.head
; u
!= NULL
; u
= lookup
->next
)
614 lookup
= &u
->output_section_statement
;
616 if (strcmp (name
, lookup
->name
) == 0
617 && lookup
->constraint
!= -1
618 && (constraint
== 0 || constraint
== lookup
->constraint
))
624 lang_output_section_statement_type
*
625 lang_output_section_find (const char *const name
)
627 return lang_output_section_find_1 (name
, 0);
630 static lang_output_section_statement_type
*
631 lang_output_section_statement_lookup_1 (const char *const name
, int constraint
)
633 lang_output_section_statement_type
*lookup
;
635 lookup
= lang_output_section_find_1 (name
, constraint
);
638 lookup
= new_stat (lang_output_section_statement
, stat_ptr
);
639 lookup
->region
= NULL
;
640 lookup
->lma_region
= NULL
;
642 lookup
->block_value
= 1;
646 lookup
->bfd_section
= NULL
;
647 lookup
->processed
= 0;
648 lookup
->constraint
= constraint
;
649 lookup
->sectype
= normal_section
;
650 lookup
->addr_tree
= NULL
;
651 lang_list_init (&lookup
->children
);
653 lookup
->memspec
= NULL
;
655 lookup
->subsection_alignment
= -1;
656 lookup
->section_alignment
= -1;
657 lookup
->load_base
= NULL
;
658 lookup
->update_dot_tree
= NULL
;
659 lookup
->phdrs
= NULL
;
661 lang_statement_append (&lang_output_section_statement
,
662 (lang_statement_union_type
*) lookup
,
668 lang_output_section_statement_type
*
669 lang_output_section_statement_lookup (const char *const name
)
671 return lang_output_section_statement_lookup_1 (name
, 0);
675 lang_map_flags (flagword flag
)
677 if (flag
& SEC_ALLOC
)
683 if (flag
& SEC_READONLY
)
696 lang_memory_region_type
*m
;
699 minfo (_("\nMemory Configuration\n\n"));
700 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
701 _("Name"), _("Origin"), _("Length"), _("Attributes"));
703 for (m
= lang_memory_region_list
; m
!= NULL
; m
= m
->next
)
708 fprintf (config
.map_file
, "%-16s ", m
->name
);
710 sprintf_vma (buf
, m
->origin
);
711 minfo ("0x%s ", buf
);
719 minfo ("0x%V", m
->length
);
720 if (m
->flags
|| m
->not_flags
)
728 lang_map_flags (m
->flags
);
734 lang_map_flags (m
->not_flags
);
741 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
743 if (! command_line
.reduce_memory_overheads
)
745 obstack_begin (&map_obstack
, 1000);
746 for (p
= link_info
.input_bfds
; p
!= (bfd
*) NULL
; p
= p
->link_next
)
747 bfd_map_over_sections (p
, init_map_userdata
, 0);
748 bfd_link_hash_traverse (link_info
.hash
, sort_def_symbol
, 0);
754 init_map_userdata (abfd
, sec
, data
)
755 bfd
*abfd ATTRIBUTE_UNUSED
;
757 void *data ATTRIBUTE_UNUSED
;
759 fat_section_userdata_type
*new_data
760 = ((fat_section_userdata_type
*) (stat_alloc
761 (sizeof (fat_section_userdata_type
))));
763 ASSERT (get_userdata (sec
) == NULL
);
764 get_userdata (sec
) = new_data
;
765 new_data
->map_symbol_def_tail
= &new_data
->map_symbol_def_head
;
769 sort_def_symbol (hash_entry
, info
)
770 struct bfd_link_hash_entry
*hash_entry
;
771 void *info ATTRIBUTE_UNUSED
;
773 if (hash_entry
->type
== bfd_link_hash_defined
774 || hash_entry
->type
== bfd_link_hash_defweak
)
776 struct fat_user_section_struct
*ud
;
777 struct map_symbol_def
*def
;
779 ud
= get_userdata (hash_entry
->u
.def
.section
);
782 /* ??? What do we have to do to initialize this beforehand? */
783 /* The first time we get here is bfd_abs_section... */
784 init_map_userdata (0, hash_entry
->u
.def
.section
, 0);
785 ud
= get_userdata (hash_entry
->u
.def
.section
);
787 else if (!ud
->map_symbol_def_tail
)
788 ud
->map_symbol_def_tail
= &ud
->map_symbol_def_head
;
790 def
= obstack_alloc (&map_obstack
, sizeof *def
);
791 def
->entry
= hash_entry
;
792 *(ud
->map_symbol_def_tail
) = def
;
793 ud
->map_symbol_def_tail
= &def
->next
;
798 /* Initialize an output section. */
801 init_os (lang_output_section_statement_type
*s
)
803 lean_section_userdata_type
*new;
805 if (s
->bfd_section
!= NULL
)
808 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
809 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
811 new = stat_alloc (SECTION_USERDATA_SIZE
);
812 memset (new, 0, SECTION_USERDATA_SIZE
);
814 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
815 if (s
->bfd_section
== NULL
)
816 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
817 if (s
->bfd_section
== NULL
)
819 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
820 output_bfd
->xvec
->name
, s
->name
);
822 s
->bfd_section
->output_section
= s
->bfd_section
;
824 /* We initialize an output sections output offset to minus its own
825 vma to allow us to output a section through itself. */
826 s
->bfd_section
->output_offset
= 0;
827 get_userdata (s
->bfd_section
) = new;
829 /* If there is a base address, make sure that any sections it might
830 mention are initialized. */
831 if (s
->addr_tree
!= NULL
)
832 exp_init_os (s
->addr_tree
);
834 if (s
->load_base
!= NULL
)
835 exp_init_os (s
->load_base
);
838 /* Make sure that all output sections mentioned in an expression are
842 exp_init_os (etree_type
*exp
)
844 switch (exp
->type
.node_class
)
847 exp_init_os (exp
->assign
.src
);
851 exp_init_os (exp
->binary
.lhs
);
852 exp_init_os (exp
->binary
.rhs
);
856 exp_init_os (exp
->trinary
.cond
);
857 exp_init_os (exp
->trinary
.lhs
);
858 exp_init_os (exp
->trinary
.rhs
);
862 exp_init_os (exp
->assert_s
.child
);
866 exp_init_os (exp
->unary
.child
);
870 switch (exp
->type
.node_code
)
876 lang_output_section_statement_type
*os
;
878 os
= lang_output_section_find (exp
->name
.name
);
879 if (os
!= NULL
&& os
->bfd_section
== NULL
)
891 section_already_linked (bfd
*abfd
, asection
*sec
, void *data
)
893 lang_input_statement_type
*entry
= data
;
895 /* If we are only reading symbols from this object, then we want to
896 discard all sections. */
897 if (entry
->just_syms_flag
)
899 bfd_link_just_syms (sec
, &link_info
);
903 bfd_section_already_linked (abfd
, sec
);
906 /* The wild routines.
908 These expand statements like *(.text) and foo.o to a list of
909 explicit actions, like foo.o(.text), bar.o(.text) and
910 foo.o(.text, .data). */
912 /* Return TRUE if the PATTERN argument is a wildcard pattern.
913 Although backslashes are treated specially if a pattern contains
914 wildcards, we do not consider the mere presence of a backslash to
915 be enough to cause the pattern to be treated as a wildcard.
916 That lets us handle DOS filenames more naturally. */
919 wildcardp (const char *pattern
)
923 for (s
= pattern
; *s
!= '\0'; ++s
)
931 /* Add SECTION to the output section OUTPUT. Do this by creating a
932 lang_input_section statement which is placed at PTR. FILE is the
933 input file which holds SECTION. */
936 lang_add_section (lang_statement_list_type
*ptr
,
938 lang_output_section_statement_type
*output
,
939 lang_input_statement_type
*file
)
941 flagword flags
= section
->flags
;
944 /* Discard sections marked with SEC_EXCLUDE. */
945 discard
= (flags
& SEC_EXCLUDE
) != 0;
947 /* Discard input sections which are assigned to a section named
948 DISCARD_SECTION_NAME. */
949 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
952 /* Discard debugging sections if we are stripping debugging
954 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
955 && (flags
& SEC_DEBUGGING
) != 0)
960 if (section
->output_section
== NULL
)
962 /* This prevents future calls from assigning this section. */
963 section
->output_section
= bfd_abs_section_ptr
;
968 if (section
->output_section
== NULL
)
971 lang_input_section_type
*new;
974 if (output
->bfd_section
== NULL
)
977 first
= ! output
->bfd_section
->linker_has_input
;
978 output
->bfd_section
->linker_has_input
= 1;
980 /* Add a section reference to the list. */
981 new = new_stat (lang_input_section
, ptr
);
983 new->section
= section
;
985 section
->output_section
= output
->bfd_section
;
987 flags
= section
->flags
;
989 /* We don't copy the SEC_NEVER_LOAD flag from an input section
990 to an output section, because we want to be able to include a
991 SEC_NEVER_LOAD section in the middle of an otherwise loaded
992 section (I don't know why we want to do this, but we do).
993 build_link_order in ldwrite.c handles this case by turning
994 the embedded SEC_NEVER_LOAD section into a fill. */
996 flags
&= ~ SEC_NEVER_LOAD
;
998 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
999 already been processed. One reason to do this is that on pe
1000 format targets, .text$foo sections go into .text and it's odd
1001 to see .text with SEC_LINK_ONCE set. */
1003 if (! link_info
.relocatable
)
1004 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1006 /* If this is not the first input section, and the SEC_READONLY
1007 flag is not currently set, then don't set it just because the
1008 input section has it set. */
1010 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1011 flags
&= ~ SEC_READONLY
;
1013 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1015 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1016 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1017 || ((flags
& SEC_MERGE
)
1018 && section
->output_section
->entsize
!= section
->entsize
)))
1020 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1021 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1024 section
->output_section
->flags
|= flags
;
1026 if (flags
& SEC_MERGE
)
1027 section
->output_section
->entsize
= section
->entsize
;
1029 /* If SEC_READONLY is not set in the input section, then clear
1030 it from the output section. */
1031 if ((section
->flags
& SEC_READONLY
) == 0)
1032 section
->output_section
->flags
&= ~SEC_READONLY
;
1034 switch (output
->sectype
)
1036 case normal_section
:
1041 case overlay_section
:
1042 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1044 case noload_section
:
1045 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1046 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1050 /* Copy over SEC_SMALL_DATA. */
1051 if (section
->flags
& SEC_SMALL_DATA
)
1052 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1054 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1055 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1057 /* If supplied an alignment, then force it. */
1058 if (output
->section_alignment
!= -1)
1059 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1061 if (section
->flags
& SEC_BLOCK
)
1063 section
->output_section
->flags
|= SEC_BLOCK
;
1064 /* FIXME: This value should really be obtained from the bfd... */
1065 output
->block_value
= 128;
1070 /* Handle wildcard sorting. This returns the lang_input_section which
1071 should follow the one we are going to create for SECTION and FILE,
1072 based on the sorting requirements of WILD. It returns NULL if the
1073 new section should just go at the end of the current list. */
1075 static lang_statement_union_type
*
1076 wild_sort (lang_wild_statement_type
*wild
,
1077 struct wildcard_list
*sec
,
1078 lang_input_statement_type
*file
,
1081 const char *section_name
;
1082 lang_statement_union_type
*l
;
1084 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1087 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1088 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1090 lang_input_section_type
*ls
;
1092 if (l
->header
.type
!= lang_input_section_enum
)
1094 ls
= &l
->input_section
;
1096 /* Sorting by filename takes precedence over sorting by section
1099 if (wild
->filenames_sorted
)
1101 const char *fn
, *ln
;
1105 /* The PE support for the .idata section as generated by
1106 dlltool assumes that files will be sorted by the name of
1107 the archive and then the name of the file within the
1110 if (file
->the_bfd
!= NULL
1111 && bfd_my_archive (file
->the_bfd
) != NULL
)
1113 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1118 fn
= file
->filename
;
1122 if (ls
->ifile
->the_bfd
!= NULL
1123 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1125 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1130 ln
= ls
->ifile
->filename
;
1134 i
= strcmp (fn
, ln
);
1143 fn
= file
->filename
;
1145 ln
= ls
->ifile
->filename
;
1147 i
= strcmp (fn
, ln
);
1155 /* Here either the files are not sorted by name, or we are
1156 looking at the sections for this file. */
1158 if (sec
!= NULL
&& sec
->spec
.sorted
)
1160 if (strcmp (section_name
,
1161 bfd_get_section_name (ls
->ifile
->the_bfd
,
1171 /* Expand a wild statement for a particular FILE. SECTION may be
1172 NULL, in which case it is a wild card. */
1175 output_section_callback (lang_wild_statement_type
*ptr
,
1176 struct wildcard_list
*sec
,
1178 lang_input_statement_type
*file
,
1181 lang_statement_union_type
*before
;
1183 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1184 if (unique_section_p (section
))
1187 before
= wild_sort (ptr
, sec
, file
, section
);
1189 /* Here BEFORE points to the lang_input_section which
1190 should follow the one we are about to add. If BEFORE
1191 is NULL, then the section should just go at the end
1192 of the current list. */
1195 lang_add_section (&ptr
->children
, section
,
1196 (lang_output_section_statement_type
*) output
,
1200 lang_statement_list_type list
;
1201 lang_statement_union_type
**pp
;
1203 lang_list_init (&list
);
1204 lang_add_section (&list
, section
,
1205 (lang_output_section_statement_type
*) output
,
1208 /* If we are discarding the section, LIST.HEAD will
1210 if (list
.head
!= NULL
)
1212 ASSERT (list
.head
->header
.next
== NULL
);
1214 for (pp
= &ptr
->children
.head
;
1216 pp
= &(*pp
)->header
.next
)
1217 ASSERT (*pp
!= NULL
);
1219 list
.head
->header
.next
= *pp
;
1225 /* Check if all sections in a wild statement for a particular FILE
1229 check_section_callback (lang_wild_statement_type
*ptr ATTRIBUTE_UNUSED
,
1230 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
1232 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
1235 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1236 if (unique_section_p (section
))
1239 if (section
->output_section
== NULL
&& (section
->flags
& SEC_READONLY
) == 0)
1240 ((lang_output_section_statement_type
*) data
)->all_input_readonly
= FALSE
;
1243 /* This is passed a file name which must have been seen already and
1244 added to the statement tree. We will see if it has been opened
1245 already and had its symbols read. If not then we'll read it. */
1247 static lang_input_statement_type
*
1248 lookup_name (const char *name
)
1250 lang_input_statement_type
*search
;
1252 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1254 search
= (lang_input_statement_type
*) search
->next_real_file
)
1256 /* Use the local_sym_name as the name of the file that has
1257 already been loaded as filename might have been transformed
1258 via the search directory lookup mechanism. */
1259 const char * filename
= search
->local_sym_name
;
1261 if (filename
== NULL
&& name
== NULL
)
1263 if (filename
!= NULL
1265 && strcmp (filename
, name
) == 0)
1270 search
= new_afile (name
, lang_input_file_is_search_file_enum
,
1271 default_target
, FALSE
);
1273 /* If we have already added this file, or this file is not real
1274 (FIXME: can that ever actually happen?) or the name is NULL
1275 (FIXME: can that ever actually happen?) don't add this file. */
1278 || search
->filename
== NULL
)
1281 if (! load_symbols (search
, NULL
))
1287 /* Get the symbols for an input file. */
1290 load_symbols (lang_input_statement_type
*entry
,
1291 lang_statement_list_type
*place
)
1298 ldfile_open_file (entry
);
1300 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1301 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1304 lang_statement_list_type
*hold
;
1305 bfd_boolean bad_load
= TRUE
;
1306 bfd_boolean save_ldlang_sysrooted_script
;
1308 err
= bfd_get_error ();
1310 /* See if the emulation has some special knowledge. */
1311 if (ldemul_unrecognized_file (entry
))
1314 if (err
== bfd_error_file_ambiguously_recognized
)
1318 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1319 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1320 for (p
= matching
; *p
!= NULL
; p
++)
1324 else if (err
!= bfd_error_file_not_recognized
1326 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1330 bfd_close (entry
->the_bfd
);
1331 entry
->the_bfd
= NULL
;
1333 /* Try to interpret the file as a linker script. */
1334 ldfile_open_command_file (entry
->filename
);
1338 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1339 ldlang_sysrooted_script
= entry
->sysrooted
;
1341 ldfile_assumed_script
= TRUE
;
1342 parser_input
= input_script
;
1344 ldfile_assumed_script
= FALSE
;
1346 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1352 if (ldemul_recognized_file (entry
))
1355 /* We don't call ldlang_add_file for an archive. Instead, the
1356 add_symbols entry point will call ldlang_add_file, via the
1357 add_archive_element callback, for each element of the archive
1359 switch (bfd_get_format (entry
->the_bfd
))
1365 ldlang_add_file (entry
);
1366 if (trace_files
|| trace_file_tries
)
1367 info_msg ("%I\n", entry
);
1371 if (entry
->whole_archive
)
1374 bfd_boolean loaded
= TRUE
;
1378 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1383 if (! bfd_check_format (member
, bfd_object
))
1385 einfo (_("%F%B: member %B in archive is not an object\n"),
1386 entry
->the_bfd
, member
);
1390 if (! ((*link_info
.callbacks
->add_archive_element
)
1391 (&link_info
, member
, "--whole-archive")))
1394 if (! bfd_link_add_symbols (member
, &link_info
))
1396 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1401 entry
->loaded
= loaded
;
1407 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1408 entry
->loaded
= TRUE
;
1410 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1412 return entry
->loaded
;
1415 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1416 may be NULL, indicating that it is a wildcard. Separate
1417 lang_input_section statements are created for each part of the
1418 expansion; they are added after the wild statement S. OUTPUT is
1419 the output section. */
1422 wild (lang_wild_statement_type
*s
,
1423 const char *target ATTRIBUTE_UNUSED
,
1424 lang_output_section_statement_type
*output
)
1426 struct wildcard_list
*sec
;
1428 walk_wild (s
, output_section_callback
, output
);
1430 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1432 if (default_common_section
!= NULL
)
1434 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1436 /* Remember the section that common is going to in case we
1437 later get something which doesn't know where to put it. */
1438 default_common_section
= output
;
1443 /* Return TRUE iff target is the sought target. */
1446 get_target (const bfd_target
*target
, void *data
)
1448 const char *sought
= data
;
1450 return strcmp (target
->name
, sought
) == 0;
1453 /* Like strcpy() but convert to lower case as well. */
1456 stricpy (char *dest
, char *src
)
1460 while ((c
= *src
++) != 0)
1461 *dest
++ = TOLOWER (c
);
1466 /* Remove the first occurrence of needle (if any) in haystack
1470 strcut (char *haystack
, char *needle
)
1472 haystack
= strstr (haystack
, needle
);
1478 for (src
= haystack
+ strlen (needle
); *src
;)
1479 *haystack
++ = *src
++;
1485 /* Compare two target format name strings.
1486 Return a value indicating how "similar" they are. */
1489 name_compare (char *first
, char *second
)
1495 copy1
= xmalloc (strlen (first
) + 1);
1496 copy2
= xmalloc (strlen (second
) + 1);
1498 /* Convert the names to lower case. */
1499 stricpy (copy1
, first
);
1500 stricpy (copy2
, second
);
1502 /* Remove size and endian strings from the name. */
1503 strcut (copy1
, "big");
1504 strcut (copy1
, "little");
1505 strcut (copy2
, "big");
1506 strcut (copy2
, "little");
1508 /* Return a value based on how many characters match,
1509 starting from the beginning. If both strings are
1510 the same then return 10 * their length. */
1511 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1512 if (copy1
[result
] == 0)
1524 /* Set by closest_target_match() below. */
1525 static const bfd_target
*winner
;
1527 /* Scan all the valid bfd targets looking for one that has the endianness
1528 requirement that was specified on the command line, and is the nearest
1529 match to the original output target. */
1532 closest_target_match (const bfd_target
*target
, void *data
)
1534 const bfd_target
*original
= data
;
1536 if (command_line
.endian
== ENDIAN_BIG
1537 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1540 if (command_line
.endian
== ENDIAN_LITTLE
1541 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1544 /* Must be the same flavour. */
1545 if (target
->flavour
!= original
->flavour
)
1548 /* If we have not found a potential winner yet, then record this one. */
1555 /* Oh dear, we now have two potential candidates for a successful match.
1556 Compare their names and choose the better one. */
1557 if (name_compare (target
->name
, original
->name
)
1558 > name_compare (winner
->name
, original
->name
))
1561 /* Keep on searching until wqe have checked them all. */
1565 /* Return the BFD target format of the first input file. */
1568 get_first_input_target (void)
1570 char *target
= NULL
;
1572 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1574 if (s
->header
.type
== lang_input_statement_enum
1577 ldfile_open_file (s
);
1579 if (s
->the_bfd
!= NULL
1580 && bfd_check_format (s
->the_bfd
, bfd_object
))
1582 target
= bfd_get_target (s
->the_bfd
);
1594 lang_get_output_target (void)
1598 /* Has the user told us which output format to use? */
1599 if (output_target
!= NULL
)
1600 return output_target
;
1602 /* No - has the current target been set to something other than
1604 if (current_target
!= default_target
)
1605 return current_target
;
1607 /* No - can we determine the format of the first input file? */
1608 target
= get_first_input_target ();
1612 /* Failed - use the default output target. */
1613 return default_target
;
1616 /* Open the output file. */
1619 open_output (const char *name
)
1623 output_target
= lang_get_output_target ();
1625 /* Has the user requested a particular endianness on the command
1627 if (command_line
.endian
!= ENDIAN_UNSET
)
1629 const bfd_target
*target
;
1630 enum bfd_endian desired_endian
;
1632 /* Get the chosen target. */
1633 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1635 /* If the target is not supported, we cannot do anything. */
1638 if (command_line
.endian
== ENDIAN_BIG
)
1639 desired_endian
= BFD_ENDIAN_BIG
;
1641 desired_endian
= BFD_ENDIAN_LITTLE
;
1643 /* See if the target has the wrong endianness. This should
1644 not happen if the linker script has provided big and
1645 little endian alternatives, but some scrips don't do
1647 if (target
->byteorder
!= desired_endian
)
1649 /* If it does, then see if the target provides
1650 an alternative with the correct endianness. */
1651 if (target
->alternative_target
!= NULL
1652 && (target
->alternative_target
->byteorder
== desired_endian
))
1653 output_target
= target
->alternative_target
->name
;
1656 /* Try to find a target as similar as possible to
1657 the default target, but which has the desired
1658 endian characteristic. */
1659 bfd_search_for_target (closest_target_match
,
1662 /* Oh dear - we could not find any targets that
1663 satisfy our requirements. */
1665 einfo (_("%P: warning: could not find any targets"
1666 " that match endianness requirement\n"));
1668 output_target
= winner
->name
;
1674 output
= bfd_openw (name
, output_target
);
1678 if (bfd_get_error () == bfd_error_invalid_target
)
1679 einfo (_("%P%F: target %s not found\n"), output_target
);
1681 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1684 delete_output_file_on_failure
= TRUE
;
1687 output
->flags
|= D_PAGED
;
1690 if (! bfd_set_format (output
, bfd_object
))
1691 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1692 if (! bfd_set_arch_mach (output
,
1693 ldfile_output_architecture
,
1694 ldfile_output_machine
))
1695 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1697 link_info
.hash
= bfd_link_hash_table_create (output
);
1698 if (link_info
.hash
== NULL
)
1699 einfo (_("%P%F: can not create link hash table: %E\n"));
1701 bfd_set_gp_size (output
, g_switch_value
);
1706 ldlang_open_output (lang_statement_union_type
*statement
)
1708 switch (statement
->header
.type
)
1710 case lang_output_statement_enum
:
1711 ASSERT (output_bfd
== NULL
);
1712 output_bfd
= open_output (statement
->output_statement
.name
);
1713 ldemul_set_output_arch ();
1714 if (config
.magic_demand_paged
&& !link_info
.relocatable
)
1715 output_bfd
->flags
|= D_PAGED
;
1717 output_bfd
->flags
&= ~D_PAGED
;
1718 if (config
.text_read_only
)
1719 output_bfd
->flags
|= WP_TEXT
;
1721 output_bfd
->flags
&= ~WP_TEXT
;
1722 if (link_info
.traditional_format
)
1723 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1725 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1728 case lang_target_statement_enum
:
1729 current_target
= statement
->target_statement
.target
;
1736 /* Convert between addresses in bytes and sizes in octets.
1737 For currently supported targets, octets_per_byte is always a power
1738 of two, so we can use shifts. */
1739 #define TO_ADDR(X) ((X) >> opb_shift)
1740 #define TO_SIZE(X) ((X) << opb_shift)
1742 /* Support the above. */
1743 static unsigned int opb_shift
= 0;
1748 unsigned x
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
1749 ldfile_output_machine
);
1752 while ((x
& 1) == 0)
1760 /* Open all the input files. */
1763 open_input_bfds (lang_statement_union_type
*s
, bfd_boolean force
)
1765 for (; s
!= NULL
; s
= s
->header
.next
)
1767 switch (s
->header
.type
)
1769 case lang_constructors_statement_enum
:
1770 open_input_bfds (constructor_list
.head
, force
);
1772 case lang_output_section_statement_enum
:
1773 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1775 case lang_wild_statement_enum
:
1776 /* Maybe we should load the file's symbols. */
1777 if (s
->wild_statement
.filename
1778 && ! wildcardp (s
->wild_statement
.filename
))
1779 lookup_name (s
->wild_statement
.filename
);
1780 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1782 case lang_group_statement_enum
:
1784 struct bfd_link_hash_entry
*undefs
;
1786 /* We must continually search the entries in the group
1787 until no new symbols are added to the list of undefined
1792 undefs
= link_info
.hash
->undefs_tail
;
1793 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
1795 while (undefs
!= link_info
.hash
->undefs_tail
);
1798 case lang_target_statement_enum
:
1799 current_target
= s
->target_statement
.target
;
1801 case lang_input_statement_enum
:
1802 if (s
->input_statement
.real
)
1804 lang_statement_list_type add
;
1806 s
->input_statement
.target
= current_target
;
1808 /* If we are being called from within a group, and this
1809 is an archive which has already been searched, then
1810 force it to be researched unless the whole archive
1811 has been loaded already. */
1813 && !s
->input_statement
.whole_archive
1814 && s
->input_statement
.loaded
1815 && bfd_check_format (s
->input_statement
.the_bfd
,
1817 s
->input_statement
.loaded
= FALSE
;
1819 lang_list_init (&add
);
1821 if (! load_symbols (&s
->input_statement
, &add
))
1822 config
.make_executable
= FALSE
;
1824 if (add
.head
!= NULL
)
1826 *add
.tail
= s
->header
.next
;
1827 s
->header
.next
= add
.head
;
1837 /* If there are [COMMONS] statements, put a wild one into the bss
1841 lang_reasonable_defaults (void)
1844 lang_output_section_statement_lookup (".text");
1845 lang_output_section_statement_lookup (".data");
1847 default_common_section
= lang_output_section_statement_lookup (".bss");
1849 if (!placed_commons
)
1851 lang_wild_statement_type
*new =
1852 new_stat (lang_wild_statement
,
1853 &default_common_section
->children
);
1855 new->section_name
= "COMMON";
1856 new->filename
= NULL
;
1857 lang_list_init (&new->children
);
1862 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1865 lang_track_definedness (const char *name
)
1867 if (bfd_hash_lookup (&lang_definedness_table
, name
, TRUE
, FALSE
) == NULL
)
1868 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
1871 /* New-function for the definedness hash table. */
1873 static struct bfd_hash_entry
*
1874 lang_definedness_newfunc (struct bfd_hash_entry
*entry
,
1875 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
1876 const char *name ATTRIBUTE_UNUSED
)
1878 struct lang_definedness_hash_entry
*ret
1879 = (struct lang_definedness_hash_entry
*) entry
;
1882 ret
= (struct lang_definedness_hash_entry
*)
1883 bfd_hash_allocate (table
, sizeof (struct lang_definedness_hash_entry
));
1886 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
1888 ret
->iteration
= -1;
1892 /* Return the iteration when the definition of NAME was last updated. A
1893 value of -1 means that the symbol is not defined in the linker script
1894 or the command line, but may be defined in the linker symbol table. */
1897 lang_symbol_definition_iteration (const char *name
)
1899 struct lang_definedness_hash_entry
*defentry
1900 = (struct lang_definedness_hash_entry
*)
1901 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1903 /* We've already created this one on the presence of DEFINED in the
1904 script, so it can't be NULL unless something is borked elsewhere in
1906 if (defentry
== NULL
)
1909 return defentry
->iteration
;
1912 /* Update the definedness state of NAME. */
1915 lang_update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
1917 struct lang_definedness_hash_entry
*defentry
1918 = (struct lang_definedness_hash_entry
*)
1919 bfd_hash_lookup (&lang_definedness_table
, name
, FALSE
, FALSE
);
1921 /* We don't keep track of symbols not tested with DEFINED. */
1922 if (defentry
== NULL
)
1925 /* If the symbol was already defined, and not from an earlier statement
1926 iteration, don't update the definedness iteration, because that'd
1927 make the symbol seem defined in the linker script at this point, and
1928 it wasn't; it was defined in some object. If we do anyway, DEFINED
1929 would start to yield false before this point and the construct "sym =
1930 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1932 if (h
->type
!= bfd_link_hash_undefined
1933 && h
->type
!= bfd_link_hash_common
1934 && h
->type
!= bfd_link_hash_new
1935 && defentry
->iteration
== -1)
1938 defentry
->iteration
= lang_statement_iteration
;
1941 /* Add the supplied name to the symbol table as an undefined reference.
1942 This is a two step process as the symbol table doesn't even exist at
1943 the time the ld command line is processed. First we put the name
1944 on a list, then, once the output file has been opened, transfer the
1945 name to the symbol table. */
1947 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
1949 #define ldlang_undef_chain_list_head entry_symbol.next
1952 ldlang_add_undef (const char *const name
)
1954 ldlang_undef_chain_list_type
*new =
1955 stat_alloc (sizeof (ldlang_undef_chain_list_type
));
1957 new->next
= ldlang_undef_chain_list_head
;
1958 ldlang_undef_chain_list_head
= new;
1960 new->name
= xstrdup (name
);
1962 if (output_bfd
!= NULL
)
1963 insert_undefined (new->name
);
1966 /* Insert NAME as undefined in the symbol table. */
1969 insert_undefined (const char *name
)
1971 struct bfd_link_hash_entry
*h
;
1973 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
1975 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1976 if (h
->type
== bfd_link_hash_new
)
1978 h
->type
= bfd_link_hash_undefined
;
1979 h
->u
.undef
.abfd
= NULL
;
1980 bfd_link_add_undef (link_info
.hash
, h
);
1984 /* Run through the list of undefineds created above and place them
1985 into the linker hash table as undefined symbols belonging to the
1989 lang_place_undefineds (void)
1991 ldlang_undef_chain_list_type
*ptr
;
1993 for (ptr
= ldlang_undef_chain_list_head
; ptr
!= NULL
; ptr
= ptr
->next
)
1994 insert_undefined (ptr
->name
);
1997 /* Check for all readonly or some readwrite sections. */
2000 check_input_sections
2001 (lang_statement_union_type
*s
,
2002 lang_output_section_statement_type
*output_section_statement
)
2004 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
2006 switch (s
->header
.type
)
2008 case lang_wild_statement_enum
:
2009 walk_wild (&s
->wild_statement
, check_section_callback
,
2010 output_section_statement
);
2011 if (! output_section_statement
->all_input_readonly
)
2014 case lang_constructors_statement_enum
:
2015 check_input_sections (constructor_list
.head
,
2016 output_section_statement
);
2017 if (! output_section_statement
->all_input_readonly
)
2020 case lang_group_statement_enum
:
2021 check_input_sections (s
->group_statement
.children
.head
,
2022 output_section_statement
);
2023 if (! output_section_statement
->all_input_readonly
)
2032 /* Open input files and attach to output sections. */
2035 map_input_to_output_sections
2036 (lang_statement_union_type
*s
, const char *target
,
2037 lang_output_section_statement_type
*output_section_statement
)
2039 for (; s
!= NULL
; s
= s
->header
.next
)
2041 switch (s
->header
.type
)
2043 case lang_wild_statement_enum
:
2044 wild (&s
->wild_statement
, target
, output_section_statement
);
2046 case lang_constructors_statement_enum
:
2047 map_input_to_output_sections (constructor_list
.head
,
2049 output_section_statement
);
2051 case lang_output_section_statement_enum
:
2052 if (s
->output_section_statement
.constraint
)
2054 if (s
->output_section_statement
.constraint
== -1)
2056 s
->output_section_statement
.all_input_readonly
= TRUE
;
2057 check_input_sections (s
->output_section_statement
.children
.head
,
2058 &s
->output_section_statement
);
2059 if ((s
->output_section_statement
.all_input_readonly
2060 && s
->output_section_statement
.constraint
== ONLY_IF_RW
)
2061 || (!s
->output_section_statement
.all_input_readonly
2062 && s
->output_section_statement
.constraint
== ONLY_IF_RO
))
2064 s
->output_section_statement
.constraint
= -1;
2069 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2071 &s
->output_section_statement
);
2073 case lang_output_statement_enum
:
2075 case lang_target_statement_enum
:
2076 target
= s
->target_statement
.target
;
2078 case lang_group_statement_enum
:
2079 map_input_to_output_sections (s
->group_statement
.children
.head
,
2081 output_section_statement
);
2083 case lang_data_statement_enum
:
2084 /* Make sure that any sections mentioned in the expression
2086 exp_init_os (s
->data_statement
.exp
);
2088 case lang_fill_statement_enum
:
2089 case lang_input_section_enum
:
2090 case lang_object_symbols_statement_enum
:
2091 case lang_reloc_statement_enum
:
2092 case lang_padding_statement_enum
:
2093 case lang_input_statement_enum
:
2094 if (output_section_statement
!= NULL
2095 && output_section_statement
->bfd_section
== NULL
)
2096 init_os (output_section_statement
);
2098 case lang_assignment_statement_enum
:
2099 if (output_section_statement
!= NULL
2100 && output_section_statement
->bfd_section
== NULL
)
2101 init_os (output_section_statement
);
2103 /* Make sure that any sections mentioned in the assignment
2105 exp_init_os (s
->assignment_statement
.exp
);
2107 case lang_afile_asection_pair_statement_enum
:
2110 case lang_address_statement_enum
:
2111 /* Mark the specified section with the supplied address. */
2113 lang_output_section_statement_type
*os
=
2114 lang_output_section_statement_lookup
2115 (s
->address_statement
.section_name
);
2117 if (os
->bfd_section
== NULL
)
2119 os
->addr_tree
= s
->address_statement
.address
;
2126 /* An output section might have been removed after its statement was
2127 added. For example, ldemul_before_allocation can remove dynamic
2128 sections if they turn out to be not needed. Clean them up here. */
2131 strip_excluded_output_sections (void)
2133 lang_statement_union_type
*u
;
2135 for (u
= lang_output_section_statement
.head
;
2137 u
= u
->output_section_statement
.next
)
2139 lang_output_section_statement_type
*os
;
2142 os
= &u
->output_section_statement
;
2143 if (os
->constraint
== -1)
2145 s
= os
->bfd_section
;
2146 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2150 os
->bfd_section
= NULL
;
2152 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2155 bfd_section_list_remove (output_bfd
, p
);
2156 output_bfd
->section_count
--;
2164 print_output_section_statement
2165 (lang_output_section_statement_type
*output_section_statement
)
2167 asection
*section
= output_section_statement
->bfd_section
;
2170 if (output_section_statement
!= abs_output_section
)
2172 minfo ("\n%s", output_section_statement
->name
);
2174 if (section
!= NULL
)
2176 print_dot
= section
->vma
;
2178 len
= strlen (output_section_statement
->name
);
2179 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2184 while (len
< SECTION_NAME_MAP_LENGTH
)
2190 minfo ("0x%V %W", section
->vma
, section
->size
);
2192 if (output_section_statement
->load_base
!= NULL
)
2196 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2197 "load base", lang_final_phase_enum
);
2198 minfo (_(" load address 0x%V"), addr
);
2205 print_statement_list (output_section_statement
->children
.head
,
2206 output_section_statement
);
2210 print_assignment (lang_assignment_statement_type
*assignment
,
2211 lang_output_section_statement_type
*output_section
)
2214 etree_value_type result
;
2216 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2219 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2220 lang_final_phase_enum
, print_dot
, &print_dot
);
2226 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2227 dst
= assignment
->exp
->assign
.dst
;
2229 minfo ("0x%V", value
);
2230 if (dst
[0] == '.' && dst
[1] == 0)
2243 exp_print_tree (assignment
->exp
);
2249 print_input_statement (lang_input_statement_type
*statm
)
2251 if (statm
->filename
!= NULL
)
2253 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2257 /* Print all symbols defined in a particular section. This is called
2258 via bfd_link_hash_traverse, or by print_all_symbols. */
2261 print_one_symbol (struct bfd_link_hash_entry
*hash_entry
, void *ptr
)
2263 asection
*sec
= ptr
;
2265 if ((hash_entry
->type
== bfd_link_hash_defined
2266 || hash_entry
->type
== bfd_link_hash_defweak
)
2267 && sec
== hash_entry
->u
.def
.section
)
2271 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2274 (hash_entry
->u
.def
.value
2275 + hash_entry
->u
.def
.section
->output_offset
2276 + hash_entry
->u
.def
.section
->output_section
->vma
));
2278 minfo (" %T\n", hash_entry
->root
.string
);
2285 print_all_symbols (sec
)
2288 struct fat_user_section_struct
*ud
= get_userdata (sec
);
2289 struct map_symbol_def
*def
;
2291 *ud
->map_symbol_def_tail
= 0;
2292 for (def
= ud
->map_symbol_def_head
; def
; def
= def
->next
)
2293 print_one_symbol (def
->entry
, sec
);
2296 /* Print information about an input section to the map file. */
2299 print_input_section (lang_input_section_type
*in
)
2301 asection
*i
= in
->section
;
2302 bfd_size_type size
= i
->size
;
2311 minfo ("%s", i
->name
);
2313 len
= 1 + strlen (i
->name
);
2314 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2319 while (len
< SECTION_NAME_MAP_LENGTH
)
2325 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2326 addr
= i
->output_section
->vma
+ i
->output_offset
;
2333 minfo ("0x%V %W %B\n", addr
, TO_ADDR (size
), i
->owner
);
2335 if (size
!= i
->rawsize
&& i
->rawsize
!= 0)
2337 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2349 minfo (_("%W (size before relaxing)\n"), i
->rawsize
);
2352 if (i
->output_section
!= NULL
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2354 if (command_line
.reduce_memory_overheads
)
2355 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, i
);
2357 print_all_symbols (i
);
2359 print_dot
= addr
+ TO_ADDR (size
);
2365 print_fill_statement (lang_fill_statement_type
*fill
)
2369 fputs (" FILL mask 0x", config
.map_file
);
2370 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2371 fprintf (config
.map_file
, "%02x", *p
);
2372 fputs ("\n", config
.map_file
);
2376 print_data_statement (lang_data_statement_type
*data
)
2384 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2387 addr
= data
->output_vma
;
2388 if (data
->output_section
!= NULL
)
2389 addr
+= data
->output_section
->vma
;
2417 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2419 if (data
->exp
->type
.node_class
!= etree_value
)
2422 exp_print_tree (data
->exp
);
2427 print_dot
= addr
+ TO_ADDR (size
);
2430 /* Print an address statement. These are generated by options like
2434 print_address_statement (lang_address_statement_type
*address
)
2436 minfo (_("Address of section %s set to "), address
->section_name
);
2437 exp_print_tree (address
->address
);
2441 /* Print a reloc statement. */
2444 print_reloc_statement (lang_reloc_statement_type
*reloc
)
2451 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2454 addr
= reloc
->output_vma
;
2455 if (reloc
->output_section
!= NULL
)
2456 addr
+= reloc
->output_section
->vma
;
2458 size
= bfd_get_reloc_size (reloc
->howto
);
2460 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2462 if (reloc
->name
!= NULL
)
2463 minfo ("%s+", reloc
->name
);
2465 minfo ("%s+", reloc
->section
->name
);
2467 exp_print_tree (reloc
->addend_exp
);
2471 print_dot
= addr
+ TO_ADDR (size
);
2475 print_padding_statement (lang_padding_statement_type
*s
)
2483 len
= sizeof " *fill*" - 1;
2484 while (len
< SECTION_NAME_MAP_LENGTH
)
2490 addr
= s
->output_offset
;
2491 if (s
->output_section
!= NULL
)
2492 addr
+= s
->output_section
->vma
;
2493 minfo ("0x%V %W ", addr
, (bfd_vma
) s
->size
);
2495 if (s
->fill
->size
!= 0)
2499 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2500 fprintf (config
.map_file
, "%02x", *p
);
2505 print_dot
= addr
+ TO_ADDR (s
->size
);
2509 print_wild_statement (lang_wild_statement_type
*w
,
2510 lang_output_section_statement_type
*os
)
2512 struct wildcard_list
*sec
;
2516 if (w
->filenames_sorted
)
2518 if (w
->filename
!= NULL
)
2519 minfo ("%s", w
->filename
);
2522 if (w
->filenames_sorted
)
2526 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2528 if (sec
->spec
.sorted
)
2530 if (sec
->spec
.exclude_name_list
!= NULL
)
2533 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2534 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2535 minfo (" %s", tmp
->name
);
2538 if (sec
->spec
.name
!= NULL
)
2539 minfo ("%s", sec
->spec
.name
);
2542 if (sec
->spec
.sorted
)
2551 print_statement_list (w
->children
.head
, os
);
2554 /* Print a group statement. */
2557 print_group (lang_group_statement_type
*s
,
2558 lang_output_section_statement_type
*os
)
2560 fprintf (config
.map_file
, "START GROUP\n");
2561 print_statement_list (s
->children
.head
, os
);
2562 fprintf (config
.map_file
, "END GROUP\n");
2565 /* Print the list of statements in S.
2566 This can be called for any statement type. */
2569 print_statement_list (lang_statement_union_type
*s
,
2570 lang_output_section_statement_type
*os
)
2574 print_statement (s
, os
);
2579 /* Print the first statement in statement list S.
2580 This can be called for any statement type. */
2583 print_statement (lang_statement_union_type
*s
,
2584 lang_output_section_statement_type
*os
)
2586 switch (s
->header
.type
)
2589 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2592 case lang_constructors_statement_enum
:
2593 if (constructor_list
.head
!= NULL
)
2595 if (constructors_sorted
)
2596 minfo (" SORT (CONSTRUCTORS)\n");
2598 minfo (" CONSTRUCTORS\n");
2599 print_statement_list (constructor_list
.head
, os
);
2602 case lang_wild_statement_enum
:
2603 print_wild_statement (&s
->wild_statement
, os
);
2605 case lang_address_statement_enum
:
2606 print_address_statement (&s
->address_statement
);
2608 case lang_object_symbols_statement_enum
:
2609 minfo (" CREATE_OBJECT_SYMBOLS\n");
2611 case lang_fill_statement_enum
:
2612 print_fill_statement (&s
->fill_statement
);
2614 case lang_data_statement_enum
:
2615 print_data_statement (&s
->data_statement
);
2617 case lang_reloc_statement_enum
:
2618 print_reloc_statement (&s
->reloc_statement
);
2620 case lang_input_section_enum
:
2621 print_input_section (&s
->input_section
);
2623 case lang_padding_statement_enum
:
2624 print_padding_statement (&s
->padding_statement
);
2626 case lang_output_section_statement_enum
:
2627 print_output_section_statement (&s
->output_section_statement
);
2629 case lang_assignment_statement_enum
:
2630 print_assignment (&s
->assignment_statement
, os
);
2632 case lang_target_statement_enum
:
2633 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2635 case lang_output_statement_enum
:
2636 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2637 if (output_target
!= NULL
)
2638 minfo (" %s", output_target
);
2641 case lang_input_statement_enum
:
2642 print_input_statement (&s
->input_statement
);
2644 case lang_group_statement_enum
:
2645 print_group (&s
->group_statement
, os
);
2647 case lang_afile_asection_pair_statement_enum
:
2654 print_statements (void)
2656 print_statement_list (statement_list
.head
, abs_output_section
);
2659 /* Print the first N statements in statement list S to STDERR.
2660 If N == 0, nothing is printed.
2661 If N < 0, the entire list is printed.
2662 Intended to be called from GDB. */
2665 dprint_statement (lang_statement_union_type
*s
, int n
)
2667 FILE *map_save
= config
.map_file
;
2669 config
.map_file
= stderr
;
2672 print_statement_list (s
, abs_output_section
);
2675 while (s
&& --n
>= 0)
2677 print_statement (s
, abs_output_section
);
2682 config
.map_file
= map_save
;
2686 insert_pad (lang_statement_union_type
**ptr
,
2688 unsigned int alignment_needed
,
2689 asection
*output_section
,
2692 static fill_type zero_fill
= { 1, { 0 } };
2693 lang_statement_union_type
*pad
;
2695 pad
= ((lang_statement_union_type
*)
2696 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2697 if (ptr
!= &statement_list
.head
2698 && pad
->header
.type
== lang_padding_statement_enum
2699 && pad
->padding_statement
.output_section
== output_section
)
2701 /* Use the existing pad statement. The above test on output
2702 section is probably redundant, but it doesn't hurt to check. */
2706 /* Make a new padding statement, linked into existing chain. */
2707 pad
= stat_alloc (sizeof (lang_padding_statement_type
));
2708 pad
->header
.next
= *ptr
;
2710 pad
->header
.type
= lang_padding_statement_enum
;
2711 pad
->padding_statement
.output_section
= output_section
;
2714 pad
->padding_statement
.fill
= fill
;
2716 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2717 pad
->padding_statement
.size
= alignment_needed
;
2718 output_section
->size
+= alignment_needed
;
2721 /* Work out how much this section will move the dot point. */
2725 (lang_statement_union_type
**this_ptr
,
2726 lang_output_section_statement_type
*output_section_statement
,
2730 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2731 asection
*i
= is
->section
;
2733 if (!is
->ifile
->just_syms_flag
&& (i
->flags
& SEC_EXCLUDE
) == 0)
2735 unsigned int alignment_needed
;
2738 /* Align this section first to the input sections requirement,
2739 then to the output section's requirement. If this alignment
2740 is greater than any seen before, then record it too. Perform
2741 the alignment by inserting a magic 'padding' statement. */
2743 if (output_section_statement
->subsection_alignment
!= -1)
2744 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2746 o
= output_section_statement
->bfd_section
;
2747 if (o
->alignment_power
< i
->alignment_power
)
2748 o
->alignment_power
= i
->alignment_power
;
2750 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2752 if (alignment_needed
!= 0)
2754 insert_pad (this_ptr
, fill
, TO_SIZE (alignment_needed
), o
, dot
);
2755 dot
+= alignment_needed
;
2758 /* Remember where in the output section this input section goes. */
2760 i
->output_offset
= dot
- o
->vma
;
2762 /* Mark how big the output section must be to contain this now. */
2763 dot
+= TO_ADDR (i
->size
);
2764 o
->size
= TO_SIZE (dot
- o
->vma
);
2768 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2774 #define IGNORE_SECTION(s) \
2775 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2776 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2777 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2780 /* Check to see if any allocated sections overlap with other allocated
2781 sections. This can happen when the linker script specifically specifies
2782 the output section addresses of the two sections. */
2785 lang_check_section_addresses (void)
2789 /* Scan all sections in the output list. */
2790 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2794 /* Ignore sections which are not loaded or which have no contents. */
2795 if (IGNORE_SECTION (s
))
2798 /* Once we reach section 's' stop our seach. This prevents two
2799 warning messages from being produced, one for 'section A overlaps
2800 section B' and one for 'section B overlaps section A'. */
2801 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2808 /* Only consider loadable sections with real contents. */
2809 if (IGNORE_SECTION (os
))
2812 /* We must check the sections' LMA addresses not their
2813 VMA addresses because overlay sections can have
2814 overlapping VMAs but they must have distinct LMAs. */
2815 s_start
= bfd_section_lma (output_bfd
, s
);
2816 os_start
= bfd_section_lma (output_bfd
, os
);
2817 s_end
= s_start
+ TO_ADDR (s
->size
) - 1;
2818 os_end
= os_start
+ TO_ADDR (os
->size
) - 1;
2820 /* Look for an overlap. */
2821 if ((s_end
< os_start
) || (s_start
> os_end
))
2825 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2826 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2828 /* Once we have found one overlap for this section,
2829 stop looking for others. */
2835 /* Make sure the new address is within the region. We explicitly permit the
2836 current address to be at the exact end of the region when the address is
2837 non-zero, in case the region is at the end of addressable memory and the
2838 calculation wraps around. */
2841 os_region_check (lang_output_section_statement_type
*os
,
2842 lang_memory_region_type
*region
,
2846 if ((region
->current
< region
->origin
2847 || (region
->current
- region
->origin
> region
->length
))
2848 && ((region
->current
!= region
->origin
+ region
->length
)
2853 einfo (_("%X%P: address 0x%v of %B section %s"
2854 " is not within region %s\n"),
2856 os
->bfd_section
->owner
,
2857 os
->bfd_section
->name
,
2862 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2864 os
->bfd_section
->owner
,
2865 os
->bfd_section
->name
);
2867 /* Reset the region pointer. */
2868 region
->current
= region
->origin
;
2872 /* Set the sizes for all the output sections. */
2875 lang_size_sections_1
2876 (lang_statement_union_type
*s
,
2877 lang_output_section_statement_type
*output_section_statement
,
2878 lang_statement_union_type
**prev
,
2882 bfd_boolean check_regions
)
2884 /* Size up the sections from their constituent parts. */
2885 for (; s
!= NULL
; s
= s
->header
.next
)
2887 switch (s
->header
.type
)
2889 case lang_output_section_statement_enum
:
2892 lang_output_section_statement_type
*os
;
2894 os
= &s
->output_section_statement
;
2895 if (os
->bfd_section
== NULL
)
2896 /* This section was never actually created. */
2899 /* If this is a COFF shared library section, use the size and
2900 address from the input section. FIXME: This is COFF
2901 specific; it would be cleaner if there were some other way
2902 to do this, but nothing simple comes to mind. */
2903 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2907 if (os
->children
.head
== NULL
2908 || os
->children
.head
->header
.next
!= NULL
2909 || (os
->children
.head
->header
.type
2910 != lang_input_section_enum
))
2911 einfo (_("%P%X: Internal error on COFF shared library"
2912 " section %s\n"), os
->name
);
2914 input
= os
->children
.head
->input_section
.section
;
2915 bfd_set_section_vma (os
->bfd_section
->owner
,
2917 bfd_section_vma (input
->owner
, input
));
2918 os
->bfd_section
->size
= input
->size
;
2922 if (bfd_is_abs_section (os
->bfd_section
))
2924 /* No matter what happens, an abs section starts at zero. */
2925 ASSERT (os
->bfd_section
->vma
== 0);
2929 if (os
->addr_tree
== NULL
)
2931 /* No address specified for this section, get one
2932 from the region specification. */
2933 if (os
->region
== NULL
2934 || ((os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))
2935 && os
->region
->name
[0] == '*'
2936 && strcmp (os
->region
->name
,
2937 DEFAULT_MEMORY_REGION
) == 0))
2939 os
->region
= lang_memory_default (os
->bfd_section
);
2942 /* If a loadable section is using the default memory
2943 region, and some non default memory regions were
2944 defined, issue an error message. */
2945 if (!IGNORE_SECTION (os
->bfd_section
)
2946 && ! link_info
.relocatable
2948 && strcmp (os
->region
->name
,
2949 DEFAULT_MEMORY_REGION
) == 0
2950 && lang_memory_region_list
!= NULL
2951 && (strcmp (lang_memory_region_list
->name
,
2952 DEFAULT_MEMORY_REGION
) != 0
2953 || lang_memory_region_list
->next
!= NULL
))
2955 /* By default this is an error rather than just a
2956 warning because if we allocate the section to the
2957 default memory region we can end up creating an
2958 excessively large binary, or even seg faulting when
2959 attempting to perform a negative seek. See
2960 sources.redhat.com/ml/binutils/2003-04/msg00423.html
2961 for an example of this. This behaviour can be
2962 overridden by the using the --no-check-sections
2964 if (command_line
.check_section_addresses
)
2965 einfo (_("%P%F: error: no memory region specified"
2966 " for loadable section `%s'\n"),
2967 bfd_get_section_name (output_bfd
,
2970 einfo (_("%P: warning: no memory region specified"
2971 " for loadable section `%s'\n"),
2972 bfd_get_section_name (output_bfd
,
2976 dot
= os
->region
->current
;
2978 if (os
->section_alignment
== -1)
2983 dot
= align_power (dot
,
2984 os
->bfd_section
->alignment_power
);
2986 if (dot
!= olddot
&& config
.warn_section_align
)
2987 einfo (_("%P: warning: changing start of section"
2988 " %s by %u bytes\n"),
2989 os
->name
, (unsigned int) (dot
- olddot
));
2997 r
= exp_fold_tree (os
->addr_tree
,
2999 lang_allocating_phase_enum
,
3004 einfo (_("%F%S: non constant or forward reference"
3005 " address expression for section %s\n"),
3008 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
3011 /* The section starts here.
3012 First, align to what the section needs. */
3014 if (os
->section_alignment
!= -1)
3015 dot
= align_power (dot
, os
->section_alignment
);
3017 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3019 os
->bfd_section
->output_offset
= 0;
3022 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3023 os
->fill
, dot
, relax
, check_regions
);
3025 /* Put the section within the requested block size, or
3026 align at the block boundary. */
3027 after
= ((os
->bfd_section
->vma
3028 + TO_ADDR (os
->bfd_section
->size
)
3029 + os
->block_value
- 1)
3030 & - (bfd_vma
) os
->block_value
);
3032 if (bfd_is_abs_section (os
->bfd_section
))
3033 ASSERT (after
== os
->bfd_section
->vma
);
3035 os
->bfd_section
->size
3036 = TO_SIZE (after
- os
->bfd_section
->vma
);
3038 dot
= os
->bfd_section
->vma
;
3039 /* .tbss sections effectively have zero size. */
3040 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3041 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3042 || link_info
.relocatable
)
3043 dot
+= TO_ADDR (os
->bfd_section
->size
);
3047 if (os
->update_dot_tree
!= 0)
3048 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3049 lang_allocating_phase_enum
, dot
, &dot
);
3051 /* Update dot in the region ?
3052 We only do this if the section is going to be allocated,
3053 since unallocated sections do not contribute to the region's
3054 overall size in memory.
3056 If the SEC_NEVER_LOAD bit is not set, it will affect the
3057 addresses of sections after it. We have to update
3059 if (os
->region
!= NULL
3060 && ((os
->bfd_section
->flags
& SEC_NEVER_LOAD
) == 0
3061 || (os
->bfd_section
->flags
& (SEC_ALLOC
| SEC_LOAD
))))
3063 os
->region
->current
= dot
;
3066 /* Make sure the new address is within the region. */
3067 os_region_check (os
, os
->region
, os
->addr_tree
,
3068 os
->bfd_section
->vma
);
3070 /* If there's no load address specified, use the run
3071 region as the load region. */
3072 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3073 os
->lma_region
= os
->region
;
3075 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3077 /* Set load_base, which will be handled later. */
3078 os
->load_base
= exp_intop (os
->lma_region
->current
);
3079 os
->lma_region
->current
+=
3080 TO_ADDR (os
->bfd_section
->size
);
3082 os_region_check (os
, os
->lma_region
, NULL
,
3083 os
->bfd_section
->lma
);
3089 case lang_constructors_statement_enum
:
3090 dot
= lang_size_sections_1 (constructor_list
.head
,
3091 output_section_statement
,
3092 &s
->wild_statement
.children
.head
,
3093 fill
, dot
, relax
, check_regions
);
3096 case lang_data_statement_enum
:
3098 unsigned int size
= 0;
3100 s
->data_statement
.output_vma
=
3101 dot
- output_section_statement
->bfd_section
->vma
;
3102 s
->data_statement
.output_section
=
3103 output_section_statement
->bfd_section
;
3105 /* We might refer to provided symbols in the expression, and
3106 need to mark them as needed. */
3107 exp_fold_tree (s
->data_statement
.exp
, abs_output_section
,
3108 lang_allocating_phase_enum
, dot
, &dot
);
3110 switch (s
->data_statement
.type
)
3128 if (size
< TO_SIZE ((unsigned) 1))
3129 size
= TO_SIZE ((unsigned) 1);
3130 dot
+= TO_ADDR (size
);
3131 output_section_statement
->bfd_section
->size
+= size
;
3132 /* The output section gets contents, and then we inspect for
3133 any flags set in the input script which override any ALLOC. */
3134 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3135 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3137 output_section_statement
->bfd_section
->flags
|=
3138 SEC_ALLOC
| SEC_LOAD
;
3143 case lang_reloc_statement_enum
:
3147 s
->reloc_statement
.output_vma
=
3148 dot
- output_section_statement
->bfd_section
->vma
;
3149 s
->reloc_statement
.output_section
=
3150 output_section_statement
->bfd_section
;
3151 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3152 dot
+= TO_ADDR (size
);
3153 output_section_statement
->bfd_section
->size
+= size
;
3157 case lang_wild_statement_enum
:
3159 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3160 output_section_statement
,
3161 &s
->wild_statement
.children
.head
,
3162 fill
, dot
, relax
, check_regions
);
3166 case lang_object_symbols_statement_enum
:
3167 link_info
.create_object_symbols_section
=
3168 output_section_statement
->bfd_section
;
3170 case lang_output_statement_enum
:
3171 case lang_target_statement_enum
:
3173 case lang_input_section_enum
:
3177 i
= (*prev
)->input_section
.section
;
3182 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3183 einfo (_("%P%F: can't relax section: %E\n"));
3187 dot
= size_input_section (prev
, output_section_statement
,
3188 output_section_statement
->fill
, dot
);
3191 case lang_input_statement_enum
:
3193 case lang_fill_statement_enum
:
3194 s
->fill_statement
.output_section
=
3195 output_section_statement
->bfd_section
;
3197 fill
= s
->fill_statement
.fill
;
3199 case lang_assignment_statement_enum
:
3201 bfd_vma newdot
= dot
;
3203 exp_fold_tree (s
->assignment_statement
.exp
,
3204 output_section_statement
,
3205 lang_allocating_phase_enum
,
3211 if (output_section_statement
== abs_output_section
)
3213 /* If we don't have an output section, then just adjust
3214 the default memory address. */
3215 lang_memory_region_lookup (DEFAULT_MEMORY_REGION
,
3216 FALSE
)->current
= newdot
;
3220 /* Insert a pad after this statement. We can't
3221 put the pad before when relaxing, in case the
3222 assignment references dot. */
3223 insert_pad (&s
->header
.next
, fill
, TO_SIZE (newdot
- dot
),
3224 output_section_statement
->bfd_section
, dot
);
3226 /* Don't neuter the pad below when relaxing. */
3230 /* If dot is advanced, this implies that the section should
3231 have space allocated to it, unless the user has explicitly
3232 stated that the section should never be loaded. */
3233 if (!(output_section_statement
->flags
3234 & (SEC_NEVER_LOAD
| SEC_ALLOC
)))
3235 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
;
3242 case lang_padding_statement_enum
:
3243 /* If this is the first time lang_size_sections is called,
3244 we won't have any padding statements. If this is the
3245 second or later passes when relaxing, we should allow
3246 padding to shrink. If padding is needed on this pass, it
3247 will be added back in. */
3248 s
->padding_statement
.size
= 0;
3250 /* Make sure output_offset is valid. If relaxation shrinks
3251 the section and this pad isn't needed, it's possible to
3252 have output_offset larger than the final size of the
3253 section. bfd_set_section_contents will complain even for
3254 a pad size of zero. */
3255 s
->padding_statement
.output_offset
3256 = dot
- output_section_statement
->bfd_section
->vma
;
3259 case lang_group_statement_enum
:
3260 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3261 output_section_statement
,
3262 &s
->group_statement
.children
.head
,
3263 fill
, dot
, relax
, check_regions
);
3270 /* We can only get here when relaxing is turned on. */
3271 case lang_address_statement_enum
:
3274 prev
= &s
->header
.next
;
3281 (lang_statement_union_type
*s
,
3282 lang_output_section_statement_type
*output_section_statement
,
3283 lang_statement_union_type
**prev
,
3287 bfd_boolean check_regions
)
3291 /* Callers of exp_fold_tree need to increment this. */
3292 lang_statement_iteration
++;
3294 exp_data_seg
.phase
= exp_dataseg_none
;
3295 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3296 dot
, relax
, check_regions
);
3297 if (exp_data_seg
.phase
== exp_dataseg_end_seen
3298 && link_info
.relro
&& exp_data_seg
.relro_end
)
3300 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3301 to put exp_data_seg.relro on a (common) page boundary. */
3303 exp_data_seg
.phase
= exp_dataseg_relro_adjust
;
3304 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3305 dot
, relax
, check_regions
);
3306 link_info
.relro_start
= exp_data_seg
.base
;
3307 link_info
.relro_end
= exp_data_seg
.relro_end
;
3309 else if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3311 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3312 a page could be saved in the data segment. */
3313 bfd_vma first
, last
;
3315 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3316 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3318 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3319 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3320 && first
+ last
<= exp_data_seg
.pagesize
)
3322 exp_data_seg
.phase
= exp_dataseg_adjust
;
3323 lang_statement_iteration
++;
3324 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3325 fill
, dot
, relax
, check_regions
);
3332 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3335 lang_do_assignments_1
3336 (lang_statement_union_type
*s
,
3337 lang_output_section_statement_type
*output_section_statement
,
3341 for (; s
!= NULL
; s
= s
->header
.next
)
3343 switch (s
->header
.type
)
3345 case lang_constructors_statement_enum
:
3346 dot
= lang_do_assignments_1 (constructor_list
.head
,
3347 output_section_statement
,
3352 case lang_output_section_statement_enum
:
3354 lang_output_section_statement_type
*os
;
3356 os
= &(s
->output_section_statement
);
3357 if (os
->bfd_section
!= NULL
)
3359 dot
= os
->bfd_section
->vma
;
3360 lang_do_assignments_1 (os
->children
.head
, os
, os
->fill
, dot
);
3361 /* .tbss sections effectively have zero size. */
3362 if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) != 0
3363 || (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
) == 0
3364 || link_info
.relocatable
)
3365 dot
+= TO_ADDR (os
->bfd_section
->size
);
3369 /* If nothing has been placed into the output section then
3370 it won't have a bfd_section. */
3371 if (os
->bfd_section
)
3373 os
->bfd_section
->lma
3374 = exp_get_abs_int (os
->load_base
, 0, "load base",
3375 lang_final_phase_enum
);
3380 case lang_wild_statement_enum
:
3382 dot
= lang_do_assignments_1 (s
->wild_statement
.children
.head
,
3383 output_section_statement
,
3388 case lang_object_symbols_statement_enum
:
3389 case lang_output_statement_enum
:
3390 case lang_target_statement_enum
:
3392 case lang_common_statement_enum
:
3395 case lang_data_statement_enum
:
3397 etree_value_type value
;
3399 value
= exp_fold_tree (s
->data_statement
.exp
,
3401 lang_final_phase_enum
, dot
, &dot
);
3403 einfo (_("%F%P: invalid data statement\n"));
3404 s
->data_statement
.value
3405 = value
.value
+ value
.section
->bfd_section
->vma
;
3409 switch (s
->data_statement
.type
)
3427 if (size
< TO_SIZE ((unsigned) 1))
3428 size
= TO_SIZE ((unsigned) 1);
3429 dot
+= TO_ADDR (size
);
3433 case lang_reloc_statement_enum
:
3435 etree_value_type value
;
3437 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3439 lang_final_phase_enum
, dot
, &dot
);
3440 s
->reloc_statement
.addend_value
= value
.value
;
3442 einfo (_("%F%P: invalid reloc statement\n"));
3444 dot
+= TO_ADDR (bfd_get_reloc_size (s
->reloc_statement
.howto
));
3447 case lang_input_section_enum
:
3449 asection
*in
= s
->input_section
.section
;
3451 if ((in
->flags
& SEC_EXCLUDE
) == 0)
3452 dot
+= TO_ADDR (in
->size
);
3456 case lang_input_statement_enum
:
3458 case lang_fill_statement_enum
:
3459 fill
= s
->fill_statement
.fill
;
3461 case lang_assignment_statement_enum
:
3463 exp_fold_tree (s
->assignment_statement
.exp
,
3464 output_section_statement
,
3465 lang_final_phase_enum
,
3471 case lang_padding_statement_enum
:
3472 dot
+= TO_ADDR (s
->padding_statement
.size
);
3475 case lang_group_statement_enum
:
3476 dot
= lang_do_assignments_1 (s
->group_statement
.children
.head
,
3477 output_section_statement
,
3485 case lang_address_statement_enum
:
3495 (lang_statement_union_type
*s
,
3496 lang_output_section_statement_type
*output_section_statement
,
3500 /* Callers of exp_fold_tree need to increment this. */
3501 lang_statement_iteration
++;
3502 lang_do_assignments_1 (s
, output_section_statement
, fill
, dot
);
3505 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3506 operator .startof. (section_name), it produces an undefined symbol
3507 .startof.section_name. Similarly, when it sees
3508 .sizeof. (section_name), it produces an undefined symbol
3509 .sizeof.section_name. For all the output sections, we look for
3510 such symbols, and set them to the correct value. */
3513 lang_set_startof (void)
3517 if (link_info
.relocatable
)
3520 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3522 const char *secname
;
3524 struct bfd_link_hash_entry
*h
;
3526 secname
= bfd_get_section_name (output_bfd
, s
);
3527 buf
= xmalloc (10 + strlen (secname
));
3529 sprintf (buf
, ".startof.%s", secname
);
3530 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3531 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3533 h
->type
= bfd_link_hash_defined
;
3534 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3535 h
->u
.def
.section
= bfd_abs_section_ptr
;
3538 sprintf (buf
, ".sizeof.%s", secname
);
3539 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3540 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3542 h
->type
= bfd_link_hash_defined
;
3543 h
->u
.def
.value
= TO_ADDR (s
->size
);
3544 h
->u
.def
.section
= bfd_abs_section_ptr
;
3554 struct bfd_link_hash_entry
*h
;
3557 if (link_info
.relocatable
|| link_info
.shared
)
3562 if (entry_symbol
.name
== NULL
)
3564 /* No entry has been specified. Look for start, but don't warn
3565 if we don't find it. */
3566 entry_symbol
.name
= "start";
3570 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3571 FALSE
, FALSE
, TRUE
);
3573 && (h
->type
== bfd_link_hash_defined
3574 || h
->type
== bfd_link_hash_defweak
)
3575 && h
->u
.def
.section
->output_section
!= NULL
)
3579 val
= (h
->u
.def
.value
3580 + bfd_get_section_vma (output_bfd
,
3581 h
->u
.def
.section
->output_section
)
3582 + h
->u
.def
.section
->output_offset
);
3583 if (! bfd_set_start_address (output_bfd
, val
))
3584 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3591 /* We couldn't find the entry symbol. Try parsing it as a
3593 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3596 if (! bfd_set_start_address (output_bfd
, val
))
3597 einfo (_("%P%F: can't set start address\n"));
3603 /* Can't find the entry symbol, and it's not a number. Use
3604 the first address in the text section. */
3605 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3609 einfo (_("%P: warning: cannot find entry symbol %s;"
3610 " defaulting to %V\n"),
3612 bfd_get_section_vma (output_bfd
, ts
));
3613 if (! bfd_set_start_address (output_bfd
,
3614 bfd_get_section_vma (output_bfd
,
3616 einfo (_("%P%F: can't set start address\n"));
3621 einfo (_("%P: warning: cannot find entry symbol %s;"
3622 " not setting start address\n"),
3628 /* Don't bfd_hash_table_free (&lang_definedness_table);
3629 map file output may result in a call of lang_track_definedness. */
3632 /* This is a small function used when we want to ignore errors from
3636 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3638 /* Don't do anything. */
3641 /* Check that the architecture of all the input files is compatible
3642 with the output file. Also call the backend to let it do any
3643 other checking that is needed. */
3648 lang_statement_union_type
*file
;
3650 const bfd_arch_info_type
*compatible
;
3652 for (file
= file_chain
.head
; file
!= NULL
; file
= file
->input_statement
.next
)
3654 input_bfd
= file
->input_statement
.the_bfd
;
3656 = bfd_arch_get_compatible (input_bfd
, output_bfd
,
3657 command_line
.accept_unknown_input_arch
);
3659 /* In general it is not possible to perform a relocatable
3660 link between differing object formats when the input
3661 file has relocations, because the relocations in the
3662 input format may not have equivalent representations in
3663 the output format (and besides BFD does not translate
3664 relocs for other link purposes than a final link). */
3665 if ((link_info
.relocatable
|| link_info
.emitrelocations
)
3666 && (compatible
== NULL
3667 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3668 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3670 einfo (_("%P%F: Relocatable linking with relocations from"
3671 " format %s (%B) to format %s (%B) is not supported\n"),
3672 bfd_get_target (input_bfd
), input_bfd
,
3673 bfd_get_target (output_bfd
), output_bfd
);
3674 /* einfo with %F exits. */
3677 if (compatible
== NULL
)
3679 if (command_line
.warn_mismatch
)
3680 einfo (_("%P: warning: %s architecture of input file `%B'"
3681 " is incompatible with %s output\n"),
3682 bfd_printable_name (input_bfd
), input_bfd
,
3683 bfd_printable_name (output_bfd
));
3685 else if (bfd_count_sections (input_bfd
))
3687 /* If the input bfd has no contents, it shouldn't set the
3688 private data of the output bfd. */
3690 bfd_error_handler_type pfn
= NULL
;
3692 /* If we aren't supposed to warn about mismatched input
3693 files, temporarily set the BFD error handler to a
3694 function which will do nothing. We still want to call
3695 bfd_merge_private_bfd_data, since it may set up
3696 information which is needed in the output file. */
3697 if (! command_line
.warn_mismatch
)
3698 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3699 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3701 if (command_line
.warn_mismatch
)
3702 einfo (_("%P%X: failed to merge target specific data"
3703 " of file %B\n"), input_bfd
);
3705 if (! command_line
.warn_mismatch
)
3706 bfd_set_error_handler (pfn
);
3711 /* Look through all the global common symbols and attach them to the
3712 correct section. The -sort-common command line switch may be used
3713 to roughly sort the entries by size. */
3718 if (command_line
.inhibit_common_definition
)
3720 if (link_info
.relocatable
3721 && ! command_line
.force_common_definition
)
3724 if (! config
.sort_common
)
3725 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, NULL
);
3730 for (power
= 4; power
>= 0; power
--)
3731 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, &power
);
3735 /* Place one common symbol in the correct section. */
3738 lang_one_common (struct bfd_link_hash_entry
*h
, void *info
)
3740 unsigned int power_of_two
;
3744 if (h
->type
!= bfd_link_hash_common
)
3748 power_of_two
= h
->u
.c
.p
->alignment_power
;
3750 if (config
.sort_common
3751 && power_of_two
< (unsigned int) *(int *) info
)
3754 section
= h
->u
.c
.p
->section
;
3756 /* Increase the size of the section to align the common sym. */
3757 section
->size
+= ((bfd_vma
) 1 << (power_of_two
+ opb_shift
)) - 1;
3758 section
->size
&= (- (bfd_vma
) 1 << (power_of_two
+ opb_shift
));
3760 /* Adjust the alignment if necessary. */
3761 if (power_of_two
> section
->alignment_power
)
3762 section
->alignment_power
= power_of_two
;
3764 /* Change the symbol from common to defined. */
3765 h
->type
= bfd_link_hash_defined
;
3766 h
->u
.def
.section
= section
;
3767 h
->u
.def
.value
= section
->size
;
3769 /* Increase the size of the section. */
3770 section
->size
+= size
;
3772 /* Make sure the section is allocated in memory, and make sure that
3773 it is no longer a common section. */
3774 section
->flags
|= SEC_ALLOC
;
3775 section
->flags
&= ~SEC_IS_COMMON
;
3777 if (config
.map_file
!= NULL
)
3779 static bfd_boolean header_printed
;
3784 if (! header_printed
)
3786 minfo (_("\nAllocating common symbols\n"));
3787 minfo (_("Common symbol size file\n\n"));
3788 header_printed
= TRUE
;
3791 name
= demangle (h
->root
.string
);
3793 len
= strlen (name
);
3808 if (size
<= 0xffffffff)
3809 sprintf (buf
, "%lx", (unsigned long) size
);
3811 sprintf_vma (buf
, size
);
3821 minfo ("%B\n", section
->owner
);
3827 /* Run through the input files and ensure that every input section has
3828 somewhere to go. If one is found without a destination then create
3829 an input request and place it into the statement tree. */
3832 lang_place_orphans (void)
3834 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3838 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3840 if (s
->output_section
== NULL
)
3842 /* This section of the file is not attached, root
3843 around for a sensible place for it to go. */
3845 if (file
->just_syms_flag
)
3848 if ((s
->flags
& SEC_EXCLUDE
) != 0)
3849 s
->output_section
= bfd_abs_section_ptr
;
3850 else if (strcmp (s
->name
, "COMMON") == 0)
3852 /* This is a lonely common section which must have
3853 come from an archive. We attach to the section
3854 with the wildcard. */
3855 if (! link_info
.relocatable
3856 || command_line
.force_common_definition
)
3858 if (default_common_section
== NULL
)
3861 /* This message happens when using the
3862 svr3.ifile linker script, so I have
3864 info_msg (_("%P: no [COMMON] command,"
3865 " defaulting to .bss\n"));
3867 default_common_section
=
3868 lang_output_section_statement_lookup (".bss");
3871 lang_add_section (&default_common_section
->children
, s
,
3872 default_common_section
, file
);
3875 else if (ldemul_place_orphan (file
, s
))
3879 lang_output_section_statement_type
*os
;
3881 os
= lang_output_section_statement_lookup (s
->name
);
3882 lang_add_section (&os
->children
, s
, os
, file
);
3890 lang_set_flags (lang_memory_region_type
*ptr
, const char *flags
, int invert
)
3892 flagword
*ptr_flags
;
3894 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3900 *ptr_flags
|= SEC_ALLOC
;
3904 *ptr_flags
|= SEC_READONLY
;
3908 *ptr_flags
|= SEC_DATA
;
3912 *ptr_flags
|= SEC_CODE
;
3917 *ptr_flags
|= SEC_LOAD
;
3921 einfo (_("%P%F: invalid syntax in flags\n"));
3928 /* Call a function on each input file. This function will be called
3929 on an archive, but not on the elements. */
3932 lang_for_each_input_file (void (*func
) (lang_input_statement_type
*))
3934 lang_input_statement_type
*f
;
3936 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3938 f
= (lang_input_statement_type
*) f
->next_real_file
)
3942 /* Call a function on each file. The function will be called on all
3943 the elements of an archive which are included in the link, but will
3944 not be called on the archive file itself. */
3947 lang_for_each_file (void (*func
) (lang_input_statement_type
*))
3949 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3956 ldlang_add_file (lang_input_statement_type
*entry
)
3960 lang_statement_append (&file_chain
,
3961 (lang_statement_union_type
*) entry
,
3964 /* The BFD linker needs to have a list of all input BFDs involved in
3966 ASSERT (entry
->the_bfd
->link_next
== NULL
);
3967 ASSERT (entry
->the_bfd
!= output_bfd
);
3968 for (pp
= &link_info
.input_bfds
; *pp
!= NULL
; pp
= &(*pp
)->link_next
)
3970 *pp
= entry
->the_bfd
;
3971 entry
->the_bfd
->usrdata
= entry
;
3972 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3974 /* Look through the sections and check for any which should not be
3975 included in the link. We need to do this now, so that we can
3976 notice when the backend linker tries to report multiple
3977 definition errors for symbols which are in sections we aren't
3978 going to link. FIXME: It might be better to entirely ignore
3979 symbols which are defined in sections which are going to be
3980 discarded. This would require modifying the backend linker for
3981 each backend which might set the SEC_LINK_ONCE flag. If we do
3982 this, we should probably handle SEC_EXCLUDE in the same way. */
3984 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, entry
);
3988 lang_add_output (const char *name
, int from_script
)
3990 /* Make -o on command line override OUTPUT in script. */
3991 if (!had_output_filename
|| !from_script
)
3993 output_filename
= name
;
3994 had_output_filename
= TRUE
;
3998 static lang_output_section_statement_type
*current_section
;
4009 for (l
= 0; l
< 32; l
++)
4011 if (i
>= (unsigned int) x
)
4019 lang_output_section_statement_type
*
4020 lang_enter_output_section_statement (const char *output_section_statement_name
,
4021 etree_type
*address_exp
,
4022 enum section_type sectype
,
4024 etree_type
*subalign
,
4028 lang_output_section_statement_type
*os
;
4032 lang_output_section_statement_lookup_1 (output_section_statement_name
,
4035 /* Add this statement to tree. */
4037 add_statement (lang_output_section_statement_enum
,
4038 output_section_statement
);
4040 /* Make next things chain into subchain of this. */
4042 if (os
->addr_tree
== NULL
)
4044 os
->addr_tree
= address_exp
;
4046 os
->sectype
= sectype
;
4047 if (sectype
!= noload_section
)
4048 os
->flags
= SEC_NO_FLAGS
;
4050 os
->flags
= SEC_NEVER_LOAD
;
4051 os
->block_value
= 1;
4052 stat_ptr
= &os
->children
;
4054 os
->subsection_alignment
=
4055 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4056 os
->section_alignment
=
4057 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4059 os
->load_base
= ebase
;
4066 lang_output_statement_type
*new =
4067 new_stat (lang_output_statement
, stat_ptr
);
4069 new->name
= output_filename
;
4072 /* Reset the current counters in the regions. */
4075 lang_reset_memory_regions (void)
4077 lang_memory_region_type
*p
= lang_memory_region_list
;
4080 for (p
= lang_memory_region_list
; p
!= NULL
; p
= p
->next
)
4082 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4083 p
->current
= p
->origin
;
4086 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4088 /* Save the last size for possible use by bfd_relax_section. */
4089 o
->rawsize
= o
->size
;
4094 /* Worker for lang_gc_sections_1. */
4097 gc_section_callback (lang_wild_statement_type
*ptr
,
4098 struct wildcard_list
*sec ATTRIBUTE_UNUSED
,
4100 lang_input_statement_type
*file ATTRIBUTE_UNUSED
,
4101 void *data ATTRIBUTE_UNUSED
)
4103 /* If the wild pattern was marked KEEP, the member sections
4104 should be as well. */
4105 if (ptr
->keep_sections
)
4106 section
->flags
|= SEC_KEEP
;
4109 /* Iterate over sections marking them against GC. */
4112 lang_gc_sections_1 (lang_statement_union_type
*s
)
4114 for (; s
!= NULL
; s
= s
->header
.next
)
4116 switch (s
->header
.type
)
4118 case lang_wild_statement_enum
:
4119 walk_wild (&s
->wild_statement
, gc_section_callback
, NULL
);
4121 case lang_constructors_statement_enum
:
4122 lang_gc_sections_1 (constructor_list
.head
);
4124 case lang_output_section_statement_enum
:
4125 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4127 case lang_group_statement_enum
:
4128 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4137 lang_gc_sections (void)
4139 struct bfd_link_hash_entry
*h
;
4140 ldlang_undef_chain_list_type
*ulist
;
4142 /* Keep all sections so marked in the link script. */
4144 lang_gc_sections_1 (statement_list
.head
);
4146 /* Keep all sections containing symbols undefined on the command-line,
4147 and the section containing the entry symbol. */
4149 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4151 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4152 FALSE
, FALSE
, FALSE
);
4155 && (h
->type
== bfd_link_hash_defined
4156 || h
->type
== bfd_link_hash_defweak
)
4157 && ! bfd_is_abs_section (h
->u
.def
.section
))
4159 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4163 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4164 the special case of debug info. (See bfd/stabs.c)
4165 Twiddle the flag here, to simplify later linker code. */
4166 if (link_info
.relocatable
)
4168 LANG_FOR_EACH_INPUT_STATEMENT (f
)
4171 for (sec
= f
->the_bfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4172 if ((sec
->flags
& SEC_DEBUGGING
) == 0)
4173 sec
->flags
&= ~SEC_EXCLUDE
;
4177 if (command_line
.gc_sections
)
4178 bfd_gc_sections (output_bfd
, &link_info
);
4184 lang_reasonable_defaults ();
4185 current_target
= default_target
;
4187 /* Open the output file. */
4188 lang_for_each_statement (ldlang_open_output
);
4191 ldemul_create_output_section_statements ();
4193 /* Add to the hash table all undefineds on the command line. */
4194 lang_place_undefineds ();
4196 if (!bfd_section_already_linked_table_init ())
4197 einfo (_("%P%F: Failed to create hash table\n"));
4199 /* Create a bfd for each input file. */
4200 current_target
= default_target
;
4201 open_input_bfds (statement_list
.head
, FALSE
);
4203 link_info
.gc_sym_list
= &entry_symbol
;
4204 if (entry_symbol
.name
== NULL
)
4205 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4207 ldemul_after_open ();
4209 bfd_section_already_linked_table_free ();
4211 /* Make sure that we're not mixing architectures. We call this
4212 after all the input files have been opened, but before we do any
4213 other processing, so that any operations merge_private_bfd_data
4214 does on the output file will be known during the rest of the
4218 /* Handle .exports instead of a version script if we're told to do so. */
4219 if (command_line
.version_exports_section
)
4220 lang_do_version_exports_section ();
4222 /* Build all sets based on the information gathered from the input
4224 ldctor_build_sets ();
4226 /* Remove unreferenced sections if asked to. */
4227 lang_gc_sections ();
4229 /* Size up the common data. */
4232 /* Run through the contours of the script and attach input sections
4233 to the correct output sections. */
4234 map_input_to_output_sections (statement_list
.head
, NULL
, NULL
);
4236 /* Find any sections not attached explicitly and handle them. */
4237 lang_place_orphans ();
4239 if (! link_info
.relocatable
)
4243 /* Merge SEC_MERGE sections. This has to be done after GC of
4244 sections, so that GCed sections are not merged, but before
4245 assigning dynamic symbols, since removing whole input sections
4247 bfd_merge_sections (output_bfd
, &link_info
);
4249 /* Look for a text section and set the readonly attribute in it. */
4250 found
= bfd_get_section_by_name (output_bfd
, ".text");
4254 if (config
.text_read_only
)
4255 found
->flags
|= SEC_READONLY
;
4257 found
->flags
&= ~SEC_READONLY
;
4261 /* Do anything special before sizing sections. This is where ELF
4262 and other back-ends size dynamic sections. */
4263 ldemul_before_allocation ();
4265 if (!link_info
.relocatable
)
4266 strip_excluded_output_sections ();
4268 /* We must record the program headers before we try to fix the
4269 section positions, since they will affect SIZEOF_HEADERS. */
4270 lang_record_phdrs ();
4272 /* Size up the sections. */
4273 lang_size_sections (statement_list
.head
, abs_output_section
,
4274 &statement_list
.head
, 0, 0, NULL
,
4275 command_line
.relax
? FALSE
: TRUE
);
4277 /* Now run around and relax if we can. */
4278 if (command_line
.relax
)
4280 /* Keep relaxing until bfd_relax_section gives up. */
4281 bfd_boolean relax_again
;
4285 relax_again
= FALSE
;
4287 /* Note: pe-dll.c does something like this also. If you find
4288 you need to change this code, you probably need to change
4289 pe-dll.c also. DJ */
4291 /* Do all the assignments with our current guesses as to
4293 lang_do_assignments (statement_list
.head
, abs_output_section
,
4296 /* We must do this after lang_do_assignments, because it uses
4298 lang_reset_memory_regions ();
4300 /* Perform another relax pass - this time we know where the
4301 globals are, so can make a better guess. */
4302 lang_size_sections (statement_list
.head
, abs_output_section
,
4303 &statement_list
.head
, 0, 0, &relax_again
, FALSE
);
4305 /* If the normal relax is done and the relax finalize pass
4306 is not performed yet, we perform another relax pass. */
4307 if (!relax_again
&& link_info
.need_relax_finalize
)
4309 link_info
.need_relax_finalize
= FALSE
;
4313 while (relax_again
);
4315 /* Final extra sizing to report errors. */
4316 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4317 lang_reset_memory_regions ();
4318 lang_size_sections (statement_list
.head
, abs_output_section
,
4319 &statement_list
.head
, 0, 0, NULL
, TRUE
);
4322 /* See if anything special should be done now we know how big
4324 ldemul_after_allocation ();
4326 /* Fix any .startof. or .sizeof. symbols. */
4327 lang_set_startof ();
4329 /* Do all the assignments, now that we know the final resting places
4330 of all the symbols. */
4332 lang_do_assignments (statement_list
.head
, abs_output_section
, NULL
, 0);
4334 /* Make sure that the section addresses make sense. */
4335 if (! link_info
.relocatable
4336 && command_line
.check_section_addresses
)
4337 lang_check_section_addresses ();
4345 /* EXPORTED TO YACC */
4348 lang_add_wild (struct wildcard_spec
*filespec
,
4349 struct wildcard_list
*section_list
,
4350 bfd_boolean keep_sections
)
4352 struct wildcard_list
*curr
, *next
;
4353 lang_wild_statement_type
*new;
4355 /* Reverse the list as the parser puts it back to front. */
4356 for (curr
= section_list
, section_list
= NULL
;
4358 section_list
= curr
, curr
= next
)
4360 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4361 placed_commons
= TRUE
;
4364 curr
->next
= section_list
;
4367 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4369 if (strcmp (filespec
->name
, "*") == 0)
4370 filespec
->name
= NULL
;
4371 else if (! wildcardp (filespec
->name
))
4372 lang_has_input_file
= TRUE
;
4375 new = new_stat (lang_wild_statement
, stat_ptr
);
4376 new->filename
= NULL
;
4377 new->filenames_sorted
= FALSE
;
4378 if (filespec
!= NULL
)
4380 new->filename
= filespec
->name
;
4381 new->filenames_sorted
= filespec
->sorted
;
4383 new->section_list
= section_list
;
4384 new->keep_sections
= keep_sections
;
4385 lang_list_init (&new->children
);
4389 lang_section_start (const char *name
, etree_type
*address
)
4391 lang_address_statement_type
*ad
;
4393 ad
= new_stat (lang_address_statement
, stat_ptr
);
4394 ad
->section_name
= name
;
4395 ad
->address
= address
;
4398 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4399 because of a -e argument on the command line, or zero if this is
4400 called by ENTRY in a linker script. Command line arguments take
4404 lang_add_entry (const char *name
, bfd_boolean cmdline
)
4406 if (entry_symbol
.name
== NULL
4408 || ! entry_from_cmdline
)
4410 entry_symbol
.name
= name
;
4411 entry_from_cmdline
= cmdline
;
4416 lang_add_target (const char *name
)
4418 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4426 lang_add_map (const char *name
)
4433 map_option_f
= TRUE
;
4441 lang_add_fill (fill_type
*fill
)
4443 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4450 lang_add_data (int type
, union etree_union
*exp
)
4453 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4461 /* Create a new reloc statement. RELOC is the BFD relocation type to
4462 generate. HOWTO is the corresponding howto structure (we could
4463 look this up, but the caller has already done so). SECTION is the
4464 section to generate a reloc against, or NAME is the name of the
4465 symbol to generate a reloc against. Exactly one of SECTION and
4466 NAME must be NULL. ADDEND is an expression for the addend. */
4469 lang_add_reloc (bfd_reloc_code_real_type reloc
,
4470 reloc_howto_type
*howto
,
4473 union etree_union
*addend
)
4475 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4479 p
->section
= section
;
4481 p
->addend_exp
= addend
;
4483 p
->addend_value
= 0;
4484 p
->output_section
= NULL
;
4488 lang_assignment_statement_type
*
4489 lang_add_assignment (etree_type
*exp
)
4491 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4499 lang_add_attribute (enum statement_enum attribute
)
4501 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4505 lang_startup (const char *name
)
4507 if (startup_file
!= NULL
)
4509 einfo (_("%P%Fmultiple STARTUP files\n"));
4511 first_file
->filename
= name
;
4512 first_file
->local_sym_name
= name
;
4513 first_file
->real
= TRUE
;
4515 startup_file
= name
;
4519 lang_float (bfd_boolean maybe
)
4521 lang_float_flag
= maybe
;
4525 /* Work out the load- and run-time regions from a script statement, and
4526 store them in *LMA_REGION and *REGION respectively.
4528 MEMSPEC is the name of the run-time region, or the value of
4529 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4530 LMA_MEMSPEC is the name of the load-time region, or null if the
4531 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4532 had an explicit load address.
4534 It is an error to specify both a load region and a load address. */
4537 lang_get_regions (lang_memory_region_type
**region
,
4538 lang_memory_region_type
**lma_region
,
4539 const char *memspec
,
4540 const char *lma_memspec
,
4541 bfd_boolean have_lma
,
4542 bfd_boolean have_vma
)
4544 *lma_region
= lang_memory_region_lookup (lma_memspec
, FALSE
);
4546 /* If no runtime region or VMA has been specified, but the load region
4547 has been specified, then use the load region for the runtime region
4549 if (lma_memspec
!= NULL
4551 && strcmp (memspec
, DEFAULT_MEMORY_REGION
) == 0)
4552 *region
= *lma_region
;
4554 *region
= lang_memory_region_lookup (memspec
, FALSE
);
4556 if (have_lma
&& lma_memspec
!= 0)
4557 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4561 lang_leave_output_section_statement (fill_type
*fill
, const char *memspec
,
4562 lang_output_section_phdr_list
*phdrs
,
4563 const char *lma_memspec
)
4565 lang_get_regions (¤t_section
->region
,
4566 ¤t_section
->lma_region
,
4567 memspec
, lma_memspec
,
4568 current_section
->load_base
!= NULL
,
4569 current_section
->addr_tree
!= NULL
);
4570 current_section
->fill
= fill
;
4571 current_section
->phdrs
= phdrs
;
4572 stat_ptr
= &statement_list
;
4575 /* Create an absolute symbol with the given name with the value of the
4576 address of first byte of the section named.
4578 If the symbol already exists, then do nothing. */
4581 lang_abs_symbol_at_beginning_of (const char *secname
, const char *name
)
4583 struct bfd_link_hash_entry
*h
;
4585 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4587 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4589 if (h
->type
== bfd_link_hash_new
4590 || h
->type
== bfd_link_hash_undefined
)
4594 h
->type
= bfd_link_hash_defined
;
4596 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4600 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4602 h
->u
.def
.section
= bfd_abs_section_ptr
;
4606 /* Create an absolute symbol with the given name with the value of the
4607 address of the first byte after the end of the section named.
4609 If the symbol already exists, then do nothing. */
4612 lang_abs_symbol_at_end_of (const char *secname
, const char *name
)
4614 struct bfd_link_hash_entry
*h
;
4616 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4618 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4620 if (h
->type
== bfd_link_hash_new
4621 || h
->type
== bfd_link_hash_undefined
)
4625 h
->type
= bfd_link_hash_defined
;
4627 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4631 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4632 + TO_ADDR (sec
->size
));
4634 h
->u
.def
.section
= bfd_abs_section_ptr
;
4639 lang_statement_append (lang_statement_list_type
*list
,
4640 lang_statement_union_type
*element
,
4641 lang_statement_union_type
**field
)
4643 *(list
->tail
) = element
;
4647 /* Set the output format type. -oformat overrides scripts. */
4650 lang_add_output_format (const char *format
,
4655 if (output_target
== NULL
|| !from_script
)
4657 if (command_line
.endian
== ENDIAN_BIG
4660 else if (command_line
.endian
== ENDIAN_LITTLE
4664 output_target
= format
;
4668 /* Enter a group. This creates a new lang_group_statement, and sets
4669 stat_ptr to build new statements within the group. */
4672 lang_enter_group (void)
4674 lang_group_statement_type
*g
;
4676 g
= new_stat (lang_group_statement
, stat_ptr
);
4677 lang_list_init (&g
->children
);
4678 stat_ptr
= &g
->children
;
4681 /* Leave a group. This just resets stat_ptr to start writing to the
4682 regular list of statements again. Note that this will not work if
4683 groups can occur inside anything else which can adjust stat_ptr,
4684 but currently they can't. */
4687 lang_leave_group (void)
4689 stat_ptr
= &statement_list
;
4692 /* Add a new program header. This is called for each entry in a PHDRS
4693 command in a linker script. */
4696 lang_new_phdr (const char *name
,
4698 bfd_boolean filehdr
,
4703 struct lang_phdr
*n
, **pp
;
4705 n
= stat_alloc (sizeof (struct lang_phdr
));
4708 n
->type
= exp_get_value_int (type
, 0, "program header type",
4709 lang_final_phase_enum
);
4710 n
->filehdr
= filehdr
;
4715 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4720 /* Record the program header information in the output BFD. FIXME: We
4721 should not be calling an ELF specific function here. */
4724 lang_record_phdrs (void)
4728 lang_output_section_phdr_list
*last
;
4729 struct lang_phdr
*l
;
4730 lang_statement_union_type
*u
;
4733 secs
= xmalloc (alc
* sizeof (asection
*));
4735 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4742 for (u
= lang_output_section_statement
.head
;
4744 u
= u
->output_section_statement
.next
)
4746 lang_output_section_statement_type
*os
;
4747 lang_output_section_phdr_list
*pl
;
4749 os
= &u
->output_section_statement
;
4750 if (os
->constraint
== -1)
4758 if (os
->sectype
== noload_section
4759 || os
->bfd_section
== NULL
4760 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4765 if (os
->bfd_section
== NULL
)
4768 for (; pl
!= NULL
; pl
= pl
->next
)
4770 if (strcmp (pl
->name
, l
->name
) == 0)
4775 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
4777 secs
[c
] = os
->bfd_section
;
4784 if (l
->flags
== NULL
)
4787 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4788 lang_final_phase_enum
);
4793 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4794 lang_final_phase_enum
);
4796 if (! bfd_record_phdr (output_bfd
, l
->type
,
4797 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4798 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4799 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4804 /* Make sure all the phdr assignments succeeded. */
4805 for (u
= lang_output_section_statement
.head
;
4807 u
= u
->output_section_statement
.next
)
4809 lang_output_section_phdr_list
*pl
;
4811 if (u
->output_section_statement
.constraint
== -1
4812 || u
->output_section_statement
.bfd_section
== NULL
)
4815 for (pl
= u
->output_section_statement
.phdrs
;
4818 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4819 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4820 u
->output_section_statement
.name
, pl
->name
);
4824 /* Record a list of sections which may not be cross referenced. */
4827 lang_add_nocrossref (lang_nocrossref_type
*l
)
4829 struct lang_nocrossrefs
*n
;
4831 n
= xmalloc (sizeof *n
);
4832 n
->next
= nocrossref_list
;
4834 nocrossref_list
= n
;
4836 /* Set notice_all so that we get informed about all symbols. */
4837 link_info
.notice_all
= TRUE
;
4840 /* Overlay handling. We handle overlays with some static variables. */
4842 /* The overlay virtual address. */
4843 static etree_type
*overlay_vma
;
4844 /* And subsection alignment. */
4845 static etree_type
*overlay_subalign
;
4847 /* An expression for the maximum section size seen so far. */
4848 static etree_type
*overlay_max
;
4850 /* A list of all the sections in this overlay. */
4852 struct overlay_list
{
4853 struct overlay_list
*next
;
4854 lang_output_section_statement_type
*os
;
4857 static struct overlay_list
*overlay_list
;
4859 /* Start handling an overlay. */
4862 lang_enter_overlay (etree_type
*vma_expr
, etree_type
*subalign
)
4864 /* The grammar should prevent nested overlays from occurring. */
4865 ASSERT (overlay_vma
== NULL
4866 && overlay_subalign
== NULL
4867 && overlay_max
== NULL
);
4869 overlay_vma
= vma_expr
;
4870 overlay_subalign
= subalign
;
4873 /* Start a section in an overlay. We handle this by calling
4874 lang_enter_output_section_statement with the correct VMA.
4875 lang_leave_overlay sets up the LMA and memory regions. */
4878 lang_enter_overlay_section (const char *name
)
4880 struct overlay_list
*n
;
4883 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4884 0, overlay_subalign
, 0, 0);
4886 /* If this is the first section, then base the VMA of future
4887 sections on this one. This will work correctly even if `.' is
4888 used in the addresses. */
4889 if (overlay_list
== NULL
)
4890 overlay_vma
= exp_nameop (ADDR
, name
);
4892 /* Remember the section. */
4893 n
= xmalloc (sizeof *n
);
4894 n
->os
= current_section
;
4895 n
->next
= overlay_list
;
4898 size
= exp_nameop (SIZEOF
, name
);
4900 /* Arrange to work out the maximum section end address. */
4901 if (overlay_max
== NULL
)
4904 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4907 /* Finish a section in an overlay. There isn't any special to do
4911 lang_leave_overlay_section (fill_type
*fill
,
4912 lang_output_section_phdr_list
*phdrs
)
4919 name
= current_section
->name
;
4921 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4922 region and that no load-time region has been specified. It doesn't
4923 really matter what we say here, since lang_leave_overlay will
4925 lang_leave_output_section_statement (fill
, DEFAULT_MEMORY_REGION
, phdrs
, 0);
4927 /* Define the magic symbols. */
4929 clean
= xmalloc (strlen (name
) + 1);
4931 for (s1
= name
; *s1
!= '\0'; s1
++)
4932 if (ISALNUM (*s1
) || *s1
== '_')
4936 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4937 sprintf (buf
, "__load_start_%s", clean
);
4938 lang_add_assignment (exp_assop ('=', buf
,
4939 exp_nameop (LOADADDR
, name
)));
4941 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4942 sprintf (buf
, "__load_stop_%s", clean
);
4943 lang_add_assignment (exp_assop ('=', buf
,
4945 exp_nameop (LOADADDR
, name
),
4946 exp_nameop (SIZEOF
, name
))));
4951 /* Finish an overlay. If there are any overlay wide settings, this
4952 looks through all the sections in the overlay and sets them. */
4955 lang_leave_overlay (etree_type
*lma_expr
,
4958 const char *memspec
,
4959 lang_output_section_phdr_list
*phdrs
,
4960 const char *lma_memspec
)
4962 lang_memory_region_type
*region
;
4963 lang_memory_region_type
*lma_region
;
4964 struct overlay_list
*l
;
4965 lang_nocrossref_type
*nocrossref
;
4967 lang_get_regions (®ion
, &lma_region
,
4968 memspec
, lma_memspec
,
4969 lma_expr
!= NULL
, FALSE
);
4973 /* After setting the size of the last section, set '.' to end of the
4975 if (overlay_list
!= NULL
)
4976 overlay_list
->os
->update_dot_tree
4977 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
4982 struct overlay_list
*next
;
4984 if (fill
!= NULL
&& l
->os
->fill
== NULL
)
4987 l
->os
->region
= region
;
4988 l
->os
->lma_region
= lma_region
;
4990 /* The first section has the load address specified in the
4991 OVERLAY statement. The rest are worked out from that.
4992 The base address is not needed (and should be null) if
4993 an LMA region was specified. */
4995 l
->os
->load_base
= lma_expr
;
4996 else if (lma_region
== 0)
4997 l
->os
->load_base
= exp_binop ('+',
4998 exp_nameop (LOADADDR
, l
->next
->os
->name
),
4999 exp_nameop (SIZEOF
, l
->next
->os
->name
));
5001 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
5002 l
->os
->phdrs
= phdrs
;
5006 lang_nocrossref_type
*nc
;
5008 nc
= xmalloc (sizeof *nc
);
5009 nc
->name
= l
->os
->name
;
5010 nc
->next
= nocrossref
;
5019 if (nocrossref
!= NULL
)
5020 lang_add_nocrossref (nocrossref
);
5023 overlay_list
= NULL
;
5027 /* Version handling. This is only useful for ELF. */
5029 /* This global variable holds the version tree that we build. */
5031 struct bfd_elf_version_tree
*lang_elf_version_info
;
5033 /* If PREV is NULL, return first version pattern matching particular symbol.
5034 If PREV is non-NULL, return first version pattern matching particular
5035 symbol after PREV (previously returned by lang_vers_match). */
5037 static struct bfd_elf_version_expr
*
5038 lang_vers_match (struct bfd_elf_version_expr_head
*head
,
5039 struct bfd_elf_version_expr
*prev
,
5042 const char *cxx_sym
= sym
;
5043 const char *java_sym
= sym
;
5044 struct bfd_elf_version_expr
*expr
= NULL
;
5046 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5048 cxx_sym
= cplus_demangle (sym
, DMGL_PARAMS
| DMGL_ANSI
);
5052 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5054 java_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5059 if (head
->htab
&& (prev
== NULL
|| prev
->symbol
))
5061 struct bfd_elf_version_expr e
;
5063 switch (prev
? prev
->mask
: 0)
5066 if (head
->mask
& BFD_ELF_VERSION_C_TYPE
)
5069 expr
= htab_find (head
->htab
, &e
);
5070 while (expr
&& strcmp (expr
->symbol
, sym
) == 0)
5071 if (expr
->mask
== BFD_ELF_VERSION_C_TYPE
)
5077 case BFD_ELF_VERSION_C_TYPE
:
5078 if (head
->mask
& BFD_ELF_VERSION_CXX_TYPE
)
5081 expr
= htab_find (head
->htab
, &e
);
5082 while (expr
&& strcmp (expr
->symbol
, cxx_sym
) == 0)
5083 if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5089 case BFD_ELF_VERSION_CXX_TYPE
:
5090 if (head
->mask
& BFD_ELF_VERSION_JAVA_TYPE
)
5092 e
.symbol
= java_sym
;
5093 expr
= htab_find (head
->htab
, &e
);
5094 while (expr
&& strcmp (expr
->symbol
, java_sym
) == 0)
5095 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5106 /* Finally, try the wildcards. */
5107 if (prev
== NULL
|| prev
->symbol
)
5108 expr
= head
->remaining
;
5115 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5118 if (expr
->mask
== BFD_ELF_VERSION_JAVA_TYPE
)
5120 else if (expr
->mask
== BFD_ELF_VERSION_CXX_TYPE
)
5124 if (fnmatch (expr
->pattern
, s
, 0) == 0)
5131 free ((char *) cxx_sym
);
5132 if (java_sym
!= sym
)
5133 free ((char *) java_sym
);
5137 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5138 return a string pointing to the symbol name. */
5141 realsymbol (const char *pattern
)
5144 bfd_boolean changed
= FALSE
, backslash
= FALSE
;
5145 char *s
, *symbol
= xmalloc (strlen (pattern
) + 1);
5147 for (p
= pattern
, s
= symbol
; *p
!= '\0'; ++p
)
5149 /* It is a glob pattern only if there is no preceding
5151 if (! backslash
&& (*p
== '?' || *p
== '*' || *p
== '['))
5159 /* Remove the preceding backslash. */
5166 backslash
= *p
== '\\';
5181 /* This is called for each variable name or match expression. */
5183 struct bfd_elf_version_expr
*
5184 lang_new_vers_pattern (struct bfd_elf_version_expr
*orig
,
5188 struct bfd_elf_version_expr
*ret
;
5190 ret
= xmalloc (sizeof *ret
);
5195 ret
->symbol
= realsymbol (new);
5197 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5198 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5199 else if (strcasecmp (lang
, "C++") == 0)
5200 ret
->mask
= BFD_ELF_VERSION_CXX_TYPE
;
5201 else if (strcasecmp (lang
, "Java") == 0)
5202 ret
->mask
= BFD_ELF_VERSION_JAVA_TYPE
;
5205 einfo (_("%X%P: unknown language `%s' in version information\n"),
5207 ret
->mask
= BFD_ELF_VERSION_C_TYPE
;
5210 return ldemul_new_vers_pattern (ret
);
5213 /* This is called for each set of variable names and match
5216 struct bfd_elf_version_tree
*
5217 lang_new_vers_node (struct bfd_elf_version_expr
*globals
,
5218 struct bfd_elf_version_expr
*locals
)
5220 struct bfd_elf_version_tree
*ret
;
5222 ret
= xcalloc (1, sizeof *ret
);
5223 ret
->globals
.list
= globals
;
5224 ret
->locals
.list
= locals
;
5225 ret
->match
= lang_vers_match
;
5226 ret
->name_indx
= (unsigned int) -1;
5230 /* This static variable keeps track of version indices. */
5232 static int version_index
;
5235 version_expr_head_hash (const void *p
)
5237 const struct bfd_elf_version_expr
*e
= p
;
5239 return htab_hash_string (e
->symbol
);
5243 version_expr_head_eq (const void *p1
, const void *p2
)
5245 const struct bfd_elf_version_expr
*e1
= p1
;
5246 const struct bfd_elf_version_expr
*e2
= p2
;
5248 return strcmp (e1
->symbol
, e2
->symbol
) == 0;
5252 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head
*head
)
5255 struct bfd_elf_version_expr
*e
, *next
;
5256 struct bfd_elf_version_expr
**list_loc
, **remaining_loc
;
5258 for (e
= head
->list
; e
; e
= e
->next
)
5262 head
->mask
|= e
->mask
;
5267 head
->htab
= htab_create (count
* 2, version_expr_head_hash
,
5268 version_expr_head_eq
, NULL
);
5269 list_loc
= &head
->list
;
5270 remaining_loc
= &head
->remaining
;
5271 for (e
= head
->list
; e
; e
= next
)
5277 remaining_loc
= &e
->next
;
5281 void **loc
= htab_find_slot (head
->htab
, e
, INSERT
);
5285 struct bfd_elf_version_expr
*e1
, *last
;
5291 if (e1
->mask
== e
->mask
)
5299 while (e1
&& strcmp (e1
->symbol
, e
->symbol
) == 0);
5303 /* This is a duplicate. */
5304 /* FIXME: Memory leak. Sometimes pattern is not
5305 xmalloced alone, but in larger chunk of memory. */
5306 /* free (e->symbol); */
5311 e
->next
= last
->next
;
5319 list_loc
= &e
->next
;
5323 *remaining_loc
= NULL
;
5324 *list_loc
= head
->remaining
;
5327 head
->remaining
= head
->list
;
5330 /* This is called when we know the name and dependencies of the
5334 lang_register_vers_node (const char *name
,
5335 struct bfd_elf_version_tree
*version
,
5336 struct bfd_elf_version_deps
*deps
)
5338 struct bfd_elf_version_tree
*t
, **pp
;
5339 struct bfd_elf_version_expr
*e1
;
5344 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5345 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5347 einfo (_("%X%P: anonymous version tag cannot be combined"
5348 " with other version tags\n"));
5353 /* Make sure this node has a unique name. */
5354 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5355 if (strcmp (t
->name
, name
) == 0)
5356 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5358 lang_finalize_version_expr_head (&version
->globals
);
5359 lang_finalize_version_expr_head (&version
->locals
);
5361 /* Check the global and local match names, and make sure there
5362 aren't any duplicates. */
5364 for (e1
= version
->globals
.list
; e1
!= NULL
; e1
= e1
->next
)
5366 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5368 struct bfd_elf_version_expr
*e2
;
5370 if (t
->locals
.htab
&& e1
->symbol
)
5372 e2
= htab_find (t
->locals
.htab
, e1
);
5373 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5375 if (e1
->mask
== e2
->mask
)
5376 einfo (_("%X%P: duplicate expression `%s'"
5377 " in version information\n"), e1
->symbol
);
5381 else if (!e1
->symbol
)
5382 for (e2
= t
->locals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5383 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
5384 && e1
->mask
== e2
->mask
)
5385 einfo (_("%X%P: duplicate expression `%s'"
5386 " in version information\n"), e1
->pattern
);
5390 for (e1
= version
->locals
.list
; e1
!= NULL
; e1
= e1
->next
)
5392 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5394 struct bfd_elf_version_expr
*e2
;
5396 if (t
->globals
.htab
&& e1
->symbol
)
5398 e2
= htab_find (t
->globals
.htab
, e1
);
5399 while (e2
&& strcmp (e1
->symbol
, e2
->symbol
) == 0)
5401 if (e1
->mask
== e2
->mask
)
5402 einfo (_("%X%P: duplicate expression `%s'"
5403 " in version information\n"),
5408 else if (!e1
->symbol
)
5409 for (e2
= t
->globals
.remaining
; e2
!= NULL
; e2
= e2
->next
)
5410 if (strcmp (e1
->pattern
, e2
->pattern
) == 0
5411 && e1
->mask
== e2
->mask
)
5412 einfo (_("%X%P: duplicate expression `%s'"
5413 " in version information\n"), e1
->pattern
);
5417 version
->deps
= deps
;
5418 version
->name
= name
;
5419 if (name
[0] != '\0')
5422 version
->vernum
= version_index
;
5425 version
->vernum
= 0;
5427 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5432 /* This is called when we see a version dependency. */
5434 struct bfd_elf_version_deps
*
5435 lang_add_vers_depend (struct bfd_elf_version_deps
*list
, const char *name
)
5437 struct bfd_elf_version_deps
*ret
;
5438 struct bfd_elf_version_tree
*t
;
5440 ret
= xmalloc (sizeof *ret
);
5443 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5445 if (strcmp (t
->name
, name
) == 0)
5447 ret
->version_needed
= t
;
5452 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5458 lang_do_version_exports_section (void)
5460 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5462 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5464 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5472 contents
= xmalloc (len
);
5473 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5474 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5477 while (p
< contents
+ len
)
5479 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5480 p
= strchr (p
, '\0') + 1;
5483 /* Do not free the contents, as we used them creating the regex. */
5485 /* Do not include this section in the link. */
5486 sec
->flags
|= SEC_EXCLUDE
;
5489 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5490 lang_register_vers_node (command_line
.version_exports_section
,
5491 lang_new_vers_node (greg
, lreg
), NULL
);
5495 lang_add_unique (const char *name
)
5497 struct unique_sections
*ent
;
5499 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5500 if (strcmp (ent
->name
, name
) == 0)
5503 ent
= xmalloc (sizeof *ent
);
5504 ent
->name
= xstrdup (name
);
5505 ent
->next
= unique_section_list
;
5506 unique_section_list
= ent
;