1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "libiberty.h"
44 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
46 lang_statement_list_type
*));
49 static struct obstack stat_obstack
;
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static const char *startup_file
;
54 static lang_statement_list_type input_file_chain
;
55 static boolean placed_commons
= false;
56 static lang_output_section_statement_type
*default_common_section
;
57 static boolean map_option_f
;
58 static bfd_vma print_dot
;
59 static lang_input_statement_type
*first_file
;
60 static const char *current_target
;
61 static const char *output_target
;
62 static lang_statement_list_type statement_list
;
63 static struct lang_phdr
*lang_phdr_list
;
65 static void lang_for_each_statement_worker
66 PARAMS ((void (*) (lang_statement_union_type
*),
67 lang_statement_union_type
*));
68 static lang_input_statement_type
*new_afile
69 PARAMS ((const char *, lang_input_file_enum_type
, const char *, boolean
));
70 static lang_memory_region_type
*lang_memory_default
PARAMS ((asection
*));
71 static void lang_map_flags
PARAMS ((flagword
));
72 static void init_os
PARAMS ((lang_output_section_statement_type
*));
73 static void exp_init_os
PARAMS ((etree_type
*));
74 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
75 static struct bfd_hash_entry
*already_linked_newfunc
76 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
77 static void already_linked_table_init
PARAMS ((void));
78 static void already_linked_table_free
PARAMS ((void));
79 static boolean wildcardp
PARAMS ((const char *));
80 static lang_statement_union_type
*wild_sort
81 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
83 static void output_section_callback
84 PARAMS ((lang_wild_statement_type
*, asection
*,
85 lang_input_statement_type
*, PTR
));
86 static lang_input_statement_type
*lookup_name
PARAMS ((const char *));
87 static void load_symbols
88 PARAMS ((lang_input_statement_type
*, lang_statement_list_type
*));
90 PARAMS ((lang_wild_statement_type
*, const char *, const char *,
91 const char *, lang_output_section_statement_type
*));
92 static bfd
*open_output
PARAMS ((const char *));
93 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*));
94 static void open_input_bfds
PARAMS ((lang_statement_union_type
*, boolean
));
95 static void lang_reasonable_defaults
PARAMS ((void));
96 static void lang_place_undefineds
PARAMS ((void));
97 static void map_input_to_output_sections
98 PARAMS ((lang_statement_union_type
*, const char *,
99 lang_output_section_statement_type
*));
100 static void print_output_section_statement
101 PARAMS ((lang_output_section_statement_type
*));
102 static void print_assignment
103 PARAMS ((lang_assignment_statement_type
*,
104 lang_output_section_statement_type
*));
105 static void print_input_statement
PARAMS ((lang_input_statement_type
*));
106 static boolean print_one_symbol
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
107 static void print_input_section
PARAMS ((lang_input_section_type
*));
108 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*));
109 static void print_data_statement
PARAMS ((lang_data_statement_type
*));
110 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
111 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*));
112 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*));
113 static void print_wild_statement
114 PARAMS ((lang_wild_statement_type
*, lang_output_section_statement_type
*));
115 static void print_group
116 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
117 static void print_statement
118 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
119 static void print_statement_list
120 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
121 static void print_statements
PARAMS ((void));
122 static bfd_vma insert_pad
123 PARAMS ((lang_statement_union_type
**this_ptr
, fill_type fill
,
124 unsigned int power
, asection
*output_section_statement
,
126 static bfd_vma size_input_section
127 PARAMS ((lang_statement_union_type
**this_ptr
,
128 lang_output_section_statement_type
*output_section_statement
,
129 fill_type fill
, bfd_vma dot
, boolean relax
));
130 static void lang_finish
PARAMS ((void));
131 static void ignore_bfd_errors
PARAMS ((const char *, ...));
132 static void lang_check
PARAMS ((void));
133 static void lang_common
PARAMS ((void));
134 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
135 static void lang_place_orphans
PARAMS ((void));
136 static int topower
PARAMS ((int));
137 static void lang_set_startof
PARAMS ((void));
138 static void reset_memory_regions
PARAMS ((void));
139 static void gc_section_callback
140 PARAMS ((lang_wild_statement_type
*, asection
*,
141 lang_input_statement_type
*, PTR
));
142 static void lang_record_phdrs
PARAMS ((void));
143 static void lang_gc_wild
144 PARAMS ((lang_wild_statement_type
*, const char *, const char *));
145 static void lang_gc_sections_1
PARAMS ((lang_statement_union_type
*));
146 static void lang_gc_sections
PARAMS ((void));
147 static int lang_vers_match_lang_c
148 PARAMS ((struct bfd_elf_version_expr
*, const char *));
149 static int lang_vers_match_lang_cplusplus
150 PARAMS ((struct bfd_elf_version_expr
*, const char *));
151 static int lang_vers_match_lang_java
152 PARAMS ((struct bfd_elf_version_expr
*, const char *));
153 static void lang_do_version_exports_section
PARAMS ((void));
154 static void lang_check_section_addresses
PARAMS ((void));
155 static void os_region_check
156 PARAMS ((lang_output_section_statement_type
*,
157 struct memory_region_struct
*, etree_type
*, bfd_vma
));
159 typedef void (*callback_t
) PARAMS ((lang_wild_statement_type
*,
160 asection
*, lang_input_statement_type
*,
162 static void walk_wild
163 PARAMS ((lang_wild_statement_type
*, const char *, const char *,
165 static void walk_wild_section
166 PARAMS ((lang_wild_statement_type
*, const char *,
167 lang_input_statement_type
*, callback_t
, PTR
));
168 static void walk_wild_file
169 PARAMS ((lang_wild_statement_type
*, const char *,
170 lang_input_statement_type
*, callback_t
, PTR
));
172 static int get_target
PARAMS ((const bfd_target
*, PTR
));
173 static void stricpy
PARAMS ((char *, char *));
174 static void strcut
PARAMS ((char *, char *));
175 static int name_compare
PARAMS ((char *, char *));
176 static int closest_target_match
PARAMS ((const bfd_target
*, PTR
));
177 static char * get_first_input_target
PARAMS ((void));
180 lang_output_section_statement_type
*abs_output_section
;
181 lang_statement_list_type lang_output_section_statement
;
182 lang_statement_list_type
*stat_ptr
= &statement_list
;
183 lang_statement_list_type file_chain
= { NULL
, NULL
};
184 const char *entry_symbol
= NULL
;
185 boolean entry_from_cmdline
;
186 boolean lang_has_input_file
= false;
187 boolean had_output_filename
= false;
188 boolean lang_float_flag
= false;
189 boolean delete_output_file_on_failure
= false;
190 struct lang_nocrossrefs
*nocrossref_list
;
192 etree_type
*base
; /* Relocation base - or null */
194 #if defined(__STDC__) || defined(ALMOST_STDC)
195 #define cat(a,b) a##b
197 #define cat(a,b) a/**/b
200 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
202 #define outside_section_address(q) ((q)->output_offset + (q)->output_section->vma)
204 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
206 #define SECTION_NAME_MAP_LENGTH (16)
212 return obstack_alloc (&stat_obstack
, size
);
215 /* Generic traversal routines for finding matching sections. */
218 walk_wild_section (ptr
, section
, file
, callback
, data
)
219 lang_wild_statement_type
*ptr
;
221 lang_input_statement_type
*file
;
225 /* Don't process sections from files which were excluded. */
226 if (ptr
->exclude_filename_list
!= NULL
)
228 struct name_list
*list_tmp
;
229 for (list_tmp
= ptr
->exclude_filename_list
; list_tmp
; list_tmp
= list_tmp
->next
)
233 if (wildcardp (list_tmp
->name
))
234 match
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0 ? true : false;
236 match
= strcmp (list_tmp
->name
, file
->filename
) == 0 ? true : false;
243 if (file
->just_syms_flag
== false)
245 register asection
*s
;
251 wildcard
= wildcardp (section
);
253 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
263 name
= bfd_get_section_name (file
->the_bfd
, s
);
265 match
= fnmatch (section
, name
, 0) == 0 ? true : false;
267 match
= strcmp (section
, name
) == 0 ? true : false;
271 (*callback
) (ptr
, s
, file
, data
);
276 /* Handle a wild statement for a single file F. */
279 walk_wild_file (s
, section
, f
, callback
, data
)
280 lang_wild_statement_type
*s
;
282 lang_input_statement_type
*f
;
286 if (f
->the_bfd
== NULL
287 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
288 walk_wild_section (s
, section
, f
, callback
, data
);
293 /* This is an archive file. We must map each member of the
294 archive separately. */
295 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
296 while (member
!= NULL
)
298 /* When lookup_name is called, it will call the add_symbols
299 entry point for the archive. For each element of the
300 archive which is included, BFD will call ldlang_add_file,
301 which will set the usrdata field of the member to the
302 lang_input_statement. */
303 if (member
->usrdata
!= NULL
)
305 walk_wild_section (s
, section
,
306 (lang_input_statement_type
*) member
->usrdata
,
310 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
316 walk_wild (s
, section
, file
, callback
, data
)
317 lang_wild_statement_type
*s
;
323 if (file
== (char *) NULL
)
325 /* Perform the iteration over all files in the list. */
326 LANG_FOR_EACH_INPUT_STATEMENT (f
)
328 walk_wild_file (s
, section
, f
, callback
, data
);
331 else if (wildcardp (file
))
333 LANG_FOR_EACH_INPUT_STATEMENT (f
)
335 if (fnmatch (file
, f
->filename
, FNM_FILE_NAME
) == 0)
336 walk_wild_file (s
, section
, f
, callback
, data
);
341 lang_input_statement_type
*f
;
343 /* Perform the iteration over a single file. */
344 f
= lookup_name (file
);
345 walk_wild_file (s
, section
, f
, callback
, data
);
349 /* lang_for_each_statement walks the parse tree and calls the provided
350 function for each node. */
353 lang_for_each_statement_worker (func
, s
)
354 void (*func
) PARAMS ((lang_statement_union_type
*));
355 lang_statement_union_type
*s
;
357 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
361 switch (s
->header
.type
)
363 case lang_constructors_statement_enum
:
364 lang_for_each_statement_worker (func
, constructor_list
.head
);
366 case lang_output_section_statement_enum
:
367 lang_for_each_statement_worker
369 s
->output_section_statement
.children
.head
);
371 case lang_wild_statement_enum
:
372 lang_for_each_statement_worker
374 s
->wild_statement
.children
.head
);
376 case lang_group_statement_enum
:
377 lang_for_each_statement_worker (func
,
378 s
->group_statement
.children
.head
);
380 case lang_data_statement_enum
:
381 case lang_reloc_statement_enum
:
382 case lang_object_symbols_statement_enum
:
383 case lang_output_statement_enum
:
384 case lang_target_statement_enum
:
385 case lang_input_section_enum
:
386 case lang_input_statement_enum
:
387 case lang_assignment_statement_enum
:
388 case lang_padding_statement_enum
:
389 case lang_address_statement_enum
:
390 case lang_fill_statement_enum
:
400 lang_for_each_statement (func
)
401 void (*func
) PARAMS ((lang_statement_union_type
*));
403 lang_for_each_statement_worker (func
, statement_list
.head
);
406 /*----------------------------------------------------------------------*/
409 lang_list_init (list
)
410 lang_statement_list_type
*list
;
412 list
->head
= (lang_statement_union_type
*) NULL
;
413 list
->tail
= &list
->head
;
416 /* Build a new statement node for the parse tree. */
418 static lang_statement_union_type
*
419 new_statement (type
, size
, list
)
420 enum statement_enum type
;
422 lang_statement_list_type
*list
;
424 lang_statement_union_type
*new = (lang_statement_union_type
*)
427 new->header
.type
= type
;
428 new->header
.next
= (lang_statement_union_type
*) NULL
;
429 lang_statement_append (list
, new, &new->header
.next
);
433 /* Build a new input file node for the language. There are several
434 ways in which we treat an input file, eg, we only look at symbols,
435 or prefix it with a -l etc.
437 We can be supplied with requests for input files more than once;
438 they may, for example be split over serveral lines like foo.o(.text)
439 foo.o(.data) etc, so when asked for a file we check that we havn't
440 got it already so we don't duplicate the bfd. */
442 static lang_input_statement_type
*
443 new_afile (name
, file_type
, target
, add_to_list
)
445 lang_input_file_enum_type file_type
;
449 lang_input_statement_type
*p
;
452 p
= new_stat (lang_input_statement
, stat_ptr
);
455 p
= ((lang_input_statement_type
*)
456 stat_alloc (sizeof (lang_input_statement_type
)));
457 p
->header
.next
= NULL
;
460 lang_has_input_file
= true;
464 case lang_input_file_is_symbols_only_enum
:
466 p
->is_archive
= false;
468 p
->local_sym_name
= name
;
469 p
->just_syms_flag
= true;
470 p
->search_dirs_flag
= false;
472 case lang_input_file_is_fake_enum
:
474 p
->is_archive
= false;
476 p
->local_sym_name
= name
;
477 p
->just_syms_flag
= false;
478 p
->search_dirs_flag
= false;
480 case lang_input_file_is_l_enum
:
481 p
->is_archive
= true;
484 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
485 p
->just_syms_flag
= false;
486 p
->search_dirs_flag
= true;
488 case lang_input_file_is_marker_enum
:
490 p
->is_archive
= false;
492 p
->local_sym_name
= name
;
493 p
->just_syms_flag
= false;
494 p
->search_dirs_flag
= true;
496 case lang_input_file_is_search_file_enum
:
498 p
->is_archive
= false;
500 p
->local_sym_name
= name
;
501 p
->just_syms_flag
= false;
502 p
->search_dirs_flag
= true;
504 case lang_input_file_is_file_enum
:
506 p
->is_archive
= false;
508 p
->local_sym_name
= name
;
509 p
->just_syms_flag
= false;
510 p
->search_dirs_flag
= false;
515 p
->the_bfd
= (bfd
*) NULL
;
516 p
->asymbols
= (asymbol
**) NULL
;
517 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
518 p
->next
= (lang_statement_union_type
*) NULL
;
520 p
->dynamic
= config
.dynamic_link
;
521 p
->whole_archive
= whole_archive
;
523 lang_statement_append (&input_file_chain
,
524 (lang_statement_union_type
*) p
,
529 lang_input_statement_type
*
530 lang_add_input_file (name
, file_type
, target
)
532 lang_input_file_enum_type file_type
;
535 lang_has_input_file
= true;
536 return new_afile (name
, file_type
, target
, true);
539 /* Build enough state so that the parser can build its tree. */
544 obstack_begin (&stat_obstack
, 1000);
546 stat_ptr
= &statement_list
;
548 lang_list_init (stat_ptr
);
550 lang_list_init (&input_file_chain
);
551 lang_list_init (&lang_output_section_statement
);
552 lang_list_init (&file_chain
);
553 first_file
= lang_add_input_file ((char *) NULL
,
554 lang_input_file_is_marker_enum
,
557 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
559 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
563 /*----------------------------------------------------------------------
564 A region is an area of memory declared with the
565 MEMORY { name:org=exp, len=exp ... }
568 We maintain a list of all the regions here.
570 If no regions are specified in the script, then the default is used
571 which is created when looked up to be the entire data space. */
573 static lang_memory_region_type
*lang_memory_region_list
;
574 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
576 lang_memory_region_type
*
577 lang_memory_region_lookup (name
)
578 const char *const name
;
580 lang_memory_region_type
*p
;
582 for (p
= lang_memory_region_list
;
583 p
!= (lang_memory_region_type
*) NULL
;
586 if (strcmp (p
->name
, name
) == 0)
593 /* This code used to always use the first region in the list as the
594 default region. I changed it to instead use a region
595 encompassing all of memory as the default region. This permits
596 NOLOAD sections to work reasonably without requiring a region.
597 People should specify what region they mean, if they really want
599 if (strcmp (name
, "*default*") == 0)
601 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
603 return lang_memory_region_list
;
609 lang_memory_region_type
*new =
610 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
612 new->name
= buystring (name
);
613 new->next
= (lang_memory_region_type
*) NULL
;
615 *lang_memory_region_list_tail
= new;
616 lang_memory_region_list_tail
= &new->next
;
620 new->length
= ~(bfd_size_type
) 0;
622 new->had_full_message
= false;
628 static lang_memory_region_type
*
629 lang_memory_default (section
)
632 lang_memory_region_type
*p
;
634 flagword sec_flags
= section
->flags
;
636 /* Override SEC_DATA to mean a writable section. */
637 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
638 sec_flags
|= SEC_DATA
;
640 for (p
= lang_memory_region_list
;
641 p
!= (lang_memory_region_type
*) NULL
;
644 if ((p
->flags
& sec_flags
) != 0
645 && (p
->not_flags
& sec_flags
) == 0)
650 return lang_memory_region_lookup ("*default*");
653 lang_output_section_statement_type
*
654 lang_output_section_find (name
)
655 const char *const name
;
657 lang_statement_union_type
*u
;
658 lang_output_section_statement_type
*lookup
;
660 for (u
= lang_output_section_statement
.head
;
661 u
!= (lang_statement_union_type
*) NULL
;
664 lookup
= &u
->output_section_statement
;
665 if (strcmp (name
, lookup
->name
) == 0)
670 return (lang_output_section_statement_type
*) NULL
;
673 lang_output_section_statement_type
*
674 lang_output_section_statement_lookup (name
)
675 const char *const name
;
677 lang_output_section_statement_type
*lookup
;
679 lookup
= lang_output_section_find (name
);
680 if (lookup
== (lang_output_section_statement_type
*) NULL
)
683 lookup
= (lang_output_section_statement_type
*)
684 new_stat (lang_output_section_statement
, stat_ptr
);
685 lookup
->region
= (lang_memory_region_type
*) NULL
;
686 lookup
->lma_region
= (lang_memory_region_type
*) NULL
;
688 lookup
->block_value
= 1;
691 lookup
->next
= (lang_statement_union_type
*) NULL
;
692 lookup
->bfd_section
= (asection
*) NULL
;
693 lookup
->processed
= false;
694 lookup
->sectype
= normal_section
;
695 lookup
->addr_tree
= (etree_type
*) NULL
;
696 lang_list_init (&lookup
->children
);
698 lookup
->memspec
= (const char *) NULL
;
700 lookup
->subsection_alignment
= -1;
701 lookup
->section_alignment
= -1;
702 lookup
->load_base
= (union etree_union
*) NULL
;
703 lookup
->phdrs
= NULL
;
705 lang_statement_append (&lang_output_section_statement
,
706 (lang_statement_union_type
*) lookup
,
713 lang_map_flags (flag
)
716 if (flag
& SEC_ALLOC
)
722 if (flag
& SEC_READONLY
)
735 lang_memory_region_type
*m
;
737 minfo (_("\nMemory Configuration\n\n"));
738 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
739 _("Name"), _("Origin"), _("Length"), _("Attributes"));
741 for (m
= lang_memory_region_list
;
742 m
!= (lang_memory_region_type
*) NULL
;
748 fprintf (config
.map_file
, "%-16s ", m
->name
);
750 sprintf_vma (buf
, m
->origin
);
751 minfo ("0x%s ", buf
);
759 minfo ("0x%V", m
->length
);
760 if (m
->flags
|| m
->not_flags
)
768 lang_map_flags (m
->flags
);
774 lang_map_flags (m
->not_flags
);
781 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
786 /* Initialize an output section. */
790 lang_output_section_statement_type
*s
;
792 section_userdata_type
*new;
794 if (s
->bfd_section
!= NULL
)
797 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
798 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME
);
800 new = ((section_userdata_type
*)
801 stat_alloc (sizeof (section_userdata_type
)));
803 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
804 if (s
->bfd_section
== (asection
*) NULL
)
805 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
806 if (s
->bfd_section
== (asection
*) NULL
)
808 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
809 output_bfd
->xvec
->name
, s
->name
);
811 s
->bfd_section
->output_section
= s
->bfd_section
;
813 /* We initialize an output sections output offset to minus its own
814 vma to allow us to output a section through itself. */
815 s
->bfd_section
->output_offset
= 0;
816 get_userdata (s
->bfd_section
) = (PTR
) new;
818 /* If there is a base address, make sure that any sections it might
819 mention are initialized. */
820 if (s
->addr_tree
!= NULL
)
821 exp_init_os (s
->addr_tree
);
824 /* Make sure that all output sections mentioned in an expression are
831 switch (exp
->type
.node_class
)
834 exp_init_os (exp
->assign
.src
);
838 exp_init_os (exp
->binary
.lhs
);
839 exp_init_os (exp
->binary
.rhs
);
843 exp_init_os (exp
->trinary
.cond
);
844 exp_init_os (exp
->trinary
.lhs
);
845 exp_init_os (exp
->trinary
.rhs
);
849 exp_init_os (exp
->unary
.child
);
853 switch (exp
->type
.node_code
)
859 lang_output_section_statement_type
*os
;
861 os
= lang_output_section_find (exp
->name
.name
);
862 if (os
!= NULL
&& os
->bfd_section
== NULL
)
873 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
874 once into the output. This routine checks each section, and
875 arrange to discard it if a section of the same name has already
876 been linked. If the section has COMDAT information, then it uses
877 that to decide whether the section should be included. This code
878 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
879 that is, it does not depend solely upon the section name.
880 section_already_linked is called via bfd_map_over_sections. */
882 /* This is the shape of the elements inside the already_linked hash
883 table. It maps a name onto a list of already_linked elements with
884 the same name. It's possible to get more than one element in a
885 list if the COMDAT sections have different names. */
887 struct already_linked_hash_entry
889 struct bfd_hash_entry root
;
890 struct already_linked
*entry
;
893 struct already_linked
895 struct already_linked
*next
;
899 /* The hash table. */
901 static struct bfd_hash_table already_linked_table
;
904 section_already_linked (abfd
, sec
, data
)
909 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
912 struct already_linked
*l
;
913 struct already_linked_hash_entry
*already_linked_list
;
915 /* If we are only reading symbols from this object, then we want to
916 discard all sections. */
917 if (entry
->just_syms_flag
)
919 sec
->output_section
= bfd_abs_section_ptr
;
920 sec
->output_offset
= sec
->vma
;
924 flags
= bfd_get_section_flags (abfd
, sec
);
926 if ((flags
& SEC_LINK_ONCE
) == 0)
929 /* FIXME: When doing a relocateable link, we may have trouble
930 copying relocations in other sections that refer to local symbols
931 in the section being discarded. Those relocations will have to
932 be converted somehow; as of this writing I'm not sure that any of
933 the backends handle that correctly.
935 It is tempting to instead not discard link once sections when
936 doing a relocateable link (technically, they should be discarded
937 whenever we are building constructors). However, that fails,
938 because the linker winds up combining all the link once sections
939 into a single large link once section, which defeats the purpose
940 of having link once sections in the first place.
942 Also, not merging link once sections in a relocateable link
943 causes trouble for MIPS ELF, which relies in link once semantics
944 to handle the .reginfo section correctly. */
946 name
= bfd_get_section_name (abfd
, sec
);
948 already_linked_list
=
949 ((struct already_linked_hash_entry
*)
950 bfd_hash_lookup (&already_linked_table
, name
, true, false));
952 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
954 if (sec
->comdat
== NULL
955 || l
->sec
->comdat
== NULL
956 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
958 /* The section has already been linked. See if we should
960 switch (flags
& SEC_LINK_DUPLICATES
)
965 case SEC_LINK_DUPLICATES_DISCARD
:
968 case SEC_LINK_DUPLICATES_ONE_ONLY
:
969 if (sec
->comdat
== NULL
)
970 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
973 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
974 abfd
, name
, sec
->comdat
->name
);
977 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
978 /* FIXME: We should really dig out the contents of both
979 sections and memcmp them. The COFF/PE spec says that
980 the Microsoft linker does not implement this
981 correctly, so I'm not going to bother doing it
984 case SEC_LINK_DUPLICATES_SAME_SIZE
:
985 if (bfd_section_size (abfd
, sec
)
986 != bfd_section_size (l
->sec
->owner
, l
->sec
))
987 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
992 /* Set the output_section field so that wild_doit does not
993 create a lang_input_section structure for this section.
994 Since there might be a symbol in the section being
995 discarded, we must retain a pointer to the section which
996 we are really going to use. */
997 sec
->output_section
= bfd_abs_section_ptr
;
998 if (sec
->comdat
!= NULL
)
999 sec
->comdat
->sec
= l
->sec
;
1005 /* This is the first section with this name. Record it. Allocate
1006 the memory from the same obstack as the hash table is kept in. */
1008 l
= ((struct already_linked
*)
1009 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1012 l
->next
= already_linked_list
->entry
;
1013 already_linked_list
->entry
= l
;
1016 /* Support routines for the hash table used by section_already_linked,
1017 initialize the table, fill in an entry and remove the table. */
1019 static struct bfd_hash_entry
*
1020 already_linked_newfunc (entry
, table
, string
)
1021 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1022 struct bfd_hash_table
*table
;
1023 const char *string ATTRIBUTE_UNUSED
;
1025 struct already_linked_hash_entry
*ret
=
1026 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1030 return (struct bfd_hash_entry
*) ret
;
1034 already_linked_table_init ()
1036 if (! bfd_hash_table_init_n (&already_linked_table
,
1037 already_linked_newfunc
,
1039 einfo (_("%P%F: Failed to create hash table\n"));
1043 already_linked_table_free ()
1045 bfd_hash_table_free (&already_linked_table
);
1048 /* The wild routines.
1050 These expand statements like *(.text) and foo.o to a list of
1051 explicit actions, like foo.o(.text), bar.o(.text) and
1052 foo.o(.text, .data). */
1054 /* Return true if the PATTERN argument is a wildcard pattern.
1055 Although backslashes are treated specially if a pattern contains
1056 wildcards, we do not consider the mere presence of a backslash to
1057 be enough to cause the the pattern to be treated as a wildcard.
1058 That lets us handle DOS filenames more naturally. */
1062 const char *pattern
;
1066 for (s
= pattern
; *s
!= '\0'; ++s
)
1074 /* Add SECTION to the output section OUTPUT. Do this by creating a
1075 lang_input_section statement which is placed at PTR. FILE is the
1076 input file which holds SECTION. */
1079 wild_doit (ptr
, section
, output
, file
)
1080 lang_statement_list_type
*ptr
;
1082 lang_output_section_statement_type
*output
;
1083 lang_input_statement_type
*file
;
1088 flags
= bfd_get_section_flags (section
->owner
, section
);
1092 /* If we are doing a final link, discard sections marked with
1094 if (! link_info
.relocateable
1095 && (flags
& SEC_EXCLUDE
) != 0)
1098 /* Discard input sections which are assigned to a section named
1099 DISCARD_SECTION_NAME. */
1100 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1103 /* Discard debugging sections if we are stripping debugging
1105 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1106 && (flags
& SEC_DEBUGGING
) != 0)
1111 if (section
->output_section
== NULL
)
1113 /* This prevents future calls from assigning this section. */
1114 section
->output_section
= bfd_abs_section_ptr
;
1119 if (section
->output_section
== NULL
)
1122 lang_input_section_type
*new;
1125 if (output
->bfd_section
== NULL
)
1133 /* Add a section reference to the list. */
1134 new = new_stat (lang_input_section
, ptr
);
1136 new->section
= section
;
1138 section
->output_section
= output
->bfd_section
;
1140 flags
= section
->flags
;
1142 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1143 to an output section, because we want to be able to include a
1144 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1145 section (I don't know why we want to do this, but we do).
1146 build_link_order in ldwrite.c handles this case by turning
1147 the embedded SEC_NEVER_LOAD section into a fill. */
1149 flags
&= ~ SEC_NEVER_LOAD
;
1151 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1152 already been processed. One reason to do this is that on pe
1153 format targets, .text$foo sections go into .text and it's odd
1154 to see .text with SEC_LINK_ONCE set. */
1156 if (! link_info
.relocateable
)
1157 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1159 /* If this is not the first input section, and the SEC_READONLY
1160 flag is not currently set, then don't set it just because the
1161 input section has it set. */
1163 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1164 flags
&= ~ SEC_READONLY
;
1166 section
->output_section
->flags
|= flags
;
1168 /* If SEC_READONLY is not set in the input section, then clear
1169 it from the output section. */
1170 if ((section
->flags
& SEC_READONLY
) == 0)
1171 section
->output_section
->flags
&= ~SEC_READONLY
;
1173 switch (output
->sectype
)
1175 case normal_section
:
1180 case overlay_section
:
1181 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1183 case noload_section
:
1184 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1185 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1189 /* Copy over SEC_SMALL_DATA. */
1190 if (section
->flags
& SEC_SMALL_DATA
)
1191 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1193 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1194 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1196 /* If supplied an aligment, then force it. */
1197 if (output
->section_alignment
!= -1)
1198 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1200 if (section
->flags
& SEC_BLOCK
)
1202 section
->output_section
->flags
|= SEC_BLOCK
;
1203 /* FIXME: This value should really be obtained from the bfd... */
1204 output
->block_value
= 128;
1209 /* Handle wildcard sorting. This returns the lang_input_section which
1210 should follow the one we are going to create for SECTION and FILE,
1211 based on the sorting requirements of WILD. It returns NULL if the
1212 new section should just go at the end of the current list. */
1214 static lang_statement_union_type
*
1215 wild_sort (wild
, file
, section
)
1216 lang_wild_statement_type
*wild
;
1217 lang_input_statement_type
*file
;
1220 const char *section_name
;
1221 lang_statement_union_type
*l
;
1223 if (! wild
->filenames_sorted
&& ! wild
->sections_sorted
)
1226 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1227 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->next
)
1229 lang_input_section_type
*ls
;
1231 if (l
->header
.type
!= lang_input_section_enum
)
1233 ls
= &l
->input_section
;
1235 /* Sorting by filename takes precedence over sorting by section
1238 if (wild
->filenames_sorted
)
1240 const char *fn
, *ln
;
1244 /* The PE support for the .idata section as generated by
1245 dlltool assumes that files will be sorted by the name of
1246 the archive and then the name of the file within the
1249 if (file
->the_bfd
!= NULL
1250 && bfd_my_archive (file
->the_bfd
) != NULL
)
1252 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1257 fn
= file
->filename
;
1261 if (ls
->ifile
->the_bfd
!= NULL
1262 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1264 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1269 ln
= ls
->ifile
->filename
;
1273 i
= strcmp (fn
, ln
);
1282 fn
= file
->filename
;
1284 ln
= ls
->ifile
->filename
;
1286 i
= strcmp (fn
, ln
);
1294 /* Here either the files are not sorted by name, or we are
1295 looking at the sections for this file. */
1297 if (wild
->sections_sorted
)
1299 if (strcmp (section_name
,
1300 bfd_get_section_name (ls
->ifile
->the_bfd
,
1310 /* Expand a wild statement for a particular FILE. SECTION may be
1311 NULL, in which case it is a wild card. */
1314 output_section_callback (ptr
, section
, file
, output
)
1315 lang_wild_statement_type
*ptr
;
1317 lang_input_statement_type
*file
;
1320 lang_statement_union_type
*before
;
1322 /* If the wild pattern was marked KEEP, the member sections
1323 should be as well. */
1324 if (ptr
->keep_sections
)
1325 section
->flags
|= SEC_KEEP
;
1327 before
= wild_sort (ptr
, file
, section
);
1329 /* Here BEFORE points to the lang_input_section which
1330 should follow the one we are about to add. If BEFORE
1331 is NULL, then the section should just go at the end
1332 of the current list. */
1335 wild_doit (&ptr
->children
, section
,
1336 (lang_output_section_statement_type
*) output
,
1340 lang_statement_list_type list
;
1341 lang_statement_union_type
**pp
;
1343 lang_list_init (&list
);
1344 wild_doit (&list
, section
,
1345 (lang_output_section_statement_type
*) output
,
1348 /* If we are discarding the section, LIST.HEAD will
1350 if (list
.head
!= NULL
)
1352 ASSERT (list
.head
->next
== NULL
);
1354 for (pp
= &ptr
->children
.head
;
1357 ASSERT (*pp
!= NULL
);
1359 list
.head
->next
= *pp
;
1365 /* This is passed a file name which must have been seen already and
1366 added to the statement tree. We will see if it has been opened
1367 already and had its symbols read. If not then we'll read it. */
1369 static lang_input_statement_type
*
1373 lang_input_statement_type
*search
;
1375 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1376 search
!= (lang_input_statement_type
*) NULL
;
1377 search
= (lang_input_statement_type
*) search
->next_real_file
)
1379 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1381 if (search
->filename
!= (char *) NULL
1382 && name
!= (char *) NULL
1383 && strcmp (search
->filename
, name
) == 0)
1387 if (search
== (lang_input_statement_type
*) NULL
)
1388 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1391 /* If we have already added this file, or this file is not real
1392 (FIXME: can that ever actually happen?) or the name is NULL
1393 (FIXME: can that ever actually happen?) don't add this file. */
1396 || search
->filename
== (const char *) NULL
)
1399 load_symbols (search
, (lang_statement_list_type
*) NULL
);
1404 /* Get the symbols for an input file. */
1407 load_symbols (entry
, place
)
1408 lang_input_statement_type
*entry
;
1409 lang_statement_list_type
*place
;
1416 ldfile_open_file (entry
);
1418 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1419 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1422 lang_statement_list_type
*hold
;
1424 err
= bfd_get_error ();
1425 if (err
== bfd_error_file_ambiguously_recognized
)
1429 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1430 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1431 for (p
= matching
; *p
!= NULL
; p
++)
1435 else if (err
!= bfd_error_file_not_recognized
1437 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1439 bfd_close (entry
->the_bfd
);
1440 entry
->the_bfd
= NULL
;
1442 /* See if the emulation has some special knowledge. */
1444 if (ldemul_unrecognized_file (entry
))
1447 /* Try to interpret the file as a linker script. */
1449 ldfile_open_command_file (entry
->filename
);
1454 ldfile_assumed_script
= true;
1455 parser_input
= input_script
;
1457 ldfile_assumed_script
= false;
1464 if (ldemul_recognized_file (entry
))
1467 /* We don't call ldlang_add_file for an archive. Instead, the
1468 add_symbols entry point will call ldlang_add_file, via the
1469 add_archive_element callback, for each element of the archive
1471 switch (bfd_get_format (entry
->the_bfd
))
1477 ldlang_add_file (entry
);
1478 if (trace_files
|| trace_file_tries
)
1479 info_msg ("%I\n", entry
);
1483 if (entry
->whole_archive
)
1485 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1487 while (member
!= NULL
)
1489 if (! bfd_check_format (member
, bfd_object
))
1490 einfo (_("%F%B: object %B in archive is not object\n"),
1491 entry
->the_bfd
, member
);
1492 if (! ((*link_info
.callbacks
->add_archive_element
)
1493 (&link_info
, member
, "--whole-archive")))
1495 if (! bfd_link_add_symbols (member
, &link_info
))
1496 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1497 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1501 entry
->loaded
= true;
1507 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1508 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1510 entry
->loaded
= true;
1513 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1514 indicating that it is a wildcard. Separate lang_input_section
1515 statements are created for each part of the expansion; they are
1516 added after the wild statement S. OUTPUT is the output section. */
1519 wild (s
, section
, file
, target
, output
)
1520 lang_wild_statement_type
*s
;
1521 const char *section
;
1523 const char *target ATTRIBUTE_UNUSED
;
1524 lang_output_section_statement_type
*output
;
1526 walk_wild (s
, section
, file
, output_section_callback
, (PTR
) output
);
1528 if (section
!= (char *) NULL
1529 && strcmp (section
, "COMMON") == 0
1530 && default_common_section
== NULL
)
1532 /* Remember the section that common is going to in case we later
1533 get something which doesn't know where to put it. */
1534 default_common_section
= output
;
1538 /* Return true iff target is the sought target. */
1541 get_target (target
, data
)
1542 const bfd_target
*target
;
1545 const char *sought
= (const char *) data
;
1547 return strcmp (target
->name
, sought
) == 0;
1550 /* Like strcpy() but convert to lower case as well. */
1559 while ((c
= *src
++) != 0)
1561 if (isupper ((unsigned char) c
))
1570 /* Remove the first occurance of needle (if any) in haystack
1574 strcut (haystack
, needle
)
1578 haystack
= strstr (haystack
, needle
);
1584 for (src
= haystack
+ strlen (needle
); *src
;)
1585 *haystack
++ = *src
++;
1591 /* Compare two target format name strings.
1592 Return a value indicating how "similar" they are. */
1595 name_compare (first
, second
)
1603 copy1
= xmalloc (strlen (first
) + 1);
1604 copy2
= xmalloc (strlen (second
) + 1);
1606 /* Convert the names to lower case. */
1607 stricpy (copy1
, first
);
1608 stricpy (copy2
, second
);
1610 /* Remove and endian strings from the name. */
1611 strcut (copy1
, "big");
1612 strcut (copy1
, "little");
1613 strcut (copy2
, "big");
1614 strcut (copy2
, "little");
1616 /* Return a value based on how many characters match,
1617 starting from the beginning. If both strings are
1618 the same then return 10 * their length. */
1619 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1620 if (copy1
[result
] == 0)
1632 /* Set by closest_target_match() below. */
1633 static const bfd_target
*winner
;
1635 /* Scan all the valid bfd targets looking for one that has the endianness
1636 requirement that was specified on the command line, and is the nearest
1637 match to the original output target. */
1640 closest_target_match (target
, data
)
1641 const bfd_target
*target
;
1644 const bfd_target
*original
= (const bfd_target
*) data
;
1646 if (command_line
.endian
== ENDIAN_BIG
1647 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1650 if (command_line
.endian
== ENDIAN_LITTLE
1651 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1654 /* Must be the same flavour. */
1655 if (target
->flavour
!= original
->flavour
)
1658 /* If we have not found a potential winner yet, then record this one. */
1665 /* Oh dear, we now have two potential candidates for a successful match.
1666 Compare their names and choose the better one. */
1667 if (name_compare (target
->name
, original
->name
) > name_compare (winner
->name
, original
->name
))
1670 /* Keep on searching until wqe have checked them all. */
1674 /* Return the BFD target format of the first input file. */
1677 get_first_input_target ()
1679 char *target
= NULL
;
1681 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1683 if (s
->header
.type
== lang_input_statement_enum
1686 ldfile_open_file (s
);
1688 if (s
->the_bfd
!= NULL
1689 && bfd_check_format (s
->the_bfd
, bfd_object
))
1691 target
= bfd_get_target (s
->the_bfd
);
1702 /* Open the output file. */
1710 /* Has the user told us which output format to use? */
1711 if (output_target
== (char *) NULL
)
1713 /* No - has the current target been set to something other than
1715 if (current_target
!= default_target
)
1716 output_target
= current_target
;
1718 /* No - can we determine the format of the first input file? */
1721 output_target
= get_first_input_target ();
1723 /* Failed - use the default output target. */
1724 if (output_target
== NULL
)
1725 output_target
= default_target
;
1729 /* Has the user requested a particular endianness on the command
1731 if (command_line
.endian
!= ENDIAN_UNSET
)
1733 const bfd_target
*target
;
1734 enum bfd_endian desired_endian
;
1736 /* Get the chosen target. */
1737 target
= bfd_search_for_target (get_target
, (PTR
) output_target
);
1739 /* If the target is not supported, we cannot do anything. */
1742 if (command_line
.endian
== ENDIAN_BIG
)
1743 desired_endian
= BFD_ENDIAN_BIG
;
1745 desired_endian
= BFD_ENDIAN_LITTLE
;
1747 /* See if the target has the wrong endianness. This should
1748 not happen if the linker script has provided big and
1749 little endian alternatives, but some scrips don't do
1751 if (target
->byteorder
!= desired_endian
)
1753 /* If it does, then see if the target provides
1754 an alternative with the correct endianness. */
1755 if (target
->alternative_target
!= NULL
1756 && (target
->alternative_target
->byteorder
== desired_endian
))
1757 output_target
= target
->alternative_target
->name
;
1760 /* Try to find a target as similar as possible to
1761 the default target, but which has the desired
1762 endian characteristic. */
1763 (void) bfd_search_for_target (closest_target_match
, (PTR
) target
);
1765 /* Oh dear - we could not find any targets that
1766 satisfy our requirements. */
1768 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1770 output_target
= winner
->name
;
1776 output
= bfd_openw (name
, output_target
);
1778 if (output
== (bfd
*) NULL
)
1780 if (bfd_get_error () == bfd_error_invalid_target
)
1781 einfo (_("%P%F: target %s not found\n"), output_target
);
1783 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1786 delete_output_file_on_failure
= true;
1789 output
->flags
|= D_PAGED
;
1792 if (! bfd_set_format (output
, bfd_object
))
1793 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1794 if (! bfd_set_arch_mach (output
,
1795 ldfile_output_architecture
,
1796 ldfile_output_machine
))
1797 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1799 link_info
.hash
= bfd_link_hash_table_create (output
);
1800 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1801 einfo (_("%P%F: can not create link hash table: %E\n"));
1803 bfd_set_gp_size (output
, g_switch_value
);
1808 ldlang_open_output (statement
)
1809 lang_statement_union_type
*statement
;
1811 switch (statement
->header
.type
)
1813 case lang_output_statement_enum
:
1814 ASSERT (output_bfd
== (bfd
*) NULL
);
1815 output_bfd
= open_output (statement
->output_statement
.name
);
1816 ldemul_set_output_arch ();
1817 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1818 output_bfd
->flags
|= D_PAGED
;
1820 output_bfd
->flags
&= ~D_PAGED
;
1821 if (config
.text_read_only
)
1822 output_bfd
->flags
|= WP_TEXT
;
1824 output_bfd
->flags
&= ~WP_TEXT
;
1825 if (link_info
.traditional_format
)
1826 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1828 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1831 case lang_target_statement_enum
:
1832 current_target
= statement
->target_statement
.target
;
1839 /* Open all the input files. */
1842 open_input_bfds (s
, force
)
1843 lang_statement_union_type
*s
;
1846 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1848 switch (s
->header
.type
)
1850 case lang_constructors_statement_enum
:
1851 open_input_bfds (constructor_list
.head
, force
);
1853 case lang_output_section_statement_enum
:
1854 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1856 case lang_wild_statement_enum
:
1857 /* Maybe we should load the file's symbols. */
1858 if (s
->wild_statement
.filename
1859 && ! wildcardp (s
->wild_statement
.filename
))
1860 (void) lookup_name (s
->wild_statement
.filename
);
1861 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1863 case lang_group_statement_enum
:
1865 struct bfd_link_hash_entry
*undefs
;
1867 /* We must continually search the entries in the group
1868 until no new symbols are added to the list of undefined
1873 undefs
= link_info
.hash
->undefs_tail
;
1874 open_input_bfds (s
->group_statement
.children
.head
, true);
1876 while (undefs
!= link_info
.hash
->undefs_tail
);
1879 case lang_target_statement_enum
:
1880 current_target
= s
->target_statement
.target
;
1882 case lang_input_statement_enum
:
1883 if (s
->input_statement
.real
)
1885 lang_statement_list_type add
;
1887 s
->input_statement
.target
= current_target
;
1889 /* If we are being called from within a group, and this
1890 is an archive which has already been searched, then
1891 force it to be researched unless the whole archive
1892 has been loaded already. */
1894 && !s
->input_statement
.whole_archive
1895 && s
->input_statement
.loaded
1896 && bfd_check_format (s
->input_statement
.the_bfd
,
1898 s
->input_statement
.loaded
= false;
1900 lang_list_init (&add
);
1902 load_symbols (&s
->input_statement
, &add
);
1904 if (add
.head
!= NULL
)
1906 *add
.tail
= s
->next
;
1917 /* If there are [COMMONS] statements, put a wild one into the bss
1921 lang_reasonable_defaults ()
1924 lang_output_section_statement_lookup (".text");
1925 lang_output_section_statement_lookup (".data");
1927 default_common_section
= lang_output_section_statement_lookup (".bss");
1929 if (placed_commons
== false)
1931 lang_wild_statement_type
*new =
1932 new_stat (lang_wild_statement
,
1933 &default_common_section
->children
);
1935 new->section_name
= "COMMON";
1936 new->filename
= (char *) NULL
;
1937 lang_list_init (&new->children
);
1942 /* Add the supplied name to the symbol table as an undefined reference.
1943 Remove items from the chain as we open input bfds. */
1944 typedef struct ldlang_undef_chain_list
1946 struct ldlang_undef_chain_list
*next
;
1948 } ldlang_undef_chain_list_type
;
1950 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1953 ldlang_add_undef (name
)
1954 const char *const name
;
1956 ldlang_undef_chain_list_type
*new =
1957 ((ldlang_undef_chain_list_type
*)
1958 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1960 new->next
= ldlang_undef_chain_list_head
;
1961 ldlang_undef_chain_list_head
= new;
1963 new->name
= buystring (name
);
1966 /* Run through the list of undefineds created above and place them
1967 into the linker hash table as undefined symbols belonging to the
1971 lang_place_undefineds ()
1973 ldlang_undef_chain_list_type
*ptr
;
1975 for (ptr
= ldlang_undef_chain_list_head
;
1976 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1979 struct bfd_link_hash_entry
*h
;
1981 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1982 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1983 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1984 if (h
->type
== bfd_link_hash_new
)
1986 h
->type
= bfd_link_hash_undefined
;
1987 h
->u
.undef
.abfd
= NULL
;
1988 bfd_link_add_undef (link_info
.hash
, h
);
1993 /* Open input files and attatch to output sections. */
1996 map_input_to_output_sections (s
, target
, output_section_statement
)
1997 lang_statement_union_type
*s
;
1999 lang_output_section_statement_type
*output_section_statement
;
2001 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2003 switch (s
->header
.type
)
2006 case lang_wild_statement_enum
:
2007 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
2008 s
->wild_statement
.filename
, target
,
2009 output_section_statement
);
2012 case lang_constructors_statement_enum
:
2013 map_input_to_output_sections (constructor_list
.head
,
2015 output_section_statement
);
2017 case lang_output_section_statement_enum
:
2018 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2020 &s
->output_section_statement
);
2022 case lang_output_statement_enum
:
2024 case lang_target_statement_enum
:
2025 target
= s
->target_statement
.target
;
2027 case lang_group_statement_enum
:
2028 map_input_to_output_sections (s
->group_statement
.children
.head
,
2030 output_section_statement
);
2032 case lang_fill_statement_enum
:
2033 case lang_input_section_enum
:
2034 case lang_object_symbols_statement_enum
:
2035 case lang_data_statement_enum
:
2036 case lang_reloc_statement_enum
:
2037 case lang_padding_statement_enum
:
2038 case lang_input_statement_enum
:
2039 if (output_section_statement
!= NULL
2040 && output_section_statement
->bfd_section
== NULL
)
2041 init_os (output_section_statement
);
2043 case lang_assignment_statement_enum
:
2044 if (output_section_statement
!= NULL
2045 && output_section_statement
->bfd_section
== NULL
)
2046 init_os (output_section_statement
);
2048 /* Make sure that any sections mentioned in the assignment
2050 exp_init_os (s
->assignment_statement
.exp
);
2052 case lang_afile_asection_pair_statement_enum
:
2055 case lang_address_statement_enum
:
2056 /* Mark the specified section with the supplied address. */
2058 lang_output_section_statement_type
*os
=
2059 lang_output_section_statement_lookup
2060 (s
->address_statement
.section_name
);
2062 if (os
->bfd_section
== NULL
)
2064 os
->addr_tree
= s
->address_statement
.address
;
2072 print_output_section_statement (output_section_statement
)
2073 lang_output_section_statement_type
*output_section_statement
;
2075 asection
*section
= output_section_statement
->bfd_section
;
2078 if (output_section_statement
!= abs_output_section
)
2080 minfo ("\n%s", output_section_statement
->name
);
2082 if (section
!= NULL
)
2084 print_dot
= section
->vma
;
2086 len
= strlen (output_section_statement
->name
);
2087 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2092 while (len
< SECTION_NAME_MAP_LENGTH
)
2098 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2100 if (output_section_statement
->load_base
!= NULL
)
2104 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2105 "load base", lang_final_phase_enum
);
2106 minfo (_(" load address 0x%V"), addr
);
2113 print_statement_list (output_section_statement
->children
.head
,
2114 output_section_statement
);
2118 print_assignment (assignment
, output_section
)
2119 lang_assignment_statement_type
*assignment
;
2120 lang_output_section_statement_type
*output_section
;
2123 etree_value_type result
;
2125 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2128 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2129 lang_final_phase_enum
, print_dot
, &print_dot
);
2131 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2142 exp_print_tree (assignment
->exp
);
2148 print_input_statement (statm
)
2149 lang_input_statement_type
*statm
;
2151 if (statm
->filename
!= (char *) NULL
)
2153 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2157 /* Print all symbols defined in a particular section. This is called
2158 via bfd_link_hash_traverse. */
2161 print_one_symbol (hash_entry
, ptr
)
2162 struct bfd_link_hash_entry
*hash_entry
;
2165 asection
*sec
= (asection
*) ptr
;
2167 if ((hash_entry
->type
== bfd_link_hash_defined
2168 || hash_entry
->type
== bfd_link_hash_defweak
)
2169 && sec
== hash_entry
->u
.def
.section
)
2173 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2176 (hash_entry
->u
.def
.value
2177 + hash_entry
->u
.def
.section
->output_offset
2178 + hash_entry
->u
.def
.section
->output_section
->vma
));
2180 minfo (" %T\n", hash_entry
->root
.string
);
2186 /* Print information about an input section to the map file. */
2189 print_input_section (in
)
2190 lang_input_section_type
*in
;
2192 asection
*i
= in
->section
;
2193 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2194 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2195 ldfile_output_machine
);
2200 minfo ("%s", i
->name
);
2202 if (i
->output_section
!= NULL
)
2206 len
= 1 + strlen (i
->name
);
2207 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2212 while (len
< SECTION_NAME_MAP_LENGTH
)
2218 minfo ("0x%V %W %B\n",
2219 i
->output_section
->vma
+ i
->output_offset
, size
/ opb
,
2222 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2224 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2236 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2239 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2241 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
/ opb
;
2247 print_fill_statement (fill
)
2248 lang_fill_statement_type
*fill
;
2250 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2254 print_data_statement (data
)
2255 lang_data_statement_type
*data
;
2261 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2262 ldfile_output_machine
);
2264 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2267 addr
= data
->output_vma
;
2268 if (data
->output_section
!= NULL
)
2269 addr
+= data
->output_section
->vma
;
2297 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2299 if (data
->exp
->type
.node_class
!= etree_value
)
2302 exp_print_tree (data
->exp
);
2307 print_dot
= addr
+ size
/ opb
;
2311 /* Print an address statement. These are generated by options like
2315 print_address_statement (address
)
2316 lang_address_statement_type
*address
;
2318 minfo (_("Address of section %s set to "), address
->section_name
);
2319 exp_print_tree (address
->address
);
2323 /* Print a reloc statement. */
2326 print_reloc_statement (reloc
)
2327 lang_reloc_statement_type
*reloc
;
2332 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2333 ldfile_output_machine
);
2335 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2338 addr
= reloc
->output_vma
;
2339 if (reloc
->output_section
!= NULL
)
2340 addr
+= reloc
->output_section
->vma
;
2342 size
= bfd_get_reloc_size (reloc
->howto
);
2344 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2346 if (reloc
->name
!= NULL
)
2347 minfo ("%s+", reloc
->name
);
2349 minfo ("%s+", reloc
->section
->name
);
2351 exp_print_tree (reloc
->addend_exp
);
2355 print_dot
= addr
+ size
/ opb
;
2359 print_padding_statement (s
)
2360 lang_padding_statement_type
*s
;
2364 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2365 ldfile_output_machine
);
2369 len
= sizeof " *fill*" - 1;
2370 while (len
< SECTION_NAME_MAP_LENGTH
)
2376 addr
= s
->output_offset
;
2377 if (s
->output_section
!= NULL
)
2378 addr
+= s
->output_section
->vma
;
2379 minfo ("0x%V %W", addr
, s
->size
);
2382 minfo (" %u", s
->fill
);
2386 print_dot
= addr
+ s
->size
/ opb
;
2390 print_wild_statement (w
, os
)
2391 lang_wild_statement_type
*w
;
2392 lang_output_section_statement_type
*os
;
2396 if (w
->filenames_sorted
)
2398 if (w
->exclude_filename_list
!= NULL
)
2401 minfo ("EXCLUDE_FILE ( %s", w
->exclude_filename_list
->name
);
2402 for (tmp
= w
->exclude_filename_list
->next
; tmp
; tmp
= tmp
->next
)
2403 minfo (", %s", tmp
->name
);
2406 if (w
->filename
!= NULL
)
2407 minfo ("%s", w
->filename
);
2410 if (w
->filenames_sorted
)
2414 if (w
->sections_sorted
)
2416 if (w
->section_name
!= NULL
)
2417 minfo ("%s", w
->section_name
);
2420 if (w
->sections_sorted
)
2426 print_statement_list (w
->children
.head
, os
);
2429 /* Print a group statement. */
2433 lang_group_statement_type
*s
;
2434 lang_output_section_statement_type
*os
;
2436 fprintf (config
.map_file
, "START GROUP\n");
2437 print_statement_list (s
->children
.head
, os
);
2438 fprintf (config
.map_file
, "END GROUP\n");
2441 /* Print the list of statements in S.
2442 This can be called for any statement type. */
2445 print_statement_list (s
, os
)
2446 lang_statement_union_type
*s
;
2447 lang_output_section_statement_type
*os
;
2451 print_statement (s
, os
);
2456 /* Print the first statement in statement list S.
2457 This can be called for any statement type. */
2460 print_statement (s
, os
)
2461 lang_statement_union_type
*s
;
2462 lang_output_section_statement_type
*os
;
2464 switch (s
->header
.type
)
2467 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2470 case lang_constructors_statement_enum
:
2471 if (constructor_list
.head
!= NULL
)
2473 if (constructors_sorted
)
2474 minfo (" SORT (CONSTRUCTORS)\n");
2476 minfo (" CONSTRUCTORS\n");
2477 print_statement_list (constructor_list
.head
, os
);
2480 case lang_wild_statement_enum
:
2481 print_wild_statement (&s
->wild_statement
, os
);
2483 case lang_address_statement_enum
:
2484 print_address_statement (&s
->address_statement
);
2486 case lang_object_symbols_statement_enum
:
2487 minfo (" CREATE_OBJECT_SYMBOLS\n");
2489 case lang_fill_statement_enum
:
2490 print_fill_statement (&s
->fill_statement
);
2492 case lang_data_statement_enum
:
2493 print_data_statement (&s
->data_statement
);
2495 case lang_reloc_statement_enum
:
2496 print_reloc_statement (&s
->reloc_statement
);
2498 case lang_input_section_enum
:
2499 print_input_section (&s
->input_section
);
2501 case lang_padding_statement_enum
:
2502 print_padding_statement (&s
->padding_statement
);
2504 case lang_output_section_statement_enum
:
2505 print_output_section_statement (&s
->output_section_statement
);
2507 case lang_assignment_statement_enum
:
2508 print_assignment (&s
->assignment_statement
, os
);
2510 case lang_target_statement_enum
:
2511 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2513 case lang_output_statement_enum
:
2514 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2515 if (output_target
!= NULL
)
2516 minfo (" %s", output_target
);
2519 case lang_input_statement_enum
:
2520 print_input_statement (&s
->input_statement
);
2522 case lang_group_statement_enum
:
2523 print_group (&s
->group_statement
, os
);
2525 case lang_afile_asection_pair_statement_enum
:
2534 print_statement_list (statement_list
.head
, abs_output_section
);
2537 /* Print the first N statements in statement list S to STDERR.
2538 If N == 0, nothing is printed.
2539 If N < 0, the entire list is printed.
2540 Intended to be called from GDB. */
2543 dprint_statement (s
, n
)
2544 lang_statement_union_type
*s
;
2547 FILE *map_save
= config
.map_file
;
2549 config
.map_file
= stderr
;
2552 print_statement_list (s
, abs_output_section
);
2555 while (s
&& --n
>= 0)
2557 print_statement (s
, abs_output_section
);
2562 config
.map_file
= map_save
;
2566 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
2567 lang_statement_union_type
**this_ptr
;
2570 asection
*output_section_statement
;
2573 /* Align this section first to the
2574 input sections requirement, then
2575 to the output section's requirement.
2576 If this alignment is > than any seen before,
2577 then record it too. Perform the alignment by
2578 inserting a magic 'padding' statement. */
2580 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2581 ldfile_output_machine
);
2582 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
2584 if (alignment_needed
!= 0)
2586 lang_statement_union_type
*new =
2587 ((lang_statement_union_type
*)
2588 stat_alloc (sizeof (lang_padding_statement_type
)));
2590 /* Link into existing chain. */
2591 new->header
.next
= *this_ptr
;
2593 new->header
.type
= lang_padding_statement_enum
;
2594 new->padding_statement
.output_section
= output_section_statement
;
2595 new->padding_statement
.output_offset
=
2596 dot
- output_section_statement
->vma
;
2597 new->padding_statement
.fill
= fill
;
2598 new->padding_statement
.size
= alignment_needed
* opb
;
2601 /* Remember the most restrictive alignment. */
2602 if (power
> output_section_statement
->alignment_power
)
2604 output_section_statement
->alignment_power
= power
;
2606 output_section_statement
->_raw_size
+= alignment_needed
* opb
;
2608 return dot
+ alignment_needed
;
2611 /* Work out how much this section will move the dot point. */
2614 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
2615 lang_statement_union_type
**this_ptr
;
2616 lang_output_section_statement_type
*output_section_statement
;
2619 boolean relax ATTRIBUTE_UNUSED
;
2621 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2622 asection
*i
= is
->section
;
2623 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2624 ldfile_output_machine
);
2626 if (is
->ifile
->just_syms_flag
== false)
2628 if (output_section_statement
->subsection_alignment
!= -1)
2629 i
->alignment_power
=
2630 output_section_statement
->subsection_alignment
;
2632 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
2633 output_section_statement
->bfd_section
, dot
);
2635 /* Remember where in the output section this input section goes. */
2637 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
2639 /* Mark how big the output section must be to contain this now. */
2640 if (i
->_cooked_size
!= 0)
2641 dot
+= i
->_cooked_size
/ opb
;
2643 dot
+= i
->_raw_size
/ opb
;
2644 output_section_statement
->bfd_section
->_raw_size
=
2645 (dot
- output_section_statement
->bfd_section
->vma
) * opb
;
2649 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2655 #define IGNORE_SECTION(bfd, s) \
2656 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
2657 || bfd_section_size (bfd, s) == 0)
2659 /* Check to see if any allocated sections overlap with other allocated
2660 sections. This can happen when the linker script specifically specifies
2661 the output section addresses of the two sections. */
2664 lang_check_section_addresses ()
2667 unsigned opb
= bfd_octets_per_byte (output_bfd
);
2669 /* Scan all sections in the output list. */
2670 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2674 /* Ignore sections which are not loaded or which have no contents. */
2675 if (IGNORE_SECTION (output_bfd
, s
))
2678 /* Once we reach section 's' stop our seach. This prevents two
2679 warning messages from being produced, one for 'section A overlaps
2680 section B' and one for 'section B overlaps section A'. */
2681 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2688 /* Only consider loadable sections with real contents. */
2689 if (IGNORE_SECTION (output_bfd
, os
))
2692 /* We must check the sections' LMA addresses not their
2693 VMA addresses because overlay sections can have
2694 overlapping VMAs but they must have distinct LMAs. */
2695 s_start
= bfd_section_lma (output_bfd
, s
);
2696 os_start
= bfd_section_lma (output_bfd
, os
);
2697 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) / opb
- 1;
2698 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) / opb
- 1;
2700 /* Look for an overlap. */
2701 if ((s_end
< os_start
) || (s_start
> os_end
))
2705 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2706 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2708 /* Once we have found one overlap for this section,
2709 stop looking for others. */
2715 /* This variable indicates whether bfd_relax_section should be called
2718 static boolean relax_again
;
2720 /* Make sure the new address is within the region. We explicitly permit the
2721 current address to be at the exact end of the region when the address is
2722 non-zero, in case the region is at the end of addressable memory and the
2723 calculation wraps around. */
2726 os_region_check (os
, region
, tree
, base
)
2727 lang_output_section_statement_type
*os
;
2728 struct memory_region_struct
*region
;
2732 if ((region
->current
< region
->origin
2733 || (region
->current
- region
->origin
> region
->length
))
2734 && ((region
->current
!= region
->origin
+ region
->length
)
2737 if (tree
!= (etree_type
*) NULL
)
2739 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2741 os
->bfd_section
->owner
,
2742 os
->bfd_section
->name
,
2747 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2749 os
->bfd_section
->owner
,
2750 os
->bfd_section
->name
);
2752 /* Reset the region pointer. */
2753 region
->current
= region
->origin
;
2757 /* Set the sizes for all the output sections. */
2760 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2761 lang_statement_union_type
*s
;
2762 lang_output_section_statement_type
*output_section_statement
;
2763 lang_statement_union_type
**prev
;
2768 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2769 ldfile_output_machine
);
2771 /* Size up the sections from their constituent parts. */
2772 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2774 switch (s
->header
.type
)
2776 case lang_output_section_statement_enum
:
2779 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2781 if (os
->bfd_section
== NULL
)
2782 /* This section was never actually created. */
2785 /* If this is a COFF shared library section, use the size and
2786 address from the input section. FIXME: This is COFF
2787 specific; it would be cleaner if there were some other way
2788 to do this, but nothing simple comes to mind. */
2789 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2793 if (os
->children
.head
== NULL
2794 || os
->children
.head
->next
!= NULL
2795 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2796 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2799 input
= os
->children
.head
->input_section
.section
;
2800 bfd_set_section_vma (os
->bfd_section
->owner
,
2802 bfd_section_vma (input
->owner
, input
));
2803 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2807 if (bfd_is_abs_section (os
->bfd_section
))
2809 /* No matter what happens, an abs section starts at zero. */
2810 ASSERT (os
->bfd_section
->vma
== 0);
2814 if (os
->addr_tree
== (etree_type
*) NULL
)
2816 /* No address specified for this section, get one
2817 from the region specification. */
2818 if (os
->region
== (lang_memory_region_type
*) NULL
2819 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2820 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2821 && os
->region
->name
[0] == '*'
2822 && strcmp (os
->region
->name
, "*default*") == 0))
2824 os
->region
= lang_memory_default (os
->bfd_section
);
2827 /* If a loadable section is using the default memory
2828 region, and some non default memory regions were
2829 defined, issue a warning. */
2830 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2831 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2832 && ! link_info
.relocateable
2833 && strcmp (os
->region
->name
, "*default*") == 0
2834 && lang_memory_region_list
!= NULL
2835 && (strcmp (lang_memory_region_list
->name
, "*default*") != 0
2836 || lang_memory_region_list
->next
!= NULL
))
2837 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2838 bfd_get_section_name (output_bfd
, os
->bfd_section
));
2840 dot
= os
->region
->current
;
2842 if (os
->section_alignment
== -1)
2847 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2849 if (dot
!= olddot
&& config
.warn_section_align
)
2850 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2851 os
->name
, (unsigned int) (dot
- olddot
));
2858 r
= exp_fold_tree (os
->addr_tree
,
2860 lang_allocating_phase_enum
,
2862 if (r
.valid_p
== false)
2864 einfo (_("%F%S: non constant address expression for section %s\n"),
2867 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2870 /* The section starts here.
2871 First, align to what the section needs. */
2873 if (os
->section_alignment
!= -1)
2874 dot
= align_power (dot
, os
->section_alignment
);
2876 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2878 os
->bfd_section
->output_offset
= 0;
2881 (void) lang_size_sections (os
->children
.head
, os
,
2883 os
->fill
, dot
, relax
);
2885 /* Put the section within the requested block size, or
2886 align at the block boundary. */
2887 after
= ALIGN_N (os
->bfd_section
->vma
2888 + os
->bfd_section
->_raw_size
/ opb
,
2889 /* The coercion here is important, see ld.h. */
2890 (bfd_vma
) os
->block_value
);
2892 if (bfd_is_abs_section (os
->bfd_section
))
2893 ASSERT (after
== os
->bfd_section
->vma
);
2895 os
->bfd_section
->_raw_size
=
2896 (after
- os
->bfd_section
->vma
) * opb
;
2897 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
2898 os
->processed
= true;
2900 /* Update dot in the region ?
2901 We only do this if the section is going to be allocated,
2902 since unallocated sections do not contribute to the region's
2903 overall size in memory.
2905 If the SEC_NEVER_LOAD bit is not set, it will affect the
2906 addresses of sections after it. We have to update
2908 if (os
->region
!= (lang_memory_region_type
*) NULL
2909 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2910 & SEC_NEVER_LOAD
) == 0
2911 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2912 & (SEC_ALLOC
| SEC_LOAD
))))
2914 os
->region
->current
= dot
;
2916 /* Make sure the new address is within the region. */
2917 os_region_check (os
, os
->region
, os
->addr_tree
,
2918 os
->bfd_section
->vma
);
2920 /* If there's no load address specified, use the run
2921 region as the load region. */
2922 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
2923 os
->lma_region
= os
->region
;
2925 if (os
->lma_region
!= NULL
)
2927 if (os
->load_base
!= NULL
)
2929 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
2933 /* Don't allocate twice. */
2934 if (os
->lma_region
!= os
->region
)
2936 /* Set load_base, which will be handled later. */
2938 exp_intop (os
->lma_region
->current
);
2939 os
->lma_region
->current
+=
2940 os
->bfd_section
->_raw_size
/ opb
;
2941 os_region_check (os
, os
->lma_region
, NULL
,
2942 os
->bfd_section
->lma
);
2950 case lang_constructors_statement_enum
:
2951 dot
= lang_size_sections (constructor_list
.head
,
2952 output_section_statement
,
2953 &s
->wild_statement
.children
.head
,
2958 case lang_data_statement_enum
:
2960 unsigned int size
= 0;
2962 s
->data_statement
.output_vma
=
2963 dot
- output_section_statement
->bfd_section
->vma
;
2964 s
->data_statement
.output_section
=
2965 output_section_statement
->bfd_section
;
2967 switch (s
->data_statement
.type
)
2988 output_section_statement
->bfd_section
->_raw_size
+= size
;
2989 /* The output section gets contents, and then we inspect for
2990 any flags set in the input script which override any ALLOC. */
2991 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2992 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
2994 output_section_statement
->bfd_section
->flags
|=
2995 SEC_ALLOC
| SEC_LOAD
;
3000 case lang_reloc_statement_enum
:
3004 s
->reloc_statement
.output_vma
=
3005 dot
- output_section_statement
->bfd_section
->vma
;
3006 s
->reloc_statement
.output_section
=
3007 output_section_statement
->bfd_section
;
3008 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3010 output_section_statement
->bfd_section
->_raw_size
+= size
;
3014 case lang_wild_statement_enum
:
3016 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
3017 output_section_statement
,
3018 &s
->wild_statement
.children
.head
,
3023 case lang_object_symbols_statement_enum
:
3024 link_info
.create_object_symbols_section
=
3025 output_section_statement
->bfd_section
;
3027 case lang_output_statement_enum
:
3028 case lang_target_statement_enum
:
3030 case lang_input_section_enum
:
3034 i
= (*prev
)->input_section
.section
;
3037 if (i
->_cooked_size
== 0)
3038 i
->_cooked_size
= i
->_raw_size
;
3044 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3045 einfo (_("%P%F: can't relax section: %E\n"));
3049 dot
= size_input_section (prev
,
3050 output_section_statement
,
3051 output_section_statement
->fill
,
3055 case lang_input_statement_enum
:
3057 case lang_fill_statement_enum
:
3058 s
->fill_statement
.output_section
=
3059 output_section_statement
->bfd_section
;
3061 fill
= s
->fill_statement
.fill
;
3063 case lang_assignment_statement_enum
:
3065 bfd_vma newdot
= dot
;
3067 exp_fold_tree (s
->assignment_statement
.exp
,
3068 output_section_statement
,
3069 lang_allocating_phase_enum
,
3075 /* The assignment changed dot. Insert a pad. */
3076 if (output_section_statement
== abs_output_section
)
3078 /* If we don't have an output section, then just adjust
3079 the default memory address. */
3080 lang_memory_region_lookup ("*default*")->current
= newdot
;
3084 lang_statement_union_type
*new =
3085 ((lang_statement_union_type
*)
3086 stat_alloc (sizeof (lang_padding_statement_type
)));
3088 /* Link into existing chain. */
3089 new->header
.next
= *prev
;
3091 new->header
.type
= lang_padding_statement_enum
;
3092 new->padding_statement
.output_section
=
3093 output_section_statement
->bfd_section
;
3094 new->padding_statement
.output_offset
=
3095 dot
- output_section_statement
->bfd_section
->vma
;
3096 new->padding_statement
.fill
= fill
;
3097 new->padding_statement
.size
= (newdot
- dot
) * opb
;
3098 output_section_statement
->bfd_section
->_raw_size
+=
3099 new->padding_statement
.size
;
3107 case lang_padding_statement_enum
:
3108 /* If we are relaxing, and this is not the first pass, some
3109 padding statements may have been inserted during previous
3110 passes. We may have to move the padding statement to a new
3111 location if dot has a different value at this point in this
3112 pass than it did at this point in the previous pass. */
3113 s
->padding_statement
.output_offset
=
3114 dot
- output_section_statement
->bfd_section
->vma
;
3115 dot
+= s
->padding_statement
.size
/ opb
;
3116 output_section_statement
->bfd_section
->_raw_size
+=
3117 s
->padding_statement
.size
;
3120 case lang_group_statement_enum
:
3121 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3122 output_section_statement
,
3123 &s
->group_statement
.children
.head
,
3131 /* This can only get here when relaxing is turned on. */
3133 case lang_address_statement_enum
:
3136 prev
= &s
->header
.next
;
3142 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3143 lang_statement_union_type
*s
;
3144 lang_output_section_statement_type
*output_section_statement
;
3148 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3149 ldfile_output_machine
);
3151 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3153 switch (s
->header
.type
)
3155 case lang_constructors_statement_enum
:
3156 dot
= lang_do_assignments (constructor_list
.head
,
3157 output_section_statement
,
3162 case lang_output_section_statement_enum
:
3164 lang_output_section_statement_type
*os
=
3165 &(s
->output_section_statement
);
3167 if (os
->bfd_section
!= NULL
)
3169 dot
= os
->bfd_section
->vma
;
3170 (void) lang_do_assignments (os
->children
.head
, os
,
3172 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3177 /* If nothing has been placed into the output section then
3178 it won't have a bfd_section. */
3179 if (os
->bfd_section
)
3181 os
->bfd_section
->lma
3182 = exp_get_abs_int (os
->load_base
, 0, "load base",
3183 lang_final_phase_enum
);
3188 case lang_wild_statement_enum
:
3190 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3191 output_section_statement
,
3196 case lang_object_symbols_statement_enum
:
3197 case lang_output_statement_enum
:
3198 case lang_target_statement_enum
:
3200 case lang_common_statement_enum
:
3203 case lang_data_statement_enum
:
3205 etree_value_type value
;
3207 value
= exp_fold_tree (s
->data_statement
.exp
,
3209 lang_final_phase_enum
, dot
, &dot
);
3210 s
->data_statement
.value
= value
.value
;
3211 if (value
.valid_p
== false)
3212 einfo (_("%F%P: invalid data statement\n"));
3216 switch (s
->data_statement
.type
)
3240 case lang_reloc_statement_enum
:
3242 etree_value_type value
;
3244 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3246 lang_final_phase_enum
, dot
, &dot
);
3247 s
->reloc_statement
.addend_value
= value
.value
;
3248 if (value
.valid_p
== false)
3249 einfo (_("%F%P: invalid reloc statement\n"));
3251 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
) / opb
;
3254 case lang_input_section_enum
:
3256 asection
*in
= s
->input_section
.section
;
3258 if (in
->_cooked_size
!= 0)
3259 dot
+= in
->_cooked_size
/ opb
;
3261 dot
+= in
->_raw_size
/ opb
;
3265 case lang_input_statement_enum
:
3267 case lang_fill_statement_enum
:
3268 fill
= s
->fill_statement
.fill
;
3270 case lang_assignment_statement_enum
:
3272 exp_fold_tree (s
->assignment_statement
.exp
,
3273 output_section_statement
,
3274 lang_final_phase_enum
,
3280 case lang_padding_statement_enum
:
3281 dot
+= s
->padding_statement
.size
/ opb
;
3284 case lang_group_statement_enum
:
3285 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3286 output_section_statement
,
3294 case lang_address_statement_enum
:
3302 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3303 operator .startof. (section_name), it produces an undefined symbol
3304 .startof.section_name. Similarly, when it sees
3305 .sizeof. (section_name), it produces an undefined symbol
3306 .sizeof.section_name. For all the output sections, we look for
3307 such symbols, and set them to the correct value. */
3314 if (link_info
.relocateable
)
3317 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3319 const char *secname
;
3321 struct bfd_link_hash_entry
*h
;
3323 secname
= bfd_get_section_name (output_bfd
, s
);
3324 buf
= xmalloc (10 + strlen (secname
));
3326 sprintf (buf
, ".startof.%s", secname
);
3327 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3328 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3330 h
->type
= bfd_link_hash_defined
;
3331 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3332 h
->u
.def
.section
= bfd_abs_section_ptr
;
3335 sprintf (buf
, ".sizeof.%s", secname
);
3336 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3337 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3339 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3340 ldfile_output_machine
);
3341 h
->type
= bfd_link_hash_defined
;
3342 if (s
->_cooked_size
!= 0)
3343 h
->u
.def
.value
= s
->_cooked_size
/ opb
;
3345 h
->u
.def
.value
= s
->_raw_size
/ opb
;
3346 h
->u
.def
.section
= bfd_abs_section_ptr
;
3356 struct bfd_link_hash_entry
*h
;
3359 if (link_info
.relocateable
|| link_info
.shared
)
3364 if (entry_symbol
== (char *) NULL
)
3366 /* No entry has been specified. Look for start, but don't warn
3367 if we don't find it. */
3368 entry_symbol
= "start";
3372 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3373 if (h
!= (struct bfd_link_hash_entry
*) NULL
3374 && (h
->type
== bfd_link_hash_defined
3375 || h
->type
== bfd_link_hash_defweak
)
3376 && h
->u
.def
.section
->output_section
!= NULL
)
3380 val
= (h
->u
.def
.value
3381 + bfd_get_section_vma (output_bfd
,
3382 h
->u
.def
.section
->output_section
)
3383 + h
->u
.def
.section
->output_offset
);
3384 if (! bfd_set_start_address (output_bfd
, val
))
3385 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3392 /* We couldn't find the entry symbol. Try parsing it as a
3394 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3397 if (! bfd_set_start_address (output_bfd
, val
))
3398 einfo (_("%P%F: can't set start address\n"));
3404 /* Can't find the entry symbol, and it's not a number. Use
3405 the first address in the text section. */
3406 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3407 if (ts
!= (asection
*) NULL
)
3410 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3411 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3412 if (! bfd_set_start_address (output_bfd
,
3413 bfd_get_section_vma (output_bfd
,
3415 einfo (_("%P%F: can't set start address\n"));
3420 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3427 /* This is a small function used when we want to ignore errors from
3431 #ifdef ANSI_PROTOTYPES
3432 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3434 ignore_bfd_errors (s
)
3435 const char *s ATTRIBUTE_UNUSED
;
3438 /* Don't do anything. */
3441 /* Check that the architecture of all the input files is compatible
3442 with the output file. Also call the backend to let it do any
3443 other checking that is needed. */
3448 lang_statement_union_type
*file
;
3450 const bfd_arch_info_type
*compatible
;
3452 for (file
= file_chain
.head
;
3453 file
!= (lang_statement_union_type
*) NULL
;
3454 file
= file
->input_statement
.next
)
3456 input_bfd
= file
->input_statement
.the_bfd
;
3457 compatible
= bfd_arch_get_compatible (input_bfd
,
3459 if (compatible
== NULL
)
3461 if (command_line
.warn_mismatch
)
3462 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3463 bfd_printable_name (input_bfd
), input_bfd
,
3464 bfd_printable_name (output_bfd
));
3468 bfd_error_handler_type pfn
= NULL
;
3470 /* If we aren't supposed to warn about mismatched input
3471 files, temporarily set the BFD error handler to a
3472 function which will do nothing. We still want to call
3473 bfd_merge_private_bfd_data, since it may set up
3474 information which is needed in the output file. */
3475 if (! command_line
.warn_mismatch
)
3476 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3477 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3479 if (command_line
.warn_mismatch
)
3480 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3483 if (! command_line
.warn_mismatch
)
3484 bfd_set_error_handler (pfn
);
3489 /* Look through all the global common symbols and attach them to the
3490 correct section. The -sort-common command line switch may be used
3491 to roughly sort the entries by size. */
3496 if (link_info
.relocateable
3497 && ! command_line
.force_common_definition
)
3500 if (! config
.sort_common
)
3501 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3506 for (power
= 4; power
>= 0; power
--)
3507 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3512 /* Place one common symbol in the correct section. */
3515 lang_one_common (h
, info
)
3516 struct bfd_link_hash_entry
*h
;
3519 unsigned int power_of_two
;
3522 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3523 ldfile_output_machine
);
3525 if (h
->type
!= bfd_link_hash_common
)
3529 power_of_two
= h
->u
.c
.p
->alignment_power
;
3531 if (config
.sort_common
3532 && power_of_two
< (unsigned int) *(int *) info
)
3535 section
= h
->u
.c
.p
->section
;
3537 /* Increase the size of the section. */
3538 section
->_cooked_size
= ALIGN_N ((section
->_cooked_size
+ opb
- 1) / opb
,
3539 (bfd_size_type
) (1 << power_of_two
)) * opb
;
3541 /* Adjust the alignment if necessary. */
3542 if (power_of_two
> section
->alignment_power
)
3543 section
->alignment_power
= power_of_two
;
3545 /* Change the symbol from common to defined. */
3546 h
->type
= bfd_link_hash_defined
;
3547 h
->u
.def
.section
= section
;
3548 h
->u
.def
.value
= section
->_cooked_size
;
3550 /* Increase the size of the section. */
3551 section
->_cooked_size
+= size
;
3553 /* Make sure the section is allocated in memory, and make sure that
3554 it is no longer a common section. */
3555 section
->flags
|= SEC_ALLOC
;
3556 section
->flags
&= ~SEC_IS_COMMON
;
3558 if (config
.map_file
!= NULL
)
3560 static boolean header_printed
;
3565 if (! header_printed
)
3567 minfo (_("\nAllocating common symbols\n"));
3568 minfo (_("Common symbol size file\n\n"));
3569 header_printed
= true;
3572 name
= demangle (h
->root
.string
);
3574 len
= strlen (name
);
3589 if (size
<= 0xffffffff)
3590 sprintf (buf
, "%lx", (unsigned long) size
);
3592 sprintf_vma (buf
, size
);
3602 minfo ("%B\n", section
->owner
);
3608 /* Run through the input files and ensure that every input section has
3609 somewhere to go. If one is found without a destination then create
3610 an input request and place it into the statement tree. */
3613 lang_place_orphans ()
3615 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3619 for (s
= file
->the_bfd
->sections
;
3620 s
!= (asection
*) NULL
;
3623 if (s
->output_section
== (asection
*) NULL
)
3625 /* This section of the file is not attatched, root
3626 around for a sensible place for it to go. */
3628 if (file
->just_syms_flag
)
3630 /* We are only retrieving symbol values from this
3631 file. We want the symbols to act as though the
3632 values in the file are absolute. */
3633 s
->output_section
= bfd_abs_section_ptr
;
3634 s
->output_offset
= s
->vma
;
3636 else if (strcmp (s
->name
, "COMMON") == 0)
3638 /* This is a lonely common section which must have
3639 come from an archive. We attach to the section
3640 with the wildcard. */
3641 if (! link_info
.relocateable
3642 || command_line
.force_common_definition
)
3644 if (default_common_section
== NULL
)
3647 /* This message happens when using the
3648 svr3.ifile linker script, so I have
3650 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3652 default_common_section
=
3653 lang_output_section_statement_lookup (".bss");
3656 wild_doit (&default_common_section
->children
, s
,
3657 default_common_section
, file
);
3660 else if (ldemul_place_orphan (file
, s
))
3664 lang_output_section_statement_type
*os
=
3665 lang_output_section_statement_lookup (s
->name
);
3667 wild_doit (&os
->children
, s
, os
, file
);
3675 lang_set_flags (ptr
, flags
, invert
)
3676 lang_memory_region_type
*ptr
;
3680 flagword
*ptr_flags
;
3682 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3688 *ptr_flags
|= SEC_ALLOC
;
3692 *ptr_flags
|= SEC_READONLY
;
3696 *ptr_flags
|= SEC_DATA
;
3700 *ptr_flags
|= SEC_CODE
;
3705 *ptr_flags
|= SEC_LOAD
;
3709 einfo (_("%P%F: invalid syntax in flags\n"));
3716 /* Call a function on each input file. This function will be called
3717 on an archive, but not on the elements. */
3720 lang_for_each_input_file (func
)
3721 void (*func
) PARAMS ((lang_input_statement_type
*));
3723 lang_input_statement_type
*f
;
3725 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3727 f
= (lang_input_statement_type
*) f
->next_real_file
)
3731 /* Call a function on each file. The function will be called on all
3732 the elements of an archive which are included in the link, but will
3733 not be called on the archive file itself. */
3736 lang_for_each_file (func
)
3737 void (*func
) PARAMS ((lang_input_statement_type
*));
3739 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3750 lang_for_each_input_section (func
)
3751 void (*func
) PARAMS ((bfd
*ab
, asection
*as
));
3753 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3757 for (s
= f
->the_bfd
->sections
;
3758 s
!= (asection
*) NULL
;
3761 func (f
->the_bfd
, s
);
3769 ldlang_add_file (entry
)
3770 lang_input_statement_type
*entry
;
3774 lang_statement_append (&file_chain
,
3775 (lang_statement_union_type
*) entry
,
3778 /* The BFD linker needs to have a list of all input BFDs involved in
3780 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3781 ASSERT (entry
->the_bfd
!= output_bfd
);
3782 for (pp
= &link_info
.input_bfds
;
3783 *pp
!= (bfd
*) NULL
;
3784 pp
= &(*pp
)->link_next
)
3786 *pp
= entry
->the_bfd
;
3787 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3788 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3790 /* Look through the sections and check for any which should not be
3791 included in the link. We need to do this now, so that we can
3792 notice when the backend linker tries to report multiple
3793 definition errors for symbols which are in sections we aren't
3794 going to link. FIXME: It might be better to entirely ignore
3795 symbols which are defined in sections which are going to be
3796 discarded. This would require modifying the backend linker for
3797 each backend which might set the SEC_LINK_ONCE flag. If we do
3798 this, we should probably handle SEC_EXCLUDE in the same way. */
3800 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3804 lang_add_output (name
, from_script
)
3808 /* Make -o on command line override OUTPUT in script. */
3809 if (had_output_filename
== false || !from_script
)
3811 output_filename
= name
;
3812 had_output_filename
= true;
3816 static lang_output_section_statement_type
*current_section
;
3828 for (l
= 0; l
< 32; l
++)
3830 if (i
>= (unsigned int) x
)
3838 lang_output_section_statement_type
*
3839 lang_enter_output_section_statement (output_section_statement_name
,
3840 address_exp
, sectype
, block_value
,
3841 align
, subalign
, ebase
)
3842 const char *output_section_statement_name
;
3843 etree_type
*address_exp
;
3844 enum section_type sectype
;
3845 bfd_vma block_value
;
3847 etree_type
*subalign
;
3850 lang_output_section_statement_type
*os
;
3854 lang_output_section_statement_lookup (output_section_statement_name
);
3856 /* Add this statement to tree. */
3858 add_statement (lang_output_section_statement_enum
,
3859 output_section_statement
);
3861 /* Make next things chain into subchain of this. */
3863 if (os
->addr_tree
== (etree_type
*) NULL
)
3865 os
->addr_tree
= address_exp
;
3867 os
->sectype
= sectype
;
3868 if (sectype
!= noload_section
)
3869 os
->flags
= SEC_NO_FLAGS
;
3871 os
->flags
= SEC_NEVER_LOAD
;
3872 os
->block_value
= block_value
? block_value
: 1;
3873 stat_ptr
= &os
->children
;
3875 os
->subsection_alignment
=
3876 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
3877 os
->section_alignment
=
3878 topower (exp_get_value_int (align
, -1, "section alignment", 0));
3880 os
->load_base
= ebase
;
3887 lang_output_statement_type
*new =
3888 new_stat (lang_output_statement
, stat_ptr
);
3890 new->name
= output_filename
;
3893 /* Reset the current counters in the regions. */
3896 reset_memory_regions ()
3898 lang_memory_region_type
*p
= lang_memory_region_list
;
3900 for (p
= lang_memory_region_list
;
3901 p
!= (lang_memory_region_type
*) NULL
;
3904 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3905 p
->current
= p
->origin
;
3909 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3910 as needed. SECTION may be NULL, in which case it is a wild card. */
3913 gc_section_callback (ptr
, section
, file
, data
)
3914 lang_wild_statement_type
*ptr
;
3916 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
3917 PTR data ATTRIBUTE_UNUSED
;
3919 /* If the wild pattern was marked KEEP, the member sections
3920 should be as well. */
3921 if (ptr
->keep_sections
)
3922 section
->flags
|= SEC_KEEP
;
3925 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3926 may be NULL, indicating that it is a wildcard. */
3929 lang_gc_wild (s
, section
, file
)
3930 lang_wild_statement_type
*s
;
3931 const char *section
;
3934 walk_wild (s
, section
, file
, gc_section_callback
, NULL
);
3937 /* Iterate over sections marking them against GC. */
3940 lang_gc_sections_1 (s
)
3941 lang_statement_union_type
*s
;
3943 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3945 switch (s
->header
.type
)
3947 case lang_wild_statement_enum
:
3948 lang_gc_wild (&s
->wild_statement
,
3949 s
->wild_statement
.section_name
,
3950 s
->wild_statement
.filename
);
3952 case lang_constructors_statement_enum
:
3953 lang_gc_sections_1 (constructor_list
.head
);
3955 case lang_output_section_statement_enum
:
3956 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
3958 case lang_group_statement_enum
:
3959 lang_gc_sections_1 (s
->group_statement
.children
.head
);
3970 struct bfd_link_hash_entry
*h
;
3971 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
3973 /* Keep all sections so marked in the link script. */
3975 lang_gc_sections_1 (statement_list
.head
);
3977 /* Keep all sections containing symbols undefined on the command-line.
3978 Handle the entry symbol at the same time. */
3980 if (entry_symbol
!= NULL
)
3982 fake_list_start
.next
= ldlang_undef_chain_list_head
;
3983 fake_list_start
.name
= (char *) entry_symbol
;
3984 ulist
= &fake_list_start
;
3987 ulist
= ldlang_undef_chain_list_head
;
3989 for (; ulist
; ulist
= ulist
->next
)
3991 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
3992 false, false, false);
3994 if (h
!= (struct bfd_link_hash_entry
*) NULL
3995 && (h
->type
== bfd_link_hash_defined
3996 || h
->type
== bfd_link_hash_defweak
)
3997 && ! bfd_is_abs_section (h
->u
.def
.section
))
3999 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4003 bfd_gc_sections (output_bfd
, &link_info
);
4009 lang_reasonable_defaults ();
4010 current_target
= default_target
;
4012 /* Open the output file. */
4013 lang_for_each_statement (ldlang_open_output
);
4015 ldemul_create_output_section_statements ();
4017 /* Add to the hash table all undefineds on the command line. */
4018 lang_place_undefineds ();
4020 already_linked_table_init ();
4022 /* Create a bfd for each input file. */
4023 current_target
= default_target
;
4024 open_input_bfds (statement_list
.head
, false);
4026 ldemul_after_open ();
4028 already_linked_table_free ();
4030 /* Make sure that we're not mixing architectures. We call this
4031 after all the input files have been opened, but before we do any
4032 other processing, so that any operations merge_private_bfd_data
4033 does on the output file will be known during the rest of the
4037 /* Handle .exports instead of a version script if we're told to do so. */
4038 if (command_line
.version_exports_section
)
4039 lang_do_version_exports_section ();
4041 /* Build all sets based on the information gathered from the input
4043 ldctor_build_sets ();
4045 /* Remove unreferenced sections if asked to. */
4046 if (command_line
.gc_sections
)
4047 lang_gc_sections ();
4049 /* Size up the common data. */
4052 /* Run through the contours of the script and attach input sections
4053 to the correct output sections. */
4054 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
4055 (lang_output_section_statement_type
*) NULL
);
4057 /* Find any sections not attached explicitly and handle them. */
4058 lang_place_orphans ();
4060 ldemul_before_allocation ();
4062 /* We must record the program headers before we try to fix the
4063 section positions, since they will affect SIZEOF_HEADERS. */
4064 lang_record_phdrs ();
4066 /* Now run around and relax if we can. */
4067 if (command_line
.relax
)
4069 /* First time round is a trial run to get the 'worst case'
4070 addresses of the objects if there was no relaxing. */
4071 lang_size_sections (statement_list
.head
,
4073 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4075 /* Keep relaxing until bfd_relax_section gives up. */
4078 reset_memory_regions ();
4080 relax_again
= false;
4082 /* Note: pe-dll.c does something like this also. If you find
4083 you need to change this code, you probably need to change
4084 pe-dll.c also. DJ */
4086 /* Do all the assignments with our current guesses as to
4088 lang_do_assignments (statement_list
.head
,
4090 (fill_type
) 0, (bfd_vma
) 0);
4092 /* Perform another relax pass - this time we know where the
4093 globals are, so can make better guess. */
4094 lang_size_sections (statement_list
.head
,
4096 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
4098 while (relax_again
);
4102 /* Size up the sections. */
4103 lang_size_sections (statement_list
.head
,
4105 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4108 /* See if anything special should be done now we know how big
4110 ldemul_after_allocation ();
4112 /* Fix any .startof. or .sizeof. symbols. */
4113 lang_set_startof ();
4115 /* Do all the assignments, now that we know the final resting places
4116 of all the symbols. */
4118 lang_do_assignments (statement_list
.head
,
4120 (fill_type
) 0, (bfd_vma
) 0);
4122 /* Make sure that the section addresses make sense. */
4123 if (! link_info
.relocateable
4124 && command_line
.check_section_addresses
)
4125 lang_check_section_addresses ();
4133 /* EXPORTED TO YACC */
4136 lang_add_wild (section_name
, sections_sorted
, filename
, filenames_sorted
,
4137 keep_sections
, exclude_filename_list
)
4138 const char *const section_name
;
4139 boolean sections_sorted
;
4140 const char *const filename
;
4141 boolean filenames_sorted
;
4142 boolean keep_sections
;
4143 struct name_list
*exclude_filename_list
;
4145 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
4148 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
4150 placed_commons
= true;
4152 if (filename
!= NULL
&& ! wildcardp (filename
))
4154 lang_has_input_file
= true;
4156 new->section_name
= section_name
;
4157 new->sections_sorted
= sections_sorted
;
4158 new->filename
= filename
;
4159 new->filenames_sorted
= filenames_sorted
;
4160 new->keep_sections
= keep_sections
;
4161 new->exclude_filename_list
= exclude_filename_list
;
4162 lang_list_init (&new->children
);
4166 lang_section_start (name
, address
)
4168 etree_type
*address
;
4170 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
4172 ad
->section_name
= name
;
4173 ad
->address
= address
;
4176 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4177 because of a -e argument on the command line, or zero if this is
4178 called by ENTRY in a linker script. Command line arguments take
4182 lang_add_entry (name
, cmdline
)
4186 if (entry_symbol
== NULL
4188 || ! entry_from_cmdline
)
4190 entry_symbol
= name
;
4191 entry_from_cmdline
= cmdline
;
4196 lang_add_target (name
)
4199 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4215 map_option_f
= true;
4226 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4233 lang_add_data (type
, exp
)
4235 union etree_union
*exp
;
4238 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4246 /* Create a new reloc statement. RELOC is the BFD relocation type to
4247 generate. HOWTO is the corresponding howto structure (we could
4248 look this up, but the caller has already done so). SECTION is the
4249 section to generate a reloc against, or NAME is the name of the
4250 symbol to generate a reloc against. Exactly one of SECTION and
4251 NAME must be NULL. ADDEND is an expression for the addend. */
4254 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4255 bfd_reloc_code_real_type reloc
;
4256 reloc_howto_type
*howto
;
4259 union etree_union
*addend
;
4261 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4265 p
->section
= section
;
4267 p
->addend_exp
= addend
;
4269 p
->addend_value
= 0;
4270 p
->output_section
= NULL
;
4274 lang_assignment_statement_type
*
4275 lang_add_assignment (exp
)
4278 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4286 lang_add_attribute (attribute
)
4287 enum statement_enum attribute
;
4289 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4296 if (startup_file
!= (char *) NULL
)
4298 einfo (_("%P%Fmultiple STARTUP files\n"));
4300 first_file
->filename
= name
;
4301 first_file
->local_sym_name
= name
;
4302 first_file
->real
= true;
4304 startup_file
= name
;
4311 lang_float_flag
= maybe
;
4315 lang_leave_output_section_statement (fill
, memspec
, phdrs
, lma_memspec
)
4317 const char *memspec
;
4318 struct lang_output_section_phdr_list
*phdrs
;
4319 const char *lma_memspec
;
4321 current_section
->fill
= fill
;
4322 current_section
->region
= lang_memory_region_lookup (memspec
);
4323 if (strcmp (lma_memspec
, "*default*") != 0)
4325 current_section
->lma_region
= lang_memory_region_lookup (lma_memspec
);
4326 /* If no runtime region has been given, but the load region has
4327 been, use the load region. */
4328 if (strcmp (memspec
, "*default*") == 0)
4329 current_section
->region
= lang_memory_region_lookup (lma_memspec
);
4331 current_section
->phdrs
= phdrs
;
4332 stat_ptr
= &statement_list
;
4335 /* Create an absolute symbol with the given name with the value of the
4336 address of first byte of the section named.
4338 If the symbol already exists, then do nothing. */
4341 lang_abs_symbol_at_beginning_of (secname
, name
)
4342 const char *secname
;
4345 struct bfd_link_hash_entry
*h
;
4347 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4348 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4349 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4351 if (h
->type
== bfd_link_hash_new
4352 || h
->type
== bfd_link_hash_undefined
)
4356 h
->type
= bfd_link_hash_defined
;
4358 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4359 if (sec
== (asection
*) NULL
)
4362 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4364 h
->u
.def
.section
= bfd_abs_section_ptr
;
4368 /* Create an absolute symbol with the given name with the value of the
4369 address of the first byte after the end of the section named.
4371 If the symbol already exists, then do nothing. */
4374 lang_abs_symbol_at_end_of (secname
, name
)
4375 const char *secname
;
4378 struct bfd_link_hash_entry
*h
;
4380 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4381 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4382 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4384 if (h
->type
== bfd_link_hash_new
4385 || h
->type
== bfd_link_hash_undefined
)
4389 h
->type
= bfd_link_hash_defined
;
4391 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4392 if (sec
== (asection
*) NULL
)
4395 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4396 + bfd_section_size (output_bfd
, sec
) /
4397 bfd_octets_per_byte (output_bfd
));
4399 h
->u
.def
.section
= bfd_abs_section_ptr
;
4404 lang_statement_append (list
, element
, field
)
4405 lang_statement_list_type
*list
;
4406 lang_statement_union_type
*element
;
4407 lang_statement_union_type
**field
;
4409 *(list
->tail
) = element
;
4413 /* Set the output format type. -oformat overrides scripts. */
4416 lang_add_output_format (format
, big
, little
, from_script
)
4422 if (output_target
== NULL
|| !from_script
)
4424 if (command_line
.endian
== ENDIAN_BIG
4427 else if (command_line
.endian
== ENDIAN_LITTLE
4431 output_target
= format
;
4435 /* Enter a group. This creates a new lang_group_statement, and sets
4436 stat_ptr to build new statements within the group. */
4441 lang_group_statement_type
*g
;
4443 g
= new_stat (lang_group_statement
, stat_ptr
);
4444 lang_list_init (&g
->children
);
4445 stat_ptr
= &g
->children
;
4448 /* Leave a group. This just resets stat_ptr to start writing to the
4449 regular list of statements again. Note that this will not work if
4450 groups can occur inside anything else which can adjust stat_ptr,
4451 but currently they can't. */
4456 stat_ptr
= &statement_list
;
4459 /* Add a new program header. This is called for each entry in a PHDRS
4460 command in a linker script. */
4463 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4471 struct lang_phdr
*n
, **pp
;
4473 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4476 n
->type
= exp_get_value_int (type
, 0, "program header type",
4477 lang_final_phase_enum
);
4478 n
->filehdr
= filehdr
;
4483 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4488 /* Record the program header information in the output BFD. FIXME: We
4489 should not be calling an ELF specific function here. */
4492 lang_record_phdrs ()
4496 struct lang_output_section_phdr_list
*last
;
4497 struct lang_phdr
*l
;
4498 lang_statement_union_type
*u
;
4501 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4503 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4510 for (u
= lang_output_section_statement
.head
;
4512 u
= u
->output_section_statement
.next
)
4514 lang_output_section_statement_type
*os
;
4515 struct lang_output_section_phdr_list
*pl
;
4517 os
= &u
->output_section_statement
;
4524 if (os
->sectype
== noload_section
4525 || os
->bfd_section
== NULL
4526 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4531 if (os
->bfd_section
== NULL
)
4534 for (; pl
!= NULL
; pl
= pl
->next
)
4536 if (strcmp (pl
->name
, l
->name
) == 0)
4541 secs
= ((asection
**)
4542 xrealloc (secs
, alc
* sizeof (asection
*)));
4544 secs
[c
] = os
->bfd_section
;
4551 if (l
->flags
== NULL
)
4554 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4555 lang_final_phase_enum
);
4560 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4561 lang_final_phase_enum
);
4563 if (! bfd_record_phdr (output_bfd
, l
->type
,
4564 l
->flags
== NULL
? false : true,
4566 l
->at
== NULL
? false : true,
4567 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4568 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4573 /* Make sure all the phdr assignments succeeded. */
4574 for (u
= lang_output_section_statement
.head
;
4576 u
= u
->output_section_statement
.next
)
4578 struct lang_output_section_phdr_list
*pl
;
4580 if (u
->output_section_statement
.bfd_section
== NULL
)
4583 for (pl
= u
->output_section_statement
.phdrs
;
4586 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4587 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4588 u
->output_section_statement
.name
, pl
->name
);
4592 /* Record a list of sections which may not be cross referenced. */
4595 lang_add_nocrossref (l
)
4596 struct lang_nocrossref
*l
;
4598 struct lang_nocrossrefs
*n
;
4600 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4601 n
->next
= nocrossref_list
;
4603 nocrossref_list
= n
;
4605 /* Set notice_all so that we get informed about all symbols. */
4606 link_info
.notice_all
= true;
4609 /* Overlay handling. We handle overlays with some static variables. */
4611 /* The overlay virtual address. */
4612 static etree_type
*overlay_vma
;
4614 /* The overlay load address. */
4615 static etree_type
*overlay_lma
;
4617 /* Whether nocrossrefs is set for this overlay. */
4618 static int overlay_nocrossrefs
;
4620 /* An expression for the maximum section size seen so far. */
4621 static etree_type
*overlay_max
;
4623 /* A list of all the sections in this overlay. */
4625 struct overlay_list
{
4626 struct overlay_list
*next
;
4627 lang_output_section_statement_type
*os
;
4630 static struct overlay_list
*overlay_list
;
4632 /* Start handling an overlay. */
4635 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4636 etree_type
*vma_expr
;
4637 etree_type
*lma_expr
;
4640 /* The grammar should prevent nested overlays from occurring. */
4641 ASSERT (overlay_vma
== NULL
4642 && overlay_lma
== NULL
4643 && overlay_list
== NULL
4644 && overlay_max
== NULL
);
4646 overlay_vma
= vma_expr
;
4647 overlay_lma
= lma_expr
;
4648 overlay_nocrossrefs
= nocrossrefs
;
4651 /* Start a section in an overlay. We handle this by calling
4652 lang_enter_output_section_statement with the correct VMA and LMA. */
4655 lang_enter_overlay_section (name
)
4658 struct overlay_list
*n
;
4661 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4662 0, 0, 0, overlay_lma
);
4664 /* If this is the first section, then base the VMA and LMA of future
4665 sections on this one. This will work correctly even if `.' is
4666 used in the addresses. */
4667 if (overlay_list
== NULL
)
4669 overlay_vma
= exp_nameop (ADDR
, name
);
4670 overlay_lma
= exp_nameop (LOADADDR
, name
);
4673 /* Remember the section. */
4674 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4675 n
->os
= current_section
;
4676 n
->next
= overlay_list
;
4679 size
= exp_nameop (SIZEOF
, name
);
4681 /* Adjust the LMA for the next section. */
4682 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4684 /* Arrange to work out the maximum section end address. */
4685 if (overlay_max
== NULL
)
4688 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4691 /* Finish a section in an overlay. There isn't any special to do
4695 lang_leave_overlay_section (fill
, phdrs
)
4697 struct lang_output_section_phdr_list
*phdrs
;
4704 name
= current_section
->name
;
4706 lang_leave_output_section_statement (fill
, "*default*",
4707 phdrs
, "*default*");
4709 /* Define the magic symbols. */
4711 clean
= xmalloc (strlen (name
) + 1);
4713 for (s1
= name
; *s1
!= '\0'; s1
++)
4714 if (isalnum ((unsigned char) *s1
) || *s1
== '_')
4718 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4719 sprintf (buf
, "__load_start_%s", clean
);
4720 lang_add_assignment (exp_assop ('=', buf
,
4721 exp_nameop (LOADADDR
, name
)));
4723 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4724 sprintf (buf
, "__load_stop_%s", clean
);
4725 lang_add_assignment (exp_assop ('=', buf
,
4727 exp_nameop (LOADADDR
, name
),
4728 exp_nameop (SIZEOF
, name
))));
4733 /* Finish an overlay. If there are any overlay wide settings, this
4734 looks through all the sections in the overlay and sets them. */
4737 lang_leave_overlay (fill
, memspec
, phdrs
, lma_memspec
)
4739 const char *memspec
;
4740 struct lang_output_section_phdr_list
*phdrs
;
4741 const char *lma_memspec
;
4743 lang_memory_region_type
*region
;
4744 lang_memory_region_type
*lma_region
;
4745 struct overlay_list
*l
;
4746 struct lang_nocrossref
*nocrossref
;
4748 if (memspec
== NULL
)
4751 region
= lang_memory_region_lookup (memspec
);
4753 if (lma_memspec
== NULL
)
4756 lma_region
= lang_memory_region_lookup (lma_memspec
);
4763 struct overlay_list
*next
;
4765 if (fill
!= 0 && l
->os
->fill
== 0)
4767 if (region
!= NULL
&& l
->os
->region
== NULL
)
4768 l
->os
->region
= region
;
4769 if (lma_region
!= NULL
&& l
->os
->lma_region
== NULL
)
4770 l
->os
->lma_region
= lma_region
;
4771 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4772 l
->os
->phdrs
= phdrs
;
4774 if (overlay_nocrossrefs
)
4776 struct lang_nocrossref
*nc
;
4778 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4779 nc
->name
= l
->os
->name
;
4780 nc
->next
= nocrossref
;
4789 if (nocrossref
!= NULL
)
4790 lang_add_nocrossref (nocrossref
);
4792 /* Update . for the end of the overlay. */
4793 lang_add_assignment (exp_assop ('=', ".",
4794 exp_binop ('+', overlay_vma
, overlay_max
)));
4798 overlay_nocrossrefs
= 0;
4799 overlay_list
= NULL
;
4803 /* Version handling. This is only useful for ELF. */
4805 /* This global variable holds the version tree that we build. */
4807 struct bfd_elf_version_tree
*lang_elf_version_info
;
4810 lang_vers_match_lang_c (expr
, sym
)
4811 struct bfd_elf_version_expr
*expr
;
4814 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4816 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4820 lang_vers_match_lang_cplusplus (expr
, sym
)
4821 struct bfd_elf_version_expr
*expr
;
4827 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4830 alt_sym
= cplus_demangle (sym
, /* DMGL_NO_TPARAMS */ 0);
4833 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4834 Should we early out false in this case? */
4835 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4839 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4847 lang_vers_match_lang_java (expr
, sym
)
4848 struct bfd_elf_version_expr
*expr
;
4854 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4857 alt_sym
= cplus_demangle (sym
, DMGL_JAVA
);
4860 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4861 Should we early out false in this case? */
4862 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4866 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4873 /* This is called for each variable name or match expression. */
4875 struct bfd_elf_version_expr
*
4876 lang_new_vers_regex (orig
, new, lang
)
4877 struct bfd_elf_version_expr
*orig
;
4881 struct bfd_elf_version_expr
*ret
;
4883 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4887 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4888 ret
->match
= lang_vers_match_lang_c
;
4889 else if (strcasecmp (lang
, "C++") == 0)
4890 ret
->match
= lang_vers_match_lang_cplusplus
;
4891 else if (strcasecmp (lang
, "Java") == 0)
4892 ret
->match
= lang_vers_match_lang_java
;
4895 einfo (_("%X%P: unknown language `%s' in version information\n"),
4897 ret
->match
= lang_vers_match_lang_c
;
4903 /* This is called for each set of variable names and match
4906 struct bfd_elf_version_tree
*
4907 lang_new_vers_node (globals
, locals
)
4908 struct bfd_elf_version_expr
*globals
;
4909 struct bfd_elf_version_expr
*locals
;
4911 struct bfd_elf_version_tree
*ret
;
4913 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
4917 ret
->globals
= globals
;
4918 ret
->locals
= locals
;
4920 ret
->name_indx
= (unsigned int) -1;
4925 /* This static variable keeps track of version indices. */
4927 static int version_index
;
4929 /* This is called when we know the name and dependencies of the
4933 lang_register_vers_node (name
, version
, deps
)
4935 struct bfd_elf_version_tree
*version
;
4936 struct bfd_elf_version_deps
*deps
;
4938 struct bfd_elf_version_tree
*t
, **pp
;
4939 struct bfd_elf_version_expr
*e1
;
4941 /* Make sure this node has a unique name. */
4942 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4943 if (strcmp (t
->name
, name
) == 0)
4944 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
4946 /* Check the global and local match names, and make sure there
4947 aren't any duplicates. */
4949 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
4951 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4953 struct bfd_elf_version_expr
*e2
;
4955 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
4956 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4957 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4962 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
4964 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4966 struct bfd_elf_version_expr
*e2
;
4968 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
4969 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4970 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4975 version
->deps
= deps
;
4976 version
->name
= name
;
4978 version
->vernum
= version_index
;
4980 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4985 /* This is called when we see a version dependency. */
4987 struct bfd_elf_version_deps
*
4988 lang_add_vers_depend (list
, name
)
4989 struct bfd_elf_version_deps
*list
;
4992 struct bfd_elf_version_deps
*ret
;
4993 struct bfd_elf_version_tree
*t
;
4995 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
4998 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5000 if (strcmp (t
->name
, name
) == 0)
5002 ret
->version_needed
= t
;
5007 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5013 lang_do_version_exports_section ()
5015 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5017 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5019 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5026 len
= bfd_section_size (is
->the_bfd
, sec
);
5027 contents
= xmalloc (len
);
5028 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5029 einfo (_("%X%P: unable to read .exports section contents"), sec
);
5032 while (p
< contents
+ len
)
5034 greg
= lang_new_vers_regex (greg
, p
, NULL
);
5035 p
= strchr (p
, '\0') + 1;
5038 /* Do not free the contents, as we used them creating the regex. */
5040 /* Do not include this section in the link. */
5041 bfd_set_section_flags (is
->the_bfd
, sec
,
5042 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5045 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
5046 lang_register_vers_node (command_line
.version_exports_section
,
5047 lang_new_vers_node (greg
, lreg
), NULL
);