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
*));
50 static struct obstack stat_obstack
;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static CONST
char *startup_file
;
55 static lang_statement_list_type input_file_chain
;
56 static boolean placed_commons
= false;
57 static lang_output_section_statement_type
*default_common_section
;
58 static boolean map_option_f
;
59 static bfd_vma print_dot
;
60 static lang_input_statement_type
*first_file
;
61 static CONST
char *current_target
;
62 static CONST
char *output_target
;
63 static lang_statement_list_type statement_list
;
64 static struct lang_phdr
*lang_phdr_list
;
66 static void lang_for_each_statement_worker
67 PARAMS ((void (*func
) (lang_statement_union_type
*),
68 lang_statement_union_type
*s
));
69 static lang_input_statement_type
*new_afile
70 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
71 const char *target
, boolean add_to_list
));
72 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
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
*,
78 static void already_linked_table_init
PARAMS ((void));
79 static void already_linked_table_free
PARAMS ((void));
80 static boolean wildcardp
PARAMS ((const char *));
81 static lang_statement_union_type
*wild_sort
82 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
84 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
85 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
86 lang_statement_list_type
*));
87 static void wild
PARAMS ((lang_wild_statement_type
*s
,
88 const char *section
, const char *file
,
90 lang_output_section_statement_type
*output
));
91 static bfd
*open_output
PARAMS ((const char *name
));
92 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
93 static void open_input_bfds
94 PARAMS ((lang_statement_union_type
*statement
, 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
*s
,
100 lang_output_section_statement_type
*output_section_statement
));
101 static void print_output_section_statement
102 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
103 static void print_assignment
104 PARAMS ((lang_assignment_statement_type
*assignment
,
105 lang_output_section_statement_type
*output_section
));
106 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
107 static boolean print_one_symbol
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
108 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
109 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
110 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
111 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
112 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
113 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
114 static void print_wild_statement
115 PARAMS ((lang_wild_statement_type
*w
,
116 lang_output_section_statement_type
*os
));
117 static void print_group
118 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
119 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
120 lang_output_section_statement_type
*os
));
121 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
122 lang_output_section_statement_type
*os
));
123 static void print_statements
PARAMS ((void));
124 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
125 fill_type fill
, unsigned int power
,
126 asection
*output_section_statement
,
128 static bfd_vma size_input_section
129 PARAMS ((lang_statement_union_type
**this_ptr
,
130 lang_output_section_statement_type
*output_section_statement
,
131 fill_type fill
, bfd_vma dot
, boolean relax
));
132 static void lang_finish
PARAMS ((void));
133 static void ignore_bfd_errors
PARAMS ((const char *, ...));
134 static void lang_check
PARAMS ((void));
135 static void lang_common
PARAMS ((void));
136 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
137 static void lang_place_orphans
PARAMS ((void));
138 static int topower
PARAMS ((int));
139 static void lang_set_startof
PARAMS ((void));
140 static void reset_memory_regions
PARAMS ((void));
141 static void lang_record_phdrs
PARAMS ((void));
142 static void lang_gc_wild
143 PARAMS ((lang_wild_statement_type
*, const char *, const char *));
144 static void lang_gc_sections_1
PARAMS ((lang_statement_union_type
*));
145 static void lang_gc_sections
PARAMS ((void));
146 static void lang_do_version_exports_section
PARAMS ((void));
147 static void lang_check_section_addresses
PARAMS ((void));
149 typedef void (*callback_t
) PARAMS ((lang_wild_statement_type
*,
150 asection
*, lang_input_statement_type
*,
152 static void walk_wild_section
153 PARAMS ((lang_wild_statement_type
*, const char *,
154 lang_input_statement_type
*, callback_t
, void *));
155 static void walk_wild_file
156 PARAMS ((lang_wild_statement_type
*, const char *,
157 lang_input_statement_type
*, callback_t
, void *));
159 static int get_target
PARAMS ((const bfd_target
*, void *));
160 static void stricpy
PARAMS ((char *, char *));
161 static void strcut
PARAMS ((char *, char *));
162 static int name_compare
PARAMS ((char *, char *));
163 static int closest_target_match
PARAMS ((const bfd_target
*, void *));
164 static char * get_first_input_target
PARAMS ((void));
167 lang_output_section_statement_type
*abs_output_section
;
168 lang_statement_list_type lang_output_section_statement
;
169 lang_statement_list_type
*stat_ptr
= &statement_list
;
170 lang_statement_list_type file_chain
= { NULL
, NULL
};
171 const char *entry_symbol
= NULL
;
172 boolean entry_from_cmdline
;
173 boolean lang_has_input_file
= false;
174 boolean had_output_filename
= false;
175 boolean lang_float_flag
= false;
176 boolean delete_output_file_on_failure
= false;
177 struct lang_nocrossrefs
*nocrossref_list
;
179 etree_type
*base
; /* Relocation base - or null */
182 #if defined(__STDC__) || defined(ALMOST_STDC)
183 #define cat(a,b) a##b
185 #define cat(a,b) a/**/b
188 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
190 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
192 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
194 #define SECTION_NAME_MAP_LENGTH (16)
200 return obstack_alloc (&stat_obstack
, size
);
203 /*----------------------------------------------------------------------
204 Generic traversal routines for finding matching sections.
208 walk_wild_section (ptr
, section
, file
, callback
, data
)
209 lang_wild_statement_type
*ptr
;
211 lang_input_statement_type
*file
;
215 /* Don't process sections from files which were excluded. */
216 if (ptr
->exclude_filename_list
!= NULL
)
218 struct name_list
*list_tmp
;
219 for (list_tmp
= ptr
->exclude_filename_list
; list_tmp
; list_tmp
= list_tmp
->next
)
223 if (wildcardp (list_tmp
->name
))
224 match
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0 ? true : false;
226 match
= strcmp (list_tmp
->name
, file
->filename
) == 0 ? true : false;
233 if (file
->just_syms_flag
== false)
235 register asection
*s
;
241 wildcard
= wildcardp (section
);
243 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
253 name
= bfd_get_section_name (file
->the_bfd
, s
);
255 match
= fnmatch (section
, name
, 0) == 0 ? true : false;
257 match
= strcmp (section
, name
) == 0 ? true : false;
261 (*callback
) (ptr
, s
, file
, data
);
266 /* Handle a wild statement for a single file F. */
269 walk_wild_file (s
, section
, f
, callback
, data
)
270 lang_wild_statement_type
*s
;
272 lang_input_statement_type
*f
;
276 if (f
->the_bfd
== NULL
277 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
278 walk_wild_section (s
, section
, f
, callback
, data
);
283 /* This is an archive file. We must map each member of the
284 archive separately. */
285 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
286 while (member
!= NULL
)
288 /* When lookup_name is called, it will call the add_symbols
289 entry point for the archive. For each element of the
290 archive which is included, BFD will call ldlang_add_file,
291 which will set the usrdata field of the member to the
292 lang_input_statement. */
293 if (member
->usrdata
!= NULL
)
295 walk_wild_section (s
, section
,
296 (lang_input_statement_type
*) member
->usrdata
,
300 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
306 walk_wild (s
, section
, file
, callback
, data
)
307 lang_wild_statement_type
*s
;
313 if (file
== (char *) NULL
)
315 /* Perform the iteration over all files in the list. */
316 LANG_FOR_EACH_INPUT_STATEMENT (f
)
318 walk_wild_file (s
, section
, f
, callback
, data
);
321 else if (wildcardp (file
))
323 LANG_FOR_EACH_INPUT_STATEMENT (f
)
325 if (fnmatch (file
, f
->filename
, FNM_FILE_NAME
) == 0)
326 walk_wild_file (s
, section
, f
, callback
, data
);
331 lang_input_statement_type
*f
;
333 /* Perform the iteration over a single file. */
334 f
= lookup_name (file
);
335 walk_wild_file (s
, section
, f
, callback
, data
);
339 /*----------------------------------------------------------------------
340 lang_for_each_statement walks the parse tree and calls the provided
341 function for each node
345 lang_for_each_statement_worker (func
, s
)
346 void (*func
) PARAMS ((lang_statement_union_type
*));
347 lang_statement_union_type
*s
;
349 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
353 switch (s
->header
.type
)
355 case lang_constructors_statement_enum
:
356 lang_for_each_statement_worker (func
, constructor_list
.head
);
358 case lang_output_section_statement_enum
:
359 lang_for_each_statement_worker
361 s
->output_section_statement
.children
.head
);
363 case lang_wild_statement_enum
:
364 lang_for_each_statement_worker
366 s
->wild_statement
.children
.head
);
368 case lang_group_statement_enum
:
369 lang_for_each_statement_worker (func
,
370 s
->group_statement
.children
.head
);
372 case lang_data_statement_enum
:
373 case lang_reloc_statement_enum
:
374 case lang_object_symbols_statement_enum
:
375 case lang_output_statement_enum
:
376 case lang_target_statement_enum
:
377 case lang_input_section_enum
:
378 case lang_input_statement_enum
:
379 case lang_assignment_statement_enum
:
380 case lang_padding_statement_enum
:
381 case lang_address_statement_enum
:
382 case lang_fill_statement_enum
:
392 lang_for_each_statement (func
)
393 void (*func
) PARAMS ((lang_statement_union_type
*));
395 lang_for_each_statement_worker (func
,
396 statement_list
.head
);
399 /*----------------------------------------------------------------------*/
401 lang_list_init (list
)
402 lang_statement_list_type
*list
;
404 list
->head
= (lang_statement_union_type
*) NULL
;
405 list
->tail
= &list
->head
;
408 /*----------------------------------------------------------------------
410 build a new statement node for the parse tree
415 lang_statement_union_type
*
416 new_statement (type
, size
, list
)
417 enum statement_enum type
;
419 lang_statement_list_type
* list
;
421 lang_statement_union_type
*new = (lang_statement_union_type
*)
424 new->header
.type
= type
;
425 new->header
.next
= (lang_statement_union_type
*) NULL
;
426 lang_statement_append (list
, new, &new->header
.next
);
431 Build a new input file node for the language. There are several ways
432 in which we treat an input file, eg, we only look at symbols, or
433 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.
441 static lang_input_statement_type
*
442 new_afile (name
, file_type
, target
, add_to_list
)
444 lang_input_file_enum_type file_type
;
448 lang_input_statement_type
*p
;
451 p
= new_stat (lang_input_statement
, stat_ptr
);
454 p
= ((lang_input_statement_type
*)
455 stat_alloc (sizeof (lang_input_statement_type
)));
456 p
->header
.next
= NULL
;
459 lang_has_input_file
= true;
463 case lang_input_file_is_symbols_only_enum
:
465 p
->is_archive
= false;
467 p
->local_sym_name
= name
;
468 p
->just_syms_flag
= true;
469 p
->search_dirs_flag
= false;
471 case lang_input_file_is_fake_enum
:
473 p
->is_archive
= false;
475 p
->local_sym_name
= name
;
476 p
->just_syms_flag
= false;
477 p
->search_dirs_flag
= false;
479 case lang_input_file_is_l_enum
:
480 p
->is_archive
= true;
483 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
484 p
->just_syms_flag
= false;
485 p
->search_dirs_flag
= true;
487 case lang_input_file_is_marker_enum
:
489 p
->is_archive
= false;
491 p
->local_sym_name
= name
;
492 p
->just_syms_flag
= false;
493 p
->search_dirs_flag
= true;
495 case lang_input_file_is_search_file_enum
:
497 p
->is_archive
= false;
499 p
->local_sym_name
= name
;
500 p
->just_syms_flag
= false;
501 p
->search_dirs_flag
= true;
503 case lang_input_file_is_file_enum
:
505 p
->is_archive
= false;
507 p
->local_sym_name
= name
;
508 p
->just_syms_flag
= false;
509 p
->search_dirs_flag
= false;
514 p
->the_bfd
= (bfd
*) NULL
;
515 p
->asymbols
= (asymbol
**) NULL
;
516 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
517 p
->next
= (lang_statement_union_type
*) NULL
;
519 p
->dynamic
= config
.dynamic_link
;
520 p
->whole_archive
= whole_archive
;
522 lang_statement_append (&input_file_chain
,
523 (lang_statement_union_type
*) p
,
528 lang_input_statement_type
*
529 lang_add_input_file (name
, file_type
, target
)
531 lang_input_file_enum_type file_type
;
534 lang_has_input_file
= true;
535 return new_afile (name
, file_type
, target
, true);
538 /* 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
,
554 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
556 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
560 /*----------------------------------------------------------------------
561 A region is an area of memory declared with the
562 MEMORY { name:org=exp, len=exp ... }
565 We maintain a list of all the regions here
567 If no regions are specified in the script, then the default is used
568 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;
627 lang_memory_region_type
*
628 lang_memory_default (section
)
631 lang_memory_region_type
*p
;
633 flagword sec_flags
= section
->flags
;
635 /* Override SEC_DATA to mean a writable section. */
636 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
637 sec_flags
|= SEC_DATA
;
639 for (p
= lang_memory_region_list
;
640 p
!= (lang_memory_region_type
*) NULL
;
643 if ((p
->flags
& sec_flags
) != 0
644 && (p
->not_flags
& sec_flags
) == 0)
649 return lang_memory_region_lookup ("*default*");
652 lang_output_section_statement_type
*
653 lang_output_section_find (name
)
654 CONST
char *CONST name
;
656 lang_statement_union_type
*u
;
657 lang_output_section_statement_type
*lookup
;
659 for (u
= lang_output_section_statement
.head
;
660 u
!= (lang_statement_union_type
*) NULL
;
663 lookup
= &u
->output_section_statement
;
664 if (strcmp (name
, lookup
->name
) == 0)
669 return (lang_output_section_statement_type
*) NULL
;
672 lang_output_section_statement_type
*
673 lang_output_section_statement_lookup (name
)
674 CONST
char *CONST name
;
676 lang_output_section_statement_type
*lookup
;
678 lookup
= lang_output_section_find (name
);
679 if (lookup
== (lang_output_section_statement_type
*) NULL
)
682 lookup
= (lang_output_section_statement_type
*)
683 new_stat (lang_output_section_statement
, stat_ptr
);
684 lookup
->region
= (lang_memory_region_type
*) NULL
;
685 lookup
->lma_region
= (lang_memory_region_type
*) NULL
;
687 lookup
->block_value
= 1;
690 lookup
->next
= (lang_statement_union_type
*) NULL
;
691 lookup
->bfd_section
= (asection
*) NULL
;
692 lookup
->processed
= false;
693 lookup
->sectype
= normal_section
;
694 lookup
->addr_tree
= (etree_type
*) NULL
;
695 lang_list_init (&lookup
->children
);
697 lookup
->memspec
= (CONST
char *) NULL
;
699 lookup
->subsection_alignment
= -1;
700 lookup
->section_alignment
= -1;
701 lookup
->load_base
= (union etree_union
*) NULL
;
702 lookup
->phdrs
= NULL
;
704 lang_statement_append (&lang_output_section_statement
,
705 (lang_statement_union_type
*) lookup
,
712 lang_map_flags (flag
)
715 if (flag
& SEC_ALLOC
)
721 if (flag
& SEC_READONLY
)
734 lang_memory_region_type
*m
;
736 minfo (_("\nMemory Configuration\n\n"));
737 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
738 _("Name"), _("Origin"), _("Length"), _("Attributes"));
740 for (m
= lang_memory_region_list
;
741 m
!= (lang_memory_region_type
*) NULL
;
747 fprintf (config
.map_file
, "%-16s ", m
->name
);
749 sprintf_vma (buf
, m
->origin
);
750 minfo ("0x%s ", buf
);
758 minfo ("0x%V", m
->length
);
759 if (m
->flags
|| m
->not_flags
)
767 lang_map_flags (m
->flags
);
773 lang_map_flags (m
->not_flags
);
780 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
785 /* Initialize an output section. */
789 lang_output_section_statement_type
*s
;
791 section_userdata_type
*new;
793 if (s
->bfd_section
!= NULL
)
796 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
797 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME
);
799 new = ((section_userdata_type
*)
800 stat_alloc (sizeof (section_userdata_type
)));
802 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
803 if (s
->bfd_section
== (asection
*) NULL
)
804 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
805 if (s
->bfd_section
== (asection
*) NULL
)
807 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
808 output_bfd
->xvec
->name
, s
->name
);
810 s
->bfd_section
->output_section
= s
->bfd_section
;
812 /* We initialize an output sections output offset to minus its own */
813 /* vma to allow us to output a section through itself */
814 s
->bfd_section
->output_offset
= 0;
815 get_userdata (s
->bfd_section
) = (PTR
) new;
817 /* If there is a base address, make sure that any sections it might
818 mention are initialized. */
819 if (s
->addr_tree
!= NULL
)
820 exp_init_os (s
->addr_tree
);
823 /* Make sure that all output sections mentioned in an expression are
830 switch (exp
->type
.node_class
)
833 exp_init_os (exp
->assign
.src
);
837 exp_init_os (exp
->binary
.lhs
);
838 exp_init_os (exp
->binary
.rhs
);
842 exp_init_os (exp
->trinary
.cond
);
843 exp_init_os (exp
->trinary
.lhs
);
844 exp_init_os (exp
->trinary
.rhs
);
848 exp_init_os (exp
->unary
.child
);
852 switch (exp
->type
.node_code
)
858 lang_output_section_statement_type
*os
;
860 os
= lang_output_section_find (exp
->name
.name
);
861 if (os
!= NULL
&& os
->bfd_section
== NULL
)
872 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
873 once into the output. This routine checks each section, and
874 arrange to discard it if a section of the same name has already
875 been linked. If the section has COMDAT information, then it uses
876 that to decide whether the section should be included. This code
877 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
878 that is, it does not depend solely upon the section name.
879 section_already_linked is called via bfd_map_over_sections. */
881 /* This is the shape of the elements inside the already_linked hash
882 table. It maps a name onto a list of already_linked elements with
883 the same name. It's possible to get more than one element in a
884 list if the COMDAT sections have different names. */
886 struct already_linked_hash_entry
888 struct bfd_hash_entry root
;
889 struct already_linked
*entry
;
892 struct already_linked
894 struct already_linked
*next
;
898 /* The hash table. */
900 static struct bfd_hash_table already_linked_table
;
904 section_already_linked (abfd
, sec
, data
)
909 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
912 struct already_linked
*l
;
913 struct already_linked_hash_entry
*already_linked_list
;
915 /* If we are only reading symbols from this object, then we want to
916 discard all sections. */
917 if (entry
->just_syms_flag
)
919 sec
->output_section
= bfd_abs_section_ptr
;
920 sec
->output_offset
= sec
->vma
;
924 flags
= bfd_get_section_flags (abfd
, sec
);
926 if ((flags
& SEC_LINK_ONCE
) == 0)
929 /* FIXME: When doing a relocateable link, we may have trouble
930 copying relocations in other sections that refer to local symbols
931 in the section being discarded. Those relocations will have to
932 be converted somehow; as of this writing I'm not sure that any of
933 the backends handle that correctly.
935 It is tempting to instead not discard link once sections when
936 doing a relocateable link (technically, they should be discarded
937 whenever we are building constructors). However, that fails,
938 because the linker winds up combining all the link once sections
939 into a single large link once section, which defeats the purpose
940 of having link once sections in the first place.
942 Also, not merging link once sections in a relocateable link
943 causes trouble for MIPS ELF, which relies in link once semantics
944 to handle the .reginfo section correctly. */
946 name
= bfd_get_section_name (abfd
, sec
);
948 already_linked_list
=
949 ((struct already_linked_hash_entry
*)
950 bfd_hash_lookup (&already_linked_table
, name
, true, false));
952 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
954 if (sec
->comdat
== NULL
955 || l
->sec
->comdat
== NULL
956 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
958 /* The section has already been linked. See if we should
960 switch (flags
& SEC_LINK_DUPLICATES
)
965 case SEC_LINK_DUPLICATES_DISCARD
:
968 case SEC_LINK_DUPLICATES_ONE_ONLY
:
969 if (sec
->comdat
== NULL
)
970 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
973 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
974 abfd
, name
, sec
->comdat
->name
);
977 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
978 /* FIXME: We should really dig out the contents of both
979 sections and memcmp them. The COFF/PE spec says that
980 the Microsoft linker does not implement this
981 correctly, so I'm not going to bother doing it
984 case SEC_LINK_DUPLICATES_SAME_SIZE
:
985 if (bfd_section_size (abfd
, sec
)
986 != bfd_section_size (l
->sec
->owner
, l
->sec
))
987 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
992 /* Set the output_section field so that wild_doit does not
993 create a lang_input_section structure for this section.
994 Since there might be a symbol in the section being
995 discarded, we must retain a pointer to the section which
996 we are really going to use. */
997 sec
->output_section
= bfd_abs_section_ptr
;
998 if (sec
->comdat
!= NULL
)
999 sec
->comdat
->sec
= l
->sec
;
1005 /* This is the first section with this name. Record it. Allocate
1006 the memory from the same obstack as the hash table is kept in. */
1008 l
= ((struct already_linked
*)
1009 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1012 l
->next
= already_linked_list
->entry
;
1013 already_linked_list
->entry
= l
;
1016 /* Support routines for the hash table used by section_already_linked,
1017 initialize the table, fill in an entry and remove the table. */
1019 static struct bfd_hash_entry
*
1020 already_linked_newfunc (entry
, table
, string
)
1021 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1022 struct bfd_hash_table
*table
;
1023 const char *string ATTRIBUTE_UNUSED
;
1025 struct already_linked_hash_entry
*ret
=
1026 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1030 return (struct bfd_hash_entry
*) ret
;
1034 already_linked_table_init ()
1036 if (! bfd_hash_table_init_n (&already_linked_table
,
1037 already_linked_newfunc
,
1039 einfo (_("%P%F: Failed to create hash table\n"));
1043 already_linked_table_free ()
1045 bfd_hash_table_free (&already_linked_table
);
1048 /* The wild routines.
1050 These expand statements like *(.text) and foo.o to a list of
1051 explicit actions, like foo.o(.text), bar.o(.text) and
1052 foo.o(.text, .data). */
1054 /* Return true if the PATTERN argument is a wildcard pattern.
1055 Although backslashes are treated specially if a pattern contains
1056 wildcards, we do not consider the mere presence of a backslash to
1057 be enough to cause the the pattern to be treated as a wildcard.
1058 That lets us handle DOS filenames more naturally. */
1062 const char *pattern
;
1066 for (s
= pattern
; *s
!= '\0'; ++s
)
1074 /* Add SECTION to the output section OUTPUT. Do this by creating a
1075 lang_input_section statement which is placed at PTR. FILE is the
1076 input file which holds SECTION. */
1079 wild_doit (ptr
, section
, output
, file
)
1080 lang_statement_list_type
*ptr
;
1082 lang_output_section_statement_type
*output
;
1083 lang_input_statement_type
*file
;
1088 flags
= bfd_get_section_flags (section
->owner
, section
);
1092 /* If we are doing a final link, discard sections marked with
1094 if (! link_info
.relocateable
1095 && (flags
& SEC_EXCLUDE
) != 0)
1098 /* Discard input sections which are assigned to a section named
1099 DISCARD_SECTION_NAME. */
1100 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1103 /* Discard debugging sections if we are stripping debugging
1105 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1106 && (flags
& SEC_DEBUGGING
) != 0)
1111 if (section
->output_section
== NULL
)
1113 /* This prevents future calls from assigning this section. */
1114 section
->output_section
= bfd_abs_section_ptr
;
1119 if (section
->output_section
== NULL
)
1122 lang_input_section_type
*new;
1125 if (output
->bfd_section
== NULL
)
1133 /* Add a section reference to the list */
1134 new = new_stat (lang_input_section
, ptr
);
1136 new->section
= section
;
1138 section
->output_section
= output
->bfd_section
;
1140 flags
= section
->flags
;
1142 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1143 to an output section, because we want to be able to include a
1144 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1145 section (I don't know why we want to do this, but we do).
1146 build_link_order in ldwrite.c handles this case by turning
1147 the embedded SEC_NEVER_LOAD section into a fill. */
1149 flags
&= ~ SEC_NEVER_LOAD
;
1151 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1152 already been processed. One reason to do this is that on pe
1153 format targets, .text$foo sections go into .text and it's odd
1154 to see .text with SEC_LINK_ONCE set. */
1156 if (! link_info
.relocateable
)
1157 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1159 /* If this is not the first input section, and the SEC_READONLY
1160 flag is not currently set, then don't set it just because the
1161 input section has it set. */
1163 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1164 flags
&= ~ SEC_READONLY
;
1166 section
->output_section
->flags
|= flags
;
1168 /* If SEC_READONLY is not set in the input section, then clear
1169 it from the output section. */
1170 if ((section
->flags
& SEC_READONLY
) == 0)
1171 section
->output_section
->flags
&= ~SEC_READONLY
;
1173 switch (output
->sectype
)
1175 case normal_section
:
1180 case overlay_section
:
1181 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1183 case noload_section
:
1184 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1185 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1189 /* Copy over SEC_SMALL_DATA. */
1190 if (section
->flags
& SEC_SMALL_DATA
)
1191 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1193 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1194 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1196 /* If supplied an aligment, then force it. */
1197 if (output
->section_alignment
!= -1)
1198 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1200 if (section
->flags
& SEC_BLOCK
)
1202 section
->output_section
->flags
|= SEC_BLOCK
;
1203 /* FIXME: This value should really be obtained from the bfd... */
1204 output
->block_value
= 128;
1209 /* Handle wildcard sorting. This returns the lang_input_section which
1210 should follow the one we are going to create for SECTION and FILE,
1211 based on the sorting requirements of WILD. It returns NULL if the
1212 new section should just go at the end of the current list. */
1214 static lang_statement_union_type
*
1215 wild_sort (wild
, file
, section
)
1216 lang_wild_statement_type
*wild
;
1217 lang_input_statement_type
*file
;
1220 const char *section_name
;
1221 lang_statement_union_type
*l
;
1223 if (! wild
->filenames_sorted
&& ! wild
->sections_sorted
)
1226 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1227 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->next
)
1229 lang_input_section_type
*ls
;
1231 if (l
->header
.type
!= lang_input_section_enum
)
1233 ls
= &l
->input_section
;
1235 /* Sorting by filename takes precedence over sorting by section
1238 if (wild
->filenames_sorted
)
1240 const char *fn
, *ln
;
1244 /* The PE support for the .idata section as generated by
1245 dlltool assumes that files will be sorted by the name of
1246 the archive and then the name of the file within the
1249 if (file
->the_bfd
!= NULL
1250 && bfd_my_archive (file
->the_bfd
) != NULL
)
1252 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1257 fn
= file
->filename
;
1261 if (ls
->ifile
->the_bfd
!= NULL
1262 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1264 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1269 ln
= ls
->ifile
->filename
;
1273 i
= strcmp (fn
, ln
);
1282 fn
= file
->filename
;
1284 ln
= ls
->ifile
->filename
;
1286 i
= strcmp (fn
, ln
);
1294 /* Here either the files are not sorted by name, or we are
1295 looking at the sections for this file. */
1297 if (wild
->sections_sorted
)
1299 if (strcmp (section_name
,
1300 bfd_get_section_name (ls
->ifile
->the_bfd
,
1310 /* Expand a wild statement for a particular FILE. SECTION may be
1311 NULL, in which case it is a wild card. */
1314 output_section_callback (ptr
, section
, file
, output
)
1315 lang_wild_statement_type
*ptr
;
1317 lang_input_statement_type
*file
;
1320 lang_statement_union_type
*before
;
1322 /* If the wild pattern was marked KEEP, the member sections
1323 should be as well. */
1324 if (ptr
->keep_sections
)
1325 section
->flags
|= SEC_KEEP
;
1327 before
= wild_sort (ptr
, file
, section
);
1329 /* Here BEFORE points to the lang_input_section which
1330 should follow the one we are about to add. If BEFORE
1331 is NULL, then the section should just go at the end
1332 of the current list. */
1335 wild_doit (&ptr
->children
, section
,
1336 (lang_output_section_statement_type
*) output
,
1340 lang_statement_list_type list
;
1341 lang_statement_union_type
**pp
;
1343 lang_list_init (&list
);
1344 wild_doit (&list
, section
,
1345 (lang_output_section_statement_type
*) output
,
1348 /* If we are discarding the section, LIST.HEAD will
1350 if (list
.head
!= NULL
)
1352 ASSERT (list
.head
->next
== NULL
);
1354 for (pp
= &ptr
->children
.head
;
1357 ASSERT (*pp
!= NULL
);
1359 list
.head
->next
= *pp
;
1365 /* This is passed a file name which must have been seen already and
1366 added to the statement tree. We will see if it has been opened
1367 already and had its symbols read. If not then we'll read it. */
1369 static lang_input_statement_type
*
1373 lang_input_statement_type
*search
;
1375 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1376 search
!= (lang_input_statement_type
*) NULL
;
1377 search
= (lang_input_statement_type
*) search
->next_real_file
)
1379 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1381 if (search
->filename
!= (char *) NULL
1382 && name
!= (char *) NULL
1383 && strcmp (search
->filename
, name
) == 0)
1387 if (search
== (lang_input_statement_type
*) NULL
)
1388 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1391 /* If we have already added this file, or this file is not real
1392 (FIXME: can that ever actually happen?) or the name is NULL
1393 (FIXME: can that ever actually happen?) don't add this file. */
1396 || search
->filename
== (const char *) NULL
)
1399 load_symbols (search
, (lang_statement_list_type
*) NULL
);
1404 /* Get the symbols for an input file. */
1407 load_symbols (entry
, place
)
1408 lang_input_statement_type
*entry
;
1409 lang_statement_list_type
*place
;
1416 ldfile_open_file (entry
);
1418 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1419 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1422 lang_statement_list_type
*hold
;
1424 err
= bfd_get_error ();
1425 if (err
== bfd_error_file_ambiguously_recognized
)
1429 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1430 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1431 for (p
= matching
; *p
!= NULL
; p
++)
1435 else if (err
!= bfd_error_file_not_recognized
1437 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1439 bfd_close (entry
->the_bfd
);
1440 entry
->the_bfd
= NULL
;
1442 /* See if the emulation has some special knowledge. */
1444 if (ldemul_unrecognized_file (entry
))
1447 /* Try to interpret the file as a linker script. */
1449 ldfile_open_command_file (entry
->filename
);
1454 ldfile_assumed_script
= true;
1455 parser_input
= input_script
;
1457 ldfile_assumed_script
= false;
1464 if (ldemul_recognized_file (entry
))
1467 /* We don't call ldlang_add_file for an archive. Instead, the
1468 add_symbols entry point will call ldlang_add_file, via the
1469 add_archive_element callback, for each element of the archive
1471 switch (bfd_get_format (entry
->the_bfd
))
1477 ldlang_add_file (entry
);
1478 if (trace_files
|| trace_file_tries
)
1479 info_msg ("%I\n", entry
);
1483 if (entry
->whole_archive
)
1485 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1487 while (member
!= NULL
)
1489 if (! bfd_check_format (member
, bfd_object
))
1490 einfo (_("%F%B: object %B in archive is not object\n"),
1491 entry
->the_bfd
, member
);
1492 if (! ((*link_info
.callbacks
->add_archive_element
)
1493 (&link_info
, member
, "--whole-archive")))
1495 if (! bfd_link_add_symbols (member
, &link_info
))
1496 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1497 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1501 entry
->loaded
= true;
1507 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1508 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1510 entry
->loaded
= true;
1515 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1516 indicating that it is a wildcard. Separate lang_input_section
1517 statements are created for each part of the expansion; they are
1518 added after the wild statement S. OUTPUT is the output section. */
1521 wild (s
, section
, file
, target
, output
)
1522 lang_wild_statement_type
*s
;
1523 const char *section
;
1525 const char *target ATTRIBUTE_UNUSED
;
1526 lang_output_section_statement_type
*output
;
1528 walk_wild (s
, section
, file
, output_section_callback
, (void *) output
);
1530 if (section
!= (char *) NULL
1531 && strcmp (section
, "COMMON") == 0
1532 && default_common_section
== NULL
)
1534 /* Remember the section that common is going to in case we later
1535 get something which doesn't know where to put it. */
1536 default_common_section
= output
;
1540 /* Return true iff target is the sought target. */
1542 get_target (target
, data
)
1543 const bfd_target
* target
;
1546 const char * sought
= (const char *) data
;
1548 return strcmp (target
->name
, sought
) == 0;
1551 /* Like strcpy() but convert to lower case as well. */
1559 while ((c
= * src
++) != 0)
1561 if (isupper ((unsigned char) c
))
1570 /* Remove the first occurance of needle (if any) in haystack
1573 strcut (haystack
, needle
)
1577 haystack
= strstr (haystack
, needle
);
1583 for (src
= haystack
+ strlen (needle
); * src
;)
1584 * haystack
++ = * src
++;
1590 /* Compare two target format name strings.
1591 Return a value indicating how "similar" they are. */
1593 name_compare (first
, second
)
1601 copy1
= xmalloc (strlen (first
) + 1);
1602 copy2
= xmalloc (strlen (second
) + 1);
1604 /* Convert the names to lower case. */
1605 stricpy (copy1
, first
);
1606 stricpy (copy2
, second
);
1608 /* Remove and endian strings from the name. */
1609 strcut (copy1
, "big");
1610 strcut (copy1
, "little");
1611 strcut (copy2
, "big");
1612 strcut (copy2
, "little");
1614 /* Return a value based on how many characters match,
1615 starting from the beginning. If both strings are
1616 the same then return 10 * their length. */
1617 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1618 if (copy1
[result
] == 0)
1630 /* Set by closest_target_match() below. */
1631 static const bfd_target
* winner
;
1633 /* Scan all the valid bfd targets looking for one that has the endianness
1634 requirement that was specified on the command line, and is the nearest
1635 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
&& target
->byteorder
!= BFD_ENDIAN_BIG
)
1646 if (command_line
.endian
== ENDIAN_LITTLE
&& target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1649 /* Must be the same flavour. */
1650 if (target
->flavour
!= original
->flavour
)
1653 /* If we have not found a potential winner yet, then record this one. */
1660 /* Oh dear, we now have two potential candidates for a successful match.
1661 Compare their names and choose the better one. */
1662 if (name_compare (target
->name
, original
->name
) > name_compare (winner
->name
, original
->name
))
1665 /* Keep on searching until wqe have checked them all. */
1669 /* Return the BFD target format of the first input file. */
1671 get_first_input_target ()
1673 char * target
= NULL
;
1675 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1677 if (s
->header
.type
== lang_input_statement_enum
1680 ldfile_open_file (s
);
1682 if (s
->the_bfd
!= NULL
1683 && bfd_check_format (s
->the_bfd
, bfd_object
))
1685 target
= bfd_get_target (s
->the_bfd
);
1696 /* Open the output file. */
1704 /* Has the user told us which output format to use ? */
1705 if (output_target
== (char *) NULL
)
1707 /* No - has the current target been set to something other than the default ? */
1708 if (current_target
!= default_target
)
1709 output_target
= current_target
;
1711 /* No - can we determine the format of the first input file ? */
1714 output_target
= get_first_input_target ();
1716 /* Failed - use the default output target. */
1717 if (output_target
== NULL
)
1718 output_target
= default_target
;
1722 /* Has the user requested a particular endianness on the command line ? */
1723 if (command_line
.endian
!= ENDIAN_UNSET
)
1725 const bfd_target
* target
;
1726 enum bfd_endian desired_endian
;
1728 /* Get the chosen target. */
1729 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1731 if (command_line
.endian
== ENDIAN_BIG
)
1732 desired_endian
= BFD_ENDIAN_BIG
;
1734 desired_endian
= BFD_ENDIAN_LITTLE
;
1736 /* See if the target has the wrong endianness. This should not happen
1737 if the linker script has provided big and little endian alternatives,
1738 but some scrips don't do this. */
1739 if (target
->byteorder
!= desired_endian
)
1741 /* If it does, then see if the target provides
1742 an alternative with the correct endianness. */
1743 if (target
->alternative_target
!= NULL
1744 && (target
->alternative_target
->byteorder
== desired_endian
))
1745 output_target
= target
->alternative_target
->name
;
1748 /* Try to find a target as similar as possible to the default
1749 target, but which has the desired endian characteristic. */
1750 (void) bfd_search_for_target (closest_target_match
, (void *) target
);
1752 /* Oh dear - we could not find any targets that satisfy our requirements. */
1754 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1756 output_target
= winner
->name
;
1761 output
= bfd_openw (name
, output_target
);
1763 if (output
== (bfd
*) NULL
)
1765 if (bfd_get_error () == bfd_error_invalid_target
)
1766 einfo (_("%P%F: target %s not found\n"), output_target
);
1768 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1771 delete_output_file_on_failure
= true;
1773 /* output->flags |= D_PAGED;*/
1775 if (! bfd_set_format (output
, bfd_object
))
1776 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1777 if (! bfd_set_arch_mach (output
,
1778 ldfile_output_architecture
,
1779 ldfile_output_machine
))
1780 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1782 link_info
.hash
= bfd_link_hash_table_create (output
);
1783 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1784 einfo (_("%P%F: can not create link hash table: %E\n"));
1786 bfd_set_gp_size (output
, g_switch_value
);
1791 ldlang_open_output (statement
)
1792 lang_statement_union_type
* statement
;
1794 switch (statement
->header
.type
)
1796 case lang_output_statement_enum
:
1797 ASSERT (output_bfd
== (bfd
*) NULL
);
1798 output_bfd
= open_output (statement
->output_statement
.name
);
1799 ldemul_set_output_arch ();
1800 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1801 output_bfd
->flags
|= D_PAGED
;
1803 output_bfd
->flags
&= ~D_PAGED
;
1804 if (config
.text_read_only
)
1805 output_bfd
->flags
|= WP_TEXT
;
1807 output_bfd
->flags
&= ~WP_TEXT
;
1808 if (link_info
.traditional_format
)
1809 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1811 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1814 case lang_target_statement_enum
:
1815 current_target
= statement
->target_statement
.target
;
1822 /* Open all the input files. */
1825 open_input_bfds (s
, force
)
1826 lang_statement_union_type
*s
;
1829 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1831 switch (s
->header
.type
)
1833 case lang_constructors_statement_enum
:
1834 open_input_bfds (constructor_list
.head
, force
);
1836 case lang_output_section_statement_enum
:
1837 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1839 case lang_wild_statement_enum
:
1840 /* Maybe we should load the file's symbols */
1841 if (s
->wild_statement
.filename
1842 && ! wildcardp (s
->wild_statement
.filename
))
1843 (void) lookup_name (s
->wild_statement
.filename
);
1844 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1846 case lang_group_statement_enum
:
1848 struct bfd_link_hash_entry
*undefs
;
1850 /* We must continually search the entries in the group
1851 until no new symbols are added to the list of undefined
1856 undefs
= link_info
.hash
->undefs_tail
;
1857 open_input_bfds (s
->group_statement
.children
.head
, true);
1859 while (undefs
!= link_info
.hash
->undefs_tail
);
1862 case lang_target_statement_enum
:
1863 current_target
= s
->target_statement
.target
;
1865 case lang_input_statement_enum
:
1866 if (s
->input_statement
.real
)
1868 lang_statement_list_type add
;
1870 s
->input_statement
.target
= current_target
;
1872 /* If we are being called from within a group, and this
1873 is an archive which has already been searched, then
1874 force it to be researched unless the whole archive
1875 has been loaded already. */
1877 && !s
->input_statement
.whole_archive
1878 && s
->input_statement
.loaded
1879 && bfd_check_format (s
->input_statement
.the_bfd
,
1881 s
->input_statement
.loaded
= false;
1883 lang_list_init (&add
);
1885 load_symbols (&s
->input_statement
, &add
);
1887 if (add
.head
!= NULL
)
1889 *add
.tail
= s
->next
;
1900 /* If there are [COMMONS] statements, put a wild one into the bss section */
1903 lang_reasonable_defaults ()
1906 lang_output_section_statement_lookup (".text");
1907 lang_output_section_statement_lookup (".data");
1909 default_common_section
=
1910 lang_output_section_statement_lookup (".bss");
1913 if (placed_commons
== false)
1915 lang_wild_statement_type
*new =
1916 new_stat (lang_wild_statement
,
1917 &default_common_section
->children
);
1919 new->section_name
= "COMMON";
1920 new->filename
= (char *) NULL
;
1921 lang_list_init (&new->children
);
1928 Add the supplied name to the symbol table as an undefined reference.
1929 Remove items from the chain as we open input bfds
1931 typedef struct ldlang_undef_chain_list
1933 struct ldlang_undef_chain_list
*next
;
1935 } ldlang_undef_chain_list_type
;
1937 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1940 ldlang_add_undef (name
)
1941 CONST
char *CONST name
;
1943 ldlang_undef_chain_list_type
*new =
1944 ((ldlang_undef_chain_list_type
*)
1945 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1947 new->next
= ldlang_undef_chain_list_head
;
1948 ldlang_undef_chain_list_head
= new;
1950 new->name
= buystring (name
);
1953 /* Run through the list of undefineds created above and place them
1954 into the linker hash table as undefined symbols belonging to the
1958 lang_place_undefineds ()
1960 ldlang_undef_chain_list_type
*ptr
;
1962 for (ptr
= ldlang_undef_chain_list_head
;
1963 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1966 struct bfd_link_hash_entry
*h
;
1968 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1969 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1970 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1971 if (h
->type
== bfd_link_hash_new
)
1973 h
->type
= bfd_link_hash_undefined
;
1974 h
->u
.undef
.abfd
= NULL
;
1975 bfd_link_add_undef (link_info
.hash
, h
);
1980 /* Open input files and attatch to output sections */
1982 map_input_to_output_sections (s
, target
, output_section_statement
)
1983 lang_statement_union_type
* s
;
1985 lang_output_section_statement_type
* output_section_statement
;
1987 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1989 switch (s
->header
.type
)
1993 case lang_wild_statement_enum
:
1994 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1995 s
->wild_statement
.filename
, target
,
1996 output_section_statement
);
1999 case lang_constructors_statement_enum
:
2000 map_input_to_output_sections (constructor_list
.head
,
2002 output_section_statement
);
2004 case lang_output_section_statement_enum
:
2005 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2007 &s
->output_section_statement
);
2009 case lang_output_statement_enum
:
2011 case lang_target_statement_enum
:
2012 target
= s
->target_statement
.target
;
2014 case lang_group_statement_enum
:
2015 map_input_to_output_sections (s
->group_statement
.children
.head
,
2017 output_section_statement
);
2019 case lang_fill_statement_enum
:
2020 case lang_input_section_enum
:
2021 case lang_object_symbols_statement_enum
:
2022 case lang_data_statement_enum
:
2023 case lang_reloc_statement_enum
:
2024 case lang_padding_statement_enum
:
2025 case lang_input_statement_enum
:
2026 if (output_section_statement
!= NULL
2027 && output_section_statement
->bfd_section
== NULL
)
2028 init_os (output_section_statement
);
2030 case lang_assignment_statement_enum
:
2031 if (output_section_statement
!= NULL
2032 && output_section_statement
->bfd_section
== NULL
)
2033 init_os (output_section_statement
);
2035 /* Make sure that any sections mentioned in the assignment
2037 exp_init_os (s
->assignment_statement
.exp
);
2039 case lang_afile_asection_pair_statement_enum
:
2042 case lang_address_statement_enum
:
2043 /* Mark the specified section with the supplied address */
2045 lang_output_section_statement_type
*os
=
2046 lang_output_section_statement_lookup
2047 (s
->address_statement
.section_name
);
2049 if (os
->bfd_section
== NULL
)
2051 os
->addr_tree
= s
->address_statement
.address
;
2059 print_output_section_statement (output_section_statement
)
2060 lang_output_section_statement_type
* output_section_statement
;
2062 asection
*section
= output_section_statement
->bfd_section
;
2065 if (output_section_statement
!= abs_output_section
)
2067 minfo ("\n%s", output_section_statement
->name
);
2069 if (section
!= NULL
)
2071 print_dot
= section
->vma
;
2073 len
= strlen (output_section_statement
->name
);
2074 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2079 while (len
< SECTION_NAME_MAP_LENGTH
)
2085 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2087 if (output_section_statement
->load_base
!= NULL
)
2091 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2092 "load base", lang_final_phase_enum
);
2093 minfo (_(" load address 0x%V"), addr
);
2100 print_statement_list (output_section_statement
->children
.head
,
2101 output_section_statement
);
2105 print_assignment (assignment
, output_section
)
2106 lang_assignment_statement_type
* assignment
;
2107 lang_output_section_statement_type
* output_section
;
2110 etree_value_type result
;
2112 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2115 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2116 lang_final_phase_enum
, print_dot
, &print_dot
);
2118 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2129 exp_print_tree (assignment
->exp
);
2135 print_input_statement (statm
)
2136 lang_input_statement_type
* statm
;
2138 if (statm
->filename
!= (char *) NULL
)
2140 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2144 /* Print all symbols defined in a particular section. This is called
2145 via bfd_link_hash_traverse. */
2148 print_one_symbol (hash_entry
, ptr
)
2149 struct bfd_link_hash_entry
*hash_entry
;
2152 asection
*sec
= (asection
*) ptr
;
2154 if ((hash_entry
->type
== bfd_link_hash_defined
2155 || hash_entry
->type
== bfd_link_hash_defweak
)
2156 && sec
== hash_entry
->u
.def
.section
)
2160 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2163 (hash_entry
->u
.def
.value
2164 + hash_entry
->u
.def
.section
->output_offset
2165 + hash_entry
->u
.def
.section
->output_section
->vma
));
2167 minfo (" %T\n", hash_entry
->root
.string
);
2173 /* Print information about an input section to the map file. */
2176 print_input_section (in
)
2177 lang_input_section_type
* in
;
2179 asection
*i
= in
->section
;
2180 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2181 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2182 ldfile_output_machine
);
2187 minfo ("%s", i
->name
);
2189 if (i
->output_section
!= NULL
)
2193 len
= 1 + strlen (i
->name
);
2194 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2199 while (len
< SECTION_NAME_MAP_LENGTH
)
2205 minfo ("0x%V %W %B\n",
2206 i
->output_section
->vma
+ i
->output_offset
, size
/ opb
,
2209 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2211 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2223 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2226 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2228 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
/ opb
;
2234 print_fill_statement (fill
)
2235 lang_fill_statement_type
* fill
;
2237 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2241 print_data_statement (data
)
2242 lang_data_statement_type
* data
;
2248 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2249 ldfile_output_machine
);
2251 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2254 addr
= data
->output_vma
;
2255 if (data
->output_section
!= NULL
)
2256 addr
+= data
->output_section
->vma
;
2284 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2286 if (data
->exp
->type
.node_class
!= etree_value
)
2289 exp_print_tree (data
->exp
);
2294 print_dot
= addr
+ size
/ opb
;
2298 /* Print an address statement. These are generated by options like
2302 print_address_statement (address
)
2303 lang_address_statement_type
*address
;
2305 minfo (_("Address of section %s set to "), address
->section_name
);
2306 exp_print_tree (address
->address
);
2310 /* Print a reloc statement. */
2313 print_reloc_statement (reloc
)
2314 lang_reloc_statement_type
*reloc
;
2319 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2320 ldfile_output_machine
);
2322 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2325 addr
= reloc
->output_vma
;
2326 if (reloc
->output_section
!= NULL
)
2327 addr
+= reloc
->output_section
->vma
;
2329 size
= bfd_get_reloc_size (reloc
->howto
);
2331 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2333 if (reloc
->name
!= NULL
)
2334 minfo ("%s+", reloc
->name
);
2336 minfo ("%s+", reloc
->section
->name
);
2338 exp_print_tree (reloc
->addend_exp
);
2342 print_dot
= addr
+ size
/ opb
;
2346 print_padding_statement (s
)
2347 lang_padding_statement_type
*s
;
2351 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2352 ldfile_output_machine
);
2356 len
= sizeof " *fill*" - 1;
2357 while (len
< SECTION_NAME_MAP_LENGTH
)
2363 addr
= s
->output_offset
;
2364 if (s
->output_section
!= NULL
)
2365 addr
+= s
->output_section
->vma
;
2366 minfo ("0x%V %W", addr
, s
->size
);
2369 minfo (" %u", s
->fill
);
2373 print_dot
= addr
+ s
->size
/ opb
;
2377 print_wild_statement (w
, os
)
2378 lang_wild_statement_type
* w
;
2379 lang_output_section_statement_type
* os
;
2383 if (w
->filenames_sorted
)
2385 if (w
->exclude_filename_list
!= NULL
)
2388 minfo ("EXCLUDE_FILE ( %s", w
->exclude_filename_list
->name
);
2389 for (tmp
=w
->exclude_filename_list
->next
; tmp
; tmp
= tmp
->next
)
2390 minfo (", %s", tmp
->name
);
2393 if (w
->filename
!= NULL
)
2394 minfo ("%s", w
->filename
);
2397 if (w
->filenames_sorted
)
2401 if (w
->sections_sorted
)
2403 if (w
->section_name
!= NULL
)
2404 minfo ("%s", w
->section_name
);
2407 if (w
->sections_sorted
)
2413 print_statement_list (w
->children
.head
, os
);
2416 /* Print a group statement. */
2420 lang_group_statement_type
*s
;
2421 lang_output_section_statement_type
*os
;
2423 fprintf (config
.map_file
, "START GROUP\n");
2424 print_statement_list (s
->children
.head
, os
);
2425 fprintf (config
.map_file
, "END GROUP\n");
2428 /* Print the list of statements in S.
2429 This can be called for any statement type. */
2432 print_statement_list (s
, os
)
2433 lang_statement_union_type
*s
;
2434 lang_output_section_statement_type
*os
;
2438 print_statement (s
, os
);
2443 /* Print the first statement in statement list S.
2444 This can be called for any statement type. */
2447 print_statement (s
, os
)
2448 lang_statement_union_type
*s
;
2449 lang_output_section_statement_type
*os
;
2451 switch (s
->header
.type
)
2454 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2457 case lang_constructors_statement_enum
:
2458 if (constructor_list
.head
!= NULL
)
2460 if (constructors_sorted
)
2461 minfo (" SORT (CONSTRUCTORS)\n");
2463 minfo (" CONSTRUCTORS\n");
2464 print_statement_list (constructor_list
.head
, os
);
2467 case lang_wild_statement_enum
:
2468 print_wild_statement (&s
->wild_statement
, os
);
2470 case lang_address_statement_enum
:
2471 print_address_statement (&s
->address_statement
);
2473 case lang_object_symbols_statement_enum
:
2474 minfo (" CREATE_OBJECT_SYMBOLS\n");
2476 case lang_fill_statement_enum
:
2477 print_fill_statement (&s
->fill_statement
);
2479 case lang_data_statement_enum
:
2480 print_data_statement (&s
->data_statement
);
2482 case lang_reloc_statement_enum
:
2483 print_reloc_statement (&s
->reloc_statement
);
2485 case lang_input_section_enum
:
2486 print_input_section (&s
->input_section
);
2488 case lang_padding_statement_enum
:
2489 print_padding_statement (&s
->padding_statement
);
2491 case lang_output_section_statement_enum
:
2492 print_output_section_statement (&s
->output_section_statement
);
2494 case lang_assignment_statement_enum
:
2495 print_assignment (&s
->assignment_statement
, os
);
2497 case lang_target_statement_enum
:
2498 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2500 case lang_output_statement_enum
:
2501 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2502 if (output_target
!= NULL
)
2503 minfo (" %s", output_target
);
2506 case lang_input_statement_enum
:
2507 print_input_statement (&s
->input_statement
);
2509 case lang_group_statement_enum
:
2510 print_group (&s
->group_statement
, os
);
2512 case lang_afile_asection_pair_statement_enum
:
2521 print_statement_list (statement_list
.head
, abs_output_section
);
2524 /* Print the first N statements in statement list S to STDERR.
2525 If N == 0, nothing is printed.
2526 If N < 0, the entire list is printed.
2527 Intended to be called from GDB. */
2530 dprint_statement (s
, n
)
2531 lang_statement_union_type
* s
;
2534 FILE *map_save
= config
.map_file
;
2536 config
.map_file
= stderr
;
2539 print_statement_list (s
, abs_output_section
);
2542 while (s
&& --n
>= 0)
2544 print_statement (s
, abs_output_section
);
2549 config
.map_file
= map_save
;
2553 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
2554 lang_statement_union_type
** this_ptr
;
2557 asection
* output_section_statement
;
2560 /* Align this section first to the
2561 input sections requirement, then
2562 to the output section's requirement.
2563 If this alignment is > than any seen before,
2564 then record it too. Perform the alignment by
2565 inserting a magic 'padding' statement.
2568 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2569 ldfile_output_machine
);
2570 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
2572 if (alignment_needed
!= 0)
2574 lang_statement_union_type
*new =
2575 ((lang_statement_union_type
*)
2576 stat_alloc (sizeof (lang_padding_statement_type
)));
2578 /* Link into existing chain */
2579 new->header
.next
= *this_ptr
;
2581 new->header
.type
= lang_padding_statement_enum
;
2582 new->padding_statement
.output_section
= output_section_statement
;
2583 new->padding_statement
.output_offset
=
2584 dot
- output_section_statement
->vma
;
2585 new->padding_statement
.fill
= fill
;
2586 new->padding_statement
.size
= alignment_needed
* opb
;
2590 /* Remember the most restrictive alignment */
2591 if (power
> output_section_statement
->alignment_power
)
2593 output_section_statement
->alignment_power
= power
;
2595 output_section_statement
->_raw_size
+= alignment_needed
* opb
;
2597 return dot
+ alignment_needed
;
2600 /* Work out how much this section will move the dot point */
2602 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
2603 lang_statement_union_type
** this_ptr
;
2604 lang_output_section_statement_type
* output_section_statement
;
2607 boolean relax ATTRIBUTE_UNUSED
;
2609 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2610 asection
*i
= is
->section
;
2611 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2612 ldfile_output_machine
);
2614 if (is
->ifile
->just_syms_flag
== false)
2616 if (output_section_statement
->subsection_alignment
!= -1)
2617 i
->alignment_power
=
2618 output_section_statement
->subsection_alignment
;
2620 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
2621 output_section_statement
->bfd_section
, dot
);
2623 /* Remember where in the output section this input section goes */
2625 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
2627 /* Mark how big the output section must be to contain this now
2629 if (i
->_cooked_size
!= 0)
2630 dot
+= i
->_cooked_size
/ opb
;
2632 dot
+= i
->_raw_size
/ opb
;
2633 output_section_statement
->bfd_section
->_raw_size
=
2634 (dot
- output_section_statement
->bfd_section
->vma
) * opb
;
2638 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2644 #define IGNORE_SECTION(bfd, s) \
2645 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
2646 || bfd_section_size (bfd, s) == 0)
2648 /* Check to see if any allocated sections overlap with other allocated
2649 sections. This can happen when the linker script specifically specifies
2650 the output section addresses of the two sections. */
2652 lang_check_section_addresses ()
2655 unsigned opb
= bfd_octets_per_byte (output_bfd
);
2657 /* Scan all sections in the output list. */
2658 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2662 /* Ignore sections which are not loaded or which have no contents. */
2663 if (IGNORE_SECTION (output_bfd
, s
))
2666 /* Once we reach section 's' stop our seach. This prevents two
2667 warning messages from being produced, one for 'section A overlaps
2668 section B' and one for 'section B overlaps section A'. */
2669 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2676 /* Only consider loadable sections with real contents. */
2677 if (IGNORE_SECTION (output_bfd
, os
))
2680 /* We must check the sections' LMA addresses not their
2681 VMA addresses because overlay sections can have
2682 overlapping VMAs but they must have distinct LMAs. */
2683 s_start
= bfd_section_lma (output_bfd
, s
);
2684 os_start
= bfd_section_lma (output_bfd
, os
);
2685 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) / opb
- 1;
2686 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) / opb
- 1;
2688 /* Look for an overlap. */
2689 if ((s_end
< os_start
) || (s_start
> os_end
))
2693 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2694 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2696 /* Once we have found one overlap for this section,
2697 stop looking for others. */
2703 /* This variable indicates whether bfd_relax_section should be called
2706 static boolean relax_again
;
2708 /* Make sure the new address is within the region. We explicitly permit the
2709 current address to be at the exact end of the region when the address is
2710 non-zero, in case the region is at the end of addressable memory and the
2711 calculation wraps around. */
2714 os_region_check (os
, region
, tree
, base
)
2715 lang_output_section_statement_type
*os
;
2716 struct memory_region_struct
*region
;
2720 if ((region
->current
< region
->origin
2721 || (region
->current
- region
->origin
> region
->length
))
2722 && ((region
->current
!= region
->origin
+ region
->length
)
2725 if (tree
!= (etree_type
*) NULL
)
2727 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2729 os
->bfd_section
->owner
,
2730 os
->bfd_section
->name
,
2735 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2737 os
->bfd_section
->owner
,
2738 os
->bfd_section
->name
);
2740 /* Reset the region pointer. */
2741 region
->current
= region
->origin
;
2745 /* Set the sizes for all the output sections. */
2748 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2749 lang_statement_union_type
* s
;
2750 lang_output_section_statement_type
* output_section_statement
;
2751 lang_statement_union_type
** prev
;
2756 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2757 ldfile_output_machine
);
2759 /* Size up the sections from their constituent parts. */
2760 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2762 switch (s
->header
.type
)
2764 case lang_output_section_statement_enum
:
2767 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2769 if (os
->bfd_section
== NULL
)
2770 /* This section was never actually created. */
2773 /* If this is a COFF shared library section, use the size and
2774 address from the input section. FIXME: This is COFF
2775 specific; it would be cleaner if there were some other way
2776 to do this, but nothing simple comes to mind. */
2777 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2781 if (os
->children
.head
== NULL
2782 || os
->children
.head
->next
!= NULL
2783 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2784 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2787 input
= os
->children
.head
->input_section
.section
;
2788 bfd_set_section_vma (os
->bfd_section
->owner
,
2790 bfd_section_vma (input
->owner
, input
));
2791 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2795 if (bfd_is_abs_section (os
->bfd_section
))
2797 /* No matter what happens, an abs section starts at zero. */
2798 ASSERT (os
->bfd_section
->vma
== 0);
2802 if (os
->addr_tree
== (etree_type
*) NULL
)
2804 /* No address specified for this section, get one
2805 from the region specification. */
2806 if (os
->region
== (lang_memory_region_type
*) NULL
2807 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2808 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2809 && os
->region
->name
[0] == '*'
2810 && strcmp (os
->region
->name
, "*default*") == 0))
2812 os
->region
= lang_memory_default (os
->bfd_section
);
2815 /* If a loadable section is using the default memory
2816 region, and some non default memory regions were
2817 defined, issue a warning. */
2818 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2819 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2820 && ! link_info
.relocateable
2821 && strcmp (os
->region
->name
, "*default*") == 0
2822 && lang_memory_region_list
!= NULL
2823 && (strcmp (lang_memory_region_list
->name
, "*default*") != 0
2824 || lang_memory_region_list
->next
!= NULL
))
2825 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2826 bfd_get_section_name (output_bfd
, os
->bfd_section
));
2828 dot
= os
->region
->current
;
2830 if (os
->section_alignment
== -1)
2835 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2837 if (dot
!= olddot
&& config
.warn_section_align
)
2838 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2839 os
->name
, (unsigned int) (dot
- olddot
));
2846 r
= exp_fold_tree (os
->addr_tree
,
2848 lang_allocating_phase_enum
,
2850 if (r
.valid_p
== false)
2852 einfo (_("%F%S: non constant address expression for section %s\n"),
2855 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2858 /* The section starts here.
2859 First, align to what the section needs. */
2861 if (os
->section_alignment
!= -1)
2862 dot
= align_power (dot
, os
->section_alignment
);
2864 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2866 os
->bfd_section
->output_offset
= 0;
2869 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2870 os
->fill
, dot
, relax
);
2872 /* put the section within the requested block size, or align at
2873 the block boundary */
2874 after
= ALIGN_N (os
->bfd_section
->vma
2875 + os
->bfd_section
->_raw_size
/ opb
,
2876 /* The coercion here is important, see ld.h. */
2877 (bfd_vma
) os
->block_value
);
2879 if (bfd_is_abs_section (os
->bfd_section
))
2880 ASSERT (after
== os
->bfd_section
->vma
);
2882 os
->bfd_section
->_raw_size
=
2883 (after
- os
->bfd_section
->vma
) * opb
;
2884 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
2885 os
->processed
= true;
2887 /* Update dot in the region ?
2888 We only do this if the section is going to be allocated,
2889 since unallocated sections do not contribute to the region's
2890 overall size in memory.
2892 If the SEC_NEVER_LOAD bit is not set, it will affect the
2893 addresses of sections after it. We have to update
2895 if (os
->region
!= (lang_memory_region_type
*) NULL
2896 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2897 & SEC_NEVER_LOAD
) == 0
2898 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2899 & (SEC_ALLOC
| SEC_LOAD
))))
2901 os
->region
->current
= dot
;
2903 /* Make sure the new address is within the region. */
2904 os_region_check (os
, os
->region
, os
->addr_tree
,
2905 os
->bfd_section
->vma
);
2907 /* if there's no load address specified, use the run region as
2909 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
2910 os
->lma_region
= os
->region
;
2912 if (os
->lma_region
!= NULL
)
2914 if (os
->load_base
!= NULL
)
2916 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
2920 /* don't allocate twice */
2921 if (os
->lma_region
!= os
->region
)
2923 /* set load_base, which will be handled later */
2924 os
->load_base
= exp_intop (os
->lma_region
->current
);
2925 os
->lma_region
->current
+=
2926 os
->bfd_section
->_raw_size
/ opb
;
2927 os_region_check (os
, os
->lma_region
, NULL
,
2928 os
->bfd_section
->lma
);
2936 case lang_constructors_statement_enum
:
2937 dot
= lang_size_sections (constructor_list
.head
,
2938 output_section_statement
,
2939 &s
->wild_statement
.children
.head
,
2944 case lang_data_statement_enum
:
2946 unsigned int size
= 0;
2948 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2949 s
->data_statement
.output_section
=
2950 output_section_statement
->bfd_section
;
2952 switch (s
->data_statement
.type
)
2973 output_section_statement
->bfd_section
->_raw_size
+= size
;
2974 /* The output section gets contents, and then we inspect for
2975 any flags set in the input script which override any ALLOC. */
2976 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2977 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2978 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2983 case lang_reloc_statement_enum
:
2987 s
->reloc_statement
.output_vma
=
2988 dot
- output_section_statement
->bfd_section
->vma
;
2989 s
->reloc_statement
.output_section
=
2990 output_section_statement
->bfd_section
;
2991 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2993 output_section_statement
->bfd_section
->_raw_size
+= size
;
2997 case lang_wild_statement_enum
:
2999 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
3000 output_section_statement
,
3001 &s
->wild_statement
.children
.head
,
3007 case lang_object_symbols_statement_enum
:
3008 link_info
.create_object_symbols_section
=
3009 output_section_statement
->bfd_section
;
3011 case lang_output_statement_enum
:
3012 case lang_target_statement_enum
:
3014 case lang_input_section_enum
:
3018 i
= (*prev
)->input_section
.section
;
3021 if (i
->_cooked_size
== 0)
3022 i
->_cooked_size
= i
->_raw_size
;
3028 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3029 einfo (_("%P%F: can't relax section: %E\n"));
3033 dot
= size_input_section (prev
,
3034 output_section_statement
,
3035 output_section_statement
->fill
,
3039 case lang_input_statement_enum
:
3041 case lang_fill_statement_enum
:
3042 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
3044 fill
= s
->fill_statement
.fill
;
3046 case lang_assignment_statement_enum
:
3048 bfd_vma newdot
= dot
;
3050 exp_fold_tree (s
->assignment_statement
.exp
,
3051 output_section_statement
,
3052 lang_allocating_phase_enum
,
3058 /* The assignment changed dot. Insert a pad. */
3059 if (output_section_statement
== abs_output_section
)
3061 /* If we don't have an output section, then just adjust
3062 the default memory address. */
3063 lang_memory_region_lookup ("*default*")->current
= newdot
;
3067 lang_statement_union_type
*new =
3068 ((lang_statement_union_type
*)
3069 stat_alloc (sizeof (lang_padding_statement_type
)));
3071 /* Link into existing chain. */
3072 new->header
.next
= *prev
;
3074 new->header
.type
= lang_padding_statement_enum
;
3075 new->padding_statement
.output_section
=
3076 output_section_statement
->bfd_section
;
3077 new->padding_statement
.output_offset
=
3078 dot
- output_section_statement
->bfd_section
->vma
;
3079 new->padding_statement
.fill
= fill
;
3080 new->padding_statement
.size
= (newdot
- dot
) * opb
;
3081 output_section_statement
->bfd_section
->_raw_size
+=
3082 new->padding_statement
.size
;
3090 case lang_padding_statement_enum
:
3091 /* If we are relaxing, and this is not the first pass, some
3092 padding statements may have been inserted during previous
3093 passes. We may have to move the padding statement to a new
3094 location if dot has a different value at this point in this
3095 pass than it did at this point in the previous pass. */
3096 s
->padding_statement
.output_offset
=
3097 dot
- output_section_statement
->bfd_section
->vma
;
3098 dot
+= s
->padding_statement
.size
/ opb
;
3099 output_section_statement
->bfd_section
->_raw_size
+=
3100 s
->padding_statement
.size
;
3103 case lang_group_statement_enum
:
3104 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3105 output_section_statement
,
3106 &s
->group_statement
.children
.head
,
3114 /* This can only get here when relaxing is turned on. */
3116 case lang_address_statement_enum
:
3119 prev
= &s
->header
.next
;
3125 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3126 lang_statement_union_type
* s
;
3127 lang_output_section_statement_type
* output_section_statement
;
3131 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3132 ldfile_output_machine
);
3134 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3136 switch (s
->header
.type
)
3138 case lang_constructors_statement_enum
:
3139 dot
= lang_do_assignments (constructor_list
.head
,
3140 output_section_statement
,
3145 case lang_output_section_statement_enum
:
3147 lang_output_section_statement_type
*os
=
3148 &(s
->output_section_statement
);
3150 if (os
->bfd_section
!= NULL
)
3152 dot
= os
->bfd_section
->vma
;
3153 (void) lang_do_assignments (os
->children
.head
, os
,
3155 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3160 /* If nothing has been placed into the output section then
3161 it won't have a bfd_section. */
3162 if (os
->bfd_section
)
3164 os
->bfd_section
->lma
3165 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
3170 case lang_wild_statement_enum
:
3172 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3173 output_section_statement
,
3178 case lang_object_symbols_statement_enum
:
3179 case lang_output_statement_enum
:
3180 case lang_target_statement_enum
:
3182 case lang_common_statement_enum
:
3185 case lang_data_statement_enum
:
3187 etree_value_type value
;
3189 value
= exp_fold_tree (s
->data_statement
.exp
,
3191 lang_final_phase_enum
, dot
, &dot
);
3192 s
->data_statement
.value
= value
.value
;
3193 if (value
.valid_p
== false)
3194 einfo (_("%F%P: invalid data statement\n"));
3198 switch (s
->data_statement
.type
)
3222 case lang_reloc_statement_enum
:
3224 etree_value_type value
;
3226 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3228 lang_final_phase_enum
, dot
, &dot
);
3229 s
->reloc_statement
.addend_value
= value
.value
;
3230 if (value
.valid_p
== false)
3231 einfo (_("%F%P: invalid reloc statement\n"));
3233 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
) / opb
;
3236 case lang_input_section_enum
:
3238 asection
*in
= s
->input_section
.section
;
3240 if (in
->_cooked_size
!= 0)
3241 dot
+= in
->_cooked_size
/ opb
;
3243 dot
+= in
->_raw_size
/ opb
;
3247 case lang_input_statement_enum
:
3249 case lang_fill_statement_enum
:
3250 fill
= s
->fill_statement
.fill
;
3252 case lang_assignment_statement_enum
:
3254 exp_fold_tree (s
->assignment_statement
.exp
,
3255 output_section_statement
,
3256 lang_final_phase_enum
,
3262 case lang_padding_statement_enum
:
3263 dot
+= s
->padding_statement
.size
/ opb
;
3266 case lang_group_statement_enum
:
3267 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3268 output_section_statement
,
3276 case lang_address_statement_enum
:
3284 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3285 operator .startof. (section_name), it produces an undefined symbol
3286 .startof.section_name. Similarly, when it sees
3287 .sizeof. (section_name), it produces an undefined symbol
3288 .sizeof.section_name. For all the output sections, we look for
3289 such symbols, and set them to the correct value. */
3296 if (link_info
.relocateable
)
3299 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3301 const char *secname
;
3303 struct bfd_link_hash_entry
*h
;
3305 secname
= bfd_get_section_name (output_bfd
, s
);
3306 buf
= xmalloc (10 + strlen (secname
));
3308 sprintf (buf
, ".startof.%s", secname
);
3309 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3310 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3312 h
->type
= bfd_link_hash_defined
;
3313 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3314 h
->u
.def
.section
= bfd_abs_section_ptr
;
3317 sprintf (buf
, ".sizeof.%s", secname
);
3318 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3319 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3321 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3322 ldfile_output_machine
);
3323 h
->type
= bfd_link_hash_defined
;
3324 if (s
->_cooked_size
!= 0)
3325 h
->u
.def
.value
= s
->_cooked_size
/ opb
;
3327 h
->u
.def
.value
= s
->_raw_size
/ opb
;
3328 h
->u
.def
.section
= bfd_abs_section_ptr
;
3338 struct bfd_link_hash_entry
*h
;
3341 if (link_info
.relocateable
|| link_info
.shared
)
3346 if (entry_symbol
== (char *) NULL
)
3348 /* No entry has been specified. Look for start, but don't warn
3349 if we don't find it. */
3350 entry_symbol
= "start";
3354 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3355 if (h
!= (struct bfd_link_hash_entry
*) NULL
3356 && (h
->type
== bfd_link_hash_defined
3357 || h
->type
== bfd_link_hash_defweak
)
3358 && h
->u
.def
.section
->output_section
!= NULL
)
3362 val
= (h
->u
.def
.value
3363 + bfd_get_section_vma (output_bfd
,
3364 h
->u
.def
.section
->output_section
)
3365 + h
->u
.def
.section
->output_offset
);
3366 if (! bfd_set_start_address (output_bfd
, val
))
3367 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3374 /* We couldn't find the entry symbol. Try parsing it as a
3376 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3379 if (! bfd_set_start_address (output_bfd
, val
))
3380 einfo (_("%P%F: can't set start address\n"));
3386 /* Can't find the entry symbol, and it's not a number. Use
3387 the first address in the text section. */
3388 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3389 if (ts
!= (asection
*) NULL
)
3392 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3393 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3394 if (! bfd_set_start_address (output_bfd
,
3395 bfd_get_section_vma (output_bfd
,
3397 einfo (_("%P%F: can't set start address\n"));
3402 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3409 /* This is a small function used when we want to ignore errors from
3413 #ifdef ANSI_PROTOTYPES
3414 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3416 ignore_bfd_errors (s
)
3417 const char *s ATTRIBUTE_UNUSED
;
3420 /* Don't do anything. */
3423 /* Check that the architecture of all the input files is compatible
3424 with the output file. Also call the backend to let it do any
3425 other checking that is needed. */
3430 lang_statement_union_type
*file
;
3432 CONST bfd_arch_info_type
*compatible
;
3434 for (file
= file_chain
.head
;
3435 file
!= (lang_statement_union_type
*) NULL
;
3436 file
= file
->input_statement
.next
)
3438 input_bfd
= file
->input_statement
.the_bfd
;
3439 compatible
= bfd_arch_get_compatible (input_bfd
,
3441 if (compatible
== NULL
)
3443 if (command_line
.warn_mismatch
)
3444 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3445 bfd_printable_name (input_bfd
), input_bfd
,
3446 bfd_printable_name (output_bfd
));
3450 bfd_error_handler_type pfn
= NULL
;
3452 /* If we aren't supposed to warn about mismatched input
3453 files, temporarily set the BFD error handler to a
3454 function which will do nothing. We still want to call
3455 bfd_merge_private_bfd_data, since it may set up
3456 information which is needed in the output file. */
3457 if (! command_line
.warn_mismatch
)
3458 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3459 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3461 if (command_line
.warn_mismatch
)
3462 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3465 if (! command_line
.warn_mismatch
)
3466 bfd_set_error_handler (pfn
);
3471 /* Look through all the global common symbols and attach them to the
3472 correct section. The -sort-common command line switch may be used
3473 to roughly sort the entries by size. */
3478 if (link_info
.relocateable
3479 && ! command_line
.force_common_definition
)
3482 if (! config
.sort_common
)
3483 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3488 for (power
= 4; power
>= 0; power
--)
3489 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3494 /* Place one common symbol in the correct section. */
3497 lang_one_common (h
, info
)
3498 struct bfd_link_hash_entry
*h
;
3501 unsigned int power_of_two
;
3504 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3505 ldfile_output_machine
);
3507 if (h
->type
!= bfd_link_hash_common
)
3511 power_of_two
= h
->u
.c
.p
->alignment_power
;
3513 if (config
.sort_common
3514 && power_of_two
< (unsigned int) *(int *) info
)
3517 section
= h
->u
.c
.p
->section
;
3519 /* Increase the size of the section. */
3520 section
->_cooked_size
= ALIGN_N ((section
->_cooked_size
+ opb
- 1) / opb
,
3521 (bfd_size_type
) (1 << power_of_two
)) * opb
;
3523 /* Adjust the alignment if necessary. */
3524 if (power_of_two
> section
->alignment_power
)
3525 section
->alignment_power
= power_of_two
;
3527 /* Change the symbol from common to defined. */
3528 h
->type
= bfd_link_hash_defined
;
3529 h
->u
.def
.section
= section
;
3530 h
->u
.def
.value
= section
->_cooked_size
;
3532 /* Increase the size of the section. */
3533 section
->_cooked_size
+= size
;
3535 /* Make sure the section is allocated in memory, and make sure that
3536 it is no longer a common section. */
3537 section
->flags
|= SEC_ALLOC
;
3538 section
->flags
&= ~ SEC_IS_COMMON
;
3540 if (config
.map_file
!= NULL
)
3542 static boolean header_printed
;
3547 if (! header_printed
)
3549 minfo (_("\nAllocating common symbols\n"));
3550 minfo (_("Common symbol size file\n\n"));
3551 header_printed
= true;
3554 name
= demangle (h
->root
.string
);
3556 len
= strlen (name
);
3571 if (size
<= 0xffffffff)
3572 sprintf (buf
, "%lx", (unsigned long) size
);
3574 sprintf_vma (buf
, size
);
3584 minfo ("%B\n", section
->owner
);
3591 run through the input files and ensure that every input
3592 section has somewhere to go. If one is found without
3593 a destination then create an input request and place it
3594 into the statement tree.
3598 lang_place_orphans ()
3600 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3604 for (s
= file
->the_bfd
->sections
;
3605 s
!= (asection
*) NULL
;
3608 if (s
->output_section
== (asection
*) NULL
)
3610 /* This section of the file is not attatched, root
3611 around for a sensible place for it to go */
3613 if (file
->just_syms_flag
)
3615 /* We are only retrieving symbol values from this
3616 file. We want the symbols to act as though the
3617 values in the file are absolute. */
3618 s
->output_section
= bfd_abs_section_ptr
;
3619 s
->output_offset
= s
->vma
;
3621 else if (strcmp (s
->name
, "COMMON") == 0)
3623 /* This is a lonely common section which must have
3624 come from an archive. We attach to the section
3625 with the wildcard. */
3626 if (! link_info
.relocateable
3627 || command_line
.force_common_definition
)
3629 if (default_common_section
== NULL
)
3632 /* This message happens when using the
3633 svr3.ifile linker script, so I have
3635 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3637 default_common_section
=
3638 lang_output_section_statement_lookup (".bss");
3641 wild_doit (&default_common_section
->children
, s
,
3642 default_common_section
, file
);
3645 else if (ldemul_place_orphan (file
, s
))
3649 lang_output_section_statement_type
*os
=
3650 lang_output_section_statement_lookup (s
->name
);
3652 wild_doit (&os
->children
, s
, os
, file
);
3661 lang_set_flags (ptr
, flags
, invert
)
3662 lang_memory_region_type
*ptr
;
3666 flagword
*ptr_flags
;
3668 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3674 *ptr_flags
|= SEC_ALLOC
;
3678 *ptr_flags
|= SEC_READONLY
;
3682 *ptr_flags
|= SEC_DATA
;
3686 *ptr_flags
|= SEC_CODE
;
3691 *ptr_flags
|= SEC_LOAD
;
3695 einfo (_("%P%F: invalid syntax in flags\n"));
3702 /* Call a function on each input file. This function will be called
3703 on an archive, but not on the elements. */
3706 lang_for_each_input_file (func
)
3707 void (*func
) PARAMS ((lang_input_statement_type
*));
3709 lang_input_statement_type
*f
;
3711 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3713 f
= (lang_input_statement_type
*) f
->next_real_file
)
3717 /* Call a function on each file. The function will be called on all
3718 the elements of an archive which are included in the link, but will
3719 not be called on the archive file itself. */
3722 lang_for_each_file (func
)
3723 void (*func
) PARAMS ((lang_input_statement_type
*));
3725 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3736 lang_for_each_input_section (func
)
3737 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
3739 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3743 for (s
= f
->the_bfd
->sections
;
3744 s
!= (asection
*) NULL
;
3747 func (f
->the_bfd
, s
);
3755 ldlang_add_file (entry
)
3756 lang_input_statement_type
* entry
;
3760 lang_statement_append (&file_chain
,
3761 (lang_statement_union_type
*) entry
,
3764 /* The BFD linker needs to have a list of all input BFDs involved in
3766 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3767 ASSERT (entry
->the_bfd
!= output_bfd
);
3768 for (pp
= &link_info
.input_bfds
;
3769 *pp
!= (bfd
*) NULL
;
3770 pp
= &(*pp
)->link_next
)
3772 *pp
= entry
->the_bfd
;
3773 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3774 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3776 /* Look through the sections and check for any which should not be
3777 included in the link. We need to do this now, so that we can
3778 notice when the backend linker tries to report multiple
3779 definition errors for symbols which are in sections we aren't
3780 going to link. FIXME: It might be better to entirely ignore
3781 symbols which are defined in sections which are going to be
3782 discarded. This would require modifying the backend linker for
3783 each backend which might set the SEC_LINK_ONCE flag. If we do
3784 this, we should probably handle SEC_EXCLUDE in the same way. */
3786 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3790 lang_add_output (name
, from_script
)
3794 /* Make -o on command line override OUTPUT in script. */
3795 if (had_output_filename
== false || !from_script
)
3797 output_filename
= name
;
3798 had_output_filename
= true;
3803 static lang_output_section_statement_type
*current_section
;
3815 for (l
= 0; l
< 32; l
++)
3817 if (i
>= (unsigned int) x
)
3825 lang_output_section_statement_type
*
3826 lang_enter_output_section_statement (output_section_statement_name
,
3827 address_exp
, sectype
, block_value
,
3828 align
, subalign
, ebase
)
3829 const char *output_section_statement_name
;
3830 etree_type
* address_exp
;
3831 enum section_type sectype
;
3832 bfd_vma block_value
;
3834 etree_type
*subalign
;
3837 lang_output_section_statement_type
*os
;
3841 lang_output_section_statement_lookup (output_section_statement_name
);
3845 /* Add this statement to tree */
3846 /* add_statement(lang_output_section_statement_enum,
3847 output_section_statement);*/
3848 /* Make next things chain into subchain of this */
3850 if (os
->addr_tree
==
3851 (etree_type
*) NULL
)
3856 os
->sectype
= sectype
;
3857 if (sectype
!= noload_section
)
3858 os
->flags
= SEC_NO_FLAGS
;
3860 os
->flags
= SEC_NEVER_LOAD
;
3861 os
->block_value
= block_value
? block_value
: 1;
3862 stat_ptr
= &os
->children
;
3864 os
->subsection_alignment
= topower(
3865 exp_get_value_int(subalign
, -1,
3866 "subsection alignment",
3868 os
->section_alignment
= topower(
3869 exp_get_value_int(align
, -1,
3870 "section alignment", 0));
3872 os
->load_base
= ebase
;
3880 lang_output_statement_type
*new =
3881 new_stat (lang_output_statement
, stat_ptr
);
3883 new->name
= output_filename
;
3886 /* Reset the current counters in the regions */
3888 reset_memory_regions ()
3890 lang_memory_region_type
*p
= lang_memory_region_list
;
3892 for (p
= lang_memory_region_list
;
3893 p
!= (lang_memory_region_type
*) NULL
;
3896 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3897 p
->current
= p
->origin
;
3901 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3902 as needed. SECTION may be NULL, in which case it is a wild card. */
3905 gc_section_callback (ptr
, section
, file
, data
)
3906 lang_wild_statement_type
*ptr
;
3908 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
3909 void *data ATTRIBUTE_UNUSED
;
3911 /* If the wild pattern was marked KEEP, the member sections
3912 should be as well. */
3913 if (ptr
->keep_sections
)
3914 section
->flags
|= SEC_KEEP
;
3917 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3918 may be NULL, indicating that it is a wildcard. */
3921 lang_gc_wild (s
, section
, file
)
3922 lang_wild_statement_type
*s
;
3923 const char *section
;
3926 walk_wild (s
, section
, file
, gc_section_callback
, NULL
);
3929 /* Iterate over sections marking them against GC. */
3932 lang_gc_sections_1 (s
)
3933 lang_statement_union_type
* s
;
3935 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3937 switch (s
->header
.type
)
3939 case lang_wild_statement_enum
:
3940 lang_gc_wild (&s
->wild_statement
,
3941 s
->wild_statement
.section_name
,
3942 s
->wild_statement
.filename
);
3944 case lang_constructors_statement_enum
:
3945 lang_gc_sections_1 (constructor_list
.head
);
3947 case lang_output_section_statement_enum
:
3948 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
3950 case lang_group_statement_enum
:
3951 lang_gc_sections_1 (s
->group_statement
.children
.head
);
3962 struct bfd_link_hash_entry
*h
;
3963 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
3965 /* Keep all sections so marked in the link script. */
3967 lang_gc_sections_1 (statement_list
.head
);
3969 /* Keep all sections containing symbols undefined on the command-line.
3970 Handle the entry symbol at the same time. */
3972 if (entry_symbol
!= NULL
)
3974 fake_list_start
.next
= ldlang_undef_chain_list_head
;
3975 fake_list_start
.name
= (char *) entry_symbol
;
3976 ulist
= &fake_list_start
;
3979 ulist
= ldlang_undef_chain_list_head
;
3981 for (; ulist
; ulist
= ulist
->next
)
3983 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
3984 false, false, false);
3986 if (h
!= (struct bfd_link_hash_entry
*) NULL
3987 && (h
->type
== bfd_link_hash_defined
3988 || h
->type
== bfd_link_hash_defweak
)
3989 && ! bfd_is_abs_section (h
->u
.def
.section
))
3991 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3995 bfd_gc_sections (output_bfd
, &link_info
);
4001 lang_reasonable_defaults ();
4002 current_target
= default_target
;
4004 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
4006 ldemul_create_output_section_statements ();
4008 /* Add to the hash table all undefineds on the command line */
4009 lang_place_undefineds ();
4011 already_linked_table_init ();
4013 /* Create a bfd for each input file */
4014 current_target
= default_target
;
4015 open_input_bfds (statement_list
.head
, false);
4017 ldemul_after_open ();
4019 already_linked_table_free ();
4021 /* Make sure that we're not mixing architectures. We call this
4022 after all the input files have been opened, but before we do any
4023 other processing, so that any operations merge_private_bfd_data
4024 does on the output file will be known during the rest of the
4028 /* Handle .exports instead of a version script if we're told to do so. */
4029 if (command_line
.version_exports_section
)
4030 lang_do_version_exports_section ();
4032 /* Build all sets based on the information gathered from the input
4034 ldctor_build_sets ();
4036 /* Remove unreferenced sections if asked to. */
4037 if (command_line
.gc_sections
)
4038 lang_gc_sections ();
4040 /* Size up the common data */
4043 /* Run through the contours of the script and attach input sections
4044 to the correct output sections
4046 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
4047 (lang_output_section_statement_type
*) NULL
);
4050 /* Find any sections not attached explicitly and handle them */
4051 lang_place_orphans ();
4053 ldemul_before_allocation ();
4055 /* We must record the program headers before we try to fix the
4056 section positions, since they will affect SIZEOF_HEADERS. */
4057 lang_record_phdrs ();
4059 /* Now run around and relax if we can */
4060 if (command_line
.relax
)
4062 /* First time round is a trial run to get the 'worst case'
4063 addresses of the objects if there was no relaxing. */
4064 lang_size_sections (statement_list
.head
,
4066 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4068 /* Keep relaxing until bfd_relax_section gives up. */
4071 reset_memory_regions ();
4073 relax_again
= false;
4075 /* Note: pe-dll.c does something like this also. If you find
4076 you need to change this code, you probably need to change
4077 pe-dll.c also. DJ */
4079 /* Do all the assignments with our current guesses as to
4081 lang_do_assignments (statement_list
.head
,
4083 (fill_type
) 0, (bfd_vma
) 0);
4085 /* Perform another relax pass - this time we know where the
4086 globals are, so can make better guess. */
4087 lang_size_sections (statement_list
.head
,
4089 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
4091 while (relax_again
);
4095 /* Size up the sections. */
4096 lang_size_sections (statement_list
.head
,
4098 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4101 /* See if anything special should be done now we know how big
4103 ldemul_after_allocation ();
4105 /* Fix any .startof. or .sizeof. symbols. */
4106 lang_set_startof ();
4108 /* Do all the assignments, now that we know the final restingplaces
4109 of all the symbols */
4111 lang_do_assignments (statement_list
.head
,
4113 (fill_type
) 0, (bfd_vma
) 0);
4115 /* Make sure that the section addresses make sense. */
4116 if (! link_info
.relocateable
4117 && command_line
.check_section_addresses
)
4118 lang_check_section_addresses ();
4126 /* EXPORTED TO YACC */
4129 lang_add_wild (section_name
, sections_sorted
, filename
, filenames_sorted
,
4130 keep_sections
, exclude_filename_list
)
4131 const char *const section_name
;
4132 boolean sections_sorted
;
4133 const char *const filename
;
4134 boolean filenames_sorted
;
4135 boolean keep_sections
;
4136 struct name_list
*exclude_filename_list
;
4138 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
4141 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
4143 placed_commons
= true;
4145 if (filename
!= NULL
&& ! wildcardp (filename
))
4147 lang_has_input_file
= true;
4149 new->section_name
= section_name
;
4150 new->sections_sorted
= sections_sorted
;
4151 new->filename
= filename
;
4152 new->filenames_sorted
= filenames_sorted
;
4153 new->keep_sections
= keep_sections
;
4154 new->exclude_filename_list
= exclude_filename_list
;
4155 lang_list_init (&new->children
);
4159 lang_section_start (name
, address
)
4161 etree_type
* address
;
4163 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
4165 ad
->section_name
= name
;
4166 ad
->address
= address
;
4169 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4170 because of a -e argument on the command line, or zero if this is
4171 called by ENTRY in a linker script. Command line arguments take
4175 lang_add_entry (name
, cmdline
)
4179 if (entry_symbol
== NULL
4181 || ! entry_from_cmdline
)
4183 entry_symbol
= name
;
4184 entry_from_cmdline
= cmdline
;
4189 lang_add_target (name
)
4192 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4208 map_option_f
= true;
4219 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4226 lang_add_data (type
, exp
)
4228 union etree_union
*exp
;
4231 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4239 /* Create a new reloc statement. RELOC is the BFD relocation type to
4240 generate. HOWTO is the corresponding howto structure (we could
4241 look this up, but the caller has already done so). SECTION is the
4242 section to generate a reloc against, or NAME is the name of the
4243 symbol to generate a reloc against. Exactly one of SECTION and
4244 NAME must be NULL. ADDEND is an expression for the addend. */
4247 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4248 bfd_reloc_code_real_type reloc
;
4249 reloc_howto_type
*howto
;
4252 union etree_union
*addend
;
4254 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4258 p
->section
= section
;
4260 p
->addend_exp
= addend
;
4262 p
->addend_value
= 0;
4263 p
->output_section
= NULL
;
4267 lang_assignment_statement_type
*
4268 lang_add_assignment (exp
)
4271 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4279 lang_add_attribute (attribute
)
4280 enum statement_enum attribute
;
4282 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4289 if (startup_file
!= (char *) NULL
)
4291 einfo (_("%P%Fmultiple STARTUP files\n"));
4293 first_file
->filename
= name
;
4294 first_file
->local_sym_name
= name
;
4295 first_file
->real
= true;
4297 startup_file
= name
;
4304 lang_float_flag
= maybe
;
4308 lang_leave_output_section_statement (fill
, memspec
, phdrs
, lma_memspec
)
4310 const char *memspec
;
4311 struct lang_output_section_phdr_list
*phdrs
;
4312 const char *lma_memspec
;
4314 current_section
->fill
= fill
;
4315 current_section
->region
= lang_memory_region_lookup (memspec
);
4316 if (strcmp (lma_memspec
, "*default*") != 0)
4318 current_section
->lma_region
= lang_memory_region_lookup (lma_memspec
);
4319 /* if no runtime region has been given, but the load region has been,
4320 use the load region */
4321 if (strcmp (memspec
, "*default*") == 0)
4322 current_section
->region
= lang_memory_region_lookup (lma_memspec
);
4324 current_section
->phdrs
= phdrs
;
4325 stat_ptr
= &statement_list
;
4329 Create an absolute symbol with the given name with the value of the
4330 address of first byte of the section named.
4332 If the symbol already exists, then do nothing.
4335 lang_abs_symbol_at_beginning_of (secname
, name
)
4336 const char *secname
;
4339 struct bfd_link_hash_entry
*h
;
4341 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4342 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4343 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4345 if (h
->type
== bfd_link_hash_new
4346 || h
->type
== bfd_link_hash_undefined
)
4350 h
->type
= bfd_link_hash_defined
;
4352 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4353 if (sec
== (asection
*) NULL
)
4356 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4358 h
->u
.def
.section
= bfd_abs_section_ptr
;
4363 Create an absolute symbol with the given name with the value of the
4364 address of the first byte after the end of the section named.
4366 If the symbol already exists, then do nothing.
4369 lang_abs_symbol_at_end_of (secname
, name
)
4370 const char *secname
;
4373 struct bfd_link_hash_entry
*h
;
4375 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4376 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4377 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4379 if (h
->type
== bfd_link_hash_new
4380 || h
->type
== bfd_link_hash_undefined
)
4384 h
->type
= bfd_link_hash_defined
;
4386 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4387 if (sec
== (asection
*) NULL
)
4390 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4391 + bfd_section_size (output_bfd
, sec
) /
4392 bfd_octets_per_byte (output_bfd
));
4394 h
->u
.def
.section
= bfd_abs_section_ptr
;
4399 lang_statement_append (list
, element
, field
)
4400 lang_statement_list_type
* list
;
4401 lang_statement_union_type
* element
;
4402 lang_statement_union_type
** field
;
4404 *(list
->tail
) = element
;
4408 /* Set the output format type. -oformat overrides scripts. */
4411 lang_add_output_format (format
, big
, little
, from_script
)
4417 if (output_target
== NULL
|| !from_script
)
4419 if (command_line
.endian
== ENDIAN_BIG
4422 else if (command_line
.endian
== ENDIAN_LITTLE
4426 output_target
= format
;
4430 /* Enter a group. This creates a new lang_group_statement, and sets
4431 stat_ptr to build new statements within the group. */
4436 lang_group_statement_type
*g
;
4438 g
= new_stat (lang_group_statement
, stat_ptr
);
4439 lang_list_init (&g
->children
);
4440 stat_ptr
= &g
->children
;
4443 /* Leave a group. This just resets stat_ptr to start writing to the
4444 regular list of statements again. Note that this will not work if
4445 groups can occur inside anything else which can adjust stat_ptr,
4446 but currently they can't. */
4451 stat_ptr
= &statement_list
;
4454 /* Add a new program header. This is called for each entry in a PHDRS
4455 command in a linker script. */
4458 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4466 struct lang_phdr
*n
, **pp
;
4468 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4471 n
->type
= exp_get_value_int (type
, 0, "program header type",
4472 lang_final_phase_enum
);
4473 n
->filehdr
= filehdr
;
4478 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4483 /* Record the program header information in the output BFD. FIXME: We
4484 should not be calling an ELF specific function here. */
4487 lang_record_phdrs ()
4491 struct lang_output_section_phdr_list
*last
;
4492 struct lang_phdr
*l
;
4493 lang_statement_union_type
*u
;
4496 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4498 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4505 for (u
= lang_output_section_statement
.head
;
4507 u
= u
->output_section_statement
.next
)
4509 lang_output_section_statement_type
*os
;
4510 struct lang_output_section_phdr_list
*pl
;
4512 os
= &u
->output_section_statement
;
4519 if (os
->sectype
== noload_section
4520 || os
->bfd_section
== NULL
4521 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4526 if (os
->bfd_section
== NULL
)
4529 for (; pl
!= NULL
; pl
= pl
->next
)
4531 if (strcmp (pl
->name
, l
->name
) == 0)
4536 secs
= ((asection
**)
4537 xrealloc (secs
, alc
* sizeof (asection
*)));
4539 secs
[c
] = os
->bfd_section
;
4546 if (l
->flags
== NULL
)
4549 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4550 lang_final_phase_enum
);
4555 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4556 lang_final_phase_enum
);
4558 if (! bfd_record_phdr (output_bfd
, l
->type
,
4559 l
->flags
== NULL
? false : true,
4561 l
->at
== NULL
? false : true,
4562 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4563 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4568 /* Make sure all the phdr assignments succeeded. */
4569 for (u
= lang_output_section_statement
.head
;
4571 u
= u
->output_section_statement
.next
)
4573 struct lang_output_section_phdr_list
*pl
;
4575 if (u
->output_section_statement
.bfd_section
== NULL
)
4578 for (pl
= u
->output_section_statement
.phdrs
;
4581 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4582 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4583 u
->output_section_statement
.name
, pl
->name
);
4587 /* Record a list of sections which may not be cross referenced. */
4590 lang_add_nocrossref (l
)
4591 struct lang_nocrossref
*l
;
4593 struct lang_nocrossrefs
*n
;
4595 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4596 n
->next
= nocrossref_list
;
4598 nocrossref_list
= n
;
4600 /* Set notice_all so that we get informed about all symbols. */
4601 link_info
.notice_all
= true;
4604 /* Overlay handling. We handle overlays with some static variables. */
4606 /* The overlay virtual address. */
4607 static etree_type
*overlay_vma
;
4609 /* The overlay load address. */
4610 static etree_type
*overlay_lma
;
4612 /* Whether nocrossrefs is set for this overlay. */
4613 static int overlay_nocrossrefs
;
4615 /* An expression for the maximum section size seen so far. */
4616 static etree_type
*overlay_max
;
4618 /* A list of all the sections in this overlay. */
4622 struct overlay_list
*next
;
4623 lang_output_section_statement_type
*os
;
4626 static struct overlay_list
*overlay_list
;
4628 /* Start handling an overlay. */
4631 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4632 etree_type
*vma_expr
;
4633 etree_type
*lma_expr
;
4636 /* The grammar should prevent nested overlays from occurring. */
4637 ASSERT (overlay_vma
== NULL
4638 && overlay_lma
== NULL
4639 && overlay_list
== NULL
4640 && overlay_max
== NULL
);
4642 overlay_vma
= vma_expr
;
4643 overlay_lma
= lma_expr
;
4644 overlay_nocrossrefs
= nocrossrefs
;
4647 /* Start a section in an overlay. We handle this by calling
4648 lang_enter_output_section_statement with the correct VMA and LMA. */
4651 lang_enter_overlay_section (name
)
4654 struct overlay_list
*n
;
4657 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4658 0, 0, 0, overlay_lma
);
4660 /* If this is the first section, then base the VMA and LMA of future
4661 sections on this one. This will work correctly even if `.' is
4662 used in the addresses. */
4663 if (overlay_list
== NULL
)
4665 overlay_vma
= exp_nameop (ADDR
, name
);
4666 overlay_lma
= exp_nameop (LOADADDR
, name
);
4669 /* Remember the section. */
4670 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4671 n
->os
= current_section
;
4672 n
->next
= overlay_list
;
4675 size
= exp_nameop (SIZEOF
, name
);
4677 /* Adjust the LMA for the next section. */
4678 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4680 /* Arrange to work out the maximum section end address. */
4681 if (overlay_max
== NULL
)
4684 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4687 /* Finish a section in an overlay. There isn't any special to do
4691 lang_leave_overlay_section (fill
, phdrs
)
4693 struct lang_output_section_phdr_list
*phdrs
;
4700 name
= current_section
->name
;
4702 lang_leave_output_section_statement (fill
, "*default*",
4703 phdrs
, "*default*");
4705 /* Define the magic symbols. */
4707 clean
= xmalloc (strlen (name
) + 1);
4709 for (s1
= name
; *s1
!= '\0'; s1
++)
4710 if (isalnum ((unsigned char) *s1
) || *s1
== '_')
4714 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4715 sprintf (buf
, "__load_start_%s", clean
);
4716 lang_add_assignment (exp_assop ('=', buf
,
4717 exp_nameop (LOADADDR
, name
)));
4719 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4720 sprintf (buf
, "__load_stop_%s", clean
);
4721 lang_add_assignment (exp_assop ('=', buf
,
4723 exp_nameop (LOADADDR
, name
),
4724 exp_nameop (SIZEOF
, name
))));
4729 /* Finish an overlay. If there are any overlay wide settings, this
4730 looks through all the sections in the overlay and sets them. */
4733 lang_leave_overlay (fill
, memspec
, phdrs
, lma_memspec
)
4735 const char *memspec
;
4736 struct lang_output_section_phdr_list
*phdrs
;
4737 const char *lma_memspec
;
4739 lang_memory_region_type
*region
;
4740 lang_memory_region_type
*lma_region
;
4741 struct overlay_list
*l
;
4742 struct lang_nocrossref
*nocrossref
;
4744 if (memspec
== NULL
)
4747 region
= lang_memory_region_lookup (memspec
);
4749 if (lma_memspec
== NULL
)
4752 lma_region
= lang_memory_region_lookup (lma_memspec
);
4759 struct overlay_list
*next
;
4761 if (fill
!= 0 && l
->os
->fill
== 0)
4763 if (region
!= NULL
&& l
->os
->region
== NULL
)
4764 l
->os
->region
= region
;
4765 if (lma_region
!= NULL
&& l
->os
->lma_region
== NULL
)
4766 l
->os
->lma_region
= lma_region
;
4767 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4768 l
->os
->phdrs
= phdrs
;
4770 if (overlay_nocrossrefs
)
4772 struct lang_nocrossref
*nc
;
4774 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4775 nc
->name
= l
->os
->name
;
4776 nc
->next
= nocrossref
;
4785 if (nocrossref
!= NULL
)
4786 lang_add_nocrossref (nocrossref
);
4788 /* Update . for the end of the overlay. */
4789 lang_add_assignment (exp_assop ('=', ".",
4790 exp_binop ('+', overlay_vma
, overlay_max
)));
4794 overlay_nocrossrefs
= 0;
4795 overlay_list
= NULL
;
4799 /* Version handling. This is only useful for ELF. */
4801 /* This global variable holds the version tree that we build. */
4803 struct bfd_elf_version_tree
*lang_elf_version_info
;
4806 lang_vers_match_lang_c (expr
, sym
)
4807 struct bfd_elf_version_expr
*expr
;
4810 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4812 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4816 lang_vers_match_lang_cplusplus (expr
, sym
)
4817 struct bfd_elf_version_expr
*expr
;
4823 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4826 alt_sym
= cplus_demangle(sym
, /* DMGL_NO_TPARAMS */ 0);
4829 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4830 Should we early out false in this case? */
4831 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4835 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4843 lang_vers_match_lang_java (expr
, sym
)
4844 struct bfd_elf_version_expr
*expr
;
4850 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4853 alt_sym
= cplus_demangle(sym
, DMGL_JAVA
);
4856 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4857 Should we early out false in this case? */
4858 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4862 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4869 /* This is called for each variable name or match expression. */
4871 struct bfd_elf_version_expr
*
4872 lang_new_vers_regex (orig
, new, lang
)
4873 struct bfd_elf_version_expr
*orig
;
4877 struct bfd_elf_version_expr
*ret
;
4879 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4883 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4884 ret
->match
= lang_vers_match_lang_c
;
4885 else if (strcasecmp (lang
, "C++") == 0)
4886 ret
->match
= lang_vers_match_lang_cplusplus
;
4887 else if (strcasecmp (lang
, "Java") == 0)
4888 ret
->match
= lang_vers_match_lang_java
;
4891 einfo (_("%X%P: unknown language `%s' in version information\n"),
4893 ret
->match
= lang_vers_match_lang_c
;
4899 /* This is called for each set of variable names and match
4902 struct bfd_elf_version_tree
*
4903 lang_new_vers_node (globals
, locals
)
4904 struct bfd_elf_version_expr
*globals
;
4905 struct bfd_elf_version_expr
*locals
;
4907 struct bfd_elf_version_tree
*ret
;
4909 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
4913 ret
->globals
= globals
;
4914 ret
->locals
= locals
;
4916 ret
->name_indx
= (unsigned int) -1;
4921 /* This static variable keeps track of version indices. */
4923 static int version_index
;
4925 /* This is called when we know the name and dependencies of the
4929 lang_register_vers_node (name
, version
, deps
)
4931 struct bfd_elf_version_tree
*version
;
4932 struct bfd_elf_version_deps
*deps
;
4934 struct bfd_elf_version_tree
*t
, **pp
;
4935 struct bfd_elf_version_expr
*e1
;
4937 /* Make sure this node has a unique name. */
4938 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4939 if (strcmp (t
->name
, name
) == 0)
4940 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
4942 /* Check the global and local match names, and make sure there
4943 aren't any duplicates. */
4945 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
4947 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4949 struct bfd_elf_version_expr
*e2
;
4951 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
4952 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4953 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4958 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
4960 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4962 struct bfd_elf_version_expr
*e2
;
4964 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
4965 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4966 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4971 version
->deps
= deps
;
4972 version
->name
= name
;
4974 version
->vernum
= version_index
;
4976 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4981 /* This is called when we see a version dependency. */
4983 struct bfd_elf_version_deps
*
4984 lang_add_vers_depend (list
, name
)
4985 struct bfd_elf_version_deps
*list
;
4988 struct bfd_elf_version_deps
*ret
;
4989 struct bfd_elf_version_tree
*t
;
4991 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
4994 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4996 if (strcmp (t
->name
, name
) == 0)
4998 ret
->version_needed
= t
;
5003 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5009 lang_do_version_exports_section ()
5011 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5013 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5015 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5022 len
= bfd_section_size (is
->the_bfd
, sec
);
5023 contents
= xmalloc (len
);
5024 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5025 einfo (_("%X%P: unable to read .exports section contents"), sec
);
5028 while (p
< contents
+len
)
5030 greg
= lang_new_vers_regex (greg
, p
, NULL
);
5031 p
= strchr (p
, '\0') + 1;
5034 /* Do not free the contents, as we used them creating the regex. */
5036 /* Do not include this section in the link. */
5037 bfd_set_section_flags (is
->the_bfd
, sec
,
5038 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5041 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
5042 lang_register_vers_node (command_line
.version_exports_section
,
5043 lang_new_vers_node (greg
, lreg
), NULL
);