1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "libiberty.h"
26 #include "safe-ctype.h"
44 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
47 /* Locals variables. */
48 static struct obstack stat_obstack
;
50 #define obstack_chunk_alloc xmalloc
51 #define obstack_chunk_free free
52 static const char *startup_file
;
53 static lang_statement_list_type input_file_chain
;
54 static bfd_boolean placed_commons
= FALSE
;
55 static lang_output_section_statement_type
*default_common_section
;
56 static bfd_boolean map_option_f
;
57 static bfd_vma print_dot
;
58 static lang_input_statement_type
*first_file
;
59 static const char *current_target
;
60 static const char *output_target
;
61 static lang_statement_list_type statement_list
;
62 static struct lang_phdr
*lang_phdr_list
;
64 /* Forward declarations. */
65 static lang_statement_union_type
*new_statement
66 PARAMS ((enum statement_enum
, size_t, lang_statement_list_type
*));
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*) (lang_statement_union_type
*),
69 lang_statement_union_type
*));
70 static lang_input_statement_type
*new_afile
71 PARAMS ((const char *, lang_input_file_enum_type
, const char *,
73 static lang_memory_region_type
*lang_memory_default
74 PARAMS ((asection
*));
75 static void lang_map_flags
78 PARAMS ((lang_output_section_statement_type
*));
79 static void exp_init_os
80 PARAMS ((etree_type
*));
81 static void section_already_linked
82 PARAMS ((bfd
*, asection
*, PTR
));
83 static struct bfd_hash_entry
*already_linked_newfunc
84 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
85 static void already_linked_table_init
87 static void already_linked_table_free
89 static bfd_boolean wildcardp
90 PARAMS ((const char *));
91 static lang_statement_union_type
*wild_sort
92 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*,
93 lang_input_statement_type
*, asection
*));
94 static void output_section_callback
95 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*, asection
*,
96 lang_input_statement_type
*, PTR
));
97 static lang_input_statement_type
*lookup_name
98 PARAMS ((const char *));
99 static bfd_boolean load_symbols
100 PARAMS ((lang_input_statement_type
*, lang_statement_list_type
*));
102 PARAMS ((lang_wild_statement_type
*,
103 const char *, lang_output_section_statement_type
*));
104 static bfd
*open_output
105 PARAMS ((const char *));
106 static void ldlang_open_output
107 PARAMS ((lang_statement_union_type
*));
108 static void open_input_bfds
109 PARAMS ((lang_statement_union_type
*, bfd_boolean
));
110 static void lang_reasonable_defaults
112 static void insert_undefined
113 PARAMS ((const char *));
114 static void lang_place_undefineds
116 static void map_input_to_output_sections
117 PARAMS ((lang_statement_union_type
*, const char *,
118 lang_output_section_statement_type
*));
119 static void strip_excluded_output_sections
121 static void print_output_section_statement
122 PARAMS ((lang_output_section_statement_type
*));
123 static void print_assignment
124 PARAMS ((lang_assignment_statement_type
*,
125 lang_output_section_statement_type
*));
126 static void print_input_statement
127 PARAMS ((lang_input_statement_type
*));
128 static bfd_boolean print_one_symbol
129 PARAMS ((struct bfd_link_hash_entry
*, PTR
));
130 static void print_input_section
131 PARAMS ((lang_input_section_type
*));
132 static void print_fill_statement
133 PARAMS ((lang_fill_statement_type
*));
134 static void print_data_statement
135 PARAMS ((lang_data_statement_type
*));
136 static void print_address_statement
137 PARAMS ((lang_address_statement_type
*));
138 static void print_reloc_statement
139 PARAMS ((lang_reloc_statement_type
*));
140 static void print_padding_statement
141 PARAMS ((lang_padding_statement_type
*));
142 static void print_wild_statement
143 PARAMS ((lang_wild_statement_type
*, lang_output_section_statement_type
*));
144 static void print_group
145 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
146 static void print_statement
147 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
148 static void print_statement_list
149 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
150 static void print_statements
152 static void insert_pad
153 PARAMS ((lang_statement_union_type
**, fill_type
*,
154 unsigned int, asection
*, bfd_vma
));
155 static bfd_vma size_input_section
156 PARAMS ((lang_statement_union_type
**, lang_output_section_statement_type
*,
157 fill_type
*, bfd_vma
));
158 static void lang_finish
160 static void ignore_bfd_errors
161 PARAMS ((const char *, ...));
162 static void lang_check
164 static void lang_common
166 static bfd_boolean lang_one_common
167 PARAMS ((struct bfd_link_hash_entry
*, PTR
));
168 static void lang_place_orphans
172 static void lang_set_startof
174 static void gc_section_callback
175 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*, asection
*,
176 lang_input_statement_type
*, PTR
));
177 static void lang_get_regions
178 PARAMS ((struct memory_region_struct
**, struct memory_region_struct
**,
179 const char *, const char *, int));
180 static void lang_record_phdrs
182 static void lang_gc_wild
183 PARAMS ((lang_wild_statement_type
*));
184 static void lang_gc_sections_1
185 PARAMS ((lang_statement_union_type
*));
186 static void lang_gc_sections
188 static int lang_vers_match_lang_c
189 PARAMS ((struct bfd_elf_version_expr
*, const char *));
190 static int lang_vers_match_lang_cplusplus
191 PARAMS ((struct bfd_elf_version_expr
*, const char *));
192 static int lang_vers_match_lang_java
193 PARAMS ((struct bfd_elf_version_expr
*, const char *));
194 static void lang_do_version_exports_section
196 static void lang_check_section_addresses
198 static void os_region_check
199 PARAMS ((lang_output_section_statement_type
*,
200 struct memory_region_struct
*, etree_type
*, bfd_vma
));
201 static bfd_vma lang_size_sections_1
202 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*,
203 lang_statement_union_type
**, fill_type
*, bfd_vma
, bfd_boolean
*,
205 typedef void (*callback_t
)
206 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*, asection
*,
207 lang_input_statement_type
*, PTR
));
208 static void walk_wild
209 PARAMS ((lang_wild_statement_type
*, callback_t
, PTR
));
210 static void walk_wild_section
211 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
213 static void walk_wild_file
214 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
216 static int get_target
217 PARAMS ((const bfd_target
*, PTR
));
219 PARAMS ((char *, char *));
221 PARAMS ((char *, char *));
222 static int name_compare
223 PARAMS ((char *, char *));
224 static int closest_target_match
225 PARAMS ((const bfd_target
*, PTR
));
226 static char * get_first_input_target
229 /* Exported variables. */
230 lang_output_section_statement_type
*abs_output_section
;
231 lang_statement_list_type lang_output_section_statement
;
232 lang_statement_list_type
*stat_ptr
= &statement_list
;
233 lang_statement_list_type file_chain
= { NULL
, NULL
};
234 struct bfd_sym_chain entry_symbol
= { NULL
, NULL
};
235 const char *entry_section
= ".text";
236 bfd_boolean entry_from_cmdline
;
237 bfd_boolean lang_has_input_file
= FALSE
;
238 bfd_boolean had_output_filename
= FALSE
;
239 bfd_boolean lang_float_flag
= FALSE
;
240 bfd_boolean delete_output_file_on_failure
= FALSE
;
241 struct lang_nocrossrefs
*nocrossref_list
;
242 struct unique_sections
*unique_section_list
;
243 static bfd_boolean ldlang_sysrooted_script
= FALSE
;
245 etree_type
*base
; /* Relocation base - or null */
247 #if defined (__STDC__) || defined (ALMOST_STDC)
248 #define cat(a,b) a##b
250 #define cat(a,b) a/**/b
253 /* Don't beautify the line below with "innocent" whitespace, it breaks
254 the K&R C preprocessor! */
255 #define new_stat(x, y) \
256 (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
258 #define outside_section_address(q) \
259 ((q)->output_offset + (q)->output_section->vma)
261 #define outside_symbol_address(q) \
262 ((q)->value + outside_section_address (q->section))
264 #define SECTION_NAME_MAP_LENGTH (16)
270 return obstack_alloc (&stat_obstack
, size
);
274 unique_section_p (secnam
)
277 struct unique_sections
*unam
;
279 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
280 if (wildcardp (unam
->name
)
281 ? fnmatch (unam
->name
, secnam
, 0) == 0
282 : strcmp (unam
->name
, secnam
) == 0)
290 /* Generic traversal routines for finding matching sections. */
293 walk_wild_section (ptr
, file
, callback
, data
)
294 lang_wild_statement_type
*ptr
;
295 lang_input_statement_type
*file
;
301 if (file
->just_syms_flag
)
304 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
306 struct wildcard_list
*sec
;
308 sec
= ptr
->section_list
;
310 (*callback
) (ptr
, sec
, s
, file
, data
);
314 bfd_boolean skip
= FALSE
;
315 struct name_list
*list_tmp
;
317 /* Don't process sections from files which were
319 for (list_tmp
= sec
->spec
.exclude_name_list
;
321 list_tmp
= list_tmp
->next
)
323 if (wildcardp (list_tmp
->name
))
324 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
326 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
328 /* If this file is part of an archive, and the archive is
329 excluded, exclude this file. */
330 if (! skip
&& file
->the_bfd
!= NULL
331 && file
->the_bfd
->my_archive
!= NULL
332 && file
->the_bfd
->my_archive
->filename
!= NULL
)
334 if (wildcardp (list_tmp
->name
))
335 skip
= fnmatch (list_tmp
->name
,
336 file
->the_bfd
->my_archive
->filename
,
339 skip
= strcmp (list_tmp
->name
,
340 file
->the_bfd
->my_archive
->filename
) == 0;
347 if (!skip
&& sec
->spec
.name
!= NULL
)
349 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
351 if (wildcardp (sec
->spec
.name
))
352 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
354 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
358 (*callback
) (ptr
, sec
, s
, file
, data
);
365 /* Handle a wild statement for a single file F. */
368 walk_wild_file (s
, f
, callback
, data
)
369 lang_wild_statement_type
*s
;
370 lang_input_statement_type
*f
;
374 if (f
->the_bfd
== NULL
375 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
376 walk_wild_section (s
, f
, callback
, data
);
381 /* This is an archive file. We must map each member of the
382 archive separately. */
383 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
384 while (member
!= NULL
)
386 /* When lookup_name is called, it will call the add_symbols
387 entry point for the archive. For each element of the
388 archive which is included, BFD will call ldlang_add_file,
389 which will set the usrdata field of the member to the
390 lang_input_statement. */
391 if (member
->usrdata
!= NULL
)
393 walk_wild_section (s
,
394 (lang_input_statement_type
*) member
->usrdata
,
398 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
404 walk_wild (s
, callback
, data
)
405 lang_wild_statement_type
*s
;
409 const char *file_spec
= s
->filename
;
411 if (file_spec
== NULL
)
413 /* Perform the iteration over all files in the list. */
414 LANG_FOR_EACH_INPUT_STATEMENT (f
)
416 walk_wild_file (s
, f
, callback
, data
);
419 else if (wildcardp (file_spec
))
421 LANG_FOR_EACH_INPUT_STATEMENT (f
)
423 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
424 walk_wild_file (s
, f
, callback
, data
);
429 lang_input_statement_type
*f
;
431 /* Perform the iteration over a single file. */
432 f
= lookup_name (file_spec
);
434 walk_wild_file (s
, f
, callback
, data
);
438 /* lang_for_each_statement walks the parse tree and calls the provided
439 function for each node. */
442 lang_for_each_statement_worker (func
, s
)
443 void (*func
) PARAMS ((lang_statement_union_type
*));
444 lang_statement_union_type
*s
;
446 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
450 switch (s
->header
.type
)
452 case lang_constructors_statement_enum
:
453 lang_for_each_statement_worker (func
, constructor_list
.head
);
455 case lang_output_section_statement_enum
:
456 lang_for_each_statement_worker
458 s
->output_section_statement
.children
.head
);
460 case lang_wild_statement_enum
:
461 lang_for_each_statement_worker
463 s
->wild_statement
.children
.head
);
465 case lang_group_statement_enum
:
466 lang_for_each_statement_worker (func
,
467 s
->group_statement
.children
.head
);
469 case lang_data_statement_enum
:
470 case lang_reloc_statement_enum
:
471 case lang_object_symbols_statement_enum
:
472 case lang_output_statement_enum
:
473 case lang_target_statement_enum
:
474 case lang_input_section_enum
:
475 case lang_input_statement_enum
:
476 case lang_assignment_statement_enum
:
477 case lang_padding_statement_enum
:
478 case lang_address_statement_enum
:
479 case lang_fill_statement_enum
:
489 lang_for_each_statement (func
)
490 void (*func
) PARAMS ((lang_statement_union_type
*));
492 lang_for_each_statement_worker (func
, statement_list
.head
);
495 /*----------------------------------------------------------------------*/
498 lang_list_init (list
)
499 lang_statement_list_type
*list
;
501 list
->head
= (lang_statement_union_type
*) NULL
;
502 list
->tail
= &list
->head
;
505 /* Build a new statement node for the parse tree. */
507 static lang_statement_union_type
*
508 new_statement (type
, size
, list
)
509 enum statement_enum type
;
511 lang_statement_list_type
*list
;
513 lang_statement_union_type
*new = (lang_statement_union_type
*)
516 new->header
.type
= type
;
517 new->header
.next
= (lang_statement_union_type
*) NULL
;
518 lang_statement_append (list
, new, &new->header
.next
);
522 /* Build a new input file node for the language. There are several
523 ways in which we treat an input file, eg, we only look at symbols,
524 or prefix it with a -l etc.
526 We can be supplied with requests for input files more than once;
527 they may, for example be split over several lines like foo.o(.text)
528 foo.o(.data) etc, so when asked for a file we check that we haven't
529 got it already so we don't duplicate the bfd. */
531 static lang_input_statement_type
*
532 new_afile (name
, file_type
, target
, add_to_list
)
534 lang_input_file_enum_type file_type
;
536 bfd_boolean add_to_list
;
538 lang_input_statement_type
*p
;
541 p
= new_stat (lang_input_statement
, stat_ptr
);
544 p
= ((lang_input_statement_type
*)
545 stat_alloc (sizeof (lang_input_statement_type
)));
546 p
->header
.next
= NULL
;
549 lang_has_input_file
= TRUE
;
551 p
->sysrooted
= FALSE
;
554 case lang_input_file_is_symbols_only_enum
:
556 p
->is_archive
= FALSE
;
558 p
->local_sym_name
= name
;
559 p
->just_syms_flag
= TRUE
;
560 p
->search_dirs_flag
= FALSE
;
562 case lang_input_file_is_fake_enum
:
564 p
->is_archive
= FALSE
;
566 p
->local_sym_name
= name
;
567 p
->just_syms_flag
= FALSE
;
568 p
->search_dirs_flag
= FALSE
;
570 case lang_input_file_is_l_enum
:
571 p
->is_archive
= TRUE
;
574 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
575 p
->just_syms_flag
= FALSE
;
576 p
->search_dirs_flag
= TRUE
;
578 case lang_input_file_is_marker_enum
:
580 p
->is_archive
= FALSE
;
582 p
->local_sym_name
= name
;
583 p
->just_syms_flag
= FALSE
;
584 p
->search_dirs_flag
= TRUE
;
586 case lang_input_file_is_search_file_enum
:
587 p
->sysrooted
= ldlang_sysrooted_script
;
589 p
->is_archive
= FALSE
;
591 p
->local_sym_name
= name
;
592 p
->just_syms_flag
= FALSE
;
593 p
->search_dirs_flag
= TRUE
;
595 case lang_input_file_is_file_enum
:
597 p
->is_archive
= FALSE
;
599 p
->local_sym_name
= name
;
600 p
->just_syms_flag
= FALSE
;
601 p
->search_dirs_flag
= FALSE
;
606 p
->the_bfd
= (bfd
*) NULL
;
607 p
->asymbols
= (asymbol
**) NULL
;
608 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
609 p
->next
= (lang_statement_union_type
*) NULL
;
611 p
->dynamic
= config
.dynamic_link
;
612 p
->whole_archive
= whole_archive
;
614 lang_statement_append (&input_file_chain
,
615 (lang_statement_union_type
*) p
,
620 lang_input_statement_type
*
621 lang_add_input_file (name
, file_type
, target
)
623 lang_input_file_enum_type file_type
;
626 lang_has_input_file
= TRUE
;
627 return new_afile (name
, file_type
, target
, TRUE
);
630 /* Build enough state so that the parser can build its tree. */
635 obstack_begin (&stat_obstack
, 1000);
637 stat_ptr
= &statement_list
;
639 lang_list_init (stat_ptr
);
641 lang_list_init (&input_file_chain
);
642 lang_list_init (&lang_output_section_statement
);
643 lang_list_init (&file_chain
);
644 first_file
= lang_add_input_file ((char *) NULL
,
645 lang_input_file_is_marker_enum
,
648 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
650 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
654 /*----------------------------------------------------------------------
655 A region is an area of memory declared with the
656 MEMORY { name:org=exp, len=exp ... }
659 We maintain a list of all the regions here.
661 If no regions are specified in the script, then the default is used
662 which is created when looked up to be the entire data space. */
664 static lang_memory_region_type
*lang_memory_region_list
;
665 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
667 lang_memory_region_type
*
668 lang_memory_region_lookup (name
)
669 const char *const name
;
671 lang_memory_region_type
*p
;
673 /* NAME is NULL for LMA memspecs if no region was specified. */
677 for (p
= lang_memory_region_list
;
678 p
!= (lang_memory_region_type
*) NULL
;
681 if (strcmp (p
->name
, name
) == 0)
688 /* This code used to always use the first region in the list as the
689 default region. I changed it to instead use a region
690 encompassing all of memory as the default region. This permits
691 NOLOAD sections to work reasonably without requiring a region.
692 People should specify what region they mean, if they really want
694 if (strcmp (name
, "*default*") == 0)
696 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
698 return lang_memory_region_list
;
704 lang_memory_region_type
*new =
705 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
707 new->name
= xstrdup (name
);
708 new->next
= (lang_memory_region_type
*) NULL
;
710 *lang_memory_region_list_tail
= new;
711 lang_memory_region_list_tail
= &new->next
;
715 new->length
= ~(bfd_size_type
) 0;
717 new->had_full_message
= FALSE
;
723 static lang_memory_region_type
*
724 lang_memory_default (section
)
727 lang_memory_region_type
*p
;
729 flagword sec_flags
= section
->flags
;
731 /* Override SEC_DATA to mean a writable section. */
732 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
733 sec_flags
|= SEC_DATA
;
735 for (p
= lang_memory_region_list
;
736 p
!= (lang_memory_region_type
*) NULL
;
739 if ((p
->flags
& sec_flags
) != 0
740 && (p
->not_flags
& sec_flags
) == 0)
745 return lang_memory_region_lookup ("*default*");
748 lang_output_section_statement_type
*
749 lang_output_section_find (name
)
750 const char *const name
;
752 lang_statement_union_type
*u
;
753 lang_output_section_statement_type
*lookup
;
755 for (u
= lang_output_section_statement
.head
;
756 u
!= (lang_statement_union_type
*) NULL
;
759 lookup
= &u
->output_section_statement
;
760 if (strcmp (name
, lookup
->name
) == 0)
765 return (lang_output_section_statement_type
*) NULL
;
768 lang_output_section_statement_type
*
769 lang_output_section_statement_lookup (name
)
770 const char *const name
;
772 lang_output_section_statement_type
*lookup
;
774 lookup
= lang_output_section_find (name
);
775 if (lookup
== (lang_output_section_statement_type
*) NULL
)
778 lookup
= (lang_output_section_statement_type
*)
779 new_stat (lang_output_section_statement
, stat_ptr
);
780 lookup
->region
= (lang_memory_region_type
*) NULL
;
781 lookup
->lma_region
= (lang_memory_region_type
*) NULL
;
782 lookup
->fill
= (fill_type
*) 0;
783 lookup
->block_value
= 1;
786 lookup
->next
= (lang_statement_union_type
*) NULL
;
787 lookup
->bfd_section
= (asection
*) NULL
;
788 lookup
->processed
= FALSE
;
789 lookup
->sectype
= normal_section
;
790 lookup
->addr_tree
= (etree_type
*) NULL
;
791 lang_list_init (&lookup
->children
);
793 lookup
->memspec
= (const char *) NULL
;
795 lookup
->subsection_alignment
= -1;
796 lookup
->section_alignment
= -1;
797 lookup
->load_base
= (union etree_union
*) NULL
;
798 lookup
->update_dot_tree
= NULL
;
799 lookup
->phdrs
= NULL
;
801 lang_statement_append (&lang_output_section_statement
,
802 (lang_statement_union_type
*) lookup
,
809 lang_map_flags (flag
)
812 if (flag
& SEC_ALLOC
)
818 if (flag
& SEC_READONLY
)
831 lang_memory_region_type
*m
;
833 minfo (_("\nMemory Configuration\n\n"));
834 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
835 _("Name"), _("Origin"), _("Length"), _("Attributes"));
837 for (m
= lang_memory_region_list
;
838 m
!= (lang_memory_region_type
*) NULL
;
844 fprintf (config
.map_file
, "%-16s ", m
->name
);
846 sprintf_vma (buf
, m
->origin
);
847 minfo ("0x%s ", buf
);
855 minfo ("0x%V", m
->length
);
856 if (m
->flags
|| m
->not_flags
)
864 lang_map_flags (m
->flags
);
870 lang_map_flags (m
->not_flags
);
877 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
882 /* Initialize an output section. */
886 lang_output_section_statement_type
*s
;
888 section_userdata_type
*new;
890 if (s
->bfd_section
!= NULL
)
893 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
894 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
896 new = ((section_userdata_type
*)
897 stat_alloc (sizeof (section_userdata_type
)));
899 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
900 if (s
->bfd_section
== (asection
*) NULL
)
901 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
902 if (s
->bfd_section
== (asection
*) NULL
)
904 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
905 output_bfd
->xvec
->name
, s
->name
);
907 s
->bfd_section
->output_section
= s
->bfd_section
;
909 /* We initialize an output sections output offset to minus its own
910 vma to allow us to output a section through itself. */
911 s
->bfd_section
->output_offset
= 0;
912 get_userdata (s
->bfd_section
) = (PTR
) new;
914 /* If there is a base address, make sure that any sections it might
915 mention are initialized. */
916 if (s
->addr_tree
!= NULL
)
917 exp_init_os (s
->addr_tree
);
919 if (s
->load_base
!= NULL
)
920 exp_init_os (s
->load_base
);
923 /* Make sure that all output sections mentioned in an expression are
930 switch (exp
->type
.node_class
)
933 exp_init_os (exp
->assign
.src
);
937 exp_init_os (exp
->binary
.lhs
);
938 exp_init_os (exp
->binary
.rhs
);
942 exp_init_os (exp
->trinary
.cond
);
943 exp_init_os (exp
->trinary
.lhs
);
944 exp_init_os (exp
->trinary
.rhs
);
948 exp_init_os (exp
->unary
.child
);
952 switch (exp
->type
.node_code
)
958 lang_output_section_statement_type
*os
;
960 os
= lang_output_section_find (exp
->name
.name
);
961 if (os
!= NULL
&& os
->bfd_section
== NULL
)
972 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
973 once into the output. This routine checks each section, and
974 arrange to discard it if a section of the same name has already
975 been linked. If the section has COMDAT information, then it uses
976 that to decide whether the section should be included. This code
977 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
978 that is, it does not depend solely upon the section name.
979 section_already_linked is called via bfd_map_over_sections. */
981 /* This is the shape of the elements inside the already_linked hash
982 table. It maps a name onto a list of already_linked elements with
983 the same name. It's possible to get more than one element in a
984 list if the COMDAT sections have different names. */
986 struct already_linked_hash_entry
988 struct bfd_hash_entry root
;
989 struct already_linked
*entry
;
992 struct already_linked
994 struct already_linked
*next
;
998 /* The hash table. */
1000 static struct bfd_hash_table already_linked_table
;
1003 section_already_linked (abfd
, sec
, data
)
1008 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
1011 struct already_linked
*l
;
1012 struct already_linked_hash_entry
*already_linked_list
;
1014 /* If we are only reading symbols from this object, then we want to
1015 discard all sections. */
1016 if (entry
->just_syms_flag
)
1018 bfd_link_just_syms (sec
, &link_info
);
1022 flags
= bfd_get_section_flags (abfd
, sec
);
1024 if ((flags
& SEC_LINK_ONCE
) == 0)
1027 /* FIXME: When doing a relocatable link, we may have trouble
1028 copying relocations in other sections that refer to local symbols
1029 in the section being discarded. Those relocations will have to
1030 be converted somehow; as of this writing I'm not sure that any of
1031 the backends handle that correctly.
1033 It is tempting to instead not discard link once sections when
1034 doing a relocatable link (technically, they should be discarded
1035 whenever we are building constructors). However, that fails,
1036 because the linker winds up combining all the link once sections
1037 into a single large link once section, which defeats the purpose
1038 of having link once sections in the first place.
1040 Also, not merging link once sections in a relocatable link
1041 causes trouble for MIPS ELF, which relies on link once semantics
1042 to handle the .reginfo section correctly. */
1044 name
= bfd_get_section_name (abfd
, sec
);
1046 already_linked_list
=
1047 ((struct already_linked_hash_entry
*)
1048 bfd_hash_lookup (&already_linked_table
, name
, TRUE
, FALSE
));
1050 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
1052 if (sec
->comdat
== NULL
1053 || l
->sec
->comdat
== NULL
1054 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
1056 /* The section has already been linked. See if we should
1058 switch (flags
& SEC_LINK_DUPLICATES
)
1063 case SEC_LINK_DUPLICATES_DISCARD
:
1066 case SEC_LINK_DUPLICATES_ONE_ONLY
:
1067 if (sec
->comdat
== NULL
)
1068 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
1071 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
1072 abfd
, name
, sec
->comdat
->name
);
1075 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
1076 /* FIXME: We should really dig out the contents of both
1077 sections and memcmp them. The COFF/PE spec says that
1078 the Microsoft linker does not implement this
1079 correctly, so I'm not going to bother doing it
1082 case SEC_LINK_DUPLICATES_SAME_SIZE
:
1083 if (bfd_section_size (abfd
, sec
)
1084 != bfd_section_size (l
->sec
->owner
, l
->sec
))
1085 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
1090 /* Set the output_section field so that lang_add_section
1091 does not create a lang_input_section structure for this
1092 section. Since there might be a symbol in the section
1093 being discarded, we must retain a pointer to the section
1094 which we are really going to use. */
1095 sec
->output_section
= bfd_abs_section_ptr
;
1096 sec
->kept_section
= l
->sec
;
1098 if (flags
& SEC_GROUP
)
1099 bfd_discard_group (abfd
, sec
);
1105 /* This is the first section with this name. Record it. Allocate
1106 the memory from the same obstack as the hash table is kept in. */
1108 l
= ((struct already_linked
*)
1109 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1112 l
->next
= already_linked_list
->entry
;
1113 already_linked_list
->entry
= l
;
1116 /* Support routines for the hash table used by section_already_linked,
1117 initialize the table, fill in an entry and remove the table. */
1119 static struct bfd_hash_entry
*
1120 already_linked_newfunc (entry
, table
, string
)
1121 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1122 struct bfd_hash_table
*table
;
1123 const char *string ATTRIBUTE_UNUSED
;
1125 struct already_linked_hash_entry
*ret
=
1126 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1130 return (struct bfd_hash_entry
*) ret
;
1134 already_linked_table_init ()
1136 if (! bfd_hash_table_init_n (&already_linked_table
,
1137 already_linked_newfunc
,
1139 einfo (_("%P%F: Failed to create hash table\n"));
1143 already_linked_table_free ()
1145 bfd_hash_table_free (&already_linked_table
);
1148 /* The wild routines.
1150 These expand statements like *(.text) and foo.o to a list of
1151 explicit actions, like foo.o(.text), bar.o(.text) and
1152 foo.o(.text, .data). */
1154 /* Return TRUE if the PATTERN argument is a wildcard pattern.
1155 Although backslashes are treated specially if a pattern contains
1156 wildcards, we do not consider the mere presence of a backslash to
1157 be enough to cause the pattern to be treated as a wildcard.
1158 That lets us handle DOS filenames more naturally. */
1162 const char *pattern
;
1166 for (s
= pattern
; *s
!= '\0'; ++s
)
1174 /* Add SECTION to the output section OUTPUT. Do this by creating a
1175 lang_input_section statement which is placed at PTR. FILE is the
1176 input file which holds SECTION. */
1179 lang_add_section (ptr
, section
, output
, file
)
1180 lang_statement_list_type
*ptr
;
1182 lang_output_section_statement_type
*output
;
1183 lang_input_statement_type
*file
;
1186 bfd_boolean discard
;
1188 flags
= bfd_get_section_flags (section
->owner
, section
);
1192 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1193 link. Discard debugging sections marked with SEC_EXCLUDE on a
1194 relocatable link too. */
1195 if ((flags
& SEC_EXCLUDE
) != 0
1196 && ((flags
& SEC_DEBUGGING
) != 0 || !link_info
.relocateable
))
1199 /* Discard input sections which are assigned to a section named
1200 DISCARD_SECTION_NAME. */
1201 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1204 /* Discard debugging sections if we are stripping debugging
1206 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1207 && (flags
& SEC_DEBUGGING
) != 0)
1212 if (section
->output_section
== NULL
)
1214 /* This prevents future calls from assigning this section. */
1215 section
->output_section
= bfd_abs_section_ptr
;
1220 if (section
->output_section
== NULL
)
1223 lang_input_section_type
*new;
1226 if (output
->bfd_section
== NULL
)
1229 first
= ! output
->bfd_section
->linker_has_input
;
1230 output
->bfd_section
->linker_has_input
= 1;
1232 /* Add a section reference to the list. */
1233 new = new_stat (lang_input_section
, ptr
);
1235 new->section
= section
;
1237 section
->output_section
= output
->bfd_section
;
1239 flags
= section
->flags
;
1241 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1242 to an output section, because we want to be able to include a
1243 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1244 section (I don't know why we want to do this, but we do).
1245 build_link_order in ldwrite.c handles this case by turning
1246 the embedded SEC_NEVER_LOAD section into a fill. */
1248 flags
&= ~ SEC_NEVER_LOAD
;
1250 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1251 already been processed. One reason to do this is that on pe
1252 format targets, .text$foo sections go into .text and it's odd
1253 to see .text with SEC_LINK_ONCE set. */
1255 if (! link_info
.relocateable
)
1256 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1258 /* If this is not the first input section, and the SEC_READONLY
1259 flag is not currently set, then don't set it just because the
1260 input section has it set. */
1262 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1263 flags
&= ~ SEC_READONLY
;
1265 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1267 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1268 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1269 || ((flags
& SEC_MERGE
)
1270 && section
->output_section
->entsize
!= section
->entsize
)))
1272 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1273 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1276 /* For now make .tbss normal section. */
1277 if ((flags
& SEC_THREAD_LOCAL
) && ! link_info
.relocateable
)
1280 section
->output_section
->flags
|= flags
;
1282 if (flags
& SEC_MERGE
)
1283 section
->output_section
->entsize
= section
->entsize
;
1285 /* If SEC_READONLY is not set in the input section, then clear
1286 it from the output section. */
1287 if ((section
->flags
& SEC_READONLY
) == 0)
1288 section
->output_section
->flags
&= ~SEC_READONLY
;
1290 switch (output
->sectype
)
1292 case normal_section
:
1297 case overlay_section
:
1298 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1300 case noload_section
:
1301 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1302 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1306 /* Copy over SEC_SMALL_DATA. */
1307 if (section
->flags
& SEC_SMALL_DATA
)
1308 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1310 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1311 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1313 /* If supplied an alignment, then force it. */
1314 if (output
->section_alignment
!= -1)
1315 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1317 if (section
->flags
& SEC_BLOCK
)
1319 section
->output_section
->flags
|= SEC_BLOCK
;
1320 /* FIXME: This value should really be obtained from the bfd... */
1321 output
->block_value
= 128;
1326 /* Handle wildcard sorting. This returns the lang_input_section which
1327 should follow the one we are going to create for SECTION and FILE,
1328 based on the sorting requirements of WILD. It returns NULL if the
1329 new section should just go at the end of the current list. */
1331 static lang_statement_union_type
*
1332 wild_sort (wild
, sec
, file
, section
)
1333 lang_wild_statement_type
*wild
;
1334 struct wildcard_list
*sec
;
1335 lang_input_statement_type
*file
;
1338 const char *section_name
;
1339 lang_statement_union_type
*l
;
1341 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1344 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1345 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1347 lang_input_section_type
*ls
;
1349 if (l
->header
.type
!= lang_input_section_enum
)
1351 ls
= &l
->input_section
;
1353 /* Sorting by filename takes precedence over sorting by section
1356 if (wild
->filenames_sorted
)
1358 const char *fn
, *ln
;
1362 /* The PE support for the .idata section as generated by
1363 dlltool assumes that files will be sorted by the name of
1364 the archive and then the name of the file within the
1367 if (file
->the_bfd
!= NULL
1368 && bfd_my_archive (file
->the_bfd
) != NULL
)
1370 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1375 fn
= file
->filename
;
1379 if (ls
->ifile
->the_bfd
!= NULL
1380 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1382 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1387 ln
= ls
->ifile
->filename
;
1391 i
= strcmp (fn
, ln
);
1400 fn
= file
->filename
;
1402 ln
= ls
->ifile
->filename
;
1404 i
= strcmp (fn
, ln
);
1412 /* Here either the files are not sorted by name, or we are
1413 looking at the sections for this file. */
1415 if (sec
!= NULL
&& sec
->spec
.sorted
)
1417 if (strcmp (section_name
,
1418 bfd_get_section_name (ls
->ifile
->the_bfd
,
1428 /* Expand a wild statement for a particular FILE. SECTION may be
1429 NULL, in which case it is a wild card. */
1432 output_section_callback (ptr
, sec
, section
, file
, output
)
1433 lang_wild_statement_type
*ptr
;
1434 struct wildcard_list
*sec
;
1436 lang_input_statement_type
*file
;
1439 lang_statement_union_type
*before
;
1441 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1442 if (unique_section_p (bfd_get_section_name (file
->the_bfd
, section
)))
1445 /* If the wild pattern was marked KEEP, the member sections
1446 should be as well. */
1447 if (ptr
->keep_sections
)
1448 section
->flags
|= SEC_KEEP
;
1450 before
= wild_sort (ptr
, sec
, file
, section
);
1452 /* Here BEFORE points to the lang_input_section which
1453 should follow the one we are about to add. If BEFORE
1454 is NULL, then the section should just go at the end
1455 of the current list. */
1458 lang_add_section (&ptr
->children
, section
,
1459 (lang_output_section_statement_type
*) output
,
1463 lang_statement_list_type list
;
1464 lang_statement_union_type
**pp
;
1466 lang_list_init (&list
);
1467 lang_add_section (&list
, section
,
1468 (lang_output_section_statement_type
*) output
,
1471 /* If we are discarding the section, LIST.HEAD will
1473 if (list
.head
!= NULL
)
1475 ASSERT (list
.head
->header
.next
== NULL
);
1477 for (pp
= &ptr
->children
.head
;
1479 pp
= &(*pp
)->header
.next
)
1480 ASSERT (*pp
!= NULL
);
1482 list
.head
->header
.next
= *pp
;
1488 /* This is passed a file name which must have been seen already and
1489 added to the statement tree. We will see if it has been opened
1490 already and had its symbols read. If not then we'll read it. */
1492 static lang_input_statement_type
*
1496 lang_input_statement_type
*search
;
1498 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1499 search
!= (lang_input_statement_type
*) NULL
;
1500 search
= (lang_input_statement_type
*) search
->next_real_file
)
1502 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1504 if (search
->filename
!= (char *) NULL
1505 && name
!= (char *) NULL
1506 && strcmp (search
->filename
, name
) == 0)
1510 if (search
== (lang_input_statement_type
*) NULL
)
1511 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1514 /* If we have already added this file, or this file is not real
1515 (FIXME: can that ever actually happen?) or the name is NULL
1516 (FIXME: can that ever actually happen?) don't add this file. */
1519 || search
->filename
== (const char *) NULL
)
1522 if (! load_symbols (search
, (lang_statement_list_type
*) NULL
))
1528 /* Get the symbols for an input file. */
1531 load_symbols (entry
, place
)
1532 lang_input_statement_type
*entry
;
1533 lang_statement_list_type
*place
;
1540 ldfile_open_file (entry
);
1542 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1543 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1546 lang_statement_list_type
*hold
;
1547 bfd_boolean bad_load
= TRUE
;
1548 bfd_boolean save_ldlang_sysrooted_script
;
1550 err
= bfd_get_error ();
1552 /* See if the emulation has some special knowledge. */
1553 if (ldemul_unrecognized_file (entry
))
1556 if (err
== bfd_error_file_ambiguously_recognized
)
1560 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1561 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1562 for (p
= matching
; *p
!= NULL
; p
++)
1566 else if (err
!= bfd_error_file_not_recognized
1568 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1572 bfd_close (entry
->the_bfd
);
1573 entry
->the_bfd
= NULL
;
1575 /* Try to interpret the file as a linker script. */
1576 ldfile_open_command_file (entry
->filename
);
1580 save_ldlang_sysrooted_script
= ldlang_sysrooted_script
;
1581 ldlang_sysrooted_script
= entry
->sysrooted
;
1583 ldfile_assumed_script
= TRUE
;
1584 parser_input
= input_script
;
1586 ldfile_assumed_script
= FALSE
;
1588 ldlang_sysrooted_script
= save_ldlang_sysrooted_script
;
1594 if (ldemul_recognized_file (entry
))
1597 /* We don't call ldlang_add_file for an archive. Instead, the
1598 add_symbols entry point will call ldlang_add_file, via the
1599 add_archive_element callback, for each element of the archive
1601 switch (bfd_get_format (entry
->the_bfd
))
1607 ldlang_add_file (entry
);
1608 if (trace_files
|| trace_file_tries
)
1609 info_msg ("%I\n", entry
);
1613 if (entry
->whole_archive
)
1616 bfd_boolean loaded
= TRUE
;
1620 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1625 if (! bfd_check_format (member
, bfd_object
))
1627 einfo (_("%F%B: member %B in archive is not an object\n"),
1628 entry
->the_bfd
, member
);
1632 if (! ((*link_info
.callbacks
->add_archive_element
)
1633 (&link_info
, member
, "--whole-archive")))
1636 if (! bfd_link_add_symbols (member
, &link_info
))
1638 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1643 entry
->loaded
= loaded
;
1649 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1650 entry
->loaded
= TRUE
;
1652 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1654 return entry
->loaded
;
1657 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1658 may be NULL, indicating that it is a wildcard. Separate
1659 lang_input_section statements are created for each part of the
1660 expansion; they are added after the wild statement S. OUTPUT is
1661 the output section. */
1664 wild (s
, target
, output
)
1665 lang_wild_statement_type
*s
;
1666 const char *target ATTRIBUTE_UNUSED
;
1667 lang_output_section_statement_type
*output
;
1669 struct wildcard_list
*sec
;
1671 walk_wild (s
, output_section_callback
, (PTR
) output
);
1673 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1675 if (default_common_section
!= NULL
)
1677 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1679 /* Remember the section that common is going to in case we
1680 later get something which doesn't know where to put it. */
1681 default_common_section
= output
;
1686 /* Return TRUE iff target is the sought target. */
1689 get_target (target
, data
)
1690 const bfd_target
*target
;
1693 const char *sought
= (const char *) data
;
1695 return strcmp (target
->name
, sought
) == 0;
1698 /* Like strcpy() but convert to lower case as well. */
1707 while ((c
= *src
++) != 0)
1708 *dest
++ = TOLOWER (c
);
1713 /* Remove the first occurrence of needle (if any) in haystack
1717 strcut (haystack
, needle
)
1721 haystack
= strstr (haystack
, needle
);
1727 for (src
= haystack
+ strlen (needle
); *src
;)
1728 *haystack
++ = *src
++;
1734 /* Compare two target format name strings.
1735 Return a value indicating how "similar" they are. */
1738 name_compare (first
, second
)
1746 copy1
= xmalloc (strlen (first
) + 1);
1747 copy2
= xmalloc (strlen (second
) + 1);
1749 /* Convert the names to lower case. */
1750 stricpy (copy1
, first
);
1751 stricpy (copy2
, second
);
1753 /* Remove and endian strings from the name. */
1754 strcut (copy1
, "big");
1755 strcut (copy1
, "little");
1756 strcut (copy2
, "big");
1757 strcut (copy2
, "little");
1759 /* Return a value based on how many characters match,
1760 starting from the beginning. If both strings are
1761 the same then return 10 * their length. */
1762 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1763 if (copy1
[result
] == 0)
1775 /* Set by closest_target_match() below. */
1776 static const bfd_target
*winner
;
1778 /* Scan all the valid bfd targets looking for one that has the endianness
1779 requirement that was specified on the command line, and is the nearest
1780 match to the original output target. */
1783 closest_target_match (target
, data
)
1784 const bfd_target
*target
;
1787 const bfd_target
*original
= (const bfd_target
*) data
;
1789 if (command_line
.endian
== ENDIAN_BIG
1790 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1793 if (command_line
.endian
== ENDIAN_LITTLE
1794 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1797 /* Must be the same flavour. */
1798 if (target
->flavour
!= original
->flavour
)
1801 /* If we have not found a potential winner yet, then record this one. */
1808 /* Oh dear, we now have two potential candidates for a successful match.
1809 Compare their names and choose the better one. */
1810 if (name_compare (target
->name
, original
->name
)
1811 > name_compare (winner
->name
, original
->name
))
1814 /* Keep on searching until wqe have checked them all. */
1818 /* Return the BFD target format of the first input file. */
1821 get_first_input_target ()
1823 char *target
= NULL
;
1825 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1827 if (s
->header
.type
== lang_input_statement_enum
1830 ldfile_open_file (s
);
1832 if (s
->the_bfd
!= NULL
1833 && bfd_check_format (s
->the_bfd
, bfd_object
))
1835 target
= bfd_get_target (s
->the_bfd
);
1847 lang_get_output_target ()
1851 /* Has the user told us which output format to use? */
1852 if (output_target
!= (char *) NULL
)
1853 return output_target
;
1855 /* No - has the current target been set to something other than
1857 if (current_target
!= default_target
)
1858 return current_target
;
1860 /* No - can we determine the format of the first input file? */
1861 target
= get_first_input_target ();
1865 /* Failed - use the default output target. */
1866 return default_target
;
1869 /* Open the output file. */
1877 output_target
= lang_get_output_target ();
1879 /* Has the user requested a particular endianness on the command
1881 if (command_line
.endian
!= ENDIAN_UNSET
)
1883 const bfd_target
*target
;
1884 enum bfd_endian desired_endian
;
1886 /* Get the chosen target. */
1887 target
= bfd_search_for_target (get_target
, (PTR
) output_target
);
1889 /* If the target is not supported, we cannot do anything. */
1892 if (command_line
.endian
== ENDIAN_BIG
)
1893 desired_endian
= BFD_ENDIAN_BIG
;
1895 desired_endian
= BFD_ENDIAN_LITTLE
;
1897 /* See if the target has the wrong endianness. This should
1898 not happen if the linker script has provided big and
1899 little endian alternatives, but some scrips don't do
1901 if (target
->byteorder
!= desired_endian
)
1903 /* If it does, then see if the target provides
1904 an alternative with the correct endianness. */
1905 if (target
->alternative_target
!= NULL
1906 && (target
->alternative_target
->byteorder
== desired_endian
))
1907 output_target
= target
->alternative_target
->name
;
1910 /* Try to find a target as similar as possible to
1911 the default target, but which has the desired
1912 endian characteristic. */
1913 (void) bfd_search_for_target (closest_target_match
,
1916 /* Oh dear - we could not find any targets that
1917 satisfy our requirements. */
1919 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1921 output_target
= winner
->name
;
1927 output
= bfd_openw (name
, output_target
);
1929 if (output
== (bfd
*) NULL
)
1931 if (bfd_get_error () == bfd_error_invalid_target
)
1932 einfo (_("%P%F: target %s not found\n"), output_target
);
1934 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1937 delete_output_file_on_failure
= TRUE
;
1940 output
->flags
|= D_PAGED
;
1943 if (! bfd_set_format (output
, bfd_object
))
1944 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1945 if (! bfd_set_arch_mach (output
,
1946 ldfile_output_architecture
,
1947 ldfile_output_machine
))
1948 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1950 link_info
.hash
= bfd_link_hash_table_create (output
);
1951 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1952 einfo (_("%P%F: can not create link hash table: %E\n"));
1954 bfd_set_gp_size (output
, g_switch_value
);
1959 ldlang_open_output (statement
)
1960 lang_statement_union_type
*statement
;
1962 switch (statement
->header
.type
)
1964 case lang_output_statement_enum
:
1965 ASSERT (output_bfd
== (bfd
*) NULL
);
1966 output_bfd
= open_output (statement
->output_statement
.name
);
1967 ldemul_set_output_arch ();
1968 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1969 output_bfd
->flags
|= D_PAGED
;
1971 output_bfd
->flags
&= ~D_PAGED
;
1972 if (config
.text_read_only
)
1973 output_bfd
->flags
|= WP_TEXT
;
1975 output_bfd
->flags
&= ~WP_TEXT
;
1976 if (link_info
.traditional_format
)
1977 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1979 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1982 case lang_target_statement_enum
:
1983 current_target
= statement
->target_statement
.target
;
1990 /* Open all the input files. */
1993 open_input_bfds (s
, force
)
1994 lang_statement_union_type
*s
;
1997 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
1999 switch (s
->header
.type
)
2001 case lang_constructors_statement_enum
:
2002 open_input_bfds (constructor_list
.head
, force
);
2004 case lang_output_section_statement_enum
:
2005 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
2007 case lang_wild_statement_enum
:
2008 /* Maybe we should load the file's symbols. */
2009 if (s
->wild_statement
.filename
2010 && ! wildcardp (s
->wild_statement
.filename
))
2011 (void) lookup_name (s
->wild_statement
.filename
);
2012 open_input_bfds (s
->wild_statement
.children
.head
, force
);
2014 case lang_group_statement_enum
:
2016 struct bfd_link_hash_entry
*undefs
;
2018 /* We must continually search the entries in the group
2019 until no new symbols are added to the list of undefined
2024 undefs
= link_info
.hash
->undefs_tail
;
2025 open_input_bfds (s
->group_statement
.children
.head
, TRUE
);
2027 while (undefs
!= link_info
.hash
->undefs_tail
);
2030 case lang_target_statement_enum
:
2031 current_target
= s
->target_statement
.target
;
2033 case lang_input_statement_enum
:
2034 if (s
->input_statement
.real
)
2036 lang_statement_list_type add
;
2038 s
->input_statement
.target
= current_target
;
2040 /* If we are being called from within a group, and this
2041 is an archive which has already been searched, then
2042 force it to be researched unless the whole archive
2043 has been loaded already. */
2045 && !s
->input_statement
.whole_archive
2046 && s
->input_statement
.loaded
2047 && bfd_check_format (s
->input_statement
.the_bfd
,
2049 s
->input_statement
.loaded
= FALSE
;
2051 lang_list_init (&add
);
2053 if (! load_symbols (&s
->input_statement
, &add
))
2054 config
.make_executable
= FALSE
;
2056 if (add
.head
!= NULL
)
2058 *add
.tail
= s
->header
.next
;
2059 s
->header
.next
= add
.head
;
2069 /* If there are [COMMONS] statements, put a wild one into the bss
2073 lang_reasonable_defaults ()
2076 lang_output_section_statement_lookup (".text");
2077 lang_output_section_statement_lookup (".data");
2079 default_common_section
= lang_output_section_statement_lookup (".bss");
2081 if (!placed_commons
)
2083 lang_wild_statement_type
*new =
2084 new_stat (lang_wild_statement
,
2085 &default_common_section
->children
);
2087 new->section_name
= "COMMON";
2088 new->filename
= (char *) NULL
;
2089 lang_list_init (&new->children
);
2094 /* Add the supplied name to the symbol table as an undefined reference.
2095 This is a two step process as the symbol table doesn't even exist at
2096 the time the ld command line is processed. First we put the name
2097 on a list, then, once the output file has been opened, transfer the
2098 name to the symbol table. */
2100 typedef struct bfd_sym_chain ldlang_undef_chain_list_type
;
2102 #define ldlang_undef_chain_list_head entry_symbol.next
2105 ldlang_add_undef (name
)
2106 const char *const name
;
2108 ldlang_undef_chain_list_type
*new =
2109 ((ldlang_undef_chain_list_type
*)
2110 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
2112 new->next
= ldlang_undef_chain_list_head
;
2113 ldlang_undef_chain_list_head
= new;
2115 new->name
= xstrdup (name
);
2117 if (output_bfd
!= NULL
)
2118 insert_undefined (new->name
);
2121 /* Insert NAME as undefined in the symbol table. */
2124 insert_undefined (name
)
2127 struct bfd_link_hash_entry
*h
;
2129 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, FALSE
, TRUE
);
2130 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2131 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2132 if (h
->type
== bfd_link_hash_new
)
2134 h
->type
= bfd_link_hash_undefined
;
2135 h
->u
.undef
.abfd
= NULL
;
2136 bfd_link_add_undef (link_info
.hash
, h
);
2140 /* Run through the list of undefineds created above and place them
2141 into the linker hash table as undefined symbols belonging to the
2145 lang_place_undefineds ()
2147 ldlang_undef_chain_list_type
*ptr
;
2149 for (ptr
= ldlang_undef_chain_list_head
;
2150 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
2153 insert_undefined (ptr
->name
);
2157 /* Open input files and attach to output sections. */
2160 map_input_to_output_sections (s
, target
, output_section_statement
)
2161 lang_statement_union_type
*s
;
2163 lang_output_section_statement_type
*output_section_statement
;
2165 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
2167 switch (s
->header
.type
)
2169 case lang_wild_statement_enum
:
2170 wild (&s
->wild_statement
, target
, output_section_statement
);
2172 case lang_constructors_statement_enum
:
2173 map_input_to_output_sections (constructor_list
.head
,
2175 output_section_statement
);
2177 case lang_output_section_statement_enum
:
2178 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2180 &s
->output_section_statement
);
2182 case lang_output_statement_enum
:
2184 case lang_target_statement_enum
:
2185 target
= s
->target_statement
.target
;
2187 case lang_group_statement_enum
:
2188 map_input_to_output_sections (s
->group_statement
.children
.head
,
2190 output_section_statement
);
2192 case lang_fill_statement_enum
:
2193 case lang_input_section_enum
:
2194 case lang_object_symbols_statement_enum
:
2195 case lang_data_statement_enum
:
2196 case lang_reloc_statement_enum
:
2197 case lang_padding_statement_enum
:
2198 case lang_input_statement_enum
:
2199 if (output_section_statement
!= NULL
2200 && output_section_statement
->bfd_section
== NULL
)
2201 init_os (output_section_statement
);
2203 case lang_assignment_statement_enum
:
2204 if (output_section_statement
!= NULL
2205 && output_section_statement
->bfd_section
== NULL
)
2206 init_os (output_section_statement
);
2208 /* Make sure that any sections mentioned in the assignment
2210 exp_init_os (s
->assignment_statement
.exp
);
2212 case lang_afile_asection_pair_statement_enum
:
2215 case lang_address_statement_enum
:
2216 /* Mark the specified section with the supplied address. */
2218 lang_output_section_statement_type
*os
=
2219 lang_output_section_statement_lookup
2220 (s
->address_statement
.section_name
);
2222 if (os
->bfd_section
== NULL
)
2224 os
->addr_tree
= s
->address_statement
.address
;
2231 /* An output section might have been removed after its statement was
2232 added. For example, ldemul_before_allocation can remove dynamic
2233 sections if they turn out to be not needed. Clean them up here. */
2236 strip_excluded_output_sections ()
2238 lang_statement_union_type
*u
;
2240 for (u
= lang_output_section_statement
.head
;
2242 u
= u
->output_section_statement
.next
)
2244 lang_output_section_statement_type
*os
;
2247 os
= &u
->output_section_statement
;
2248 s
= os
->bfd_section
;
2249 if (s
!= NULL
&& (s
->flags
& SEC_EXCLUDE
) != 0)
2253 os
->bfd_section
= NULL
;
2255 for (p
= &output_bfd
->sections
; *p
; p
= &(*p
)->next
)
2258 bfd_section_list_remove (output_bfd
, p
);
2259 output_bfd
->section_count
--;
2267 print_output_section_statement (output_section_statement
)
2268 lang_output_section_statement_type
*output_section_statement
;
2270 asection
*section
= output_section_statement
->bfd_section
;
2273 if (output_section_statement
!= abs_output_section
)
2275 minfo ("\n%s", output_section_statement
->name
);
2277 if (section
!= NULL
)
2279 print_dot
= section
->vma
;
2281 len
= strlen (output_section_statement
->name
);
2282 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2287 while (len
< SECTION_NAME_MAP_LENGTH
)
2293 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2295 if (output_section_statement
->load_base
!= NULL
)
2299 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2300 "load base", lang_final_phase_enum
);
2301 minfo (_(" load address 0x%V"), addr
);
2308 print_statement_list (output_section_statement
->children
.head
,
2309 output_section_statement
);
2313 print_assignment (assignment
, output_section
)
2314 lang_assignment_statement_type
*assignment
;
2315 lang_output_section_statement_type
*output_section
;
2318 etree_value_type result
;
2320 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2323 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2324 lang_final_phase_enum
, print_dot
, &print_dot
);
2330 value
= result
.value
+ result
.section
->bfd_section
->vma
;
2331 dst
= assignment
->exp
->assign
.dst
;
2333 minfo ("0x%V", value
);
2334 if (dst
[0] == '.' && dst
[1] == 0)
2347 exp_print_tree (assignment
->exp
);
2353 print_input_statement (statm
)
2354 lang_input_statement_type
*statm
;
2356 if (statm
->filename
!= (char *) NULL
)
2358 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2362 /* Print all symbols defined in a particular section. This is called
2363 via bfd_link_hash_traverse. */
2366 print_one_symbol (hash_entry
, ptr
)
2367 struct bfd_link_hash_entry
*hash_entry
;
2370 asection
*sec
= (asection
*) ptr
;
2372 if ((hash_entry
->type
== bfd_link_hash_defined
2373 || hash_entry
->type
== bfd_link_hash_defweak
)
2374 && sec
== hash_entry
->u
.def
.section
)
2378 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2381 (hash_entry
->u
.def
.value
2382 + hash_entry
->u
.def
.section
->output_offset
2383 + hash_entry
->u
.def
.section
->output_section
->vma
));
2385 minfo (" %T\n", hash_entry
->root
.string
);
2391 /* Print information about an input section to the map file. */
2394 print_input_section (in
)
2395 lang_input_section_type
*in
;
2397 asection
*i
= in
->section
;
2398 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2399 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2400 ldfile_output_machine
);
2405 minfo ("%s", i
->name
);
2407 if (i
->output_section
!= NULL
)
2411 len
= 1 + strlen (i
->name
);
2412 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2417 while (len
< SECTION_NAME_MAP_LENGTH
)
2423 minfo ("0x%V %W %B\n",
2424 i
->output_section
->vma
+ i
->output_offset
, size
/ opb
,
2427 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2429 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2441 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2444 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2446 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
/ opb
;
2452 print_fill_statement (fill
)
2453 lang_fill_statement_type
*fill
;
2457 fputs (" FILL mask 0x", config
.map_file
);
2458 for (p
= fill
->fill
->data
, size
= fill
->fill
->size
; size
!= 0; p
++, size
--)
2459 fprintf (config
.map_file
, "%02x", *p
);
2460 fputs ("\n", config
.map_file
);
2464 print_data_statement (data
)
2465 lang_data_statement_type
*data
;
2471 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2472 ldfile_output_machine
);
2474 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2477 addr
= data
->output_vma
;
2478 if (data
->output_section
!= NULL
)
2479 addr
+= data
->output_section
->vma
;
2507 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2509 if (data
->exp
->type
.node_class
!= etree_value
)
2512 exp_print_tree (data
->exp
);
2517 print_dot
= addr
+ size
/ opb
;
2521 /* Print an address statement. These are generated by options like
2525 print_address_statement (address
)
2526 lang_address_statement_type
*address
;
2528 minfo (_("Address of section %s set to "), address
->section_name
);
2529 exp_print_tree (address
->address
);
2533 /* Print a reloc statement. */
2536 print_reloc_statement (reloc
)
2537 lang_reloc_statement_type
*reloc
;
2542 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2543 ldfile_output_machine
);
2545 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2548 addr
= reloc
->output_vma
;
2549 if (reloc
->output_section
!= NULL
)
2550 addr
+= reloc
->output_section
->vma
;
2552 size
= bfd_get_reloc_size (reloc
->howto
);
2554 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2556 if (reloc
->name
!= NULL
)
2557 minfo ("%s+", reloc
->name
);
2559 minfo ("%s+", reloc
->section
->name
);
2561 exp_print_tree (reloc
->addend_exp
);
2565 print_dot
= addr
+ size
/ opb
;
2569 print_padding_statement (s
)
2570 lang_padding_statement_type
*s
;
2574 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2575 ldfile_output_machine
);
2579 len
= sizeof " *fill*" - 1;
2580 while (len
< SECTION_NAME_MAP_LENGTH
)
2586 addr
= s
->output_offset
;
2587 if (s
->output_section
!= NULL
)
2588 addr
+= s
->output_section
->vma
;
2589 minfo ("0x%V %W ", addr
, s
->size
);
2591 if (s
->fill
->size
!= 0)
2595 for (p
= s
->fill
->data
, size
= s
->fill
->size
; size
!= 0; p
++, size
--)
2596 fprintf (config
.map_file
, "%02x", *p
);
2601 print_dot
= addr
+ s
->size
/ opb
;
2605 print_wild_statement (w
, os
)
2606 lang_wild_statement_type
*w
;
2607 lang_output_section_statement_type
*os
;
2609 struct wildcard_list
*sec
;
2613 if (w
->filenames_sorted
)
2615 if (w
->filename
!= NULL
)
2616 minfo ("%s", w
->filename
);
2619 if (w
->filenames_sorted
)
2623 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2625 if (sec
->spec
.sorted
)
2627 if (sec
->spec
.exclude_name_list
!= NULL
)
2630 minfo ("EXCLUDE_FILE(%s", sec
->spec
.exclude_name_list
->name
);
2631 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2632 minfo (" %s", tmp
->name
);
2635 if (sec
->spec
.name
!= NULL
)
2636 minfo ("%s", sec
->spec
.name
);
2639 if (sec
->spec
.sorted
)
2648 print_statement_list (w
->children
.head
, os
);
2651 /* Print a group statement. */
2655 lang_group_statement_type
*s
;
2656 lang_output_section_statement_type
*os
;
2658 fprintf (config
.map_file
, "START GROUP\n");
2659 print_statement_list (s
->children
.head
, os
);
2660 fprintf (config
.map_file
, "END GROUP\n");
2663 /* Print the list of statements in S.
2664 This can be called for any statement type. */
2667 print_statement_list (s
, os
)
2668 lang_statement_union_type
*s
;
2669 lang_output_section_statement_type
*os
;
2673 print_statement (s
, os
);
2678 /* Print the first statement in statement list S.
2679 This can be called for any statement type. */
2682 print_statement (s
, os
)
2683 lang_statement_union_type
*s
;
2684 lang_output_section_statement_type
*os
;
2686 switch (s
->header
.type
)
2689 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2692 case lang_constructors_statement_enum
:
2693 if (constructor_list
.head
!= NULL
)
2695 if (constructors_sorted
)
2696 minfo (" SORT (CONSTRUCTORS)\n");
2698 minfo (" CONSTRUCTORS\n");
2699 print_statement_list (constructor_list
.head
, os
);
2702 case lang_wild_statement_enum
:
2703 print_wild_statement (&s
->wild_statement
, os
);
2705 case lang_address_statement_enum
:
2706 print_address_statement (&s
->address_statement
);
2708 case lang_object_symbols_statement_enum
:
2709 minfo (" CREATE_OBJECT_SYMBOLS\n");
2711 case lang_fill_statement_enum
:
2712 print_fill_statement (&s
->fill_statement
);
2714 case lang_data_statement_enum
:
2715 print_data_statement (&s
->data_statement
);
2717 case lang_reloc_statement_enum
:
2718 print_reloc_statement (&s
->reloc_statement
);
2720 case lang_input_section_enum
:
2721 print_input_section (&s
->input_section
);
2723 case lang_padding_statement_enum
:
2724 print_padding_statement (&s
->padding_statement
);
2726 case lang_output_section_statement_enum
:
2727 print_output_section_statement (&s
->output_section_statement
);
2729 case lang_assignment_statement_enum
:
2730 print_assignment (&s
->assignment_statement
, os
);
2732 case lang_target_statement_enum
:
2733 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2735 case lang_output_statement_enum
:
2736 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2737 if (output_target
!= NULL
)
2738 minfo (" %s", output_target
);
2741 case lang_input_statement_enum
:
2742 print_input_statement (&s
->input_statement
);
2744 case lang_group_statement_enum
:
2745 print_group (&s
->group_statement
, os
);
2747 case lang_afile_asection_pair_statement_enum
:
2756 print_statement_list (statement_list
.head
, abs_output_section
);
2759 /* Print the first N statements in statement list S to STDERR.
2760 If N == 0, nothing is printed.
2761 If N < 0, the entire list is printed.
2762 Intended to be called from GDB. */
2765 dprint_statement (s
, n
)
2766 lang_statement_union_type
*s
;
2769 FILE *map_save
= config
.map_file
;
2771 config
.map_file
= stderr
;
2774 print_statement_list (s
, abs_output_section
);
2777 while (s
&& --n
>= 0)
2779 print_statement (s
, abs_output_section
);
2784 config
.map_file
= map_save
;
2788 insert_pad (ptr
, fill
, alignment_needed
, output_section
, dot
)
2789 lang_statement_union_type
**ptr
;
2791 unsigned int alignment_needed
;
2792 asection
*output_section
;
2795 static fill_type zero_fill
= { 1, { 0 } };
2796 lang_statement_union_type
*pad
;
2798 pad
= ((lang_statement_union_type
*)
2799 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2800 if (ptr
!= &statement_list
.head
2801 && pad
->header
.type
== lang_padding_statement_enum
2802 && pad
->padding_statement
.output_section
== output_section
)
2804 /* Use the existing pad statement. The above test on output
2805 section is probably redundant, but it doesn't hurt to check. */
2809 /* Make a new padding statement, linked into existing chain. */
2810 pad
= ((lang_statement_union_type
*)
2811 stat_alloc (sizeof (lang_padding_statement_type
)));
2812 pad
->header
.next
= *ptr
;
2814 pad
->header
.type
= lang_padding_statement_enum
;
2815 pad
->padding_statement
.output_section
= output_section
;
2816 if (fill
== (fill_type
*) 0)
2818 pad
->padding_statement
.fill
= fill
;
2820 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2821 pad
->padding_statement
.size
= alignment_needed
;
2822 output_section
->_raw_size
+= alignment_needed
;
2825 /* Work out how much this section will move the dot point. */
2828 size_input_section (this_ptr
, output_section_statement
, fill
, dot
)
2829 lang_statement_union_type
**this_ptr
;
2830 lang_output_section_statement_type
*output_section_statement
;
2834 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2835 asection
*i
= is
->section
;
2837 if (!is
->ifile
->just_syms_flag
)
2839 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2840 ldfile_output_machine
);
2841 unsigned int alignment_needed
;
2844 /* Align this section first to the input sections requirement,
2845 then to the output section's requirement. If this alignment
2846 is greater than any seen before, then record it too. Perform
2847 the alignment by inserting a magic 'padding' statement. */
2849 if (output_section_statement
->subsection_alignment
!= -1)
2850 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2852 o
= output_section_statement
->bfd_section
;
2853 if (o
->alignment_power
< i
->alignment_power
)
2854 o
->alignment_power
= i
->alignment_power
;
2856 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2858 if (alignment_needed
!= 0)
2860 insert_pad (this_ptr
, fill
, alignment_needed
* opb
, o
, dot
);
2861 dot
+= alignment_needed
;
2864 /* Remember where in the output section this input section goes. */
2866 i
->output_offset
= dot
- o
->vma
;
2868 /* Mark how big the output section must be to contain this now. */
2869 if (i
->_cooked_size
!= 0)
2870 dot
+= i
->_cooked_size
/ opb
;
2872 dot
+= i
->_raw_size
/ opb
;
2873 o
->_raw_size
= (dot
- o
->vma
) * opb
;
2877 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2883 #define IGNORE_SECTION(bfd, s) \
2884 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2885 != (SEC_ALLOC | SEC_LOAD)) \
2886 || bfd_section_size (bfd, s) == 0)
2888 /* Check to see if any allocated sections overlap with other allocated
2889 sections. This can happen when the linker script specifically specifies
2890 the output section addresses of the two sections. */
2893 lang_check_section_addresses ()
2896 unsigned opb
= bfd_octets_per_byte (output_bfd
);
2898 /* Scan all sections in the output list. */
2899 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2903 /* Ignore sections which are not loaded or which have no contents. */
2904 if (IGNORE_SECTION (output_bfd
, s
))
2907 /* Once we reach section 's' stop our seach. This prevents two
2908 warning messages from being produced, one for 'section A overlaps
2909 section B' and one for 'section B overlaps section A'. */
2910 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2917 /* Only consider loadable sections with real contents. */
2918 if (IGNORE_SECTION (output_bfd
, os
))
2921 /* We must check the sections' LMA addresses not their
2922 VMA addresses because overlay sections can have
2923 overlapping VMAs but they must have distinct LMAs. */
2924 s_start
= bfd_section_lma (output_bfd
, s
);
2925 os_start
= bfd_section_lma (output_bfd
, os
);
2926 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) / opb
- 1;
2927 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) / opb
- 1;
2929 /* Look for an overlap. */
2930 if ((s_end
< os_start
) || (s_start
> os_end
))
2934 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2935 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2937 /* Once we have found one overlap for this section,
2938 stop looking for others. */
2944 /* Make sure the new address is within the region. We explicitly permit the
2945 current address to be at the exact end of the region when the address is
2946 non-zero, in case the region is at the end of addressable memory and the
2947 calculation wraps around. */
2950 os_region_check (os
, region
, tree
, base
)
2951 lang_output_section_statement_type
*os
;
2952 struct memory_region_struct
*region
;
2956 if ((region
->current
< region
->origin
2957 || (region
->current
- region
->origin
> region
->length
))
2958 && ((region
->current
!= region
->origin
+ region
->length
)
2961 if (tree
!= (etree_type
*) NULL
)
2963 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2965 os
->bfd_section
->owner
,
2966 os
->bfd_section
->name
,
2971 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2973 os
->bfd_section
->owner
,
2974 os
->bfd_section
->name
);
2976 /* Reset the region pointer. */
2977 region
->current
= region
->origin
;
2981 /* Set the sizes for all the output sections. */
2984 lang_size_sections_1 (s
, output_section_statement
, prev
, fill
, dot
, relax
,
2986 lang_statement_union_type
*s
;
2987 lang_output_section_statement_type
*output_section_statement
;
2988 lang_statement_union_type
**prev
;
2992 bfd_boolean check_regions
;
2994 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2995 ldfile_output_machine
);
2997 /* Size up the sections from their constituent parts. */
2998 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3000 switch (s
->header
.type
)
3002 case lang_output_section_statement_enum
:
3005 lang_output_section_statement_type
*os
;
3007 os
= &s
->output_section_statement
;
3008 if (os
->bfd_section
== NULL
)
3009 /* This section was never actually created. */
3012 /* If this is a COFF shared library section, use the size and
3013 address from the input section. FIXME: This is COFF
3014 specific; it would be cleaner if there were some other way
3015 to do this, but nothing simple comes to mind. */
3016 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
3020 if (os
->children
.head
== NULL
3021 || os
->children
.head
->header
.next
!= NULL
3022 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
3023 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
3026 input
= os
->children
.head
->input_section
.section
;
3027 bfd_set_section_vma (os
->bfd_section
->owner
,
3029 bfd_section_vma (input
->owner
, input
));
3030 os
->bfd_section
->_raw_size
= input
->_raw_size
;
3034 if (bfd_is_abs_section (os
->bfd_section
))
3036 /* No matter what happens, an abs section starts at zero. */
3037 ASSERT (os
->bfd_section
->vma
== 0);
3041 if (os
->addr_tree
== (etree_type
*) NULL
)
3043 /* No address specified for this section, get one
3044 from the region specification. */
3045 if (os
->region
== (lang_memory_region_type
*) NULL
3046 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3047 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
3048 && os
->region
->name
[0] == '*'
3049 && strcmp (os
->region
->name
, "*default*") == 0))
3051 os
->region
= lang_memory_default (os
->bfd_section
);
3054 /* If a loadable section is using the default memory
3055 region, and some non default memory regions were
3056 defined, issue an error message. */
3057 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3058 & (SEC_ALLOC
| SEC_LOAD
)) != 0
3059 && (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3060 & SEC_NEVER_LOAD
) == 0
3061 && ! link_info
.relocateable
3063 && strcmp (os
->region
->name
, "*default*") == 0
3064 && lang_memory_region_list
!= NULL
3065 && (strcmp (lang_memory_region_list
->name
,
3067 || lang_memory_region_list
->next
!= NULL
))
3069 /* By default this is an error rather than just a
3070 warning because if we allocate the section to the
3071 default memory region we can end up creating an
3072 excessivly large binary, or even seg faulting when
3073 attmepting to perform a negative seek. See
3074 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
3075 for an example of this. This behaviour can be
3076 overridden by the using the --no-check-sections
3078 if (command_line
.check_section_addresses
)
3079 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
3080 bfd_get_section_name (output_bfd
,
3083 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
3084 bfd_get_section_name (output_bfd
,
3088 dot
= os
->region
->current
;
3090 if (os
->section_alignment
== -1)
3095 dot
= align_power (dot
,
3096 os
->bfd_section
->alignment_power
);
3098 if (dot
!= olddot
&& config
.warn_section_align
)
3099 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
3100 os
->name
, (unsigned int) (dot
- olddot
));
3107 r
= exp_fold_tree (os
->addr_tree
,
3109 lang_allocating_phase_enum
,
3112 einfo (_("%F%S: non constant address expression for section %s\n"),
3115 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
3118 /* The section starts here.
3119 First, align to what the section needs. */
3121 if (os
->section_alignment
!= -1)
3122 dot
= align_power (dot
, os
->section_alignment
);
3124 bfd_set_section_vma (0, os
->bfd_section
, dot
);
3126 os
->bfd_section
->output_offset
= 0;
3129 lang_size_sections_1 (os
->children
.head
, os
, &os
->children
.head
,
3130 os
->fill
, dot
, relax
, check_regions
);
3132 /* Put the section within the requested block size, or
3133 align at the block boundary. */
3134 after
= align_n (os
->bfd_section
->vma
3135 + os
->bfd_section
->_raw_size
/ opb
,
3136 (bfd_vma
) os
->block_value
);
3138 if (bfd_is_abs_section (os
->bfd_section
))
3139 ASSERT (after
== os
->bfd_section
->vma
);
3140 else if ((os
->bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0
3141 && (os
->bfd_section
->flags
& SEC_THREAD_LOCAL
)
3142 && ! link_info
.relocateable
)
3143 os
->bfd_section
->_raw_size
= 0;
3145 os
->bfd_section
->_raw_size
=
3146 (after
- os
->bfd_section
->vma
) * opb
;
3148 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3149 os
->processed
= TRUE
;
3151 if (os
->update_dot_tree
!= 0)
3152 exp_fold_tree (os
->update_dot_tree
, abs_output_section
,
3153 lang_allocating_phase_enum
, dot
, &dot
);
3155 /* Update dot in the region ?
3156 We only do this if the section is going to be allocated,
3157 since unallocated sections do not contribute to the region's
3158 overall size in memory.
3160 If the SEC_NEVER_LOAD bit is not set, it will affect the
3161 addresses of sections after it. We have to update
3163 if (os
->region
!= (lang_memory_region_type
*) NULL
3164 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3165 & SEC_NEVER_LOAD
) == 0
3166 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
3167 & (SEC_ALLOC
| SEC_LOAD
))))
3169 os
->region
->current
= dot
;
3172 /* Make sure the new address is within the region. */
3173 os_region_check (os
, os
->region
, os
->addr_tree
,
3174 os
->bfd_section
->vma
);
3176 /* If there's no load address specified, use the run
3177 region as the load region. */
3178 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
3179 os
->lma_region
= os
->region
;
3181 if (os
->lma_region
!= NULL
&& os
->lma_region
!= os
->region
)
3183 /* Set load_base, which will be handled later. */
3184 os
->load_base
= exp_intop (os
->lma_region
->current
);
3185 os
->lma_region
->current
+=
3186 os
->bfd_section
->_raw_size
/ opb
;
3188 os_region_check (os
, os
->lma_region
, NULL
,
3189 os
->bfd_section
->lma
);
3195 case lang_constructors_statement_enum
:
3196 dot
= lang_size_sections_1 (constructor_list
.head
,
3197 output_section_statement
,
3198 &s
->wild_statement
.children
.head
,
3199 fill
, dot
, relax
, check_regions
);
3202 case lang_data_statement_enum
:
3204 unsigned int size
= 0;
3206 s
->data_statement
.output_vma
=
3207 dot
- output_section_statement
->bfd_section
->vma
;
3208 s
->data_statement
.output_section
=
3209 output_section_statement
->bfd_section
;
3211 switch (s
->data_statement
.type
)
3232 output_section_statement
->bfd_section
->_raw_size
+= size
;
3233 /* The output section gets contents, and then we inspect for
3234 any flags set in the input script which override any ALLOC. */
3235 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3236 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3238 output_section_statement
->bfd_section
->flags
|=
3239 SEC_ALLOC
| SEC_LOAD
;
3244 case lang_reloc_statement_enum
:
3248 s
->reloc_statement
.output_vma
=
3249 dot
- output_section_statement
->bfd_section
->vma
;
3250 s
->reloc_statement
.output_section
=
3251 output_section_statement
->bfd_section
;
3252 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3254 output_section_statement
->bfd_section
->_raw_size
+= size
;
3258 case lang_wild_statement_enum
:
3260 dot
= lang_size_sections_1 (s
->wild_statement
.children
.head
,
3261 output_section_statement
,
3262 &s
->wild_statement
.children
.head
,
3263 fill
, dot
, relax
, check_regions
);
3267 case lang_object_symbols_statement_enum
:
3268 link_info
.create_object_symbols_section
=
3269 output_section_statement
->bfd_section
;
3271 case lang_output_statement_enum
:
3272 case lang_target_statement_enum
:
3274 case lang_input_section_enum
:
3278 i
= (*prev
)->input_section
.section
;
3281 if (i
->_cooked_size
== 0)
3282 i
->_cooked_size
= i
->_raw_size
;
3288 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3289 einfo (_("%P%F: can't relax section: %E\n"));
3293 dot
= size_input_section (prev
, output_section_statement
,
3294 output_section_statement
->fill
, dot
);
3297 case lang_input_statement_enum
:
3299 case lang_fill_statement_enum
:
3300 s
->fill_statement
.output_section
=
3301 output_section_statement
->bfd_section
;
3303 fill
= s
->fill_statement
.fill
;
3305 case lang_assignment_statement_enum
:
3307 bfd_vma newdot
= dot
;
3309 exp_fold_tree (s
->assignment_statement
.exp
,
3310 output_section_statement
,
3311 lang_allocating_phase_enum
,
3317 if (output_section_statement
== abs_output_section
)
3319 /* If we don't have an output section, then just adjust
3320 the default memory address. */
3321 lang_memory_region_lookup ("*default*")->current
= newdot
;
3325 /* Insert a pad after this statement. We can't
3326 put the pad before when relaxing, in case the
3327 assignment references dot. */
3328 insert_pad (&s
->header
.next
, fill
, (newdot
- dot
) * opb
,
3329 output_section_statement
->bfd_section
, dot
);
3331 /* Don't neuter the pad below when relaxing. */
3340 case lang_padding_statement_enum
:
3341 /* If this is the first time lang_size_sections is called,
3342 we won't have any padding statements. If this is the
3343 second or later passes when relaxing, we should allow
3344 padding to shrink. If padding is needed on this pass, it
3345 will be added back in. */
3346 s
->padding_statement
.size
= 0;
3348 /* Make sure output_offset is valid. If relaxation shrinks
3349 the section and this pad isn't needed, it's possible to
3350 have output_offset larger than the final size of the
3351 section. bfd_set_section_contents will complain even for
3352 a pad size of zero. */
3353 s
->padding_statement
.output_offset
3354 = dot
- output_section_statement
->bfd_section
->vma
;
3357 case lang_group_statement_enum
:
3358 dot
= lang_size_sections_1 (s
->group_statement
.children
.head
,
3359 output_section_statement
,
3360 &s
->group_statement
.children
.head
,
3361 fill
, dot
, relax
, check_regions
);
3368 /* We can only get here when relaxing is turned on. */
3369 case lang_address_statement_enum
:
3372 prev
= &s
->header
.next
;
3378 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
,
3380 lang_statement_union_type
*s
;
3381 lang_output_section_statement_type
*output_section_statement
;
3382 lang_statement_union_type
**prev
;
3386 bfd_boolean check_regions
;
3390 exp_data_seg
.phase
= exp_dataseg_none
;
3391 result
= lang_size_sections_1 (s
, output_section_statement
, prev
, fill
,
3392 dot
, relax
, check_regions
);
3393 if (exp_data_seg
.phase
== exp_dataseg_end_seen
)
3395 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3396 a page could be saved in the data segment. */
3397 bfd_vma first
, last
;
3399 first
= -exp_data_seg
.base
& (exp_data_seg
.pagesize
- 1);
3400 last
= exp_data_seg
.end
& (exp_data_seg
.pagesize
- 1);
3402 && ((exp_data_seg
.base
& ~(exp_data_seg
.pagesize
- 1))
3403 != (exp_data_seg
.end
& ~(exp_data_seg
.pagesize
- 1)))
3404 && first
+ last
<= exp_data_seg
.pagesize
)
3406 exp_data_seg
.phase
= exp_dataseg_adjust
;
3407 result
= lang_size_sections_1 (s
, output_section_statement
, prev
,
3408 fill
, dot
, relax
, check_regions
);
3416 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3417 lang_statement_union_type
*s
;
3418 lang_output_section_statement_type
*output_section_statement
;
3422 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3423 ldfile_output_machine
);
3425 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3427 switch (s
->header
.type
)
3429 case lang_constructors_statement_enum
:
3430 dot
= lang_do_assignments (constructor_list
.head
,
3431 output_section_statement
,
3436 case lang_output_section_statement_enum
:
3438 lang_output_section_statement_type
*os
;
3440 os
= &(s
->output_section_statement
);
3441 if (os
->bfd_section
!= NULL
)
3443 dot
= os
->bfd_section
->vma
;
3444 (void) lang_do_assignments (os
->children
.head
, os
,
3446 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3451 /* If nothing has been placed into the output section then
3452 it won't have a bfd_section. */
3453 if (os
->bfd_section
)
3455 os
->bfd_section
->lma
3456 = exp_get_abs_int (os
->load_base
, 0, "load base",
3457 lang_final_phase_enum
);
3462 case lang_wild_statement_enum
:
3464 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3465 output_section_statement
,
3470 case lang_object_symbols_statement_enum
:
3471 case lang_output_statement_enum
:
3472 case lang_target_statement_enum
:
3474 case lang_common_statement_enum
:
3477 case lang_data_statement_enum
:
3479 etree_value_type value
;
3481 value
= exp_fold_tree (s
->data_statement
.exp
,
3483 lang_final_phase_enum
, dot
, &dot
);
3484 s
->data_statement
.value
= value
.value
;
3486 einfo (_("%F%P: invalid data statement\n"));
3490 switch (s
->data_statement
.type
)
3514 case lang_reloc_statement_enum
:
3516 etree_value_type value
;
3518 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3520 lang_final_phase_enum
, dot
, &dot
);
3521 s
->reloc_statement
.addend_value
= value
.value
;
3523 einfo (_("%F%P: invalid reloc statement\n"));
3525 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
) / opb
;
3528 case lang_input_section_enum
:
3530 asection
*in
= s
->input_section
.section
;
3532 if (in
->_cooked_size
!= 0)
3533 dot
+= in
->_cooked_size
/ opb
;
3535 dot
+= in
->_raw_size
/ opb
;
3539 case lang_input_statement_enum
:
3541 case lang_fill_statement_enum
:
3542 fill
= s
->fill_statement
.fill
;
3544 case lang_assignment_statement_enum
:
3546 exp_fold_tree (s
->assignment_statement
.exp
,
3547 output_section_statement
,
3548 lang_final_phase_enum
,
3554 case lang_padding_statement_enum
:
3555 dot
+= s
->padding_statement
.size
/ opb
;
3558 case lang_group_statement_enum
:
3559 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3560 output_section_statement
,
3568 case lang_address_statement_enum
:
3576 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3577 operator .startof. (section_name), it produces an undefined symbol
3578 .startof.section_name. Similarly, when it sees
3579 .sizeof. (section_name), it produces an undefined symbol
3580 .sizeof.section_name. For all the output sections, we look for
3581 such symbols, and set them to the correct value. */
3588 if (link_info
.relocateable
)
3591 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3593 const char *secname
;
3595 struct bfd_link_hash_entry
*h
;
3597 secname
= bfd_get_section_name (output_bfd
, s
);
3598 buf
= xmalloc (10 + strlen (secname
));
3600 sprintf (buf
, ".startof.%s", secname
);
3601 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3602 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3604 h
->type
= bfd_link_hash_defined
;
3605 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3606 h
->u
.def
.section
= bfd_abs_section_ptr
;
3609 sprintf (buf
, ".sizeof.%s", secname
);
3610 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, FALSE
, FALSE
, TRUE
);
3611 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3615 opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3616 ldfile_output_machine
);
3617 h
->type
= bfd_link_hash_defined
;
3618 if (s
->_cooked_size
!= 0)
3619 h
->u
.def
.value
= s
->_cooked_size
/ opb
;
3621 h
->u
.def
.value
= s
->_raw_size
/ opb
;
3622 h
->u
.def
.section
= bfd_abs_section_ptr
;
3632 struct bfd_link_hash_entry
*h
;
3635 if (link_info
.relocateable
|| link_info
.shared
)
3640 if (entry_symbol
.name
== (const char *) NULL
)
3642 /* No entry has been specified. Look for start, but don't warn
3643 if we don't find it. */
3644 entry_symbol
.name
= "start";
3648 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
.name
,
3649 FALSE
, FALSE
, TRUE
);
3650 if (h
!= (struct bfd_link_hash_entry
*) NULL
3651 && (h
->type
== bfd_link_hash_defined
3652 || h
->type
== bfd_link_hash_defweak
)
3653 && h
->u
.def
.section
->output_section
!= NULL
)
3657 val
= (h
->u
.def
.value
3658 + bfd_get_section_vma (output_bfd
,
3659 h
->u
.def
.section
->output_section
)
3660 + h
->u
.def
.section
->output_offset
);
3661 if (! bfd_set_start_address (output_bfd
, val
))
3662 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
.name
);
3669 /* We couldn't find the entry symbol. Try parsing it as a
3671 val
= bfd_scan_vma (entry_symbol
.name
, &send
, 0);
3674 if (! bfd_set_start_address (output_bfd
, val
))
3675 einfo (_("%P%F: can't set start address\n"));
3681 /* Can't find the entry symbol, and it's not a number. Use
3682 the first address in the text section. */
3683 ts
= bfd_get_section_by_name (output_bfd
, entry_section
);
3684 if (ts
!= (asection
*) NULL
)
3687 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3689 bfd_get_section_vma (output_bfd
, ts
));
3690 if (! bfd_set_start_address (output_bfd
,
3691 bfd_get_section_vma (output_bfd
,
3693 einfo (_("%P%F: can't set start address\n"));
3698 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3705 /* This is a small function used when we want to ignore errors from
3709 #ifdef ANSI_PROTOTYPES
3710 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3712 ignore_bfd_errors (s
)
3713 const char *s ATTRIBUTE_UNUSED
;
3716 /* Don't do anything. */
3719 /* Check that the architecture of all the input files is compatible
3720 with the output file. Also call the backend to let it do any
3721 other checking that is needed. */
3726 lang_statement_union_type
*file
;
3728 const bfd_arch_info_type
*compatible
;
3730 for (file
= file_chain
.head
;
3731 file
!= (lang_statement_union_type
*) NULL
;
3732 file
= file
->input_statement
.next
)
3734 input_bfd
= file
->input_statement
.the_bfd
;
3735 compatible
= bfd_arch_get_compatible (input_bfd
, output_bfd
,
3736 command_line
.accept_unknown_input_arch
);
3738 /* In general it is not possible to perform a relocatable
3739 link between differing object formats when the input
3740 file has relocations, because the relocations in the
3741 input format may not have equivalent representations in
3742 the output format (and besides BFD does not translate
3743 relocs for other link purposes than a final link). */
3744 if ((link_info
.relocateable
|| link_info
.emitrelocations
)
3745 && (compatible
== NULL
3746 || bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
))
3747 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3749 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3750 bfd_get_target (input_bfd
), input_bfd
,
3751 bfd_get_target (output_bfd
), output_bfd
);
3752 /* einfo with %F exits. */
3755 if (compatible
== NULL
)
3757 if (command_line
.warn_mismatch
)
3758 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3759 bfd_printable_name (input_bfd
), input_bfd
,
3760 bfd_printable_name (output_bfd
));
3762 else if (bfd_count_sections (input_bfd
))
3764 /* If the input bfd has no contents, it shouldn't set the
3765 private data of the output bfd. */
3767 bfd_error_handler_type pfn
= NULL
;
3769 /* If we aren't supposed to warn about mismatched input
3770 files, temporarily set the BFD error handler to a
3771 function which will do nothing. We still want to call
3772 bfd_merge_private_bfd_data, since it may set up
3773 information which is needed in the output file. */
3774 if (! command_line
.warn_mismatch
)
3775 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3776 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3778 if (command_line
.warn_mismatch
)
3779 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3782 if (! command_line
.warn_mismatch
)
3783 bfd_set_error_handler (pfn
);
3788 /* Look through all the global common symbols and attach them to the
3789 correct section. The -sort-common command line switch may be used
3790 to roughly sort the entries by size. */
3795 if (command_line
.inhibit_common_definition
)
3797 if (link_info
.relocateable
3798 && ! command_line
.force_common_definition
)
3801 if (! config
.sort_common
)
3802 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3807 for (power
= 4; power
>= 0; power
--)
3808 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3813 /* Place one common symbol in the correct section. */
3816 lang_one_common (h
, info
)
3817 struct bfd_link_hash_entry
*h
;
3820 unsigned int power_of_two
;
3823 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3824 ldfile_output_machine
);
3826 if (h
->type
!= bfd_link_hash_common
)
3830 power_of_two
= h
->u
.c
.p
->alignment_power
;
3832 if (config
.sort_common
3833 && power_of_two
< (unsigned int) *(int *) info
)
3836 section
= h
->u
.c
.p
->section
;
3838 /* Increase the size of the section. */
3839 section
->_cooked_size
= align_n ((section
->_cooked_size
+ opb
- 1) / opb
,
3840 (bfd_vma
) 1 << power_of_two
) * opb
;
3842 /* Adjust the alignment if necessary. */
3843 if (power_of_two
> section
->alignment_power
)
3844 section
->alignment_power
= power_of_two
;
3846 /* Change the symbol from common to defined. */
3847 h
->type
= bfd_link_hash_defined
;
3848 h
->u
.def
.section
= section
;
3849 h
->u
.def
.value
= section
->_cooked_size
;
3851 /* Increase the size of the section. */
3852 section
->_cooked_size
+= size
;
3854 /* Make sure the section is allocated in memory, and make sure that
3855 it is no longer a common section. */
3856 section
->flags
|= SEC_ALLOC
;
3857 section
->flags
&= ~SEC_IS_COMMON
;
3859 if (config
.map_file
!= NULL
)
3861 static bfd_boolean header_printed
;
3866 if (! header_printed
)
3868 minfo (_("\nAllocating common symbols\n"));
3869 minfo (_("Common symbol size file\n\n"));
3870 header_printed
= TRUE
;
3873 name
= demangle (h
->root
.string
);
3875 len
= strlen (name
);
3890 if (size
<= 0xffffffff)
3891 sprintf (buf
, "%lx", (unsigned long) size
);
3893 sprintf_vma (buf
, size
);
3903 minfo ("%B\n", section
->owner
);
3909 /* Run through the input files and ensure that every input section has
3910 somewhere to go. If one is found without a destination then create
3911 an input request and place it into the statement tree. */
3914 lang_place_orphans ()
3916 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3920 for (s
= file
->the_bfd
->sections
;
3921 s
!= (asection
*) NULL
;
3924 if (s
->output_section
== (asection
*) NULL
)
3926 /* This section of the file is not attached, root
3927 around for a sensible place for it to go. */
3929 if (file
->just_syms_flag
)
3933 else if (strcmp (s
->name
, "COMMON") == 0)
3935 /* This is a lonely common section which must have
3936 come from an archive. We attach to the section
3937 with the wildcard. */
3938 if (! link_info
.relocateable
3939 || command_line
.force_common_definition
)
3941 if (default_common_section
== NULL
)
3944 /* This message happens when using the
3945 svr3.ifile linker script, so I have
3947 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3949 default_common_section
=
3950 lang_output_section_statement_lookup (".bss");
3953 lang_add_section (&default_common_section
->children
, s
,
3954 default_common_section
, file
);
3957 else if (ldemul_place_orphan (file
, s
))
3961 lang_output_section_statement_type
*os
;
3963 os
= lang_output_section_statement_lookup (s
->name
);
3964 lang_add_section (&os
->children
, s
, os
, file
);
3972 lang_set_flags (ptr
, flags
, invert
)
3973 lang_memory_region_type
*ptr
;
3977 flagword
*ptr_flags
;
3979 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3985 *ptr_flags
|= SEC_ALLOC
;
3989 *ptr_flags
|= SEC_READONLY
;
3993 *ptr_flags
|= SEC_DATA
;
3997 *ptr_flags
|= SEC_CODE
;
4002 *ptr_flags
|= SEC_LOAD
;
4006 einfo (_("%P%F: invalid syntax in flags\n"));
4013 /* Call a function on each input file. This function will be called
4014 on an archive, but not on the elements. */
4017 lang_for_each_input_file (func
)
4018 void (*func
) PARAMS ((lang_input_statement_type
*));
4020 lang_input_statement_type
*f
;
4022 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
4024 f
= (lang_input_statement_type
*) f
->next_real_file
)
4028 /* Call a function on each file. The function will be called on all
4029 the elements of an archive which are included in the link, but will
4030 not be called on the archive file itself. */
4033 lang_for_each_file (func
)
4034 void (*func
) PARAMS ((lang_input_statement_type
*));
4036 LANG_FOR_EACH_INPUT_STATEMENT (f
)
4047 lang_for_each_input_section (func
)
4048 void (*func
) PARAMS ((bfd
*ab
, asection
*as
));
4050 LANG_FOR_EACH_INPUT_STATEMENT (f
)
4054 for (s
= f
->the_bfd
->sections
;
4055 s
!= (asection
*) NULL
;
4058 func (f
->the_bfd
, s
);
4066 ldlang_add_file (entry
)
4067 lang_input_statement_type
*entry
;
4071 lang_statement_append (&file_chain
,
4072 (lang_statement_union_type
*) entry
,
4075 /* The BFD linker needs to have a list of all input BFDs involved in
4077 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
4078 ASSERT (entry
->the_bfd
!= output_bfd
);
4079 for (pp
= &link_info
.input_bfds
;
4080 *pp
!= (bfd
*) NULL
;
4081 pp
= &(*pp
)->link_next
)
4083 *pp
= entry
->the_bfd
;
4084 entry
->the_bfd
->usrdata
= (PTR
) entry
;
4085 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
4087 /* Look through the sections and check for any which should not be
4088 included in the link. We need to do this now, so that we can
4089 notice when the backend linker tries to report multiple
4090 definition errors for symbols which are in sections we aren't
4091 going to link. FIXME: It might be better to entirely ignore
4092 symbols which are defined in sections which are going to be
4093 discarded. This would require modifying the backend linker for
4094 each backend which might set the SEC_LINK_ONCE flag. If we do
4095 this, we should probably handle SEC_EXCLUDE in the same way. */
4097 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
4101 lang_add_output (name
, from_script
)
4105 /* Make -o on command line override OUTPUT in script. */
4106 if (!had_output_filename
|| !from_script
)
4108 output_filename
= name
;
4109 had_output_filename
= TRUE
;
4113 static lang_output_section_statement_type
*current_section
;
4125 for (l
= 0; l
< 32; l
++)
4127 if (i
>= (unsigned int) x
)
4135 lang_output_section_statement_type
*
4136 lang_enter_output_section_statement (output_section_statement_name
,
4137 address_exp
, sectype
, block_value
,
4138 align
, subalign
, ebase
)
4139 const char *output_section_statement_name
;
4140 etree_type
*address_exp
;
4141 enum section_type sectype
;
4142 bfd_vma block_value
;
4144 etree_type
*subalign
;
4147 lang_output_section_statement_type
*os
;
4151 lang_output_section_statement_lookup (output_section_statement_name
);
4153 /* Add this statement to tree. */
4155 add_statement (lang_output_section_statement_enum
,
4156 output_section_statement
);
4158 /* Make next things chain into subchain of this. */
4160 if (os
->addr_tree
== (etree_type
*) NULL
)
4162 os
->addr_tree
= address_exp
;
4164 os
->sectype
= sectype
;
4165 if (sectype
!= noload_section
)
4166 os
->flags
= SEC_NO_FLAGS
;
4168 os
->flags
= SEC_NEVER_LOAD
;
4169 os
->block_value
= block_value
? block_value
: 1;
4170 stat_ptr
= &os
->children
;
4172 os
->subsection_alignment
=
4173 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
4174 os
->section_alignment
=
4175 topower (exp_get_value_int (align
, -1, "section alignment", 0));
4177 os
->load_base
= ebase
;
4184 lang_output_statement_type
*new =
4185 new_stat (lang_output_statement
, stat_ptr
);
4187 new->name
= output_filename
;
4190 /* Reset the current counters in the regions. */
4193 lang_reset_memory_regions ()
4195 lang_memory_region_type
*p
= lang_memory_region_list
;
4198 for (p
= lang_memory_region_list
;
4199 p
!= (lang_memory_region_type
*) NULL
;
4202 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
4203 p
->current
= p
->origin
;
4206 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4210 /* If the wild pattern was marked KEEP, the member sections
4211 should be as well. */
4214 gc_section_callback (ptr
, sec
, section
, file
, data
)
4215 lang_wild_statement_type
*ptr
;
4216 struct wildcard_list
*sec ATTRIBUTE_UNUSED
;
4218 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
4219 PTR data ATTRIBUTE_UNUSED
;
4221 if (ptr
->keep_sections
)
4222 section
->flags
|= SEC_KEEP
;
4225 /* Handle a wild statement, marking it against GC. */
4229 lang_wild_statement_type
*s
;
4231 walk_wild (s
, gc_section_callback
, NULL
);
4234 /* Iterate over sections marking them against GC. */
4237 lang_gc_sections_1 (s
)
4238 lang_statement_union_type
*s
;
4240 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
4242 switch (s
->header
.type
)
4244 case lang_wild_statement_enum
:
4245 lang_gc_wild (&s
->wild_statement
);
4247 case lang_constructors_statement_enum
:
4248 lang_gc_sections_1 (constructor_list
.head
);
4250 case lang_output_section_statement_enum
:
4251 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4253 case lang_group_statement_enum
:
4254 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4265 struct bfd_link_hash_entry
*h
;
4266 ldlang_undef_chain_list_type
*ulist
;
4268 /* Keep all sections so marked in the link script. */
4270 lang_gc_sections_1 (statement_list
.head
);
4272 /* Keep all sections containing symbols undefined on the command-line,
4273 and the section containing the entry symbol. */
4275 for (ulist
= link_info
.gc_sym_list
; ulist
; ulist
= ulist
->next
)
4277 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4278 FALSE
, FALSE
, FALSE
);
4280 if (h
!= (struct bfd_link_hash_entry
*) NULL
4281 && (h
->type
== bfd_link_hash_defined
4282 || h
->type
== bfd_link_hash_defweak
)
4283 && ! bfd_is_abs_section (h
->u
.def
.section
))
4285 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4289 bfd_gc_sections (output_bfd
, &link_info
);
4295 lang_reasonable_defaults ();
4296 current_target
= default_target
;
4298 /* Open the output file. */
4299 lang_for_each_statement (ldlang_open_output
);
4301 ldemul_create_output_section_statements ();
4303 /* Add to the hash table all undefineds on the command line. */
4304 lang_place_undefineds ();
4306 already_linked_table_init ();
4308 /* Create a bfd for each input file. */
4309 current_target
= default_target
;
4310 open_input_bfds (statement_list
.head
, FALSE
);
4312 link_info
.gc_sym_list
= &entry_symbol
;
4313 if (entry_symbol
.name
== NULL
)
4314 link_info
.gc_sym_list
= ldlang_undef_chain_list_head
;
4316 ldemul_after_open ();
4318 already_linked_table_free ();
4320 /* Make sure that we're not mixing architectures. We call this
4321 after all the input files have been opened, but before we do any
4322 other processing, so that any operations merge_private_bfd_data
4323 does on the output file will be known during the rest of the
4327 /* Handle .exports instead of a version script if we're told to do so. */
4328 if (command_line
.version_exports_section
)
4329 lang_do_version_exports_section ();
4331 /* Build all sets based on the information gathered from the input
4333 ldctor_build_sets ();
4335 /* Remove unreferenced sections if asked to. */
4336 if (command_line
.gc_sections
)
4337 lang_gc_sections ();
4339 /* If there were any SEC_MERGE sections, finish their merging, so that
4340 section sizes can be computed. This has to be done after GC of sections,
4341 so that GCed sections are not merged, but before assigning output
4342 sections, since removing whole input sections is hard then. */
4343 bfd_merge_sections (output_bfd
, &link_info
);
4345 /* Size up the common data. */
4348 /* Run through the contours of the script and attach input sections
4349 to the correct output sections. */
4350 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
4351 (lang_output_section_statement_type
*) NULL
);
4353 /* Find any sections not attached explicitly and handle them. */
4354 lang_place_orphans ();
4356 if (! link_info
.relocateable
)
4358 /* Look for a text section and set the readonly attribute in it. */
4359 asection
*found
= bfd_get_section_by_name (output_bfd
, ".text");
4361 if (found
!= (asection
*) NULL
)
4363 if (config
.text_read_only
)
4364 found
->flags
|= SEC_READONLY
;
4366 found
->flags
&= ~SEC_READONLY
;
4370 /* Do anything special before sizing sections. This is where ELF
4371 and other back-ends size dynamic sections. */
4372 ldemul_before_allocation ();
4374 if (!link_info
.relocateable
)
4375 strip_excluded_output_sections ();
4377 /* We must record the program headers before we try to fix the
4378 section positions, since they will affect SIZEOF_HEADERS. */
4379 lang_record_phdrs ();
4381 /* Size up the sections. */
4382 lang_size_sections (statement_list
.head
,
4384 &statement_list
.head
, 0, (bfd_vma
) 0, NULL
,
4385 command_line
.relax
? FALSE
: TRUE
);
4387 /* Now run around and relax if we can. */
4388 if (command_line
.relax
)
4390 /* Keep relaxing until bfd_relax_section gives up. */
4391 bfd_boolean relax_again
;
4395 lang_reset_memory_regions ();
4397 relax_again
= FALSE
;
4399 /* Note: pe-dll.c does something like this also. If you find
4400 you need to change this code, you probably need to change
4401 pe-dll.c also. DJ */
4403 /* Do all the assignments with our current guesses as to
4405 lang_do_assignments (statement_list
.head
,
4407 (fill_type
*) 0, (bfd_vma
) 0);
4409 /* Perform another relax pass - this time we know where the
4410 globals are, so can make a better guess. */
4411 lang_size_sections (statement_list
.head
,
4413 &statement_list
.head
, 0, (bfd_vma
) 0,
4414 &relax_again
, FALSE
);
4416 /* If the normal relax is done and the relax finalize pass
4417 is not performed yet, we perform another relax pass. */
4418 if (!relax_again
&& !link_info
.relax_finalizing
)
4420 link_info
.relax_finalizing
= TRUE
;
4424 while (relax_again
);
4426 /* Final extra sizing to report errors. */
4427 lang_reset_memory_regions ();
4428 lang_do_assignments (statement_list
.head
,
4430 (fill_type
*) 0, (bfd_vma
) 0);
4431 lang_size_sections (statement_list
.head
,
4433 & statement_list
.head
, 0, (bfd_vma
) 0,
4437 /* See if anything special should be done now we know how big
4439 ldemul_after_allocation ();
4441 /* Fix any .startof. or .sizeof. symbols. */
4442 lang_set_startof ();
4444 /* Do all the assignments, now that we know the final resting places
4445 of all the symbols. */
4447 lang_do_assignments (statement_list
.head
,
4449 (fill_type
*) 0, (bfd_vma
) 0);
4451 /* Make sure that the section addresses make sense. */
4452 if (! link_info
.relocateable
4453 && command_line
.check_section_addresses
)
4454 lang_check_section_addresses ();
4462 /* EXPORTED TO YACC */
4465 lang_add_wild (filespec
, section_list
, keep_sections
)
4466 struct wildcard_spec
*filespec
;
4467 struct wildcard_list
*section_list
;
4468 bfd_boolean keep_sections
;
4470 struct wildcard_list
*curr
, *next
;
4471 lang_wild_statement_type
*new;
4473 /* Reverse the list as the parser puts it back to front. */
4474 for (curr
= section_list
, section_list
= NULL
;
4476 section_list
= curr
, curr
= next
)
4478 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4479 placed_commons
= TRUE
;
4482 curr
->next
= section_list
;
4485 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4487 if (strcmp (filespec
->name
, "*") == 0)
4488 filespec
->name
= NULL
;
4489 else if (! wildcardp (filespec
->name
))
4490 lang_has_input_file
= TRUE
;
4493 new = new_stat (lang_wild_statement
, stat_ptr
);
4494 new->filename
= NULL
;
4495 new->filenames_sorted
= FALSE
;
4496 if (filespec
!= NULL
)
4498 new->filename
= filespec
->name
;
4499 new->filenames_sorted
= filespec
->sorted
;
4501 new->section_list
= section_list
;
4502 new->keep_sections
= keep_sections
;
4503 lang_list_init (&new->children
);
4507 lang_section_start (name
, address
)
4509 etree_type
*address
;
4511 lang_address_statement_type
*ad
;
4513 ad
= new_stat (lang_address_statement
, stat_ptr
);
4514 ad
->section_name
= name
;
4515 ad
->address
= address
;
4518 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4519 because of a -e argument on the command line, or zero if this is
4520 called by ENTRY in a linker script. Command line arguments take
4524 lang_add_entry (name
, cmdline
)
4526 bfd_boolean cmdline
;
4528 if (entry_symbol
.name
== NULL
4530 || ! entry_from_cmdline
)
4532 entry_symbol
.name
= name
;
4533 entry_from_cmdline
= cmdline
;
4538 lang_add_target (name
)
4541 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4557 map_option_f
= TRUE
;
4565 lang_add_fill (fill
)
4568 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4575 lang_add_data (type
, exp
)
4577 union etree_union
*exp
;
4580 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4588 /* Create a new reloc statement. RELOC is the BFD relocation type to
4589 generate. HOWTO is the corresponding howto structure (we could
4590 look this up, but the caller has already done so). SECTION is the
4591 section to generate a reloc against, or NAME is the name of the
4592 symbol to generate a reloc against. Exactly one of SECTION and
4593 NAME must be NULL. ADDEND is an expression for the addend. */
4596 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4597 bfd_reloc_code_real_type reloc
;
4598 reloc_howto_type
*howto
;
4601 union etree_union
*addend
;
4603 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4607 p
->section
= section
;
4609 p
->addend_exp
= addend
;
4611 p
->addend_value
= 0;
4612 p
->output_section
= NULL
;
4616 lang_assignment_statement_type
*
4617 lang_add_assignment (exp
)
4620 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4628 lang_add_attribute (attribute
)
4629 enum statement_enum attribute
;
4631 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4638 if (startup_file
!= (char *) NULL
)
4640 einfo (_("%P%Fmultiple STARTUP files\n"));
4642 first_file
->filename
= name
;
4643 first_file
->local_sym_name
= name
;
4644 first_file
->real
= TRUE
;
4646 startup_file
= name
;
4653 lang_float_flag
= maybe
;
4657 /* Work out the load- and run-time regions from a script statement, and
4658 store them in *LMA_REGION and *REGION respectively.
4660 MEMSPEC is the name of the run-time region, or "*default*" if the
4661 statement didn't specify one. LMA_MEMSPEC is the name of the
4662 load-time region, or null if the statement didn't specify one.
4663 HAVE_LMA_P is TRUE if the statement had an explicit load address.
4665 It is an error to specify both a load region and a load address. */
4668 lang_get_regions (region
, lma_region
, memspec
, lma_memspec
, have_lma_p
)
4669 struct memory_region_struct
**region
, **lma_region
;
4670 const char *memspec
, *lma_memspec
;
4673 *lma_region
= lang_memory_region_lookup (lma_memspec
);
4675 /* If no runtime region has been given, but the load region has
4676 been, use the load region. */
4677 if (lma_memspec
!= 0 && strcmp (memspec
, "*default*") == 0)
4678 *region
= *lma_region
;
4680 *region
= lang_memory_region_lookup (memspec
);
4682 if (have_lma_p
&& lma_memspec
!= 0)
4683 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4687 lang_leave_output_section_statement (fill
, memspec
, phdrs
, lma_memspec
)
4689 const char *memspec
;
4690 struct lang_output_section_phdr_list
*phdrs
;
4691 const char *lma_memspec
;
4693 lang_get_regions (¤t_section
->region
,
4694 ¤t_section
->lma_region
,
4695 memspec
, lma_memspec
,
4696 current_section
->load_base
!= 0);
4697 current_section
->fill
= fill
;
4698 current_section
->phdrs
= phdrs
;
4699 stat_ptr
= &statement_list
;
4702 /* Create an absolute symbol with the given name with the value of the
4703 address of first byte of the section named.
4705 If the symbol already exists, then do nothing. */
4708 lang_abs_symbol_at_beginning_of (secname
, name
)
4709 const char *secname
;
4712 struct bfd_link_hash_entry
*h
;
4714 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4715 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4716 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4718 if (h
->type
== bfd_link_hash_new
4719 || h
->type
== bfd_link_hash_undefined
)
4723 h
->type
= bfd_link_hash_defined
;
4725 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4726 if (sec
== (asection
*) NULL
)
4729 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4731 h
->u
.def
.section
= bfd_abs_section_ptr
;
4735 /* Create an absolute symbol with the given name with the value of the
4736 address of the first byte after the end of the section named.
4738 If the symbol already exists, then do nothing. */
4741 lang_abs_symbol_at_end_of (secname
, name
)
4742 const char *secname
;
4745 struct bfd_link_hash_entry
*h
;
4747 h
= bfd_link_hash_lookup (link_info
.hash
, name
, TRUE
, TRUE
, TRUE
);
4748 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4749 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4751 if (h
->type
== bfd_link_hash_new
4752 || h
->type
== bfd_link_hash_undefined
)
4756 h
->type
= bfd_link_hash_defined
;
4758 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4759 if (sec
== (asection
*) NULL
)
4762 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4763 + bfd_section_size (output_bfd
, sec
) /
4764 bfd_octets_per_byte (output_bfd
));
4766 h
->u
.def
.section
= bfd_abs_section_ptr
;
4771 lang_statement_append (list
, element
, field
)
4772 lang_statement_list_type
*list
;
4773 lang_statement_union_type
*element
;
4774 lang_statement_union_type
**field
;
4776 *(list
->tail
) = element
;
4780 /* Set the output format type. -oformat overrides scripts. */
4783 lang_add_output_format (format
, big
, little
, from_script
)
4789 if (output_target
== NULL
|| !from_script
)
4791 if (command_line
.endian
== ENDIAN_BIG
4794 else if (command_line
.endian
== ENDIAN_LITTLE
4798 output_target
= format
;
4802 /* Enter a group. This creates a new lang_group_statement, and sets
4803 stat_ptr to build new statements within the group. */
4808 lang_group_statement_type
*g
;
4810 g
= new_stat (lang_group_statement
, stat_ptr
);
4811 lang_list_init (&g
->children
);
4812 stat_ptr
= &g
->children
;
4815 /* Leave a group. This just resets stat_ptr to start writing to the
4816 regular list of statements again. Note that this will not work if
4817 groups can occur inside anything else which can adjust stat_ptr,
4818 but currently they can't. */
4823 stat_ptr
= &statement_list
;
4826 /* Add a new program header. This is called for each entry in a PHDRS
4827 command in a linker script. */
4830 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4833 bfd_boolean filehdr
;
4838 struct lang_phdr
*n
, **pp
;
4840 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4843 n
->type
= exp_get_value_int (type
, 0, "program header type",
4844 lang_final_phase_enum
);
4845 n
->filehdr
= filehdr
;
4850 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4855 /* Record the program header information in the output BFD. FIXME: We
4856 should not be calling an ELF specific function here. */
4859 lang_record_phdrs ()
4863 struct lang_output_section_phdr_list
*last
;
4864 struct lang_phdr
*l
;
4865 lang_statement_union_type
*u
;
4868 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4870 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4877 for (u
= lang_output_section_statement
.head
;
4879 u
= u
->output_section_statement
.next
)
4881 lang_output_section_statement_type
*os
;
4882 struct lang_output_section_phdr_list
*pl
;
4884 os
= &u
->output_section_statement
;
4891 if (os
->sectype
== noload_section
4892 || os
->bfd_section
== NULL
4893 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4898 if (os
->bfd_section
== NULL
)
4901 for (; pl
!= NULL
; pl
= pl
->next
)
4903 if (strcmp (pl
->name
, l
->name
) == 0)
4908 secs
= ((asection
**)
4909 xrealloc (secs
, alc
* sizeof (asection
*)));
4911 secs
[c
] = os
->bfd_section
;
4918 if (l
->flags
== NULL
)
4921 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4922 lang_final_phase_enum
);
4927 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4928 lang_final_phase_enum
);
4930 if (! bfd_record_phdr (output_bfd
, l
->type
,
4931 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4932 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4933 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4938 /* Make sure all the phdr assignments succeeded. */
4939 for (u
= lang_output_section_statement
.head
;
4941 u
= u
->output_section_statement
.next
)
4943 struct lang_output_section_phdr_list
*pl
;
4945 if (u
->output_section_statement
.bfd_section
== NULL
)
4948 for (pl
= u
->output_section_statement
.phdrs
;
4951 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4952 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4953 u
->output_section_statement
.name
, pl
->name
);
4957 /* Record a list of sections which may not be cross referenced. */
4960 lang_add_nocrossref (l
)
4961 struct lang_nocrossref
*l
;
4963 struct lang_nocrossrefs
*n
;
4965 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4966 n
->next
= nocrossref_list
;
4968 nocrossref_list
= n
;
4970 /* Set notice_all so that we get informed about all symbols. */
4971 link_info
.notice_all
= TRUE
;
4974 /* Overlay handling. We handle overlays with some static variables. */
4976 /* The overlay virtual address. */
4977 static etree_type
*overlay_vma
;
4979 /* An expression for the maximum section size seen so far. */
4980 static etree_type
*overlay_max
;
4982 /* A list of all the sections in this overlay. */
4984 struct overlay_list
{
4985 struct overlay_list
*next
;
4986 lang_output_section_statement_type
*os
;
4989 static struct overlay_list
*overlay_list
;
4991 /* Start handling an overlay. */
4994 lang_enter_overlay (vma_expr
)
4995 etree_type
*vma_expr
;
4997 /* The grammar should prevent nested overlays from occurring. */
4998 ASSERT (overlay_vma
== NULL
&& overlay_max
== NULL
);
5000 overlay_vma
= vma_expr
;
5003 /* Start a section in an overlay. We handle this by calling
5004 lang_enter_output_section_statement with the correct VMA.
5005 lang_leave_overlay sets up the LMA and memory regions. */
5008 lang_enter_overlay_section (name
)
5011 struct overlay_list
*n
;
5014 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
5017 /* If this is the first section, then base the VMA of future
5018 sections on this one. This will work correctly even if `.' is
5019 used in the addresses. */
5020 if (overlay_list
== NULL
)
5021 overlay_vma
= exp_nameop (ADDR
, name
);
5023 /* Remember the section. */
5024 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
5025 n
->os
= current_section
;
5026 n
->next
= overlay_list
;
5029 size
= exp_nameop (SIZEOF
, name
);
5031 /* Arrange to work out the maximum section end address. */
5032 if (overlay_max
== NULL
)
5035 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
5038 /* Finish a section in an overlay. There isn't any special to do
5042 lang_leave_overlay_section (fill
, phdrs
)
5044 struct lang_output_section_phdr_list
*phdrs
;
5051 name
= current_section
->name
;
5053 /* For now, assume that "*default*" is the run-time memory region and
5054 that no load-time region has been specified. It doesn't really
5055 matter what we say here, since lang_leave_overlay will override it. */
5056 lang_leave_output_section_statement (fill
, "*default*", phdrs
, 0);
5058 /* Define the magic symbols. */
5060 clean
= xmalloc (strlen (name
) + 1);
5062 for (s1
= name
; *s1
!= '\0'; s1
++)
5063 if (ISALNUM (*s1
) || *s1
== '_')
5067 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
5068 sprintf (buf
, "__load_start_%s", clean
);
5069 lang_add_assignment (exp_assop ('=', buf
,
5070 exp_nameop (LOADADDR
, name
)));
5072 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
5073 sprintf (buf
, "__load_stop_%s", clean
);
5074 lang_add_assignment (exp_assop ('=', buf
,
5076 exp_nameop (LOADADDR
, name
),
5077 exp_nameop (SIZEOF
, name
))));
5082 /* Finish an overlay. If there are any overlay wide settings, this
5083 looks through all the sections in the overlay and sets them. */
5086 lang_leave_overlay (lma_expr
, nocrossrefs
, fill
, memspec
, phdrs
, lma_memspec
)
5087 etree_type
*lma_expr
;
5090 const char *memspec
;
5091 struct lang_output_section_phdr_list
*phdrs
;
5092 const char *lma_memspec
;
5094 lang_memory_region_type
*region
;
5095 lang_memory_region_type
*lma_region
;
5096 struct overlay_list
*l
;
5097 struct lang_nocrossref
*nocrossref
;
5099 lang_get_regions (®ion
, &lma_region
,
5100 memspec
, lma_memspec
,
5105 /* After setting the size of the last section, set '.' to end of the
5107 if (overlay_list
!= NULL
)
5108 overlay_list
->os
->update_dot_tree
5109 = exp_assop ('=', ".", exp_binop ('+', overlay_vma
, overlay_max
));
5114 struct overlay_list
*next
;
5116 if (fill
!= (fill_type
*) 0 && l
->os
->fill
== (fill_type
*) 0)
5119 l
->os
->region
= region
;
5120 l
->os
->lma_region
= lma_region
;
5122 /* The first section has the load address specified in the
5123 OVERLAY statement. The rest are worked out from that.
5124 The base address is not needed (and should be null) if
5125 an LMA region was specified. */
5127 l
->os
->load_base
= lma_expr
;
5128 else if (lma_region
== 0)
5129 l
->os
->load_base
= exp_binop ('+',
5130 exp_nameop (LOADADDR
, l
->next
->os
->name
),
5131 exp_nameop (SIZEOF
, l
->next
->os
->name
));
5133 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
5134 l
->os
->phdrs
= phdrs
;
5138 struct lang_nocrossref
*nc
;
5140 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
5141 nc
->name
= l
->os
->name
;
5142 nc
->next
= nocrossref
;
5151 if (nocrossref
!= NULL
)
5152 lang_add_nocrossref (nocrossref
);
5155 overlay_list
= NULL
;
5159 /* Version handling. This is only useful for ELF. */
5161 /* This global variable holds the version tree that we build. */
5163 struct bfd_elf_version_tree
*lang_elf_version_info
;
5166 lang_vers_match_lang_c (expr
, sym
)
5167 struct bfd_elf_version_expr
*expr
;
5170 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5172 return fnmatch (expr
->pattern
, sym
, 0) == 0;
5176 lang_vers_match_lang_cplusplus (expr
, sym
)
5177 struct bfd_elf_version_expr
*expr
;
5183 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5186 alt_sym
= cplus_demangle (sym
, /* DMGL_NO_TPARAMS */ 0);
5189 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
5190 Should we early out FALSE in this case? */
5191 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
5195 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
5203 lang_vers_match_lang_java (expr
, sym
)
5204 struct bfd_elf_version_expr
*expr
;
5210 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
5213 alt_sym
= cplus_demangle (sym
, DMGL_JAVA
);
5216 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
5217 Should we early out FALSE in this case? */
5218 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
5222 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
5229 /* This is called for each variable name or match expression. */
5231 struct bfd_elf_version_expr
*
5232 lang_new_vers_pattern (orig
, new, lang
)
5233 struct bfd_elf_version_expr
*orig
;
5237 struct bfd_elf_version_expr
*ret
;
5239 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
5245 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
5246 ret
->match
= lang_vers_match_lang_c
;
5247 else if (strcasecmp (lang
, "C++") == 0)
5248 ret
->match
= lang_vers_match_lang_cplusplus
;
5249 else if (strcasecmp (lang
, "Java") == 0)
5250 ret
->match
= lang_vers_match_lang_java
;
5253 einfo (_("%X%P: unknown language `%s' in version information\n"),
5255 ret
->match
= lang_vers_match_lang_c
;
5258 return ldemul_new_vers_pattern (ret
);
5261 /* This is called for each set of variable names and match
5264 struct bfd_elf_version_tree
*
5265 lang_new_vers_node (globals
, locals
)
5266 struct bfd_elf_version_expr
*globals
;
5267 struct bfd_elf_version_expr
*locals
;
5269 struct bfd_elf_version_tree
*ret
;
5271 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
5275 ret
->globals
= globals
;
5276 ret
->locals
= locals
;
5278 ret
->name_indx
= (unsigned int) -1;
5283 /* This static variable keeps track of version indices. */
5285 static int version_index
;
5287 /* This is called when we know the name and dependencies of the
5291 lang_register_vers_node (name
, version
, deps
)
5293 struct bfd_elf_version_tree
*version
;
5294 struct bfd_elf_version_deps
*deps
;
5296 struct bfd_elf_version_tree
*t
, **pp
;
5297 struct bfd_elf_version_expr
*e1
;
5302 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5303 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5305 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5310 /* Make sure this node has a unique name. */
5311 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5312 if (strcmp (t
->name
, name
) == 0)
5313 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5315 /* Check the global and local match names, and make sure there
5316 aren't any duplicates. */
5318 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
5320 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5322 struct bfd_elf_version_expr
*e2
;
5324 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
5325 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
5326 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5331 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
5333 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5335 struct bfd_elf_version_expr
*e2
;
5337 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
5338 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
5339 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5344 version
->deps
= deps
;
5345 version
->name
= name
;
5346 if (name
[0] != '\0')
5349 version
->vernum
= version_index
;
5352 version
->vernum
= 0;
5354 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5359 /* This is called when we see a version dependency. */
5361 struct bfd_elf_version_deps
*
5362 lang_add_vers_depend (list
, name
)
5363 struct bfd_elf_version_deps
*list
;
5366 struct bfd_elf_version_deps
*ret
;
5367 struct bfd_elf_version_tree
*t
;
5369 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
5372 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5374 if (strcmp (t
->name
, name
) == 0)
5376 ret
->version_needed
= t
;
5381 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5387 lang_do_version_exports_section ()
5389 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5391 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5393 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5400 len
= bfd_section_size (is
->the_bfd
, sec
);
5401 contents
= xmalloc (len
);
5402 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5403 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5406 while (p
< contents
+ len
)
5408 greg
= lang_new_vers_pattern (greg
, p
, NULL
);
5409 p
= strchr (p
, '\0') + 1;
5412 /* Do not free the contents, as we used them creating the regex. */
5414 /* Do not include this section in the link. */
5415 bfd_set_section_flags (is
->the_bfd
, sec
,
5416 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5419 lreg
= lang_new_vers_pattern (NULL
, "*", NULL
);
5420 lang_register_vers_node (command_line
.version_exports_section
,
5421 lang_new_vers_node (greg
, lreg
), NULL
);
5425 lang_add_unique (name
)
5428 struct unique_sections
*ent
;
5430 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5431 if (strcmp (ent
->name
, name
) == 0)
5434 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
5435 ent
->name
= xstrdup (name
);
5436 ent
->next
= unique_section_list
;
5437 unique_section_list
= ent
;