1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
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
!= NULL
)
220 if (wildcardp (ptr
->exclude_filename
))
221 match
= fnmatch (ptr
->exclude_filename
, file
->filename
, 0) == 0 ? true : false;
223 match
= strcmp (ptr
->exclude_filename
, file
->filename
) == 0 ? true : false;
229 if (file
->just_syms_flag
== false)
231 register asection
*s
;
237 wildcard
= wildcardp (section
);
239 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
249 name
= bfd_get_section_name (file
->the_bfd
, s
);
251 match
= fnmatch (section
, name
, 0) == 0 ? true : false;
253 match
= strcmp (section
, name
) == 0 ? true : false;
257 (*callback
) (ptr
, s
, file
, data
);
262 /* Handle a wild statement for a single file F. */
265 walk_wild_file (s
, section
, f
, callback
, data
)
266 lang_wild_statement_type
*s
;
268 lang_input_statement_type
*f
;
272 if (f
->the_bfd
== NULL
273 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
274 walk_wild_section (s
, section
, f
, callback
, data
);
279 /* This is an archive file. We must map each member of the
280 archive separately. */
281 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
282 while (member
!= NULL
)
284 /* When lookup_name is called, it will call the add_symbols
285 entry point for the archive. For each element of the
286 archive which is included, BFD will call ldlang_add_file,
287 which will set the usrdata field of the member to the
288 lang_input_statement. */
289 if (member
->usrdata
!= NULL
)
291 walk_wild_section (s
, section
,
292 (lang_input_statement_type
*) member
->usrdata
,
296 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
302 walk_wild (s
, section
, file
, callback
, data
)
303 lang_wild_statement_type
*s
;
309 if (file
== (char *) NULL
)
311 /* Perform the iteration over all files in the list. */
312 LANG_FOR_EACH_INPUT_STATEMENT (f
)
314 walk_wild_file (s
, section
, f
, callback
, data
);
317 else if (wildcardp (file
))
319 LANG_FOR_EACH_INPUT_STATEMENT (f
)
321 if (fnmatch (file
, f
->filename
, FNM_FILE_NAME
) == 0)
322 walk_wild_file (s
, section
, f
, callback
, data
);
327 lang_input_statement_type
*f
;
329 /* Perform the iteration over a single file. */
330 f
= lookup_name (file
);
331 walk_wild_file (s
, section
, f
, callback
, data
);
335 /*----------------------------------------------------------------------
336 lang_for_each_statement walks the parse tree and calls the provided
337 function for each node
341 lang_for_each_statement_worker (func
, s
)
342 void (*func
) PARAMS ((lang_statement_union_type
*));
343 lang_statement_union_type
*s
;
345 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
349 switch (s
->header
.type
)
351 case lang_constructors_statement_enum
:
352 lang_for_each_statement_worker (func
, constructor_list
.head
);
354 case lang_output_section_statement_enum
:
355 lang_for_each_statement_worker
357 s
->output_section_statement
.children
.head
);
359 case lang_wild_statement_enum
:
360 lang_for_each_statement_worker
362 s
->wild_statement
.children
.head
);
364 case lang_group_statement_enum
:
365 lang_for_each_statement_worker (func
,
366 s
->group_statement
.children
.head
);
368 case lang_data_statement_enum
:
369 case lang_reloc_statement_enum
:
370 case lang_object_symbols_statement_enum
:
371 case lang_output_statement_enum
:
372 case lang_target_statement_enum
:
373 case lang_input_section_enum
:
374 case lang_input_statement_enum
:
375 case lang_assignment_statement_enum
:
376 case lang_padding_statement_enum
:
377 case lang_address_statement_enum
:
378 case lang_fill_statement_enum
:
388 lang_for_each_statement (func
)
389 void (*func
) PARAMS ((lang_statement_union_type
*));
391 lang_for_each_statement_worker (func
,
392 statement_list
.head
);
395 /*----------------------------------------------------------------------*/
397 lang_list_init (list
)
398 lang_statement_list_type
*list
;
400 list
->head
= (lang_statement_union_type
*) NULL
;
401 list
->tail
= &list
->head
;
404 /*----------------------------------------------------------------------
406 build a new statement node for the parse tree
411 lang_statement_union_type
*
412 new_statement (type
, size
, list
)
413 enum statement_enum type
;
415 lang_statement_list_type
* list
;
417 lang_statement_union_type
*new = (lang_statement_union_type
*)
420 new->header
.type
= type
;
421 new->header
.next
= (lang_statement_union_type
*) NULL
;
422 lang_statement_append (list
, new, &new->header
.next
);
427 Build a new input file node for the language. There are several ways
428 in which we treat an input file, eg, we only look at symbols, or
429 prefix it with a -l etc.
431 We can be supplied with requests for input files more than once;
432 they may, for example be split over serveral lines like foo.o(.text)
433 foo.o(.data) etc, so when asked for a file we check that we havn't
434 got it already so we don't duplicate the bfd.
437 static lang_input_statement_type
*
438 new_afile (name
, file_type
, target
, add_to_list
)
440 lang_input_file_enum_type file_type
;
444 lang_input_statement_type
*p
;
447 p
= new_stat (lang_input_statement
, stat_ptr
);
450 p
= ((lang_input_statement_type
*)
451 stat_alloc (sizeof (lang_input_statement_type
)));
452 p
->header
.next
= NULL
;
455 lang_has_input_file
= true;
459 case lang_input_file_is_symbols_only_enum
:
461 p
->is_archive
= false;
463 p
->local_sym_name
= name
;
464 p
->just_syms_flag
= true;
465 p
->search_dirs_flag
= false;
467 case lang_input_file_is_fake_enum
:
469 p
->is_archive
= false;
471 p
->local_sym_name
= name
;
472 p
->just_syms_flag
= false;
473 p
->search_dirs_flag
= false;
475 case lang_input_file_is_l_enum
:
476 p
->is_archive
= true;
479 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
480 p
->just_syms_flag
= false;
481 p
->search_dirs_flag
= true;
483 case lang_input_file_is_marker_enum
:
485 p
->is_archive
= false;
487 p
->local_sym_name
= name
;
488 p
->just_syms_flag
= false;
489 p
->search_dirs_flag
= true;
491 case lang_input_file_is_search_file_enum
:
493 p
->is_archive
= false;
495 p
->local_sym_name
= name
;
496 p
->just_syms_flag
= false;
497 p
->search_dirs_flag
= true;
499 case lang_input_file_is_file_enum
:
501 p
->is_archive
= false;
503 p
->local_sym_name
= name
;
504 p
->just_syms_flag
= false;
505 p
->search_dirs_flag
= false;
510 p
->the_bfd
= (bfd
*) NULL
;
511 p
->asymbols
= (asymbol
**) NULL
;
512 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
513 p
->next
= (lang_statement_union_type
*) NULL
;
515 p
->dynamic
= config
.dynamic_link
;
516 p
->whole_archive
= whole_archive
;
518 lang_statement_append (&input_file_chain
,
519 (lang_statement_union_type
*) p
,
524 lang_input_statement_type
*
525 lang_add_input_file (name
, file_type
, target
)
527 lang_input_file_enum_type file_type
;
530 lang_has_input_file
= true;
531 return new_afile (name
, file_type
, target
, true);
534 /* Build enough state so that the parser can build its tree */
538 obstack_begin (&stat_obstack
, 1000);
540 stat_ptr
= &statement_list
;
542 lang_list_init (stat_ptr
);
544 lang_list_init (&input_file_chain
);
545 lang_list_init (&lang_output_section_statement
);
546 lang_list_init (&file_chain
);
547 first_file
= lang_add_input_file ((char *) NULL
,
548 lang_input_file_is_marker_enum
,
550 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
552 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
556 /*----------------------------------------------------------------------
557 A region is an area of memory declared with the
558 MEMORY { name:org=exp, len=exp ... }
561 We maintain a list of all the regions here
563 If no regions are specified in the script, then the default is used
564 which is created when looked up to be the entire data space
567 static lang_memory_region_type
*lang_memory_region_list
;
568 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
570 lang_memory_region_type
*
571 lang_memory_region_lookup (name
)
572 CONST
char *CONST name
;
574 lang_memory_region_type
*p
;
576 for (p
= lang_memory_region_list
;
577 p
!= (lang_memory_region_type
*) NULL
;
580 if (strcmp (p
->name
, name
) == 0)
587 /* This code used to always use the first region in the list as the
588 default region. I changed it to instead use a region
589 encompassing all of memory as the default region. This permits
590 NOLOAD sections to work reasonably without requiring a region.
591 People should specify what region they mean, if they really want
593 if (strcmp (name
, "*default*") == 0)
595 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
597 return lang_memory_region_list
;
603 lang_memory_region_type
*new =
604 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
606 new->name
= buystring (name
);
607 new->next
= (lang_memory_region_type
*) NULL
;
609 *lang_memory_region_list_tail
= new;
610 lang_memory_region_list_tail
= &new->next
;
614 new->length
= ~(bfd_size_type
)0;
616 new->had_full_message
= false;
623 lang_memory_region_type
*
624 lang_memory_default (section
)
627 lang_memory_region_type
*p
;
629 flagword sec_flags
= section
->flags
;
631 /* Override SEC_DATA to mean a writable section. */
632 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
633 sec_flags
|= SEC_DATA
;
635 for (p
= lang_memory_region_list
;
636 p
!= (lang_memory_region_type
*) NULL
;
639 if ((p
->flags
& sec_flags
) != 0
640 && (p
->not_flags
& sec_flags
) == 0)
645 return lang_memory_region_lookup ("*default*");
648 lang_output_section_statement_type
*
649 lang_output_section_find (name
)
650 CONST
char *CONST name
;
652 lang_statement_union_type
*u
;
653 lang_output_section_statement_type
*lookup
;
655 for (u
= lang_output_section_statement
.head
;
656 u
!= (lang_statement_union_type
*) NULL
;
659 lookup
= &u
->output_section_statement
;
660 if (strcmp (name
, lookup
->name
) == 0)
665 return (lang_output_section_statement_type
*) NULL
;
668 lang_output_section_statement_type
*
669 lang_output_section_statement_lookup (name
)
670 CONST
char *CONST name
;
672 lang_output_section_statement_type
*lookup
;
674 lookup
= lang_output_section_find (name
);
675 if (lookup
== (lang_output_section_statement_type
*) NULL
)
678 lookup
= (lang_output_section_statement_type
*)
679 new_stat (lang_output_section_statement
, stat_ptr
);
680 lookup
->region
= (lang_memory_region_type
*) NULL
;
682 lookup
->block_value
= 1;
685 lookup
->next
= (lang_statement_union_type
*) NULL
;
686 lookup
->bfd_section
= (asection
*) NULL
;
687 lookup
->processed
= false;
688 lookup
->sectype
= normal_section
;
689 lookup
->addr_tree
= (etree_type
*) NULL
;
690 lang_list_init (&lookup
->children
);
692 lookup
->memspec
= (CONST
char *) NULL
;
694 lookup
->subsection_alignment
= -1;
695 lookup
->section_alignment
= -1;
696 lookup
->load_base
= (union etree_union
*) NULL
;
697 lookup
->phdrs
= NULL
;
699 lang_statement_append (&lang_output_section_statement
,
700 (lang_statement_union_type
*) lookup
,
707 lang_map_flags (flag
)
710 if (flag
& SEC_ALLOC
)
716 if (flag
& SEC_READONLY
)
729 lang_memory_region_type
*m
;
731 minfo (_("\nMemory Configuration\n\n"));
732 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
733 _("Name"), _("Origin"), _("Length"), _("Attributes"));
735 for (m
= lang_memory_region_list
;
736 m
!= (lang_memory_region_type
*) NULL
;
742 fprintf (config
.map_file
, "%-16s ", m
->name
);
744 sprintf_vma (buf
, m
->origin
);
745 minfo ("0x%s ", buf
);
753 minfo ("0x%V", m
->length
);
754 if (m
->flags
|| m
->not_flags
)
762 lang_map_flags (m
->flags
);
768 lang_map_flags (m
->not_flags
);
775 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
780 /* Initialize an output section. */
784 lang_output_section_statement_type
*s
;
786 section_userdata_type
*new;
788 if (s
->bfd_section
!= NULL
)
791 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
792 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME
);
794 new = ((section_userdata_type
*)
795 stat_alloc (sizeof (section_userdata_type
)));
797 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
798 if (s
->bfd_section
== (asection
*) NULL
)
799 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
800 if (s
->bfd_section
== (asection
*) NULL
)
802 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
803 output_bfd
->xvec
->name
, s
->name
);
805 s
->bfd_section
->output_section
= s
->bfd_section
;
807 /* We initialize an output sections output offset to minus its own */
808 /* vma to allow us to output a section through itself */
809 s
->bfd_section
->output_offset
= 0;
810 get_userdata (s
->bfd_section
) = (PTR
) new;
812 /* If there is a base address, make sure that any sections it might
813 mention are initialized. */
814 if (s
->addr_tree
!= NULL
)
815 exp_init_os (s
->addr_tree
);
818 /* Make sure that all output sections mentioned in an expression are
825 switch (exp
->type
.node_class
)
828 exp_init_os (exp
->assign
.src
);
832 exp_init_os (exp
->binary
.lhs
);
833 exp_init_os (exp
->binary
.rhs
);
837 exp_init_os (exp
->trinary
.cond
);
838 exp_init_os (exp
->trinary
.lhs
);
839 exp_init_os (exp
->trinary
.rhs
);
843 exp_init_os (exp
->unary
.child
);
847 switch (exp
->type
.node_code
)
853 lang_output_section_statement_type
*os
;
855 os
= lang_output_section_find (exp
->name
.name
);
856 if (os
!= NULL
&& os
->bfd_section
== NULL
)
867 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
868 once into the output. This routine checks each section, and
869 arrange to discard it if a section of the same name has already
870 been linked. If the section has COMDAT information, then it uses
871 that to decide whether the section should be included. This code
872 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
873 that is, it does not depend solely upon the section name.
874 section_already_linked is called via bfd_map_over_sections. */
876 /* This is the shape of the elements inside the already_linked hash
877 table. It maps a name onto a list of already_linked elements with
878 the same name. It's possible to get more than one element in a
879 list if the COMDAT sections have different names. */
881 struct already_linked_hash_entry
883 struct bfd_hash_entry root
;
884 struct already_linked
*entry
;
887 struct already_linked
889 struct already_linked
*next
;
893 /* The hash table. */
895 static struct bfd_hash_table already_linked_table
;
899 section_already_linked (abfd
, sec
, data
)
904 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
907 struct already_linked
*l
;
908 struct already_linked_hash_entry
*already_linked_list
;
910 /* If we are only reading symbols from this object, then we want to
911 discard all sections. */
912 if (entry
->just_syms_flag
)
914 sec
->output_section
= bfd_abs_section_ptr
;
915 sec
->output_offset
= sec
->vma
;
919 flags
= bfd_get_section_flags (abfd
, sec
);
921 if ((flags
& SEC_LINK_ONCE
) == 0)
924 /* FIXME: When doing a relocateable link, we may have trouble
925 copying relocations in other sections that refer to local symbols
926 in the section being discarded. Those relocations will have to
927 be converted somehow; as of this writing I'm not sure that any of
928 the backends handle that correctly.
930 It is tempting to instead not discard link once sections when
931 doing a relocateable link (technically, they should be discarded
932 whenever we are building constructors). However, that fails,
933 because the linker winds up combining all the link once sections
934 into a single large link once section, which defeats the purpose
935 of having link once sections in the first place.
937 Also, not merging link once sections in a relocateable link
938 causes trouble for MIPS ELF, which relies in link once semantics
939 to handle the .reginfo section correctly. */
941 name
= bfd_get_section_name (abfd
, sec
);
943 already_linked_list
=
944 ((struct already_linked_hash_entry
*)
945 bfd_hash_lookup (&already_linked_table
, name
, true, false));
947 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
949 if (sec
->comdat
== NULL
950 || l
->sec
->comdat
== NULL
951 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
953 /* The section has already been linked. See if we should
955 switch (flags
& SEC_LINK_DUPLICATES
)
960 case SEC_LINK_DUPLICATES_DISCARD
:
963 case SEC_LINK_DUPLICATES_ONE_ONLY
:
964 if (sec
->comdat
== NULL
)
965 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
968 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
969 abfd
, name
, sec
->comdat
->name
);
972 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
973 /* FIXME: We should really dig out the contents of both
974 sections and memcmp them. The COFF/PE spec says that
975 the Microsoft linker does not implement this
976 correctly, so I'm not going to bother doing it
979 case SEC_LINK_DUPLICATES_SAME_SIZE
:
980 if (bfd_section_size (abfd
, sec
)
981 != bfd_section_size (l
->sec
->owner
, l
->sec
))
982 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
987 /* Set the output_section field so that wild_doit does not
988 create a lang_input_section structure for this section.
989 Since there might be a symbol in the section being
990 discarded, we must retain a pointer to the section which
991 we are really going to use. */
992 sec
->output_section
= bfd_abs_section_ptr
;
993 if (sec
->comdat
!= NULL
)
994 sec
->comdat
->sec
= l
->sec
;
1000 /* This is the first section with this name. Record it. Allocate
1001 the memory from the same obstack as the hash table is kept in. */
1003 l
= ((struct already_linked
*)
1004 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1007 l
->next
= already_linked_list
->entry
;
1008 already_linked_list
->entry
= l
;
1011 /* Support routines for the hash table used by section_already_linked,
1012 initialize the table, fill in an entry and remove the table. */
1014 static struct bfd_hash_entry
*
1015 already_linked_newfunc (entry
, table
, string
)
1016 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1017 struct bfd_hash_table
*table
;
1018 const char *string ATTRIBUTE_UNUSED
;
1020 struct already_linked_hash_entry
*ret
=
1021 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1025 return (struct bfd_hash_entry
*) ret
;
1029 already_linked_table_init ()
1031 if (! bfd_hash_table_init_n (&already_linked_table
,
1032 already_linked_newfunc
,
1034 einfo (_("%P%F: Failed to create hash table\n"));
1038 already_linked_table_free ()
1040 bfd_hash_table_free (&already_linked_table
);
1043 /* The wild routines.
1045 These expand statements like *(.text) and foo.o to a list of
1046 explicit actions, like foo.o(.text), bar.o(.text) and
1047 foo.o(.text, .data). */
1049 /* Return true if the PATTERN argument is a wildcard pattern.
1050 Although backslashes are treated specially if a pattern contains
1051 wildcards, we do not consider the mere presence of a backslash to
1052 be enough to cause the the pattern to be treated as a wildcard.
1053 That lets us handle DOS filenames more naturally. */
1057 const char *pattern
;
1061 for (s
= pattern
; *s
!= '\0'; ++s
)
1069 /* Add SECTION to the output section OUTPUT. Do this by creating a
1070 lang_input_section statement which is placed at PTR. FILE is the
1071 input file which holds SECTION. */
1074 wild_doit (ptr
, section
, output
, file
)
1075 lang_statement_list_type
*ptr
;
1077 lang_output_section_statement_type
*output
;
1078 lang_input_statement_type
*file
;
1083 flags
= bfd_get_section_flags (section
->owner
, section
);
1087 /* If we are doing a final link, discard sections marked with
1089 if (! link_info
.relocateable
1090 && (flags
& SEC_EXCLUDE
) != 0)
1093 /* Discard input sections which are assigned to a section named
1094 DISCARD_SECTION_NAME. */
1095 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1098 /* Discard debugging sections if we are stripping debugging
1100 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1101 && (flags
& SEC_DEBUGGING
) != 0)
1106 if (section
->output_section
== NULL
)
1108 /* This prevents future calls from assigning this section. */
1109 section
->output_section
= bfd_abs_section_ptr
;
1114 if (section
->output_section
== NULL
)
1117 lang_input_section_type
*new;
1120 if (output
->bfd_section
== NULL
)
1128 /* Add a section reference to the list */
1129 new = new_stat (lang_input_section
, ptr
);
1131 new->section
= section
;
1133 section
->output_section
= output
->bfd_section
;
1135 flags
= section
->flags
;
1137 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1138 to an output section, because we want to be able to include a
1139 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1140 section (I don't know why we want to do this, but we do).
1141 build_link_order in ldwrite.c handles this case by turning
1142 the embedded SEC_NEVER_LOAD section into a fill. */
1144 flags
&= ~ SEC_NEVER_LOAD
;
1146 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1147 already been processed. One reason to do this is that on pe
1148 format targets, .text$foo sections go into .text and it's odd
1149 to see .text with SEC_LINK_ONCE set. */
1151 if (! link_info
.relocateable
)
1152 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1154 /* If this is not the first input section, and the SEC_READONLY
1155 flag is not currently set, then don't set it just because the
1156 input section has it set. */
1158 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1159 flags
&= ~ SEC_READONLY
;
1161 section
->output_section
->flags
|= flags
;
1163 /* If SEC_READONLY is not set in the input section, then clear
1164 it from the output section. */
1165 if ((section
->flags
& SEC_READONLY
) == 0)
1166 section
->output_section
->flags
&= ~SEC_READONLY
;
1168 switch (output
->sectype
)
1170 case normal_section
:
1175 case overlay_section
:
1176 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1178 case noload_section
:
1179 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1180 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1184 /* Copy over SEC_SMALL_DATA. */
1185 if (section
->flags
& SEC_SMALL_DATA
)
1186 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1188 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1189 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1191 /* If supplied an aligment, then force it. */
1192 if (output
->section_alignment
!= -1)
1193 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1197 /* Handle wildcard sorting. This returns the lang_input_section which
1198 should follow the one we are going to create for SECTION and FILE,
1199 based on the sorting requirements of WILD. It returns NULL if the
1200 new section should just go at the end of the current list. */
1202 static lang_statement_union_type
*
1203 wild_sort (wild
, file
, section
)
1204 lang_wild_statement_type
*wild
;
1205 lang_input_statement_type
*file
;
1208 const char *section_name
;
1209 lang_statement_union_type
*l
;
1211 if (! wild
->filenames_sorted
&& ! wild
->sections_sorted
)
1214 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1215 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->next
)
1217 lang_input_section_type
*ls
;
1219 if (l
->header
.type
!= lang_input_section_enum
)
1221 ls
= &l
->input_section
;
1223 /* Sorting by filename takes precedence over sorting by section
1226 if (wild
->filenames_sorted
)
1228 const char *fn
, *ln
;
1232 /* The PE support for the .idata section as generated by
1233 dlltool assumes that files will be sorted by the name of
1234 the archive and then the name of the file within the
1237 if (file
->the_bfd
!= NULL
1238 && bfd_my_archive (file
->the_bfd
) != NULL
)
1240 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1245 fn
= file
->filename
;
1249 if (ls
->ifile
->the_bfd
!= NULL
1250 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1252 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1257 ln
= ls
->ifile
->filename
;
1261 i
= strcmp (fn
, ln
);
1270 fn
= file
->filename
;
1272 ln
= ls
->ifile
->filename
;
1274 i
= strcmp (fn
, ln
);
1282 /* Here either the files are not sorted by name, or we are
1283 looking at the sections for this file. */
1285 if (wild
->sections_sorted
)
1287 if (strcmp (section_name
,
1288 bfd_get_section_name (ls
->ifile
->the_bfd
,
1298 /* Expand a wild statement for a particular FILE. SECTION may be
1299 NULL, in which case it is a wild card. */
1302 output_section_callback (ptr
, section
, file
, output
)
1303 lang_wild_statement_type
*ptr
;
1305 lang_input_statement_type
*file
;
1308 lang_statement_union_type
*before
;
1310 /* If the wild pattern was marked KEEP, the member sections
1311 should be as well. */
1312 if (ptr
->keep_sections
)
1313 section
->flags
|= SEC_KEEP
;
1315 before
= wild_sort (ptr
, file
, section
);
1317 /* Here BEFORE points to the lang_input_section which
1318 should follow the one we are about to add. If BEFORE
1319 is NULL, then the section should just go at the end
1320 of the current list. */
1323 wild_doit (&ptr
->children
, section
,
1324 (lang_output_section_statement_type
*) output
,
1328 lang_statement_list_type list
;
1329 lang_statement_union_type
**pp
;
1331 lang_list_init (&list
);
1332 wild_doit (&list
, section
,
1333 (lang_output_section_statement_type
*) output
,
1336 /* If we are discarding the section, LIST.HEAD will
1338 if (list
.head
!= NULL
)
1340 ASSERT (list
.head
->next
== NULL
);
1342 for (pp
= &ptr
->children
.head
;
1345 ASSERT (*pp
!= NULL
);
1347 list
.head
->next
= *pp
;
1353 /* This is passed a file name which must have been seen already and
1354 added to the statement tree. We will see if it has been opened
1355 already and had its symbols read. If not then we'll read it. */
1357 static lang_input_statement_type
*
1361 lang_input_statement_type
*search
;
1363 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1364 search
!= (lang_input_statement_type
*) NULL
;
1365 search
= (lang_input_statement_type
*) search
->next_real_file
)
1367 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1369 if (search
->filename
!= (char *) NULL
1370 && name
!= (char *) NULL
1371 && strcmp (search
->filename
, name
) == 0)
1375 if (search
== (lang_input_statement_type
*) NULL
)
1376 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1379 /* If we have already added this file, or this file is not real
1380 (FIXME: can that ever actually happen?) or the name is NULL
1381 (FIXME: can that ever actually happen?) don't add this file. */
1384 || search
->filename
== (const char *) NULL
)
1387 load_symbols (search
, (lang_statement_list_type
*) NULL
);
1392 /* Get the symbols for an input file. */
1395 load_symbols (entry
, place
)
1396 lang_input_statement_type
*entry
;
1397 lang_statement_list_type
*place
;
1404 ldfile_open_file (entry
);
1406 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1407 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1410 lang_statement_list_type
*hold
;
1412 err
= bfd_get_error ();
1413 if (err
== bfd_error_file_ambiguously_recognized
)
1417 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1418 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1419 for (p
= matching
; *p
!= NULL
; p
++)
1423 else if (err
!= bfd_error_file_not_recognized
1425 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1427 bfd_close (entry
->the_bfd
);
1428 entry
->the_bfd
= NULL
;
1430 /* See if the emulation has some special knowledge. */
1432 if (ldemul_unrecognized_file (entry
))
1435 /* Try to interpret the file as a linker script. */
1437 ldfile_open_command_file (entry
->filename
);
1442 ldfile_assumed_script
= true;
1443 parser_input
= input_script
;
1445 ldfile_assumed_script
= false;
1452 if (ldemul_recognized_file (entry
))
1455 /* We don't call ldlang_add_file for an archive. Instead, the
1456 add_symbols entry point will call ldlang_add_file, via the
1457 add_archive_element callback, for each element of the archive
1459 switch (bfd_get_format (entry
->the_bfd
))
1465 ldlang_add_file (entry
);
1466 if (trace_files
|| trace_file_tries
)
1467 info_msg ("%I\n", entry
);
1471 if (entry
->whole_archive
)
1473 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1475 while (member
!= NULL
)
1477 if (! bfd_check_format (member
, bfd_object
))
1478 einfo (_("%F%B: object %B in archive is not object\n"),
1479 entry
->the_bfd
, member
);
1480 if (! ((*link_info
.callbacks
->add_archive_element
)
1481 (&link_info
, member
, "--whole-archive")))
1483 if (! bfd_link_add_symbols (member
, &link_info
))
1484 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1485 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1489 entry
->loaded
= true;
1495 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1496 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1498 entry
->loaded
= true;
1503 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1504 indicating that it is a wildcard. Separate lang_input_section
1505 statements are created for each part of the expansion; they are
1506 added after the wild statement S. OUTPUT is the output section. */
1509 wild (s
, section
, file
, target
, output
)
1510 lang_wild_statement_type
*s
;
1511 const char *section
;
1513 const char *target ATTRIBUTE_UNUSED
;
1514 lang_output_section_statement_type
*output
;
1516 walk_wild (s
, section
, file
, output_section_callback
, (void *) output
);
1518 if (section
!= (char *) NULL
1519 && strcmp (section
, "COMMON") == 0
1520 && default_common_section
== NULL
)
1522 /* Remember the section that common is going to in case we later
1523 get something which doesn't know where to put it. */
1524 default_common_section
= output
;
1528 /* Return true iff target is the sought target. */
1530 get_target (target
, data
)
1531 const bfd_target
* target
;
1534 const char * sought
= (const char *) data
;
1536 return strcmp (target
->name
, sought
) == 0;
1539 /* Like strcpy() but convert to lower case as well. */
1547 while ((c
= * src
++) != 0)
1549 if (isupper ((unsigned char) c
))
1558 /* Remove the first occurance of needle (if any) in haystack
1561 strcut (haystack
, needle
)
1565 haystack
= strstr (haystack
, needle
);
1571 for (src
= haystack
+ strlen (needle
); * src
;)
1572 * haystack
++ = * src
++;
1578 /* Compare two target format name strings.
1579 Return a value indicating how "similar" they are. */
1581 name_compare (first
, second
)
1589 copy1
= xmalloc (strlen (first
) + 1);
1590 copy2
= xmalloc (strlen (second
) + 1);
1592 /* Convert the names to lower case. */
1593 stricpy (copy1
, first
);
1594 stricpy (copy2
, second
);
1596 /* Remove and endian strings from the name. */
1597 strcut (copy1
, "big");
1598 strcut (copy1
, "little");
1599 strcut (copy2
, "big");
1600 strcut (copy2
, "little");
1602 /* Return a value based on how many characters match,
1603 starting from the beginning. If both strings are
1604 the same then return 10 * their length. */
1605 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1606 if (copy1
[result
] == 0)
1618 /* Set by closest_target_match() below. */
1619 static const bfd_target
* winner
;
1621 /* Scan all the valid bfd targets looking for one that has the endianness
1622 requirement that was specified on the command line, and is the nearest
1623 match to the original output target. */
1625 closest_target_match (target
, data
)
1626 const bfd_target
* target
;
1629 const bfd_target
* original
= (const bfd_target
*) data
;
1631 if (command_line
.endian
== ENDIAN_BIG
&& target
->byteorder
!= BFD_ENDIAN_BIG
)
1634 if (command_line
.endian
== ENDIAN_LITTLE
&& target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1637 /* Must be the same flavour. */
1638 if (target
->flavour
!= original
->flavour
)
1641 /* If we have not found a potential winner yet, then record this one. */
1648 /* Oh dear, we now have two potential candidates for a successful match.
1649 Compare their names and choose the better one. */
1650 if (name_compare (target
->name
, original
->name
) > name_compare (winner
->name
, original
->name
))
1653 /* Keep on searching until wqe have checked them all. */
1657 /* Return the BFD target format of the first input file. */
1659 get_first_input_target ()
1661 char * target
= NULL
;
1663 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1665 if (s
->header
.type
== lang_input_statement_enum
1668 ldfile_open_file (s
);
1670 if (s
->the_bfd
!= NULL
1671 && bfd_check_format (s
->the_bfd
, bfd_object
))
1673 target
= bfd_get_target (s
->the_bfd
);
1684 /* Open the output file. */
1692 /* Has the user told us which output format to use ? */
1693 if (output_target
== (char *) NULL
)
1695 /* No - has the current target been set to something other than the default ? */
1696 if (current_target
!= default_target
)
1697 output_target
= current_target
;
1699 /* No - can we determine the format of the first input file ? */
1702 output_target
= get_first_input_target ();
1704 /* Failed - use the default output target. */
1705 if (output_target
== NULL
)
1706 output_target
= default_target
;
1710 /* Has the user requested a particular endianness on the command line ? */
1711 if (command_line
.endian
!= ENDIAN_UNSET
)
1713 const bfd_target
* target
;
1714 enum bfd_endian desired_endian
;
1716 /* Get the chosen target. */
1717 target
= bfd_search_for_target (get_target
, (void *) output_target
);
1719 if (command_line
.endian
== ENDIAN_BIG
)
1720 desired_endian
= BFD_ENDIAN_BIG
;
1722 desired_endian
= BFD_ENDIAN_LITTLE
;
1724 /* See if the target has the wrong endianness. This should not happen
1725 if the linker script has provided big and little endian alternatives,
1726 but some scrips don't do this. */
1727 if (target
->byteorder
!= desired_endian
)
1729 /* If it does, then see if the target provides
1730 an alternative with the correct endianness. */
1731 if (target
->alternative_target
!= NULL
1732 && (target
->alternative_target
->byteorder
== desired_endian
))
1733 output_target
= target
->alternative_target
->name
;
1736 /* Try to find a target as similar as possible to the default
1737 target, but which has the desired endian characteristic. */
1738 (void) bfd_search_for_target (closest_target_match
, (void *) target
);
1740 /* Oh dear - we could not find any targets that satisfy our requirements. */
1742 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1744 output_target
= winner
->name
;
1749 output
= bfd_openw (name
, output_target
);
1751 if (output
== (bfd
*) NULL
)
1753 if (bfd_get_error () == bfd_error_invalid_target
)
1754 einfo (_("%P%F: target %s not found\n"), output_target
);
1756 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1759 delete_output_file_on_failure
= true;
1761 /* output->flags |= D_PAGED;*/
1763 if (! bfd_set_format (output
, bfd_object
))
1764 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1765 if (! bfd_set_arch_mach (output
,
1766 ldfile_output_architecture
,
1767 ldfile_output_machine
))
1768 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1770 link_info
.hash
= bfd_link_hash_table_create (output
);
1771 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1772 einfo (_("%P%F: can not create link hash table: %E\n"));
1774 bfd_set_gp_size (output
, g_switch_value
);
1779 ldlang_open_output (statement
)
1780 lang_statement_union_type
* statement
;
1782 switch (statement
->header
.type
)
1784 case lang_output_statement_enum
:
1785 ASSERT (output_bfd
== (bfd
*) NULL
);
1786 output_bfd
= open_output (statement
->output_statement
.name
);
1787 ldemul_set_output_arch ();
1788 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1789 output_bfd
->flags
|= D_PAGED
;
1791 output_bfd
->flags
&= ~D_PAGED
;
1792 if (config
.text_read_only
)
1793 output_bfd
->flags
|= WP_TEXT
;
1795 output_bfd
->flags
&= ~WP_TEXT
;
1796 if (link_info
.traditional_format
)
1797 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1799 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1802 case lang_target_statement_enum
:
1803 current_target
= statement
->target_statement
.target
;
1810 /* Open all the input files. */
1813 open_input_bfds (s
, force
)
1814 lang_statement_union_type
*s
;
1817 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1819 switch (s
->header
.type
)
1821 case lang_constructors_statement_enum
:
1822 open_input_bfds (constructor_list
.head
, force
);
1824 case lang_output_section_statement_enum
:
1825 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1827 case lang_wild_statement_enum
:
1828 /* Maybe we should load the file's symbols */
1829 if (s
->wild_statement
.filename
1830 && ! wildcardp (s
->wild_statement
.filename
))
1831 (void) lookup_name (s
->wild_statement
.filename
);
1832 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1834 case lang_group_statement_enum
:
1836 struct bfd_link_hash_entry
*undefs
;
1838 /* We must continually search the entries in the group
1839 until no new symbols are added to the list of undefined
1844 undefs
= link_info
.hash
->undefs_tail
;
1845 open_input_bfds (s
->group_statement
.children
.head
, true);
1847 while (undefs
!= link_info
.hash
->undefs_tail
);
1850 case lang_target_statement_enum
:
1851 current_target
= s
->target_statement
.target
;
1853 case lang_input_statement_enum
:
1854 if (s
->input_statement
.real
)
1856 lang_statement_list_type add
;
1858 s
->input_statement
.target
= current_target
;
1860 /* If we are being called from within a group, and this
1861 is an archive which has already been searched, then
1862 force it to be researched. */
1864 && s
->input_statement
.loaded
1865 && bfd_check_format (s
->input_statement
.the_bfd
,
1867 s
->input_statement
.loaded
= false;
1869 lang_list_init (&add
);
1871 load_symbols (&s
->input_statement
, &add
);
1873 if (add
.head
!= NULL
)
1875 *add
.tail
= s
->next
;
1886 /* If there are [COMMONS] statements, put a wild one into the bss section */
1889 lang_reasonable_defaults ()
1892 lang_output_section_statement_lookup (".text");
1893 lang_output_section_statement_lookup (".data");
1895 default_common_section
=
1896 lang_output_section_statement_lookup (".bss");
1899 if (placed_commons
== false)
1901 lang_wild_statement_type
*new =
1902 new_stat (lang_wild_statement
,
1903 &default_common_section
->children
);
1905 new->section_name
= "COMMON";
1906 new->filename
= (char *) NULL
;
1907 lang_list_init (&new->children
);
1914 Add the supplied name to the symbol table as an undefined reference.
1915 Remove items from the chain as we open input bfds
1917 typedef struct ldlang_undef_chain_list
1919 struct ldlang_undef_chain_list
*next
;
1921 } ldlang_undef_chain_list_type
;
1923 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1926 ldlang_add_undef (name
)
1927 CONST
char *CONST name
;
1929 ldlang_undef_chain_list_type
*new =
1930 ((ldlang_undef_chain_list_type
*)
1931 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1933 new->next
= ldlang_undef_chain_list_head
;
1934 ldlang_undef_chain_list_head
= new;
1936 new->name
= buystring (name
);
1939 /* Run through the list of undefineds created above and place them
1940 into the linker hash table as undefined symbols belonging to the
1944 lang_place_undefineds ()
1946 ldlang_undef_chain_list_type
*ptr
;
1948 for (ptr
= ldlang_undef_chain_list_head
;
1949 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1952 struct bfd_link_hash_entry
*h
;
1954 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1955 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1956 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1957 if (h
->type
== bfd_link_hash_new
)
1959 h
->type
= bfd_link_hash_undefined
;
1960 h
->u
.undef
.abfd
= NULL
;
1961 bfd_link_add_undef (link_info
.hash
, h
);
1966 /* Open input files and attatch to output sections */
1968 map_input_to_output_sections (s
, target
, output_section_statement
)
1969 lang_statement_union_type
* s
;
1971 lang_output_section_statement_type
* output_section_statement
;
1973 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1975 switch (s
->header
.type
)
1979 case lang_wild_statement_enum
:
1980 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1981 s
->wild_statement
.filename
, target
,
1982 output_section_statement
);
1985 case lang_constructors_statement_enum
:
1986 map_input_to_output_sections (constructor_list
.head
,
1988 output_section_statement
);
1990 case lang_output_section_statement_enum
:
1991 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1993 &s
->output_section_statement
);
1995 case lang_output_statement_enum
:
1997 case lang_target_statement_enum
:
1998 target
= s
->target_statement
.target
;
2000 case lang_group_statement_enum
:
2001 map_input_to_output_sections (s
->group_statement
.children
.head
,
2003 output_section_statement
);
2005 case lang_fill_statement_enum
:
2006 case lang_input_section_enum
:
2007 case lang_object_symbols_statement_enum
:
2008 case lang_data_statement_enum
:
2009 case lang_reloc_statement_enum
:
2010 case lang_padding_statement_enum
:
2011 case lang_input_statement_enum
:
2012 if (output_section_statement
!= NULL
2013 && output_section_statement
->bfd_section
== NULL
)
2014 init_os (output_section_statement
);
2016 case lang_assignment_statement_enum
:
2017 if (output_section_statement
!= NULL
2018 && output_section_statement
->bfd_section
== NULL
)
2019 init_os (output_section_statement
);
2021 /* Make sure that any sections mentioned in the assignment
2023 exp_init_os (s
->assignment_statement
.exp
);
2025 case lang_afile_asection_pair_statement_enum
:
2028 case lang_address_statement_enum
:
2029 /* Mark the specified section with the supplied address */
2031 lang_output_section_statement_type
*os
=
2032 lang_output_section_statement_lookup
2033 (s
->address_statement
.section_name
);
2035 if (os
->bfd_section
== NULL
)
2037 os
->addr_tree
= s
->address_statement
.address
;
2045 print_output_section_statement (output_section_statement
)
2046 lang_output_section_statement_type
* output_section_statement
;
2048 asection
*section
= output_section_statement
->bfd_section
;
2051 if (output_section_statement
!= abs_output_section
)
2053 minfo ("\n%s", output_section_statement
->name
);
2055 if (section
!= NULL
)
2057 print_dot
= section
->vma
;
2059 len
= strlen (output_section_statement
->name
);
2060 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2065 while (len
< SECTION_NAME_MAP_LENGTH
)
2071 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2073 if (output_section_statement
->load_base
!= NULL
)
2077 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2078 "load base", lang_final_phase_enum
);
2079 minfo (_(" load address 0x%V"), addr
);
2086 print_statement_list (output_section_statement
->children
.head
,
2087 output_section_statement
);
2091 print_assignment (assignment
, output_section
)
2092 lang_assignment_statement_type
* assignment
;
2093 lang_output_section_statement_type
* output_section
;
2096 etree_value_type result
;
2098 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2101 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2102 lang_final_phase_enum
, print_dot
, &print_dot
);
2104 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2115 exp_print_tree (assignment
->exp
);
2121 print_input_statement (statm
)
2122 lang_input_statement_type
* statm
;
2124 if (statm
->filename
!= (char *) NULL
)
2126 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2130 /* Print all symbols defined in a particular section. This is called
2131 via bfd_link_hash_traverse. */
2134 print_one_symbol (hash_entry
, ptr
)
2135 struct bfd_link_hash_entry
*hash_entry
;
2138 asection
*sec
= (asection
*) ptr
;
2140 if ((hash_entry
->type
== bfd_link_hash_defined
2141 || hash_entry
->type
== bfd_link_hash_defweak
)
2142 && sec
== hash_entry
->u
.def
.section
)
2146 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2149 (hash_entry
->u
.def
.value
2150 + hash_entry
->u
.def
.section
->output_offset
2151 + hash_entry
->u
.def
.section
->output_section
->vma
));
2153 minfo (" %T\n", hash_entry
->root
.string
);
2159 /* Print information about an input section to the map file. */
2162 print_input_section (in
)
2163 lang_input_section_type
* in
;
2165 asection
*i
= in
->section
;
2166 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2172 minfo ("%s", i
->name
);
2174 if (i
->output_section
!= NULL
)
2178 len
= 1 + strlen (i
->name
);
2179 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2184 while (len
< SECTION_NAME_MAP_LENGTH
)
2190 minfo ("0x%V %W %B\n",
2191 i
->output_section
->vma
+ i
->output_offset
, size
,
2194 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2196 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2208 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2211 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2213 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
;
2219 print_fill_statement (fill
)
2220 lang_fill_statement_type
* fill
;
2222 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2226 print_data_statement (data
)
2227 lang_data_statement_type
* data
;
2234 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2237 addr
= data
->output_vma
;
2238 if (data
->output_section
!= NULL
)
2239 addr
+= data
->output_section
->vma
;
2267 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2269 if (data
->exp
->type
.node_class
!= etree_value
)
2272 exp_print_tree (data
->exp
);
2277 print_dot
= addr
+ size
;
2280 /* Print an address statement. These are generated by options like
2284 print_address_statement (address
)
2285 lang_address_statement_type
*address
;
2287 minfo (_("Address of section %s set to "), address
->section_name
);
2288 exp_print_tree (address
->address
);
2292 /* Print a reloc statement. */
2295 print_reloc_statement (reloc
)
2296 lang_reloc_statement_type
*reloc
;
2302 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2305 addr
= reloc
->output_vma
;
2306 if (reloc
->output_section
!= NULL
)
2307 addr
+= reloc
->output_section
->vma
;
2309 size
= bfd_get_reloc_size (reloc
->howto
);
2311 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2313 if (reloc
->name
!= NULL
)
2314 minfo ("%s+", reloc
->name
);
2316 minfo ("%s+", reloc
->section
->name
);
2318 exp_print_tree (reloc
->addend_exp
);
2322 print_dot
= addr
+ size
;
2326 print_padding_statement (s
)
2327 lang_padding_statement_type
*s
;
2334 len
= sizeof " *fill*" - 1;
2335 while (len
< SECTION_NAME_MAP_LENGTH
)
2341 addr
= s
->output_offset
;
2342 if (s
->output_section
!= NULL
)
2343 addr
+= s
->output_section
->vma
;
2344 minfo ("0x%V %W", addr
, s
->size
);
2347 minfo (" %u", s
->fill
);
2351 print_dot
= addr
+ s
->size
;
2355 print_wild_statement (w
, os
)
2356 lang_wild_statement_type
* w
;
2357 lang_output_section_statement_type
* os
;
2361 if (w
->filenames_sorted
)
2363 if (w
->exclude_filename
!= NULL
)
2364 minfo ("EXCLUDE_FILE ( %s )", w
->exclude_filename
);
2365 if (w
->filename
!= NULL
)
2366 minfo ("%s", w
->filename
);
2369 if (w
->filenames_sorted
)
2373 if (w
->sections_sorted
)
2375 if (w
->section_name
!= NULL
)
2376 minfo ("%s", w
->section_name
);
2379 if (w
->sections_sorted
)
2385 print_statement_list (w
->children
.head
, os
);
2388 /* Print a group statement. */
2392 lang_group_statement_type
*s
;
2393 lang_output_section_statement_type
*os
;
2395 fprintf (config
.map_file
, "START GROUP\n");
2396 print_statement_list (s
->children
.head
, os
);
2397 fprintf (config
.map_file
, "END GROUP\n");
2400 /* Print the list of statements in S.
2401 This can be called for any statement type. */
2404 print_statement_list (s
, os
)
2405 lang_statement_union_type
*s
;
2406 lang_output_section_statement_type
*os
;
2410 print_statement (s
, os
);
2415 /* Print the first statement in statement list S.
2416 This can be called for any statement type. */
2419 print_statement (s
, os
)
2420 lang_statement_union_type
*s
;
2421 lang_output_section_statement_type
*os
;
2423 switch (s
->header
.type
)
2426 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2429 case lang_constructors_statement_enum
:
2430 if (constructor_list
.head
!= NULL
)
2432 if (constructors_sorted
)
2433 minfo (" SORT (CONSTRUCTORS)\n");
2435 minfo (" CONSTRUCTORS\n");
2436 print_statement_list (constructor_list
.head
, os
);
2439 case lang_wild_statement_enum
:
2440 print_wild_statement (&s
->wild_statement
, os
);
2442 case lang_address_statement_enum
:
2443 print_address_statement (&s
->address_statement
);
2445 case lang_object_symbols_statement_enum
:
2446 minfo (" CREATE_OBJECT_SYMBOLS\n");
2448 case lang_fill_statement_enum
:
2449 print_fill_statement (&s
->fill_statement
);
2451 case lang_data_statement_enum
:
2452 print_data_statement (&s
->data_statement
);
2454 case lang_reloc_statement_enum
:
2455 print_reloc_statement (&s
->reloc_statement
);
2457 case lang_input_section_enum
:
2458 print_input_section (&s
->input_section
);
2460 case lang_padding_statement_enum
:
2461 print_padding_statement (&s
->padding_statement
);
2463 case lang_output_section_statement_enum
:
2464 print_output_section_statement (&s
->output_section_statement
);
2466 case lang_assignment_statement_enum
:
2467 print_assignment (&s
->assignment_statement
, os
);
2469 case lang_target_statement_enum
:
2470 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2472 case lang_output_statement_enum
:
2473 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2474 if (output_target
!= NULL
)
2475 minfo (" %s", output_target
);
2478 case lang_input_statement_enum
:
2479 print_input_statement (&s
->input_statement
);
2481 case lang_group_statement_enum
:
2482 print_group (&s
->group_statement
, os
);
2484 case lang_afile_asection_pair_statement_enum
:
2493 print_statement_list (statement_list
.head
, abs_output_section
);
2496 /* Print the first N statements in statement list S to STDERR.
2497 If N == 0, nothing is printed.
2498 If N < 0, the entire list is printed.
2499 Intended to be called from GDB. */
2502 dprint_statement (s
, n
)
2503 lang_statement_union_type
* s
;
2506 FILE *map_save
= config
.map_file
;
2508 config
.map_file
= stderr
;
2511 print_statement_list (s
, abs_output_section
);
2514 while (s
&& --n
>= 0)
2516 print_statement (s
, abs_output_section
);
2521 config
.map_file
= map_save
;
2525 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
2526 lang_statement_union_type
** this_ptr
;
2529 asection
* output_section_statement
;
2532 /* Align this section first to the
2533 input sections requirement, then
2534 to the output section's requirement.
2535 If this alignment is > than any seen before,
2536 then record it too. Perform the alignment by
2537 inserting a magic 'padding' statement.
2540 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
2542 if (alignment_needed
!= 0)
2544 lang_statement_union_type
*new =
2545 ((lang_statement_union_type
*)
2546 stat_alloc (sizeof (lang_padding_statement_type
)));
2548 /* Link into existing chain */
2549 new->header
.next
= *this_ptr
;
2551 new->header
.type
= lang_padding_statement_enum
;
2552 new->padding_statement
.output_section
= output_section_statement
;
2553 new->padding_statement
.output_offset
=
2554 dot
- output_section_statement
->vma
;
2555 new->padding_statement
.fill
= fill
;
2556 new->padding_statement
.size
= alignment_needed
;
2560 /* Remember the most restrictive alignment */
2561 if (power
> output_section_statement
->alignment_power
)
2563 output_section_statement
->alignment_power
= power
;
2565 output_section_statement
->_raw_size
+= alignment_needed
;
2566 return alignment_needed
+ dot
;
2570 /* Work out how much this section will move the dot point */
2572 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
2573 lang_statement_union_type
** this_ptr
;
2574 lang_output_section_statement_type
* output_section_statement
;
2577 boolean relax ATTRIBUTE_UNUSED
;
2579 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2580 asection
*i
= is
->section
;
2582 if (is
->ifile
->just_syms_flag
== false)
2584 if (output_section_statement
->subsection_alignment
!= -1)
2585 i
->alignment_power
=
2586 output_section_statement
->subsection_alignment
;
2588 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
2589 output_section_statement
->bfd_section
, dot
);
2591 /* Remember where in the output section this input section goes */
2593 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
2595 /* Mark how big the output section must be to contain this now
2597 if (i
->_cooked_size
!= 0)
2598 dot
+= i
->_cooked_size
;
2600 dot
+= i
->_raw_size
;
2601 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
2605 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2611 /* Check to see if any allocated sections overlap with other allocated
2612 sections. This can happen when the linker script specifically specifies
2613 the output section addresses of the two sections. */
2615 lang_check_section_addresses ()
2619 /* Scan all sections in the output list. */
2620 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2621 /* Ignore sections which are not loaded or which have no contents. */
2622 if ((bfd_get_section_flags (output_bfd
, s
) & (SEC_ALLOC
| SEC_LOAD
))
2623 && bfd_section_size (output_bfd
, s
) != 0)
2627 /* Once we reach section 's' stop our seach. This prevents two
2628 warning messages from being produced, one for 'section A overlaps
2629 section B' and one for 'section B overlaps section A'. */
2630 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2637 /* Only consider loadable sections with real contents. */
2638 if (((bfd_get_section_flags (output_bfd
, os
)
2639 & (SEC_ALLOC
| SEC_LOAD
)) == 0)
2640 || bfd_section_size (output_bfd
, os
) == 0)
2643 /* We must check the sections' LMA addresses not their
2644 VMA addresses because overlay sections can have
2645 overlapping VMAs but they must have distinct LMAs. */
2646 s_start
= bfd_section_lma (output_bfd
, s
);
2647 os_start
= bfd_section_lma (output_bfd
, os
);
2648 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) - 1;
2649 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) - 1;
2651 /* Look for an overlap. */
2652 if ((s_end
< os_start
) || (s_start
> os_end
))
2656 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2657 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2659 /* Once we have found one overlap for this section,
2660 stop looking for others. */
2666 /* This variable indicates whether bfd_relax_section should be called
2669 static boolean relax_again
;
2671 /* Set the sizes for all the output sections. */
2674 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2675 lang_statement_union_type
* s
;
2676 lang_output_section_statement_type
* output_section_statement
;
2677 lang_statement_union_type
** prev
;
2682 /* Size up the sections from their constituent parts. */
2683 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2685 switch (s
->header
.type
)
2687 case lang_output_section_statement_enum
:
2690 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2692 if (os
->bfd_section
== NULL
)
2693 /* This section was never actually created. */
2696 /* If this is a COFF shared library section, use the size and
2697 address from the input section. FIXME: This is COFF
2698 specific; it would be cleaner if there were some other way
2699 to do this, but nothing simple comes to mind. */
2700 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2704 if (os
->children
.head
== NULL
2705 || os
->children
.head
->next
!= NULL
2706 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2707 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2710 input
= os
->children
.head
->input_section
.section
;
2711 bfd_set_section_vma (os
->bfd_section
->owner
,
2713 bfd_section_vma (input
->owner
, input
));
2714 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2718 if (bfd_is_abs_section (os
->bfd_section
))
2720 /* No matter what happens, an abs section starts at zero. */
2721 ASSERT (os
->bfd_section
->vma
== 0);
2725 if (os
->addr_tree
== (etree_type
*) NULL
)
2727 /* No address specified for this section, get one
2728 from the region specification. */
2729 if (os
->region
== (lang_memory_region_type
*) NULL
2730 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2731 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2732 && os
->region
->name
[0] == '*'
2733 && strcmp (os
->region
->name
, "*default*") == 0))
2735 os
->region
= lang_memory_default (os
->bfd_section
);
2738 /* If a loadable section is using the default memory
2739 region, and some non default memory regions were
2740 defined, issue a warning. */
2741 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2742 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2743 && ! link_info
.relocateable
2744 && strcmp (os
->region
->name
, "*default*") == 0
2745 && lang_memory_region_list
!= NULL
2746 && (strcmp (lang_memory_region_list
->name
, "*default*") != 0
2747 || lang_memory_region_list
->next
!= NULL
))
2748 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2749 bfd_get_section_name (output_bfd
, os
->bfd_section
));
2751 dot
= os
->region
->current
;
2753 if (os
->section_alignment
== -1)
2758 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2760 if (dot
!= olddot
&& config
.warn_section_align
)
2761 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2762 os
->name
, (unsigned int) (dot
- olddot
));
2769 r
= exp_fold_tree (os
->addr_tree
,
2771 lang_allocating_phase_enum
,
2773 if (r
.valid_p
== false)
2775 einfo (_("%F%S: non constant address expression for section %s\n"),
2778 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2781 /* The section starts here.
2782 First, align to what the section needs. */
2784 if (os
->section_alignment
!= -1)
2785 dot
= align_power (dot
, os
->section_alignment
);
2787 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2789 os
->bfd_section
->output_offset
= 0;
2792 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2793 os
->fill
, dot
, relax
);
2795 /* Ignore the size of the input sections, use the vma and size to
2798 after
= ALIGN_N (os
->bfd_section
->vma
+
2799 os
->bfd_section
->_raw_size
,
2800 /* The coercion here is important, see ld.h. */
2801 (bfd_vma
) os
->block_value
);
2803 if (bfd_is_abs_section (os
->bfd_section
))
2804 ASSERT (after
== os
->bfd_section
->vma
);
2806 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
2807 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2808 os
->processed
= true;
2810 /* Update dot in the region ?
2811 We only do this if the section is going to be allocated,
2812 since unallocated sections do not contribute to the region's
2813 overall size in memory. */
2814 if (os
->region
!= (lang_memory_region_type
*) NULL
2815 && (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2816 & (SEC_ALLOC
| SEC_LOAD
)))
2818 os
->region
->current
= dot
;
2820 /* Make sure the new address is within the region. We
2821 explicitly permit the current address to be at the
2822 exact end of the region when the VMA is non-zero,
2823 in case the region is at the end of addressable
2824 memory and the calculation wraps around. */
2825 if ((os
->region
->current
< os
->region
->origin
2826 || (os
->region
->current
- os
->region
->origin
2827 > os
->region
->length
))
2828 && ((os
->region
->current
2829 != os
->region
->origin
+ os
->region
->length
)
2830 || os
->bfd_section
->vma
== 0))
2833 if (os
->addr_tree
!= (etree_type
*) NULL
)
2835 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2836 os
->region
->current
,
2837 os
->bfd_section
->owner
,
2838 os
->bfd_section
->name
,
2843 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2845 os
->bfd_section
->owner
,
2846 os
->bfd_section
->name
);
2848 /* Reset the region pointer. */
2849 os
->region
->current
= os
->region
->origin
;
2855 case lang_constructors_statement_enum
:
2856 dot
= lang_size_sections (constructor_list
.head
,
2857 output_section_statement
,
2858 &s
->wild_statement
.children
.head
,
2863 case lang_data_statement_enum
:
2865 unsigned int size
= 0;
2867 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2868 s
->data_statement
.output_section
=
2869 output_section_statement
->bfd_section
;
2871 switch (s
->data_statement
.type
)
2889 output_section_statement
->bfd_section
->_raw_size
+= size
;
2890 /* The output section gets contents, and then we inspect for
2891 any flags set in the input script which override any ALLOC. */
2892 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2893 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2894 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2899 case lang_reloc_statement_enum
:
2903 s
->reloc_statement
.output_vma
=
2904 dot
- output_section_statement
->bfd_section
->vma
;
2905 s
->reloc_statement
.output_section
=
2906 output_section_statement
->bfd_section
;
2907 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2909 output_section_statement
->bfd_section
->_raw_size
+= size
;
2913 case lang_wild_statement_enum
:
2915 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2916 output_section_statement
,
2917 &s
->wild_statement
.children
.head
,
2923 case lang_object_symbols_statement_enum
:
2924 link_info
.create_object_symbols_section
=
2925 output_section_statement
->bfd_section
;
2927 case lang_output_statement_enum
:
2928 case lang_target_statement_enum
:
2930 case lang_input_section_enum
:
2934 i
= (*prev
)->input_section
.section
;
2937 if (i
->_cooked_size
== 0)
2938 i
->_cooked_size
= i
->_raw_size
;
2944 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2945 einfo (_("%P%F: can't relax section: %E\n"));
2949 dot
= size_input_section (prev
,
2950 output_section_statement
,
2951 output_section_statement
->fill
,
2955 case lang_input_statement_enum
:
2957 case lang_fill_statement_enum
:
2958 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2960 fill
= s
->fill_statement
.fill
;
2962 case lang_assignment_statement_enum
:
2964 bfd_vma newdot
= dot
;
2966 exp_fold_tree (s
->assignment_statement
.exp
,
2967 output_section_statement
,
2968 lang_allocating_phase_enum
,
2974 /* The assignment changed dot. Insert a pad. */
2975 if (output_section_statement
== abs_output_section
)
2977 /* If we don't have an output section, then just adjust
2978 the default memory address. */
2979 lang_memory_region_lookup ("*default*")->current
= newdot
;
2983 lang_statement_union_type
*new =
2984 ((lang_statement_union_type
*)
2985 stat_alloc (sizeof (lang_padding_statement_type
)));
2987 /* Link into existing chain. */
2988 new->header
.next
= *prev
;
2990 new->header
.type
= lang_padding_statement_enum
;
2991 new->padding_statement
.output_section
=
2992 output_section_statement
->bfd_section
;
2993 new->padding_statement
.output_offset
=
2994 dot
- output_section_statement
->bfd_section
->vma
;
2995 new->padding_statement
.fill
= fill
;
2996 new->padding_statement
.size
= newdot
- dot
;
2997 output_section_statement
->bfd_section
->_raw_size
+=
2998 new->padding_statement
.size
;
3006 case lang_padding_statement_enum
:
3007 /* If we are relaxing, and this is not the first pass, some
3008 padding statements may have been inserted during previous
3009 passes. We may have to move the padding statement to a new
3010 location if dot has a different value at this point in this
3011 pass than it did at this point in the previous pass. */
3012 s
->padding_statement
.output_offset
=
3013 dot
- output_section_statement
->bfd_section
->vma
;
3014 dot
+= s
->padding_statement
.size
;
3015 output_section_statement
->bfd_section
->_raw_size
+=
3016 s
->padding_statement
.size
;
3019 case lang_group_statement_enum
:
3020 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3021 output_section_statement
,
3022 &s
->group_statement
.children
.head
,
3030 /* This can only get here when relaxing is turned on. */
3032 case lang_address_statement_enum
:
3035 prev
= &s
->header
.next
;
3041 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3042 lang_statement_union_type
* s
;
3043 lang_output_section_statement_type
* output_section_statement
;
3047 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3049 switch (s
->header
.type
)
3051 case lang_constructors_statement_enum
:
3052 dot
= lang_do_assignments (constructor_list
.head
,
3053 output_section_statement
,
3058 case lang_output_section_statement_enum
:
3060 lang_output_section_statement_type
*os
=
3061 &(s
->output_section_statement
);
3063 if (os
->bfd_section
!= NULL
)
3065 dot
= os
->bfd_section
->vma
;
3066 (void) lang_do_assignments (os
->children
.head
, os
,
3068 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
3072 /* If nothing has been placed into the output section then
3073 it won't have a bfd_section. */
3074 if (os
->bfd_section
)
3076 os
->bfd_section
->lma
3077 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
3082 case lang_wild_statement_enum
:
3084 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3085 output_section_statement
,
3090 case lang_object_symbols_statement_enum
:
3091 case lang_output_statement_enum
:
3092 case lang_target_statement_enum
:
3094 case lang_common_statement_enum
:
3097 case lang_data_statement_enum
:
3099 etree_value_type value
;
3101 value
= exp_fold_tree (s
->data_statement
.exp
,
3103 lang_final_phase_enum
, dot
, &dot
);
3104 s
->data_statement
.value
= value
.value
;
3105 if (value
.valid_p
== false)
3106 einfo (_("%F%P: invalid data statement\n"));
3108 switch (s
->data_statement
.type
)
3126 case lang_reloc_statement_enum
:
3128 etree_value_type value
;
3130 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3132 lang_final_phase_enum
, dot
, &dot
);
3133 s
->reloc_statement
.addend_value
= value
.value
;
3134 if (value
.valid_p
== false)
3135 einfo (_("%F%P: invalid reloc statement\n"));
3137 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3140 case lang_input_section_enum
:
3142 asection
*in
= s
->input_section
.section
;
3144 if (in
->_cooked_size
!= 0)
3145 dot
+= in
->_cooked_size
;
3147 dot
+= in
->_raw_size
;
3151 case lang_input_statement_enum
:
3153 case lang_fill_statement_enum
:
3154 fill
= s
->fill_statement
.fill
;
3156 case lang_assignment_statement_enum
:
3158 exp_fold_tree (s
->assignment_statement
.exp
,
3159 output_section_statement
,
3160 lang_final_phase_enum
,
3166 case lang_padding_statement_enum
:
3167 dot
+= s
->padding_statement
.size
;
3170 case lang_group_statement_enum
:
3171 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3172 output_section_statement
,
3180 case lang_address_statement_enum
:
3188 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3189 operator .startof. (section_name), it produces an undefined symbol
3190 .startof.section_name. Similarly, when it sees
3191 .sizeof. (section_name), it produces an undefined symbol
3192 .sizeof.section_name. For all the output sections, we look for
3193 such symbols, and set them to the correct value. */
3200 if (link_info
.relocateable
)
3203 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3205 const char *secname
;
3207 struct bfd_link_hash_entry
*h
;
3209 secname
= bfd_get_section_name (output_bfd
, s
);
3210 buf
= xmalloc (10 + strlen (secname
));
3212 sprintf (buf
, ".startof.%s", secname
);
3213 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3214 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3216 h
->type
= bfd_link_hash_defined
;
3217 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3218 h
->u
.def
.section
= bfd_abs_section_ptr
;
3221 sprintf (buf
, ".sizeof.%s", secname
);
3222 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3223 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3225 h
->type
= bfd_link_hash_defined
;
3226 if (s
->_cooked_size
!= 0)
3227 h
->u
.def
.value
= s
->_cooked_size
;
3229 h
->u
.def
.value
= s
->_raw_size
;
3230 h
->u
.def
.section
= bfd_abs_section_ptr
;
3240 struct bfd_link_hash_entry
*h
;
3243 if (link_info
.relocateable
|| link_info
.shared
)
3248 if (entry_symbol
== (char *) NULL
)
3250 /* No entry has been specified. Look for start, but don't warn
3251 if we don't find it. */
3252 entry_symbol
= "start";
3256 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3257 if (h
!= (struct bfd_link_hash_entry
*) NULL
3258 && (h
->type
== bfd_link_hash_defined
3259 || h
->type
== bfd_link_hash_defweak
)
3260 && h
->u
.def
.section
->output_section
!= NULL
)
3264 val
= (h
->u
.def
.value
3265 + bfd_get_section_vma (output_bfd
,
3266 h
->u
.def
.section
->output_section
)
3267 + h
->u
.def
.section
->output_offset
);
3268 if (! bfd_set_start_address (output_bfd
, val
))
3269 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3276 /* We couldn't find the entry symbol. Try parsing it as a
3278 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3281 if (! bfd_set_start_address (output_bfd
, val
))
3282 einfo (_("%P%F: can't set start address\n"));
3288 /* Can't find the entry symbol, and it's not a number. Use
3289 the first address in the text section. */
3290 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3291 if (ts
!= (asection
*) NULL
)
3294 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3295 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3296 if (! bfd_set_start_address (output_bfd
,
3297 bfd_get_section_vma (output_bfd
,
3299 einfo (_("%P%F: can't set start address\n"));
3304 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3311 /* This is a small function used when we want to ignore errors from
3315 #ifdef ANSI_PROTOTYPES
3316 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3318 ignore_bfd_errors (s
)
3319 const char *s ATTRIBUTE_UNUSED
;
3322 /* Don't do anything. */
3325 /* Check that the architecture of all the input files is compatible
3326 with the output file. Also call the backend to let it do any
3327 other checking that is needed. */
3332 lang_statement_union_type
*file
;
3334 CONST bfd_arch_info_type
*compatible
;
3336 for (file
= file_chain
.head
;
3337 file
!= (lang_statement_union_type
*) NULL
;
3338 file
= file
->input_statement
.next
)
3340 input_bfd
= file
->input_statement
.the_bfd
;
3341 compatible
= bfd_arch_get_compatible (input_bfd
,
3343 if (compatible
== NULL
)
3345 if (command_line
.warn_mismatch
)
3346 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3347 bfd_printable_name (input_bfd
), input_bfd
,
3348 bfd_printable_name (output_bfd
));
3352 bfd_error_handler_type pfn
= NULL
;
3354 /* If we aren't supposed to warn about mismatched input
3355 files, temporarily set the BFD error handler to a
3356 function which will do nothing. We still want to call
3357 bfd_merge_private_bfd_data, since it may set up
3358 information which is needed in the output file. */
3359 if (! command_line
.warn_mismatch
)
3360 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3361 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3363 if (command_line
.warn_mismatch
)
3364 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3367 if (! command_line
.warn_mismatch
)
3368 bfd_set_error_handler (pfn
);
3373 /* Look through all the global common symbols and attach them to the
3374 correct section. The -sort-common command line switch may be used
3375 to roughly sort the entries by size. */
3380 if (link_info
.relocateable
3381 && ! command_line
.force_common_definition
)
3384 if (! config
.sort_common
)
3385 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3390 for (power
= 4; power
>= 0; power
--)
3391 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3396 /* Place one common symbol in the correct section. */
3399 lang_one_common (h
, info
)
3400 struct bfd_link_hash_entry
*h
;
3403 unsigned int power_of_two
;
3407 if (h
->type
!= bfd_link_hash_common
)
3411 power_of_two
= h
->u
.c
.p
->alignment_power
;
3413 if (config
.sort_common
3414 && power_of_two
< (unsigned int) *(int *) info
)
3417 section
= h
->u
.c
.p
->section
;
3419 /* Increase the size of the section. */
3420 section
->_cooked_size
= ALIGN_N (section
->_cooked_size
,
3421 (bfd_size_type
) (1 << power_of_two
));
3423 /* Adjust the alignment if necessary. */
3424 if (power_of_two
> section
->alignment_power
)
3425 section
->alignment_power
= power_of_two
;
3427 /* Change the symbol from common to defined. */
3428 h
->type
= bfd_link_hash_defined
;
3429 h
->u
.def
.section
= section
;
3430 h
->u
.def
.value
= section
->_cooked_size
;
3432 /* Increase the size of the section. */
3433 section
->_cooked_size
+= size
;
3435 /* Make sure the section is allocated in memory, and make sure that
3436 it is no longer a common section. */
3437 section
->flags
|= SEC_ALLOC
;
3438 section
->flags
&= ~ SEC_IS_COMMON
;
3440 if (config
.map_file
!= NULL
)
3442 static boolean header_printed
;
3447 if (! header_printed
)
3449 minfo (_("\nAllocating common symbols\n"));
3450 minfo (_("Common symbol size file\n\n"));
3451 header_printed
= true;
3454 name
= demangle (h
->root
.string
);
3456 len
= strlen (name
);
3471 if (size
<= 0xffffffff)
3472 sprintf (buf
, "%lx", (unsigned long) size
);
3474 sprintf_vma (buf
, size
);
3484 minfo ("%B\n", section
->owner
);
3491 run through the input files and ensure that every input
3492 section has somewhere to go. If one is found without
3493 a destination then create an input request and place it
3494 into the statement tree.
3498 lang_place_orphans ()
3500 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3504 for (s
= file
->the_bfd
->sections
;
3505 s
!= (asection
*) NULL
;
3508 if (s
->output_section
== (asection
*) NULL
)
3510 /* This section of the file is not attatched, root
3511 around for a sensible place for it to go */
3513 if (file
->just_syms_flag
)
3515 /* We are only retrieving symbol values from this
3516 file. We want the symbols to act as though the
3517 values in the file are absolute. */
3518 s
->output_section
= bfd_abs_section_ptr
;
3519 s
->output_offset
= s
->vma
;
3521 else if (strcmp (s
->name
, "COMMON") == 0)
3523 /* This is a lonely common section which must have
3524 come from an archive. We attach to the section
3525 with the wildcard. */
3526 if (! link_info
.relocateable
3527 || command_line
.force_common_definition
)
3529 if (default_common_section
== NULL
)
3532 /* This message happens when using the
3533 svr3.ifile linker script, so I have
3535 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3537 default_common_section
=
3538 lang_output_section_statement_lookup (".bss");
3541 wild_doit (&default_common_section
->children
, s
,
3542 default_common_section
, file
);
3545 else if (ldemul_place_orphan (file
, s
))
3549 lang_output_section_statement_type
*os
=
3550 lang_output_section_statement_lookup (s
->name
);
3552 wild_doit (&os
->children
, s
, os
, file
);
3561 lang_set_flags (ptr
, flags
, invert
)
3562 lang_memory_region_type
*ptr
;
3566 flagword
*ptr_flags
;
3568 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3574 *ptr_flags
|= SEC_ALLOC
;
3578 *ptr_flags
|= SEC_READONLY
;
3582 *ptr_flags
|= SEC_DATA
;
3586 *ptr_flags
|= SEC_CODE
;
3591 *ptr_flags
|= SEC_LOAD
;
3595 einfo (_("%P%F: invalid syntax in flags\n"));
3602 /* Call a function on each input file. This function will be called
3603 on an archive, but not on the elements. */
3606 lang_for_each_input_file (func
)
3607 void (*func
) PARAMS ((lang_input_statement_type
*));
3609 lang_input_statement_type
*f
;
3611 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3613 f
= (lang_input_statement_type
*) f
->next_real_file
)
3617 /* Call a function on each file. The function will be called on all
3618 the elements of an archive which are included in the link, but will
3619 not be called on the archive file itself. */
3622 lang_for_each_file (func
)
3623 void (*func
) PARAMS ((lang_input_statement_type
*));
3625 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3636 lang_for_each_input_section (func
)
3637 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
3639 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3643 for (s
= f
->the_bfd
->sections
;
3644 s
!= (asection
*) NULL
;
3647 func (f
->the_bfd
, s
);
3655 ldlang_add_file (entry
)
3656 lang_input_statement_type
* entry
;
3660 lang_statement_append (&file_chain
,
3661 (lang_statement_union_type
*) entry
,
3664 /* The BFD linker needs to have a list of all input BFDs involved in
3666 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3667 ASSERT (entry
->the_bfd
!= output_bfd
);
3668 for (pp
= &link_info
.input_bfds
;
3669 *pp
!= (bfd
*) NULL
;
3670 pp
= &(*pp
)->link_next
)
3672 *pp
= entry
->the_bfd
;
3673 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3674 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3676 /* Look through the sections and check for any which should not be
3677 included in the link. We need to do this now, so that we can
3678 notice when the backend linker tries to report multiple
3679 definition errors for symbols which are in sections we aren't
3680 going to link. FIXME: It might be better to entirely ignore
3681 symbols which are defined in sections which are going to be
3682 discarded. This would require modifying the backend linker for
3683 each backend which might set the SEC_LINK_ONCE flag. If we do
3684 this, we should probably handle SEC_EXCLUDE in the same way. */
3686 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3690 lang_add_output (name
, from_script
)
3694 /* Make -o on command line override OUTPUT in script. */
3695 if (had_output_filename
== false || !from_script
)
3697 output_filename
= name
;
3698 had_output_filename
= true;
3703 static lang_output_section_statement_type
*current_section
;
3715 for (l
= 0; l
< 32; l
++)
3717 if (i
>= (unsigned int) x
)
3726 lang_enter_output_section_statement (output_section_statement_name
,
3727 address_exp
, sectype
, block_value
,
3728 align
, subalign
, ebase
)
3729 const char *output_section_statement_name
;
3730 etree_type
* address_exp
;
3731 enum section_type sectype
;
3732 bfd_vma block_value
;
3734 etree_type
*subalign
;
3737 lang_output_section_statement_type
*os
;
3741 lang_output_section_statement_lookup (output_section_statement_name
);
3745 /* Add this statement to tree */
3746 /* add_statement(lang_output_section_statement_enum,
3747 output_section_statement);*/
3748 /* Make next things chain into subchain of this */
3750 if (os
->addr_tree
==
3751 (etree_type
*) NULL
)
3756 os
->sectype
= sectype
;
3757 if (sectype
!= noload_section
)
3758 os
->flags
= SEC_NO_FLAGS
;
3760 os
->flags
= SEC_NEVER_LOAD
;
3761 os
->block_value
= block_value
? block_value
: 1;
3762 stat_ptr
= &os
->children
;
3764 os
->subsection_alignment
= topower(
3765 exp_get_value_int(subalign
, -1,
3766 "subsection alignment",
3768 os
->section_alignment
= topower(
3769 exp_get_value_int(align
, -1,
3770 "section alignment", 0));
3772 os
->load_base
= ebase
;
3779 lang_output_statement_type
*new =
3780 new_stat (lang_output_statement
, stat_ptr
);
3782 new->name
= output_filename
;
3785 /* Reset the current counters in the regions */
3787 reset_memory_regions ()
3789 lang_memory_region_type
*p
= lang_memory_region_list
;
3791 for (p
= lang_memory_region_list
;
3792 p
!= (lang_memory_region_type
*) NULL
;
3795 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3796 p
->current
= p
->origin
;
3800 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3801 as needed. SECTION may be NULL, in which case it is a wild card. */
3804 gc_section_callback (ptr
, section
, file
, data
)
3805 lang_wild_statement_type
*ptr
;
3807 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
3808 void *data ATTRIBUTE_UNUSED
;
3810 /* If the wild pattern was marked KEEP, the member sections
3811 should be as well. */
3812 if (ptr
->keep_sections
)
3813 section
->flags
|= SEC_KEEP
;
3816 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3817 may be NULL, indicating that it is a wildcard. */
3820 lang_gc_wild (s
, section
, file
)
3821 lang_wild_statement_type
*s
;
3822 const char *section
;
3825 walk_wild (s
, section
, file
, gc_section_callback
, NULL
);
3828 /* Iterate over sections marking them against GC. */
3831 lang_gc_sections_1 (s
)
3832 lang_statement_union_type
* s
;
3834 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
3836 switch (s
->header
.type
)
3838 case lang_wild_statement_enum
:
3839 lang_gc_wild (&s
->wild_statement
,
3840 s
->wild_statement
.section_name
,
3841 s
->wild_statement
.filename
);
3843 case lang_constructors_statement_enum
:
3844 lang_gc_sections_1 (constructor_list
.head
);
3846 case lang_output_section_statement_enum
:
3847 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
3849 case lang_group_statement_enum
:
3850 lang_gc_sections_1 (s
->group_statement
.children
.head
);
3861 struct bfd_link_hash_entry
*h
;
3862 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
3864 /* Keep all sections so marked in the link script. */
3866 lang_gc_sections_1 (statement_list
.head
);
3868 /* Keep all sections containing symbols undefined on the command-line.
3869 Handle the entry symbol at the same time. */
3871 if (entry_symbol
!= NULL
)
3873 fake_list_start
.next
= ldlang_undef_chain_list_head
;
3874 fake_list_start
.name
= (char *) entry_symbol
;
3875 ulist
= &fake_list_start
;
3878 ulist
= ldlang_undef_chain_list_head
;
3880 for (; ulist
; ulist
= ulist
->next
)
3882 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
3883 false, false, false);
3885 if (h
!= (struct bfd_link_hash_entry
*) NULL
3886 && (h
->type
== bfd_link_hash_defined
3887 || h
->type
== bfd_link_hash_defweak
)
3888 && ! bfd_is_abs_section (h
->u
.def
.section
))
3890 h
->u
.def
.section
->flags
|= SEC_KEEP
;
3894 bfd_gc_sections (output_bfd
, &link_info
);
3900 lang_reasonable_defaults ();
3901 current_target
= default_target
;
3903 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
3905 ldemul_create_output_section_statements ();
3907 /* Add to the hash table all undefineds on the command line */
3908 lang_place_undefineds ();
3910 already_linked_table_init ();
3912 /* Create a bfd for each input file */
3913 current_target
= default_target
;
3914 open_input_bfds (statement_list
.head
, false);
3916 ldemul_after_open ();
3918 already_linked_table_free ();
3920 /* Make sure that we're not mixing architectures. We call this
3921 after all the input files have been opened, but before we do any
3922 other processing, so that any operations merge_private_bfd_data
3923 does on the output file will be known during the rest of the
3927 /* Handle .exports instead of a version script if we're told to do so. */
3928 if (command_line
.version_exports_section
)
3929 lang_do_version_exports_section ();
3931 /* Build all sets based on the information gathered from the input
3933 ldctor_build_sets ();
3935 /* Remove unreferenced sections if asked to. */
3936 if (command_line
.gc_sections
)
3937 lang_gc_sections ();
3939 /* Size up the common data */
3942 /* Run through the contours of the script and attach input sections
3943 to the correct output sections
3945 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
3946 (lang_output_section_statement_type
*) NULL
);
3949 /* Find any sections not attached explicitly and handle them */
3950 lang_place_orphans ();
3952 ldemul_before_allocation ();
3954 /* We must record the program headers before we try to fix the
3955 section positions, since they will affect SIZEOF_HEADERS. */
3956 lang_record_phdrs ();
3958 /* Now run around and relax if we can */
3959 if (command_line
.relax
)
3961 /* First time round is a trial run to get the 'worst case'
3962 addresses of the objects if there was no relaxing. */
3963 lang_size_sections (statement_list
.head
,
3965 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
3967 /* Keep relaxing until bfd_relax_section gives up. */
3970 reset_memory_regions ();
3972 relax_again
= false;
3974 /* Note: pe-dll.c does something like this also. If you find
3975 you need to change this code, you probably need to change
3976 pe-dll.c also. DJ */
3978 /* Do all the assignments with our current guesses as to
3980 lang_do_assignments (statement_list
.head
,
3982 (fill_type
) 0, (bfd_vma
) 0);
3984 /* Perform another relax pass - this time we know where the
3985 globals are, so can make better guess. */
3986 lang_size_sections (statement_list
.head
,
3988 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
3990 while (relax_again
);
3994 /* Size up the sections. */
3995 lang_size_sections (statement_list
.head
,
3997 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
4000 /* See if anything special should be done now we know how big
4002 ldemul_after_allocation ();
4004 /* Fix any .startof. or .sizeof. symbols. */
4005 lang_set_startof ();
4007 /* Do all the assignments, now that we know the final restingplaces
4008 of all the symbols */
4010 lang_do_assignments (statement_list
.head
,
4012 (fill_type
) 0, (bfd_vma
) 0);
4014 /* Make sure that the section addresses make sense. */
4015 if (! link_info
.relocateable
4016 && command_line
.check_section_addresses
)
4017 lang_check_section_addresses ();
4025 /* EXPORTED TO YACC */
4028 lang_add_wild (section_name
, sections_sorted
, filename
, filenames_sorted
,
4029 keep_sections
, exclude_filename
)
4030 const char *const section_name
;
4031 boolean sections_sorted
;
4032 const char *const filename
;
4033 boolean filenames_sorted
;
4034 boolean keep_sections
;
4035 const char *exclude_filename
;
4037 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
4040 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
4042 placed_commons
= true;
4044 if (filename
!= NULL
&& ! wildcardp (filename
))
4046 lang_has_input_file
= true;
4048 new->section_name
= section_name
;
4049 new->sections_sorted
= sections_sorted
;
4050 new->filename
= filename
;
4051 new->filenames_sorted
= filenames_sorted
;
4052 new->keep_sections
= keep_sections
;
4053 new->exclude_filename
= exclude_filename
;
4054 lang_list_init (&new->children
);
4058 lang_section_start (name
, address
)
4060 etree_type
* address
;
4062 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
4064 ad
->section_name
= name
;
4065 ad
->address
= address
;
4068 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4069 because of a -e argument on the command line, or zero if this is
4070 called by ENTRY in a linker script. Command line arguments take
4074 lang_add_entry (name
, cmdline
)
4078 if (entry_symbol
== NULL
4080 || ! entry_from_cmdline
)
4082 entry_symbol
= name
;
4083 entry_from_cmdline
= cmdline
;
4088 lang_add_target (name
)
4091 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4107 map_option_f
= true;
4118 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4125 lang_add_data (type
, exp
)
4127 union etree_union
*exp
;
4130 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4138 /* Create a new reloc statement. RELOC is the BFD relocation type to
4139 generate. HOWTO is the corresponding howto structure (we could
4140 look this up, but the caller has already done so). SECTION is the
4141 section to generate a reloc against, or NAME is the name of the
4142 symbol to generate a reloc against. Exactly one of SECTION and
4143 NAME must be NULL. ADDEND is an expression for the addend. */
4146 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4147 bfd_reloc_code_real_type reloc
;
4148 reloc_howto_type
*howto
;
4151 union etree_union
*addend
;
4153 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4157 p
->section
= section
;
4159 p
->addend_exp
= addend
;
4161 p
->addend_value
= 0;
4162 p
->output_section
= NULL
;
4166 lang_assignment_statement_type
*
4167 lang_add_assignment (exp
)
4170 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4178 lang_add_attribute (attribute
)
4179 enum statement_enum attribute
;
4181 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4188 if (startup_file
!= (char *) NULL
)
4190 einfo (_("%P%Fmultiple STARTUP files\n"));
4192 first_file
->filename
= name
;
4193 first_file
->local_sym_name
= name
;
4194 first_file
->real
= true;
4196 startup_file
= name
;
4203 lang_float_flag
= maybe
;
4207 lang_leave_output_section_statement (fill
, memspec
, phdrs
)
4209 const char *memspec
;
4210 struct lang_output_section_phdr_list
*phdrs
;
4212 current_section
->fill
= fill
;
4213 current_section
->region
= lang_memory_region_lookup (memspec
);
4214 current_section
->phdrs
= phdrs
;
4215 stat_ptr
= &statement_list
;
4219 Create an absolute symbol with the given name with the value of the
4220 address of first byte of the section named.
4222 If the symbol already exists, then do nothing.
4225 lang_abs_symbol_at_beginning_of (secname
, name
)
4226 const char *secname
;
4229 struct bfd_link_hash_entry
*h
;
4231 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4232 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4233 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4235 if (h
->type
== bfd_link_hash_new
4236 || h
->type
== bfd_link_hash_undefined
)
4240 h
->type
= bfd_link_hash_defined
;
4242 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4243 if (sec
== (asection
*) NULL
)
4246 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4248 h
->u
.def
.section
= bfd_abs_section_ptr
;
4253 Create an absolute symbol with the given name with the value of the
4254 address of the first byte after the end of the section named.
4256 If the symbol already exists, then do nothing.
4259 lang_abs_symbol_at_end_of (secname
, name
)
4260 const char *secname
;
4263 struct bfd_link_hash_entry
*h
;
4265 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4266 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4267 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4269 if (h
->type
== bfd_link_hash_new
4270 || h
->type
== bfd_link_hash_undefined
)
4274 h
->type
= bfd_link_hash_defined
;
4276 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4277 if (sec
== (asection
*) NULL
)
4280 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4281 + bfd_section_size (output_bfd
, sec
));
4283 h
->u
.def
.section
= bfd_abs_section_ptr
;
4288 lang_statement_append (list
, element
, field
)
4289 lang_statement_list_type
* list
;
4290 lang_statement_union_type
* element
;
4291 lang_statement_union_type
** field
;
4293 *(list
->tail
) = element
;
4297 /* Set the output format type. -oformat overrides scripts. */
4300 lang_add_output_format (format
, big
, little
, from_script
)
4306 if (output_target
== NULL
|| !from_script
)
4308 if (command_line
.endian
== ENDIAN_BIG
4311 else if (command_line
.endian
== ENDIAN_LITTLE
4315 output_target
= format
;
4319 /* Enter a group. This creates a new lang_group_statement, and sets
4320 stat_ptr to build new statements within the group. */
4325 lang_group_statement_type
*g
;
4327 g
= new_stat (lang_group_statement
, stat_ptr
);
4328 lang_list_init (&g
->children
);
4329 stat_ptr
= &g
->children
;
4332 /* Leave a group. This just resets stat_ptr to start writing to the
4333 regular list of statements again. Note that this will not work if
4334 groups can occur inside anything else which can adjust stat_ptr,
4335 but currently they can't. */
4340 stat_ptr
= &statement_list
;
4343 /* Add a new program header. This is called for each entry in a PHDRS
4344 command in a linker script. */
4347 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4355 struct lang_phdr
*n
, **pp
;
4357 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4360 n
->type
= exp_get_value_int (type
, 0, "program header type",
4361 lang_final_phase_enum
);
4362 n
->filehdr
= filehdr
;
4367 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4372 /* Record the program header information in the output BFD. FIXME: We
4373 should not be calling an ELF specific function here. */
4376 lang_record_phdrs ()
4380 struct lang_output_section_phdr_list
*last
;
4381 struct lang_phdr
*l
;
4382 lang_statement_union_type
*u
;
4385 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4387 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4394 for (u
= lang_output_section_statement
.head
;
4396 u
= u
->output_section_statement
.next
)
4398 lang_output_section_statement_type
*os
;
4399 struct lang_output_section_phdr_list
*pl
;
4401 os
= &u
->output_section_statement
;
4408 if (os
->sectype
== noload_section
4409 || os
->bfd_section
== NULL
4410 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4415 if (os
->bfd_section
== NULL
)
4418 for (; pl
!= NULL
; pl
= pl
->next
)
4420 if (strcmp (pl
->name
, l
->name
) == 0)
4425 secs
= ((asection
**)
4426 xrealloc (secs
, alc
* sizeof (asection
*)));
4428 secs
[c
] = os
->bfd_section
;
4435 if (l
->flags
== NULL
)
4438 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4439 lang_final_phase_enum
);
4444 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4445 lang_final_phase_enum
);
4447 if (! bfd_record_phdr (output_bfd
, l
->type
,
4448 l
->flags
== NULL
? false : true,
4450 l
->at
== NULL
? false : true,
4451 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4452 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4457 /* Make sure all the phdr assignments succeeded. */
4458 for (u
= lang_output_section_statement
.head
;
4460 u
= u
->output_section_statement
.next
)
4462 struct lang_output_section_phdr_list
*pl
;
4464 if (u
->output_section_statement
.bfd_section
== NULL
)
4467 for (pl
= u
->output_section_statement
.phdrs
;
4470 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4471 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4472 u
->output_section_statement
.name
, pl
->name
);
4476 /* Record a list of sections which may not be cross referenced. */
4479 lang_add_nocrossref (l
)
4480 struct lang_nocrossref
*l
;
4482 struct lang_nocrossrefs
*n
;
4484 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4485 n
->next
= nocrossref_list
;
4487 nocrossref_list
= n
;
4489 /* Set notice_all so that we get informed about all symbols. */
4490 link_info
.notice_all
= true;
4493 /* Overlay handling. We handle overlays with some static variables. */
4495 /* The overlay virtual address. */
4496 static etree_type
*overlay_vma
;
4498 /* The overlay load address. */
4499 static etree_type
*overlay_lma
;
4501 /* Whether nocrossrefs is set for this overlay. */
4502 static int overlay_nocrossrefs
;
4504 /* An expression for the maximum section size seen so far. */
4505 static etree_type
*overlay_max
;
4507 /* A list of all the sections in this overlay. */
4511 struct overlay_list
*next
;
4512 lang_output_section_statement_type
*os
;
4515 static struct overlay_list
*overlay_list
;
4517 /* Start handling an overlay. */
4520 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4521 etree_type
*vma_expr
;
4522 etree_type
*lma_expr
;
4525 /* The grammar should prevent nested overlays from occurring. */
4526 ASSERT (overlay_vma
== NULL
4527 && overlay_lma
== NULL
4528 && overlay_list
== NULL
4529 && overlay_max
== NULL
);
4531 overlay_vma
= vma_expr
;
4532 overlay_lma
= lma_expr
;
4533 overlay_nocrossrefs
= nocrossrefs
;
4536 /* Start a section in an overlay. We handle this by calling
4537 lang_enter_output_section_statement with the correct VMA and LMA. */
4540 lang_enter_overlay_section (name
)
4543 struct overlay_list
*n
;
4546 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4547 0, 0, 0, overlay_lma
);
4549 /* If this is the first section, then base the VMA and LMA of future
4550 sections on this one. This will work correctly even if `.' is
4551 used in the addresses. */
4552 if (overlay_list
== NULL
)
4554 overlay_vma
= exp_nameop (ADDR
, name
);
4555 overlay_lma
= exp_nameop (LOADADDR
, name
);
4558 /* Remember the section. */
4559 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4560 n
->os
= current_section
;
4561 n
->next
= overlay_list
;
4564 size
= exp_nameop (SIZEOF
, name
);
4566 /* Adjust the LMA for the next section. */
4567 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4569 /* Arrange to work out the maximum section end address. */
4570 if (overlay_max
== NULL
)
4573 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4576 /* Finish a section in an overlay. There isn't any special to do
4580 lang_leave_overlay_section (fill
, phdrs
)
4582 struct lang_output_section_phdr_list
*phdrs
;
4589 name
= current_section
->name
;
4591 lang_leave_output_section_statement (fill
, "*default*", phdrs
);
4593 /* Define the magic symbols. */
4595 clean
= xmalloc (strlen (name
) + 1);
4597 for (s1
= name
; *s1
!= '\0'; s1
++)
4598 if (isalnum ((unsigned char) *s1
) || *s1
== '_')
4602 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4603 sprintf (buf
, "__load_start_%s", clean
);
4604 lang_add_assignment (exp_assop ('=', buf
,
4605 exp_nameop (LOADADDR
, name
)));
4607 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4608 sprintf (buf
, "__load_stop_%s", clean
);
4609 lang_add_assignment (exp_assop ('=', buf
,
4611 exp_nameop (LOADADDR
, name
),
4612 exp_nameop (SIZEOF
, name
))));
4617 /* Finish an overlay. If there are any overlay wide settings, this
4618 looks through all the sections in the overlay and sets them. */
4621 lang_leave_overlay (fill
, memspec
, phdrs
)
4623 const char *memspec
;
4624 struct lang_output_section_phdr_list
*phdrs
;
4626 lang_memory_region_type
*region
;
4627 struct overlay_list
*l
;
4628 struct lang_nocrossref
*nocrossref
;
4630 if (memspec
== NULL
)
4633 region
= lang_memory_region_lookup (memspec
);
4640 struct overlay_list
*next
;
4642 if (fill
!= 0 && l
->os
->fill
== 0)
4644 if (region
!= NULL
&& l
->os
->region
== NULL
)
4645 l
->os
->region
= region
;
4646 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4647 l
->os
->phdrs
= phdrs
;
4649 if (overlay_nocrossrefs
)
4651 struct lang_nocrossref
*nc
;
4653 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4654 nc
->name
= l
->os
->name
;
4655 nc
->next
= nocrossref
;
4664 if (nocrossref
!= NULL
)
4665 lang_add_nocrossref (nocrossref
);
4667 /* Update . for the end of the overlay. */
4668 lang_add_assignment (exp_assop ('=', ".",
4669 exp_binop ('+', overlay_vma
, overlay_max
)));
4673 overlay_nocrossrefs
= 0;
4674 overlay_list
= NULL
;
4678 /* Version handling. This is only useful for ELF. */
4680 /* This global variable holds the version tree that we build. */
4682 struct bfd_elf_version_tree
*lang_elf_version_info
;
4685 lang_vers_match_lang_c (expr
, sym
)
4686 struct bfd_elf_version_expr
*expr
;
4689 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4691 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4695 lang_vers_match_lang_cplusplus (expr
, sym
)
4696 struct bfd_elf_version_expr
*expr
;
4702 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4705 alt_sym
= cplus_demangle(sym
, /* DMGL_NO_TPARAMS */ 0);
4708 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4709 Should we early out false in this case? */
4710 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4714 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4722 lang_vers_match_lang_java (expr
, sym
)
4723 struct bfd_elf_version_expr
*expr
;
4729 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4732 alt_sym
= cplus_demangle(sym
, DMGL_JAVA
);
4735 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4736 Should we early out false in this case? */
4737 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4741 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4748 /* This is called for each variable name or match expression. */
4750 struct bfd_elf_version_expr
*
4751 lang_new_vers_regex (orig
, new, lang
)
4752 struct bfd_elf_version_expr
*orig
;
4756 struct bfd_elf_version_expr
*ret
;
4758 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4762 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4763 ret
->match
= lang_vers_match_lang_c
;
4764 else if (strcasecmp (lang
, "C++") == 0)
4765 ret
->match
= lang_vers_match_lang_cplusplus
;
4766 else if (strcasecmp (lang
, "Java") == 0)
4767 ret
->match
= lang_vers_match_lang_java
;
4770 einfo (_("%X%P: unknown language `%s' in version information\n"),
4772 ret
->match
= lang_vers_match_lang_c
;
4778 /* This is called for each set of variable names and match
4781 struct bfd_elf_version_tree
*
4782 lang_new_vers_node (globals
, locals
)
4783 struct bfd_elf_version_expr
*globals
;
4784 struct bfd_elf_version_expr
*locals
;
4786 struct bfd_elf_version_tree
*ret
;
4788 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
4792 ret
->globals
= globals
;
4793 ret
->locals
= locals
;
4795 ret
->name_indx
= (unsigned int) -1;
4800 /* This static variable keeps track of version indices. */
4802 static int version_index
;
4804 /* This is called when we know the name and dependencies of the
4808 lang_register_vers_node (name
, version
, deps
)
4810 struct bfd_elf_version_tree
*version
;
4811 struct bfd_elf_version_deps
*deps
;
4813 struct bfd_elf_version_tree
*t
, **pp
;
4814 struct bfd_elf_version_expr
*e1
;
4816 /* Make sure this node has a unique name. */
4817 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4818 if (strcmp (t
->name
, name
) == 0)
4819 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
4821 /* Check the global and local match names, and make sure there
4822 aren't any duplicates. */
4824 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
4826 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4828 struct bfd_elf_version_expr
*e2
;
4830 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
4831 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4832 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4837 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
4839 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4841 struct bfd_elf_version_expr
*e2
;
4843 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
4844 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
4845 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4850 version
->deps
= deps
;
4851 version
->name
= name
;
4853 version
->vernum
= version_index
;
4855 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4860 /* This is called when we see a version dependency. */
4862 struct bfd_elf_version_deps
*
4863 lang_add_vers_depend (list
, name
)
4864 struct bfd_elf_version_deps
*list
;
4867 struct bfd_elf_version_deps
*ret
;
4868 struct bfd_elf_version_tree
*t
;
4870 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
4873 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
4875 if (strcmp (t
->name
, name
) == 0)
4877 ret
->version_needed
= t
;
4882 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
4888 lang_do_version_exports_section ()
4890 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
4892 LANG_FOR_EACH_INPUT_STATEMENT (is
)
4894 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
4901 len
= bfd_section_size (is
->the_bfd
, sec
);
4902 contents
= xmalloc (len
);
4903 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
4904 einfo (_("%X%P: unable to read .exports section contents"), sec
);
4907 while (p
< contents
+len
)
4909 greg
= lang_new_vers_regex (greg
, p
, NULL
);
4910 p
= strchr (p
, '\0') + 1;
4913 /* Do not free the contents, as we used them creating the regex. */
4915 /* Do not include this section in the link. */
4916 bfd_set_section_flags (is
->the_bfd
, sec
,
4917 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
4920 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
4921 lang_register_vers_node (command_line
.version_exports_section
,
4922 lang_new_vers_node (greg
, lreg
), NULL
);