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
**, fill_type
,
124 unsigned int, asection
*, bfd_vma
));
125 static bfd_vma size_input_section
126 PARAMS ((lang_statement_union_type
**, lang_output_section_statement_type
*,
127 fill_type
, bfd_vma
, boolean
));
128 static void lang_finish
PARAMS ((void));
129 static void ignore_bfd_errors
PARAMS ((const char *, ...));
130 static void lang_check
PARAMS ((void));
131 static void lang_common
PARAMS ((void));
132 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
133 static void lang_place_orphans
PARAMS ((void));
134 static int topower
PARAMS ((int));
135 static void lang_set_startof
PARAMS ((void));
136 static void reset_memory_regions
PARAMS ((void));
137 static void gc_section_callback
138 PARAMS ((lang_wild_statement_type
*, asection
*,
139 lang_input_statement_type
*, PTR
));
140 static void lang_record_phdrs
PARAMS ((void));
141 static void lang_gc_wild
142 PARAMS ((lang_wild_statement_type
*, const char *, const char *));
143 static void lang_gc_sections_1
PARAMS ((lang_statement_union_type
*));
144 static void lang_gc_sections
PARAMS ((void));
145 static int lang_vers_match_lang_c
146 PARAMS ((struct bfd_elf_version_expr
*, const char *));
147 static int lang_vers_match_lang_cplusplus
148 PARAMS ((struct bfd_elf_version_expr
*, const char *));
149 static int lang_vers_match_lang_java
150 PARAMS ((struct bfd_elf_version_expr
*, const char *));
151 static void lang_do_version_exports_section
PARAMS ((void));
152 static void lang_check_section_addresses
PARAMS ((void));
153 static void os_region_check
154 PARAMS ((lang_output_section_statement_type
*,
155 struct memory_region_struct
*, etree_type
*, bfd_vma
));
157 typedef void (*callback_t
) PARAMS ((lang_wild_statement_type
*,
158 asection
*, lang_input_statement_type
*,
160 static void walk_wild
161 PARAMS ((lang_wild_statement_type
*, const char *, const char *,
163 static void walk_wild_section
164 PARAMS ((lang_wild_statement_type
*, const char *,
165 lang_input_statement_type
*, callback_t
, PTR
));
166 static void walk_wild_file
167 PARAMS ((lang_wild_statement_type
*, const char *,
168 lang_input_statement_type
*, callback_t
, PTR
));
170 static int get_target
PARAMS ((const bfd_target
*, PTR
));
171 static void stricpy
PARAMS ((char *, char *));
172 static void strcut
PARAMS ((char *, char *));
173 static int name_compare
PARAMS ((char *, char *));
174 static int closest_target_match
PARAMS ((const bfd_target
*, PTR
));
175 static char * get_first_input_target
PARAMS ((void));
178 lang_output_section_statement_type
*abs_output_section
;
179 lang_statement_list_type lang_output_section_statement
;
180 lang_statement_list_type
*stat_ptr
= &statement_list
;
181 lang_statement_list_type file_chain
= { NULL
, NULL
};
182 const char *entry_symbol
= NULL
;
183 boolean entry_from_cmdline
;
184 boolean lang_has_input_file
= false;
185 boolean had_output_filename
= false;
186 boolean lang_float_flag
= false;
187 boolean delete_output_file_on_failure
= false;
188 struct lang_nocrossrefs
*nocrossref_list
;
190 etree_type
*base
; /* Relocation base - or null */
192 #if defined(__STDC__) || defined(ALMOST_STDC)
193 #define cat(a,b) a##b
195 #define cat(a,b) a/**/b
198 #define new_stat(x, y) (cat (x, _type)*) new_statement (cat (x, _enum), sizeof (cat (x, _type)), y)
200 #define outside_section_address(q) ((q)->output_offset + (q)->output_section->vma)
202 #define outside_symbol_address(q) ((q)->value + outside_section_address (q->section))
204 #define SECTION_NAME_MAP_LENGTH (16)
210 return obstack_alloc (&stat_obstack
, size
);
213 /* Generic traversal routines for finding matching sections. */
216 walk_wild_section (ptr
, section
, file
, callback
, data
)
217 lang_wild_statement_type
*ptr
;
219 lang_input_statement_type
*file
;
223 /* Don't process sections from files which were excluded. */
224 if (ptr
->exclude_filename_list
!= NULL
)
226 struct name_list
*list_tmp
;
227 for (list_tmp
= ptr
->exclude_filename_list
; list_tmp
; list_tmp
= list_tmp
->next
)
231 if (wildcardp (list_tmp
->name
))
232 match
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0 ? true : false;
234 match
= strcmp (list_tmp
->name
, file
->filename
) == 0 ? true : false;
241 if (file
->just_syms_flag
== false)
243 register asection
*s
;
249 wildcard
= wildcardp (section
);
251 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
261 name
= bfd_get_section_name (file
->the_bfd
, s
);
263 match
= fnmatch (section
, name
, 0) == 0 ? true : false;
265 match
= strcmp (section
, name
) == 0 ? true : false;
269 (*callback
) (ptr
, s
, file
, data
);
274 /* Handle a wild statement for a single file F. */
277 walk_wild_file (s
, section
, f
, callback
, data
)
278 lang_wild_statement_type
*s
;
280 lang_input_statement_type
*f
;
284 if (f
->the_bfd
== NULL
285 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
286 walk_wild_section (s
, section
, f
, callback
, data
);
291 /* This is an archive file. We must map each member of the
292 archive separately. */
293 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
294 while (member
!= NULL
)
296 /* When lookup_name is called, it will call the add_symbols
297 entry point for the archive. For each element of the
298 archive which is included, BFD will call ldlang_add_file,
299 which will set the usrdata field of the member to the
300 lang_input_statement. */
301 if (member
->usrdata
!= NULL
)
303 walk_wild_section (s
, section
,
304 (lang_input_statement_type
*) member
->usrdata
,
308 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
314 walk_wild (s
, section
, file
, callback
, data
)
315 lang_wild_statement_type
*s
;
321 if (file
== (char *) NULL
)
323 /* Perform the iteration over all files in the list. */
324 LANG_FOR_EACH_INPUT_STATEMENT (f
)
326 walk_wild_file (s
, section
, f
, callback
, data
);
329 else if (wildcardp (file
))
331 LANG_FOR_EACH_INPUT_STATEMENT (f
)
333 if (fnmatch (file
, f
->filename
, FNM_FILE_NAME
) == 0)
334 walk_wild_file (s
, section
, f
, callback
, data
);
339 lang_input_statement_type
*f
;
341 /* Perform the iteration over a single file. */
342 f
= lookup_name (file
);
343 walk_wild_file (s
, section
, f
, callback
, data
);
347 /* lang_for_each_statement walks the parse tree and calls the provided
348 function for each node. */
351 lang_for_each_statement_worker (func
, s
)
352 void (*func
) PARAMS ((lang_statement_union_type
*));
353 lang_statement_union_type
*s
;
355 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
359 switch (s
->header
.type
)
361 case lang_constructors_statement_enum
:
362 lang_for_each_statement_worker (func
, constructor_list
.head
);
364 case lang_output_section_statement_enum
:
365 lang_for_each_statement_worker
367 s
->output_section_statement
.children
.head
);
369 case lang_wild_statement_enum
:
370 lang_for_each_statement_worker
372 s
->wild_statement
.children
.head
);
374 case lang_group_statement_enum
:
375 lang_for_each_statement_worker (func
,
376 s
->group_statement
.children
.head
);
378 case lang_data_statement_enum
:
379 case lang_reloc_statement_enum
:
380 case lang_object_symbols_statement_enum
:
381 case lang_output_statement_enum
:
382 case lang_target_statement_enum
:
383 case lang_input_section_enum
:
384 case lang_input_statement_enum
:
385 case lang_assignment_statement_enum
:
386 case lang_padding_statement_enum
:
387 case lang_address_statement_enum
:
388 case lang_fill_statement_enum
:
398 lang_for_each_statement (func
)
399 void (*func
) PARAMS ((lang_statement_union_type
*));
401 lang_for_each_statement_worker (func
, statement_list
.head
);
404 /*----------------------------------------------------------------------*/
407 lang_list_init (list
)
408 lang_statement_list_type
*list
;
410 list
->head
= (lang_statement_union_type
*) NULL
;
411 list
->tail
= &list
->head
;
414 /* Build a new statement node for the parse tree. */
416 static lang_statement_union_type
*
417 new_statement (type
, size
, list
)
418 enum statement_enum type
;
420 lang_statement_list_type
*list
;
422 lang_statement_union_type
*new = (lang_statement_union_type
*)
425 new->header
.type
= type
;
426 new->header
.next
= (lang_statement_union_type
*) NULL
;
427 lang_statement_append (list
, new, &new->header
.next
);
431 /* Build a new input file node for the language. There are several
432 ways in which we treat an input file, eg, we only look at symbols,
433 or prefix it with a -l etc.
435 We can be supplied with requests for input files more than once;
436 they may, for example be split over serveral lines like foo.o(.text)
437 foo.o(.data) etc, so when asked for a file we check that we havn't
438 got it already so we don't duplicate the bfd. */
440 static lang_input_statement_type
*
441 new_afile (name
, file_type
, target
, add_to_list
)
443 lang_input_file_enum_type file_type
;
447 lang_input_statement_type
*p
;
450 p
= new_stat (lang_input_statement
, stat_ptr
);
453 p
= ((lang_input_statement_type
*)
454 stat_alloc (sizeof (lang_input_statement_type
)));
455 p
->header
.next
= NULL
;
458 lang_has_input_file
= true;
462 case lang_input_file_is_symbols_only_enum
:
464 p
->is_archive
= false;
466 p
->local_sym_name
= name
;
467 p
->just_syms_flag
= true;
468 p
->search_dirs_flag
= false;
470 case lang_input_file_is_fake_enum
:
472 p
->is_archive
= false;
474 p
->local_sym_name
= name
;
475 p
->just_syms_flag
= false;
476 p
->search_dirs_flag
= false;
478 case lang_input_file_is_l_enum
:
479 p
->is_archive
= true;
482 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
483 p
->just_syms_flag
= false;
484 p
->search_dirs_flag
= true;
486 case lang_input_file_is_marker_enum
:
488 p
->is_archive
= false;
490 p
->local_sym_name
= name
;
491 p
->just_syms_flag
= false;
492 p
->search_dirs_flag
= true;
494 case lang_input_file_is_search_file_enum
:
496 p
->is_archive
= false;
498 p
->local_sym_name
= name
;
499 p
->just_syms_flag
= false;
500 p
->search_dirs_flag
= true;
502 case lang_input_file_is_file_enum
:
504 p
->is_archive
= false;
506 p
->local_sym_name
= name
;
507 p
->just_syms_flag
= false;
508 p
->search_dirs_flag
= false;
513 p
->the_bfd
= (bfd
*) NULL
;
514 p
->asymbols
= (asymbol
**) NULL
;
515 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
516 p
->next
= (lang_statement_union_type
*) NULL
;
518 p
->dynamic
= config
.dynamic_link
;
519 p
->whole_archive
= whole_archive
;
521 lang_statement_append (&input_file_chain
,
522 (lang_statement_union_type
*) p
,
527 lang_input_statement_type
*
528 lang_add_input_file (name
, file_type
, target
)
530 lang_input_file_enum_type file_type
;
533 lang_has_input_file
= true;
534 return new_afile (name
, file_type
, target
, true);
537 /* Build enough state so that the parser can build its tree. */
542 obstack_begin (&stat_obstack
, 1000);
544 stat_ptr
= &statement_list
;
546 lang_list_init (stat_ptr
);
548 lang_list_init (&input_file_chain
);
549 lang_list_init (&lang_output_section_statement
);
550 lang_list_init (&file_chain
);
551 first_file
= lang_add_input_file ((char *) NULL
,
552 lang_input_file_is_marker_enum
,
555 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
557 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
561 /*----------------------------------------------------------------------
562 A region is an area of memory declared with the
563 MEMORY { name:org=exp, len=exp ... }
566 We maintain a list of all the regions here.
568 If no regions are specified in the script, then the default is used
569 which is created when looked up to be the entire data space. */
571 static lang_memory_region_type
*lang_memory_region_list
;
572 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
574 lang_memory_region_type
*
575 lang_memory_region_lookup (name
)
576 const char *const name
;
578 lang_memory_region_type
*p
;
580 for (p
= lang_memory_region_list
;
581 p
!= (lang_memory_region_type
*) NULL
;
584 if (strcmp (p
->name
, name
) == 0)
591 /* This code used to always use the first region in the list as the
592 default region. I changed it to instead use a region
593 encompassing all of memory as the default region. This permits
594 NOLOAD sections to work reasonably without requiring a region.
595 People should specify what region they mean, if they really want
597 if (strcmp (name
, "*default*") == 0)
599 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
601 return lang_memory_region_list
;
607 lang_memory_region_type
*new =
608 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
610 new->name
= buystring (name
);
611 new->next
= (lang_memory_region_type
*) NULL
;
613 *lang_memory_region_list_tail
= new;
614 lang_memory_region_list_tail
= &new->next
;
618 new->length
= ~(bfd_size_type
) 0;
620 new->had_full_message
= false;
626 static lang_memory_region_type
*
627 lang_memory_default (section
)
630 lang_memory_region_type
*p
;
632 flagword sec_flags
= section
->flags
;
634 /* Override SEC_DATA to mean a writable section. */
635 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
636 sec_flags
|= SEC_DATA
;
638 for (p
= lang_memory_region_list
;
639 p
!= (lang_memory_region_type
*) NULL
;
642 if ((p
->flags
& sec_flags
) != 0
643 && (p
->not_flags
& sec_flags
) == 0)
648 return lang_memory_region_lookup ("*default*");
651 lang_output_section_statement_type
*
652 lang_output_section_find (name
)
653 const char *const name
;
655 lang_statement_union_type
*u
;
656 lang_output_section_statement_type
*lookup
;
658 for (u
= lang_output_section_statement
.head
;
659 u
!= (lang_statement_union_type
*) NULL
;
662 lookup
= &u
->output_section_statement
;
663 if (strcmp (name
, lookup
->name
) == 0)
668 return (lang_output_section_statement_type
*) NULL
;
671 lang_output_section_statement_type
*
672 lang_output_section_statement_lookup (name
)
673 const char *const name
;
675 lang_output_section_statement_type
*lookup
;
677 lookup
= lang_output_section_find (name
);
678 if (lookup
== (lang_output_section_statement_type
*) NULL
)
681 lookup
= (lang_output_section_statement_type
*)
682 new_stat (lang_output_section_statement
, stat_ptr
);
683 lookup
->region
= (lang_memory_region_type
*) NULL
;
684 lookup
->lma_region
= (lang_memory_region_type
*) NULL
;
686 lookup
->block_value
= 1;
689 lookup
->next
= (lang_statement_union_type
*) NULL
;
690 lookup
->bfd_section
= (asection
*) NULL
;
691 lookup
->processed
= false;
692 lookup
->sectype
= normal_section
;
693 lookup
->addr_tree
= (etree_type
*) NULL
;
694 lang_list_init (&lookup
->children
);
696 lookup
->memspec
= (const char *) NULL
;
698 lookup
->subsection_alignment
= -1;
699 lookup
->section_alignment
= -1;
700 lookup
->load_base
= (union etree_union
*) NULL
;
701 lookup
->phdrs
= NULL
;
703 lang_statement_append (&lang_output_section_statement
,
704 (lang_statement_union_type
*) lookup
,
711 lang_map_flags (flag
)
714 if (flag
& SEC_ALLOC
)
720 if (flag
& SEC_READONLY
)
733 lang_memory_region_type
*m
;
735 minfo (_("\nMemory Configuration\n\n"));
736 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
737 _("Name"), _("Origin"), _("Length"), _("Attributes"));
739 for (m
= lang_memory_region_list
;
740 m
!= (lang_memory_region_type
*) NULL
;
746 fprintf (config
.map_file
, "%-16s ", m
->name
);
748 sprintf_vma (buf
, m
->origin
);
749 minfo ("0x%s ", buf
);
757 minfo ("0x%V", m
->length
);
758 if (m
->flags
|| m
->not_flags
)
766 lang_map_flags (m
->flags
);
772 lang_map_flags (m
->not_flags
);
779 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
784 /* Initialize an output section. */
788 lang_output_section_statement_type
*s
;
790 section_userdata_type
*new;
792 if (s
->bfd_section
!= NULL
)
795 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
796 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME
);
798 new = ((section_userdata_type
*)
799 stat_alloc (sizeof (section_userdata_type
)));
801 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
802 if (s
->bfd_section
== (asection
*) NULL
)
803 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
804 if (s
->bfd_section
== (asection
*) NULL
)
806 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
807 output_bfd
->xvec
->name
, s
->name
);
809 s
->bfd_section
->output_section
= s
->bfd_section
;
811 /* We initialize an output sections output offset to minus its own
812 vma to allow us to output a section through itself. */
813 s
->bfd_section
->output_offset
= 0;
814 get_userdata (s
->bfd_section
) = (PTR
) new;
816 /* If there is a base address, make sure that any sections it might
817 mention are initialized. */
818 if (s
->addr_tree
!= NULL
)
819 exp_init_os (s
->addr_tree
);
822 /* Make sure that all output sections mentioned in an expression are
829 switch (exp
->type
.node_class
)
832 exp_init_os (exp
->assign
.src
);
836 exp_init_os (exp
->binary
.lhs
);
837 exp_init_os (exp
->binary
.rhs
);
841 exp_init_os (exp
->trinary
.cond
);
842 exp_init_os (exp
->trinary
.lhs
);
843 exp_init_os (exp
->trinary
.rhs
);
847 exp_init_os (exp
->unary
.child
);
851 switch (exp
->type
.node_code
)
857 lang_output_section_statement_type
*os
;
859 os
= lang_output_section_find (exp
->name
.name
);
860 if (os
!= NULL
&& os
->bfd_section
== NULL
)
871 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
872 once into the output. This routine checks each section, and
873 arrange to discard it if a section of the same name has already
874 been linked. If the section has COMDAT information, then it uses
875 that to decide whether the section should be included. This code
876 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
877 that is, it does not depend solely upon the section name.
878 section_already_linked is called via bfd_map_over_sections. */
880 /* This is the shape of the elements inside the already_linked hash
881 table. It maps a name onto a list of already_linked elements with
882 the same name. It's possible to get more than one element in a
883 list if the COMDAT sections have different names. */
885 struct already_linked_hash_entry
887 struct bfd_hash_entry root
;
888 struct already_linked
*entry
;
891 struct already_linked
893 struct already_linked
*next
;
897 /* The hash table. */
899 static struct bfd_hash_table already_linked_table
;
902 section_already_linked (abfd
, sec
, data
)
907 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
910 struct already_linked
*l
;
911 struct already_linked_hash_entry
*already_linked_list
;
913 /* If we are only reading symbols from this object, then we want to
914 discard all sections. */
915 if (entry
->just_syms_flag
)
917 sec
->output_section
= bfd_abs_section_ptr
;
918 sec
->output_offset
= sec
->vma
;
922 flags
= bfd_get_section_flags (abfd
, sec
);
924 if ((flags
& SEC_LINK_ONCE
) == 0)
927 /* FIXME: When doing a relocateable link, we may have trouble
928 copying relocations in other sections that refer to local symbols
929 in the section being discarded. Those relocations will have to
930 be converted somehow; as of this writing I'm not sure that any of
931 the backends handle that correctly.
933 It is tempting to instead not discard link once sections when
934 doing a relocateable link (technically, they should be discarded
935 whenever we are building constructors). However, that fails,
936 because the linker winds up combining all the link once sections
937 into a single large link once section, which defeats the purpose
938 of having link once sections in the first place.
940 Also, not merging link once sections in a relocateable link
941 causes trouble for MIPS ELF, which relies in link once semantics
942 to handle the .reginfo section correctly. */
944 name
= bfd_get_section_name (abfd
, sec
);
946 already_linked_list
=
947 ((struct already_linked_hash_entry
*)
948 bfd_hash_lookup (&already_linked_table
, name
, true, false));
950 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
952 if (sec
->comdat
== NULL
953 || l
->sec
->comdat
== NULL
954 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
956 /* The section has already been linked. See if we should
958 switch (flags
& SEC_LINK_DUPLICATES
)
963 case SEC_LINK_DUPLICATES_DISCARD
:
966 case SEC_LINK_DUPLICATES_ONE_ONLY
:
967 if (sec
->comdat
== NULL
)
968 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
971 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
972 abfd
, name
, sec
->comdat
->name
);
975 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
976 /* FIXME: We should really dig out the contents of both
977 sections and memcmp them. The COFF/PE spec says that
978 the Microsoft linker does not implement this
979 correctly, so I'm not going to bother doing it
982 case SEC_LINK_DUPLICATES_SAME_SIZE
:
983 if (bfd_section_size (abfd
, sec
)
984 != bfd_section_size (l
->sec
->owner
, l
->sec
))
985 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
990 /* Set the output_section field so that wild_doit does not
991 create a lang_input_section structure for this section.
992 Since there might be a symbol in the section being
993 discarded, we must retain a pointer to the section which
994 we are really going to use. */
995 sec
->output_section
= bfd_abs_section_ptr
;
996 sec
->kept_section
= l
->sec
;
1002 /* This is the first section with this name. Record it. Allocate
1003 the memory from the same obstack as the hash table is kept in. */
1005 l
= ((struct already_linked
*)
1006 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1009 l
->next
= already_linked_list
->entry
;
1010 already_linked_list
->entry
= l
;
1013 /* Support routines for the hash table used by section_already_linked,
1014 initialize the table, fill in an entry and remove the table. */
1016 static struct bfd_hash_entry
*
1017 already_linked_newfunc (entry
, table
, string
)
1018 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1019 struct bfd_hash_table
*table
;
1020 const char *string ATTRIBUTE_UNUSED
;
1022 struct already_linked_hash_entry
*ret
=
1023 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1027 return (struct bfd_hash_entry
*) ret
;
1031 already_linked_table_init ()
1033 if (! bfd_hash_table_init_n (&already_linked_table
,
1034 already_linked_newfunc
,
1036 einfo (_("%P%F: Failed to create hash table\n"));
1040 already_linked_table_free ()
1042 bfd_hash_table_free (&already_linked_table
);
1045 /* The wild routines.
1047 These expand statements like *(.text) and foo.o to a list of
1048 explicit actions, like foo.o(.text), bar.o(.text) and
1049 foo.o(.text, .data). */
1051 /* Return true if the PATTERN argument is a wildcard pattern.
1052 Although backslashes are treated specially if a pattern contains
1053 wildcards, we do not consider the mere presence of a backslash to
1054 be enough to cause the the pattern to be treated as a wildcard.
1055 That lets us handle DOS filenames more naturally. */
1059 const char *pattern
;
1063 for (s
= pattern
; *s
!= '\0'; ++s
)
1071 /* Add SECTION to the output section OUTPUT. Do this by creating a
1072 lang_input_section statement which is placed at PTR. FILE is the
1073 input file which holds SECTION. */
1076 wild_doit (ptr
, section
, output
, file
)
1077 lang_statement_list_type
*ptr
;
1079 lang_output_section_statement_type
*output
;
1080 lang_input_statement_type
*file
;
1085 flags
= bfd_get_section_flags (section
->owner
, section
);
1089 /* If we are doing a final link, discard sections marked with
1091 if (! link_info
.relocateable
1092 && (flags
& SEC_EXCLUDE
) != 0)
1095 /* Discard input sections which are assigned to a section named
1096 DISCARD_SECTION_NAME. */
1097 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1100 /* Discard debugging sections if we are stripping debugging
1102 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1103 && (flags
& SEC_DEBUGGING
) != 0)
1108 if (section
->output_section
== NULL
)
1110 /* This prevents future calls from assigning this section. */
1111 section
->output_section
= bfd_abs_section_ptr
;
1116 if (section
->output_section
== NULL
)
1119 lang_input_section_type
*new;
1122 if (output
->bfd_section
== NULL
)
1130 /* Add a section reference to the list. */
1131 new = new_stat (lang_input_section
, ptr
);
1133 new->section
= section
;
1135 section
->output_section
= output
->bfd_section
;
1137 flags
= section
->flags
;
1139 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1140 to an output section, because we want to be able to include a
1141 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1142 section (I don't know why we want to do this, but we do).
1143 build_link_order in ldwrite.c handles this case by turning
1144 the embedded SEC_NEVER_LOAD section into a fill. */
1146 flags
&= ~ SEC_NEVER_LOAD
;
1148 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1149 already been processed. One reason to do this is that on pe
1150 format targets, .text$foo sections go into .text and it's odd
1151 to see .text with SEC_LINK_ONCE set. */
1153 if (! link_info
.relocateable
)
1154 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1156 /* If this is not the first input section, and the SEC_READONLY
1157 flag is not currently set, then don't set it just because the
1158 input section has it set. */
1160 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1161 flags
&= ~ SEC_READONLY
;
1163 section
->output_section
->flags
|= flags
;
1165 /* If SEC_READONLY is not set in the input section, then clear
1166 it from the output section. */
1167 if ((section
->flags
& SEC_READONLY
) == 0)
1168 section
->output_section
->flags
&= ~SEC_READONLY
;
1170 switch (output
->sectype
)
1172 case normal_section
:
1177 case overlay_section
:
1178 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1180 case noload_section
:
1181 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1182 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1186 /* Copy over SEC_SMALL_DATA. */
1187 if (section
->flags
& SEC_SMALL_DATA
)
1188 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1190 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1191 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1193 /* If supplied an aligment, then force it. */
1194 if (output
->section_alignment
!= -1)
1195 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1197 if (section
->flags
& SEC_BLOCK
)
1199 section
->output_section
->flags
|= SEC_BLOCK
;
1200 /* FIXME: This value should really be obtained from the bfd... */
1201 output
->block_value
= 128;
1206 /* Handle wildcard sorting. This returns the lang_input_section which
1207 should follow the one we are going to create for SECTION and FILE,
1208 based on the sorting requirements of WILD. It returns NULL if the
1209 new section should just go at the end of the current list. */
1211 static lang_statement_union_type
*
1212 wild_sort (wild
, file
, section
)
1213 lang_wild_statement_type
*wild
;
1214 lang_input_statement_type
*file
;
1217 const char *section_name
;
1218 lang_statement_union_type
*l
;
1220 if (! wild
->filenames_sorted
&& ! wild
->sections_sorted
)
1223 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1224 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->next
)
1226 lang_input_section_type
*ls
;
1228 if (l
->header
.type
!= lang_input_section_enum
)
1230 ls
= &l
->input_section
;
1232 /* Sorting by filename takes precedence over sorting by section
1235 if (wild
->filenames_sorted
)
1237 const char *fn
, *ln
;
1241 /* The PE support for the .idata section as generated by
1242 dlltool assumes that files will be sorted by the name of
1243 the archive and then the name of the file within the
1246 if (file
->the_bfd
!= NULL
1247 && bfd_my_archive (file
->the_bfd
) != NULL
)
1249 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1254 fn
= file
->filename
;
1258 if (ls
->ifile
->the_bfd
!= NULL
1259 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1261 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1266 ln
= ls
->ifile
->filename
;
1270 i
= strcmp (fn
, ln
);
1279 fn
= file
->filename
;
1281 ln
= ls
->ifile
->filename
;
1283 i
= strcmp (fn
, ln
);
1291 /* Here either the files are not sorted by name, or we are
1292 looking at the sections for this file. */
1294 if (wild
->sections_sorted
)
1296 if (strcmp (section_name
,
1297 bfd_get_section_name (ls
->ifile
->the_bfd
,
1307 /* Expand a wild statement for a particular FILE. SECTION may be
1308 NULL, in which case it is a wild card. */
1311 output_section_callback (ptr
, section
, file
, output
)
1312 lang_wild_statement_type
*ptr
;
1314 lang_input_statement_type
*file
;
1317 lang_statement_union_type
*before
;
1319 /* If the wild pattern was marked KEEP, the member sections
1320 should be as well. */
1321 if (ptr
->keep_sections
)
1322 section
->flags
|= SEC_KEEP
;
1324 before
= wild_sort (ptr
, file
, section
);
1326 /* Here BEFORE points to the lang_input_section which
1327 should follow the one we are about to add. If BEFORE
1328 is NULL, then the section should just go at the end
1329 of the current list. */
1332 wild_doit (&ptr
->children
, section
,
1333 (lang_output_section_statement_type
*) output
,
1337 lang_statement_list_type list
;
1338 lang_statement_union_type
**pp
;
1340 lang_list_init (&list
);
1341 wild_doit (&list
, section
,
1342 (lang_output_section_statement_type
*) output
,
1345 /* If we are discarding the section, LIST.HEAD will
1347 if (list
.head
!= NULL
)
1349 ASSERT (list
.head
->next
== NULL
);
1351 for (pp
= &ptr
->children
.head
;
1354 ASSERT (*pp
!= NULL
);
1356 list
.head
->next
= *pp
;
1362 /* This is passed a file name which must have been seen already and
1363 added to the statement tree. We will see if it has been opened
1364 already and had its symbols read. If not then we'll read it. */
1366 static lang_input_statement_type
*
1370 lang_input_statement_type
*search
;
1372 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1373 search
!= (lang_input_statement_type
*) NULL
;
1374 search
= (lang_input_statement_type
*) search
->next_real_file
)
1376 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1378 if (search
->filename
!= (char *) NULL
1379 && name
!= (char *) NULL
1380 && strcmp (search
->filename
, name
) == 0)
1384 if (search
== (lang_input_statement_type
*) NULL
)
1385 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1388 /* If we have already added this file, or this file is not real
1389 (FIXME: can that ever actually happen?) or the name is NULL
1390 (FIXME: can that ever actually happen?) don't add this file. */
1393 || search
->filename
== (const char *) NULL
)
1396 load_symbols (search
, (lang_statement_list_type
*) NULL
);
1401 /* Get the symbols for an input file. */
1404 load_symbols (entry
, place
)
1405 lang_input_statement_type
*entry
;
1406 lang_statement_list_type
*place
;
1413 ldfile_open_file (entry
);
1415 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1416 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1419 lang_statement_list_type
*hold
;
1421 err
= bfd_get_error ();
1422 if (err
== bfd_error_file_ambiguously_recognized
)
1426 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1427 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1428 for (p
= matching
; *p
!= NULL
; p
++)
1432 else if (err
!= bfd_error_file_not_recognized
1434 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1436 bfd_close (entry
->the_bfd
);
1437 entry
->the_bfd
= NULL
;
1439 /* See if the emulation has some special knowledge. */
1441 if (ldemul_unrecognized_file (entry
))
1444 /* Try to interpret the file as a linker script. */
1446 ldfile_open_command_file (entry
->filename
);
1451 ldfile_assumed_script
= true;
1452 parser_input
= input_script
;
1454 ldfile_assumed_script
= false;
1461 if (ldemul_recognized_file (entry
))
1464 /* We don't call ldlang_add_file for an archive. Instead, the
1465 add_symbols entry point will call ldlang_add_file, via the
1466 add_archive_element callback, for each element of the archive
1468 switch (bfd_get_format (entry
->the_bfd
))
1474 ldlang_add_file (entry
);
1475 if (trace_files
|| trace_file_tries
)
1476 info_msg ("%I\n", entry
);
1480 if (entry
->whole_archive
)
1482 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1484 while (member
!= NULL
)
1486 if (! bfd_check_format (member
, bfd_object
))
1487 einfo (_("%F%B: object %B in archive is not object\n"),
1488 entry
->the_bfd
, member
);
1489 if (! ((*link_info
.callbacks
->add_archive_element
)
1490 (&link_info
, member
, "--whole-archive")))
1492 if (! bfd_link_add_symbols (member
, &link_info
))
1493 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1494 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1498 entry
->loaded
= true;
1504 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1505 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1507 entry
->loaded
= true;
1510 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1511 indicating that it is a wildcard. Separate lang_input_section
1512 statements are created for each part of the expansion; they are
1513 added after the wild statement S. OUTPUT is the output section. */
1516 wild (s
, section
, file
, target
, output
)
1517 lang_wild_statement_type
*s
;
1518 const char *section
;
1520 const char *target ATTRIBUTE_UNUSED
;
1521 lang_output_section_statement_type
*output
;
1523 walk_wild (s
, section
, file
, output_section_callback
, (PTR
) output
);
1525 if (section
!= (char *) NULL
1526 && strcmp (section
, "COMMON") == 0
1527 && default_common_section
== NULL
)
1529 /* Remember the section that common is going to in case we later
1530 get something which doesn't know where to put it. */
1531 default_common_section
= output
;
1535 /* Return true iff target is the sought target. */
1538 get_target (target
, data
)
1539 const bfd_target
*target
;
1542 const char *sought
= (const char *) data
;
1544 return strcmp (target
->name
, sought
) == 0;
1547 /* Like strcpy() but convert to lower case as well. */
1556 while ((c
= *src
++) != 0)
1558 if (isupper ((unsigned char) c
))
1567 /* Remove the first occurance of needle (if any) in haystack
1571 strcut (haystack
, needle
)
1575 haystack
= strstr (haystack
, needle
);
1581 for (src
= haystack
+ strlen (needle
); *src
;)
1582 *haystack
++ = *src
++;
1588 /* Compare two target format name strings.
1589 Return a value indicating how "similar" they are. */
1592 name_compare (first
, second
)
1600 copy1
= xmalloc (strlen (first
) + 1);
1601 copy2
= xmalloc (strlen (second
) + 1);
1603 /* Convert the names to lower case. */
1604 stricpy (copy1
, first
);
1605 stricpy (copy2
, second
);
1607 /* Remove and endian strings from the name. */
1608 strcut (copy1
, "big");
1609 strcut (copy1
, "little");
1610 strcut (copy2
, "big");
1611 strcut (copy2
, "little");
1613 /* Return a value based on how many characters match,
1614 starting from the beginning. If both strings are
1615 the same then return 10 * their length. */
1616 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1617 if (copy1
[result
] == 0)
1629 /* Set by closest_target_match() below. */
1630 static const bfd_target
*winner
;
1632 /* Scan all the valid bfd targets looking for one that has the endianness
1633 requirement that was specified on the command line, and is the nearest
1634 match to the original output target. */
1637 closest_target_match (target
, data
)
1638 const bfd_target
*target
;
1641 const bfd_target
*original
= (const bfd_target
*) data
;
1643 if (command_line
.endian
== ENDIAN_BIG
1644 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1647 if (command_line
.endian
== ENDIAN_LITTLE
1648 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1651 /* Must be the same flavour. */
1652 if (target
->flavour
!= original
->flavour
)
1655 /* If we have not found a potential winner yet, then record this one. */
1662 /* Oh dear, we now have two potential candidates for a successful match.
1663 Compare their names and choose the better one. */
1664 if (name_compare (target
->name
, original
->name
) > name_compare (winner
->name
, original
->name
))
1667 /* Keep on searching until wqe have checked them all. */
1671 /* Return the BFD target format of the first input file. */
1674 get_first_input_target ()
1676 char *target
= NULL
;
1678 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1680 if (s
->header
.type
== lang_input_statement_enum
1683 ldfile_open_file (s
);
1685 if (s
->the_bfd
!= NULL
1686 && bfd_check_format (s
->the_bfd
, bfd_object
))
1688 target
= bfd_get_target (s
->the_bfd
);
1699 /* Open the output file. */
1707 /* Has the user told us which output format to use? */
1708 if (output_target
== (char *) NULL
)
1710 /* No - has the current target been set to something other than
1712 if (current_target
!= default_target
)
1713 output_target
= current_target
;
1715 /* No - can we determine the format of the first input file? */
1718 output_target
= get_first_input_target ();
1720 /* Failed - use the default output target. */
1721 if (output_target
== NULL
)
1722 output_target
= default_target
;
1726 /* Has the user requested a particular endianness on the command
1728 if (command_line
.endian
!= ENDIAN_UNSET
)
1730 const bfd_target
*target
;
1731 enum bfd_endian desired_endian
;
1733 /* Get the chosen target. */
1734 target
= bfd_search_for_target (get_target
, (PTR
) output_target
);
1736 /* If the target is not supported, we cannot do anything. */
1739 if (command_line
.endian
== ENDIAN_BIG
)
1740 desired_endian
= BFD_ENDIAN_BIG
;
1742 desired_endian
= BFD_ENDIAN_LITTLE
;
1744 /* See if the target has the wrong endianness. This should
1745 not happen if the linker script has provided big and
1746 little endian alternatives, but some scrips don't do
1748 if (target
->byteorder
!= desired_endian
)
1750 /* If it does, then see if the target provides
1751 an alternative with the correct endianness. */
1752 if (target
->alternative_target
!= NULL
1753 && (target
->alternative_target
->byteorder
== desired_endian
))
1754 output_target
= target
->alternative_target
->name
;
1757 /* Try to find a target as similar as possible to
1758 the default target, but which has the desired
1759 endian characteristic. */
1760 (void) bfd_search_for_target (closest_target_match
, (PTR
) target
);
1762 /* Oh dear - we could not find any targets that
1763 satisfy our requirements. */
1765 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1767 output_target
= winner
->name
;
1773 output
= bfd_openw (name
, output_target
);
1775 if (output
== (bfd
*) NULL
)
1777 if (bfd_get_error () == bfd_error_invalid_target
)
1778 einfo (_("%P%F: target %s not found\n"), output_target
);
1780 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1783 delete_output_file_on_failure
= true;
1786 output
->flags
|= D_PAGED
;
1789 if (! bfd_set_format (output
, bfd_object
))
1790 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1791 if (! bfd_set_arch_mach (output
,
1792 ldfile_output_architecture
,
1793 ldfile_output_machine
))
1794 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1796 link_info
.hash
= bfd_link_hash_table_create (output
);
1797 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1798 einfo (_("%P%F: can not create link hash table: %E\n"));
1800 bfd_set_gp_size (output
, g_switch_value
);
1805 ldlang_open_output (statement
)
1806 lang_statement_union_type
*statement
;
1808 switch (statement
->header
.type
)
1810 case lang_output_statement_enum
:
1811 ASSERT (output_bfd
== (bfd
*) NULL
);
1812 output_bfd
= open_output (statement
->output_statement
.name
);
1813 ldemul_set_output_arch ();
1814 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1815 output_bfd
->flags
|= D_PAGED
;
1817 output_bfd
->flags
&= ~D_PAGED
;
1818 if (config
.text_read_only
)
1819 output_bfd
->flags
|= WP_TEXT
;
1821 output_bfd
->flags
&= ~WP_TEXT
;
1822 if (link_info
.traditional_format
)
1823 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1825 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1828 case lang_target_statement_enum
:
1829 current_target
= statement
->target_statement
.target
;
1836 /* Open all the input files. */
1839 open_input_bfds (s
, force
)
1840 lang_statement_union_type
*s
;
1843 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1845 switch (s
->header
.type
)
1847 case lang_constructors_statement_enum
:
1848 open_input_bfds (constructor_list
.head
, force
);
1850 case lang_output_section_statement_enum
:
1851 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1853 case lang_wild_statement_enum
:
1854 /* Maybe we should load the file's symbols. */
1855 if (s
->wild_statement
.filename
1856 && ! wildcardp (s
->wild_statement
.filename
))
1857 (void) lookup_name (s
->wild_statement
.filename
);
1858 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1860 case lang_group_statement_enum
:
1862 struct bfd_link_hash_entry
*undefs
;
1864 /* We must continually search the entries in the group
1865 until no new symbols are added to the list of undefined
1870 undefs
= link_info
.hash
->undefs_tail
;
1871 open_input_bfds (s
->group_statement
.children
.head
, true);
1873 while (undefs
!= link_info
.hash
->undefs_tail
);
1876 case lang_target_statement_enum
:
1877 current_target
= s
->target_statement
.target
;
1879 case lang_input_statement_enum
:
1880 if (s
->input_statement
.real
)
1882 lang_statement_list_type add
;
1884 s
->input_statement
.target
= current_target
;
1886 /* If we are being called from within a group, and this
1887 is an archive which has already been searched, then
1888 force it to be researched unless the whole archive
1889 has been loaded already. */
1891 && !s
->input_statement
.whole_archive
1892 && s
->input_statement
.loaded
1893 && bfd_check_format (s
->input_statement
.the_bfd
,
1895 s
->input_statement
.loaded
= false;
1897 lang_list_init (&add
);
1899 load_symbols (&s
->input_statement
, &add
);
1901 if (add
.head
!= NULL
)
1903 *add
.tail
= s
->next
;
1914 /* If there are [COMMONS] statements, put a wild one into the bss
1918 lang_reasonable_defaults ()
1921 lang_output_section_statement_lookup (".text");
1922 lang_output_section_statement_lookup (".data");
1924 default_common_section
= lang_output_section_statement_lookup (".bss");
1926 if (placed_commons
== false)
1928 lang_wild_statement_type
*new =
1929 new_stat (lang_wild_statement
,
1930 &default_common_section
->children
);
1932 new->section_name
= "COMMON";
1933 new->filename
= (char *) NULL
;
1934 lang_list_init (&new->children
);
1939 /* Add the supplied name to the symbol table as an undefined reference.
1940 Remove items from the chain as we open input bfds. */
1941 typedef struct ldlang_undef_chain_list
1943 struct ldlang_undef_chain_list
*next
;
1945 } ldlang_undef_chain_list_type
;
1947 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1950 ldlang_add_undef (name
)
1951 const char *const name
;
1953 ldlang_undef_chain_list_type
*new =
1954 ((ldlang_undef_chain_list_type
*)
1955 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1957 new->next
= ldlang_undef_chain_list_head
;
1958 ldlang_undef_chain_list_head
= new;
1960 new->name
= buystring (name
);
1963 /* Run through the list of undefineds created above and place them
1964 into the linker hash table as undefined symbols belonging to the
1968 lang_place_undefineds ()
1970 ldlang_undef_chain_list_type
*ptr
;
1972 for (ptr
= ldlang_undef_chain_list_head
;
1973 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1976 struct bfd_link_hash_entry
*h
;
1978 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1979 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1980 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1981 if (h
->type
== bfd_link_hash_new
)
1983 h
->type
= bfd_link_hash_undefined
;
1984 h
->u
.undef
.abfd
= NULL
;
1985 bfd_link_add_undef (link_info
.hash
, h
);
1990 /* Open input files and attatch to output sections. */
1993 map_input_to_output_sections (s
, target
, output_section_statement
)
1994 lang_statement_union_type
*s
;
1996 lang_output_section_statement_type
*output_section_statement
;
1998 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2000 switch (s
->header
.type
)
2003 case lang_wild_statement_enum
:
2004 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
2005 s
->wild_statement
.filename
, target
,
2006 output_section_statement
);
2009 case lang_constructors_statement_enum
:
2010 map_input_to_output_sections (constructor_list
.head
,
2012 output_section_statement
);
2014 case lang_output_section_statement_enum
:
2015 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2017 &s
->output_section_statement
);
2019 case lang_output_statement_enum
:
2021 case lang_target_statement_enum
:
2022 target
= s
->target_statement
.target
;
2024 case lang_group_statement_enum
:
2025 map_input_to_output_sections (s
->group_statement
.children
.head
,
2027 output_section_statement
);
2029 case lang_fill_statement_enum
:
2030 case lang_input_section_enum
:
2031 case lang_object_symbols_statement_enum
:
2032 case lang_data_statement_enum
:
2033 case lang_reloc_statement_enum
:
2034 case lang_padding_statement_enum
:
2035 case lang_input_statement_enum
:
2036 if (output_section_statement
!= NULL
2037 && output_section_statement
->bfd_section
== NULL
)
2038 init_os (output_section_statement
);
2040 case lang_assignment_statement_enum
:
2041 if (output_section_statement
!= NULL
2042 && output_section_statement
->bfd_section
== NULL
)
2043 init_os (output_section_statement
);
2045 /* Make sure that any sections mentioned in the assignment
2047 exp_init_os (s
->assignment_statement
.exp
);
2049 case lang_afile_asection_pair_statement_enum
:
2052 case lang_address_statement_enum
:
2053 /* Mark the specified section with the supplied address. */
2055 lang_output_section_statement_type
*os
=
2056 lang_output_section_statement_lookup
2057 (s
->address_statement
.section_name
);
2059 if (os
->bfd_section
== NULL
)
2061 os
->addr_tree
= s
->address_statement
.address
;
2069 print_output_section_statement (output_section_statement
)
2070 lang_output_section_statement_type
*output_section_statement
;
2072 asection
*section
= output_section_statement
->bfd_section
;
2075 if (output_section_statement
!= abs_output_section
)
2077 minfo ("\n%s", output_section_statement
->name
);
2079 if (section
!= NULL
)
2081 print_dot
= section
->vma
;
2083 len
= strlen (output_section_statement
->name
);
2084 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2089 while (len
< SECTION_NAME_MAP_LENGTH
)
2095 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2097 if (output_section_statement
->load_base
!= NULL
)
2101 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2102 "load base", lang_final_phase_enum
);
2103 minfo (_(" load address 0x%V"), addr
);
2110 print_statement_list (output_section_statement
->children
.head
,
2111 output_section_statement
);
2115 print_assignment (assignment
, output_section
)
2116 lang_assignment_statement_type
*assignment
;
2117 lang_output_section_statement_type
*output_section
;
2120 etree_value_type result
;
2122 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2125 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2126 lang_final_phase_enum
, print_dot
, &print_dot
);
2128 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2139 exp_print_tree (assignment
->exp
);
2145 print_input_statement (statm
)
2146 lang_input_statement_type
*statm
;
2148 if (statm
->filename
!= (char *) NULL
)
2150 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2154 /* Print all symbols defined in a particular section. This is called
2155 via bfd_link_hash_traverse. */
2158 print_one_symbol (hash_entry
, ptr
)
2159 struct bfd_link_hash_entry
*hash_entry
;
2162 asection
*sec
= (asection
*) ptr
;
2164 if ((hash_entry
->type
== bfd_link_hash_defined
2165 || hash_entry
->type
== bfd_link_hash_defweak
)
2166 && sec
== hash_entry
->u
.def
.section
)
2170 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2173 (hash_entry
->u
.def
.value
2174 + hash_entry
->u
.def
.section
->output_offset
2175 + hash_entry
->u
.def
.section
->output_section
->vma
));
2177 minfo (" %T\n", hash_entry
->root
.string
);
2183 /* Print information about an input section to the map file. */
2186 print_input_section (in
)
2187 lang_input_section_type
*in
;
2189 asection
*i
= in
->section
;
2190 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2191 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2192 ldfile_output_machine
);
2197 minfo ("%s", i
->name
);
2199 if (i
->output_section
!= NULL
)
2203 len
= 1 + strlen (i
->name
);
2204 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2209 while (len
< SECTION_NAME_MAP_LENGTH
)
2215 minfo ("0x%V %W %B\n",
2216 i
->output_section
->vma
+ i
->output_offset
, size
/ opb
,
2219 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2221 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2233 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2236 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2238 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
/ opb
;
2244 print_fill_statement (fill
)
2245 lang_fill_statement_type
*fill
;
2247 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2251 print_data_statement (data
)
2252 lang_data_statement_type
*data
;
2258 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2259 ldfile_output_machine
);
2261 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2264 addr
= data
->output_vma
;
2265 if (data
->output_section
!= NULL
)
2266 addr
+= data
->output_section
->vma
;
2294 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2296 if (data
->exp
->type
.node_class
!= etree_value
)
2299 exp_print_tree (data
->exp
);
2304 print_dot
= addr
+ size
/ opb
;
2308 /* Print an address statement. These are generated by options like
2312 print_address_statement (address
)
2313 lang_address_statement_type
*address
;
2315 minfo (_("Address of section %s set to "), address
->section_name
);
2316 exp_print_tree (address
->address
);
2320 /* Print a reloc statement. */
2323 print_reloc_statement (reloc
)
2324 lang_reloc_statement_type
*reloc
;
2329 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2330 ldfile_output_machine
);
2332 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2335 addr
= reloc
->output_vma
;
2336 if (reloc
->output_section
!= NULL
)
2337 addr
+= reloc
->output_section
->vma
;
2339 size
= bfd_get_reloc_size (reloc
->howto
);
2341 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2343 if (reloc
->name
!= NULL
)
2344 minfo ("%s+", reloc
->name
);
2346 minfo ("%s+", reloc
->section
->name
);
2348 exp_print_tree (reloc
->addend_exp
);
2352 print_dot
= addr
+ size
/ opb
;
2356 print_padding_statement (s
)
2357 lang_padding_statement_type
*s
;
2361 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2362 ldfile_output_machine
);
2366 len
= sizeof " *fill*" - 1;
2367 while (len
< SECTION_NAME_MAP_LENGTH
)
2373 addr
= s
->output_offset
;
2374 if (s
->output_section
!= NULL
)
2375 addr
+= s
->output_section
->vma
;
2376 minfo ("0x%V %W", addr
, s
->size
);
2379 minfo (" %u", s
->fill
);
2383 print_dot
= addr
+ s
->size
/ opb
;
2387 print_wild_statement (w
, os
)
2388 lang_wild_statement_type
*w
;
2389 lang_output_section_statement_type
*os
;
2393 if (w
->filenames_sorted
)
2395 if (w
->exclude_filename_list
!= NULL
)
2398 minfo ("EXCLUDE_FILE ( %s", w
->exclude_filename_list
->name
);
2399 for (tmp
= w
->exclude_filename_list
->next
; tmp
; tmp
= tmp
->next
)
2400 minfo (", %s", tmp
->name
);
2403 if (w
->filename
!= NULL
)
2404 minfo ("%s", w
->filename
);
2407 if (w
->filenames_sorted
)
2411 if (w
->sections_sorted
)
2413 if (w
->section_name
!= NULL
)
2414 minfo ("%s", w
->section_name
);
2417 if (w
->sections_sorted
)
2423 print_statement_list (w
->children
.head
, os
);
2426 /* Print a group statement. */
2430 lang_group_statement_type
*s
;
2431 lang_output_section_statement_type
*os
;
2433 fprintf (config
.map_file
, "START GROUP\n");
2434 print_statement_list (s
->children
.head
, os
);
2435 fprintf (config
.map_file
, "END GROUP\n");
2438 /* Print the list of statements in S.
2439 This can be called for any statement type. */
2442 print_statement_list (s
, os
)
2443 lang_statement_union_type
*s
;
2444 lang_output_section_statement_type
*os
;
2448 print_statement (s
, os
);
2453 /* Print the first statement in statement list S.
2454 This can be called for any statement type. */
2457 print_statement (s
, os
)
2458 lang_statement_union_type
*s
;
2459 lang_output_section_statement_type
*os
;
2461 switch (s
->header
.type
)
2464 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2467 case lang_constructors_statement_enum
:
2468 if (constructor_list
.head
!= NULL
)
2470 if (constructors_sorted
)
2471 minfo (" SORT (CONSTRUCTORS)\n");
2473 minfo (" CONSTRUCTORS\n");
2474 print_statement_list (constructor_list
.head
, os
);
2477 case lang_wild_statement_enum
:
2478 print_wild_statement (&s
->wild_statement
, os
);
2480 case lang_address_statement_enum
:
2481 print_address_statement (&s
->address_statement
);
2483 case lang_object_symbols_statement_enum
:
2484 minfo (" CREATE_OBJECT_SYMBOLS\n");
2486 case lang_fill_statement_enum
:
2487 print_fill_statement (&s
->fill_statement
);
2489 case lang_data_statement_enum
:
2490 print_data_statement (&s
->data_statement
);
2492 case lang_reloc_statement_enum
:
2493 print_reloc_statement (&s
->reloc_statement
);
2495 case lang_input_section_enum
:
2496 print_input_section (&s
->input_section
);
2498 case lang_padding_statement_enum
:
2499 print_padding_statement (&s
->padding_statement
);
2501 case lang_output_section_statement_enum
:
2502 print_output_section_statement (&s
->output_section_statement
);
2504 case lang_assignment_statement_enum
:
2505 print_assignment (&s
->assignment_statement
, os
);
2507 case lang_target_statement_enum
:
2508 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2510 case lang_output_statement_enum
:
2511 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2512 if (output_target
!= NULL
)
2513 minfo (" %s", output_target
);
2516 case lang_input_statement_enum
:
2517 print_input_statement (&s
->input_statement
);
2519 case lang_group_statement_enum
:
2520 print_group (&s
->group_statement
, os
);
2522 case lang_afile_asection_pair_statement_enum
:
2531 print_statement_list (statement_list
.head
, abs_output_section
);
2534 /* Print the first N statements in statement list S to STDERR.
2535 If N == 0, nothing is printed.
2536 If N < 0, the entire list is printed.
2537 Intended to be called from GDB. */
2540 dprint_statement (s
, n
)
2541 lang_statement_union_type
*s
;
2544 FILE *map_save
= config
.map_file
;
2546 config
.map_file
= stderr
;
2549 print_statement_list (s
, abs_output_section
);
2552 while (s
&& --n
>= 0)
2554 print_statement (s
, abs_output_section
);
2559 config
.map_file
= map_save
;
2563 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
2564 lang_statement_union_type
**this_ptr
;
2567 asection
*output_section_statement
;
2570 /* Align this section first to the
2571 input sections requirement, then
2572 to the output section's requirement.
2573 If this alignment is > than any seen before,
2574 then record it too. Perform the alignment by
2575 inserting a magic 'padding' statement. */
2577 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2578 ldfile_output_machine
);
2579 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
2581 if (alignment_needed
!= 0)
2583 lang_statement_union_type
*new =
2584 ((lang_statement_union_type
*)
2585 stat_alloc (sizeof (lang_padding_statement_type
)));
2587 /* Link into existing chain. */
2588 new->header
.next
= *this_ptr
;
2590 new->header
.type
= lang_padding_statement_enum
;
2591 new->padding_statement
.output_section
= output_section_statement
;
2592 new->padding_statement
.output_offset
=
2593 dot
- output_section_statement
->vma
;
2594 new->padding_statement
.fill
= fill
;
2595 new->padding_statement
.size
= alignment_needed
* opb
;
2598 /* Remember the most restrictive alignment. */
2599 if (power
> output_section_statement
->alignment_power
)
2601 output_section_statement
->alignment_power
= power
;
2603 output_section_statement
->_raw_size
+= alignment_needed
* opb
;
2605 return dot
+ alignment_needed
;
2608 /* Work out how much this section will move the dot point. */
2611 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
2612 lang_statement_union_type
**this_ptr
;
2613 lang_output_section_statement_type
*output_section_statement
;
2616 boolean relax ATTRIBUTE_UNUSED
;
2618 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2619 asection
*i
= is
->section
;
2620 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2621 ldfile_output_machine
);
2623 if (is
->ifile
->just_syms_flag
== false)
2625 if (output_section_statement
->subsection_alignment
!= -1)
2626 i
->alignment_power
=
2627 output_section_statement
->subsection_alignment
;
2629 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
2630 output_section_statement
->bfd_section
, dot
);
2632 /* Remember where in the output section this input section goes. */
2634 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
2636 /* Mark how big the output section must be to contain this now. */
2637 if (i
->_cooked_size
!= 0)
2638 dot
+= i
->_cooked_size
/ opb
;
2640 dot
+= i
->_raw_size
/ opb
;
2641 output_section_statement
->bfd_section
->_raw_size
=
2642 (dot
- output_section_statement
->bfd_section
->vma
) * opb
;
2646 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2652 #define IGNORE_SECTION(bfd, s) \
2653 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
2654 || bfd_section_size (bfd, s) == 0)
2656 /* Check to see if any allocated sections overlap with other allocated
2657 sections. This can happen when the linker script specifically specifies
2658 the output section addresses of the two sections. */
2661 lang_check_section_addresses ()
2664 unsigned opb
= bfd_octets_per_byte (output_bfd
);
2666 /* Scan all sections in the output list. */
2667 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2671 /* Ignore sections which are not loaded or which have no contents. */
2672 if (IGNORE_SECTION (output_bfd
, s
))
2675 /* Once we reach section 's' stop our seach. This prevents two
2676 warning messages from being produced, one for 'section A overlaps
2677 section B' and one for 'section B overlaps section A'. */
2678 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2685 /* Only consider loadable sections with real contents. */
2686 if (IGNORE_SECTION (output_bfd
, os
))
2689 /* We must check the sections' LMA addresses not their
2690 VMA addresses because overlay sections can have
2691 overlapping VMAs but they must have distinct LMAs. */
2692 s_start
= bfd_section_lma (output_bfd
, s
);
2693 os_start
= bfd_section_lma (output_bfd
, os
);
2694 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) / opb
- 1;
2695 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) / opb
- 1;
2697 /* Look for an overlap. */
2698 if ((s_end
< os_start
) || (s_start
> os_end
))
2702 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2703 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2705 /* Once we have found one overlap for this section,
2706 stop looking for others. */
2712 /* This variable indicates whether bfd_relax_section should be called
2715 static boolean relax_again
;
2717 /* Make sure the new address is within the region. We explicitly permit the
2718 current address to be at the exact end of the region when the address is
2719 non-zero, in case the region is at the end of addressable memory and the
2720 calculation wraps around. */
2723 os_region_check (os
, region
, tree
, base
)
2724 lang_output_section_statement_type
*os
;
2725 struct memory_region_struct
*region
;
2729 if ((region
->current
< region
->origin
2730 || (region
->current
- region
->origin
> region
->length
))
2731 && ((region
->current
!= region
->origin
+ region
->length
)
2734 if (tree
!= (etree_type
*) NULL
)
2736 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2738 os
->bfd_section
->owner
,
2739 os
->bfd_section
->name
,
2744 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2746 os
->bfd_section
->owner
,
2747 os
->bfd_section
->name
);
2749 /* Reset the region pointer. */
2750 region
->current
= region
->origin
;
2754 /* Set the sizes for all the output sections. */
2757 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2758 lang_statement_union_type
*s
;
2759 lang_output_section_statement_type
*output_section_statement
;
2760 lang_statement_union_type
**prev
;
2765 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2766 ldfile_output_machine
);
2768 /* Size up the sections from their constituent parts. */
2769 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2771 switch (s
->header
.type
)
2773 case lang_output_section_statement_enum
:
2776 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2778 if (os
->bfd_section
== NULL
)
2779 /* This section was never actually created. */
2782 /* If this is a COFF shared library section, use the size and
2783 address from the input section. FIXME: This is COFF
2784 specific; it would be cleaner if there were some other way
2785 to do this, but nothing simple comes to mind. */
2786 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2790 if (os
->children
.head
== NULL
2791 || os
->children
.head
->next
!= NULL
2792 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2793 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2796 input
= os
->children
.head
->input_section
.section
;
2797 bfd_set_section_vma (os
->bfd_section
->owner
,
2799 bfd_section_vma (input
->owner
, input
));
2800 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2804 if (bfd_is_abs_section (os
->bfd_section
))
2806 /* No matter what happens, an abs section starts at zero. */
2807 ASSERT (os
->bfd_section
->vma
== 0);
2811 if (os
->addr_tree
== (etree_type
*) NULL
)
2813 /* No address specified for this section, get one
2814 from the region specification. */
2815 if (os
->region
== (lang_memory_region_type
*) NULL
2816 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2817 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2818 && os
->region
->name
[0] == '*'
2819 && strcmp (os
->region
->name
, "*default*") == 0))
2821 os
->region
= lang_memory_default (os
->bfd_section
);
2824 /* If a loadable section is using the default memory
2825 region, and some non default memory regions were
2826 defined, issue a warning. */
2827 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2828 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2829 && ! link_info
.relocateable
2830 && strcmp (os
->region
->name
, "*default*") == 0
2831 && lang_memory_region_list
!= NULL
2832 && (strcmp (lang_memory_region_list
->name
, "*default*") != 0
2833 || lang_memory_region_list
->next
!= NULL
))
2834 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2835 bfd_get_section_name (output_bfd
, os
->bfd_section
));
2837 dot
= os
->region
->current
;
2839 if (os
->section_alignment
== -1)
2844 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2846 if (dot
!= olddot
&& config
.warn_section_align
)
2847 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2848 os
->name
, (unsigned int) (dot
- olddot
));
2855 r
= exp_fold_tree (os
->addr_tree
,
2857 lang_allocating_phase_enum
,
2859 if (r
.valid_p
== false)
2861 einfo (_("%F%S: non constant address expression for section %s\n"),
2864 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2867 /* The section starts here.
2868 First, align to what the section needs. */
2870 if (os
->section_alignment
!= -1)
2871 dot
= align_power (dot
, os
->section_alignment
);
2873 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2875 os
->bfd_section
->output_offset
= 0;
2878 (void) lang_size_sections (os
->children
.head
, os
,
2880 os
->fill
, dot
, relax
);
2882 /* Put the section within the requested block size, or
2883 align at the block boundary. */
2884 after
= ALIGN_N (os
->bfd_section
->vma
2885 + os
->bfd_section
->_raw_size
/ opb
,
2886 /* The coercion here is important, see ld.h. */
2887 (bfd_vma
) os
->block_value
);
2889 if (bfd_is_abs_section (os
->bfd_section
))
2890 ASSERT (after
== os
->bfd_section
->vma
);
2892 os
->bfd_section
->_raw_size
=
2893 (after
- os
->bfd_section
->vma
) * opb
;
2894 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
2895 os
->processed
= true;
2897 /* Update dot in the region ?
2898 We only do this if the section is going to be allocated,
2899 since unallocated sections do not contribute to the region's
2900 overall size in memory.
2902 If the SEC_NEVER_LOAD bit is not set, it will affect the
2903 addresses of sections after it. We have to update
2905 if (os
->region
!= (lang_memory_region_type
*) NULL
2906 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2907 & SEC_NEVER_LOAD
) == 0
2908 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2909 & (SEC_ALLOC
| SEC_LOAD
))))
2911 os
->region
->current
= dot
;
2913 /* Make sure the new address is within the region. */
2914 os_region_check (os
, os
->region
, os
->addr_tree
,
2915 os
->bfd_section
->vma
);
2917 /* If there's no load address specified, use the run
2918 region as the load region. */
2919 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
2920 os
->lma_region
= os
->region
;
2922 if (os
->lma_region
!= NULL
)
2924 if (os
->load_base
!= NULL
)
2926 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
2930 /* Don't allocate twice. */
2931 if (os
->lma_region
!= os
->region
)
2933 /* Set load_base, which will be handled later. */
2935 exp_intop (os
->lma_region
->current
);
2936 os
->lma_region
->current
+=
2937 os
->bfd_section
->_raw_size
/ opb
;
2938 os_region_check (os
, os
->lma_region
, NULL
,
2939 os
->bfd_section
->lma
);
2947 case lang_constructors_statement_enum
:
2948 dot
= lang_size_sections (constructor_list
.head
,
2949 output_section_statement
,
2950 &s
->wild_statement
.children
.head
,
2955 case lang_data_statement_enum
:
2957 unsigned int size
= 0;
2959 s
->data_statement
.output_vma
=
2960 dot
- output_section_statement
->bfd_section
->vma
;
2961 s
->data_statement
.output_section
=
2962 output_section_statement
->bfd_section
;
2964 switch (s
->data_statement
.type
)
2985 output_section_statement
->bfd_section
->_raw_size
+= size
;
2986 /* The output section gets contents, and then we inspect for
2987 any flags set in the input script which override any ALLOC. */
2988 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2989 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
2991 output_section_statement
->bfd_section
->flags
|=
2992 SEC_ALLOC
| SEC_LOAD
;
2997 case lang_reloc_statement_enum
:
3001 s
->reloc_statement
.output_vma
=
3002 dot
- output_section_statement
->bfd_section
->vma
;
3003 s
->reloc_statement
.output_section
=
3004 output_section_statement
->bfd_section
;
3005 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3007 output_section_statement
->bfd_section
->_raw_size
+= size
;
3011 case lang_wild_statement_enum
:
3013 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
3014 output_section_statement
,
3015 &s
->wild_statement
.children
.head
,
3020 case lang_object_symbols_statement_enum
:
3021 link_info
.create_object_symbols_section
=
3022 output_section_statement
->bfd_section
;
3024 case lang_output_statement_enum
:
3025 case lang_target_statement_enum
:
3027 case lang_input_section_enum
:
3031 i
= (*prev
)->input_section
.section
;
3034 if (i
->_cooked_size
== 0)
3035 i
->_cooked_size
= i
->_raw_size
;
3041 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3042 einfo (_("%P%F: can't relax section: %E\n"));
3046 dot
= size_input_section (prev
,
3047 output_section_statement
,
3048 output_section_statement
->fill
,
3052 case lang_input_statement_enum
:
3054 case lang_fill_statement_enum
:
3055 s
->fill_statement
.output_section
=
3056 output_section_statement
->bfd_section
;
3058 fill
= s
->fill_statement
.fill
;
3060 case lang_assignment_statement_enum
:
3062 bfd_vma newdot
= dot
;
3064 exp_fold_tree (s
->assignment_statement
.exp
,
3065 output_section_statement
,
3066 lang_allocating_phase_enum
,
3072 /* The assignment changed dot. Insert a pad. */
3073 if (output_section_statement
== abs_output_section
)
3075 /* If we don't have an output section, then just adjust
3076 the default memory address. */
3077 lang_memory_region_lookup ("*default*")->current
= newdot
;
3081 lang_statement_union_type
*new =
3082 ((lang_statement_union_type
*)
3083 stat_alloc (sizeof (lang_padding_statement_type
)));
3085 /* Link into existing chain. */
3086 new->header
.next
= *prev
;
3088 new->header
.type
= lang_padding_statement_enum
;
3089 new->padding_statement
.output_section
=
3090 output_section_statement
->bfd_section
;
3091 new->padding_statement
.output_offset
=
3092 dot
- output_section_statement
->bfd_section
->vma
;
3093 new->padding_statement
.fill
= fill
;
3094 new->padding_statement
.size
= (newdot
- dot
) * opb
;
3095 output_section_statement
->bfd_section
->_raw_size
+=
3096 new->padding_statement
.size
;
3104 case lang_padding_statement_enum
:
3105 /* If we are relaxing, and this is not the first pass, some
3106 padding statements may have been inserted during previous
3107 passes. We may have to move the padding statement to a new
3108 location if dot has a different value at this point in this
3109 pass than it did at this point in the previous pass. */
3110 s
->padding_statement
.output_offset
=
3111 dot
- output_section_statement
->bfd_section
->vma
;
3112 dot
+= s
->padding_statement
.size
/ opb
;
3113 output_section_statement
->bfd_section
->_raw_size
+=
3114 s
->padding_statement
.size
;
3117 case lang_group_statement_enum
:
3118 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3119 output_section_statement
,
3120 &s
->group_statement
.children
.head
,
3128 /* This can only get here when relaxing is turned on. */
3130 case lang_address_statement_enum
:
3133 prev
= &s
->header
.next
;
3139 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3140 lang_statement_union_type
*s
;
3141 lang_output_section_statement_type
*output_section_statement
;
3145 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3146 ldfile_output_machine
);
3148 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3150 switch (s
->header
.type
)
3152 case lang_constructors_statement_enum
:
3153 dot
= lang_do_assignments (constructor_list
.head
,
3154 output_section_statement
,
3159 case lang_output_section_statement_enum
:
3161 lang_output_section_statement_type
*os
=
3162 &(s
->output_section_statement
);
3164 if (os
->bfd_section
!= NULL
)
3166 dot
= os
->bfd_section
->vma
;
3167 (void) lang_do_assignments (os
->children
.head
, os
,
3169 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3174 /* If nothing has been placed into the output section then
3175 it won't have a bfd_section. */
3176 if (os
->bfd_section
)
3178 os
->bfd_section
->lma
3179 = exp_get_abs_int (os
->load_base
, 0, "load base",
3180 lang_final_phase_enum
);
3185 case lang_wild_statement_enum
:
3187 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3188 output_section_statement
,
3193 case lang_object_symbols_statement_enum
:
3194 case lang_output_statement_enum
:
3195 case lang_target_statement_enum
:
3197 case lang_common_statement_enum
:
3200 case lang_data_statement_enum
:
3202 etree_value_type value
;
3204 value
= exp_fold_tree (s
->data_statement
.exp
,
3206 lang_final_phase_enum
, dot
, &dot
);
3207 s
->data_statement
.value
= value
.value
;
3208 if (value
.valid_p
== false)
3209 einfo (_("%F%P: invalid data statement\n"));
3213 switch (s
->data_statement
.type
)
3237 case lang_reloc_statement_enum
:
3239 etree_value_type value
;
3241 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3243 lang_final_phase_enum
, dot
, &dot
);
3244 s
->reloc_statement
.addend_value
= value
.value
;
3245 if (value
.valid_p
== false)
3246 einfo (_("%F%P: invalid reloc statement\n"));
3248 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
) / opb
;
3251 case lang_input_section_enum
:
3253 asection
*in
= s
->input_section
.section
;
3255 if (in
->_cooked_size
!= 0)
3256 dot
+= in
->_cooked_size
/ opb
;
3258 dot
+= in
->_raw_size
/ opb
;
3262 case lang_input_statement_enum
:
3264 case lang_fill_statement_enum
:
3265 fill
= s
->fill_statement
.fill
;
3267 case lang_assignment_statement_enum
:
3269 exp_fold_tree (s
->assignment_statement
.exp
,
3270 output_section_statement
,
3271 lang_final_phase_enum
,
3277 case lang_padding_statement_enum
:
3278 dot
+= s
->padding_statement
.size
/ opb
;
3281 case lang_group_statement_enum
:
3282 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3283 output_section_statement
,
3291 case lang_address_statement_enum
:
3299 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3300 operator .startof. (section_name), it produces an undefined symbol
3301 .startof.section_name. Similarly, when it sees
3302 .sizeof. (section_name), it produces an undefined symbol
3303 .sizeof.section_name. For all the output sections, we look for
3304 such symbols, and set them to the correct value. */
3311 if (link_info
.relocateable
)
3314 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3316 const char *secname
;
3318 struct bfd_link_hash_entry
*h
;
3320 secname
= bfd_get_section_name (output_bfd
, s
);
3321 buf
= xmalloc (10 + strlen (secname
));
3323 sprintf (buf
, ".startof.%s", secname
);
3324 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3325 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3327 h
->type
= bfd_link_hash_defined
;
3328 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3329 h
->u
.def
.section
= bfd_abs_section_ptr
;
3332 sprintf (buf
, ".sizeof.%s", secname
);
3333 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3334 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3336 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3337 ldfile_output_machine
);
3338 h
->type
= bfd_link_hash_defined
;
3339 if (s
->_cooked_size
!= 0)
3340 h
->u
.def
.value
= s
->_cooked_size
/ opb
;
3342 h
->u
.def
.value
= s
->_raw_size
/ opb
;
3343 h
->u
.def
.section
= bfd_abs_section_ptr
;
3353 struct bfd_link_hash_entry
*h
;
3356 if (link_info
.relocateable
|| link_info
.shared
)
3361 if (entry_symbol
== (char *) NULL
)
3363 /* No entry has been specified. Look for start, but don't warn
3364 if we don't find it. */
3365 entry_symbol
= "start";
3369 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3370 if (h
!= (struct bfd_link_hash_entry
*) NULL
3371 && (h
->type
== bfd_link_hash_defined
3372 || h
->type
== bfd_link_hash_defweak
)
3373 && h
->u
.def
.section
->output_section
!= NULL
)
3377 val
= (h
->u
.def
.value
3378 + bfd_get_section_vma (output_bfd
,
3379 h
->u
.def
.section
->output_section
)
3380 + h
->u
.def
.section
->output_offset
);
3381 if (! bfd_set_start_address (output_bfd
, val
))
3382 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3389 /* We couldn't find the entry symbol. Try parsing it as a
3391 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3394 if (! bfd_set_start_address (output_bfd
, val
))
3395 einfo (_("%P%F: can't set start address\n"));
3401 /* Can't find the entry symbol, and it's not a number. Use
3402 the first address in the text section. */
3403 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3404 if (ts
!= (asection
*) NULL
)
3407 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3408 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3409 if (! bfd_set_start_address (output_bfd
,
3410 bfd_get_section_vma (output_bfd
,
3412 einfo (_("%P%F: can't set start address\n"));
3417 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3424 /* This is a small function used when we want to ignore errors from
3428 #ifdef ANSI_PROTOTYPES
3429 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3431 ignore_bfd_errors (s
)
3432 const char *s ATTRIBUTE_UNUSED
;
3435 /* Don't do anything. */
3438 /* Check that the architecture of all the input files is compatible
3439 with the output file. Also call the backend to let it do any
3440 other checking that is needed. */
3445 lang_statement_union_type
*file
;
3447 const bfd_arch_info_type
*compatible
;
3449 for (file
= file_chain
.head
;
3450 file
!= (lang_statement_union_type
*) NULL
;
3451 file
= file
->input_statement
.next
)
3453 input_bfd
= file
->input_statement
.the_bfd
;
3454 compatible
= bfd_arch_get_compatible (input_bfd
,
3456 if (compatible
== NULL
)
3458 if (command_line
.warn_mismatch
)
3459 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3460 bfd_printable_name (input_bfd
), input_bfd
,
3461 bfd_printable_name (output_bfd
));
3465 bfd_error_handler_type pfn
= NULL
;
3467 /* If we aren't supposed to warn about mismatched input
3468 files, temporarily set the BFD error handler to a
3469 function which will do nothing. We still want to call
3470 bfd_merge_private_bfd_data, since it may set up
3471 information which is needed in the output file. */
3472 if (! command_line
.warn_mismatch
)
3473 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3474 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3476 if (command_line
.warn_mismatch
)
3477 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3480 if (! command_line
.warn_mismatch
)
3481 bfd_set_error_handler (pfn
);
3486 /* Look through all the global common symbols and attach them to the
3487 correct section. The -sort-common command line switch may be used
3488 to roughly sort the entries by size. */
3493 if (link_info
.relocateable
3494 && ! command_line
.force_common_definition
)
3497 if (! config
.sort_common
)
3498 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3503 for (power
= 4; power
>= 0; power
--)
3504 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3509 /* Place one common symbol in the correct section. */
3512 lang_one_common (h
, info
)
3513 struct bfd_link_hash_entry
*h
;
3516 unsigned int power_of_two
;
3519 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3520 ldfile_output_machine
);
3522 if (h
->type
!= bfd_link_hash_common
)
3526 power_of_two
= h
->u
.c
.p
->alignment_power
;
3528 if (config
.sort_common
3529 && power_of_two
< (unsigned int) *(int *) info
)
3532 section
= h
->u
.c
.p
->section
;
3534 /* Increase the size of the section. */
3535 section
->_cooked_size
= ALIGN_N ((section
->_cooked_size
+ opb
- 1) / opb
,
3536 (bfd_size_type
) (1 << power_of_two
)) * opb
;
3538 /* Adjust the alignment if necessary. */
3539 if (power_of_two
> section
->alignment_power
)
3540 section
->alignment_power
= power_of_two
;
3542 /* Change the symbol from common to defined. */
3543 h
->type
= bfd_link_hash_defined
;
3544 h
->u
.def
.section
= section
;
3545 h
->u
.def
.value
= section
->_cooked_size
;
3547 /* Increase the size of the section. */
3548 section
->_cooked_size
+= size
;
3550 /* Make sure the section is allocated in memory, and make sure that
3551 it is no longer a common section. */
3552 section
->flags
|= SEC_ALLOC
;
3553 section
->flags
&= ~SEC_IS_COMMON
;
3555 if (config
.map_file
!= NULL
)
3557 static boolean header_printed
;
3562 if (! header_printed
)
3564 minfo (_("\nAllocating common symbols\n"));
3565 minfo (_("Common symbol size file\n\n"));
3566 header_printed
= true;
3569 name
= demangle (h
->root
.string
);
3571 len
= strlen (name
);
3586 if (size
<= 0xffffffff)
3587 sprintf (buf
, "%lx", (unsigned long) size
);
3589 sprintf_vma (buf
, size
);
3599 minfo ("%B\n", section
->owner
);
3605 /* Run through the input files and ensure that every input section has
3606 somewhere to go. If one is found without a destination then create
3607 an input request and place it into the statement tree. */
3610 lang_place_orphans ()
3612 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3616 for (s
= file
->the_bfd
->sections
;
3617 s
!= (asection
*) NULL
;
3620 if (s
->output_section
== (asection
*) NULL
)
3622 /* This section of the file is not attatched, root
3623 around for a sensible place for it to go. */
3625 if (file
->just_syms_flag
)
3627 /* We are only retrieving symbol values from this
3628 file. We want the symbols to act as though the
3629 values in the file are absolute. */
3630 s
->output_section
= bfd_abs_section_ptr
;
3631 s
->output_offset
= s
->vma
;
3633 else if (strcmp (s
->name
, "COMMON") == 0)
3635 /* This is a lonely common section which must have
3636 come from an archive. We attach to the section
3637 with the wildcard. */
3638 if (! link_info
.relocateable
3639 || command_line
.force_common_definition
)
3641 if (default_common_section
== NULL
)
3644 /* This message happens when using the
3645 svr3.ifile linker script, so I have
3647 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3649 default_common_section
=
3650 lang_output_section_statement_lookup (".bss");
3653 wild_doit (&default_common_section
->children
, s
,
3654 default_common_section
, file
);
3657 else if (ldemul_place_orphan (file
, s
))
3661 lang_output_section_statement_type
*os
=
3662 lang_output_section_statement_lookup (s
->name
);
3664 wild_doit (&os
->children
, s
, os
, file
);
3672 lang_set_flags (ptr
, flags
, invert
)
3673 lang_memory_region_type
*ptr
;
3677 flagword
*ptr_flags
;
3679 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3685 *ptr_flags
|= SEC_ALLOC
;
3689 *ptr_flags
|= SEC_READONLY
;
3693 *ptr_flags
|= SEC_DATA
;
3697 *ptr_flags
|= SEC_CODE
;
3702 *ptr_flags
|= SEC_LOAD
;
3706 einfo (_("%P%F: invalid syntax in flags\n"));
3713 /* Call a function on each input file. This function will be called
3714 on an archive, but not on the elements. */
3717 lang_for_each_input_file (func
)
3718 void (*func
) PARAMS ((lang_input_statement_type
*));
3720 lang_input_statement_type
*f
;
3722 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3724 f
= (lang_input_statement_type
*) f
->next_real_file
)
3728 /* Call a function on each file. The function will be called on all
3729 the elements of an archive which are included in the link, but will
3730 not be called on the archive file itself. */
3733 lang_for_each_file (func
)
3734 void (*func
) PARAMS ((lang_input_statement_type
*));
3736 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3747 lang_for_each_input_section (func
)
3748 void (*func
) PARAMS ((bfd
*ab
, asection
*as
));
3750 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3754 for (s
= f
->the_bfd
->sections
;
3755 s
!= (asection
*) NULL
;
3758 func (f
->the_bfd
, s
);
3766 ldlang_add_file (entry
)
3767 lang_input_statement_type
*entry
;
3771 lang_statement_append (&file_chain
,
3772 (lang_statement_union_type
*) entry
,
3775 /* The BFD linker needs to have a list of all input BFDs involved in
3777 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3778 ASSERT (entry
->the_bfd
!= output_bfd
);
3779 for (pp
= &link_info
.input_bfds
;
3780 *pp
!= (bfd
*) NULL
;
3781 pp
= &(*pp
)->link_next
)
3783 *pp
= entry
->the_bfd
;
3784 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3785 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3787 /* Look through the sections and check for any which should not be
3788 included in the link. We need to do this now, so that we can
3789 notice when the backend linker tries to report multiple
3790 definition errors for symbols which are in sections we aren't
3791 going to link. FIXME: It might be better to entirely ignore
3792 symbols which are defined in sections which are going to be
3793 discarded. This would require modifying the backend linker for
3794 each backend which might set the SEC_LINK_ONCE flag. If we do
3795 this, we should probably handle SEC_EXCLUDE in the same way. */
3797 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3801 lang_add_output (name
, from_script
)
3805 /* Make -o on command line override OUTPUT in script. */
3806 if (had_output_filename
== false || !from_script
)
3808 output_filename
= name
;
3809 had_output_filename
= true;
3813 static lang_output_section_statement_type
*current_section
;
3825 for (l
= 0; l
< 32; l
++)
3827 if (i
>= (unsigned int) x
)
3835 lang_output_section_statement_type
*
3836 lang_enter_output_section_statement (output_section_statement_name
,
3837 address_exp
, sectype
, block_value
,
3838 align
, subalign
, ebase
)
3839 const char *output_section_statement_name
;
3840 etree_type
*address_exp
;
3841 enum section_type sectype
;
3842 bfd_vma block_value
;
3844 etree_type
*subalign
;
3847 lang_output_section_statement_type
*os
;
3851 lang_output_section_statement_lookup (output_section_statement_name
);
3853 /* Add this statement to tree. */
3855 add_statement (lang_output_section_statement_enum
,
3856 output_section_statement
);
3858 /* Make next things chain into subchain of this. */
3860 if (os
->addr_tree
== (etree_type
*) NULL
)
3862 os
->addr_tree
= address_exp
;
3864 os
->sectype
= sectype
;
3865 if (sectype
!= noload_section
)
3866 os
->flags
= SEC_NO_FLAGS
;
3868 os
->flags
= SEC_NEVER_LOAD
;
3869 os
->block_value
= block_value
? block_value
: 1;
3870 stat_ptr
= &os
->children
;
3872 os
->subsection_alignment
=
3873 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
3874 os
->section_alignment
=
3875 topower (exp_get_value_int (align
, -1, "section alignment", 0));
3877 os
->load_base
= ebase
;
3884 lang_output_statement_type
*new =
3885 new_stat (lang_output_statement
, stat_ptr
);
3887 new->name
= output_filename
;
3890 /* Reset the current counters in the regions. */
3893 reset_memory_regions ()
3895 lang_memory_region_type
*p
= lang_memory_region_list
;
3897 for (p
= lang_memory_region_list
;
3898 p
!= (lang_memory_region_type
*) NULL
;
3901 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3902 p
->current
= p
->origin
;
3906 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3907 as needed. SECTION may be NULL, in which case it is a wild card. */
3910 gc_section_callback (ptr
, section
, file
, data
)
3911 lang_wild_statement_type
*ptr
;
3913 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
3914 PTR data ATTRIBUTE_UNUSED
;
3916 /* If the wild pattern was marked KEEP, the member sections
3917 should be as well. */
3918 if (ptr
->keep_sections
)
3919 section
->flags
|= SEC_KEEP
;
3922 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3923 may be NULL, indicating that it is a wildcard. */
3926 lang_gc_wild (s
, section
, file
)
3927 lang_wild_statement_type
*s
;
3928 const char *section
;
3931 walk_wild (s
, section
, file
, gc_section_callback
, NULL
);
3934 /* Iterate over sections marking them against GC. */
3937 lang_gc_sections_1 (s
)
3938 lang_statement_union_type
*s
;
3940 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3942 switch (s
->header
.type
)
3944 case lang_wild_statement_enum
:
3945 lang_gc_wild (&s
->wild_statement
,
3946 s
->wild_statement
.section_name
,
3947 s
->wild_statement
.filename
);
3949 case lang_constructors_statement_enum
:
3950 lang_gc_sections_1 (constructor_list
.head
);
3952 case lang_output_section_statement_enum
:
3953 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
3955 case lang_group_statement_enum
:
3956 lang_gc_sections_1 (s
->group_statement
.children
.head
);
3967 struct bfd_link_hash_entry
*h
;
3968 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
3970 /* Keep all sections so marked in the link script. */
3972 lang_gc_sections_1 (statement_list
.head
);
3974 /* Keep all sections containing symbols undefined on the command-line.
3975 Handle the entry symbol at the same time. */
3977 if (entry_symbol
!= NULL
)
3979 fake_list_start
.next
= ldlang_undef_chain_list_head
;
3980 fake_list_start
.name
= (char *) entry_symbol
;
3981 ulist
= &fake_list_start
;
3984 ulist
= ldlang_undef_chain_list_head
;
3986 for (; ulist
; ulist
= ulist
->next
)
3988 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
3989 false, false, false);
3991 if (h
!= (struct bfd_link_hash_entry
*) NULL
3992 && (h
->type
== bfd_link_hash_defined
3993 || h
->type
== bfd_link_hash_defweak
)
3994 && ! bfd_is_abs_section (h
->u
.def
.section
))
3996 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4000 bfd_gc_sections (output_bfd
, &link_info
);
4006 lang_reasonable_defaults ();
4007 current_target
= default_target
;
4009 /* Open the output file. */
4010 lang_for_each_statement (ldlang_open_output
);
4012 ldemul_create_output_section_statements ();
4014 /* Add to the hash table all undefineds on the command line. */
4015 lang_place_undefineds ();
4017 already_linked_table_init ();
4019 /* Create a bfd for each input file. */
4020 current_target
= default_target
;
4021 open_input_bfds (statement_list
.head
, false);
4023 ldemul_after_open ();
4025 already_linked_table_free ();
4027 /* Make sure that we're not mixing architectures. We call this
4028 after all the input files have been opened, but before we do any
4029 other processing, so that any operations merge_private_bfd_data
4030 does on the output file will be known during the rest of the
4034 /* Handle .exports instead of a version script if we're told to do so. */
4035 if (command_line
.version_exports_section
)
4036 lang_do_version_exports_section ();
4038 /* Build all sets based on the information gathered from the input
4040 ldctor_build_sets ();
4042 /* Remove unreferenced sections if asked to. */
4043 if (command_line
.gc_sections
)
4044 lang_gc_sections ();
4046 /* Size up the common data. */
4049 /* Run through the contours of the script and attach input sections
4050 to the correct output sections. */
4051 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
4052 (lang_output_section_statement_type
*) NULL
);
4054 /* Find any sections not attached explicitly and handle them. */
4055 lang_place_orphans ();
4057 ldemul_before_allocation ();
4059 /* We must record the program headers before we try to fix the
4060 section positions, since they will affect SIZEOF_HEADERS. */
4061 lang_record_phdrs ();
4063 /* Now run around and relax if we can. */
4064 if (command_line
.relax
)
4066 /* First time round is a trial run to get the 'worst case'
4067 addresses of the objects if there was no relaxing. */
4068 lang_size_sections (statement_list
.head
,
4070 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4072 /* Keep relaxing until bfd_relax_section gives up. */
4075 reset_memory_regions ();
4077 relax_again
= false;
4079 /* Note: pe-dll.c does something like this also. If you find
4080 you need to change this code, you probably need to change
4081 pe-dll.c also. DJ */
4083 /* Do all the assignments with our current guesses as to
4085 lang_do_assignments (statement_list
.head
,
4087 (fill_type
) 0, (bfd_vma
) 0);
4089 /* Perform another relax pass - this time we know where the
4090 globals are, so can make better guess. */
4091 lang_size_sections (statement_list
.head
,
4093 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
4095 while (relax_again
);
4099 /* Size up the sections. */
4100 lang_size_sections (statement_list
.head
,
4102 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4105 /* See if anything special should be done now we know how big
4107 ldemul_after_allocation ();
4109 /* Fix any .startof. or .sizeof. symbols. */
4110 lang_set_startof ();
4112 /* Do all the assignments, now that we know the final resting places
4113 of all the symbols. */
4115 lang_do_assignments (statement_list
.head
,
4117 (fill_type
) 0, (bfd_vma
) 0);
4119 /* Make sure that the section addresses make sense. */
4120 if (! link_info
.relocateable
4121 && command_line
.check_section_addresses
)
4122 lang_check_section_addresses ();
4130 /* EXPORTED TO YACC */
4133 lang_add_wild (section_name
, sections_sorted
, filename
, filenames_sorted
,
4134 keep_sections
, exclude_filename_list
)
4135 const char *const section_name
;
4136 boolean sections_sorted
;
4137 const char *const filename
;
4138 boolean filenames_sorted
;
4139 boolean keep_sections
;
4140 struct name_list
*exclude_filename_list
;
4142 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
4145 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
4147 placed_commons
= true;
4149 if (filename
!= NULL
&& ! wildcardp (filename
))
4151 lang_has_input_file
= true;
4153 new->section_name
= section_name
;
4154 new->sections_sorted
= sections_sorted
;
4155 new->filename
= filename
;
4156 new->filenames_sorted
= filenames_sorted
;
4157 new->keep_sections
= keep_sections
;
4158 new->exclude_filename_list
= exclude_filename_list
;
4159 lang_list_init (&new->children
);
4163 lang_section_start (name
, address
)
4165 etree_type
*address
;
4167 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
4169 ad
->section_name
= name
;
4170 ad
->address
= address
;
4173 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4174 because of a -e argument on the command line, or zero if this is
4175 called by ENTRY in a linker script. Command line arguments take
4179 lang_add_entry (name
, cmdline
)
4183 if (entry_symbol
== NULL
4185 || ! entry_from_cmdline
)
4187 entry_symbol
= name
;
4188 entry_from_cmdline
= cmdline
;
4193 lang_add_target (name
)
4196 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4212 map_option_f
= true;
4223 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4230 lang_add_data (type
, exp
)
4232 union etree_union
*exp
;
4235 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4243 /* Create a new reloc statement. RELOC is the BFD relocation type to
4244 generate. HOWTO is the corresponding howto structure (we could
4245 look this up, but the caller has already done so). SECTION is the
4246 section to generate a reloc against, or NAME is the name of the
4247 symbol to generate a reloc against. Exactly one of SECTION and
4248 NAME must be NULL. ADDEND is an expression for the addend. */
4251 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4252 bfd_reloc_code_real_type reloc
;
4253 reloc_howto_type
*howto
;
4256 union etree_union
*addend
;
4258 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4262 p
->section
= section
;
4264 p
->addend_exp
= addend
;
4266 p
->addend_value
= 0;
4267 p
->output_section
= NULL
;
4271 lang_assignment_statement_type
*
4272 lang_add_assignment (exp
)
4275 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4283 lang_add_attribute (attribute
)
4284 enum statement_enum attribute
;
4286 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4293 if (startup_file
!= (char *) NULL
)
4295 einfo (_("%P%Fmultiple STARTUP files\n"));
4297 first_file
->filename
= name
;
4298 first_file
->local_sym_name
= name
;
4299 first_file
->real
= true;
4301 startup_file
= name
;
4308 lang_float_flag
= maybe
;
4312 lang_leave_output_section_statement (fill
, memspec
, phdrs
, lma_memspec
)
4314 const char *memspec
;
4315 struct lang_output_section_phdr_list
*phdrs
;
4316 const char *lma_memspec
;
4318 current_section
->fill
= fill
;
4319 current_section
->region
= lang_memory_region_lookup (memspec
);
4320 if (strcmp (lma_memspec
, "*default*") != 0)
4322 current_section
->lma_region
= lang_memory_region_lookup (lma_memspec
);
4323 /* If no runtime region has been given, but the load region has
4324 been, use the load region. */
4325 if (strcmp (memspec
, "*default*") == 0)
4326 current_section
->region
= lang_memory_region_lookup (lma_memspec
);
4328 current_section
->phdrs
= phdrs
;
4329 stat_ptr
= &statement_list
;
4332 /* Create an absolute symbol with the given name with the value of the
4333 address of first byte of the section named.
4335 If the symbol already exists, then do nothing. */
4338 lang_abs_symbol_at_beginning_of (secname
, name
)
4339 const char *secname
;
4342 struct bfd_link_hash_entry
*h
;
4344 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4345 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4346 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4348 if (h
->type
== bfd_link_hash_new
4349 || h
->type
== bfd_link_hash_undefined
)
4353 h
->type
= bfd_link_hash_defined
;
4355 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4356 if (sec
== (asection
*) NULL
)
4359 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4361 h
->u
.def
.section
= bfd_abs_section_ptr
;
4365 /* Create an absolute symbol with the given name with the value of the
4366 address of the first byte after the end of the section named.
4368 If the symbol already exists, then do nothing. */
4371 lang_abs_symbol_at_end_of (secname
, name
)
4372 const char *secname
;
4375 struct bfd_link_hash_entry
*h
;
4377 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4378 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4379 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4381 if (h
->type
== bfd_link_hash_new
4382 || h
->type
== bfd_link_hash_undefined
)
4386 h
->type
= bfd_link_hash_defined
;
4388 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4389 if (sec
== (asection
*) NULL
)
4392 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4393 + bfd_section_size (output_bfd
, sec
) /
4394 bfd_octets_per_byte (output_bfd
));
4396 h
->u
.def
.section
= bfd_abs_section_ptr
;
4401 lang_statement_append (list
, element
, field
)
4402 lang_statement_list_type
*list
;
4403 lang_statement_union_type
*element
;
4404 lang_statement_union_type
**field
;
4406 *(list
->tail
) = element
;
4410 /* Set the output format type. -oformat overrides scripts. */
4413 lang_add_output_format (format
, big
, little
, from_script
)
4419 if (output_target
== NULL
|| !from_script
)
4421 if (command_line
.endian
== ENDIAN_BIG
4424 else if (command_line
.endian
== ENDIAN_LITTLE
4428 output_target
= format
;
4432 /* Enter a group. This creates a new lang_group_statement, and sets
4433 stat_ptr to build new statements within the group. */
4438 lang_group_statement_type
*g
;
4440 g
= new_stat (lang_group_statement
, stat_ptr
);
4441 lang_list_init (&g
->children
);
4442 stat_ptr
= &g
->children
;
4445 /* Leave a group. This just resets stat_ptr to start writing to the
4446 regular list of statements again. Note that this will not work if
4447 groups can occur inside anything else which can adjust stat_ptr,
4448 but currently they can't. */
4453 stat_ptr
= &statement_list
;
4456 /* Add a new program header. This is called for each entry in a PHDRS
4457 command in a linker script. */
4460 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4468 struct lang_phdr
*n
, **pp
;
4470 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4473 n
->type
= exp_get_value_int (type
, 0, "program header type",
4474 lang_final_phase_enum
);
4475 n
->filehdr
= filehdr
;
4480 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4485 /* Record the program header information in the output BFD. FIXME: We
4486 should not be calling an ELF specific function here. */
4489 lang_record_phdrs ()
4493 struct lang_output_section_phdr_list
*last
;
4494 struct lang_phdr
*l
;
4495 lang_statement_union_type
*u
;
4498 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4500 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4507 for (u
= lang_output_section_statement
.head
;
4509 u
= u
->output_section_statement
.next
)
4511 lang_output_section_statement_type
*os
;
4512 struct lang_output_section_phdr_list
*pl
;
4514 os
= &u
->output_section_statement
;
4521 if (os
->sectype
== noload_section
4522 || os
->bfd_section
== NULL
4523 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4528 if (os
->bfd_section
== NULL
)
4531 for (; pl
!= NULL
; pl
= pl
->next
)
4533 if (strcmp (pl
->name
, l
->name
) == 0)
4538 secs
= ((asection
**)
4539 xrealloc (secs
, alc
* sizeof (asection
*)));
4541 secs
[c
] = os
->bfd_section
;
4548 if (l
->flags
== NULL
)
4551 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4552 lang_final_phase_enum
);
4557 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4558 lang_final_phase_enum
);
4560 if (! bfd_record_phdr (output_bfd
, l
->type
,
4561 l
->flags
== NULL
? false : true,
4563 l
->at
== NULL
? false : true,
4564 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4565 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4570 /* Make sure all the phdr assignments succeeded. */
4571 for (u
= lang_output_section_statement
.head
;
4573 u
= u
->output_section_statement
.next
)
4575 struct lang_output_section_phdr_list
*pl
;
4577 if (u
->output_section_statement
.bfd_section
== NULL
)
4580 for (pl
= u
->output_section_statement
.phdrs
;
4583 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4584 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4585 u
->output_section_statement
.name
, pl
->name
);
4589 /* Record a list of sections which may not be cross referenced. */
4592 lang_add_nocrossref (l
)
4593 struct lang_nocrossref
*l
;
4595 struct lang_nocrossrefs
*n
;
4597 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4598 n
->next
= nocrossref_list
;
4600 nocrossref_list
= n
;
4602 /* Set notice_all so that we get informed about all symbols. */
4603 link_info
.notice_all
= true;
4606 /* Overlay handling. We handle overlays with some static variables. */
4608 /* The overlay virtual address. */
4609 static etree_type
*overlay_vma
;
4611 /* The overlay load address. */
4612 static etree_type
*overlay_lma
;
4614 /* Whether nocrossrefs is set for this overlay. */
4615 static int overlay_nocrossrefs
;
4617 /* An expression for the maximum section size seen so far. */
4618 static etree_type
*overlay_max
;
4620 /* A list of all the sections in this overlay. */
4622 struct overlay_list
{
4623 struct overlay_list
*next
;
4624 lang_output_section_statement_type
*os
;
4627 static struct overlay_list
*overlay_list
;
4629 /* Start handling an overlay. */
4632 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4633 etree_type
*vma_expr
;
4634 etree_type
*lma_expr
;
4637 /* The grammar should prevent nested overlays from occurring. */
4638 ASSERT (overlay_vma
== NULL
4639 && overlay_lma
== NULL
4640 && overlay_list
== NULL
4641 && overlay_max
== NULL
);
4643 overlay_vma
= vma_expr
;
4644 overlay_lma
= lma_expr
;
4645 overlay_nocrossrefs
= nocrossrefs
;
4648 /* Start a section in an overlay. We handle this by calling
4649 lang_enter_output_section_statement with the correct VMA and LMA. */
4652 lang_enter_overlay_section (name
)
4655 struct overlay_list
*n
;
4658 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4659 0, 0, 0, overlay_lma
);
4661 /* If this is the first section, then base the VMA and LMA of future
4662 sections on this one. This will work correctly even if `.' is
4663 used in the addresses. */
4664 if (overlay_list
== NULL
)
4666 overlay_vma
= exp_nameop (ADDR
, name
);
4667 overlay_lma
= exp_nameop (LOADADDR
, name
);
4670 /* Remember the section. */
4671 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4672 n
->os
= current_section
;
4673 n
->next
= overlay_list
;
4676 size
= exp_nameop (SIZEOF
, name
);
4678 /* Adjust the LMA for the next section. */
4679 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4681 /* Arrange to work out the maximum section end address. */
4682 if (overlay_max
== NULL
)
4685 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4688 /* Finish a section in an overlay. There isn't any special to do
4692 lang_leave_overlay_section (fill
, phdrs
)
4694 struct lang_output_section_phdr_list
*phdrs
;
4701 name
= current_section
->name
;
4703 lang_leave_output_section_statement (fill
, "*default*",
4704 phdrs
, "*default*");
4706 /* Define the magic symbols. */
4708 clean
= xmalloc (strlen (name
) + 1);
4710 for (s1
= name
; *s1
!= '\0'; s1
++)
4711 if (isalnum ((unsigned char) *s1
) || *s1
== '_')
4715 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4716 sprintf (buf
, "__load_start_%s", clean
);
4717 lang_add_assignment (exp_assop ('=', buf
,
4718 exp_nameop (LOADADDR
, name
)));
4720 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4721 sprintf (buf
, "__load_stop_%s", clean
);
4722 lang_add_assignment (exp_assop ('=', buf
,
4724 exp_nameop (LOADADDR
, name
),
4725 exp_nameop (SIZEOF
, name
))));
4730 /* Finish an overlay. If there are any overlay wide settings, this
4731 looks through all the sections in the overlay and sets them. */
4734 lang_leave_overlay (fill
, memspec
, phdrs
, lma_memspec
)
4736 const char *memspec
;
4737 struct lang_output_section_phdr_list
*phdrs
;
4738 const char *lma_memspec
;
4740 lang_memory_region_type
*region
;
4741 lang_memory_region_type
*lma_region
;
4742 struct overlay_list
*l
;
4743 struct lang_nocrossref
*nocrossref
;
4745 if (memspec
== NULL
)
4748 region
= lang_memory_region_lookup (memspec
);
4750 if (lma_memspec
== NULL
)
4753 lma_region
= lang_memory_region_lookup (lma_memspec
);
4760 struct overlay_list
*next
;
4762 if (fill
!= 0 && l
->os
->fill
== 0)
4764 if (region
!= NULL
&& l
->os
->region
== NULL
)
4765 l
->os
->region
= region
;
4766 if (lma_region
!= NULL
&& l
->os
->lma_region
== NULL
)
4767 l
->os
->lma_region
= lma_region
;
4768 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4769 l
->os
->phdrs
= phdrs
;
4771 if (overlay_nocrossrefs
)
4773 struct lang_nocrossref
*nc
;
4775 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4776 nc
->name
= l
->os
->name
;
4777 nc
->next
= nocrossref
;
4786 if (nocrossref
!= NULL
)
4787 lang_add_nocrossref (nocrossref
);
4789 /* Update . for the end of the overlay. */
4790 lang_add_assignment (exp_assop ('=', ".",
4791 exp_binop ('+', overlay_vma
, overlay_max
)));
4795 overlay_nocrossrefs
= 0;
4796 overlay_list
= NULL
;
4800 /* Version handling. This is only useful for ELF. */
4802 /* This global variable holds the version tree that we build. */
4804 struct bfd_elf_version_tree
*lang_elf_version_info
;
4807 lang_vers_match_lang_c (expr
, sym
)
4808 struct bfd_elf_version_expr
*expr
;
4811 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4813 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4817 lang_vers_match_lang_cplusplus (expr
, sym
)
4818 struct bfd_elf_version_expr
*expr
;
4824 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4827 alt_sym
= cplus_demangle (sym
, /* DMGL_NO_TPARAMS */ 0);
4830 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4831 Should we early out false in this case? */
4832 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4836 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4844 lang_vers_match_lang_java (expr
, sym
)
4845 struct bfd_elf_version_expr
*expr
;
4851 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4854 alt_sym
= cplus_demangle (sym
, DMGL_JAVA
);
4857 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4858 Should we early out false in this case? */
4859 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4863 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4870 /* This is called for each variable name or match expression. */
4872 struct bfd_elf_version_expr
*
4873 lang_new_vers_regex (orig
, new, lang
)
4874 struct bfd_elf_version_expr
*orig
;
4878 struct bfd_elf_version_expr
*ret
;
4880 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4884 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4885 ret
->match
= lang_vers_match_lang_c
;
4886 else if (strcasecmp (lang
, "C++") == 0)
4887 ret
->match
= lang_vers_match_lang_cplusplus
;
4888 else if (strcasecmp (lang
, "Java") == 0)
4889 ret
->match
= lang_vers_match_lang_java
;
4892 einfo (_("%X%P: unknown language `%s' in version information\n"),
4894 ret
->match
= lang_vers_match_lang_c
;
4900 /* This is called for each set of variable names and match
4903 struct bfd_elf_version_tree
*
4904 lang_new_vers_node (globals
, locals
)
4905 struct bfd_elf_version_expr
*globals
;
4906 struct bfd_elf_version_expr
*locals
;
4908 struct bfd_elf_version_tree
*ret
;
4910 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
4914 ret
->globals
= globals
;
4915 ret
->locals
= locals
;
4917 ret
->name_indx
= (unsigned int) -1;
4922 /* This static variable keeps track of version indices. */
4924 static int version_index
;
4926 /* This is called when we know the name and dependencies of the
4930 lang_register_vers_node (name
, version
, deps
)
4932 struct bfd_elf_version_tree
*version
;
4933 struct bfd_elf_version_deps
*deps
;
4935 struct bfd_elf_version_tree
*t
, **pp
;
4936 struct bfd_elf_version_expr
*e1
;
4938 /* Make sure this node has a unique name. */
4939 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4940 if (strcmp (t
->name
, name
) == 0)
4941 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
4943 /* Check the global and local match names, and make sure there
4944 aren't any duplicates. */
4946 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
4948 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4950 struct bfd_elf_version_expr
*e2
;
4952 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
4953 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4954 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4959 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
4961 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4963 struct bfd_elf_version_expr
*e2
;
4965 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
4966 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4967 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4972 version
->deps
= deps
;
4973 version
->name
= name
;
4975 version
->vernum
= version_index
;
4977 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4982 /* This is called when we see a version dependency. */
4984 struct bfd_elf_version_deps
*
4985 lang_add_vers_depend (list
, name
)
4986 struct bfd_elf_version_deps
*list
;
4989 struct bfd_elf_version_deps
*ret
;
4990 struct bfd_elf_version_tree
*t
;
4992 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
4995 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4997 if (strcmp (t
->name
, name
) == 0)
4999 ret
->version_needed
= t
;
5004 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5010 lang_do_version_exports_section ()
5012 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5014 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5016 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5023 len
= bfd_section_size (is
->the_bfd
, sec
);
5024 contents
= xmalloc (len
);
5025 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5026 einfo (_("%X%P: unable to read .exports section contents"), sec
);
5029 while (p
< contents
+ len
)
5031 greg
= lang_new_vers_regex (greg
, p
, NULL
);
5032 p
= strchr (p
, '\0') + 1;
5035 /* Do not free the contents, as we used them creating the regex. */
5037 /* Do not include this section in the link. */
5038 bfd_set_section_flags (is
->the_bfd
, sec
,
5039 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5042 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
5043 lang_register_vers_node (command_line
.version_exports_section
,
5044 lang_new_vers_node (greg
, lreg
), NULL
);