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 lang_statement_list_type lang_output_section_statement
;
62 static CONST
char *current_target
;
63 static CONST
char *output_target
;
64 static lang_statement_list_type statement_list
;
65 static struct lang_phdr
*lang_phdr_list
;
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*func
) (lang_statement_union_type
*),
69 lang_statement_union_type
*s
));
70 static lang_input_statement_type
*new_afile
71 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
72 const char *target
, boolean add_to_list
));
73 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
74 static void exp_init_os
PARAMS ((etree_type
*));
75 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
76 static struct bfd_hash_entry
*already_linked_newfunc
77 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
79 static void already_linked_table_init
PARAMS ((void));
80 static void already_linked_table_free
PARAMS ((void));
81 static boolean wildcardp
PARAMS ((const char *));
82 static lang_statement_union_type
*wild_sort
83 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
85 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
86 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
87 lang_statement_list_type
*));
88 static void wild
PARAMS ((lang_wild_statement_type
*s
,
89 const char *section
, const char *file
,
91 lang_output_section_statement_type
*output
));
92 static bfd
*open_output
PARAMS ((const char *name
));
93 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
94 static void open_input_bfds
95 PARAMS ((lang_statement_union_type
*statement
, boolean
));
96 static void lang_reasonable_defaults
PARAMS ((void));
97 static void lang_place_undefineds
PARAMS ((void));
98 static void map_input_to_output_sections
99 PARAMS ((lang_statement_union_type
*s
,
101 lang_output_section_statement_type
*output_section_statement
));
102 static void print_output_section_statement
103 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
104 static void print_assignment
105 PARAMS ((lang_assignment_statement_type
*assignment
,
106 lang_output_section_statement_type
*output_section
));
107 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
108 static boolean print_one_symbol
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
109 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
110 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
111 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
112 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
113 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
114 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
115 static void print_wild_statement
116 PARAMS ((lang_wild_statement_type
*w
,
117 lang_output_section_statement_type
*os
));
118 static void print_group
119 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
120 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
121 lang_output_section_statement_type
*os
));
122 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
123 lang_output_section_statement_type
*os
));
124 static void print_statements
PARAMS ((void));
125 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
126 fill_type fill
, unsigned int power
,
127 asection
*output_section_statement
,
129 static bfd_vma size_input_section
130 PARAMS ((lang_statement_union_type
**this_ptr
,
131 lang_output_section_statement_type
*output_section_statement
,
132 fill_type fill
, bfd_vma dot
, boolean relax
));
133 static void lang_finish
PARAMS ((void));
134 static void ignore_bfd_errors
PARAMS ((const char *, ...));
135 static void lang_check
PARAMS ((void));
136 static void lang_common
PARAMS ((void));
137 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
138 static void lang_place_orphans
PARAMS ((void));
139 static int topower
PARAMS ((int));
140 static void lang_set_startof
PARAMS ((void));
141 static void reset_memory_regions
PARAMS ((void));
142 static void lang_record_phdrs
PARAMS ((void));
143 static void lang_gc_wild
144 PARAMS ((lang_wild_statement_type
*, const char *, const char *));
145 static void lang_gc_sections_1
PARAMS ((lang_statement_union_type
*));
146 static void lang_gc_sections
PARAMS ((void));
147 static void lang_do_version_exports_section
PARAMS ((void));
148 static void lang_check_section_addresses
PARAMS ((void));
150 typedef void (*callback_t
) PARAMS ((lang_wild_statement_type
*,
151 asection
*, lang_input_statement_type
*,
153 static void walk_wild_section
154 PARAMS ((lang_wild_statement_type
*, const char *,
155 lang_input_statement_type
*, callback_t
, void *));
156 static void walk_wild_file
157 PARAMS ((lang_wild_statement_type
*, const char *,
158 lang_input_statement_type
*, callback_t
, void *));
160 static int get_target
PARAMS ((const bfd_target
*, void *));
161 static void stricpy
PARAMS ((char *, char *));
162 static void strcut
PARAMS ((char *, char *));
163 static int name_compare
PARAMS ((char *, char *));
164 static int closest_target_match
PARAMS ((const bfd_target
*, void *));
165 static char * get_first_input_target
PARAMS ((void));
168 lang_output_section_statement_type
*abs_output_section
;
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
;
686 lookup
->block_value
= 1;
689 lookup
->next
= (lang_statement_union_type
*) NULL
;
690 lookup
->bfd_section
= (asection
*) NULL
;
691 lookup
->processed
= false;
692 lookup
->sectype
= normal_section
;
693 lookup
->addr_tree
= (etree_type
*) NULL
;
694 lang_list_init (&lookup
->children
);
696 lookup
->memspec
= (CONST
char *) NULL
;
698 lookup
->subsection_alignment
= -1;
699 lookup
->section_alignment
= -1;
700 lookup
->load_base
= (union etree_union
*) NULL
;
701 lookup
->phdrs
= NULL
;
703 lang_statement_append (&lang_output_section_statement
,
704 (lang_statement_union_type
*) lookup
,
711 lang_map_flags (flag
)
714 if (flag
& SEC_ALLOC
)
720 if (flag
& SEC_READONLY
)
733 lang_memory_region_type
*m
;
735 minfo (_("\nMemory Configuration\n\n"));
736 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
737 _("Name"), _("Origin"), _("Length"), _("Attributes"));
739 for (m
= lang_memory_region_list
;
740 m
!= (lang_memory_region_type
*) NULL
;
746 fprintf (config
.map_file
, "%-16s ", m
->name
);
748 sprintf_vma (buf
, m
->origin
);
749 minfo ("0x%s ", buf
);
757 minfo ("0x%V", m
->length
);
758 if (m
->flags
|| m
->not_flags
)
766 lang_map_flags (m
->flags
);
772 lang_map_flags (m
->not_flags
);
779 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
784 /* Initialize an output section. */
788 lang_output_section_statement_type
*s
;
790 section_userdata_type
*new;
792 if (s
->bfd_section
!= NULL
)
795 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
796 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME
);
798 new = ((section_userdata_type
*)
799 stat_alloc (sizeof (section_userdata_type
)));
801 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
802 if (s
->bfd_section
== (asection
*) NULL
)
803 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
804 if (s
->bfd_section
== (asection
*) NULL
)
806 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
807 output_bfd
->xvec
->name
, s
->name
);
809 s
->bfd_section
->output_section
= s
->bfd_section
;
811 /* We initialize an output sections output offset to minus its own */
812 /* vma to allow us to output a section through itself */
813 s
->bfd_section
->output_offset
= 0;
814 get_userdata (s
->bfd_section
) = (PTR
) new;
816 /* If there is a base address, make sure that any sections it might
817 mention are initialized. */
818 if (s
->addr_tree
!= NULL
)
819 exp_init_os (s
->addr_tree
);
822 /* Make sure that all output sections mentioned in an expression are
829 switch (exp
->type
.node_class
)
832 exp_init_os (exp
->assign
.src
);
836 exp_init_os (exp
->binary
.lhs
);
837 exp_init_os (exp
->binary
.rhs
);
841 exp_init_os (exp
->trinary
.cond
);
842 exp_init_os (exp
->trinary
.lhs
);
843 exp_init_os (exp
->trinary
.rhs
);
847 exp_init_os (exp
->unary
.child
);
851 switch (exp
->type
.node_code
)
857 lang_output_section_statement_type
*os
;
859 os
= lang_output_section_find (exp
->name
.name
);
860 if (os
!= NULL
&& os
->bfd_section
== NULL
)
871 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
872 once into the output. This routine checks each section, and
873 arrange to discard it if a section of the same name has already
874 been linked. If the section has COMDAT information, then it uses
875 that to decide whether the section should be included. This code
876 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
877 that is, it does not depend solely upon the section name.
878 section_already_linked is called via bfd_map_over_sections. */
880 /* This is the shape of the elements inside the already_linked hash
881 table. It maps a name onto a list of already_linked elements with
882 the same name. It's possible to get more than one element in a
883 list if the COMDAT sections have different names. */
885 struct already_linked_hash_entry
887 struct bfd_hash_entry root
;
888 struct already_linked
*entry
;
891 struct already_linked
893 struct already_linked
*next
;
897 /* The hash table. */
899 static struct bfd_hash_table already_linked_table
;
903 section_already_linked (abfd
, sec
, data
)
908 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
911 struct already_linked
*l
;
912 struct already_linked_hash_entry
*already_linked_list
;
914 /* If we are only reading symbols from this object, then we want to
915 discard all sections. */
916 if (entry
->just_syms_flag
)
918 sec
->output_section
= bfd_abs_section_ptr
;
919 sec
->output_offset
= sec
->vma
;
923 flags
= bfd_get_section_flags (abfd
, sec
);
925 if ((flags
& SEC_LINK_ONCE
) == 0)
928 /* FIXME: When doing a relocateable link, we may have trouble
929 copying relocations in other sections that refer to local symbols
930 in the section being discarded. Those relocations will have to
931 be converted somehow; as of this writing I'm not sure that any of
932 the backends handle that correctly.
934 It is tempting to instead not discard link once sections when
935 doing a relocateable link (technically, they should be discarded
936 whenever we are building constructors). However, that fails,
937 because the linker winds up combining all the link once sections
938 into a single large link once section, which defeats the purpose
939 of having link once sections in the first place.
941 Also, not merging link once sections in a relocateable link
942 causes trouble for MIPS ELF, which relies in link once semantics
943 to handle the .reginfo section correctly. */
945 name
= bfd_get_section_name (abfd
, sec
);
947 already_linked_list
=
948 ((struct already_linked_hash_entry
*)
949 bfd_hash_lookup (&already_linked_table
, name
, true, false));
951 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
953 if (sec
->comdat
== NULL
954 || l
->sec
->comdat
== NULL
955 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
957 /* The section has already been linked. See if we should
959 switch (flags
& SEC_LINK_DUPLICATES
)
964 case SEC_LINK_DUPLICATES_DISCARD
:
967 case SEC_LINK_DUPLICATES_ONE_ONLY
:
968 if (sec
->comdat
== NULL
)
969 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
972 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
973 abfd
, name
, sec
->comdat
->name
);
976 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
977 /* FIXME: We should really dig out the contents of both
978 sections and memcmp them. The COFF/PE spec says that
979 the Microsoft linker does not implement this
980 correctly, so I'm not going to bother doing it
983 case SEC_LINK_DUPLICATES_SAME_SIZE
:
984 if (bfd_section_size (abfd
, sec
)
985 != bfd_section_size (l
->sec
->owner
, l
->sec
))
986 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
991 /* Set the output_section field so that wild_doit does not
992 create a lang_input_section structure for this section.
993 Since there might be a symbol in the section being
994 discarded, we must retain a pointer to the section which
995 we are really going to use. */
996 sec
->output_section
= bfd_abs_section_ptr
;
997 if (sec
->comdat
!= NULL
)
998 sec
->comdat
->sec
= l
->sec
;
1004 /* This is the first section with this name. Record it. Allocate
1005 the memory from the same obstack as the hash table is kept in. */
1007 l
= ((struct already_linked
*)
1008 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1011 l
->next
= already_linked_list
->entry
;
1012 already_linked_list
->entry
= l
;
1015 /* Support routines for the hash table used by section_already_linked,
1016 initialize the table, fill in an entry and remove the table. */
1018 static struct bfd_hash_entry
*
1019 already_linked_newfunc (entry
, table
, string
)
1020 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1021 struct bfd_hash_table
*table
;
1022 const char *string ATTRIBUTE_UNUSED
;
1024 struct already_linked_hash_entry
*ret
=
1025 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1029 return (struct bfd_hash_entry
*) ret
;
1033 already_linked_table_init ()
1035 if (! bfd_hash_table_init_n (&already_linked_table
,
1036 already_linked_newfunc
,
1038 einfo (_("%P%F: Failed to create hash table\n"));
1042 already_linked_table_free ()
1044 bfd_hash_table_free (&already_linked_table
);
1047 /* The wild routines.
1049 These expand statements like *(.text) and foo.o to a list of
1050 explicit actions, like foo.o(.text), bar.o(.text) and
1051 foo.o(.text, .data). */
1053 /* Return true if the PATTERN argument is a wildcard pattern.
1054 Although backslashes are treated specially if a pattern contains
1055 wildcards, we do not consider the mere presence of a backslash to
1056 be enough to cause the the pattern to be treated as a wildcard.
1057 That lets us handle DOS filenames more naturally. */
1061 const char *pattern
;
1065 for (s
= pattern
; *s
!= '\0'; ++s
)
1073 /* Add SECTION to the output section OUTPUT. Do this by creating a
1074 lang_input_section statement which is placed at PTR. FILE is the
1075 input file which holds SECTION. */
1078 wild_doit (ptr
, section
, output
, file
)
1079 lang_statement_list_type
*ptr
;
1081 lang_output_section_statement_type
*output
;
1082 lang_input_statement_type
*file
;
1087 flags
= bfd_get_section_flags (section
->owner
, section
);
1091 /* If we are doing a final link, discard sections marked with
1093 if (! link_info
.relocateable
1094 && (flags
& SEC_EXCLUDE
) != 0)
1097 /* Discard input sections which are assigned to a section named
1098 DISCARD_SECTION_NAME. */
1099 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1102 /* Discard debugging sections if we are stripping debugging
1104 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1105 && (flags
& SEC_DEBUGGING
) != 0)
1110 if (section
->output_section
== NULL
)
1112 /* This prevents future calls from assigning this section. */
1113 section
->output_section
= bfd_abs_section_ptr
;
1118 if (section
->output_section
== NULL
)
1121 lang_input_section_type
*new;
1124 if (output
->bfd_section
== NULL
)
1132 /* Add a section reference to the list */
1133 new = new_stat (lang_input_section
, ptr
);
1135 new->section
= section
;
1137 section
->output_section
= output
->bfd_section
;
1139 flags
= section
->flags
;
1141 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1142 to an output section, because we want to be able to include a
1143 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1144 section (I don't know why we want to do this, but we do).
1145 build_link_order in ldwrite.c handles this case by turning
1146 the embedded SEC_NEVER_LOAD section into a fill. */
1148 flags
&= ~ SEC_NEVER_LOAD
;
1150 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1151 already been processed. One reason to do this is that on pe
1152 format targets, .text$foo sections go into .text and it's odd
1153 to see .text with SEC_LINK_ONCE set. */
1155 if (! link_info
.relocateable
)
1156 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1158 /* If this is not the first input section, and the SEC_READONLY
1159 flag is not currently set, then don't set it just because the
1160 input section has it set. */
1162 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1163 flags
&= ~ SEC_READONLY
;
1165 section
->output_section
->flags
|= flags
;
1167 /* If SEC_READONLY is not set in the input section, then clear
1168 it from the output section. */
1169 if ((section
->flags
& SEC_READONLY
) == 0)
1170 section
->output_section
->flags
&= ~SEC_READONLY
;
1172 switch (output
->sectype
)
1174 case normal_section
:
1179 case overlay_section
:
1180 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1182 case noload_section
:
1183 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1184 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1188 /* Copy over SEC_SMALL_DATA. */
1189 if (section
->flags
& SEC_SMALL_DATA
)
1190 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1192 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1193 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1195 /* If supplied an aligment, then force it. */
1196 if (output
->section_alignment
!= -1)
1197 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1201 /* Handle wildcard sorting. This returns the lang_input_section which
1202 should follow the one we are going to create for SECTION and FILE,
1203 based on the sorting requirements of WILD. It returns NULL if the
1204 new section should just go at the end of the current list. */
1206 static lang_statement_union_type
*
1207 wild_sort (wild
, file
, section
)
1208 lang_wild_statement_type
*wild
;
1209 lang_input_statement_type
*file
;
1212 const char *section_name
;
1213 lang_statement_union_type
*l
;
1215 if (! wild
->filenames_sorted
&& ! wild
->sections_sorted
)
1218 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1219 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->next
)
1221 lang_input_section_type
*ls
;
1223 if (l
->header
.type
!= lang_input_section_enum
)
1225 ls
= &l
->input_section
;
1227 /* Sorting by filename takes precedence over sorting by section
1230 if (wild
->filenames_sorted
)
1232 const char *fn
, *ln
;
1236 /* The PE support for the .idata section as generated by
1237 dlltool assumes that files will be sorted by the name of
1238 the archive and then the name of the file within the
1241 if (file
->the_bfd
!= NULL
1242 && bfd_my_archive (file
->the_bfd
) != NULL
)
1244 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1249 fn
= file
->filename
;
1253 if (ls
->ifile
->the_bfd
!= NULL
1254 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1256 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1261 ln
= ls
->ifile
->filename
;
1265 i
= strcmp (fn
, ln
);
1274 fn
= file
->filename
;
1276 ln
= ls
->ifile
->filename
;
1278 i
= strcmp (fn
, ln
);
1286 /* Here either the files are not sorted by name, or we are
1287 looking at the sections for this file. */
1289 if (wild
->sections_sorted
)
1291 if (strcmp (section_name
,
1292 bfd_get_section_name (ls
->ifile
->the_bfd
,
1302 /* Expand a wild statement for a particular FILE. SECTION may be
1303 NULL, in which case it is a wild card. */
1306 output_section_callback (ptr
, section
, file
, output
)
1307 lang_wild_statement_type
*ptr
;
1309 lang_input_statement_type
*file
;
1312 lang_statement_union_type
*before
;
1314 /* If the wild pattern was marked KEEP, the member sections
1315 should be as well. */
1316 if (ptr
->keep_sections
)
1317 section
->flags
|= SEC_KEEP
;
1319 before
= wild_sort (ptr
, file
, section
);
1321 /* Here BEFORE points to the lang_input_section which
1322 should follow the one we are about to add. If BEFORE
1323 is NULL, then the section should just go at the end
1324 of the current list. */
1327 wild_doit (&ptr
->children
, section
,
1328 (lang_output_section_statement_type
*) output
,
1332 lang_statement_list_type list
;
1333 lang_statement_union_type
**pp
;
1335 lang_list_init (&list
);
1336 wild_doit (&list
, section
,
1337 (lang_output_section_statement_type
*) output
,
1340 /* If we are discarding the section, LIST.HEAD will
1342 if (list
.head
!= NULL
)
1344 ASSERT (list
.head
->next
== NULL
);
1346 for (pp
= &ptr
->children
.head
;
1349 ASSERT (*pp
!= NULL
);
1351 list
.head
->next
= *pp
;
1357 /* This is passed a file name which must have been seen already and
1358 added to the statement tree. We will see if it has been opened
1359 already and had its symbols read. If not then we'll read it. */
1361 static lang_input_statement_type
*
1365 lang_input_statement_type
*search
;
1367 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1368 search
!= (lang_input_statement_type
*) NULL
;
1369 search
= (lang_input_statement_type
*) search
->next_real_file
)
1371 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1373 if (search
->filename
!= (char *) NULL
1374 && name
!= (char *) NULL
1375 && strcmp (search
->filename
, name
) == 0)
1379 if (search
== (lang_input_statement_type
*) NULL
)
1380 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1383 /* If we have already added this file, or this file is not real
1384 (FIXME: can that ever actually happen?) or the name is NULL
1385 (FIXME: can that ever actually happen?) don't add this file. */
1388 || search
->filename
== (const char *) NULL
)
1391 load_symbols (search
, (lang_statement_list_type
*) NULL
);
1396 /* Get the symbols for an input file. */
1399 load_symbols (entry
, place
)
1400 lang_input_statement_type
*entry
;
1401 lang_statement_list_type
*place
;
1408 ldfile_open_file (entry
);
1410 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1411 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1414 lang_statement_list_type
*hold
;
1416 err
= bfd_get_error ();
1417 if (err
== bfd_error_file_ambiguously_recognized
)
1421 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1422 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1423 for (p
= matching
; *p
!= NULL
; p
++)
1427 else if (err
!= bfd_error_file_not_recognized
1429 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1431 bfd_close (entry
->the_bfd
);
1432 entry
->the_bfd
= NULL
;
1434 /* See if the emulation has some special knowledge. */
1436 if (ldemul_unrecognized_file (entry
))
1439 /* Try to interpret the file as a linker script. */
1441 ldfile_open_command_file (entry
->filename
);
1446 ldfile_assumed_script
= true;
1447 parser_input
= input_script
;
1449 ldfile_assumed_script
= false;
1456 if (ldemul_recognized_file (entry
))
1459 /* We don't call ldlang_add_file for an archive. Instead, the
1460 add_symbols entry point will call ldlang_add_file, via the
1461 add_archive_element callback, for each element of the archive
1463 switch (bfd_get_format (entry
->the_bfd
))
1469 ldlang_add_file (entry
);
1470 if (trace_files
|| trace_file_tries
)
1471 info_msg ("%I\n", entry
);
1475 if (entry
->whole_archive
)
1477 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1479 while (member
!= NULL
)
1481 if (! bfd_check_format (member
, bfd_object
))
1482 einfo (_("%F%B: object %B in archive is not object\n"),
1483 entry
->the_bfd
, member
);
1484 if (! ((*link_info
.callbacks
->add_archive_element
)
1485 (&link_info
, member
, "--whole-archive")))
1487 if (! bfd_link_add_symbols (member
, &link_info
))
1488 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1489 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1493 entry
->loaded
= true;
1499 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1500 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1502 entry
->loaded
= true;
1507 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1508 indicating that it is a wildcard. Separate lang_input_section
1509 statements are created for each part of the expansion; they are
1510 added after the wild statement S. OUTPUT is the output section. */
1513 wild (s
, section
, file
, target
, output
)
1514 lang_wild_statement_type
*s
;
1515 const char *section
;
1517 const char *target ATTRIBUTE_UNUSED
;
1518 lang_output_section_statement_type
*output
;
1520 walk_wild (s
, section
, file
, output_section_callback
, (void *) output
);
1522 if (section
!= (char *) NULL
1523 && strcmp (section
, "COMMON") == 0
1524 && default_common_section
== NULL
)
1526 /* Remember the section that common is going to in case we later
1527 get something which doesn't know where to put it. */
1528 default_common_section
= output
;
1532 /* Return true iff target is the sought target. */
1534 get_target (target
, data
)
1535 const bfd_target
* target
;
1538 const char * sought
= (const char *) data
;
1540 return strcmp (target
->name
, sought
) == 0;
1543 /* Like strcpy() but convert to lower case as well. */
1551 while ((c
= * src
++) != 0)
1553 if (isupper ((unsigned char) c
))
1562 /* Remove the first occurance of needle (if any) in haystack
1565 strcut (haystack
, needle
)
1569 haystack
= strstr (haystack
, needle
);
1575 for (src
= haystack
+ strlen (needle
); * src
;)
1576 * haystack
++ = * src
++;
1582 /* Compare two target format name strings.
1583 Return a value indicating how "similar" they are. */
1585 name_compare (first
, second
)
1593 copy1
= xmalloc (strlen (first
) + 1);
1594 copy2
= xmalloc (strlen (second
) + 1);
1596 /* Convert the names to lower case. */
1597 stricpy (copy1
, first
);
1598 stricpy (copy2
, second
);
1600 /* Remove and endian strings from the name. */
1601 strcut (copy1
, "big");
1602 strcut (copy1
, "little");
1603 strcut (copy2
, "big");
1604 strcut (copy2
, "little");
1606 /* Return a value based on how many characters match,
1607 starting from the beginning. If both strings are
1608 the same then return 10 * their length. */
1609 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1610 if (copy1
[result
] == 0)
1622 /* Set by closest_target_match() below. */
1623 static const bfd_target
* winner
;
1625 /* Scan all the valid bfd targets looking for one that has the endianness
1626 requirement that was specified on the command line, and is the nearest
1627 match to the original output target. */
1629 closest_target_match (target
, data
)
1630 const bfd_target
* target
;
1633 const bfd_target
* original
= (const bfd_target
*) data
;
1635 if (command_line
.endian
== ENDIAN_BIG
&& target
->byteorder
!= BFD_ENDIAN_BIG
)
1638 if (command_line
.endian
== ENDIAN_LITTLE
&& target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1641 /* Must be the same flavour. */
1642 if (target
->flavour
!= original
->flavour
)
1645 /* If we have not found a potential winner yet, then record this one. */
1652 /* Oh dear, we now have two potential candidates for a successful match.
1653 Compare their names and choose the better one. */
1654 if (name_compare (target
->name
, original
->name
) > name_compare (winner
->name
, original
->name
))
1657 /* Keep on searching until wqe have checked them all. */
1661 /* Return the BFD target format of the first input file. */
1663 get_first_input_target ()
1665 char * target
= NULL
;
1667 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1669 if (s
->header
.type
== lang_input_statement_enum
1672 ldfile_open_file (s
);
1674 if (s
->the_bfd
!= NULL
1675 && bfd_check_format (s
->the_bfd
, bfd_object
))
1677 target
= bfd_get_target (s
->the_bfd
);
1688 /* Open the output file. */
1696 /* Has the user told us which output format to use ? */
1697 if (output_target
== (char *) NULL
)
1699 /* No - has the current target been set to something other than the default ? */
1700 if (current_target
!= default_target
)
1701 output_target
= current_target
;
1703 /* No - can we determine the format of the first input file ? */
1706 output_target
= get_first_input_target ();
1708 /* Failed - use the default output target. */
1709 if (output_target
== NULL
)
1710 output_target
= default_target
;
1714 /* Has the user requested a particular endianness on the command line ? */
1715 if (command_line
.endian
!= ENDIAN_UNSET
)
1717 const bfd_target
* target
;
1718 enum bfd_endian desired_endian
;
1720 /* Get the chosen target. */
1721 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1723 if (command_line
.endian
== ENDIAN_BIG
)
1724 desired_endian
= BFD_ENDIAN_BIG
;
1726 desired_endian
= BFD_ENDIAN_LITTLE
;
1728 /* See if the target has the wrong endianness. This should not happen
1729 if the linker script has provided big and little endian alternatives,
1730 but some scrips don't do this. */
1731 if (target
->byteorder
!= desired_endian
)
1733 /* If it does, then see if the target provides
1734 an alternative with the correct endianness. */
1735 if (target
->alternative_target
!= NULL
1736 && (target
->alternative_target
->byteorder
== desired_endian
))
1737 output_target
= target
->alternative_target
->name
;
1740 /* Try to find a target as similar as possible to the default
1741 target, but which has the desired endian characteristic. */
1742 (void) bfd_search_for_target (closest_target_match
, (void *) target
);
1744 /* Oh dear - we could not find any targets that satisfy our requirements. */
1746 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1748 output_target
= winner
->name
;
1753 output
= bfd_openw (name
, output_target
);
1755 if (output
== (bfd
*) NULL
)
1757 if (bfd_get_error () == bfd_error_invalid_target
)
1758 einfo (_("%P%F: target %s not found\n"), output_target
);
1760 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1763 delete_output_file_on_failure
= true;
1765 /* output->flags |= D_PAGED;*/
1767 if (! bfd_set_format (output
, bfd_object
))
1768 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1769 if (! bfd_set_arch_mach (output
,
1770 ldfile_output_architecture
,
1771 ldfile_output_machine
))
1772 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1774 link_info
.hash
= bfd_link_hash_table_create (output
);
1775 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1776 einfo (_("%P%F: can not create link hash table: %E\n"));
1778 bfd_set_gp_size (output
, g_switch_value
);
1783 ldlang_open_output (statement
)
1784 lang_statement_union_type
* statement
;
1786 switch (statement
->header
.type
)
1788 case lang_output_statement_enum
:
1789 ASSERT (output_bfd
== (bfd
*) NULL
);
1790 output_bfd
= open_output (statement
->output_statement
.name
);
1791 ldemul_set_output_arch ();
1792 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1793 output_bfd
->flags
|= D_PAGED
;
1795 output_bfd
->flags
&= ~D_PAGED
;
1796 if (config
.text_read_only
)
1797 output_bfd
->flags
|= WP_TEXT
;
1799 output_bfd
->flags
&= ~WP_TEXT
;
1800 if (link_info
.traditional_format
)
1801 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1803 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1806 case lang_target_statement_enum
:
1807 current_target
= statement
->target_statement
.target
;
1814 /* Open all the input files. */
1817 open_input_bfds (s
, force
)
1818 lang_statement_union_type
*s
;
1821 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1823 switch (s
->header
.type
)
1825 case lang_constructors_statement_enum
:
1826 open_input_bfds (constructor_list
.head
, force
);
1828 case lang_output_section_statement_enum
:
1829 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1831 case lang_wild_statement_enum
:
1832 /* Maybe we should load the file's symbols */
1833 if (s
->wild_statement
.filename
1834 && ! wildcardp (s
->wild_statement
.filename
))
1835 (void) lookup_name (s
->wild_statement
.filename
);
1836 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1838 case lang_group_statement_enum
:
1840 struct bfd_link_hash_entry
*undefs
;
1842 /* We must continually search the entries in the group
1843 until no new symbols are added to the list of undefined
1848 undefs
= link_info
.hash
->undefs_tail
;
1849 open_input_bfds (s
->group_statement
.children
.head
, true);
1851 while (undefs
!= link_info
.hash
->undefs_tail
);
1854 case lang_target_statement_enum
:
1855 current_target
= s
->target_statement
.target
;
1857 case lang_input_statement_enum
:
1858 if (s
->input_statement
.real
)
1860 lang_statement_list_type add
;
1862 s
->input_statement
.target
= current_target
;
1864 /* If we are being called from within a group, and this
1865 is an archive which has already been searched, then
1866 force it to be researched. */
1868 && s
->input_statement
.loaded
1869 && bfd_check_format (s
->input_statement
.the_bfd
,
1871 s
->input_statement
.loaded
= false;
1873 lang_list_init (&add
);
1875 load_symbols (&s
->input_statement
, &add
);
1877 if (add
.head
!= NULL
)
1879 *add
.tail
= s
->next
;
1890 /* If there are [COMMONS] statements, put a wild one into the bss section */
1893 lang_reasonable_defaults ()
1896 lang_output_section_statement_lookup (".text");
1897 lang_output_section_statement_lookup (".data");
1899 default_common_section
=
1900 lang_output_section_statement_lookup (".bss");
1903 if (placed_commons
== false)
1905 lang_wild_statement_type
*new =
1906 new_stat (lang_wild_statement
,
1907 &default_common_section
->children
);
1909 new->section_name
= "COMMON";
1910 new->filename
= (char *) NULL
;
1911 lang_list_init (&new->children
);
1918 Add the supplied name to the symbol table as an undefined reference.
1919 Remove items from the chain as we open input bfds
1921 typedef struct ldlang_undef_chain_list
1923 struct ldlang_undef_chain_list
*next
;
1925 } ldlang_undef_chain_list_type
;
1927 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1930 ldlang_add_undef (name
)
1931 CONST
char *CONST name
;
1933 ldlang_undef_chain_list_type
*new =
1934 ((ldlang_undef_chain_list_type
*)
1935 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1937 new->next
= ldlang_undef_chain_list_head
;
1938 ldlang_undef_chain_list_head
= new;
1940 new->name
= buystring (name
);
1943 /* Run through the list of undefineds created above and place them
1944 into the linker hash table as undefined symbols belonging to the
1948 lang_place_undefineds ()
1950 ldlang_undef_chain_list_type
*ptr
;
1952 for (ptr
= ldlang_undef_chain_list_head
;
1953 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1956 struct bfd_link_hash_entry
*h
;
1958 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1959 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1960 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1961 if (h
->type
== bfd_link_hash_new
)
1963 h
->type
= bfd_link_hash_undefined
;
1964 h
->u
.undef
.abfd
= NULL
;
1965 bfd_link_add_undef (link_info
.hash
, h
);
1970 /* Open input files and attatch to output sections */
1972 map_input_to_output_sections (s
, target
, output_section_statement
)
1973 lang_statement_union_type
* s
;
1975 lang_output_section_statement_type
* output_section_statement
;
1977 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1979 switch (s
->header
.type
)
1983 case lang_wild_statement_enum
:
1984 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1985 s
->wild_statement
.filename
, target
,
1986 output_section_statement
);
1989 case lang_constructors_statement_enum
:
1990 map_input_to_output_sections (constructor_list
.head
,
1992 output_section_statement
);
1994 case lang_output_section_statement_enum
:
1995 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1997 &s
->output_section_statement
);
1999 case lang_output_statement_enum
:
2001 case lang_target_statement_enum
:
2002 target
= s
->target_statement
.target
;
2004 case lang_group_statement_enum
:
2005 map_input_to_output_sections (s
->group_statement
.children
.head
,
2007 output_section_statement
);
2009 case lang_fill_statement_enum
:
2010 case lang_input_section_enum
:
2011 case lang_object_symbols_statement_enum
:
2012 case lang_data_statement_enum
:
2013 case lang_reloc_statement_enum
:
2014 case lang_padding_statement_enum
:
2015 case lang_input_statement_enum
:
2016 if (output_section_statement
!= NULL
2017 && output_section_statement
->bfd_section
== NULL
)
2018 init_os (output_section_statement
);
2020 case lang_assignment_statement_enum
:
2021 if (output_section_statement
!= NULL
2022 && output_section_statement
->bfd_section
== NULL
)
2023 init_os (output_section_statement
);
2025 /* Make sure that any sections mentioned in the assignment
2027 exp_init_os (s
->assignment_statement
.exp
);
2029 case lang_afile_asection_pair_statement_enum
:
2032 case lang_address_statement_enum
:
2033 /* Mark the specified section with the supplied address */
2035 lang_output_section_statement_type
*os
=
2036 lang_output_section_statement_lookup
2037 (s
->address_statement
.section_name
);
2039 if (os
->bfd_section
== NULL
)
2041 os
->addr_tree
= s
->address_statement
.address
;
2049 print_output_section_statement (output_section_statement
)
2050 lang_output_section_statement_type
* output_section_statement
;
2052 asection
*section
= output_section_statement
->bfd_section
;
2055 if (output_section_statement
!= abs_output_section
)
2057 minfo ("\n%s", output_section_statement
->name
);
2059 if (section
!= NULL
)
2061 print_dot
= section
->vma
;
2063 len
= strlen (output_section_statement
->name
);
2064 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2069 while (len
< SECTION_NAME_MAP_LENGTH
)
2075 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2077 if (output_section_statement
->load_base
!= NULL
)
2081 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2082 "load base", lang_final_phase_enum
);
2083 minfo (_(" load address 0x%V"), addr
);
2090 print_statement_list (output_section_statement
->children
.head
,
2091 output_section_statement
);
2095 print_assignment (assignment
, output_section
)
2096 lang_assignment_statement_type
* assignment
;
2097 lang_output_section_statement_type
* output_section
;
2100 etree_value_type result
;
2102 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2105 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2106 lang_final_phase_enum
, print_dot
, &print_dot
);
2108 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2119 exp_print_tree (assignment
->exp
);
2125 print_input_statement (statm
)
2126 lang_input_statement_type
* statm
;
2128 if (statm
->filename
!= (char *) NULL
)
2130 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2134 /* Print all symbols defined in a particular section. This is called
2135 via bfd_link_hash_traverse. */
2138 print_one_symbol (hash_entry
, ptr
)
2139 struct bfd_link_hash_entry
*hash_entry
;
2142 asection
*sec
= (asection
*) ptr
;
2144 if ((hash_entry
->type
== bfd_link_hash_defined
2145 || hash_entry
->type
== bfd_link_hash_defweak
)
2146 && sec
== hash_entry
->u
.def
.section
)
2150 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2153 (hash_entry
->u
.def
.value
2154 + hash_entry
->u
.def
.section
->output_offset
2155 + hash_entry
->u
.def
.section
->output_section
->vma
));
2157 minfo (" %T\n", hash_entry
->root
.string
);
2163 /* Print information about an input section to the map file. */
2166 print_input_section (in
)
2167 lang_input_section_type
* in
;
2169 asection
*i
= in
->section
;
2170 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2176 minfo ("%s", i
->name
);
2178 if (i
->output_section
!= NULL
)
2182 len
= 1 + strlen (i
->name
);
2183 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2188 while (len
< SECTION_NAME_MAP_LENGTH
)
2194 minfo ("0x%V %W %B\n",
2195 i
->output_section
->vma
+ i
->output_offset
, size
,
2198 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2200 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2212 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2215 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2217 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
;
2223 print_fill_statement (fill
)
2224 lang_fill_statement_type
* fill
;
2226 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2230 print_data_statement (data
)
2231 lang_data_statement_type
* data
;
2238 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2241 addr
= data
->output_vma
;
2242 if (data
->output_section
!= NULL
)
2243 addr
+= data
->output_section
->vma
;
2271 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2273 if (data
->exp
->type
.node_class
!= etree_value
)
2276 exp_print_tree (data
->exp
);
2281 print_dot
= addr
+ size
;
2284 /* Print an address statement. These are generated by options like
2288 print_address_statement (address
)
2289 lang_address_statement_type
*address
;
2291 minfo (_("Address of section %s set to "), address
->section_name
);
2292 exp_print_tree (address
->address
);
2296 /* Print a reloc statement. */
2299 print_reloc_statement (reloc
)
2300 lang_reloc_statement_type
*reloc
;
2306 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2309 addr
= reloc
->output_vma
;
2310 if (reloc
->output_section
!= NULL
)
2311 addr
+= reloc
->output_section
->vma
;
2313 size
= bfd_get_reloc_size (reloc
->howto
);
2315 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2317 if (reloc
->name
!= NULL
)
2318 minfo ("%s+", reloc
->name
);
2320 minfo ("%s+", reloc
->section
->name
);
2322 exp_print_tree (reloc
->addend_exp
);
2326 print_dot
= addr
+ size
;
2330 print_padding_statement (s
)
2331 lang_padding_statement_type
*s
;
2338 len
= sizeof " *fill*" - 1;
2339 while (len
< SECTION_NAME_MAP_LENGTH
)
2345 addr
= s
->output_offset
;
2346 if (s
->output_section
!= NULL
)
2347 addr
+= s
->output_section
->vma
;
2348 minfo ("0x%V %W", addr
, s
->size
);
2351 minfo (" %u", s
->fill
);
2355 print_dot
= addr
+ s
->size
;
2359 print_wild_statement (w
, os
)
2360 lang_wild_statement_type
* w
;
2361 lang_output_section_statement_type
* os
;
2365 if (w
->filenames_sorted
)
2367 if (w
->exclude_filename_list
!= NULL
)
2370 minfo ("EXCLUDE_FILE ( %s", w
->exclude_filename_list
->name
);
2371 for (tmp
=w
->exclude_filename_list
->next
; tmp
; tmp
= tmp
->next
)
2372 minfo (", %s", tmp
->name
);
2375 if (w
->filename
!= NULL
)
2376 minfo ("%s", w
->filename
);
2379 if (w
->filenames_sorted
)
2383 if (w
->sections_sorted
)
2385 if (w
->section_name
!= NULL
)
2386 minfo ("%s", w
->section_name
);
2389 if (w
->sections_sorted
)
2395 print_statement_list (w
->children
.head
, os
);
2398 /* Print a group statement. */
2402 lang_group_statement_type
*s
;
2403 lang_output_section_statement_type
*os
;
2405 fprintf (config
.map_file
, "START GROUP\n");
2406 print_statement_list (s
->children
.head
, os
);
2407 fprintf (config
.map_file
, "END GROUP\n");
2410 /* Print the list of statements in S.
2411 This can be called for any statement type. */
2414 print_statement_list (s
, os
)
2415 lang_statement_union_type
*s
;
2416 lang_output_section_statement_type
*os
;
2420 print_statement (s
, os
);
2425 /* Print the first statement in statement list S.
2426 This can be called for any statement type. */
2429 print_statement (s
, os
)
2430 lang_statement_union_type
*s
;
2431 lang_output_section_statement_type
*os
;
2433 switch (s
->header
.type
)
2436 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2439 case lang_constructors_statement_enum
:
2440 if (constructor_list
.head
!= NULL
)
2442 if (constructors_sorted
)
2443 minfo (" SORT (CONSTRUCTORS)\n");
2445 minfo (" CONSTRUCTORS\n");
2446 print_statement_list (constructor_list
.head
, os
);
2449 case lang_wild_statement_enum
:
2450 print_wild_statement (&s
->wild_statement
, os
);
2452 case lang_address_statement_enum
:
2453 print_address_statement (&s
->address_statement
);
2455 case lang_object_symbols_statement_enum
:
2456 minfo (" CREATE_OBJECT_SYMBOLS\n");
2458 case lang_fill_statement_enum
:
2459 print_fill_statement (&s
->fill_statement
);
2461 case lang_data_statement_enum
:
2462 print_data_statement (&s
->data_statement
);
2464 case lang_reloc_statement_enum
:
2465 print_reloc_statement (&s
->reloc_statement
);
2467 case lang_input_section_enum
:
2468 print_input_section (&s
->input_section
);
2470 case lang_padding_statement_enum
:
2471 print_padding_statement (&s
->padding_statement
);
2473 case lang_output_section_statement_enum
:
2474 print_output_section_statement (&s
->output_section_statement
);
2476 case lang_assignment_statement_enum
:
2477 print_assignment (&s
->assignment_statement
, os
);
2479 case lang_target_statement_enum
:
2480 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2482 case lang_output_statement_enum
:
2483 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2484 if (output_target
!= NULL
)
2485 minfo (" %s", output_target
);
2488 case lang_input_statement_enum
:
2489 print_input_statement (&s
->input_statement
);
2491 case lang_group_statement_enum
:
2492 print_group (&s
->group_statement
, os
);
2494 case lang_afile_asection_pair_statement_enum
:
2503 print_statement_list (statement_list
.head
, abs_output_section
);
2506 /* Print the first N statements in statement list S to STDERR.
2507 If N == 0, nothing is printed.
2508 If N < 0, the entire list is printed.
2509 Intended to be called from GDB. */
2512 dprint_statement (s
, n
)
2513 lang_statement_union_type
* s
;
2516 FILE *map_save
= config
.map_file
;
2518 config
.map_file
= stderr
;
2521 print_statement_list (s
, abs_output_section
);
2524 while (s
&& --n
>= 0)
2526 print_statement (s
, abs_output_section
);
2531 config
.map_file
= map_save
;
2535 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
2536 lang_statement_union_type
** this_ptr
;
2539 asection
* output_section_statement
;
2542 /* Align this section first to the
2543 input sections requirement, then
2544 to the output section's requirement.
2545 If this alignment is > than any seen before,
2546 then record it too. Perform the alignment by
2547 inserting a magic 'padding' statement.
2550 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
2552 if (alignment_needed
!= 0)
2554 lang_statement_union_type
*new =
2555 ((lang_statement_union_type
*)
2556 stat_alloc (sizeof (lang_padding_statement_type
)));
2558 /* Link into existing chain */
2559 new->header
.next
= *this_ptr
;
2561 new->header
.type
= lang_padding_statement_enum
;
2562 new->padding_statement
.output_section
= output_section_statement
;
2563 new->padding_statement
.output_offset
=
2564 dot
- output_section_statement
->vma
;
2565 new->padding_statement
.fill
= fill
;
2566 new->padding_statement
.size
= alignment_needed
;
2570 /* Remember the most restrictive alignment */
2571 if (power
> output_section_statement
->alignment_power
)
2573 output_section_statement
->alignment_power
= power
;
2575 output_section_statement
->_raw_size
+= alignment_needed
;
2576 return alignment_needed
+ dot
;
2580 /* Work out how much this section will move the dot point */
2582 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
2583 lang_statement_union_type
** this_ptr
;
2584 lang_output_section_statement_type
* output_section_statement
;
2587 boolean relax ATTRIBUTE_UNUSED
;
2589 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2590 asection
*i
= is
->section
;
2592 if (is
->ifile
->just_syms_flag
== false)
2594 if (output_section_statement
->subsection_alignment
!= -1)
2595 i
->alignment_power
=
2596 output_section_statement
->subsection_alignment
;
2598 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
2599 output_section_statement
->bfd_section
, dot
);
2601 /* Remember where in the output section this input section goes */
2603 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
2605 /* Mark how big the output section must be to contain this now
2607 if (i
->_cooked_size
!= 0)
2608 dot
+= i
->_cooked_size
;
2610 dot
+= i
->_raw_size
;
2611 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
2615 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2621 #define IGNORE_SECTION(bfd, s) \
2622 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
2623 || bfd_section_size (bfd, s) == 0)
2625 /* Check to see if any allocated sections overlap with other allocated
2626 sections. This can happen when the linker script specifically specifies
2627 the output section addresses of the two sections. */
2629 lang_check_section_addresses ()
2633 /* Scan all sections in the output list. */
2634 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2638 /* Ignore sections which are not loaded or which have no contents. */
2639 if (IGNORE_SECTION (output_bfd
, s
))
2642 /* Once we reach section 's' stop our seach. This prevents two
2643 warning messages from being produced, one for 'section A overlaps
2644 section B' and one for 'section B overlaps section A'. */
2645 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2652 /* Only consider loadable sections with real contents. */
2653 if (IGNORE_SECTION (output_bfd
, os
))
2656 /* We must check the sections' LMA addresses not their
2657 VMA addresses because overlay sections can have
2658 overlapping VMAs but they must have distinct LMAs. */
2659 s_start
= bfd_section_lma (output_bfd
, s
);
2660 os_start
= bfd_section_lma (output_bfd
, os
);
2661 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) - 1;
2662 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) - 1;
2664 /* Look for an overlap. */
2665 if ((s_end
< os_start
) || (s_start
> os_end
))
2669 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2670 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2672 /* Once we have found one overlap for this section,
2673 stop looking for others. */
2679 /* This variable indicates whether bfd_relax_section should be called
2682 static boolean relax_again
;
2684 /* Set the sizes for all the output sections. */
2687 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2688 lang_statement_union_type
* s
;
2689 lang_output_section_statement_type
* output_section_statement
;
2690 lang_statement_union_type
** prev
;
2695 /* Size up the sections from their constituent parts. */
2696 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2698 switch (s
->header
.type
)
2700 case lang_output_section_statement_enum
:
2703 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2705 if (os
->bfd_section
== NULL
)
2706 /* This section was never actually created. */
2709 /* If this is a COFF shared library section, use the size and
2710 address from the input section. FIXME: This is COFF
2711 specific; it would be cleaner if there were some other way
2712 to do this, but nothing simple comes to mind. */
2713 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2717 if (os
->children
.head
== NULL
2718 || os
->children
.head
->next
!= NULL
2719 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2720 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2723 input
= os
->children
.head
->input_section
.section
;
2724 bfd_set_section_vma (os
->bfd_section
->owner
,
2726 bfd_section_vma (input
->owner
, input
));
2727 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2731 if (bfd_is_abs_section (os
->bfd_section
))
2733 /* No matter what happens, an abs section starts at zero. */
2734 ASSERT (os
->bfd_section
->vma
== 0);
2738 if (os
->addr_tree
== (etree_type
*) NULL
)
2740 /* No address specified for this section, get one
2741 from the region specification. */
2742 if (os
->region
== (lang_memory_region_type
*) NULL
2743 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2744 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2745 && os
->region
->name
[0] == '*'
2746 && strcmp (os
->region
->name
, "*default*") == 0))
2748 os
->region
= lang_memory_default (os
->bfd_section
);
2751 /* If a loadable section is using the default memory
2752 region, and some non default memory regions were
2753 defined, issue a warning. */
2754 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2755 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2756 && ! link_info
.relocateable
2757 && strcmp (os
->region
->name
, "*default*") == 0
2758 && lang_memory_region_list
!= NULL
2759 && (strcmp (lang_memory_region_list
->name
, "*default*") != 0
2760 || lang_memory_region_list
->next
!= NULL
))
2761 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2762 bfd_get_section_name (output_bfd
, os
->bfd_section
));
2764 dot
= os
->region
->current
;
2766 if (os
->section_alignment
== -1)
2771 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2773 if (dot
!= olddot
&& config
.warn_section_align
)
2774 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2775 os
->name
, (unsigned int) (dot
- olddot
));
2782 r
= exp_fold_tree (os
->addr_tree
,
2784 lang_allocating_phase_enum
,
2786 if (r
.valid_p
== false)
2788 einfo (_("%F%S: non constant address expression for section %s\n"),
2791 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2794 /* The section starts here.
2795 First, align to what the section needs. */
2797 if (os
->section_alignment
!= -1)
2798 dot
= align_power (dot
, os
->section_alignment
);
2800 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2802 os
->bfd_section
->output_offset
= 0;
2805 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2806 os
->fill
, dot
, relax
);
2808 /* Ignore the size of the input sections, use the vma and size to
2811 after
= ALIGN_N (os
->bfd_section
->vma
+
2812 os
->bfd_section
->_raw_size
,
2813 /* The coercion here is important, see ld.h. */
2814 (bfd_vma
) os
->block_value
);
2816 if (bfd_is_abs_section (os
->bfd_section
))
2817 ASSERT (after
== os
->bfd_section
->vma
);
2819 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
2820 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2821 os
->processed
= true;
2823 /* Update dot in the region ?
2824 We only do this if the section is going to be allocated,
2825 since unallocated sections do not contribute to the region's
2827 If the SEC_NEVER_LOAD bit is not set, it will affect the
2828 addresses of sections after it. We have to update
2830 if (os
->region
!= (lang_memory_region_type
*) NULL
2831 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2832 & SEC_NEVER_LOAD
) == 0
2833 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2834 & (SEC_ALLOC
| SEC_LOAD
))))
2836 os
->region
->current
= dot
;
2838 /* Make sure the new address is within the region. We
2839 explicitly permit the current address to be at the
2840 exact end of the region when the VMA is non-zero,
2841 in case the region is at the end of addressable
2842 memory and the calculation wraps around. */
2843 if ((os
->region
->current
< os
->region
->origin
2844 || (os
->region
->current
- os
->region
->origin
2845 > os
->region
->length
))
2846 && ((os
->region
->current
2847 != os
->region
->origin
+ os
->region
->length
)
2848 || os
->bfd_section
->vma
== 0))
2851 if (os
->addr_tree
!= (etree_type
*) NULL
)
2853 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2854 os
->region
->current
,
2855 os
->bfd_section
->owner
,
2856 os
->bfd_section
->name
,
2861 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2863 os
->bfd_section
->owner
,
2864 os
->bfd_section
->name
);
2866 /* Reset the region pointer. */
2867 os
->region
->current
= os
->region
->origin
;
2873 case lang_constructors_statement_enum
:
2874 dot
= lang_size_sections (constructor_list
.head
,
2875 output_section_statement
,
2876 &s
->wild_statement
.children
.head
,
2881 case lang_data_statement_enum
:
2883 unsigned int size
= 0;
2885 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2886 s
->data_statement
.output_section
=
2887 output_section_statement
->bfd_section
;
2889 switch (s
->data_statement
.type
)
2907 output_section_statement
->bfd_section
->_raw_size
+= size
;
2908 /* The output section gets contents, and then we inspect for
2909 any flags set in the input script which override any ALLOC. */
2910 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2911 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2912 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2917 case lang_reloc_statement_enum
:
2921 s
->reloc_statement
.output_vma
=
2922 dot
- output_section_statement
->bfd_section
->vma
;
2923 s
->reloc_statement
.output_section
=
2924 output_section_statement
->bfd_section
;
2925 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2927 output_section_statement
->bfd_section
->_raw_size
+= size
;
2931 case lang_wild_statement_enum
:
2933 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2934 output_section_statement
,
2935 &s
->wild_statement
.children
.head
,
2941 case lang_object_symbols_statement_enum
:
2942 link_info
.create_object_symbols_section
=
2943 output_section_statement
->bfd_section
;
2945 case lang_output_statement_enum
:
2946 case lang_target_statement_enum
:
2948 case lang_input_section_enum
:
2952 i
= (*prev
)->input_section
.section
;
2955 if (i
->_cooked_size
== 0)
2956 i
->_cooked_size
= i
->_raw_size
;
2962 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2963 einfo (_("%P%F: can't relax section: %E\n"));
2967 dot
= size_input_section (prev
,
2968 output_section_statement
,
2969 output_section_statement
->fill
,
2973 case lang_input_statement_enum
:
2975 case lang_fill_statement_enum
:
2976 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2978 fill
= s
->fill_statement
.fill
;
2980 case lang_assignment_statement_enum
:
2982 bfd_vma newdot
= dot
;
2984 exp_fold_tree (s
->assignment_statement
.exp
,
2985 output_section_statement
,
2986 lang_allocating_phase_enum
,
2992 /* The assignment changed dot. Insert a pad. */
2993 if (output_section_statement
== abs_output_section
)
2995 /* If we don't have an output section, then just adjust
2996 the default memory address. */
2997 lang_memory_region_lookup ("*default*")->current
= newdot
;
3001 lang_statement_union_type
*new =
3002 ((lang_statement_union_type
*)
3003 stat_alloc (sizeof (lang_padding_statement_type
)));
3005 /* Link into existing chain. */
3006 new->header
.next
= *prev
;
3008 new->header
.type
= lang_padding_statement_enum
;
3009 new->padding_statement
.output_section
=
3010 output_section_statement
->bfd_section
;
3011 new->padding_statement
.output_offset
=
3012 dot
- output_section_statement
->bfd_section
->vma
;
3013 new->padding_statement
.fill
= fill
;
3014 new->padding_statement
.size
= newdot
- dot
;
3015 output_section_statement
->bfd_section
->_raw_size
+=
3016 new->padding_statement
.size
;
3024 case lang_padding_statement_enum
:
3025 /* If we are relaxing, and this is not the first pass, some
3026 padding statements may have been inserted during previous
3027 passes. We may have to move the padding statement to a new
3028 location if dot has a different value at this point in this
3029 pass than it did at this point in the previous pass. */
3030 s
->padding_statement
.output_offset
=
3031 dot
- output_section_statement
->bfd_section
->vma
;
3032 dot
+= s
->padding_statement
.size
;
3033 output_section_statement
->bfd_section
->_raw_size
+=
3034 s
->padding_statement
.size
;
3037 case lang_group_statement_enum
:
3038 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3039 output_section_statement
,
3040 &s
->group_statement
.children
.head
,
3048 /* This can only get here when relaxing is turned on. */
3050 case lang_address_statement_enum
:
3053 prev
= &s
->header
.next
;
3059 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3060 lang_statement_union_type
* s
;
3061 lang_output_section_statement_type
* output_section_statement
;
3065 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3067 switch (s
->header
.type
)
3069 case lang_constructors_statement_enum
:
3070 dot
= lang_do_assignments (constructor_list
.head
,
3071 output_section_statement
,
3076 case lang_output_section_statement_enum
:
3078 lang_output_section_statement_type
*os
=
3079 &(s
->output_section_statement
);
3081 if (os
->bfd_section
!= NULL
)
3083 dot
= os
->bfd_section
->vma
;
3084 (void) lang_do_assignments (os
->children
.head
, os
,
3086 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
3090 /* If nothing has been placed into the output section then
3091 it won't have a bfd_section. */
3092 if (os
->bfd_section
)
3094 os
->bfd_section
->lma
3095 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
3100 case lang_wild_statement_enum
:
3102 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3103 output_section_statement
,
3108 case lang_object_symbols_statement_enum
:
3109 case lang_output_statement_enum
:
3110 case lang_target_statement_enum
:
3112 case lang_common_statement_enum
:
3115 case lang_data_statement_enum
:
3117 etree_value_type value
;
3119 value
= exp_fold_tree (s
->data_statement
.exp
,
3121 lang_final_phase_enum
, dot
, &dot
);
3122 s
->data_statement
.value
= value
.value
;
3123 if (value
.valid_p
== false)
3124 einfo (_("%F%P: invalid data statement\n"));
3126 switch (s
->data_statement
.type
)
3144 case lang_reloc_statement_enum
:
3146 etree_value_type value
;
3148 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3150 lang_final_phase_enum
, dot
, &dot
);
3151 s
->reloc_statement
.addend_value
= value
.value
;
3152 if (value
.valid_p
== false)
3153 einfo (_("%F%P: invalid reloc statement\n"));
3155 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3158 case lang_input_section_enum
:
3160 asection
*in
= s
->input_section
.section
;
3162 if (in
->_cooked_size
!= 0)
3163 dot
+= in
->_cooked_size
;
3165 dot
+= in
->_raw_size
;
3169 case lang_input_statement_enum
:
3171 case lang_fill_statement_enum
:
3172 fill
= s
->fill_statement
.fill
;
3174 case lang_assignment_statement_enum
:
3176 exp_fold_tree (s
->assignment_statement
.exp
,
3177 output_section_statement
,
3178 lang_final_phase_enum
,
3184 case lang_padding_statement_enum
:
3185 dot
+= s
->padding_statement
.size
;
3188 case lang_group_statement_enum
:
3189 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3190 output_section_statement
,
3198 case lang_address_statement_enum
:
3206 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3207 operator .startof. (section_name), it produces an undefined symbol
3208 .startof.section_name. Similarly, when it sees
3209 .sizeof. (section_name), it produces an undefined symbol
3210 .sizeof.section_name. For all the output sections, we look for
3211 such symbols, and set them to the correct value. */
3218 if (link_info
.relocateable
)
3221 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3223 const char *secname
;
3225 struct bfd_link_hash_entry
*h
;
3227 secname
= bfd_get_section_name (output_bfd
, s
);
3228 buf
= xmalloc (10 + strlen (secname
));
3230 sprintf (buf
, ".startof.%s", secname
);
3231 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3232 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3234 h
->type
= bfd_link_hash_defined
;
3235 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3236 h
->u
.def
.section
= bfd_abs_section_ptr
;
3239 sprintf (buf
, ".sizeof.%s", secname
);
3240 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3241 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3243 h
->type
= bfd_link_hash_defined
;
3244 if (s
->_cooked_size
!= 0)
3245 h
->u
.def
.value
= s
->_cooked_size
;
3247 h
->u
.def
.value
= s
->_raw_size
;
3248 h
->u
.def
.section
= bfd_abs_section_ptr
;
3258 struct bfd_link_hash_entry
*h
;
3261 if (link_info
.relocateable
|| link_info
.shared
)
3266 if (entry_symbol
== (char *) NULL
)
3268 /* No entry has been specified. Look for start, but don't warn
3269 if we don't find it. */
3270 entry_symbol
= "start";
3274 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3275 if (h
!= (struct bfd_link_hash_entry
*) NULL
3276 && (h
->type
== bfd_link_hash_defined
3277 || h
->type
== bfd_link_hash_defweak
)
3278 && h
->u
.def
.section
->output_section
!= NULL
)
3282 val
= (h
->u
.def
.value
3283 + bfd_get_section_vma (output_bfd
,
3284 h
->u
.def
.section
->output_section
)
3285 + h
->u
.def
.section
->output_offset
);
3286 if (! bfd_set_start_address (output_bfd
, val
))
3287 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3294 /* We couldn't find the entry symbol. Try parsing it as a
3296 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3299 if (! bfd_set_start_address (output_bfd
, val
))
3300 einfo (_("%P%F: can't set start address\n"));
3306 /* Can't find the entry symbol, and it's not a number. Use
3307 the first address in the text section. */
3308 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3309 if (ts
!= (asection
*) NULL
)
3312 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3313 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3314 if (! bfd_set_start_address (output_bfd
,
3315 bfd_get_section_vma (output_bfd
,
3317 einfo (_("%P%F: can't set start address\n"));
3322 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3329 /* This is a small function used when we want to ignore errors from
3333 #ifdef ANSI_PROTOTYPES
3334 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3336 ignore_bfd_errors (s
)
3337 const char *s ATTRIBUTE_UNUSED
;
3340 /* Don't do anything. */
3343 /* Check that the architecture of all the input files is compatible
3344 with the output file. Also call the backend to let it do any
3345 other checking that is needed. */
3350 lang_statement_union_type
*file
;
3352 CONST bfd_arch_info_type
*compatible
;
3354 for (file
= file_chain
.head
;
3355 file
!= (lang_statement_union_type
*) NULL
;
3356 file
= file
->input_statement
.next
)
3358 input_bfd
= file
->input_statement
.the_bfd
;
3359 compatible
= bfd_arch_get_compatible (input_bfd
,
3361 if (compatible
== NULL
)
3363 if (command_line
.warn_mismatch
)
3364 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3365 bfd_printable_name (input_bfd
), input_bfd
,
3366 bfd_printable_name (output_bfd
));
3370 bfd_error_handler_type pfn
= NULL
;
3372 /* If we aren't supposed to warn about mismatched input
3373 files, temporarily set the BFD error handler to a
3374 function which will do nothing. We still want to call
3375 bfd_merge_private_bfd_data, since it may set up
3376 information which is needed in the output file. */
3377 if (! command_line
.warn_mismatch
)
3378 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3379 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3381 if (command_line
.warn_mismatch
)
3382 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3385 if (! command_line
.warn_mismatch
)
3386 bfd_set_error_handler (pfn
);
3391 /* Look through all the global common symbols and attach them to the
3392 correct section. The -sort-common command line switch may be used
3393 to roughly sort the entries by size. */
3398 if (link_info
.relocateable
3399 && ! command_line
.force_common_definition
)
3402 if (! config
.sort_common
)
3403 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3408 for (power
= 4; power
>= 0; power
--)
3409 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3414 /* Place one common symbol in the correct section. */
3417 lang_one_common (h
, info
)
3418 struct bfd_link_hash_entry
*h
;
3421 unsigned int power_of_two
;
3425 if (h
->type
!= bfd_link_hash_common
)
3429 power_of_two
= h
->u
.c
.p
->alignment_power
;
3431 if (config
.sort_common
3432 && power_of_two
< (unsigned int) *(int *) info
)
3435 section
= h
->u
.c
.p
->section
;
3437 /* Increase the size of the section. */
3438 section
->_cooked_size
= ALIGN_N (section
->_cooked_size
,
3439 (bfd_size_type
) (1 << power_of_two
));
3441 /* Adjust the alignment if necessary. */
3442 if (power_of_two
> section
->alignment_power
)
3443 section
->alignment_power
= power_of_two
;
3445 /* Change the symbol from common to defined. */
3446 h
->type
= bfd_link_hash_defined
;
3447 h
->u
.def
.section
= section
;
3448 h
->u
.def
.value
= section
->_cooked_size
;
3450 /* Increase the size of the section. */
3451 section
->_cooked_size
+= size
;
3453 /* Make sure the section is allocated in memory, and make sure that
3454 it is no longer a common section. */
3455 section
->flags
|= SEC_ALLOC
;
3456 section
->flags
&= ~ SEC_IS_COMMON
;
3458 if (config
.map_file
!= NULL
)
3460 static boolean header_printed
;
3465 if (! header_printed
)
3467 minfo (_("\nAllocating common symbols\n"));
3468 minfo (_("Common symbol size file\n\n"));
3469 header_printed
= true;
3472 name
= demangle (h
->root
.string
);
3474 len
= strlen (name
);
3489 if (size
<= 0xffffffff)
3490 sprintf (buf
, "%lx", (unsigned long) size
);
3492 sprintf_vma (buf
, size
);
3502 minfo ("%B\n", section
->owner
);
3509 run through the input files and ensure that every input
3510 section has somewhere to go. If one is found without
3511 a destination then create an input request and place it
3512 into the statement tree.
3516 lang_place_orphans ()
3518 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3522 for (s
= file
->the_bfd
->sections
;
3523 s
!= (asection
*) NULL
;
3526 if (s
->output_section
== (asection
*) NULL
)
3528 /* This section of the file is not attatched, root
3529 around for a sensible place for it to go */
3531 if (file
->just_syms_flag
)
3533 /* We are only retrieving symbol values from this
3534 file. We want the symbols to act as though the
3535 values in the file are absolute. */
3536 s
->output_section
= bfd_abs_section_ptr
;
3537 s
->output_offset
= s
->vma
;
3539 else if (strcmp (s
->name
, "COMMON") == 0)
3541 /* This is a lonely common section which must have
3542 come from an archive. We attach to the section
3543 with the wildcard. */
3544 if (! link_info
.relocateable
3545 || command_line
.force_common_definition
)
3547 if (default_common_section
== NULL
)
3550 /* This message happens when using the
3551 svr3.ifile linker script, so I have
3553 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3555 default_common_section
=
3556 lang_output_section_statement_lookup (".bss");
3559 wild_doit (&default_common_section
->children
, s
,
3560 default_common_section
, file
);
3563 else if (ldemul_place_orphan (file
, s
))
3567 lang_output_section_statement_type
*os
=
3568 lang_output_section_statement_lookup (s
->name
);
3570 wild_doit (&os
->children
, s
, os
, file
);
3579 lang_set_flags (ptr
, flags
, invert
)
3580 lang_memory_region_type
*ptr
;
3584 flagword
*ptr_flags
;
3586 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3592 *ptr_flags
|= SEC_ALLOC
;
3596 *ptr_flags
|= SEC_READONLY
;
3600 *ptr_flags
|= SEC_DATA
;
3604 *ptr_flags
|= SEC_CODE
;
3609 *ptr_flags
|= SEC_LOAD
;
3613 einfo (_("%P%F: invalid syntax in flags\n"));
3620 /* Call a function on each input file. This function will be called
3621 on an archive, but not on the elements. */
3624 lang_for_each_input_file (func
)
3625 void (*func
) PARAMS ((lang_input_statement_type
*));
3627 lang_input_statement_type
*f
;
3629 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3631 f
= (lang_input_statement_type
*) f
->next_real_file
)
3635 /* Call a function on each file. The function will be called on all
3636 the elements of an archive which are included in the link, but will
3637 not be called on the archive file itself. */
3640 lang_for_each_file (func
)
3641 void (*func
) PARAMS ((lang_input_statement_type
*));
3643 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3654 lang_for_each_input_section (func
)
3655 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
3657 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3661 for (s
= f
->the_bfd
->sections
;
3662 s
!= (asection
*) NULL
;
3665 func (f
->the_bfd
, s
);
3673 ldlang_add_file (entry
)
3674 lang_input_statement_type
* entry
;
3678 lang_statement_append (&file_chain
,
3679 (lang_statement_union_type
*) entry
,
3682 /* The BFD linker needs to have a list of all input BFDs involved in
3684 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3685 ASSERT (entry
->the_bfd
!= output_bfd
);
3686 for (pp
= &link_info
.input_bfds
;
3687 *pp
!= (bfd
*) NULL
;
3688 pp
= &(*pp
)->link_next
)
3690 *pp
= entry
->the_bfd
;
3691 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3692 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3694 /* Look through the sections and check for any which should not be
3695 included in the link. We need to do this now, so that we can
3696 notice when the backend linker tries to report multiple
3697 definition errors for symbols which are in sections we aren't
3698 going to link. FIXME: It might be better to entirely ignore
3699 symbols which are defined in sections which are going to be
3700 discarded. This would require modifying the backend linker for
3701 each backend which might set the SEC_LINK_ONCE flag. If we do
3702 this, we should probably handle SEC_EXCLUDE in the same way. */
3704 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3708 lang_add_output (name
, from_script
)
3712 /* Make -o on command line override OUTPUT in script. */
3713 if (had_output_filename
== false || !from_script
)
3715 output_filename
= name
;
3716 had_output_filename
= true;
3721 static lang_output_section_statement_type
*current_section
;
3733 for (l
= 0; l
< 32; l
++)
3735 if (i
>= (unsigned int) x
)
3744 lang_enter_output_section_statement (output_section_statement_name
,
3745 address_exp
, sectype
, block_value
,
3746 align
, subalign
, ebase
)
3747 const char *output_section_statement_name
;
3748 etree_type
* address_exp
;
3749 enum section_type sectype
;
3750 bfd_vma block_value
;
3752 etree_type
*subalign
;
3755 lang_output_section_statement_type
*os
;
3759 lang_output_section_statement_lookup (output_section_statement_name
);
3763 /* Add this statement to tree */
3764 /* add_statement(lang_output_section_statement_enum,
3765 output_section_statement);*/
3766 /* Make next things chain into subchain of this */
3768 if (os
->addr_tree
==
3769 (etree_type
*) NULL
)
3774 os
->sectype
= sectype
;
3775 if (sectype
!= noload_section
)
3776 os
->flags
= SEC_NO_FLAGS
;
3778 os
->flags
= SEC_NEVER_LOAD
;
3779 os
->block_value
= block_value
? block_value
: 1;
3780 stat_ptr
= &os
->children
;
3782 os
->subsection_alignment
= topower(
3783 exp_get_value_int(subalign
, -1,
3784 "subsection alignment",
3786 os
->section_alignment
= topower(
3787 exp_get_value_int(align
, -1,
3788 "section alignment", 0));
3790 os
->load_base
= ebase
;
3797 lang_output_statement_type
*new =
3798 new_stat (lang_output_statement
, stat_ptr
);
3800 new->name
= output_filename
;
3803 /* Reset the current counters in the regions */
3805 reset_memory_regions ()
3807 lang_memory_region_type
*p
= lang_memory_region_list
;
3809 for (p
= lang_memory_region_list
;
3810 p
!= (lang_memory_region_type
*) NULL
;
3813 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3814 p
->current
= p
->origin
;
3818 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3819 as needed. SECTION may be NULL, in which case it is a wild card. */
3822 gc_section_callback (ptr
, section
, file
, data
)
3823 lang_wild_statement_type
*ptr
;
3825 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
3826 void *data ATTRIBUTE_UNUSED
;
3828 /* If the wild pattern was marked KEEP, the member sections
3829 should be as well. */
3830 if (ptr
->keep_sections
)
3831 section
->flags
|= SEC_KEEP
;
3834 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3835 may be NULL, indicating that it is a wildcard. */
3838 lang_gc_wild (s
, section
, file
)
3839 lang_wild_statement_type
*s
;
3840 const char *section
;
3843 walk_wild (s
, section
, file
, gc_section_callback
, NULL
);
3846 /* Iterate over sections marking them against GC. */
3849 lang_gc_sections_1 (s
)
3850 lang_statement_union_type
* s
;
3852 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3854 switch (s
->header
.type
)
3856 case lang_wild_statement_enum
:
3857 lang_gc_wild (&s
->wild_statement
,
3858 s
->wild_statement
.section_name
,
3859 s
->wild_statement
.filename
);
3861 case lang_constructors_statement_enum
:
3862 lang_gc_sections_1 (constructor_list
.head
);
3864 case lang_output_section_statement_enum
:
3865 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
3867 case lang_group_statement_enum
:
3868 lang_gc_sections_1 (s
->group_statement
.children
.head
);
3879 struct bfd_link_hash_entry
*h
;
3880 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
3882 /* Keep all sections so marked in the link script. */
3884 lang_gc_sections_1 (statement_list
.head
);
3886 /* Keep all sections containing symbols undefined on the command-line.
3887 Handle the entry symbol at the same time. */
3889 if (entry_symbol
!= NULL
)
3891 fake_list_start
.next
= ldlang_undef_chain_list_head
;
3892 fake_list_start
.name
= (char *) entry_symbol
;
3893 ulist
= &fake_list_start
;
3896 ulist
= ldlang_undef_chain_list_head
;
3898 for (; ulist
; ulist
= ulist
->next
)
3900 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
3901 false, false, false);
3903 if (h
!= (struct bfd_link_hash_entry
*) NULL
3904 && (h
->type
== bfd_link_hash_defined
3905 || h
->type
== bfd_link_hash_defweak
)
3906 && ! bfd_is_abs_section (h
->u
.def
.section
))
3908 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3912 bfd_gc_sections (output_bfd
, &link_info
);
3918 lang_reasonable_defaults ();
3919 current_target
= default_target
;
3921 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
3923 ldemul_create_output_section_statements ();
3925 /* Add to the hash table all undefineds on the command line */
3926 lang_place_undefineds ();
3928 already_linked_table_init ();
3930 /* Create a bfd for each input file */
3931 current_target
= default_target
;
3932 open_input_bfds (statement_list
.head
, false);
3934 ldemul_after_open ();
3936 already_linked_table_free ();
3938 /* Make sure that we're not mixing architectures. We call this
3939 after all the input files have been opened, but before we do any
3940 other processing, so that any operations merge_private_bfd_data
3941 does on the output file will be known during the rest of the
3945 /* Handle .exports instead of a version script if we're told to do so. */
3946 if (command_line
.version_exports_section
)
3947 lang_do_version_exports_section ();
3949 /* Build all sets based on the information gathered from the input
3951 ldctor_build_sets ();
3953 /* Remove unreferenced sections if asked to. */
3954 if (command_line
.gc_sections
)
3955 lang_gc_sections ();
3957 /* Size up the common data */
3960 /* Run through the contours of the script and attach input sections
3961 to the correct output sections
3963 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
3964 (lang_output_section_statement_type
*) NULL
);
3967 /* Find any sections not attached explicitly and handle them */
3968 lang_place_orphans ();
3970 ldemul_before_allocation ();
3972 /* We must record the program headers before we try to fix the
3973 section positions, since they will affect SIZEOF_HEADERS. */
3974 lang_record_phdrs ();
3976 /* Now run around and relax if we can */
3977 if (command_line
.relax
)
3979 /* First time round is a trial run to get the 'worst case'
3980 addresses of the objects if there was no relaxing. */
3981 lang_size_sections (statement_list
.head
,
3983 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
3985 /* Keep relaxing until bfd_relax_section gives up. */
3988 reset_memory_regions ();
3990 relax_again
= false;
3992 /* Note: pe-dll.c does something like this also. If you find
3993 you need to change this code, you probably need to change
3994 pe-dll.c also. DJ */
3996 /* Do all the assignments with our current guesses as to
3998 lang_do_assignments (statement_list
.head
,
4000 (fill_type
) 0, (bfd_vma
) 0);
4002 /* Perform another relax pass - this time we know where the
4003 globals are, so can make better guess. */
4004 lang_size_sections (statement_list
.head
,
4006 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
4008 while (relax_again
);
4012 /* Size up the sections. */
4013 lang_size_sections (statement_list
.head
,
4015 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4018 /* See if anything special should be done now we know how big
4020 ldemul_after_allocation ();
4022 /* Fix any .startof. or .sizeof. symbols. */
4023 lang_set_startof ();
4025 /* Do all the assignments, now that we know the final restingplaces
4026 of all the symbols */
4028 lang_do_assignments (statement_list
.head
,
4030 (fill_type
) 0, (bfd_vma
) 0);
4032 /* Make sure that the section addresses make sense. */
4033 if (! link_info
.relocateable
4034 && command_line
.check_section_addresses
)
4035 lang_check_section_addresses ();
4043 /* EXPORTED TO YACC */
4046 lang_add_wild (section_name
, sections_sorted
, filename
, filenames_sorted
,
4047 keep_sections
, exclude_filename_list
)
4048 const char *const section_name
;
4049 boolean sections_sorted
;
4050 const char *const filename
;
4051 boolean filenames_sorted
;
4052 boolean keep_sections
;
4053 struct name_list
*exclude_filename_list
;
4055 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
4058 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
4060 placed_commons
= true;
4062 if (filename
!= NULL
&& ! wildcardp (filename
))
4064 lang_has_input_file
= true;
4066 new->section_name
= section_name
;
4067 new->sections_sorted
= sections_sorted
;
4068 new->filename
= filename
;
4069 new->filenames_sorted
= filenames_sorted
;
4070 new->keep_sections
= keep_sections
;
4071 new->exclude_filename_list
= exclude_filename_list
;
4072 lang_list_init (&new->children
);
4076 lang_section_start (name
, address
)
4078 etree_type
* address
;
4080 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
4082 ad
->section_name
= name
;
4083 ad
->address
= address
;
4086 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4087 because of a -e argument on the command line, or zero if this is
4088 called by ENTRY in a linker script. Command line arguments take
4092 lang_add_entry (name
, cmdline
)
4096 if (entry_symbol
== NULL
4098 || ! entry_from_cmdline
)
4100 entry_symbol
= name
;
4101 entry_from_cmdline
= cmdline
;
4106 lang_add_target (name
)
4109 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4125 map_option_f
= true;
4136 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4143 lang_add_data (type
, exp
)
4145 union etree_union
*exp
;
4148 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4156 /* Create a new reloc statement. RELOC is the BFD relocation type to
4157 generate. HOWTO is the corresponding howto structure (we could
4158 look this up, but the caller has already done so). SECTION is the
4159 section to generate a reloc against, or NAME is the name of the
4160 symbol to generate a reloc against. Exactly one of SECTION and
4161 NAME must be NULL. ADDEND is an expression for the addend. */
4164 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4165 bfd_reloc_code_real_type reloc
;
4166 reloc_howto_type
*howto
;
4169 union etree_union
*addend
;
4171 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4175 p
->section
= section
;
4177 p
->addend_exp
= addend
;
4179 p
->addend_value
= 0;
4180 p
->output_section
= NULL
;
4184 lang_assignment_statement_type
*
4185 lang_add_assignment (exp
)
4188 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4196 lang_add_attribute (attribute
)
4197 enum statement_enum attribute
;
4199 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4206 if (startup_file
!= (char *) NULL
)
4208 einfo (_("%P%Fmultiple STARTUP files\n"));
4210 first_file
->filename
= name
;
4211 first_file
->local_sym_name
= name
;
4212 first_file
->real
= true;
4214 startup_file
= name
;
4221 lang_float_flag
= maybe
;
4225 lang_leave_output_section_statement (fill
, memspec
, phdrs
)
4227 const char *memspec
;
4228 struct lang_output_section_phdr_list
*phdrs
;
4230 current_section
->fill
= fill
;
4231 current_section
->region
= lang_memory_region_lookup (memspec
);
4232 current_section
->phdrs
= phdrs
;
4233 stat_ptr
= &statement_list
;
4237 Create an absolute symbol with the given name with the value of the
4238 address of first byte of the section named.
4240 If the symbol already exists, then do nothing.
4243 lang_abs_symbol_at_beginning_of (secname
, name
)
4244 const char *secname
;
4247 struct bfd_link_hash_entry
*h
;
4249 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4250 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4251 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4253 if (h
->type
== bfd_link_hash_new
4254 || h
->type
== bfd_link_hash_undefined
)
4258 h
->type
= bfd_link_hash_defined
;
4260 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4261 if (sec
== (asection
*) NULL
)
4264 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4266 h
->u
.def
.section
= bfd_abs_section_ptr
;
4271 Create an absolute symbol with the given name with the value of the
4272 address of the first byte after the end of the section named.
4274 If the symbol already exists, then do nothing.
4277 lang_abs_symbol_at_end_of (secname
, name
)
4278 const char *secname
;
4281 struct bfd_link_hash_entry
*h
;
4283 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4284 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4285 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4287 if (h
->type
== bfd_link_hash_new
4288 || h
->type
== bfd_link_hash_undefined
)
4292 h
->type
= bfd_link_hash_defined
;
4294 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4295 if (sec
== (asection
*) NULL
)
4298 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4299 + bfd_section_size (output_bfd
, sec
));
4301 h
->u
.def
.section
= bfd_abs_section_ptr
;
4306 lang_statement_append (list
, element
, field
)
4307 lang_statement_list_type
* list
;
4308 lang_statement_union_type
* element
;
4309 lang_statement_union_type
** field
;
4311 *(list
->tail
) = element
;
4315 /* Set the output format type. -oformat overrides scripts. */
4318 lang_add_output_format (format
, big
, little
, from_script
)
4324 if (output_target
== NULL
|| !from_script
)
4326 if (command_line
.endian
== ENDIAN_BIG
4329 else if (command_line
.endian
== ENDIAN_LITTLE
4333 output_target
= format
;
4337 /* Enter a group. This creates a new lang_group_statement, and sets
4338 stat_ptr to build new statements within the group. */
4343 lang_group_statement_type
*g
;
4345 g
= new_stat (lang_group_statement
, stat_ptr
);
4346 lang_list_init (&g
->children
);
4347 stat_ptr
= &g
->children
;
4350 /* Leave a group. This just resets stat_ptr to start writing to the
4351 regular list of statements again. Note that this will not work if
4352 groups can occur inside anything else which can adjust stat_ptr,
4353 but currently they can't. */
4358 stat_ptr
= &statement_list
;
4361 /* Add a new program header. This is called for each entry in a PHDRS
4362 command in a linker script. */
4365 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4373 struct lang_phdr
*n
, **pp
;
4375 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4378 n
->type
= exp_get_value_int (type
, 0, "program header type",
4379 lang_final_phase_enum
);
4380 n
->filehdr
= filehdr
;
4385 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4390 /* Record the program header information in the output BFD. FIXME: We
4391 should not be calling an ELF specific function here. */
4394 lang_record_phdrs ()
4398 struct lang_output_section_phdr_list
*last
;
4399 struct lang_phdr
*l
;
4400 lang_statement_union_type
*u
;
4403 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4405 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4412 for (u
= lang_output_section_statement
.head
;
4414 u
= u
->output_section_statement
.next
)
4416 lang_output_section_statement_type
*os
;
4417 struct lang_output_section_phdr_list
*pl
;
4419 os
= &u
->output_section_statement
;
4426 if (os
->sectype
== noload_section
4427 || os
->bfd_section
== NULL
4428 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4433 if (os
->bfd_section
== NULL
)
4436 for (; pl
!= NULL
; pl
= pl
->next
)
4438 if (strcmp (pl
->name
, l
->name
) == 0)
4443 secs
= ((asection
**)
4444 xrealloc (secs
, alc
* sizeof (asection
*)));
4446 secs
[c
] = os
->bfd_section
;
4453 if (l
->flags
== NULL
)
4456 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4457 lang_final_phase_enum
);
4462 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4463 lang_final_phase_enum
);
4465 if (! bfd_record_phdr (output_bfd
, l
->type
,
4466 l
->flags
== NULL
? false : true,
4468 l
->at
== NULL
? false : true,
4469 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4470 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4475 /* Make sure all the phdr assignments succeeded. */
4476 for (u
= lang_output_section_statement
.head
;
4478 u
= u
->output_section_statement
.next
)
4480 struct lang_output_section_phdr_list
*pl
;
4482 if (u
->output_section_statement
.bfd_section
== NULL
)
4485 for (pl
= u
->output_section_statement
.phdrs
;
4488 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4489 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4490 u
->output_section_statement
.name
, pl
->name
);
4494 /* Record a list of sections which may not be cross referenced. */
4497 lang_add_nocrossref (l
)
4498 struct lang_nocrossref
*l
;
4500 struct lang_nocrossrefs
*n
;
4502 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4503 n
->next
= nocrossref_list
;
4505 nocrossref_list
= n
;
4507 /* Set notice_all so that we get informed about all symbols. */
4508 link_info
.notice_all
= true;
4511 /* Overlay handling. We handle overlays with some static variables. */
4513 /* The overlay virtual address. */
4514 static etree_type
*overlay_vma
;
4516 /* The overlay load address. */
4517 static etree_type
*overlay_lma
;
4519 /* Whether nocrossrefs is set for this overlay. */
4520 static int overlay_nocrossrefs
;
4522 /* An expression for the maximum section size seen so far. */
4523 static etree_type
*overlay_max
;
4525 /* A list of all the sections in this overlay. */
4529 struct overlay_list
*next
;
4530 lang_output_section_statement_type
*os
;
4533 static struct overlay_list
*overlay_list
;
4535 /* Start handling an overlay. */
4538 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4539 etree_type
*vma_expr
;
4540 etree_type
*lma_expr
;
4543 /* The grammar should prevent nested overlays from occurring. */
4544 ASSERT (overlay_vma
== NULL
4545 && overlay_lma
== NULL
4546 && overlay_list
== NULL
4547 && overlay_max
== NULL
);
4549 overlay_vma
= vma_expr
;
4550 overlay_lma
= lma_expr
;
4551 overlay_nocrossrefs
= nocrossrefs
;
4554 /* Start a section in an overlay. We handle this by calling
4555 lang_enter_output_section_statement with the correct VMA and LMA. */
4558 lang_enter_overlay_section (name
)
4561 struct overlay_list
*n
;
4564 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4565 0, 0, 0, overlay_lma
);
4567 /* If this is the first section, then base the VMA and LMA of future
4568 sections on this one. This will work correctly even if `.' is
4569 used in the addresses. */
4570 if (overlay_list
== NULL
)
4572 overlay_vma
= exp_nameop (ADDR
, name
);
4573 overlay_lma
= exp_nameop (LOADADDR
, name
);
4576 /* Remember the section. */
4577 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4578 n
->os
= current_section
;
4579 n
->next
= overlay_list
;
4582 size
= exp_nameop (SIZEOF
, name
);
4584 /* Adjust the LMA for the next section. */
4585 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4587 /* Arrange to work out the maximum section end address. */
4588 if (overlay_max
== NULL
)
4591 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4594 /* Finish a section in an overlay. There isn't any special to do
4598 lang_leave_overlay_section (fill
, phdrs
)
4600 struct lang_output_section_phdr_list
*phdrs
;
4607 name
= current_section
->name
;
4609 lang_leave_output_section_statement (fill
, "*default*", phdrs
);
4611 /* Define the magic symbols. */
4613 clean
= xmalloc (strlen (name
) + 1);
4615 for (s1
= name
; *s1
!= '\0'; s1
++)
4616 if (isalnum ((unsigned char) *s1
) || *s1
== '_')
4620 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4621 sprintf (buf
, "__load_start_%s", clean
);
4622 lang_add_assignment (exp_assop ('=', buf
,
4623 exp_nameop (LOADADDR
, name
)));
4625 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4626 sprintf (buf
, "__load_stop_%s", clean
);
4627 lang_add_assignment (exp_assop ('=', buf
,
4629 exp_nameop (LOADADDR
, name
),
4630 exp_nameop (SIZEOF
, name
))));
4635 /* Finish an overlay. If there are any overlay wide settings, this
4636 looks through all the sections in the overlay and sets them. */
4639 lang_leave_overlay (fill
, memspec
, phdrs
)
4641 const char *memspec
;
4642 struct lang_output_section_phdr_list
*phdrs
;
4644 lang_memory_region_type
*region
;
4645 struct overlay_list
*l
;
4646 struct lang_nocrossref
*nocrossref
;
4648 if (memspec
== NULL
)
4651 region
= lang_memory_region_lookup (memspec
);
4658 struct overlay_list
*next
;
4660 if (fill
!= 0 && l
->os
->fill
== 0)
4662 if (region
!= NULL
&& l
->os
->region
== NULL
)
4663 l
->os
->region
= region
;
4664 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4665 l
->os
->phdrs
= phdrs
;
4667 if (overlay_nocrossrefs
)
4669 struct lang_nocrossref
*nc
;
4671 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4672 nc
->name
= l
->os
->name
;
4673 nc
->next
= nocrossref
;
4682 if (nocrossref
!= NULL
)
4683 lang_add_nocrossref (nocrossref
);
4685 /* Update . for the end of the overlay. */
4686 lang_add_assignment (exp_assop ('=', ".",
4687 exp_binop ('+', overlay_vma
, overlay_max
)));
4691 overlay_nocrossrefs
= 0;
4692 overlay_list
= NULL
;
4696 /* Version handling. This is only useful for ELF. */
4698 /* This global variable holds the version tree that we build. */
4700 struct bfd_elf_version_tree
*lang_elf_version_info
;
4703 lang_vers_match_lang_c (expr
, sym
)
4704 struct bfd_elf_version_expr
*expr
;
4707 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4709 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4713 lang_vers_match_lang_cplusplus (expr
, sym
)
4714 struct bfd_elf_version_expr
*expr
;
4720 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4723 alt_sym
= cplus_demangle(sym
, /* DMGL_NO_TPARAMS */ 0);
4726 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4727 Should we early out false in this case? */
4728 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4732 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4740 lang_vers_match_lang_java (expr
, sym
)
4741 struct bfd_elf_version_expr
*expr
;
4747 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4750 alt_sym
= cplus_demangle(sym
, DMGL_JAVA
);
4753 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4754 Should we early out false in this case? */
4755 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4759 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4766 /* This is called for each variable name or match expression. */
4768 struct bfd_elf_version_expr
*
4769 lang_new_vers_regex (orig
, new, lang
)
4770 struct bfd_elf_version_expr
*orig
;
4774 struct bfd_elf_version_expr
*ret
;
4776 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4780 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4781 ret
->match
= lang_vers_match_lang_c
;
4782 else if (strcasecmp (lang
, "C++") == 0)
4783 ret
->match
= lang_vers_match_lang_cplusplus
;
4784 else if (strcasecmp (lang
, "Java") == 0)
4785 ret
->match
= lang_vers_match_lang_java
;
4788 einfo (_("%X%P: unknown language `%s' in version information\n"),
4790 ret
->match
= lang_vers_match_lang_c
;
4796 /* This is called for each set of variable names and match
4799 struct bfd_elf_version_tree
*
4800 lang_new_vers_node (globals
, locals
)
4801 struct bfd_elf_version_expr
*globals
;
4802 struct bfd_elf_version_expr
*locals
;
4804 struct bfd_elf_version_tree
*ret
;
4806 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
4810 ret
->globals
= globals
;
4811 ret
->locals
= locals
;
4813 ret
->name_indx
= (unsigned int) -1;
4818 /* This static variable keeps track of version indices. */
4820 static int version_index
;
4822 /* This is called when we know the name and dependencies of the
4826 lang_register_vers_node (name
, version
, deps
)
4828 struct bfd_elf_version_tree
*version
;
4829 struct bfd_elf_version_deps
*deps
;
4831 struct bfd_elf_version_tree
*t
, **pp
;
4832 struct bfd_elf_version_expr
*e1
;
4834 /* Make sure this node has a unique name. */
4835 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4836 if (strcmp (t
->name
, name
) == 0)
4837 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
4839 /* Check the global and local match names, and make sure there
4840 aren't any duplicates. */
4842 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
4844 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4846 struct bfd_elf_version_expr
*e2
;
4848 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
4849 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4850 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4855 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
4857 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4859 struct bfd_elf_version_expr
*e2
;
4861 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
4862 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4863 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4868 version
->deps
= deps
;
4869 version
->name
= name
;
4871 version
->vernum
= version_index
;
4873 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4878 /* This is called when we see a version dependency. */
4880 struct bfd_elf_version_deps
*
4881 lang_add_vers_depend (list
, name
)
4882 struct bfd_elf_version_deps
*list
;
4885 struct bfd_elf_version_deps
*ret
;
4886 struct bfd_elf_version_tree
*t
;
4888 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
4891 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4893 if (strcmp (t
->name
, name
) == 0)
4895 ret
->version_needed
= t
;
4900 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
4906 lang_do_version_exports_section ()
4908 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
4910 LANG_FOR_EACH_INPUT_STATEMENT (is
)
4912 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
4919 len
= bfd_section_size (is
->the_bfd
, sec
);
4920 contents
= xmalloc (len
);
4921 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
4922 einfo (_("%X%P: unable to read .exports section contents"), sec
);
4925 while (p
< contents
+len
)
4927 greg
= lang_new_vers_regex (greg
, p
, NULL
);
4928 p
= strchr (p
, '\0') + 1;
4931 /* Do not free the contents, as we used them creating the regex. */
4933 /* Do not include this section in the link. */
4934 bfd_set_section_flags (is
->the_bfd
, sec
,
4935 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
4938 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
4939 lang_register_vers_node (command_line
.version_exports_section
,
4940 lang_new_vers_node (greg
, lreg
), NULL
);