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 static lang_statement_union_type
*new_statement
45 PARAMS ((enum statement_enum
, size_t, lang_statement_list_type
*));
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 boolean placed_commons
= false;
55 static lang_output_section_statement_type
*default_common_section
;
56 static 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 static void lang_for_each_statement_worker
65 PARAMS ((void (*) (lang_statement_union_type
*),
66 lang_statement_union_type
*));
67 static lang_input_statement_type
*new_afile
68 PARAMS ((const char *, lang_input_file_enum_type
, const char *, boolean
));
69 static lang_memory_region_type
*lang_memory_default
PARAMS ((asection
*));
70 static void lang_map_flags
PARAMS ((flagword
));
71 static void init_os
PARAMS ((lang_output_section_statement_type
*));
72 static void exp_init_os
PARAMS ((etree_type
*));
73 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
74 static struct bfd_hash_entry
*already_linked_newfunc
75 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
76 static void already_linked_table_init
PARAMS ((void));
77 static void already_linked_table_free
PARAMS ((void));
78 static boolean wildcardp
PARAMS ((const char *));
79 static lang_statement_union_type
*wild_sort
80 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*,
81 lang_input_statement_type
*, asection
*));
82 static void output_section_callback
83 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*, asection
*,
84 lang_input_statement_type
*, PTR
));
85 static lang_input_statement_type
*lookup_name
PARAMS ((const char *));
86 static boolean load_symbols
87 PARAMS ((lang_input_statement_type
*, lang_statement_list_type
*));
89 PARAMS ((lang_wild_statement_type
*,
90 const char *, lang_output_section_statement_type
*));
91 static bfd
*open_output
PARAMS ((const char *));
92 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*));
93 static void open_input_bfds
PARAMS ((lang_statement_union_type
*, boolean
));
94 static void lang_reasonable_defaults
PARAMS ((void));
95 static void lang_place_undefineds
PARAMS ((void));
96 static void map_input_to_output_sections
97 PARAMS ((lang_statement_union_type
*, const char *,
98 lang_output_section_statement_type
*));
99 static void print_output_section_statement
100 PARAMS ((lang_output_section_statement_type
*));
101 static void print_assignment
102 PARAMS ((lang_assignment_statement_type
*,
103 lang_output_section_statement_type
*));
104 static void print_input_statement
PARAMS ((lang_input_statement_type
*));
105 static boolean print_one_symbol
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
106 static void print_input_section
PARAMS ((lang_input_section_type
*));
107 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*));
108 static void print_data_statement
PARAMS ((lang_data_statement_type
*));
109 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
110 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*));
111 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*));
112 static void print_wild_statement
113 PARAMS ((lang_wild_statement_type
*, lang_output_section_statement_type
*));
114 static void print_group
115 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
116 static void print_statement
117 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
118 static void print_statement_list
119 PARAMS ((lang_statement_union_type
*, lang_output_section_statement_type
*));
120 static void print_statements
PARAMS ((void));
121 static void insert_pad
122 PARAMS ((lang_statement_union_type
**, fill_type
,
123 unsigned int, asection
*, bfd_vma
));
124 static bfd_vma size_input_section
125 PARAMS ((lang_statement_union_type
**, lang_output_section_statement_type
*,
126 fill_type
, bfd_vma
));
127 static void lang_finish
PARAMS ((void));
128 static void ignore_bfd_errors
PARAMS ((const char *, ...));
129 static void lang_check
PARAMS ((void));
130 static void lang_common
PARAMS ((void));
131 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
132 static void lang_place_orphans
PARAMS ((void));
133 static int topower
PARAMS ((int));
134 static void lang_set_startof
PARAMS ((void));
135 static void reset_memory_regions
PARAMS ((void));
136 static void gc_section_callback
137 PARAMS ((lang_wild_statement_type
*, struct wildcard_list
*, asection
*,
138 lang_input_statement_type
*, PTR
));
139 static void lang_record_phdrs
PARAMS ((void));
140 static void lang_gc_wild
PARAMS ((lang_wild_statement_type
*));
141 static void lang_gc_sections_1
PARAMS ((lang_statement_union_type
*));
142 static void lang_gc_sections
PARAMS ((void));
143 static int lang_vers_match_lang_c
144 PARAMS ((struct bfd_elf_version_expr
*, const char *));
145 static int lang_vers_match_lang_cplusplus
146 PARAMS ((struct bfd_elf_version_expr
*, const char *));
147 static int lang_vers_match_lang_java
148 PARAMS ((struct bfd_elf_version_expr
*, const char *));
149 static void lang_do_version_exports_section
PARAMS ((void));
150 static void lang_check_section_addresses
PARAMS ((void));
151 static void os_region_check
152 PARAMS ((lang_output_section_statement_type
*,
153 struct memory_region_struct
*, etree_type
*, bfd_vma
));
155 typedef void (*callback_t
) PARAMS ((lang_wild_statement_type
*,
156 struct wildcard_list
*,
158 lang_input_statement_type
*,
160 static void walk_wild
161 PARAMS ((lang_wild_statement_type
*, callback_t
, PTR
));
162 static void walk_wild_section
163 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
165 static void walk_wild_file
166 PARAMS ((lang_wild_statement_type
*, lang_input_statement_type
*,
169 static int get_target
PARAMS ((const bfd_target
*, PTR
));
170 static void stricpy
PARAMS ((char *, char *));
171 static void strcut
PARAMS ((char *, char *));
172 static int name_compare
PARAMS ((char *, char *));
173 static int closest_target_match
PARAMS ((const bfd_target
*, PTR
));
174 static char * get_first_input_target
PARAMS ((void));
177 lang_output_section_statement_type
*abs_output_section
;
178 lang_statement_list_type lang_output_section_statement
;
179 lang_statement_list_type
*stat_ptr
= &statement_list
;
180 lang_statement_list_type file_chain
= { NULL
, NULL
};
181 const char *entry_symbol
= NULL
;
182 boolean entry_from_cmdline
;
183 boolean lang_has_input_file
= false;
184 boolean had_output_filename
= false;
185 boolean lang_float_flag
= false;
186 boolean delete_output_file_on_failure
= false;
187 struct lang_nocrossrefs
*nocrossref_list
;
188 struct unique_sections
*unique_section_list
;
190 etree_type
*base
; /* Relocation base - or null */
192 #if defined (__STDC__) || defined (ALMOST_STDC)
193 #define cat(a,b) a##b
195 #define cat(a,b) a/**/b
198 /* Don't beautify the line below with "innocent" whitespace, it breaks
199 the K&R C preprocessor! */
200 #define new_stat(x, y) \
201 (cat (x,_type)*) new_statement (cat (x,_enum), sizeof (cat (x,_type)), y)
203 #define outside_section_address(q) \
204 ((q)->output_offset + (q)->output_section->vma)
206 #define outside_symbol_address(q) \
207 ((q)->value + outside_section_address (q->section))
209 #define SECTION_NAME_MAP_LENGTH (16)
215 return obstack_alloc (&stat_obstack
, size
);
219 unique_section_p (secnam
)
222 struct unique_sections
*unam
;
224 for (unam
= unique_section_list
; unam
; unam
= unam
->next
)
225 if (wildcardp (unam
->name
)
226 ? fnmatch (unam
->name
, secnam
, 0) == 0
227 : strcmp (unam
->name
, secnam
) == 0)
235 /* Generic traversal routines for finding matching sections. */
238 walk_wild_section (ptr
, file
, callback
, data
)
239 lang_wild_statement_type
*ptr
;
240 lang_input_statement_type
*file
;
246 if (file
->just_syms_flag
)
249 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
251 struct wildcard_list
*sec
;
253 sec
= ptr
->section_list
;
255 (*callback
) (ptr
, sec
, s
, file
, data
);
259 boolean skip
= false;
260 struct name_list
*list_tmp
;
262 /* Don't process sections from files which were
264 for (list_tmp
= sec
->spec
.exclude_name_list
;
266 list_tmp
= list_tmp
->next
)
268 if (wildcardp (list_tmp
->name
))
269 skip
= fnmatch (list_tmp
->name
, file
->filename
, 0) == 0;
271 skip
= strcmp (list_tmp
->name
, file
->filename
) == 0;
273 /* If this file is part of an archive, and the archive is
274 excluded, exclude this file. */
275 if (! skip
&& file
->the_bfd
!= NULL
276 && file
->the_bfd
->my_archive
!= NULL
277 && file
->the_bfd
->my_archive
->filename
!= NULL
)
279 if (wildcardp (list_tmp
->name
))
280 skip
= fnmatch (list_tmp
->name
,
281 file
->the_bfd
->my_archive
->filename
,
284 skip
= strcmp (list_tmp
->name
,
285 file
->the_bfd
->my_archive
->filename
) == 0;
292 if (!skip
&& sec
->spec
.name
!= NULL
)
294 const char *sname
= bfd_get_section_name (file
->the_bfd
, s
);
296 if (wildcardp (sec
->spec
.name
))
297 skip
= fnmatch (sec
->spec
.name
, sname
, 0) != 0;
299 skip
= strcmp (sec
->spec
.name
, sname
) != 0;
303 (*callback
) (ptr
, sec
, s
, file
, data
);
310 /* Handle a wild statement for a single file F. */
313 walk_wild_file (s
, f
, callback
, data
)
314 lang_wild_statement_type
*s
;
315 lang_input_statement_type
*f
;
319 if (f
->the_bfd
== NULL
320 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
321 walk_wild_section (s
, f
, callback
, data
);
326 /* This is an archive file. We must map each member of the
327 archive separately. */
328 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
329 while (member
!= NULL
)
331 /* When lookup_name is called, it will call the add_symbols
332 entry point for the archive. For each element of the
333 archive which is included, BFD will call ldlang_add_file,
334 which will set the usrdata field of the member to the
335 lang_input_statement. */
336 if (member
->usrdata
!= NULL
)
338 walk_wild_section (s
,
339 (lang_input_statement_type
*) member
->usrdata
,
343 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
349 walk_wild (s
, callback
, data
)
350 lang_wild_statement_type
*s
;
354 const char *file_spec
= s
->filename
;
356 if (file_spec
== NULL
)
358 /* Perform the iteration over all files in the list. */
359 LANG_FOR_EACH_INPUT_STATEMENT (f
)
361 walk_wild_file (s
, f
, callback
, data
);
364 else if (wildcardp (file_spec
))
366 LANG_FOR_EACH_INPUT_STATEMENT (f
)
368 if (fnmatch (file_spec
, f
->filename
, FNM_FILE_NAME
) == 0)
369 walk_wild_file (s
, f
, callback
, data
);
374 lang_input_statement_type
*f
;
376 /* Perform the iteration over a single file. */
377 f
= lookup_name (file_spec
);
379 walk_wild_file (s
, f
, callback
, data
);
383 /* lang_for_each_statement walks the parse tree and calls the provided
384 function for each node. */
387 lang_for_each_statement_worker (func
, s
)
388 void (*func
) PARAMS ((lang_statement_union_type
*));
389 lang_statement_union_type
*s
;
391 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
395 switch (s
->header
.type
)
397 case lang_constructors_statement_enum
:
398 lang_for_each_statement_worker (func
, constructor_list
.head
);
400 case lang_output_section_statement_enum
:
401 lang_for_each_statement_worker
403 s
->output_section_statement
.children
.head
);
405 case lang_wild_statement_enum
:
406 lang_for_each_statement_worker
408 s
->wild_statement
.children
.head
);
410 case lang_group_statement_enum
:
411 lang_for_each_statement_worker (func
,
412 s
->group_statement
.children
.head
);
414 case lang_data_statement_enum
:
415 case lang_reloc_statement_enum
:
416 case lang_object_symbols_statement_enum
:
417 case lang_output_statement_enum
:
418 case lang_target_statement_enum
:
419 case lang_input_section_enum
:
420 case lang_input_statement_enum
:
421 case lang_assignment_statement_enum
:
422 case lang_padding_statement_enum
:
423 case lang_address_statement_enum
:
424 case lang_fill_statement_enum
:
434 lang_for_each_statement (func
)
435 void (*func
) PARAMS ((lang_statement_union_type
*));
437 lang_for_each_statement_worker (func
, statement_list
.head
);
440 /*----------------------------------------------------------------------*/
443 lang_list_init (list
)
444 lang_statement_list_type
*list
;
446 list
->head
= (lang_statement_union_type
*) NULL
;
447 list
->tail
= &list
->head
;
450 /* Build a new statement node for the parse tree. */
452 static lang_statement_union_type
*
453 new_statement (type
, size
, list
)
454 enum statement_enum type
;
456 lang_statement_list_type
*list
;
458 lang_statement_union_type
*new = (lang_statement_union_type
*)
461 new->header
.type
= type
;
462 new->header
.next
= (lang_statement_union_type
*) NULL
;
463 lang_statement_append (list
, new, &new->header
.next
);
467 /* Build a new input file node for the language. There are several
468 ways in which we treat an input file, eg, we only look at symbols,
469 or prefix it with a -l etc.
471 We can be supplied with requests for input files more than once;
472 they may, for example be split over serveral lines like foo.o(.text)
473 foo.o(.data) etc, so when asked for a file we check that we haven't
474 got it already so we don't duplicate the bfd. */
476 static lang_input_statement_type
*
477 new_afile (name
, file_type
, target
, add_to_list
)
479 lang_input_file_enum_type file_type
;
483 lang_input_statement_type
*p
;
486 p
= new_stat (lang_input_statement
, stat_ptr
);
489 p
= ((lang_input_statement_type
*)
490 stat_alloc (sizeof (lang_input_statement_type
)));
491 p
->header
.next
= NULL
;
494 lang_has_input_file
= true;
498 case lang_input_file_is_symbols_only_enum
:
500 p
->is_archive
= false;
502 p
->local_sym_name
= name
;
503 p
->just_syms_flag
= true;
504 p
->search_dirs_flag
= false;
506 case lang_input_file_is_fake_enum
:
508 p
->is_archive
= false;
510 p
->local_sym_name
= name
;
511 p
->just_syms_flag
= false;
512 p
->search_dirs_flag
= false;
514 case lang_input_file_is_l_enum
:
515 p
->is_archive
= true;
518 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
519 p
->just_syms_flag
= false;
520 p
->search_dirs_flag
= true;
522 case lang_input_file_is_marker_enum
:
524 p
->is_archive
= false;
526 p
->local_sym_name
= name
;
527 p
->just_syms_flag
= false;
528 p
->search_dirs_flag
= true;
530 case lang_input_file_is_search_file_enum
:
532 p
->is_archive
= false;
534 p
->local_sym_name
= name
;
535 p
->just_syms_flag
= false;
536 p
->search_dirs_flag
= true;
538 case lang_input_file_is_file_enum
:
540 p
->is_archive
= false;
542 p
->local_sym_name
= name
;
543 p
->just_syms_flag
= false;
544 p
->search_dirs_flag
= false;
549 p
->the_bfd
= (bfd
*) NULL
;
550 p
->asymbols
= (asymbol
**) NULL
;
551 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
552 p
->next
= (lang_statement_union_type
*) NULL
;
554 p
->dynamic
= config
.dynamic_link
;
555 p
->whole_archive
= whole_archive
;
557 lang_statement_append (&input_file_chain
,
558 (lang_statement_union_type
*) p
,
563 lang_input_statement_type
*
564 lang_add_input_file (name
, file_type
, target
)
566 lang_input_file_enum_type file_type
;
569 lang_has_input_file
= true;
570 return new_afile (name
, file_type
, target
, true);
573 /* Build enough state so that the parser can build its tree. */
578 obstack_begin (&stat_obstack
, 1000);
580 stat_ptr
= &statement_list
;
582 lang_list_init (stat_ptr
);
584 lang_list_init (&input_file_chain
);
585 lang_list_init (&lang_output_section_statement
);
586 lang_list_init (&file_chain
);
587 first_file
= lang_add_input_file ((char *) NULL
,
588 lang_input_file_is_marker_enum
,
591 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
593 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
597 /*----------------------------------------------------------------------
598 A region is an area of memory declared with the
599 MEMORY { name:org=exp, len=exp ... }
602 We maintain a list of all the regions here.
604 If no regions are specified in the script, then the default is used
605 which is created when looked up to be the entire data space. */
607 static lang_memory_region_type
*lang_memory_region_list
;
608 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
610 lang_memory_region_type
*
611 lang_memory_region_lookup (name
)
612 const char *const name
;
614 lang_memory_region_type
*p
;
616 for (p
= lang_memory_region_list
;
617 p
!= (lang_memory_region_type
*) NULL
;
620 if (strcmp (p
->name
, name
) == 0)
627 /* This code used to always use the first region in the list as the
628 default region. I changed it to instead use a region
629 encompassing all of memory as the default region. This permits
630 NOLOAD sections to work reasonably without requiring a region.
631 People should specify what region they mean, if they really want
633 if (strcmp (name
, "*default*") == 0)
635 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
637 return lang_memory_region_list
;
643 lang_memory_region_type
*new =
644 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
646 new->name
= xstrdup (name
);
647 new->next
= (lang_memory_region_type
*) NULL
;
649 *lang_memory_region_list_tail
= new;
650 lang_memory_region_list_tail
= &new->next
;
654 new->length
= ~(bfd_size_type
) 0;
656 new->had_full_message
= false;
662 static lang_memory_region_type
*
663 lang_memory_default (section
)
666 lang_memory_region_type
*p
;
668 flagword sec_flags
= section
->flags
;
670 /* Override SEC_DATA to mean a writable section. */
671 if ((sec_flags
& (SEC_ALLOC
| SEC_READONLY
| SEC_CODE
)) == SEC_ALLOC
)
672 sec_flags
|= SEC_DATA
;
674 for (p
= lang_memory_region_list
;
675 p
!= (lang_memory_region_type
*) NULL
;
678 if ((p
->flags
& sec_flags
) != 0
679 && (p
->not_flags
& sec_flags
) == 0)
684 return lang_memory_region_lookup ("*default*");
687 lang_output_section_statement_type
*
688 lang_output_section_find (name
)
689 const char *const name
;
691 lang_statement_union_type
*u
;
692 lang_output_section_statement_type
*lookup
;
694 for (u
= lang_output_section_statement
.head
;
695 u
!= (lang_statement_union_type
*) NULL
;
698 lookup
= &u
->output_section_statement
;
699 if (strcmp (name
, lookup
->name
) == 0)
704 return (lang_output_section_statement_type
*) NULL
;
707 lang_output_section_statement_type
*
708 lang_output_section_statement_lookup (name
)
709 const char *const name
;
711 lang_output_section_statement_type
*lookup
;
713 lookup
= lang_output_section_find (name
);
714 if (lookup
== (lang_output_section_statement_type
*) NULL
)
717 lookup
= (lang_output_section_statement_type
*)
718 new_stat (lang_output_section_statement
, stat_ptr
);
719 lookup
->region
= (lang_memory_region_type
*) NULL
;
720 lookup
->lma_region
= (lang_memory_region_type
*) NULL
;
722 lookup
->block_value
= 1;
725 lookup
->next
= (lang_statement_union_type
*) NULL
;
726 lookup
->bfd_section
= (asection
*) NULL
;
727 lookup
->processed
= false;
728 lookup
->sectype
= normal_section
;
729 lookup
->addr_tree
= (etree_type
*) NULL
;
730 lang_list_init (&lookup
->children
);
732 lookup
->memspec
= (const char *) NULL
;
734 lookup
->subsection_alignment
= -1;
735 lookup
->section_alignment
= -1;
736 lookup
->load_base
= (union etree_union
*) NULL
;
737 lookup
->phdrs
= NULL
;
739 lang_statement_append (&lang_output_section_statement
,
740 (lang_statement_union_type
*) lookup
,
747 lang_map_flags (flag
)
750 if (flag
& SEC_ALLOC
)
756 if (flag
& SEC_READONLY
)
769 lang_memory_region_type
*m
;
771 minfo (_("\nMemory Configuration\n\n"));
772 fprintf (config
.map_file
, "%-16s %-18s %-18s %s\n",
773 _("Name"), _("Origin"), _("Length"), _("Attributes"));
775 for (m
= lang_memory_region_list
;
776 m
!= (lang_memory_region_type
*) NULL
;
782 fprintf (config
.map_file
, "%-16s ", m
->name
);
784 sprintf_vma (buf
, m
->origin
);
785 minfo ("0x%s ", buf
);
793 minfo ("0x%V", m
->length
);
794 if (m
->flags
|| m
->not_flags
)
802 lang_map_flags (m
->flags
);
808 lang_map_flags (m
->not_flags
);
815 fprintf (config
.map_file
, _("\nLinker script and memory map\n\n"));
820 /* Initialize an output section. */
824 lang_output_section_statement_type
*s
;
826 section_userdata_type
*new;
828 if (s
->bfd_section
!= NULL
)
831 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
832 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME
);
834 new = ((section_userdata_type
*)
835 stat_alloc (sizeof (section_userdata_type
)));
837 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
838 if (s
->bfd_section
== (asection
*) NULL
)
839 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
840 if (s
->bfd_section
== (asection
*) NULL
)
842 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
843 output_bfd
->xvec
->name
, s
->name
);
845 s
->bfd_section
->output_section
= s
->bfd_section
;
847 /* We initialize an output sections output offset to minus its own
848 vma to allow us to output a section through itself. */
849 s
->bfd_section
->output_offset
= 0;
850 get_userdata (s
->bfd_section
) = (PTR
) new;
852 /* If there is a base address, make sure that any sections it might
853 mention are initialized. */
854 if (s
->addr_tree
!= NULL
)
855 exp_init_os (s
->addr_tree
);
858 /* Make sure that all output sections mentioned in an expression are
865 switch (exp
->type
.node_class
)
868 exp_init_os (exp
->assign
.src
);
872 exp_init_os (exp
->binary
.lhs
);
873 exp_init_os (exp
->binary
.rhs
);
877 exp_init_os (exp
->trinary
.cond
);
878 exp_init_os (exp
->trinary
.lhs
);
879 exp_init_os (exp
->trinary
.rhs
);
883 exp_init_os (exp
->unary
.child
);
887 switch (exp
->type
.node_code
)
893 lang_output_section_statement_type
*os
;
895 os
= lang_output_section_find (exp
->name
.name
);
896 if (os
!= NULL
&& os
->bfd_section
== NULL
)
907 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
908 once into the output. This routine checks each section, and
909 arrange to discard it if a section of the same name has already
910 been linked. If the section has COMDAT information, then it uses
911 that to decide whether the section should be included. This code
912 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
913 that is, it does not depend solely upon the section name.
914 section_already_linked is called via bfd_map_over_sections. */
916 /* This is the shape of the elements inside the already_linked hash
917 table. It maps a name onto a list of already_linked elements with
918 the same name. It's possible to get more than one element in a
919 list if the COMDAT sections have different names. */
921 struct already_linked_hash_entry
923 struct bfd_hash_entry root
;
924 struct already_linked
*entry
;
927 struct already_linked
929 struct already_linked
*next
;
933 /* The hash table. */
935 static struct bfd_hash_table already_linked_table
;
938 section_already_linked (abfd
, sec
, data
)
943 lang_input_statement_type
*entry
= (lang_input_statement_type
*) data
;
946 struct already_linked
*l
;
947 struct already_linked_hash_entry
*already_linked_list
;
949 /* If we are only reading symbols from this object, then we want to
950 discard all sections. */
951 if (entry
->just_syms_flag
)
953 sec
->output_section
= bfd_abs_section_ptr
;
954 sec
->output_offset
= sec
->vma
;
958 flags
= bfd_get_section_flags (abfd
, sec
);
960 if ((flags
& SEC_LINK_ONCE
) == 0)
963 /* FIXME: When doing a relocatable link, we may have trouble
964 copying relocations in other sections that refer to local symbols
965 in the section being discarded. Those relocations will have to
966 be converted somehow; as of this writing I'm not sure that any of
967 the backends handle that correctly.
969 It is tempting to instead not discard link once sections when
970 doing a relocatable link (technically, they should be discarded
971 whenever we are building constructors). However, that fails,
972 because the linker winds up combining all the link once sections
973 into a single large link once section, which defeats the purpose
974 of having link once sections in the first place.
976 Also, not merging link once sections in a relocatable link
977 causes trouble for MIPS ELF, which relies in link once semantics
978 to handle the .reginfo section correctly. */
980 name
= bfd_get_section_name (abfd
, sec
);
982 already_linked_list
=
983 ((struct already_linked_hash_entry
*)
984 bfd_hash_lookup (&already_linked_table
, name
, true, false));
986 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
988 if (sec
->comdat
== NULL
989 || l
->sec
->comdat
== NULL
990 || strcmp (sec
->comdat
->name
, l
->sec
->comdat
->name
) == 0)
992 /* The section has already been linked. See if we should
994 switch (flags
& SEC_LINK_DUPLICATES
)
999 case SEC_LINK_DUPLICATES_DISCARD
:
1002 case SEC_LINK_DUPLICATES_ONE_ONLY
:
1003 if (sec
->comdat
== NULL
)
1004 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
1007 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
1008 abfd
, name
, sec
->comdat
->name
);
1011 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
1012 /* FIXME: We should really dig out the contents of both
1013 sections and memcmp them. The COFF/PE spec says that
1014 the Microsoft linker does not implement this
1015 correctly, so I'm not going to bother doing it
1018 case SEC_LINK_DUPLICATES_SAME_SIZE
:
1019 if (bfd_section_size (abfd
, sec
)
1020 != bfd_section_size (l
->sec
->owner
, l
->sec
))
1021 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
1026 /* Set the output_section field so that lang_add_section
1027 does not create a lang_input_section structure for this
1029 sec
->output_section
= bfd_abs_section_ptr
;
1035 /* This is the first section with this name. Record it. Allocate
1036 the memory from the same obstack as the hash table is kept in. */
1038 l
= ((struct already_linked
*)
1039 bfd_hash_allocate (&already_linked_table
, sizeof *l
));
1042 l
->next
= already_linked_list
->entry
;
1043 already_linked_list
->entry
= l
;
1046 /* Support routines for the hash table used by section_already_linked,
1047 initialize the table, fill in an entry and remove the table. */
1049 static struct bfd_hash_entry
*
1050 already_linked_newfunc (entry
, table
, string
)
1051 struct bfd_hash_entry
*entry ATTRIBUTE_UNUSED
;
1052 struct bfd_hash_table
*table
;
1053 const char *string ATTRIBUTE_UNUSED
;
1055 struct already_linked_hash_entry
*ret
=
1056 bfd_hash_allocate (table
, sizeof (struct already_linked_hash_entry
));
1060 return (struct bfd_hash_entry
*) ret
;
1064 already_linked_table_init ()
1066 if (! bfd_hash_table_init_n (&already_linked_table
,
1067 already_linked_newfunc
,
1069 einfo (_("%P%F: Failed to create hash table\n"));
1073 already_linked_table_free ()
1075 bfd_hash_table_free (&already_linked_table
);
1078 /* The wild routines.
1080 These expand statements like *(.text) and foo.o to a list of
1081 explicit actions, like foo.o(.text), bar.o(.text) and
1082 foo.o(.text, .data). */
1084 /* Return true if the PATTERN argument is a wildcard pattern.
1085 Although backslashes are treated specially if a pattern contains
1086 wildcards, we do not consider the mere presence of a backslash to
1087 be enough to cause the pattern to be treated as a wildcard.
1088 That lets us handle DOS filenames more naturally. */
1092 const char *pattern
;
1096 for (s
= pattern
; *s
!= '\0'; ++s
)
1104 /* Add SECTION to the output section OUTPUT. Do this by creating a
1105 lang_input_section statement which is placed at PTR. FILE is the
1106 input file which holds SECTION. */
1109 lang_add_section (ptr
, section
, output
, file
)
1110 lang_statement_list_type
*ptr
;
1112 lang_output_section_statement_type
*output
;
1113 lang_input_statement_type
*file
;
1118 flags
= bfd_get_section_flags (section
->owner
, section
);
1122 /* If we are doing a final link, discard sections marked with
1124 if (! link_info
.relocateable
1125 && (flags
& SEC_EXCLUDE
) != 0)
1128 /* Discard input sections which are assigned to a section named
1129 DISCARD_SECTION_NAME. */
1130 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
1133 /* Discard debugging sections if we are stripping debugging
1135 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
1136 && (flags
& SEC_DEBUGGING
) != 0)
1141 if (section
->output_section
== NULL
)
1143 /* This prevents future calls from assigning this section. */
1144 section
->output_section
= bfd_abs_section_ptr
;
1149 if (section
->output_section
== NULL
)
1152 lang_input_section_type
*new;
1155 if (output
->bfd_section
== NULL
)
1158 first
= ! output
->bfd_section
->linker_has_input
;
1159 output
->bfd_section
->linker_has_input
= 1;
1161 /* Add a section reference to the list. */
1162 new = new_stat (lang_input_section
, ptr
);
1164 new->section
= section
;
1166 section
->output_section
= output
->bfd_section
;
1168 flags
= section
->flags
;
1170 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1171 to an output section, because we want to be able to include a
1172 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1173 section (I don't know why we want to do this, but we do).
1174 build_link_order in ldwrite.c handles this case by turning
1175 the embedded SEC_NEVER_LOAD section into a fill. */
1177 flags
&= ~ SEC_NEVER_LOAD
;
1179 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1180 already been processed. One reason to do this is that on pe
1181 format targets, .text$foo sections go into .text and it's odd
1182 to see .text with SEC_LINK_ONCE set. */
1184 if (! link_info
.relocateable
)
1185 flags
&= ~ (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
);
1187 /* If this is not the first input section, and the SEC_READONLY
1188 flag is not currently set, then don't set it just because the
1189 input section has it set. */
1191 if (! first
&& (section
->output_section
->flags
& SEC_READONLY
) == 0)
1192 flags
&= ~ SEC_READONLY
;
1194 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1196 && ((section
->output_section
->flags
& (SEC_MERGE
| SEC_STRINGS
))
1197 != (flags
& (SEC_MERGE
| SEC_STRINGS
))
1198 || ((flags
& SEC_MERGE
)
1199 && section
->output_section
->entsize
!= section
->entsize
)))
1201 section
->output_section
->flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1202 flags
&= ~ (SEC_MERGE
| SEC_STRINGS
);
1205 section
->output_section
->flags
|= flags
;
1207 if (flags
& SEC_MERGE
)
1208 section
->output_section
->entsize
= section
->entsize
;
1210 /* If SEC_READONLY is not set in the input section, then clear
1211 it from the output section. */
1212 if ((section
->flags
& SEC_READONLY
) == 0)
1213 section
->output_section
->flags
&= ~SEC_READONLY
;
1215 switch (output
->sectype
)
1217 case normal_section
:
1222 case overlay_section
:
1223 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
1225 case noload_section
:
1226 output
->bfd_section
->flags
&= ~SEC_LOAD
;
1227 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
1231 /* Copy over SEC_SMALL_DATA. */
1232 if (section
->flags
& SEC_SMALL_DATA
)
1233 section
->output_section
->flags
|= SEC_SMALL_DATA
;
1235 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
1236 output
->bfd_section
->alignment_power
= section
->alignment_power
;
1238 /* If supplied an aligment, then force it. */
1239 if (output
->section_alignment
!= -1)
1240 output
->bfd_section
->alignment_power
= output
->section_alignment
;
1242 if (section
->flags
& SEC_BLOCK
)
1244 section
->output_section
->flags
|= SEC_BLOCK
;
1245 /* FIXME: This value should really be obtained from the bfd... */
1246 output
->block_value
= 128;
1251 /* Handle wildcard sorting. This returns the lang_input_section which
1252 should follow the one we are going to create for SECTION and FILE,
1253 based on the sorting requirements of WILD. It returns NULL if the
1254 new section should just go at the end of the current list. */
1256 static lang_statement_union_type
*
1257 wild_sort (wild
, sec
, file
, section
)
1258 lang_wild_statement_type
*wild
;
1259 struct wildcard_list
*sec
;
1260 lang_input_statement_type
*file
;
1263 const char *section_name
;
1264 lang_statement_union_type
*l
;
1266 if (!wild
->filenames_sorted
&& (sec
== NULL
|| !sec
->spec
.sorted
))
1269 section_name
= bfd_get_section_name (file
->the_bfd
, section
);
1270 for (l
= wild
->children
.head
; l
!= NULL
; l
= l
->header
.next
)
1272 lang_input_section_type
*ls
;
1274 if (l
->header
.type
!= lang_input_section_enum
)
1276 ls
= &l
->input_section
;
1278 /* Sorting by filename takes precedence over sorting by section
1281 if (wild
->filenames_sorted
)
1283 const char *fn
, *ln
;
1287 /* The PE support for the .idata section as generated by
1288 dlltool assumes that files will be sorted by the name of
1289 the archive and then the name of the file within the
1292 if (file
->the_bfd
!= NULL
1293 && bfd_my_archive (file
->the_bfd
) != NULL
)
1295 fn
= bfd_get_filename (bfd_my_archive (file
->the_bfd
));
1300 fn
= file
->filename
;
1304 if (ls
->ifile
->the_bfd
!= NULL
1305 && bfd_my_archive (ls
->ifile
->the_bfd
) != NULL
)
1307 ln
= bfd_get_filename (bfd_my_archive (ls
->ifile
->the_bfd
));
1312 ln
= ls
->ifile
->filename
;
1316 i
= strcmp (fn
, ln
);
1325 fn
= file
->filename
;
1327 ln
= ls
->ifile
->filename
;
1329 i
= strcmp (fn
, ln
);
1337 /* Here either the files are not sorted by name, or we are
1338 looking at the sections for this file. */
1340 if (sec
!= NULL
&& sec
->spec
.sorted
)
1342 if (strcmp (section_name
,
1343 bfd_get_section_name (ls
->ifile
->the_bfd
,
1353 /* Expand a wild statement for a particular FILE. SECTION may be
1354 NULL, in which case it is a wild card. */
1357 output_section_callback (ptr
, sec
, section
, file
, output
)
1358 lang_wild_statement_type
*ptr
;
1359 struct wildcard_list
*sec
;
1361 lang_input_statement_type
*file
;
1364 lang_statement_union_type
*before
;
1366 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1367 if (unique_section_p (bfd_get_section_name (file
->the_bfd
, section
)))
1370 /* If the wild pattern was marked KEEP, the member sections
1371 should be as well. */
1372 if (ptr
->keep_sections
)
1373 section
->flags
|= SEC_KEEP
;
1375 before
= wild_sort (ptr
, sec
, file
, section
);
1377 /* Here BEFORE points to the lang_input_section which
1378 should follow the one we are about to add. If BEFORE
1379 is NULL, then the section should just go at the end
1380 of the current list. */
1383 lang_add_section (&ptr
->children
, section
,
1384 (lang_output_section_statement_type
*) output
,
1388 lang_statement_list_type list
;
1389 lang_statement_union_type
**pp
;
1391 lang_list_init (&list
);
1392 lang_add_section (&list
, section
,
1393 (lang_output_section_statement_type
*) output
,
1396 /* If we are discarding the section, LIST.HEAD will
1398 if (list
.head
!= NULL
)
1400 ASSERT (list
.head
->header
.next
== NULL
);
1402 for (pp
= &ptr
->children
.head
;
1404 pp
= &(*pp
)->header
.next
)
1405 ASSERT (*pp
!= NULL
);
1407 list
.head
->header
.next
= *pp
;
1413 /* This is passed a file name which must have been seen already and
1414 added to the statement tree. We will see if it has been opened
1415 already and had its symbols read. If not then we'll read it. */
1417 static lang_input_statement_type
*
1421 lang_input_statement_type
*search
;
1423 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
1424 search
!= (lang_input_statement_type
*) NULL
;
1425 search
= (lang_input_statement_type
*) search
->next_real_file
)
1427 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
1429 if (search
->filename
!= (char *) NULL
1430 && name
!= (char *) NULL
1431 && strcmp (search
->filename
, name
) == 0)
1435 if (search
== (lang_input_statement_type
*) NULL
)
1436 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
1439 /* If we have already added this file, or this file is not real
1440 (FIXME: can that ever actually happen?) or the name is NULL
1441 (FIXME: can that ever actually happen?) don't add this file. */
1444 || search
->filename
== (const char *) NULL
)
1447 if (! load_symbols (search
, (lang_statement_list_type
*) NULL
))
1453 /* Get the symbols for an input file. */
1456 load_symbols (entry
, place
)
1457 lang_input_statement_type
*entry
;
1458 lang_statement_list_type
*place
;
1465 ldfile_open_file (entry
);
1467 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
1468 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
1471 lang_statement_list_type
*hold
;
1472 boolean bad_load
= true;
1474 err
= bfd_get_error ();
1476 /* See if the emulation has some special knowledge. */
1477 if (ldemul_unrecognized_file (entry
))
1480 if (err
== bfd_error_file_ambiguously_recognized
)
1484 einfo (_("%B: file not recognized: %E\n"), entry
->the_bfd
);
1485 einfo (_("%B: matching formats:"), entry
->the_bfd
);
1486 for (p
= matching
; *p
!= NULL
; p
++)
1490 else if (err
!= bfd_error_file_not_recognized
1492 einfo (_("%F%B: file not recognized: %E\n"), entry
->the_bfd
);
1496 bfd_close (entry
->the_bfd
);
1497 entry
->the_bfd
= NULL
;
1499 /* Try to interpret the file as a linker script. */
1500 ldfile_open_command_file (entry
->filename
);
1505 ldfile_assumed_script
= true;
1506 parser_input
= input_script
;
1508 ldfile_assumed_script
= false;
1515 if (ldemul_recognized_file (entry
))
1518 /* We don't call ldlang_add_file for an archive. Instead, the
1519 add_symbols entry point will call ldlang_add_file, via the
1520 add_archive_element callback, for each element of the archive
1522 switch (bfd_get_format (entry
->the_bfd
))
1528 ldlang_add_file (entry
);
1529 if (trace_files
|| trace_file_tries
)
1530 info_msg ("%I\n", entry
);
1534 if (entry
->whole_archive
)
1536 bfd
* member
= NULL
;
1537 boolean loaded
= true;
1541 member
= bfd_openr_next_archived_file (entry
->the_bfd
, member
);
1546 if (! bfd_check_format (member
, bfd_object
))
1548 einfo (_("%F%B: member %B in archive is not an object\n"),
1549 entry
->the_bfd
, member
);
1553 if (! ((*link_info
.callbacks
->add_archive_element
)
1554 (&link_info
, member
, "--whole-archive")))
1557 if (! bfd_link_add_symbols (member
, &link_info
))
1559 einfo (_("%F%B: could not read symbols: %E\n"), member
);
1564 entry
->loaded
= loaded
;
1570 if (bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1571 entry
->loaded
= true;
1573 einfo (_("%F%B: could not read symbols: %E\n"), entry
->the_bfd
);
1575 return entry
->loaded
;
1578 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1579 may be NULL, indicating that it is a wildcard. Separate
1580 lang_input_section statements are created for each part of the
1581 expansion; they are added after the wild statement S. OUTPUT is
1582 the output section. */
1585 wild (s
, target
, output
)
1586 lang_wild_statement_type
*s
;
1587 const char *target ATTRIBUTE_UNUSED
;
1588 lang_output_section_statement_type
*output
;
1590 struct wildcard_list
*sec
;
1592 walk_wild (s
, output_section_callback
, (PTR
) output
);
1594 for (sec
= s
->section_list
; sec
!= NULL
; sec
= sec
->next
)
1596 if (default_common_section
!= NULL
)
1598 if (sec
->spec
.name
!= NULL
&& strcmp (sec
->spec
.name
, "COMMON") == 0)
1600 /* Remember the section that common is going to in case we
1601 later get something which doesn't know where to put it. */
1602 default_common_section
= output
;
1607 /* Return true iff target is the sought target. */
1610 get_target (target
, data
)
1611 const bfd_target
*target
;
1614 const char *sought
= (const char *) data
;
1616 return strcmp (target
->name
, sought
) == 0;
1619 /* Like strcpy() but convert to lower case as well. */
1628 while ((c
= *src
++) != 0)
1629 *dest
++ = TOLOWER (c
);
1634 /* Remove the first occurance of needle (if any) in haystack
1638 strcut (haystack
, needle
)
1642 haystack
= strstr (haystack
, needle
);
1648 for (src
= haystack
+ strlen (needle
); *src
;)
1649 *haystack
++ = *src
++;
1655 /* Compare two target format name strings.
1656 Return a value indicating how "similar" they are. */
1659 name_compare (first
, second
)
1667 copy1
= xmalloc (strlen (first
) + 1);
1668 copy2
= xmalloc (strlen (second
) + 1);
1670 /* Convert the names to lower case. */
1671 stricpy (copy1
, first
);
1672 stricpy (copy2
, second
);
1674 /* Remove and endian strings from the name. */
1675 strcut (copy1
, "big");
1676 strcut (copy1
, "little");
1677 strcut (copy2
, "big");
1678 strcut (copy2
, "little");
1680 /* Return a value based on how many characters match,
1681 starting from the beginning. If both strings are
1682 the same then return 10 * their length. */
1683 for (result
= 0; copy1
[result
] == copy2
[result
]; result
++)
1684 if (copy1
[result
] == 0)
1696 /* Set by closest_target_match() below. */
1697 static const bfd_target
*winner
;
1699 /* Scan all the valid bfd targets looking for one that has the endianness
1700 requirement that was specified on the command line, and is the nearest
1701 match to the original output target. */
1704 closest_target_match (target
, data
)
1705 const bfd_target
*target
;
1708 const bfd_target
*original
= (const bfd_target
*) data
;
1710 if (command_line
.endian
== ENDIAN_BIG
1711 && target
->byteorder
!= BFD_ENDIAN_BIG
)
1714 if (command_line
.endian
== ENDIAN_LITTLE
1715 && target
->byteorder
!= BFD_ENDIAN_LITTLE
)
1718 /* Must be the same flavour. */
1719 if (target
->flavour
!= original
->flavour
)
1722 /* If we have not found a potential winner yet, then record this one. */
1729 /* Oh dear, we now have two potential candidates for a successful match.
1730 Compare their names and choose the better one. */
1731 if (name_compare (target
->name
, original
->name
)
1732 > name_compare (winner
->name
, original
->name
))
1735 /* Keep on searching until wqe have checked them all. */
1739 /* Return the BFD target format of the first input file. */
1742 get_first_input_target ()
1744 char *target
= NULL
;
1746 LANG_FOR_EACH_INPUT_STATEMENT (s
)
1748 if (s
->header
.type
== lang_input_statement_enum
1751 ldfile_open_file (s
);
1753 if (s
->the_bfd
!= NULL
1754 && bfd_check_format (s
->the_bfd
, bfd_object
))
1756 target
= bfd_get_target (s
->the_bfd
);
1767 /* Open the output file. */
1775 /* Has the user told us which output format to use? */
1776 if (output_target
== (char *) NULL
)
1778 /* No - has the current target been set to something other than
1780 if (current_target
!= default_target
)
1781 output_target
= current_target
;
1783 /* No - can we determine the format of the first input file? */
1786 output_target
= get_first_input_target ();
1788 /* Failed - use the default output target. */
1789 if (output_target
== NULL
)
1790 output_target
= default_target
;
1794 /* Has the user requested a particular endianness on the command
1796 if (command_line
.endian
!= ENDIAN_UNSET
)
1798 const bfd_target
*target
;
1799 enum bfd_endian desired_endian
;
1801 /* Get the chosen target. */
1802 target
= bfd_search_for_target (get_target
, (PTR
) output_target
);
1804 /* If the target is not supported, we cannot do anything. */
1807 if (command_line
.endian
== ENDIAN_BIG
)
1808 desired_endian
= BFD_ENDIAN_BIG
;
1810 desired_endian
= BFD_ENDIAN_LITTLE
;
1812 /* See if the target has the wrong endianness. This should
1813 not happen if the linker script has provided big and
1814 little endian alternatives, but some scrips don't do
1816 if (target
->byteorder
!= desired_endian
)
1818 /* If it does, then see if the target provides
1819 an alternative with the correct endianness. */
1820 if (target
->alternative_target
!= NULL
1821 && (target
->alternative_target
->byteorder
== desired_endian
))
1822 output_target
= target
->alternative_target
->name
;
1825 /* Try to find a target as similar as possible to
1826 the default target, but which has the desired
1827 endian characteristic. */
1828 (void) bfd_search_for_target (closest_target_match
,
1831 /* Oh dear - we could not find any targets that
1832 satisfy our requirements. */
1834 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1836 output_target
= winner
->name
;
1842 output
= bfd_openw (name
, output_target
);
1844 if (output
== (bfd
*) NULL
)
1846 if (bfd_get_error () == bfd_error_invalid_target
)
1847 einfo (_("%P%F: target %s not found\n"), output_target
);
1849 einfo (_("%P%F: cannot open output file %s: %E\n"), name
);
1852 delete_output_file_on_failure
= true;
1855 output
->flags
|= D_PAGED
;
1858 if (! bfd_set_format (output
, bfd_object
))
1859 einfo (_("%P%F:%s: can not make object file: %E\n"), name
);
1860 if (! bfd_set_arch_mach (output
,
1861 ldfile_output_architecture
,
1862 ldfile_output_machine
))
1863 einfo (_("%P%F:%s: can not set architecture: %E\n"), name
);
1865 link_info
.hash
= bfd_link_hash_table_create (output
);
1866 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1867 einfo (_("%P%F: can not create link hash table: %E\n"));
1869 bfd_set_gp_size (output
, g_switch_value
);
1874 ldlang_open_output (statement
)
1875 lang_statement_union_type
*statement
;
1877 switch (statement
->header
.type
)
1879 case lang_output_statement_enum
:
1880 ASSERT (output_bfd
== (bfd
*) NULL
);
1881 output_bfd
= open_output (statement
->output_statement
.name
);
1882 ldemul_set_output_arch ();
1883 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1884 output_bfd
->flags
|= D_PAGED
;
1886 output_bfd
->flags
&= ~D_PAGED
;
1887 if (config
.text_read_only
)
1888 output_bfd
->flags
|= WP_TEXT
;
1890 output_bfd
->flags
&= ~WP_TEXT
;
1891 if (link_info
.traditional_format
)
1892 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1894 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1897 case lang_target_statement_enum
:
1898 current_target
= statement
->target_statement
.target
;
1905 /* Open all the input files. */
1908 open_input_bfds (s
, force
)
1909 lang_statement_union_type
*s
;
1912 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
1914 switch (s
->header
.type
)
1916 case lang_constructors_statement_enum
:
1917 open_input_bfds (constructor_list
.head
, force
);
1919 case lang_output_section_statement_enum
:
1920 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1922 case lang_wild_statement_enum
:
1923 /* Maybe we should load the file's symbols. */
1924 if (s
->wild_statement
.filename
1925 && ! wildcardp (s
->wild_statement
.filename
))
1926 (void) lookup_name (s
->wild_statement
.filename
);
1927 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1929 case lang_group_statement_enum
:
1931 struct bfd_link_hash_entry
*undefs
;
1933 /* We must continually search the entries in the group
1934 until no new symbols are added to the list of undefined
1939 undefs
= link_info
.hash
->undefs_tail
;
1940 open_input_bfds (s
->group_statement
.children
.head
, true);
1942 while (undefs
!= link_info
.hash
->undefs_tail
);
1945 case lang_target_statement_enum
:
1946 current_target
= s
->target_statement
.target
;
1948 case lang_input_statement_enum
:
1949 if (s
->input_statement
.real
)
1951 lang_statement_list_type add
;
1953 s
->input_statement
.target
= current_target
;
1955 /* If we are being called from within a group, and this
1956 is an archive which has already been searched, then
1957 force it to be researched unless the whole archive
1958 has been loaded already. */
1960 && !s
->input_statement
.whole_archive
1961 && s
->input_statement
.loaded
1962 && bfd_check_format (s
->input_statement
.the_bfd
,
1964 s
->input_statement
.loaded
= false;
1966 lang_list_init (&add
);
1968 if (! load_symbols (&s
->input_statement
, &add
))
1969 config
.make_executable
= false;
1971 if (add
.head
!= NULL
)
1973 *add
.tail
= s
->header
.next
;
1974 s
->header
.next
= add
.head
;
1984 /* If there are [COMMONS] statements, put a wild one into the bss
1988 lang_reasonable_defaults ()
1991 lang_output_section_statement_lookup (".text");
1992 lang_output_section_statement_lookup (".data");
1994 default_common_section
= lang_output_section_statement_lookup (".bss");
1996 if (placed_commons
== false)
1998 lang_wild_statement_type
*new =
1999 new_stat (lang_wild_statement
,
2000 &default_common_section
->children
);
2002 new->section_name
= "COMMON";
2003 new->filename
= (char *) NULL
;
2004 lang_list_init (&new->children
);
2009 /* Add the supplied name to the symbol table as an undefined reference.
2010 Remove items from the chain as we open input bfds. */
2011 typedef struct ldlang_undef_chain_list
2013 struct ldlang_undef_chain_list
*next
;
2015 } ldlang_undef_chain_list_type
;
2017 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
2020 ldlang_add_undef (name
)
2021 const char *const name
;
2023 ldlang_undef_chain_list_type
*new =
2024 ((ldlang_undef_chain_list_type
*)
2025 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
2027 new->next
= ldlang_undef_chain_list_head
;
2028 ldlang_undef_chain_list_head
= new;
2030 new->name
= xstrdup (name
);
2033 /* Run through the list of undefineds created above and place them
2034 into the linker hash table as undefined symbols belonging to the
2038 lang_place_undefineds ()
2040 ldlang_undef_chain_list_type
*ptr
;
2042 for (ptr
= ldlang_undef_chain_list_head
;
2043 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
2046 struct bfd_link_hash_entry
*h
;
2048 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
2049 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2050 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2051 if (h
->type
== bfd_link_hash_new
)
2053 h
->type
= bfd_link_hash_undefined
;
2054 h
->u
.undef
.abfd
= NULL
;
2055 bfd_link_add_undef (link_info
.hash
, h
);
2060 /* Open input files and attatch to output sections. */
2063 map_input_to_output_sections (s
, target
, output_section_statement
)
2064 lang_statement_union_type
*s
;
2066 lang_output_section_statement_type
*output_section_statement
;
2068 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
2070 switch (s
->header
.type
)
2072 case lang_wild_statement_enum
:
2073 wild (&s
->wild_statement
, target
, output_section_statement
);
2075 case lang_constructors_statement_enum
:
2076 map_input_to_output_sections (constructor_list
.head
,
2078 output_section_statement
);
2080 case lang_output_section_statement_enum
:
2081 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
2083 &s
->output_section_statement
);
2085 case lang_output_statement_enum
:
2087 case lang_target_statement_enum
:
2088 target
= s
->target_statement
.target
;
2090 case lang_group_statement_enum
:
2091 map_input_to_output_sections (s
->group_statement
.children
.head
,
2093 output_section_statement
);
2095 case lang_fill_statement_enum
:
2096 case lang_input_section_enum
:
2097 case lang_object_symbols_statement_enum
:
2098 case lang_data_statement_enum
:
2099 case lang_reloc_statement_enum
:
2100 case lang_padding_statement_enum
:
2101 case lang_input_statement_enum
:
2102 if (output_section_statement
!= NULL
2103 && output_section_statement
->bfd_section
== NULL
)
2104 init_os (output_section_statement
);
2106 case lang_assignment_statement_enum
:
2107 if (output_section_statement
!= NULL
2108 && output_section_statement
->bfd_section
== NULL
)
2109 init_os (output_section_statement
);
2111 /* Make sure that any sections mentioned in the assignment
2113 exp_init_os (s
->assignment_statement
.exp
);
2115 case lang_afile_asection_pair_statement_enum
:
2118 case lang_address_statement_enum
:
2119 /* Mark the specified section with the supplied address. */
2121 lang_output_section_statement_type
*os
=
2122 lang_output_section_statement_lookup
2123 (s
->address_statement
.section_name
);
2125 if (os
->bfd_section
== NULL
)
2127 os
->addr_tree
= s
->address_statement
.address
;
2135 print_output_section_statement (output_section_statement
)
2136 lang_output_section_statement_type
*output_section_statement
;
2138 asection
*section
= output_section_statement
->bfd_section
;
2141 if (output_section_statement
!= abs_output_section
)
2143 minfo ("\n%s", output_section_statement
->name
);
2145 if (section
!= NULL
)
2147 print_dot
= section
->vma
;
2149 len
= strlen (output_section_statement
->name
);
2150 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2155 while (len
< SECTION_NAME_MAP_LENGTH
)
2161 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
2163 if (output_section_statement
->load_base
!= NULL
)
2167 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
2168 "load base", lang_final_phase_enum
);
2169 minfo (_(" load address 0x%V"), addr
);
2176 print_statement_list (output_section_statement
->children
.head
,
2177 output_section_statement
);
2181 print_assignment (assignment
, output_section
)
2182 lang_assignment_statement_type
*assignment
;
2183 lang_output_section_statement_type
*output_section
;
2186 etree_value_type result
;
2188 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2191 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
2192 lang_final_phase_enum
, print_dot
, &print_dot
);
2194 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
2205 exp_print_tree (assignment
->exp
);
2211 print_input_statement (statm
)
2212 lang_input_statement_type
*statm
;
2214 if (statm
->filename
!= (char *) NULL
)
2216 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
2220 /* Print all symbols defined in a particular section. This is called
2221 via bfd_link_hash_traverse. */
2224 print_one_symbol (hash_entry
, ptr
)
2225 struct bfd_link_hash_entry
*hash_entry
;
2228 asection
*sec
= (asection
*) ptr
;
2230 if ((hash_entry
->type
== bfd_link_hash_defined
2231 || hash_entry
->type
== bfd_link_hash_defweak
)
2232 && sec
== hash_entry
->u
.def
.section
)
2236 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2239 (hash_entry
->u
.def
.value
2240 + hash_entry
->u
.def
.section
->output_offset
2241 + hash_entry
->u
.def
.section
->output_section
->vma
));
2243 minfo (" %T\n", hash_entry
->root
.string
);
2249 /* Print information about an input section to the map file. */
2252 print_input_section (in
)
2253 lang_input_section_type
*in
;
2255 asection
*i
= in
->section
;
2256 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
2257 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2258 ldfile_output_machine
);
2263 minfo ("%s", i
->name
);
2265 if (i
->output_section
!= NULL
)
2269 len
= 1 + strlen (i
->name
);
2270 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
2275 while (len
< SECTION_NAME_MAP_LENGTH
)
2281 minfo ("0x%V %W %B\n",
2282 i
->output_section
->vma
+ i
->output_offset
, size
/ opb
,
2285 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
2287 len
= SECTION_NAME_MAP_LENGTH
+ 3;
2299 minfo (_("%W (size before relaxing)\n"), i
->_raw_size
);
2302 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
2304 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
/ opb
;
2310 print_fill_statement (fill
)
2311 lang_fill_statement_type
*fill
;
2313 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
2317 print_data_statement (data
)
2318 lang_data_statement_type
*data
;
2324 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2325 ldfile_output_machine
);
2327 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2330 addr
= data
->output_vma
;
2331 if (data
->output_section
!= NULL
)
2332 addr
+= data
->output_section
->vma
;
2360 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
2362 if (data
->exp
->type
.node_class
!= etree_value
)
2365 exp_print_tree (data
->exp
);
2370 print_dot
= addr
+ size
/ opb
;
2374 /* Print an address statement. These are generated by options like
2378 print_address_statement (address
)
2379 lang_address_statement_type
*address
;
2381 minfo (_("Address of section %s set to "), address
->section_name
);
2382 exp_print_tree (address
->address
);
2386 /* Print a reloc statement. */
2389 print_reloc_statement (reloc
)
2390 lang_reloc_statement_type
*reloc
;
2395 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2396 ldfile_output_machine
);
2398 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
2401 addr
= reloc
->output_vma
;
2402 if (reloc
->output_section
!= NULL
)
2403 addr
+= reloc
->output_section
->vma
;
2405 size
= bfd_get_reloc_size (reloc
->howto
);
2407 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
2409 if (reloc
->name
!= NULL
)
2410 minfo ("%s+", reloc
->name
);
2412 minfo ("%s+", reloc
->section
->name
);
2414 exp_print_tree (reloc
->addend_exp
);
2418 print_dot
= addr
+ size
/ opb
;
2422 print_padding_statement (s
)
2423 lang_padding_statement_type
*s
;
2427 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2428 ldfile_output_machine
);
2432 len
= sizeof " *fill*" - 1;
2433 while (len
< SECTION_NAME_MAP_LENGTH
)
2439 addr
= s
->output_offset
;
2440 if (s
->output_section
!= NULL
)
2441 addr
+= s
->output_section
->vma
;
2442 minfo ("0x%V %W", addr
, s
->size
);
2445 minfo (" %u", s
->fill
);
2449 print_dot
= addr
+ s
->size
/ opb
;
2453 print_wild_statement (w
, os
)
2454 lang_wild_statement_type
*w
;
2455 lang_output_section_statement_type
*os
;
2457 struct wildcard_list
*sec
;
2461 if (w
->filenames_sorted
)
2463 if (w
->filename
!= NULL
)
2464 minfo ("%s", w
->filename
);
2467 if (w
->filenames_sorted
)
2471 for (sec
= w
->section_list
; sec
; sec
= sec
->next
)
2473 if (sec
->spec
.sorted
)
2475 if (sec
->spec
.exclude_name_list
!= NULL
)
2478 minfo ("EXCLUDE_FILE ( %s", sec
->spec
.exclude_name_list
->name
);
2479 for (tmp
= sec
->spec
.exclude_name_list
->next
; tmp
; tmp
= tmp
->next
)
2480 minfo (", %s", tmp
->name
);
2483 if (sec
->spec
.name
!= NULL
)
2484 minfo ("%s", sec
->spec
.name
);
2487 if (sec
->spec
.sorted
)
2494 print_statement_list (w
->children
.head
, os
);
2497 /* Print a group statement. */
2501 lang_group_statement_type
*s
;
2502 lang_output_section_statement_type
*os
;
2504 fprintf (config
.map_file
, "START GROUP\n");
2505 print_statement_list (s
->children
.head
, os
);
2506 fprintf (config
.map_file
, "END GROUP\n");
2509 /* Print the list of statements in S.
2510 This can be called for any statement type. */
2513 print_statement_list (s
, os
)
2514 lang_statement_union_type
*s
;
2515 lang_output_section_statement_type
*os
;
2519 print_statement (s
, os
);
2524 /* Print the first statement in statement list S.
2525 This can be called for any statement type. */
2528 print_statement (s
, os
)
2529 lang_statement_union_type
*s
;
2530 lang_output_section_statement_type
*os
;
2532 switch (s
->header
.type
)
2535 fprintf (config
.map_file
, _("Fail with %d\n"), s
->header
.type
);
2538 case lang_constructors_statement_enum
:
2539 if (constructor_list
.head
!= NULL
)
2541 if (constructors_sorted
)
2542 minfo (" SORT (CONSTRUCTORS)\n");
2544 minfo (" CONSTRUCTORS\n");
2545 print_statement_list (constructor_list
.head
, os
);
2548 case lang_wild_statement_enum
:
2549 print_wild_statement (&s
->wild_statement
, os
);
2551 case lang_address_statement_enum
:
2552 print_address_statement (&s
->address_statement
);
2554 case lang_object_symbols_statement_enum
:
2555 minfo (" CREATE_OBJECT_SYMBOLS\n");
2557 case lang_fill_statement_enum
:
2558 print_fill_statement (&s
->fill_statement
);
2560 case lang_data_statement_enum
:
2561 print_data_statement (&s
->data_statement
);
2563 case lang_reloc_statement_enum
:
2564 print_reloc_statement (&s
->reloc_statement
);
2566 case lang_input_section_enum
:
2567 print_input_section (&s
->input_section
);
2569 case lang_padding_statement_enum
:
2570 print_padding_statement (&s
->padding_statement
);
2572 case lang_output_section_statement_enum
:
2573 print_output_section_statement (&s
->output_section_statement
);
2575 case lang_assignment_statement_enum
:
2576 print_assignment (&s
->assignment_statement
, os
);
2578 case lang_target_statement_enum
:
2579 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
2581 case lang_output_statement_enum
:
2582 minfo ("OUTPUT(%s", s
->output_statement
.name
);
2583 if (output_target
!= NULL
)
2584 minfo (" %s", output_target
);
2587 case lang_input_statement_enum
:
2588 print_input_statement (&s
->input_statement
);
2590 case lang_group_statement_enum
:
2591 print_group (&s
->group_statement
, os
);
2593 case lang_afile_asection_pair_statement_enum
:
2602 print_statement_list (statement_list
.head
, abs_output_section
);
2605 /* Print the first N statements in statement list S to STDERR.
2606 If N == 0, nothing is printed.
2607 If N < 0, the entire list is printed.
2608 Intended to be called from GDB. */
2611 dprint_statement (s
, n
)
2612 lang_statement_union_type
*s
;
2615 FILE *map_save
= config
.map_file
;
2617 config
.map_file
= stderr
;
2620 print_statement_list (s
, abs_output_section
);
2623 while (s
&& --n
>= 0)
2625 print_statement (s
, abs_output_section
);
2630 config
.map_file
= map_save
;
2634 insert_pad (ptr
, fill
, alignment_needed
, output_section
, dot
)
2635 lang_statement_union_type
**ptr
;
2637 unsigned int alignment_needed
;
2638 asection
*output_section
;
2641 lang_statement_union_type
*pad
;
2643 pad
= ((lang_statement_union_type
*)
2644 ((char *) ptr
- offsetof (lang_statement_union_type
, header
.next
)));
2645 if (ptr
!= &statement_list
.head
2646 && pad
->header
.type
== lang_padding_statement_enum
2647 && pad
->padding_statement
.output_section
== output_section
)
2649 /* Use the existing pad statement. The above test on output
2650 section is probably redundant, but it doesn't hurt to check. */
2654 /* Make a new padding statement, linked into existing chain. */
2655 pad
= ((lang_statement_union_type
*)
2656 stat_alloc (sizeof (lang_padding_statement_type
)));
2657 pad
->header
.next
= *ptr
;
2659 pad
->header
.type
= lang_padding_statement_enum
;
2660 pad
->padding_statement
.output_section
= output_section
;
2661 pad
->padding_statement
.fill
= fill
;
2663 pad
->padding_statement
.output_offset
= dot
- output_section
->vma
;
2664 pad
->padding_statement
.size
= alignment_needed
;
2665 output_section
->_raw_size
+= alignment_needed
;
2668 /* Work out how much this section will move the dot point. */
2671 size_input_section (this_ptr
, output_section_statement
, fill
, dot
)
2672 lang_statement_union_type
**this_ptr
;
2673 lang_output_section_statement_type
*output_section_statement
;
2677 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
2678 asection
*i
= is
->section
;
2680 if (is
->ifile
->just_syms_flag
== false)
2682 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2683 ldfile_output_machine
);
2684 unsigned int alignment_needed
;
2687 /* Align this section first to the input sections requirement,
2688 then to the output section's requirement. If this alignment
2689 is greater than any seen before, then record it too. Perform
2690 the alignment by inserting a magic 'padding' statement. */
2692 if (output_section_statement
->subsection_alignment
!= -1)
2693 i
->alignment_power
= output_section_statement
->subsection_alignment
;
2695 o
= output_section_statement
->bfd_section
;
2696 if (o
->alignment_power
< i
->alignment_power
)
2697 o
->alignment_power
= i
->alignment_power
;
2699 alignment_needed
= align_power (dot
, i
->alignment_power
) - dot
;
2701 if (alignment_needed
!= 0)
2703 insert_pad (this_ptr
, fill
, alignment_needed
* opb
, o
, dot
);
2704 dot
+= alignment_needed
;
2707 /* Remember where in the output section this input section goes. */
2709 i
->output_offset
= dot
- o
->vma
;
2711 /* Mark how big the output section must be to contain this now. */
2712 if (i
->_cooked_size
!= 0)
2713 dot
+= i
->_cooked_size
/ opb
;
2715 dot
+= i
->_raw_size
/ opb
;
2716 o
->_raw_size
= (dot
- o
->vma
) * opb
;
2720 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
2726 #define IGNORE_SECTION(bfd, s) \
2727 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2728 != (SEC_ALLOC | SEC_LOAD)) \
2729 || bfd_section_size (bfd, s) == 0)
2731 /* Check to see if any allocated sections overlap with other allocated
2732 sections. This can happen when the linker script specifically specifies
2733 the output section addresses of the two sections. */
2736 lang_check_section_addresses ()
2739 unsigned opb
= bfd_octets_per_byte (output_bfd
);
2741 /* Scan all sections in the output list. */
2742 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2746 /* Ignore sections which are not loaded or which have no contents. */
2747 if (IGNORE_SECTION (output_bfd
, s
))
2750 /* Once we reach section 's' stop our seach. This prevents two
2751 warning messages from being produced, one for 'section A overlaps
2752 section B' and one for 'section B overlaps section A'. */
2753 for (os
= output_bfd
->sections
; os
!= s
; os
= os
->next
)
2760 /* Only consider loadable sections with real contents. */
2761 if (IGNORE_SECTION (output_bfd
, os
))
2764 /* We must check the sections' LMA addresses not their
2765 VMA addresses because overlay sections can have
2766 overlapping VMAs but they must have distinct LMAs. */
2767 s_start
= bfd_section_lma (output_bfd
, s
);
2768 os_start
= bfd_section_lma (output_bfd
, os
);
2769 s_end
= s_start
+ bfd_section_size (output_bfd
, s
) / opb
- 1;
2770 os_end
= os_start
+ bfd_section_size (output_bfd
, os
) / opb
- 1;
2772 /* Look for an overlap. */
2773 if ((s_end
< os_start
) || (s_start
> os_end
))
2777 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2778 s
->name
, s_start
, s_end
, os
->name
, os_start
, os_end
);
2780 /* Once we have found one overlap for this section,
2781 stop looking for others. */
2787 /* Make sure the new address is within the region. We explicitly permit the
2788 current address to be at the exact end of the region when the address is
2789 non-zero, in case the region is at the end of addressable memory and the
2790 calculation wraps around. */
2793 os_region_check (os
, region
, tree
, base
)
2794 lang_output_section_statement_type
*os
;
2795 struct memory_region_struct
*region
;
2799 if ((region
->current
< region
->origin
2800 || (region
->current
- region
->origin
> region
->length
))
2801 && ((region
->current
!= region
->origin
+ region
->length
)
2804 if (tree
!= (etree_type
*) NULL
)
2806 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2808 os
->bfd_section
->owner
,
2809 os
->bfd_section
->name
,
2814 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2816 os
->bfd_section
->owner
,
2817 os
->bfd_section
->name
);
2819 /* Reset the region pointer. */
2820 region
->current
= region
->origin
;
2824 /* Set the sizes for all the output sections. */
2827 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
2828 lang_statement_union_type
*s
;
2829 lang_output_section_statement_type
*output_section_statement
;
2830 lang_statement_union_type
**prev
;
2835 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
2836 ldfile_output_machine
);
2838 /* Size up the sections from their constituent parts. */
2839 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
2841 switch (s
->header
.type
)
2843 case lang_output_section_statement_enum
:
2846 lang_output_section_statement_type
*os
;
2848 os
= &s
->output_section_statement
;
2849 if (os
->bfd_section
== NULL
)
2850 /* This section was never actually created. */
2853 /* If this is a COFF shared library section, use the size and
2854 address from the input section. FIXME: This is COFF
2855 specific; it would be cleaner if there were some other way
2856 to do this, but nothing simple comes to mind. */
2857 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2861 if (os
->children
.head
== NULL
2862 || os
->children
.head
->header
.next
!= NULL
2863 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2864 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2867 input
= os
->children
.head
->input_section
.section
;
2868 bfd_set_section_vma (os
->bfd_section
->owner
,
2870 bfd_section_vma (input
->owner
, input
));
2871 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2875 if (bfd_is_abs_section (os
->bfd_section
))
2877 /* No matter what happens, an abs section starts at zero. */
2878 ASSERT (os
->bfd_section
->vma
== 0);
2882 if (os
->addr_tree
== (etree_type
*) NULL
)
2884 /* No address specified for this section, get one
2885 from the region specification. */
2886 if (os
->region
== (lang_memory_region_type
*) NULL
2887 || (((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2888 & (SEC_ALLOC
| SEC_LOAD
)) != 0)
2889 && os
->region
->name
[0] == '*'
2890 && strcmp (os
->region
->name
, "*default*") == 0))
2892 os
->region
= lang_memory_default (os
->bfd_section
);
2895 /* If a loadable section is using the default memory
2896 region, and some non default memory regions were
2897 defined, issue a warning. */
2898 if ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2899 & (SEC_ALLOC
| SEC_LOAD
)) != 0
2900 && ! link_info
.relocateable
2901 && strcmp (os
->region
->name
, "*default*") == 0
2902 && lang_memory_region_list
!= NULL
2903 && (strcmp (lang_memory_region_list
->name
,
2905 || lang_memory_region_list
->next
!= NULL
))
2906 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2907 bfd_get_section_name (output_bfd
,
2910 dot
= os
->region
->current
;
2912 if (os
->section_alignment
== -1)
2917 dot
= align_power (dot
,
2918 os
->bfd_section
->alignment_power
);
2920 if (dot
!= olddot
&& config
.warn_section_align
)
2921 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2922 os
->name
, (unsigned int) (dot
- olddot
));
2929 r
= exp_fold_tree (os
->addr_tree
,
2931 lang_allocating_phase_enum
,
2933 if (r
.valid_p
== false)
2935 einfo (_("%F%S: non constant address expression for section %s\n"),
2938 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2941 /* The section starts here.
2942 First, align to what the section needs. */
2944 if (os
->section_alignment
!= -1)
2945 dot
= align_power (dot
, os
->section_alignment
);
2947 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2949 os
->bfd_section
->output_offset
= 0;
2952 lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2953 os
->fill
, dot
, relax
);
2955 /* Put the section within the requested block size, or
2956 align at the block boundary. */
2957 after
= ALIGN_N (os
->bfd_section
->vma
2958 + os
->bfd_section
->_raw_size
/ opb
,
2959 /* The coercion here is important, see ld.h. */
2960 (bfd_vma
) os
->block_value
);
2962 if (bfd_is_abs_section (os
->bfd_section
))
2963 ASSERT (after
== os
->bfd_section
->vma
);
2965 os
->bfd_section
->_raw_size
=
2966 (after
- os
->bfd_section
->vma
) * opb
;
2967 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
2968 os
->processed
= true;
2970 /* Update dot in the region ?
2971 We only do this if the section is going to be allocated,
2972 since unallocated sections do not contribute to the region's
2973 overall size in memory.
2975 If the SEC_NEVER_LOAD bit is not set, it will affect the
2976 addresses of sections after it. We have to update
2978 if (os
->region
!= (lang_memory_region_type
*) NULL
2979 && ((bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2980 & SEC_NEVER_LOAD
) == 0
2981 || (bfd_get_section_flags (output_bfd
, os
->bfd_section
)
2982 & (SEC_ALLOC
| SEC_LOAD
))))
2984 os
->region
->current
= dot
;
2986 /* Make sure the new address is within the region. */
2987 os_region_check (os
, os
->region
, os
->addr_tree
,
2988 os
->bfd_section
->vma
);
2990 /* If there's no load address specified, use the run
2991 region as the load region. */
2992 if (os
->lma_region
== NULL
&& os
->load_base
== NULL
)
2993 os
->lma_region
= os
->region
;
2995 if (os
->lma_region
!= NULL
)
2997 if (os
->load_base
!= NULL
)
2999 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
3003 /* Don't allocate twice. */
3004 if (os
->lma_region
!= os
->region
)
3006 /* Set load_base, which will be handled later. */
3008 exp_intop (os
->lma_region
->current
);
3009 os
->lma_region
->current
+=
3010 os
->bfd_section
->_raw_size
/ opb
;
3011 os_region_check (os
, os
->lma_region
, NULL
,
3012 os
->bfd_section
->lma
);
3020 case lang_constructors_statement_enum
:
3021 dot
= lang_size_sections (constructor_list
.head
,
3022 output_section_statement
,
3023 &s
->wild_statement
.children
.head
,
3027 case lang_data_statement_enum
:
3029 unsigned int size
= 0;
3031 s
->data_statement
.output_vma
=
3032 dot
- output_section_statement
->bfd_section
->vma
;
3033 s
->data_statement
.output_section
=
3034 output_section_statement
->bfd_section
;
3036 switch (s
->data_statement
.type
)
3057 output_section_statement
->bfd_section
->_raw_size
+= size
;
3058 /* The output section gets contents, and then we inspect for
3059 any flags set in the input script which override any ALLOC. */
3060 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
3061 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
))
3063 output_section_statement
->bfd_section
->flags
|=
3064 SEC_ALLOC
| SEC_LOAD
;
3069 case lang_reloc_statement_enum
:
3073 s
->reloc_statement
.output_vma
=
3074 dot
- output_section_statement
->bfd_section
->vma
;
3075 s
->reloc_statement
.output_section
=
3076 output_section_statement
->bfd_section
;
3077 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
3079 output_section_statement
->bfd_section
->_raw_size
+= size
;
3083 case lang_wild_statement_enum
:
3085 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
3086 output_section_statement
,
3087 &s
->wild_statement
.children
.head
,
3092 case lang_object_symbols_statement_enum
:
3093 link_info
.create_object_symbols_section
=
3094 output_section_statement
->bfd_section
;
3096 case lang_output_statement_enum
:
3097 case lang_target_statement_enum
:
3099 case lang_input_section_enum
:
3103 i
= (*prev
)->input_section
.section
;
3106 if (i
->_cooked_size
== 0)
3107 i
->_cooked_size
= i
->_raw_size
;
3113 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
3114 einfo (_("%P%F: can't relax section: %E\n"));
3118 dot
= size_input_section (prev
, output_section_statement
,
3119 output_section_statement
->fill
, dot
);
3122 case lang_input_statement_enum
:
3124 case lang_fill_statement_enum
:
3125 s
->fill_statement
.output_section
=
3126 output_section_statement
->bfd_section
;
3128 fill
= s
->fill_statement
.fill
;
3130 case lang_assignment_statement_enum
:
3132 bfd_vma newdot
= dot
;
3134 exp_fold_tree (s
->assignment_statement
.exp
,
3135 output_section_statement
,
3136 lang_allocating_phase_enum
,
3142 if (output_section_statement
== abs_output_section
)
3144 /* If we don't have an output section, then just adjust
3145 the default memory address. */
3146 lang_memory_region_lookup ("*default*")->current
= newdot
;
3150 /* Insert a pad after this statement. We can't
3151 put the pad before when relaxing, in case the
3152 assignment references dot. */
3153 insert_pad (&s
->header
.next
, fill
, (newdot
- dot
) * opb
,
3154 output_section_statement
->bfd_section
, dot
);
3156 /* Don't neuter the pad below when relaxing. */
3165 case lang_padding_statement_enum
:
3166 /* If this is the first time lang_size_sections is called,
3167 we won't have any padding statements. If this is the
3168 second or later passes when relaxing, we should allow
3169 padding to shrink. If padding is needed on this pass, it
3170 will be added back in. */
3171 s
->padding_statement
.size
= 0;
3173 /* Make sure output_offset is valid. If relaxation shrinks
3174 the section and this pad isn't needed, it's possible to
3175 have output_offset larger than the final size of the
3176 section. bfd_set_section_contents will complain even for
3177 a pad size of zero. */
3178 s
->padding_statement
.output_offset
3179 = dot
- output_section_statement
->bfd_section
->vma
;
3182 case lang_group_statement_enum
:
3183 dot
= lang_size_sections (s
->group_statement
.children
.head
,
3184 output_section_statement
,
3185 &s
->group_statement
.children
.head
,
3193 /* We can only get here when relaxing is turned on. */
3194 case lang_address_statement_enum
:
3197 prev
= &s
->header
.next
;
3203 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
3204 lang_statement_union_type
*s
;
3205 lang_output_section_statement_type
*output_section_statement
;
3209 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3210 ldfile_output_machine
);
3212 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
3214 switch (s
->header
.type
)
3216 case lang_constructors_statement_enum
:
3217 dot
= lang_do_assignments (constructor_list
.head
,
3218 output_section_statement
,
3223 case lang_output_section_statement_enum
:
3225 lang_output_section_statement_type
*os
;
3227 os
= &(s
->output_section_statement
);
3228 if (os
->bfd_section
!= NULL
)
3230 dot
= os
->bfd_section
->vma
;
3231 (void) lang_do_assignments (os
->children
.head
, os
,
3233 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
/ opb
;
3238 /* If nothing has been placed into the output section then
3239 it won't have a bfd_section. */
3240 if (os
->bfd_section
)
3242 os
->bfd_section
->lma
3243 = exp_get_abs_int (os
->load_base
, 0, "load base",
3244 lang_final_phase_enum
);
3249 case lang_wild_statement_enum
:
3251 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
3252 output_section_statement
,
3257 case lang_object_symbols_statement_enum
:
3258 case lang_output_statement_enum
:
3259 case lang_target_statement_enum
:
3261 case lang_common_statement_enum
:
3264 case lang_data_statement_enum
:
3266 etree_value_type value
;
3268 value
= exp_fold_tree (s
->data_statement
.exp
,
3270 lang_final_phase_enum
, dot
, &dot
);
3271 s
->data_statement
.value
= value
.value
;
3272 if (value
.valid_p
== false)
3273 einfo (_("%F%P: invalid data statement\n"));
3277 switch (s
->data_statement
.type
)
3301 case lang_reloc_statement_enum
:
3303 etree_value_type value
;
3305 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
3307 lang_final_phase_enum
, dot
, &dot
);
3308 s
->reloc_statement
.addend_value
= value
.value
;
3309 if (value
.valid_p
== false)
3310 einfo (_("%F%P: invalid reloc statement\n"));
3312 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
) / opb
;
3315 case lang_input_section_enum
:
3317 asection
*in
= s
->input_section
.section
;
3319 if (in
->_cooked_size
!= 0)
3320 dot
+= in
->_cooked_size
/ opb
;
3322 dot
+= in
->_raw_size
/ opb
;
3326 case lang_input_statement_enum
:
3328 case lang_fill_statement_enum
:
3329 fill
= s
->fill_statement
.fill
;
3331 case lang_assignment_statement_enum
:
3333 exp_fold_tree (s
->assignment_statement
.exp
,
3334 output_section_statement
,
3335 lang_final_phase_enum
,
3341 case lang_padding_statement_enum
:
3342 dot
+= s
->padding_statement
.size
/ opb
;
3345 case lang_group_statement_enum
:
3346 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
3347 output_section_statement
,
3355 case lang_address_statement_enum
:
3363 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3364 operator .startof. (section_name), it produces an undefined symbol
3365 .startof.section_name. Similarly, when it sees
3366 .sizeof. (section_name), it produces an undefined symbol
3367 .sizeof.section_name. For all the output sections, we look for
3368 such symbols, and set them to the correct value. */
3375 if (link_info
.relocateable
)
3378 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
3380 const char *secname
;
3382 struct bfd_link_hash_entry
*h
;
3384 secname
= bfd_get_section_name (output_bfd
, s
);
3385 buf
= xmalloc (10 + strlen (secname
));
3387 sprintf (buf
, ".startof.%s", secname
);
3388 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3389 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3391 h
->type
= bfd_link_hash_defined
;
3392 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
3393 h
->u
.def
.section
= bfd_abs_section_ptr
;
3396 sprintf (buf
, ".sizeof.%s", secname
);
3397 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
3398 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
3402 opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3403 ldfile_output_machine
);
3404 h
->type
= bfd_link_hash_defined
;
3405 if (s
->_cooked_size
!= 0)
3406 h
->u
.def
.value
= s
->_cooked_size
/ opb
;
3408 h
->u
.def
.value
= s
->_raw_size
/ opb
;
3409 h
->u
.def
.section
= bfd_abs_section_ptr
;
3419 struct bfd_link_hash_entry
*h
;
3422 if (link_info
.relocateable
|| link_info
.shared
)
3427 if (entry_symbol
== (char *) NULL
)
3429 /* No entry has been specified. Look for start, but don't warn
3430 if we don't find it. */
3431 entry_symbol
= "start";
3435 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
3436 if (h
!= (struct bfd_link_hash_entry
*) NULL
3437 && (h
->type
== bfd_link_hash_defined
3438 || h
->type
== bfd_link_hash_defweak
)
3439 && h
->u
.def
.section
->output_section
!= NULL
)
3443 val
= (h
->u
.def
.value
3444 + bfd_get_section_vma (output_bfd
,
3445 h
->u
.def
.section
->output_section
)
3446 + h
->u
.def
.section
->output_offset
);
3447 if (! bfd_set_start_address (output_bfd
, val
))
3448 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol
);
3455 /* We couldn't find the entry symbol. Try parsing it as a
3457 val
= bfd_scan_vma (entry_symbol
, &send
, 0);
3460 if (! bfd_set_start_address (output_bfd
, val
))
3461 einfo (_("%P%F: can't set start address\n"));
3467 /* Can't find the entry symbol, and it's not a number. Use
3468 the first address in the text section. */
3469 ts
= bfd_get_section_by_name (output_bfd
, ".text");
3470 if (ts
!= (asection
*) NULL
)
3473 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3474 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
3475 if (! bfd_set_start_address (output_bfd
,
3476 bfd_get_section_vma (output_bfd
,
3478 einfo (_("%P%F: can't set start address\n"));
3483 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3490 /* This is a small function used when we want to ignore errors from
3494 #ifdef ANSI_PROTOTYPES
3495 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED
, ...)
3497 ignore_bfd_errors (s
)
3498 const char *s ATTRIBUTE_UNUSED
;
3501 /* Don't do anything. */
3504 /* Check that the architecture of all the input files is compatible
3505 with the output file. Also call the backend to let it do any
3506 other checking that is needed. */
3511 lang_statement_union_type
*file
;
3513 const bfd_arch_info_type
*compatible
;
3515 for (file
= file_chain
.head
;
3516 file
!= (lang_statement_union_type
*) NULL
;
3517 file
= file
->input_statement
.next
)
3519 input_bfd
= file
->input_statement
.the_bfd
;
3520 compatible
= bfd_arch_get_compatible (input_bfd
,
3522 if (compatible
== NULL
)
3524 if (command_line
.warn_mismatch
)
3525 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3526 bfd_printable_name (input_bfd
), input_bfd
,
3527 bfd_printable_name (output_bfd
));
3529 else if (link_info
.relocateable
3530 /* In general it is not possible to perform a relocatable
3531 link between differing object formats when the input
3532 file has relocations, because the relocations in the
3533 input format may not have equivalent representations in
3534 the output format (and besides BFD does not translate
3535 relocs for other link purposes than a final link). */
3536 && bfd_get_flavour (input_bfd
) != bfd_get_flavour (output_bfd
)
3537 && (bfd_get_file_flags (input_bfd
) & HAS_RELOC
) != 0)
3538 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3539 bfd_get_target (input_bfd
), input_bfd
,
3540 bfd_get_target (output_bfd
), output_bfd
);
3541 else if (bfd_count_sections (input_bfd
))
3543 /* If the input bfd has no contents, it shouldn't set the
3544 private data of the output bfd. */
3546 bfd_error_handler_type pfn
= NULL
;
3548 /* If we aren't supposed to warn about mismatched input
3549 files, temporarily set the BFD error handler to a
3550 function which will do nothing. We still want to call
3551 bfd_merge_private_bfd_data, since it may set up
3552 information which is needed in the output file. */
3553 if (! command_line
.warn_mismatch
)
3554 pfn
= bfd_set_error_handler (ignore_bfd_errors
);
3555 if (! bfd_merge_private_bfd_data (input_bfd
, output_bfd
))
3557 if (command_line
.warn_mismatch
)
3558 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3561 if (! command_line
.warn_mismatch
)
3562 bfd_set_error_handler (pfn
);
3567 /* Look through all the global common symbols and attach them to the
3568 correct section. The -sort-common command line switch may be used
3569 to roughly sort the entries by size. */
3574 if (command_line
.inhibit_common_definition
)
3576 if (link_info
.relocateable
3577 && ! command_line
.force_common_definition
)
3580 if (! config
.sort_common
)
3581 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
3586 for (power
= 4; power
>= 0; power
--)
3587 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
3592 /* Place one common symbol in the correct section. */
3595 lang_one_common (h
, info
)
3596 struct bfd_link_hash_entry
*h
;
3599 unsigned int power_of_two
;
3602 unsigned opb
= bfd_arch_mach_octets_per_byte (ldfile_output_architecture
,
3603 ldfile_output_machine
);
3605 if (h
->type
!= bfd_link_hash_common
)
3609 power_of_two
= h
->u
.c
.p
->alignment_power
;
3611 if (config
.sort_common
3612 && power_of_two
< (unsigned int) *(int *) info
)
3615 section
= h
->u
.c
.p
->section
;
3617 /* Increase the size of the section. */
3618 section
->_cooked_size
= ALIGN_N ((section
->_cooked_size
+ opb
- 1) / opb
,
3619 (bfd_size_type
) (1 << power_of_two
)) * opb
;
3621 /* Adjust the alignment if necessary. */
3622 if (power_of_two
> section
->alignment_power
)
3623 section
->alignment_power
= power_of_two
;
3625 /* Change the symbol from common to defined. */
3626 h
->type
= bfd_link_hash_defined
;
3627 h
->u
.def
.section
= section
;
3628 h
->u
.def
.value
= section
->_cooked_size
;
3630 /* Increase the size of the section. */
3631 section
->_cooked_size
+= size
;
3633 /* Make sure the section is allocated in memory, and make sure that
3634 it is no longer a common section. */
3635 section
->flags
|= SEC_ALLOC
;
3636 section
->flags
&= ~SEC_IS_COMMON
;
3638 if (config
.map_file
!= NULL
)
3640 static boolean header_printed
;
3645 if (! header_printed
)
3647 minfo (_("\nAllocating common symbols\n"));
3648 minfo (_("Common symbol size file\n\n"));
3649 header_printed
= true;
3652 name
= demangle (h
->root
.string
);
3654 len
= strlen (name
);
3669 if (size
<= 0xffffffff)
3670 sprintf (buf
, "%lx", (unsigned long) size
);
3672 sprintf_vma (buf
, size
);
3682 minfo ("%B\n", section
->owner
);
3688 /* Run through the input files and ensure that every input section has
3689 somewhere to go. If one is found without a destination then create
3690 an input request and place it into the statement tree. */
3693 lang_place_orphans ()
3695 LANG_FOR_EACH_INPUT_STATEMENT (file
)
3699 for (s
= file
->the_bfd
->sections
;
3700 s
!= (asection
*) NULL
;
3703 if (s
->output_section
== (asection
*) NULL
)
3705 /* This section of the file is not attatched, root
3706 around for a sensible place for it to go. */
3708 if (file
->just_syms_flag
)
3710 /* We are only retrieving symbol values from this
3711 file. We want the symbols to act as though the
3712 values in the file are absolute. */
3713 s
->output_section
= bfd_abs_section_ptr
;
3714 s
->output_offset
= s
->vma
;
3716 else if (strcmp (s
->name
, "COMMON") == 0)
3718 /* This is a lonely common section which must have
3719 come from an archive. We attach to the section
3720 with the wildcard. */
3721 if (! link_info
.relocateable
3722 || command_line
.force_common_definition
)
3724 if (default_common_section
== NULL
)
3727 /* This message happens when using the
3728 svr3.ifile linker script, so I have
3730 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3732 default_common_section
=
3733 lang_output_section_statement_lookup (".bss");
3736 lang_add_section (&default_common_section
->children
, s
,
3737 default_common_section
, file
);
3740 else if (ldemul_place_orphan (file
, s
))
3744 lang_output_section_statement_type
*os
;
3746 os
= lang_output_section_statement_lookup (s
->name
);
3747 lang_add_section (&os
->children
, s
, os
, file
);
3755 lang_set_flags (ptr
, flags
, invert
)
3756 lang_memory_region_type
*ptr
;
3760 flagword
*ptr_flags
;
3762 ptr_flags
= invert
? &ptr
->not_flags
: &ptr
->flags
;
3768 *ptr_flags
|= SEC_ALLOC
;
3772 *ptr_flags
|= SEC_READONLY
;
3776 *ptr_flags
|= SEC_DATA
;
3780 *ptr_flags
|= SEC_CODE
;
3785 *ptr_flags
|= SEC_LOAD
;
3789 einfo (_("%P%F: invalid syntax in flags\n"));
3796 /* Call a function on each input file. This function will be called
3797 on an archive, but not on the elements. */
3800 lang_for_each_input_file (func
)
3801 void (*func
) PARAMS ((lang_input_statement_type
*));
3803 lang_input_statement_type
*f
;
3805 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
3807 f
= (lang_input_statement_type
*) f
->next_real_file
)
3811 /* Call a function on each file. The function will be called on all
3812 the elements of an archive which are included in the link, but will
3813 not be called on the archive file itself. */
3816 lang_for_each_file (func
)
3817 void (*func
) PARAMS ((lang_input_statement_type
*));
3819 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3830 lang_for_each_input_section (func
)
3831 void (*func
) PARAMS ((bfd
*ab
, asection
*as
));
3833 LANG_FOR_EACH_INPUT_STATEMENT (f
)
3837 for (s
= f
->the_bfd
->sections
;
3838 s
!= (asection
*) NULL
;
3841 func (f
->the_bfd
, s
);
3849 ldlang_add_file (entry
)
3850 lang_input_statement_type
*entry
;
3854 lang_statement_append (&file_chain
,
3855 (lang_statement_union_type
*) entry
,
3858 /* The BFD linker needs to have a list of all input BFDs involved in
3860 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
3861 ASSERT (entry
->the_bfd
!= output_bfd
);
3862 for (pp
= &link_info
.input_bfds
;
3863 *pp
!= (bfd
*) NULL
;
3864 pp
= &(*pp
)->link_next
)
3866 *pp
= entry
->the_bfd
;
3867 entry
->the_bfd
->usrdata
= (PTR
) entry
;
3868 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
3870 /* Look through the sections and check for any which should not be
3871 included in the link. We need to do this now, so that we can
3872 notice when the backend linker tries to report multiple
3873 definition errors for symbols which are in sections we aren't
3874 going to link. FIXME: It might be better to entirely ignore
3875 symbols which are defined in sections which are going to be
3876 discarded. This would require modifying the backend linker for
3877 each backend which might set the SEC_LINK_ONCE flag. If we do
3878 this, we should probably handle SEC_EXCLUDE in the same way. */
3880 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) entry
);
3884 lang_add_output (name
, from_script
)
3888 /* Make -o on command line override OUTPUT in script. */
3889 if (had_output_filename
== false || !from_script
)
3891 output_filename
= name
;
3892 had_output_filename
= true;
3896 static lang_output_section_statement_type
*current_section
;
3908 for (l
= 0; l
< 32; l
++)
3910 if (i
>= (unsigned int) x
)
3918 lang_output_section_statement_type
*
3919 lang_enter_output_section_statement (output_section_statement_name
,
3920 address_exp
, sectype
, block_value
,
3921 align
, subalign
, ebase
)
3922 const char *output_section_statement_name
;
3923 etree_type
*address_exp
;
3924 enum section_type sectype
;
3925 bfd_vma block_value
;
3927 etree_type
*subalign
;
3930 lang_output_section_statement_type
*os
;
3934 lang_output_section_statement_lookup (output_section_statement_name
);
3936 /* Add this statement to tree. */
3938 add_statement (lang_output_section_statement_enum
,
3939 output_section_statement
);
3941 /* Make next things chain into subchain of this. */
3943 if (os
->addr_tree
== (etree_type
*) NULL
)
3945 os
->addr_tree
= address_exp
;
3947 os
->sectype
= sectype
;
3948 if (sectype
!= noload_section
)
3949 os
->flags
= SEC_NO_FLAGS
;
3951 os
->flags
= SEC_NEVER_LOAD
;
3952 os
->block_value
= block_value
? block_value
: 1;
3953 stat_ptr
= &os
->children
;
3955 os
->subsection_alignment
=
3956 topower (exp_get_value_int (subalign
, -1, "subsection alignment", 0));
3957 os
->section_alignment
=
3958 topower (exp_get_value_int (align
, -1, "section alignment", 0));
3960 os
->load_base
= ebase
;
3967 lang_output_statement_type
*new =
3968 new_stat (lang_output_statement
, stat_ptr
);
3970 new->name
= output_filename
;
3973 /* Reset the current counters in the regions. */
3976 reset_memory_regions ()
3978 lang_memory_region_type
*p
= lang_memory_region_list
;
3981 for (p
= lang_memory_region_list
;
3982 p
!= (lang_memory_region_type
*) NULL
;
3985 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3986 p
->current
= p
->origin
;
3989 for (o
= output_bfd
->sections
; o
!= NULL
; o
= o
->next
)
3993 /* If the wild pattern was marked KEEP, the member sections
3994 should be as well. */
3997 gc_section_callback (ptr
, sec
, section
, file
, data
)
3998 lang_wild_statement_type
*ptr
;
3999 struct wildcard_list
*sec ATTRIBUTE_UNUSED
;
4001 lang_input_statement_type
*file ATTRIBUTE_UNUSED
;
4002 PTR data ATTRIBUTE_UNUSED
;
4004 if (ptr
->keep_sections
)
4005 section
->flags
|= SEC_KEEP
;
4008 /* Handle a wild statement, marking it against GC. */
4012 lang_wild_statement_type
*s
;
4014 walk_wild (s
, gc_section_callback
, NULL
);
4017 /* Iterate over sections marking them against GC. */
4020 lang_gc_sections_1 (s
)
4021 lang_statement_union_type
*s
;
4023 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->header
.next
)
4025 switch (s
->header
.type
)
4027 case lang_wild_statement_enum
:
4028 lang_gc_wild (&s
->wild_statement
);
4030 case lang_constructors_statement_enum
:
4031 lang_gc_sections_1 (constructor_list
.head
);
4033 case lang_output_section_statement_enum
:
4034 lang_gc_sections_1 (s
->output_section_statement
.children
.head
);
4036 case lang_group_statement_enum
:
4037 lang_gc_sections_1 (s
->group_statement
.children
.head
);
4048 struct bfd_link_hash_entry
*h
;
4049 ldlang_undef_chain_list_type
*ulist
, fake_list_start
;
4051 /* Keep all sections so marked in the link script. */
4053 lang_gc_sections_1 (statement_list
.head
);
4055 /* Keep all sections containing symbols undefined on the command-line.
4056 Handle the entry symbol at the same time. */
4058 if (entry_symbol
!= NULL
)
4060 fake_list_start
.next
= ldlang_undef_chain_list_head
;
4061 fake_list_start
.name
= (char *) entry_symbol
;
4062 ulist
= &fake_list_start
;
4065 ulist
= ldlang_undef_chain_list_head
;
4067 for (; ulist
; ulist
= ulist
->next
)
4069 h
= bfd_link_hash_lookup (link_info
.hash
, ulist
->name
,
4070 false, false, false);
4072 if (h
!= (struct bfd_link_hash_entry
*) NULL
4073 && (h
->type
== bfd_link_hash_defined
4074 || h
->type
== bfd_link_hash_defweak
)
4075 && ! bfd_is_abs_section (h
->u
.def
.section
))
4077 h
->u
.def
.section
->flags
|= SEC_KEEP
;
4081 bfd_gc_sections (output_bfd
, &link_info
);
4087 lang_reasonable_defaults ();
4088 current_target
= default_target
;
4090 /* Open the output file. */
4091 lang_for_each_statement (ldlang_open_output
);
4093 ldemul_create_output_section_statements ();
4095 /* Add to the hash table all undefineds on the command line. */
4096 lang_place_undefineds ();
4098 already_linked_table_init ();
4100 /* Create a bfd for each input file. */
4101 current_target
= default_target
;
4102 open_input_bfds (statement_list
.head
, false);
4104 ldemul_after_open ();
4106 already_linked_table_free ();
4108 /* Make sure that we're not mixing architectures. We call this
4109 after all the input files have been opened, but before we do any
4110 other processing, so that any operations merge_private_bfd_data
4111 does on the output file will be known during the rest of the
4115 /* Handle .exports instead of a version script if we're told to do so. */
4116 if (command_line
.version_exports_section
)
4117 lang_do_version_exports_section ();
4119 /* Build all sets based on the information gathered from the input
4121 ldctor_build_sets ();
4123 /* Remove unreferenced sections if asked to. */
4124 if (command_line
.gc_sections
)
4125 lang_gc_sections ();
4127 /* If there were any SEC_MERGE sections, finish their merging, so that
4128 section sizes can be computed. This has to be done after GC of sections,
4129 so that GCed sections are not merged, but before assigning output
4130 sections, since removing whole input sections is hard then. */
4131 bfd_merge_sections (output_bfd
, &link_info
);
4133 /* Size up the common data. */
4136 /* Run through the contours of the script and attach input sections
4137 to the correct output sections. */
4138 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
4139 (lang_output_section_statement_type
*) NULL
);
4141 /* Find any sections not attached explicitly and handle them. */
4142 lang_place_orphans ();
4144 ldemul_before_allocation ();
4146 /* We must record the program headers before we try to fix the
4147 section positions, since they will affect SIZEOF_HEADERS. */
4148 lang_record_phdrs ();
4150 /* Size up the sections. */
4151 lang_size_sections (statement_list
.head
,
4153 &statement_list
.head
, 0, (bfd_vma
) 0, NULL
);
4155 /* Now run around and relax if we can. */
4156 if (command_line
.relax
)
4158 /* Keep relaxing until bfd_relax_section gives up. */
4159 boolean relax_again
;
4163 reset_memory_regions ();
4165 relax_again
= false;
4167 /* Note: pe-dll.c does something like this also. If you find
4168 you need to change this code, you probably need to change
4169 pe-dll.c also. DJ */
4171 /* Do all the assignments with our current guesses as to
4173 lang_do_assignments (statement_list
.head
,
4175 (fill_type
) 0, (bfd_vma
) 0);
4177 /* Perform another relax pass - this time we know where the
4178 globals are, so can make better guess. */
4179 lang_size_sections (statement_list
.head
,
4181 &(statement_list
.head
), 0, (bfd_vma
) 0,
4184 while (relax_again
);
4187 /* See if anything special should be done now we know how big
4189 ldemul_after_allocation ();
4191 /* Fix any .startof. or .sizeof. symbols. */
4192 lang_set_startof ();
4194 /* Do all the assignments, now that we know the final resting places
4195 of all the symbols. */
4197 lang_do_assignments (statement_list
.head
,
4199 (fill_type
) 0, (bfd_vma
) 0);
4201 /* Make sure that the section addresses make sense. */
4202 if (! link_info
.relocateable
4203 && command_line
.check_section_addresses
)
4204 lang_check_section_addresses ();
4212 /* EXPORTED TO YACC */
4215 lang_add_wild (filespec
, section_list
, keep_sections
)
4216 struct wildcard_spec
*filespec
;
4217 struct wildcard_list
*section_list
;
4218 boolean keep_sections
;
4220 struct wildcard_list
*curr
, *next
;
4221 lang_wild_statement_type
*new;
4223 /* Reverse the list as the parser puts it back to front. */
4224 for (curr
= section_list
, section_list
= NULL
;
4226 section_list
= curr
, curr
= next
)
4228 if (curr
->spec
.name
!= NULL
&& strcmp (curr
->spec
.name
, "COMMON") == 0)
4229 placed_commons
= true;
4232 curr
->next
= section_list
;
4235 if (filespec
!= NULL
&& filespec
->name
!= NULL
)
4237 if (strcmp (filespec
->name
, "*") == 0)
4238 filespec
->name
= NULL
;
4239 else if (! wildcardp (filespec
->name
))
4240 lang_has_input_file
= true;
4243 new = new_stat (lang_wild_statement
, stat_ptr
);
4244 new->filename
= NULL
;
4245 new->filenames_sorted
= false;
4246 if (filespec
!= NULL
)
4248 new->filename
= filespec
->name
;
4249 new->filenames_sorted
= filespec
->sorted
;
4251 new->section_list
= section_list
;
4252 new->keep_sections
= keep_sections
;
4253 lang_list_init (&new->children
);
4257 lang_section_start (name
, address
)
4259 etree_type
*address
;
4261 lang_address_statement_type
*ad
;
4263 ad
= new_stat (lang_address_statement
, stat_ptr
);
4264 ad
->section_name
= name
;
4265 ad
->address
= address
;
4268 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4269 because of a -e argument on the command line, or zero if this is
4270 called by ENTRY in a linker script. Command line arguments take
4274 lang_add_entry (name
, cmdline
)
4278 if (entry_symbol
== NULL
4280 || ! entry_from_cmdline
)
4282 entry_symbol
= name
;
4283 entry_from_cmdline
= cmdline
;
4288 lang_add_target (name
)
4291 lang_target_statement_type
*new = new_stat (lang_target_statement
,
4307 map_option_f
= true;
4318 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
4325 lang_add_data (type
, exp
)
4327 union etree_union
*exp
;
4330 lang_data_statement_type
*new = new_stat (lang_data_statement
,
4338 /* Create a new reloc statement. RELOC is the BFD relocation type to
4339 generate. HOWTO is the corresponding howto structure (we could
4340 look this up, but the caller has already done so). SECTION is the
4341 section to generate a reloc against, or NAME is the name of the
4342 symbol to generate a reloc against. Exactly one of SECTION and
4343 NAME must be NULL. ADDEND is an expression for the addend. */
4346 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
4347 bfd_reloc_code_real_type reloc
;
4348 reloc_howto_type
*howto
;
4351 union etree_union
*addend
;
4353 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
4357 p
->section
= section
;
4359 p
->addend_exp
= addend
;
4361 p
->addend_value
= 0;
4362 p
->output_section
= NULL
;
4366 lang_assignment_statement_type
*
4367 lang_add_assignment (exp
)
4370 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
4378 lang_add_attribute (attribute
)
4379 enum statement_enum attribute
;
4381 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
4388 if (startup_file
!= (char *) NULL
)
4390 einfo (_("%P%Fmultiple STARTUP files\n"));
4392 first_file
->filename
= name
;
4393 first_file
->local_sym_name
= name
;
4394 first_file
->real
= true;
4396 startup_file
= name
;
4403 lang_float_flag
= maybe
;
4407 lang_leave_output_section_statement (fill
, memspec
, phdrs
, lma_memspec
)
4409 const char *memspec
;
4410 struct lang_output_section_phdr_list
*phdrs
;
4411 const char *lma_memspec
;
4413 current_section
->fill
= fill
;
4414 current_section
->region
= lang_memory_region_lookup (memspec
);
4415 if (strcmp (lma_memspec
, "*default*") != 0)
4417 current_section
->lma_region
= lang_memory_region_lookup (lma_memspec
);
4418 /* If no runtime region has been given, but the load region has
4419 been, use the load region. */
4420 if (strcmp (memspec
, "*default*") == 0)
4421 current_section
->region
= lang_memory_region_lookup (lma_memspec
);
4423 current_section
->phdrs
= phdrs
;
4424 stat_ptr
= &statement_list
;
4427 /* Create an absolute symbol with the given name with the value of the
4428 address of first byte of the section named.
4430 If the symbol already exists, then do nothing. */
4433 lang_abs_symbol_at_beginning_of (secname
, name
)
4434 const char *secname
;
4437 struct bfd_link_hash_entry
*h
;
4439 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4440 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4441 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4443 if (h
->type
== bfd_link_hash_new
4444 || h
->type
== bfd_link_hash_undefined
)
4448 h
->type
= bfd_link_hash_defined
;
4450 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4451 if (sec
== (asection
*) NULL
)
4454 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
4456 h
->u
.def
.section
= bfd_abs_section_ptr
;
4460 /* Create an absolute symbol with the given name with the value of the
4461 address of the first byte after the end of the section named.
4463 If the symbol already exists, then do nothing. */
4466 lang_abs_symbol_at_end_of (secname
, name
)
4467 const char *secname
;
4470 struct bfd_link_hash_entry
*h
;
4472 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
4473 if (h
== (struct bfd_link_hash_entry
*) NULL
)
4474 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4476 if (h
->type
== bfd_link_hash_new
4477 || h
->type
== bfd_link_hash_undefined
)
4481 h
->type
= bfd_link_hash_defined
;
4483 sec
= bfd_get_section_by_name (output_bfd
, secname
);
4484 if (sec
== (asection
*) NULL
)
4487 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
4488 + bfd_section_size (output_bfd
, sec
) /
4489 bfd_octets_per_byte (output_bfd
));
4491 h
->u
.def
.section
= bfd_abs_section_ptr
;
4496 lang_statement_append (list
, element
, field
)
4497 lang_statement_list_type
*list
;
4498 lang_statement_union_type
*element
;
4499 lang_statement_union_type
**field
;
4501 *(list
->tail
) = element
;
4505 /* Set the output format type. -oformat overrides scripts. */
4508 lang_add_output_format (format
, big
, little
, from_script
)
4514 if (output_target
== NULL
|| !from_script
)
4516 if (command_line
.endian
== ENDIAN_BIG
4519 else if (command_line
.endian
== ENDIAN_LITTLE
4523 output_target
= format
;
4527 /* Enter a group. This creates a new lang_group_statement, and sets
4528 stat_ptr to build new statements within the group. */
4533 lang_group_statement_type
*g
;
4535 g
= new_stat (lang_group_statement
, stat_ptr
);
4536 lang_list_init (&g
->children
);
4537 stat_ptr
= &g
->children
;
4540 /* Leave a group. This just resets stat_ptr to start writing to the
4541 regular list of statements again. Note that this will not work if
4542 groups can occur inside anything else which can adjust stat_ptr,
4543 but currently they can't. */
4548 stat_ptr
= &statement_list
;
4551 /* Add a new program header. This is called for each entry in a PHDRS
4552 command in a linker script. */
4555 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
4563 struct lang_phdr
*n
, **pp
;
4565 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
4568 n
->type
= exp_get_value_int (type
, 0, "program header type",
4569 lang_final_phase_enum
);
4570 n
->filehdr
= filehdr
;
4575 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
4580 /* Record the program header information in the output BFD. FIXME: We
4581 should not be calling an ELF specific function here. */
4584 lang_record_phdrs ()
4588 struct lang_output_section_phdr_list
*last
;
4589 struct lang_phdr
*l
;
4590 lang_statement_union_type
*u
;
4593 secs
= (asection
**) xmalloc (alc
* sizeof (asection
*));
4595 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
4602 for (u
= lang_output_section_statement
.head
;
4604 u
= u
->output_section_statement
.next
)
4606 lang_output_section_statement_type
*os
;
4607 struct lang_output_section_phdr_list
*pl
;
4609 os
= &u
->output_section_statement
;
4616 if (os
->sectype
== noload_section
4617 || os
->bfd_section
== NULL
4618 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
4623 if (os
->bfd_section
== NULL
)
4626 for (; pl
!= NULL
; pl
= pl
->next
)
4628 if (strcmp (pl
->name
, l
->name
) == 0)
4633 secs
= ((asection
**)
4634 xrealloc (secs
, alc
* sizeof (asection
*)));
4636 secs
[c
] = os
->bfd_section
;
4643 if (l
->flags
== NULL
)
4646 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
4647 lang_final_phase_enum
);
4652 at
= exp_get_vma (l
->at
, 0, "phdr load address",
4653 lang_final_phase_enum
);
4655 if (! bfd_record_phdr (output_bfd
, l
->type
,
4656 l
->flags
!= NULL
, flags
, l
->at
!= NULL
,
4657 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
4658 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4663 /* Make sure all the phdr assignments succeeded. */
4664 for (u
= lang_output_section_statement
.head
;
4666 u
= u
->output_section_statement
.next
)
4668 struct lang_output_section_phdr_list
*pl
;
4670 if (u
->output_section_statement
.bfd_section
== NULL
)
4673 for (pl
= u
->output_section_statement
.phdrs
;
4676 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
4677 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4678 u
->output_section_statement
.name
, pl
->name
);
4682 /* Record a list of sections which may not be cross referenced. */
4685 lang_add_nocrossref (l
)
4686 struct lang_nocrossref
*l
;
4688 struct lang_nocrossrefs
*n
;
4690 n
= (struct lang_nocrossrefs
*) xmalloc (sizeof *n
);
4691 n
->next
= nocrossref_list
;
4693 nocrossref_list
= n
;
4695 /* Set notice_all so that we get informed about all symbols. */
4696 link_info
.notice_all
= true;
4699 /* Overlay handling. We handle overlays with some static variables. */
4701 /* The overlay virtual address. */
4702 static etree_type
*overlay_vma
;
4704 /* The overlay load address. */
4705 static etree_type
*overlay_lma
;
4707 /* Whether nocrossrefs is set for this overlay. */
4708 static int overlay_nocrossrefs
;
4710 /* An expression for the maximum section size seen so far. */
4711 static etree_type
*overlay_max
;
4713 /* A list of all the sections in this overlay. */
4715 struct overlay_list
{
4716 struct overlay_list
*next
;
4717 lang_output_section_statement_type
*os
;
4720 static struct overlay_list
*overlay_list
;
4722 /* Start handling an overlay. */
4725 lang_enter_overlay (vma_expr
, lma_expr
, nocrossrefs
)
4726 etree_type
*vma_expr
;
4727 etree_type
*lma_expr
;
4730 /* The grammar should prevent nested overlays from occurring. */
4731 ASSERT (overlay_vma
== NULL
4732 && overlay_lma
== NULL
4733 && overlay_list
== NULL
4734 && overlay_max
== NULL
);
4736 overlay_vma
= vma_expr
;
4737 overlay_lma
= lma_expr
;
4738 overlay_nocrossrefs
= nocrossrefs
;
4741 /* Start a section in an overlay. We handle this by calling
4742 lang_enter_output_section_statement with the correct VMA and LMA. */
4745 lang_enter_overlay_section (name
)
4748 struct overlay_list
*n
;
4751 lang_enter_output_section_statement (name
, overlay_vma
, normal_section
,
4752 0, 0, 0, overlay_lma
);
4754 /* If this is the first section, then base the VMA and LMA of future
4755 sections on this one. This will work correctly even if `.' is
4756 used in the addresses. */
4757 if (overlay_list
== NULL
)
4759 overlay_vma
= exp_nameop (ADDR
, name
);
4760 overlay_lma
= exp_nameop (LOADADDR
, name
);
4763 /* Remember the section. */
4764 n
= (struct overlay_list
*) xmalloc (sizeof *n
);
4765 n
->os
= current_section
;
4766 n
->next
= overlay_list
;
4769 size
= exp_nameop (SIZEOF
, name
);
4771 /* Adjust the LMA for the next section. */
4772 overlay_lma
= exp_binop ('+', overlay_lma
, size
);
4774 /* Arrange to work out the maximum section end address. */
4775 if (overlay_max
== NULL
)
4778 overlay_max
= exp_binop (MAX_K
, overlay_max
, size
);
4781 /* Finish a section in an overlay. There isn't any special to do
4785 lang_leave_overlay_section (fill
, phdrs
)
4787 struct lang_output_section_phdr_list
*phdrs
;
4794 name
= current_section
->name
;
4796 lang_leave_output_section_statement (fill
, "*default*",
4797 phdrs
, "*default*");
4799 /* Define the magic symbols. */
4801 clean
= xmalloc (strlen (name
) + 1);
4803 for (s1
= name
; *s1
!= '\0'; s1
++)
4804 if (ISALNUM (*s1
) || *s1
== '_')
4808 buf
= xmalloc (strlen (clean
) + sizeof "__load_start_");
4809 sprintf (buf
, "__load_start_%s", clean
);
4810 lang_add_assignment (exp_assop ('=', buf
,
4811 exp_nameop (LOADADDR
, name
)));
4813 buf
= xmalloc (strlen (clean
) + sizeof "__load_stop_");
4814 sprintf (buf
, "__load_stop_%s", clean
);
4815 lang_add_assignment (exp_assop ('=', buf
,
4817 exp_nameop (LOADADDR
, name
),
4818 exp_nameop (SIZEOF
, name
))));
4823 /* Finish an overlay. If there are any overlay wide settings, this
4824 looks through all the sections in the overlay and sets them. */
4827 lang_leave_overlay (fill
, memspec
, phdrs
, lma_memspec
)
4829 const char *memspec
;
4830 struct lang_output_section_phdr_list
*phdrs
;
4831 const char *lma_memspec
;
4833 lang_memory_region_type
*region
;
4834 lang_memory_region_type
* default_region
;
4835 lang_memory_region_type
*lma_region
;
4836 struct overlay_list
*l
;
4837 struct lang_nocrossref
*nocrossref
;
4839 default_region
= lang_memory_region_lookup ("*default*");
4841 if (memspec
== NULL
)
4844 region
= lang_memory_region_lookup (memspec
);
4846 if (lma_memspec
== NULL
)
4849 lma_region
= lang_memory_region_lookup (lma_memspec
);
4856 struct overlay_list
*next
;
4858 if (fill
!= 0 && l
->os
->fill
== 0)
4861 /* Assign a region to the sections, if one has been specified.
4862 Override the assignment of the default section, but not
4864 if (region
!= NULL
&&
4865 (l
->os
->region
== NULL
||
4866 l
->os
->region
== default_region
))
4867 l
->os
->region
= region
;
4869 /* We only set lma_region for the first overlay section, as
4870 subsequent overlay sections will have load_base set relative
4871 to the first section. Also, don't set lma_region if
4872 load_base is specified. FIXME: There should really be a test
4873 that `AT ( LDADDR )' doesn't conflict with `AT >LMA_REGION'
4874 rather than letting LDADDR simply override LMA_REGION. */
4875 if (lma_region
!= NULL
&& l
->os
->lma_region
== NULL
4876 && l
->next
== NULL
&& l
->os
->load_base
== NULL
)
4877 l
->os
->lma_region
= lma_region
;
4879 if (phdrs
!= NULL
&& l
->os
->phdrs
== NULL
)
4880 l
->os
->phdrs
= phdrs
;
4882 if (overlay_nocrossrefs
)
4884 struct lang_nocrossref
*nc
;
4886 nc
= (struct lang_nocrossref
*) xmalloc (sizeof *nc
);
4887 nc
->name
= l
->os
->name
;
4888 nc
->next
= nocrossref
;
4897 if (nocrossref
!= NULL
)
4898 lang_add_nocrossref (nocrossref
);
4900 /* Update . for the end of the overlay. */
4901 lang_add_assignment (exp_assop ('=', ".",
4902 exp_binop ('+', overlay_vma
, overlay_max
)));
4906 overlay_nocrossrefs
= 0;
4907 overlay_list
= NULL
;
4911 /* Version handling. This is only useful for ELF. */
4913 /* This global variable holds the version tree that we build. */
4915 struct bfd_elf_version_tree
*lang_elf_version_info
;
4918 lang_vers_match_lang_c (expr
, sym
)
4919 struct bfd_elf_version_expr
*expr
;
4922 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4924 return fnmatch (expr
->pattern
, sym
, 0) == 0;
4928 lang_vers_match_lang_cplusplus (expr
, sym
)
4929 struct bfd_elf_version_expr
*expr
;
4935 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4938 alt_sym
= cplus_demangle (sym
, /* DMGL_NO_TPARAMS */ 0);
4941 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4942 Should we early out false in this case? */
4943 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4947 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4955 lang_vers_match_lang_java (expr
, sym
)
4956 struct bfd_elf_version_expr
*expr
;
4962 if (expr
->pattern
[0] == '*' && expr
->pattern
[1] == '\0')
4965 alt_sym
= cplus_demangle (sym
, DMGL_JAVA
);
4968 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4969 Should we early out false in this case? */
4970 result
= fnmatch (expr
->pattern
, sym
, 0) == 0;
4974 result
= fnmatch (expr
->pattern
, alt_sym
, 0) == 0;
4981 /* This is called for each variable name or match expression. */
4983 struct bfd_elf_version_expr
*
4984 lang_new_vers_regex (orig
, new, lang
)
4985 struct bfd_elf_version_expr
*orig
;
4989 struct bfd_elf_version_expr
*ret
;
4991 ret
= (struct bfd_elf_version_expr
*) xmalloc (sizeof *ret
);
4995 if (lang
== NULL
|| strcasecmp (lang
, "C") == 0)
4996 ret
->match
= lang_vers_match_lang_c
;
4997 else if (strcasecmp (lang
, "C++") == 0)
4998 ret
->match
= lang_vers_match_lang_cplusplus
;
4999 else if (strcasecmp (lang
, "Java") == 0)
5000 ret
->match
= lang_vers_match_lang_java
;
5003 einfo (_("%X%P: unknown language `%s' in version information\n"),
5005 ret
->match
= lang_vers_match_lang_c
;
5011 /* This is called for each set of variable names and match
5014 struct bfd_elf_version_tree
*
5015 lang_new_vers_node (globals
, locals
)
5016 struct bfd_elf_version_expr
*globals
;
5017 struct bfd_elf_version_expr
*locals
;
5019 struct bfd_elf_version_tree
*ret
;
5021 ret
= (struct bfd_elf_version_tree
*) xmalloc (sizeof *ret
);
5025 ret
->globals
= globals
;
5026 ret
->locals
= locals
;
5028 ret
->name_indx
= (unsigned int) -1;
5033 /* This static variable keeps track of version indices. */
5035 static int version_index
;
5037 /* This is called when we know the name and dependencies of the
5041 lang_register_vers_node (name
, version
, deps
)
5043 struct bfd_elf_version_tree
*version
;
5044 struct bfd_elf_version_deps
*deps
;
5046 struct bfd_elf_version_tree
*t
, **pp
;
5047 struct bfd_elf_version_expr
*e1
;
5052 if ((name
[0] == '\0' && lang_elf_version_info
!= NULL
)
5053 || (lang_elf_version_info
&& lang_elf_version_info
->name
[0] == '\0'))
5055 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5059 /* Make sure this node has a unique name. */
5060 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5061 if (strcmp (t
->name
, name
) == 0)
5062 einfo (_("%X%P: duplicate version tag `%s'\n"), name
);
5064 /* Check the global and local match names, and make sure there
5065 aren't any duplicates. */
5067 for (e1
= version
->globals
; e1
!= NULL
; e1
= e1
->next
)
5069 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5071 struct bfd_elf_version_expr
*e2
;
5073 for (e2
= t
->locals
; e2
!= NULL
; e2
= e2
->next
)
5074 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
5075 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5080 for (e1
= version
->locals
; e1
!= NULL
; e1
= e1
->next
)
5082 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5084 struct bfd_elf_version_expr
*e2
;
5086 for (e2
= t
->globals
; e2
!= NULL
; e2
= e2
->next
)
5087 if (strcmp (e1
->pattern
, e2
->pattern
) == 0)
5088 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5093 version
->deps
= deps
;
5094 version
->name
= name
;
5095 if (name
[0] != '\0')
5098 version
->vernum
= version_index
;
5101 version
->vernum
= 0;
5103 for (pp
= &lang_elf_version_info
; *pp
!= NULL
; pp
= &(*pp
)->next
)
5108 /* This is called when we see a version dependency. */
5110 struct bfd_elf_version_deps
*
5111 lang_add_vers_depend (list
, name
)
5112 struct bfd_elf_version_deps
*list
;
5115 struct bfd_elf_version_deps
*ret
;
5116 struct bfd_elf_version_tree
*t
;
5118 ret
= (struct bfd_elf_version_deps
*) xmalloc (sizeof *ret
);
5121 for (t
= lang_elf_version_info
; t
!= NULL
; t
= t
->next
)
5123 if (strcmp (t
->name
, name
) == 0)
5125 ret
->version_needed
= t
;
5130 einfo (_("%X%P: unable to find version dependency `%s'\n"), name
);
5136 lang_do_version_exports_section ()
5138 struct bfd_elf_version_expr
*greg
= NULL
, *lreg
;
5140 LANG_FOR_EACH_INPUT_STATEMENT (is
)
5142 asection
*sec
= bfd_get_section_by_name (is
->the_bfd
, ".exports");
5149 len
= bfd_section_size (is
->the_bfd
, sec
);
5150 contents
= xmalloc (len
);
5151 if (!bfd_get_section_contents (is
->the_bfd
, sec
, contents
, 0, len
))
5152 einfo (_("%X%P: unable to read .exports section contents\n"), sec
);
5155 while (p
< contents
+ len
)
5157 greg
= lang_new_vers_regex (greg
, p
, NULL
);
5158 p
= strchr (p
, '\0') + 1;
5161 /* Do not free the contents, as we used them creating the regex. */
5163 /* Do not include this section in the link. */
5164 bfd_set_section_flags (is
->the_bfd
, sec
,
5165 bfd_get_section_flags (is
->the_bfd
, sec
) | SEC_EXCLUDE
);
5168 lreg
= lang_new_vers_regex (NULL
, "*", NULL
);
5169 lang_register_vers_node (command_line
.version_exports_section
,
5170 lang_new_vers_node (greg
, lreg
), NULL
);
5174 lang_add_unique (name
)
5177 struct unique_sections
*ent
;
5179 for (ent
= unique_section_list
; ent
; ent
= ent
->next
)
5180 if (strcmp (ent
->name
, name
) == 0)
5183 ent
= (struct unique_sections
*) xmalloc (sizeof *ent
);
5184 ent
->name
= xstrdup (name
);
5185 ent
->next
= unique_section_list
;
5186 unique_section_list
= ent
;