1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "libiberty.h"
38 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
40 lang_statement_list_type
*));
44 static struct obstack stat_obstack
;
46 #define obstack_chunk_alloc xmalloc
47 #define obstack_chunk_free free
48 static CONST
char *startup_file
;
49 static lang_statement_list_type input_file_chain
;
50 static boolean placed_commons
= false;
51 static lang_output_section_statement_type
*default_common_section
;
52 static boolean map_option_f
;
53 static bfd_vma print_dot
;
54 static lang_input_statement_type
*first_file
;
55 static lang_statement_list_type lang_output_section_statement
;
56 static CONST
char *current_target
;
57 static CONST
char *output_target
;
58 static lang_statement_list_type statement_list
;
59 static struct lang_phdr
*lang_phdr_list
;
61 static void lang_for_each_statement_worker
62 PARAMS ((void (*func
) (lang_statement_union_type
*),
63 lang_statement_union_type
*s
));
64 static lang_input_statement_type
*new_afile
65 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
66 const char *target
, boolean add_to_list
));
67 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
68 static void exp_init_os
PARAMS ((etree_type
*));
69 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
70 static boolean wildcardp
PARAMS ((const char *));
71 static void wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
73 lang_input_statement_type
*file
,
74 lang_output_section_statement_type
*output
));
75 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
76 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
77 lang_statement_list_type
*));
78 static void wild_file
PARAMS ((lang_wild_statement_type
*, const char *,
79 lang_input_statement_type
*,
80 lang_output_section_statement_type
*));
81 static void wild
PARAMS ((lang_wild_statement_type
*s
,
82 const char *section
, const char *file
,
84 lang_output_section_statement_type
*output
));
85 static bfd
*open_output
PARAMS ((const char *name
));
86 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
87 static void open_input_bfds
88 PARAMS ((lang_statement_union_type
*statement
, boolean
));
89 static void lang_reasonable_defaults
PARAMS ((void));
90 static void lang_place_undefineds
PARAMS ((void));
91 static void map_input_to_output_sections
92 PARAMS ((lang_statement_union_type
*s
,
94 lang_output_section_statement_type
*output_section_statement
));
95 static void print_output_section_statement
96 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
97 static void print_assignment
98 PARAMS ((lang_assignment_statement_type
*assignment
,
99 lang_output_section_statement_type
*output_section
));
100 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
101 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
102 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
103 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
104 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
105 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
106 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
107 static void print_wild_statement
108 PARAMS ((lang_wild_statement_type
*w
,
109 lang_output_section_statement_type
*os
));
110 static void print_group
111 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
112 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
113 lang_output_section_statement_type
*os
));
114 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
115 lang_output_section_statement_type
*os
));
116 static void print_statements
PARAMS ((void));
117 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
118 fill_type fill
, unsigned int power
,
119 asection
*output_section_statement
,
121 static bfd_vma size_input_section
122 PARAMS ((lang_statement_union_type
**this_ptr
,
123 lang_output_section_statement_type
*output_section_statement
,
124 fill_type fill
, bfd_vma dot
, boolean relax
));
125 static void lang_finish
PARAMS ((void));
126 static void lang_check
PARAMS ((void));
127 static void lang_common
PARAMS ((void));
128 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
129 static void lang_place_orphans
PARAMS ((void));
130 static int topower
PARAMS ((int));
131 static void lang_set_startof
PARAMS ((void));
132 static void reset_memory_regions
PARAMS ((void));
133 static void lang_record_phdrs
PARAMS ((void));
136 lang_output_section_statement_type
*abs_output_section
;
137 lang_statement_list_type
*stat_ptr
= &statement_list
;
138 lang_statement_list_type file_chain
= { 0 };
139 const char *entry_symbol
= NULL
;
140 boolean entry_from_cmdline
;
141 boolean lang_has_input_file
= false;
142 boolean had_output_filename
= false;
143 boolean lang_float_flag
= false;
144 boolean delete_output_file_on_failure
= false;
146 etree_type
*base
; /* Relocation base - or null */
149 #if defined(__STDC__) || defined(ALMOST_STDC)
150 #define cat(a,b) a##b
152 #define cat(a,b) a/**/b
155 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
157 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
159 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
161 #define SECTION_NAME_MAP_LENGTH (16)
167 return obstack_alloc (&stat_obstack
, size
);
170 /*----------------------------------------------------------------------
171 lang_for_each_statement walks the parse tree and calls the provided
172 function for each node
176 lang_for_each_statement_worker (func
, s
)
177 void (*func
) PARAMS ((lang_statement_union_type
*));
178 lang_statement_union_type
*s
;
180 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
184 switch (s
->header
.type
)
186 case lang_constructors_statement_enum
:
187 lang_for_each_statement_worker (func
, constructor_list
.head
);
189 case lang_output_section_statement_enum
:
190 lang_for_each_statement_worker
192 s
->output_section_statement
.children
.head
);
194 case lang_wild_statement_enum
:
195 lang_for_each_statement_worker
197 s
->wild_statement
.children
.head
);
199 case lang_group_statement_enum
:
200 lang_for_each_statement_worker (func
,
201 s
->group_statement
.children
.head
);
203 case lang_data_statement_enum
:
204 case lang_reloc_statement_enum
:
205 case lang_object_symbols_statement_enum
:
206 case lang_output_statement_enum
:
207 case lang_target_statement_enum
:
208 case lang_input_section_enum
:
209 case lang_input_statement_enum
:
210 case lang_assignment_statement_enum
:
211 case lang_padding_statement_enum
:
212 case lang_address_statement_enum
:
213 case lang_fill_statement_enum
:
223 lang_for_each_statement (func
)
224 void (*func
) PARAMS ((lang_statement_union_type
*));
226 lang_for_each_statement_worker (func
,
227 statement_list
.head
);
230 /*----------------------------------------------------------------------*/
232 lang_list_init (list
)
233 lang_statement_list_type
*list
;
235 list
->head
= (lang_statement_union_type
*) NULL
;
236 list
->tail
= &list
->head
;
239 /*----------------------------------------------------------------------
241 build a new statement node for the parse tree
246 lang_statement_union_type
*
247 new_statement (type
, size
, list
)
248 enum statement_enum type
;
250 lang_statement_list_type
* list
;
252 lang_statement_union_type
*new = (lang_statement_union_type
*)
255 new->header
.type
= type
;
256 new->header
.next
= (lang_statement_union_type
*) NULL
;
257 lang_statement_append (list
, new, &new->header
.next
);
262 Build a new input file node for the language. There are several ways
263 in which we treat an input file, eg, we only look at symbols, or
264 prefix it with a -l etc.
266 We can be supplied with requests for input files more than once;
267 they may, for example be split over serveral lines like foo.o(.text)
268 foo.o(.data) etc, so when asked for a file we check that we havn't
269 got it already so we don't duplicate the bfd.
272 static lang_input_statement_type
*
273 new_afile (name
, file_type
, target
, add_to_list
)
275 lang_input_file_enum_type file_type
;
279 lang_input_statement_type
*p
;
282 p
= new_stat (lang_input_statement
, stat_ptr
);
285 p
= ((lang_input_statement_type
*)
286 stat_alloc (sizeof (lang_input_statement_type
)));
287 p
->header
.next
= NULL
;
290 lang_has_input_file
= true;
294 case lang_input_file_is_symbols_only_enum
:
296 p
->is_archive
= false;
298 p
->local_sym_name
= name
;
299 p
->just_syms_flag
= true;
300 p
->search_dirs_flag
= false;
302 case lang_input_file_is_fake_enum
:
304 p
->is_archive
= false;
306 p
->local_sym_name
= name
;
307 p
->just_syms_flag
= false;
308 p
->search_dirs_flag
= false;
310 case lang_input_file_is_l_enum
:
311 p
->is_archive
= true;
314 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
315 p
->just_syms_flag
= false;
316 p
->search_dirs_flag
= true;
318 case lang_input_file_is_marker_enum
:
320 p
->is_archive
= false;
322 p
->local_sym_name
= name
;
323 p
->just_syms_flag
= false;
324 p
->search_dirs_flag
= true;
326 case lang_input_file_is_search_file_enum
:
328 p
->is_archive
= false;
330 p
->local_sym_name
= name
;
331 p
->just_syms_flag
= false;
332 p
->search_dirs_flag
= true;
334 case lang_input_file_is_file_enum
:
336 p
->is_archive
= false;
338 p
->local_sym_name
= name
;
339 p
->just_syms_flag
= false;
340 p
->search_dirs_flag
= false;
345 p
->the_bfd
= (bfd
*) NULL
;
346 p
->asymbols
= (asymbol
**) NULL
;
347 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
348 p
->next
= (lang_statement_union_type
*) NULL
;
350 p
->dynamic
= config
.dynamic_link
;
351 p
->whole_archive
= whole_archive
;
353 lang_statement_append (&input_file_chain
,
354 (lang_statement_union_type
*) p
,
359 lang_input_statement_type
*
360 lang_add_input_file (name
, file_type
, target
)
362 lang_input_file_enum_type file_type
;
365 lang_has_input_file
= true;
366 return new_afile (name
, file_type
, target
, true);
369 /* Build enough state so that the parser can build its tree */
373 obstack_begin (&stat_obstack
, 1000);
375 stat_ptr
= &statement_list
;
377 lang_list_init (stat_ptr
);
379 lang_list_init (&input_file_chain
);
380 lang_list_init (&lang_output_section_statement
);
381 lang_list_init (&file_chain
);
382 first_file
= lang_add_input_file ((char *) NULL
,
383 lang_input_file_is_marker_enum
,
385 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
387 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
391 /*----------------------------------------------------------------------
392 A region is an area of memory declared with the
393 MEMORY { name:org=exp, len=exp ... }
396 We maintain a list of all the regions here
398 If no regions are specified in the script, then the default is used
399 which is created when looked up to be the entire data space
402 static lang_memory_region_type
*lang_memory_region_list
;
403 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
405 lang_memory_region_type
*
406 lang_memory_region_lookup (name
)
407 CONST
char *CONST name
;
410 lang_memory_region_type
*p
= lang_memory_region_list
;
412 for (p
= lang_memory_region_list
;
413 p
!= (lang_memory_region_type
*) NULL
;
416 if (strcmp (p
->name
, name
) == 0)
423 /* This code used to always use the first region in the list as the
424 default region. I changed it to instead use a region
425 encompassing all of memory as the default region. This permits
426 NOLOAD sections to work reasonably without requiring a region.
427 People should specify what region they mean, if they really want
429 if (strcmp (name
, "*default*") == 0)
431 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
433 return lang_memory_region_list
;
439 lang_memory_region_type
*new =
440 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
442 new->name
= buystring (name
);
443 new->next
= (lang_memory_region_type
*) NULL
;
445 *lang_memory_region_list_tail
= new;
446 lang_memory_region_list_tail
= &new->next
;
448 new->length
= ~(bfd_size_type
)0;
450 new->had_full_message
= false;
457 lang_output_section_statement_type
*
458 lang_output_section_find (name
)
459 CONST
char *CONST name
;
461 lang_statement_union_type
*u
;
462 lang_output_section_statement_type
*lookup
;
464 for (u
= lang_output_section_statement
.head
;
465 u
!= (lang_statement_union_type
*) NULL
;
468 lookup
= &u
->output_section_statement
;
469 if (strcmp (name
, lookup
->name
) == 0)
474 return (lang_output_section_statement_type
*) NULL
;
477 lang_output_section_statement_type
*
478 lang_output_section_statement_lookup (name
)
479 CONST
char *CONST name
;
481 lang_output_section_statement_type
*lookup
;
483 lookup
= lang_output_section_find (name
);
484 if (lookup
== (lang_output_section_statement_type
*) NULL
)
487 lookup
= (lang_output_section_statement_type
*)
488 new_stat (lang_output_section_statement
, stat_ptr
);
489 lookup
->region
= (lang_memory_region_type
*) NULL
;
491 lookup
->block_value
= 1;
494 lookup
->next
= (lang_statement_union_type
*) NULL
;
495 lookup
->bfd_section
= (asection
*) NULL
;
496 lookup
->processed
= false;
497 lookup
->sectype
= normal_section
;
498 lookup
->addr_tree
= (etree_type
*) NULL
;
499 lang_list_init (&lookup
->children
);
501 lookup
->memspec
= (CONST
char *) NULL
;
503 lookup
->subsection_alignment
= -1;
504 lookup
->section_alignment
= -1;
505 lookup
->load_base
= (union etree_union
*) NULL
;
506 lookup
->phdrs
= NULL
;
508 lang_statement_append (&lang_output_section_statement
,
509 (lang_statement_union_type
*) lookup
,
518 lang_memory_region_type
*m
;
520 minfo ("\nMemory Configuration\n\n");
521 fprintf (config
.map_file
, "%-16s %-18s %-18s\n",
522 "Name", "Origin", "Length");
524 for (m
= lang_memory_region_list
;
525 m
!= (lang_memory_region_type
*) NULL
;
531 fprintf (config
.map_file
, "%-16s ", m
->name
);
533 sprintf_vma (buf
, m
->origin
);
534 minfo ("0x%s ", buf
);
542 minfo ("0x%V\n", m
->length
);
545 fprintf (config
.map_file
, "\nLinker script and memory map\n\n");
550 /* Initialize an output section. */
554 lang_output_section_statement_type
*s
;
556 section_userdata_type
*new;
558 if (s
->bfd_section
!= NULL
)
561 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
562 einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME
);
564 new = ((section_userdata_type
*)
565 stat_alloc (sizeof (section_userdata_type
)));
567 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
568 if (s
->bfd_section
== (asection
*) NULL
)
569 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
570 if (s
->bfd_section
== (asection
*) NULL
)
572 einfo ("%P%F: output format %s cannot represent section called %s\n",
573 output_bfd
->xvec
->name
, s
->name
);
575 s
->bfd_section
->output_section
= s
->bfd_section
;
577 /* We initialize an output sections output offset to minus its own */
578 /* vma to allow us to output a section through itself */
579 s
->bfd_section
->output_offset
= 0;
580 get_userdata (s
->bfd_section
) = (PTR
) new;
582 /* If there is a base address, make sure that any sections it might
583 mention are initialized. */
584 if (s
->addr_tree
!= NULL
)
585 exp_init_os (s
->addr_tree
);
588 /* Make sure that all output sections mentioned in an expression are
595 switch (exp
->type
.node_class
)
598 exp_init_os (exp
->assign
.src
);
602 exp_init_os (exp
->binary
.lhs
);
603 exp_init_os (exp
->binary
.rhs
);
607 exp_init_os (exp
->trinary
.cond
);
608 exp_init_os (exp
->trinary
.lhs
);
609 exp_init_os (exp
->trinary
.rhs
);
613 exp_init_os (exp
->unary
.child
);
617 switch (exp
->type
.node_code
)
622 lang_output_section_statement_type
*os
;
624 os
= lang_output_section_find (exp
->name
.name
);
625 if (os
!= NULL
&& os
->bfd_section
== NULL
)
636 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
637 once into the output. This routine checks each sections, and
638 arranges to discard it if a section of the same name has already
639 been linked. This code assumes that all relevant sections have the
640 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
641 section name. This is called via bfd_map_over_sections. */
645 section_already_linked (abfd
, sec
, ignore
)
652 struct sec_link_once
*next
;
655 static struct sec_link_once
*sec_link_once_list
;
658 struct sec_link_once
*l
;
660 flags
= bfd_get_section_flags (abfd
, sec
);
662 if ((flags
& SEC_LINK_ONCE
) == 0)
665 name
= bfd_get_section_name (abfd
, sec
);
667 for (l
= sec_link_once_list
; l
!= NULL
; l
= l
->next
)
669 if (strcmp (name
, bfd_get_section_name (l
->sec
->owner
, l
->sec
)) == 0)
671 /* The section has already been linked. See if we should
673 switch (flags
& SEC_LINK_DUPLICATES
)
678 case SEC_LINK_DUPLICATES_DISCARD
:
681 case SEC_LINK_DUPLICATES_ONE_ONLY
:
682 einfo ("%P: %B: warning: ignoring duplicate section `%s'\n",
686 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
687 /* FIXME: We should really dig out the contents of both
688 sections and memcmp them. The COFF/PE spec says that
689 the Microsoft linker does not implement this
690 correctly, so I'm not going to bother doing it
693 case SEC_LINK_DUPLICATES_SAME_SIZE
:
694 if (bfd_section_size (abfd
, sec
)
695 != bfd_section_size (l
->sec
->owner
, l
->sec
))
696 einfo ("%P: %B: warning: duplicate section `%s' has different size\n",
701 /* Set the output_section field so that wild_doit does not
702 create a lang_input_section structure for this section. */
703 sec
->output_section
= bfd_abs_section_ptr
;
709 /* This is the first section with this name. Record it. */
711 l
= (struct sec_link_once
*) xmalloc (sizeof *l
);
713 l
->next
= sec_link_once_list
;
714 sec_link_once_list
= l
;
717 /* The wild routines.
719 These expand statements like *(.text) and foo.o to a list of
720 explicit actions, like foo.o(.text), bar.o(.text) and
721 foo.o(.text, .data). */
723 /* Return true if the PATTERN argument is a wildcard pattern. */
731 for (s
= pattern
; *s
!= '\0'; ++s
)
740 /* Add SECTION to the output section OUTPUT. Do this by creating a
741 lang_input_section statement which is placed at PTR. FILE is the
742 input file which holds SECTION. */
745 wild_doit (ptr
, section
, output
, file
)
746 lang_statement_list_type
*ptr
;
748 lang_output_section_statement_type
*output
;
749 lang_input_statement_type
*file
;
754 flags
= bfd_get_section_flags (section
->owner
, section
);
758 /* If we are doing a final link, discard sections marked with
760 if (! link_info
.relocateable
761 && (flags
& SEC_EXCLUDE
) != 0)
764 /* Discard input sections which are assigned to a section named
765 DISCARD_SECTION_NAME. */
766 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
769 /* Discard debugging sections if we are stripping debugging
771 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
772 && (flags
& SEC_DEBUGGING
) != 0)
777 if (section
->output_section
== NULL
)
779 /* This prevents future calls from assigning this section. */
780 section
->output_section
= bfd_abs_section_ptr
;
785 if (section
->output_section
== NULL
)
787 lang_input_section_type
*new;
789 if (output
->bfd_section
== NULL
)
792 /* Add a section reference to the list */
793 new = new_stat (lang_input_section
, ptr
);
795 new->section
= section
;
797 section
->output_section
= output
->bfd_section
;
799 /* We don't copy the SEC_NEVER_LOAD flag from an input section
800 to an output section, because we want to be able to include a
801 SEC_NEVER_LOAD section in the middle of an otherwise loaded
802 section (I don't know why we want to do this, but we do).
803 build_link_order in ldwrite.c handles this case by turning
804 the embedded SEC_NEVER_LOAD section into a fill.
806 If final link, don't copy the SEC_LINK_ONCE flags, they've already
807 been processed. One reason to do this is that on pe format targets,
808 .text$foo sections go into .text and it's odd to see .text with
809 SEC_LINK_ONCE set. */
811 section
->output_section
->flags
|=
812 section
->flags
& (flagword
) (~ (SEC_NEVER_LOAD
813 | (! link_info
.relocateable
814 ? SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
817 switch (output
->sectype
)
824 case overlay_section
:
825 output
->bfd_section
->flags
&= ~SEC_ALLOC
;
828 output
->bfd_section
->flags
&= ~SEC_LOAD
;
829 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
833 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
834 output
->bfd_section
->alignment_power
= section
->alignment_power
;
836 /* If supplied an aligment, then force it. */
837 if (output
->section_alignment
!= -1)
838 output
->bfd_section
->alignment_power
= output
->section_alignment
;
842 /* Expand a wild statement for a particular FILE. SECTION may be
843 NULL, in which case it is a wild card. */
846 wild_section (ptr
, section
, file
, output
)
847 lang_wild_statement_type
*ptr
;
849 lang_input_statement_type
*file
;
850 lang_output_section_statement_type
*output
;
852 if (file
->just_syms_flag
== false)
854 register asection
*s
;
860 wildcard
= wildcardp (section
);
862 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
866 /* Attach all sections named SECTION. If SECTION is NULL,
867 then attach all sections.
869 Previously, if SECTION was NULL, this code did not call
870 wild_doit if the SEC_IS_COMMON flag was set for the
871 section. I did not understand that, and I took it out.
880 name
= bfd_get_section_name (file
->the_bfd
, s
);
882 match
= fnmatch (section
, name
, 0) == 0 ? true : false;
884 match
= strcmp (section
, name
) == 0 ? true : false;
887 wild_doit (&ptr
->children
, s
, output
, file
);
892 /* This is passed a file name which must have been seen already and
893 added to the statement tree. We will see if it has been opened
894 already and had its symbols read. If not then we'll read it. */
896 static lang_input_statement_type
*
900 lang_input_statement_type
*search
;
902 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
903 search
!= (lang_input_statement_type
*) NULL
;
904 search
= (lang_input_statement_type
*) search
->next_real_file
)
906 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
908 if (search
->filename
!= (char *) NULL
909 && name
!= (char *) NULL
910 && strcmp (search
->filename
, name
) == 0)
914 if (search
== (lang_input_statement_type
*) NULL
)
915 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
918 /* If we have already added this file, or this file is not real
919 (FIXME: can that ever actually happen?) or the name is NULL
920 (FIXME: can that ever actually happen?) don't add this file. */
923 || search
->filename
== (const char *) NULL
)
926 load_symbols (search
, (lang_statement_list_type
*) NULL
);
931 /* Get the symbols for an input file. */
934 load_symbols (entry
, place
)
935 lang_input_statement_type
*entry
;
936 lang_statement_list_type
*place
;
943 ldfile_open_file (entry
);
945 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
946 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
949 lang_statement_list_type
*hold
;
951 err
= bfd_get_error ();
952 if (err
== bfd_error_file_ambiguously_recognized
)
956 einfo ("%B: file not recognized: %E\n", entry
->the_bfd
);
957 einfo ("%B: matching formats:", entry
->the_bfd
);
958 for (p
= matching
; *p
!= NULL
; p
++)
962 else if (err
!= bfd_error_file_not_recognized
964 einfo ("%F%B: file not recognized: %E\n", entry
->the_bfd
);
966 bfd_close (entry
->the_bfd
);
967 entry
->the_bfd
= NULL
;
969 /* See if the emulation has some special knowledge. */
971 if (ldemul_unrecognized_file (entry
))
974 /* Try to interpret the file as a linker script. */
976 ldfile_open_command_file (entry
->filename
);
981 ldfile_assumed_script
= true;
982 parser_input
= input_script
;
984 ldfile_assumed_script
= false;
991 /* We don't call ldlang_add_file for an archive. Instead, the
992 add_symbols entry point will call ldlang_add_file, via the
993 add_archive_element callback, for each element of the archive
995 switch (bfd_get_format (entry
->the_bfd
))
1001 ldlang_add_file (entry
);
1002 if (trace_files
|| trace_file_tries
)
1003 info_msg ("%I\n", entry
);
1007 if (entry
->whole_archive
)
1009 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1011 while (member
!= NULL
)
1013 if (! bfd_check_format (member
, bfd_object
))
1014 einfo ("%F%B: object %B in archive is not object\n",
1015 entry
->the_bfd
, member
);
1016 if (! ((*link_info
.callbacks
->add_archive_element
)
1017 (&link_info
, member
, "--whole-archive")))
1019 if (! bfd_link_add_symbols (member
, &link_info
))
1020 einfo ("%F%B: could not read symbols: %E\n", member
);
1021 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
1025 entry
->loaded
= true;
1031 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
1032 einfo ("%F%B: could not read symbols: %E\n", entry
->the_bfd
);
1034 entry
->loaded
= true;
1037 /* Handle a wild statement for a single file F. */
1040 wild_file (s
, section
, f
, output
)
1041 lang_wild_statement_type
*s
;
1042 const char *section
;
1043 lang_input_statement_type
*f
;
1044 lang_output_section_statement_type
*output
;
1046 if (f
->the_bfd
== NULL
1047 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
1048 wild_section (s
, section
, f
, output
);
1053 /* This is an archive file. We must map each member of the
1054 archive separately. */
1055 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
1056 while (member
!= NULL
)
1058 /* When lookup_name is called, it will call the add_symbols
1059 entry point for the archive. For each element of the
1060 archive which is included, BFD will call ldlang_add_file,
1061 which will set the usrdata field of the member to the
1062 lang_input_statement. */
1063 if (member
->usrdata
!= NULL
)
1065 wild_section (s
, section
,
1066 (lang_input_statement_type
*) member
->usrdata
,
1070 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
1075 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1076 indicating that it is a wildcard. Separate lang_input_section
1077 statements are created for each part of the expansion; they are
1078 added after the wild statement S. OUTPUT is the output section. */
1081 wild (s
, section
, file
, target
, output
)
1082 lang_wild_statement_type
*s
;
1083 const char *section
;
1086 lang_output_section_statement_type
*output
;
1088 lang_input_statement_type
*f
;
1090 if (file
== (char *) NULL
)
1092 /* Perform the iteration over all files in the list */
1093 for (f
= (lang_input_statement_type
*) file_chain
.head
;
1094 f
!= (lang_input_statement_type
*) NULL
;
1095 f
= (lang_input_statement_type
*) f
->next
)
1097 wild_file (s
, section
, f
, output
);
1100 else if (wildcardp (file
))
1102 for (f
= (lang_input_statement_type
*) file_chain
.head
;
1103 f
!= (lang_input_statement_type
*) NULL
;
1104 f
= (lang_input_statement_type
*) f
->next
)
1106 if (fnmatch (file
, f
->filename
, FNM_FILE_NAME
) == 0)
1107 wild_file (s
, section
, f
, output
);
1112 /* Perform the iteration over a single file */
1113 f
= lookup_name (file
);
1114 wild_file (s
, section
, f
, output
);
1117 if (section
!= (char *) NULL
1118 && strcmp (section
, "COMMON") == 0
1119 && default_common_section
== NULL
)
1121 /* Remember the section that common is going to in case we later
1122 get something which doesn't know where to put it. */
1123 default_common_section
= output
;
1127 /* Open the output file. */
1135 if (output_target
== (char *) NULL
)
1137 if (current_target
!= (char *) NULL
)
1138 output_target
= current_target
;
1140 output_target
= default_target
;
1142 output
= bfd_openw (name
, output_target
);
1144 if (output
== (bfd
*) NULL
)
1146 if (bfd_get_error () == bfd_error_invalid_target
)
1148 einfo ("%P%F: target %s not found\n", output_target
);
1150 einfo ("%P%F: cannot open output file %s: %E\n", name
);
1153 delete_output_file_on_failure
= true;
1155 /* output->flags |= D_PAGED;*/
1157 if (! bfd_set_format (output
, bfd_object
))
1158 einfo ("%P%F:%s: can not make object file: %E\n", name
);
1159 if (! bfd_set_arch_mach (output
,
1160 ldfile_output_architecture
,
1161 ldfile_output_machine
))
1162 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
1164 link_info
.hash
= bfd_link_hash_table_create (output
);
1165 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1166 einfo ("%P%F: can not create link hash table: %E\n");
1168 bfd_set_gp_size (output
, g_switch_value
);
1176 ldlang_open_output (statement
)
1177 lang_statement_union_type
* statement
;
1179 switch (statement
->header
.type
)
1181 case lang_output_statement_enum
:
1182 ASSERT (output_bfd
== (bfd
*) NULL
);
1183 output_bfd
= open_output (statement
->output_statement
.name
);
1184 ldemul_set_output_arch ();
1185 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1186 output_bfd
->flags
|= D_PAGED
;
1188 output_bfd
->flags
&= ~D_PAGED
;
1189 if (config
.text_read_only
)
1190 output_bfd
->flags
|= WP_TEXT
;
1192 output_bfd
->flags
&= ~WP_TEXT
;
1193 if (link_info
.traditional_format
)
1194 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1196 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1199 case lang_target_statement_enum
:
1200 current_target
= statement
->target_statement
.target
;
1207 /* Open all the input files. */
1210 open_input_bfds (s
, force
)
1211 lang_statement_union_type
*s
;
1214 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1216 switch (s
->header
.type
)
1218 case lang_constructors_statement_enum
:
1219 open_input_bfds (constructor_list
.head
, force
);
1221 case lang_output_section_statement_enum
:
1222 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1224 case lang_wild_statement_enum
:
1225 /* Maybe we should load the file's symbols */
1226 if (s
->wild_statement
.filename
1227 && ! wildcardp (s
->wild_statement
.filename
))
1228 (void) lookup_name (s
->wild_statement
.filename
);
1229 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1231 case lang_group_statement_enum
:
1233 struct bfd_link_hash_entry
*undefs
;
1235 /* We must continually search the entries in the group
1236 until no new symbols are added to the list of undefined
1241 undefs
= link_info
.hash
->undefs_tail
;
1242 open_input_bfds (s
->group_statement
.children
.head
, true);
1244 while (undefs
!= link_info
.hash
->undefs_tail
);
1247 case lang_target_statement_enum
:
1248 current_target
= s
->target_statement
.target
;
1250 case lang_input_statement_enum
:
1251 if (s
->input_statement
.real
== true)
1253 lang_statement_list_type add
;
1255 s
->input_statement
.target
= current_target
;
1257 /* If we are being called from within a group, and this
1258 is an archive which has already been searched, then
1259 force it to be researched. */
1261 && s
->input_statement
.loaded
1262 && bfd_check_format (s
->input_statement
.the_bfd
,
1264 s
->input_statement
.loaded
= false;
1266 lang_list_init (&add
);
1268 load_symbols (&s
->input_statement
, &add
);
1270 if (add
.head
!= NULL
)
1272 *add
.tail
= s
->next
;
1283 /* If there are [COMMONS] statements, put a wild one into the bss section */
1286 lang_reasonable_defaults ()
1289 lang_output_section_statement_lookup (".text");
1290 lang_output_section_statement_lookup (".data");
1292 default_common_section
=
1293 lang_output_section_statement_lookup (".bss");
1296 if (placed_commons
== false)
1298 lang_wild_statement_type
*new =
1299 new_stat (lang_wild_statement
,
1300 &default_common_section
->children
);
1302 new->section_name
= "COMMON";
1303 new->filename
= (char *) NULL
;
1304 lang_list_init (&new->children
);
1311 Add the supplied name to the symbol table as an undefined reference.
1312 Remove items from the chain as we open input bfds
1314 typedef struct ldlang_undef_chain_list
1316 struct ldlang_undef_chain_list
*next
;
1318 } ldlang_undef_chain_list_type
;
1320 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1323 ldlang_add_undef (name
)
1324 CONST
char *CONST name
;
1326 ldlang_undef_chain_list_type
*new =
1327 ((ldlang_undef_chain_list_type
*)
1328 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1330 new->next
= ldlang_undef_chain_list_head
;
1331 ldlang_undef_chain_list_head
= new;
1333 new->name
= buystring (name
);
1336 /* Run through the list of undefineds created above and place them
1337 into the linker hash table as undefined symbols belonging to the
1341 lang_place_undefineds ()
1343 ldlang_undef_chain_list_type
*ptr
;
1345 for (ptr
= ldlang_undef_chain_list_head
;
1346 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1349 struct bfd_link_hash_entry
*h
;
1351 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1352 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1353 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1354 if (h
->type
== bfd_link_hash_new
)
1356 h
->type
= bfd_link_hash_undefined
;
1357 h
->u
.undef
.abfd
= NULL
;
1358 bfd_link_add_undef (link_info
.hash
, h
);
1363 /* Open input files and attatch to output sections */
1365 map_input_to_output_sections (s
, target
, output_section_statement
)
1366 lang_statement_union_type
* s
;
1368 lang_output_section_statement_type
* output_section_statement
;
1370 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1372 switch (s
->header
.type
)
1376 case lang_wild_statement_enum
:
1377 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1378 s
->wild_statement
.filename
, target
,
1379 output_section_statement
);
1382 case lang_constructors_statement_enum
:
1383 map_input_to_output_sections (constructor_list
.head
,
1385 output_section_statement
);
1387 case lang_output_section_statement_enum
:
1388 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1390 &s
->output_section_statement
);
1392 case lang_output_statement_enum
:
1394 case lang_target_statement_enum
:
1395 target
= s
->target_statement
.target
;
1397 case lang_group_statement_enum
:
1398 map_input_to_output_sections (s
->group_statement
.children
.head
,
1400 output_section_statement
);
1402 case lang_fill_statement_enum
:
1403 case lang_input_section_enum
:
1404 case lang_object_symbols_statement_enum
:
1405 case lang_data_statement_enum
:
1406 case lang_reloc_statement_enum
:
1407 case lang_padding_statement_enum
:
1408 case lang_input_statement_enum
:
1409 if (output_section_statement
!= NULL
1410 && output_section_statement
->bfd_section
== NULL
)
1411 init_os (output_section_statement
);
1413 case lang_assignment_statement_enum
:
1414 if (output_section_statement
!= NULL
1415 && output_section_statement
->bfd_section
== NULL
)
1416 init_os (output_section_statement
);
1418 /* Make sure that any sections mentioned in the assignment
1420 exp_init_os (s
->assignment_statement
.exp
);
1422 case lang_afile_asection_pair_statement_enum
:
1425 case lang_address_statement_enum
:
1426 /* Mark the specified section with the supplied address */
1428 lang_output_section_statement_type
*os
=
1429 lang_output_section_statement_lookup
1430 (s
->address_statement
.section_name
);
1432 if (os
->bfd_section
== NULL
)
1434 os
->addr_tree
= s
->address_statement
.address
;
1442 print_output_section_statement (output_section_statement
)
1443 lang_output_section_statement_type
* output_section_statement
;
1445 asection
*section
= output_section_statement
->bfd_section
;
1448 if (output_section_statement
!= abs_output_section
)
1450 minfo ("\n%s", output_section_statement
->name
);
1452 if (section
!= NULL
)
1454 print_dot
= section
->vma
;
1456 len
= strlen (output_section_statement
->name
);
1457 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
1462 while (len
< SECTION_NAME_MAP_LENGTH
)
1468 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
1470 if (output_section_statement
->load_base
!= NULL
)
1474 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
1475 "load base", lang_final_phase_enum
);
1476 minfo (" load address 0x%V", addr
);
1483 print_statement_list (output_section_statement
->children
.head
,
1484 output_section_statement
);
1488 print_assignment (assignment
, output_section
)
1489 lang_assignment_statement_type
* assignment
;
1490 lang_output_section_statement_type
* output_section
;
1493 etree_value_type result
;
1495 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1498 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
1499 lang_final_phase_enum
, print_dot
, &print_dot
);
1501 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
1512 exp_print_tree (assignment
->exp
);
1518 print_input_statement (statm
)
1519 lang_input_statement_type
* statm
;
1521 if (statm
->filename
!= (char *) NULL
)
1523 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1527 /* Print all symbols defined in a particular section. This is called
1528 via bfd_link_hash_traverse. */
1531 print_one_symbol (hash_entry
, ptr
)
1532 struct bfd_link_hash_entry
*hash_entry
;
1535 asection
*sec
= (asection
*) ptr
;
1537 if ((hash_entry
->type
== bfd_link_hash_defined
1538 || hash_entry
->type
== bfd_link_hash_defweak
)
1539 && sec
== hash_entry
->u
.def
.section
)
1543 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1546 (hash_entry
->u
.def
.value
1547 + hash_entry
->u
.def
.section
->output_offset
1548 + hash_entry
->u
.def
.section
->output_section
->vma
));
1550 minfo (" %T\n", hash_entry
->root
.string
);
1556 /* Print information about an input section to the map file. */
1559 print_input_section (in
)
1560 lang_input_section_type
* in
;
1562 asection
*i
= in
->section
;
1563 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
1569 minfo ("%s", i
->name
);
1571 if (i
->output_section
!= NULL
)
1575 len
= 1 + strlen (i
->name
);
1576 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
1581 while (len
< SECTION_NAME_MAP_LENGTH
)
1587 minfo ("0x%V %W %B\n",
1588 i
->output_section
->vma
+ i
->output_offset
, size
,
1591 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
1593 len
= SECTION_NAME_MAP_LENGTH
+ 3;
1605 minfo ("%W (size before relaxing)\n", i
->_raw_size
);
1608 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
1610 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
;
1616 print_fill_statement (fill
)
1617 lang_fill_statement_type
* fill
;
1619 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
1623 print_data_statement (data
)
1624 lang_data_statement_type
* data
;
1631 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1634 addr
= data
->output_vma
;
1635 if (data
->output_section
!= NULL
)
1636 addr
+= data
->output_section
->vma
;
1660 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
1662 if (data
->exp
->type
.node_class
!= etree_value
)
1665 exp_print_tree (data
->exp
);
1670 print_dot
= addr
+ size
;
1673 /* Print an address statement. These are generated by options like
1677 print_address_statement (address
)
1678 lang_address_statement_type
*address
;
1680 minfo ("Address of section %s set to ", address
->section_name
);
1681 exp_print_tree (address
->address
);
1685 /* Print a reloc statement. */
1688 print_reloc_statement (reloc
)
1689 lang_reloc_statement_type
*reloc
;
1695 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1698 addr
= reloc
->output_vma
;
1699 if (reloc
->output_section
!= NULL
)
1700 addr
+= reloc
->output_section
->vma
;
1702 size
= bfd_get_reloc_size (reloc
->howto
);
1704 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
1706 if (reloc
->name
!= NULL
)
1707 minfo ("%s+", reloc
->name
);
1709 minfo ("%s+", reloc
->section
->name
);
1711 exp_print_tree (reloc
->addend_exp
);
1715 print_dot
= addr
+ size
;
1719 print_padding_statement (s
)
1720 lang_padding_statement_type
*s
;
1727 len
= sizeof " *fill*" - 1;
1728 while (len
< SECTION_NAME_MAP_LENGTH
)
1734 addr
= s
->output_offset
;
1735 if (s
->output_section
!= NULL
)
1736 addr
+= s
->output_section
->vma
;
1737 minfo ("0x%V %W", addr
, s
->size
);
1740 minfo (" %u", s
->fill
);
1744 print_dot
= addr
+ s
->size
;
1748 print_wild_statement (w
, os
)
1749 lang_wild_statement_type
* w
;
1750 lang_output_section_statement_type
* os
;
1754 if (w
->filename
!= NULL
)
1755 minfo ("%s", w
->filename
);
1759 if (w
->section_name
!= NULL
)
1760 minfo ("(%s)", w
->section_name
);
1766 print_statement_list (w
->children
.head
, os
);
1769 /* Print a group statement. */
1773 lang_group_statement_type
*s
;
1774 lang_output_section_statement_type
*os
;
1776 fprintf (config
.map_file
, "START GROUP\n");
1777 print_statement_list (s
->children
.head
, os
);
1778 fprintf (config
.map_file
, "END GROUP\n");
1781 /* Print the list of statements in S.
1782 This can be called for any statement type. */
1785 print_statement_list (s
, os
)
1786 lang_statement_union_type
*s
;
1787 lang_output_section_statement_type
*os
;
1791 print_statement (s
, os
);
1796 /* Print the first statement in statement list S.
1797 This can be called for any statement type. */
1800 print_statement (s
, os
)
1801 lang_statement_union_type
*s
;
1802 lang_output_section_statement_type
*os
;
1804 switch (s
->header
.type
)
1807 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1810 case lang_constructors_statement_enum
:
1811 if (constructor_list
.head
!= NULL
)
1813 minfo (" CONSTRUCTORS\n");
1814 print_statement_list (constructor_list
.head
, os
);
1817 case lang_wild_statement_enum
:
1818 print_wild_statement (&s
->wild_statement
, os
);
1820 case lang_address_statement_enum
:
1821 print_address_statement (&s
->address_statement
);
1823 case lang_object_symbols_statement_enum
:
1824 minfo (" CREATE_OBJECT_SYMBOLS\n");
1826 case lang_fill_statement_enum
:
1827 print_fill_statement (&s
->fill_statement
);
1829 case lang_data_statement_enum
:
1830 print_data_statement (&s
->data_statement
);
1832 case lang_reloc_statement_enum
:
1833 print_reloc_statement (&s
->reloc_statement
);
1835 case lang_input_section_enum
:
1836 print_input_section (&s
->input_section
);
1838 case lang_padding_statement_enum
:
1839 print_padding_statement (&s
->padding_statement
);
1841 case lang_output_section_statement_enum
:
1842 print_output_section_statement (&s
->output_section_statement
);
1844 case lang_assignment_statement_enum
:
1845 print_assignment (&s
->assignment_statement
, os
);
1847 case lang_target_statement_enum
:
1848 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1850 case lang_output_statement_enum
:
1851 minfo ("OUTPUT(%s", s
->output_statement
.name
);
1852 if (output_target
!= NULL
)
1853 minfo (" %s", output_target
);
1856 case lang_input_statement_enum
:
1857 print_input_statement (&s
->input_statement
);
1859 case lang_group_statement_enum
:
1860 print_group (&s
->group_statement
, os
);
1862 case lang_afile_asection_pair_statement_enum
:
1871 print_statement_list (statement_list
.head
, abs_output_section
);
1874 /* Print the first N statements in statement list S to STDERR.
1875 If N == 0, nothing is printed.
1876 If N < 0, the entire list is printed.
1877 Intended to be called from GDB. */
1880 dprint_statement (s
, n
)
1881 lang_statement_union_type
* s
;
1884 FILE *map_save
= config
.map_file
;
1886 config
.map_file
= stderr
;
1889 print_statement_list (s
, abs_output_section
);
1892 while (s
&& --n
>= 0)
1894 print_statement (s
, abs_output_section
);
1899 config
.map_file
= map_save
;
1903 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1904 lang_statement_union_type
** this_ptr
;
1907 asection
* output_section_statement
;
1910 /* Align this section first to the
1911 input sections requirement, then
1912 to the output section's requirement.
1913 If this alignment is > than any seen before,
1914 then record it too. Perform the alignment by
1915 inserting a magic 'padding' statement.
1918 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1920 if (alignment_needed
!= 0)
1922 lang_statement_union_type
*new =
1923 ((lang_statement_union_type
*)
1924 stat_alloc (sizeof (lang_padding_statement_type
)));
1926 /* Link into existing chain */
1927 new->header
.next
= *this_ptr
;
1929 new->header
.type
= lang_padding_statement_enum
;
1930 new->padding_statement
.output_section
= output_section_statement
;
1931 new->padding_statement
.output_offset
=
1932 dot
- output_section_statement
->vma
;
1933 new->padding_statement
.fill
= fill
;
1934 new->padding_statement
.size
= alignment_needed
;
1938 /* Remember the most restrictive alignment */
1939 if (power
> output_section_statement
->alignment_power
)
1941 output_section_statement
->alignment_power
= power
;
1943 output_section_statement
->_raw_size
+= alignment_needed
;
1944 return alignment_needed
+ dot
;
1948 /* Work out how much this section will move the dot point */
1950 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1951 lang_statement_union_type
** this_ptr
;
1952 lang_output_section_statement_type
* output_section_statement
;
1957 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1958 asection
*i
= is
->section
;
1960 if (is
->ifile
->just_syms_flag
== false)
1962 if (output_section_statement
->subsection_alignment
!= -1)
1963 i
->alignment_power
=
1964 output_section_statement
->subsection_alignment
;
1966 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1967 output_section_statement
->bfd_section
, dot
);
1969 /* Remember where in the output section this input section goes */
1971 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1973 /* Mark how big the output section must be to contain this now
1975 if (i
->_cooked_size
!= 0)
1976 dot
+= i
->_cooked_size
;
1978 dot
+= i
->_raw_size
;
1979 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1983 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1989 /* This variable indicates whether bfd_relax_section should be called
1992 static boolean relax_again
;
1994 /* Set the sizes for all the output sections. */
1997 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1998 lang_statement_union_type
* s
;
1999 lang_output_section_statement_type
* output_section_statement
;
2000 lang_statement_union_type
** prev
;
2005 /* Size up the sections from their constituent parts */
2006 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2008 switch (s
->header
.type
)
2011 case lang_output_section_statement_enum
:
2014 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
2016 if (os
->bfd_section
== NULL
)
2018 /* This section was never actually created. */
2022 /* If this is a COFF shared library section, use the size and
2023 address from the input section. FIXME: This is COFF
2024 specific; it would be cleaner if there were some other way
2025 to do this, but nothing simple comes to mind. */
2026 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
2030 if (os
->children
.head
== NULL
2031 || os
->children
.head
->next
!= NULL
2032 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
2033 einfo ("%P%X: Internal error on COFF shared library section %s",
2036 input
= os
->children
.head
->input_section
.section
;
2037 bfd_set_section_vma (os
->bfd_section
->owner
,
2039 bfd_section_vma (input
->owner
, input
));
2040 os
->bfd_section
->_raw_size
= input
->_raw_size
;
2044 if (bfd_is_abs_section (os
->bfd_section
))
2046 /* No matter what happens, an abs section starts at zero */
2047 ASSERT (os
->bfd_section
->vma
== 0);
2051 if (os
->addr_tree
== (etree_type
*) NULL
)
2053 /* No address specified for this section, get one
2054 from the region specification
2056 if (os
->region
== (lang_memory_region_type
*) NULL
)
2058 os
->region
= lang_memory_region_lookup ("*default*");
2060 dot
= os
->region
->current
;
2061 if (os
->section_alignment
== -1)
2062 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
2068 r
= exp_fold_tree (os
->addr_tree
,
2070 lang_allocating_phase_enum
,
2072 if (r
.valid
== false)
2074 einfo ("%F%S: non constant address expression for section %s\n",
2077 dot
= r
.value
+ r
.section
->bfd_section
->vma
;
2079 /* The section starts here */
2080 /* First, align to what the section needs */
2082 if (os
->section_alignment
!= -1)
2083 dot
= align_power (dot
, os
->section_alignment
);
2085 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2087 os
->bfd_section
->output_offset
= 0;
2090 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2091 os
->fill
, dot
, relax
);
2092 /* Ignore the size of the input sections, use the vma and size to */
2095 after
= ALIGN_N (os
->bfd_section
->vma
+
2096 os
->bfd_section
->_raw_size
,
2097 /* The coercion here is important, see ld.h. */
2098 (bfd_vma
) os
->block_value
);
2100 if (bfd_is_abs_section (os
->bfd_section
))
2101 ASSERT (after
== os
->bfd_section
->vma
);
2103 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
2104 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2105 os
->processed
= true;
2107 /* Replace into region ? */
2108 if (os
->region
!= (lang_memory_region_type
*) NULL
)
2110 os
->region
->current
= dot
;
2111 /* Make sure this isn't silly. */
2112 if (os
->region
->current
< os
->region
->origin
2113 || (os
->region
->current
- os
->region
->origin
2114 > os
->region
->length
))
2116 if (os
->addr_tree
!= (etree_type
*) NULL
)
2118 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
2119 os
->region
->current
,
2120 os
->bfd_section
->owner
,
2121 os
->bfd_section
->name
,
2126 einfo ("%X%P: region %s is full (%B section %s)\n",
2128 os
->bfd_section
->owner
,
2129 os
->bfd_section
->name
);
2131 /* Reset the region pointer. */
2132 os
->region
->current
= os
->region
->origin
;
2138 case lang_constructors_statement_enum
:
2139 dot
= lang_size_sections (constructor_list
.head
,
2140 output_section_statement
,
2141 &s
->wild_statement
.children
.head
,
2146 case lang_data_statement_enum
:
2148 unsigned int size
= 0;
2150 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2151 s
->data_statement
.output_section
=
2152 output_section_statement
->bfd_section
;
2154 switch (s
->data_statement
.type
)
2171 output_section_statement
->bfd_section
->_raw_size
+= size
;
2172 /* The output section gets contents, and then we inspect for
2173 any flags set in the input script which override any ALLOC */
2174 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2175 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2176 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2181 case lang_reloc_statement_enum
:
2185 s
->reloc_statement
.output_vma
=
2186 dot
- output_section_statement
->bfd_section
->vma
;
2187 s
->reloc_statement
.output_section
=
2188 output_section_statement
->bfd_section
;
2189 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2191 output_section_statement
->bfd_section
->_raw_size
+= size
;
2195 case lang_wild_statement_enum
:
2197 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2198 output_section_statement
,
2199 &s
->wild_statement
.children
.head
,
2205 case lang_object_symbols_statement_enum
:
2206 link_info
.create_object_symbols_section
=
2207 output_section_statement
->bfd_section
;
2209 case lang_output_statement_enum
:
2210 case lang_target_statement_enum
:
2212 case lang_input_section_enum
:
2216 i
= (*prev
)->input_section
.section
;
2219 if (i
->_cooked_size
== 0)
2220 i
->_cooked_size
= i
->_raw_size
;
2226 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2227 einfo ("%P%F: can't relax section: %E\n");
2231 dot
= size_input_section (prev
,
2232 output_section_statement
,
2233 output_section_statement
->fill
,
2237 case lang_input_statement_enum
:
2239 case lang_fill_statement_enum
:
2240 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2242 fill
= s
->fill_statement
.fill
;
2244 case lang_assignment_statement_enum
:
2246 bfd_vma newdot
= dot
;
2248 exp_fold_tree (s
->assignment_statement
.exp
,
2249 output_section_statement
,
2250 lang_allocating_phase_enum
,
2254 if (newdot
!= dot
&& !relax
)
2256 /* The assignment changed dot. Insert a pad. */
2257 if (output_section_statement
== abs_output_section
)
2259 /* If we don't have an output section, then just adjust
2260 the default memory address. */
2261 lang_memory_region_lookup ("*default*")->current
= newdot
;
2265 lang_statement_union_type
*new =
2266 ((lang_statement_union_type
*)
2267 stat_alloc (sizeof (lang_padding_statement_type
)));
2269 /* Link into existing chain */
2270 new->header
.next
= *prev
;
2272 new->header
.type
= lang_padding_statement_enum
;
2273 new->padding_statement
.output_section
=
2274 output_section_statement
->bfd_section
;
2275 new->padding_statement
.output_offset
=
2276 dot
- output_section_statement
->bfd_section
->vma
;
2277 new->padding_statement
.fill
= fill
;
2278 new->padding_statement
.size
= newdot
- dot
;
2279 output_section_statement
->bfd_section
->_raw_size
+=
2280 new->padding_statement
.size
;
2288 case lang_padding_statement_enum
:
2289 /* If we are relaxing, and this is not the first pass, some
2290 padding statements may have been inserted during previous
2291 passes. We may have to move the padding statement to a new
2292 location if dot has a different value at this point in this
2293 pass than it did at this point in the previous pass. */
2294 s
->padding_statement
.output_offset
=
2295 dot
- output_section_statement
->bfd_section
->vma
;
2296 dot
+= s
->padding_statement
.size
;
2297 output_section_statement
->bfd_section
->_raw_size
+=
2298 s
->padding_statement
.size
;
2301 case lang_group_statement_enum
:
2302 dot
= lang_size_sections (s
->group_statement
.children
.head
,
2303 output_section_statement
,
2304 &s
->group_statement
.children
.head
,
2312 /* This can only get here when relaxing is turned on */
2314 case lang_address_statement_enum
:
2317 prev
= &s
->header
.next
;
2323 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
2324 lang_statement_union_type
* s
;
2325 lang_output_section_statement_type
* output_section_statement
;
2329 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2331 switch (s
->header
.type
)
2333 case lang_constructors_statement_enum
:
2334 dot
= lang_do_assignments (constructor_list
.head
,
2335 output_section_statement
,
2340 case lang_output_section_statement_enum
:
2342 lang_output_section_statement_type
*os
=
2343 &(s
->output_section_statement
);
2345 if (os
->bfd_section
!= NULL
)
2347 dot
= os
->bfd_section
->vma
;
2348 (void) lang_do_assignments (os
->children
.head
, os
,
2350 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2354 /* If nothing has been placed into the output section then
2355 it won't have a bfd_section. */
2356 if (os
->bfd_section
)
2358 os
->bfd_section
->lma
2359 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
2364 case lang_wild_statement_enum
:
2366 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
2367 output_section_statement
,
2372 case lang_object_symbols_statement_enum
:
2373 case lang_output_statement_enum
:
2374 case lang_target_statement_enum
:
2376 case lang_common_statement_enum
:
2379 case lang_data_statement_enum
:
2381 etree_value_type value
;
2383 value
= exp_fold_tree (s
->data_statement
.exp
,
2385 lang_final_phase_enum
, dot
, &dot
);
2386 s
->data_statement
.value
= value
.value
;
2387 if (value
.valid
== false)
2388 einfo ("%F%P: invalid data statement\n");
2390 switch (s
->data_statement
.type
)
2407 case lang_reloc_statement_enum
:
2409 etree_value_type value
;
2411 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
2413 lang_final_phase_enum
, dot
, &dot
);
2414 s
->reloc_statement
.addend_value
= value
.value
;
2415 if (value
.valid
== false)
2416 einfo ("%F%P: invalid reloc statement\n");
2418 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2421 case lang_input_section_enum
:
2423 asection
*in
= s
->input_section
.section
;
2425 if (in
->_cooked_size
!= 0)
2426 dot
+= in
->_cooked_size
;
2428 dot
+= in
->_raw_size
;
2432 case lang_input_statement_enum
:
2434 case lang_fill_statement_enum
:
2435 fill
= s
->fill_statement
.fill
;
2437 case lang_assignment_statement_enum
:
2439 exp_fold_tree (s
->assignment_statement
.exp
,
2440 output_section_statement
,
2441 lang_final_phase_enum
,
2447 case lang_padding_statement_enum
:
2448 dot
+= s
->padding_statement
.size
;
2451 case lang_group_statement_enum
:
2452 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
2453 output_section_statement
,
2461 case lang_address_statement_enum
:
2469 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2470 operator .startof. (section_name), it produces an undefined symbol
2471 .startof.section_name. Similarly, when it sees
2472 .sizeof. (section_name), it produces an undefined symbol
2473 .sizeof.section_name. For all the output sections, we look for
2474 such symbols, and set them to the correct value. */
2481 if (link_info
.relocateable
)
2484 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2486 const char *secname
;
2488 struct bfd_link_hash_entry
*h
;
2490 secname
= bfd_get_section_name (output_bfd
, s
);
2491 buf
= xmalloc (10 + strlen (secname
));
2493 sprintf (buf
, ".startof.%s", secname
);
2494 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2495 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2497 h
->type
= bfd_link_hash_defined
;
2498 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
2499 h
->u
.def
.section
= bfd_abs_section_ptr
;
2502 sprintf (buf
, ".sizeof.%s", secname
);
2503 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2504 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2506 h
->type
= bfd_link_hash_defined
;
2507 if (s
->_cooked_size
!= 0)
2508 h
->u
.def
.value
= s
->_cooked_size
;
2510 h
->u
.def
.value
= s
->_raw_size
;
2511 h
->u
.def
.section
= bfd_abs_section_ptr
;
2521 struct bfd_link_hash_entry
*h
;
2524 if (link_info
.relocateable
|| link_info
.shared
)
2529 if (entry_symbol
== (char *) NULL
)
2531 /* No entry has been specified. Look for start, but don't warn
2532 if we don't find it. */
2533 entry_symbol
= "start";
2537 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
2538 if (h
!= (struct bfd_link_hash_entry
*) NULL
2539 && (h
->type
== bfd_link_hash_defined
2540 || h
->type
== bfd_link_hash_defweak
)
2541 && h
->u
.def
.section
->output_section
!= NULL
)
2545 val
= (h
->u
.def
.value
2546 + bfd_get_section_vma (output_bfd
,
2547 h
->u
.def
.section
->output_section
)
2548 + h
->u
.def
.section
->output_offset
);
2549 if (! bfd_set_start_address (output_bfd
, val
))
2550 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
2556 /* Can't find the entry symbol. Use the first address in the
2558 ts
= bfd_get_section_by_name (output_bfd
, ".text");
2559 if (ts
!= (asection
*) NULL
)
2562 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2563 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
2564 if (! bfd_set_start_address (output_bfd
,
2565 bfd_get_section_vma (output_bfd
, ts
)))
2566 einfo ("%P%F: can't set start address\n");
2571 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2577 /* Check that the architecture of all the input files is compatible
2578 with the output file. Also call the backend to let it do any
2579 other checking that is needed. */
2584 lang_statement_union_type
*file
;
2586 CONST bfd_arch_info_type
*compatible
;
2588 for (file
= file_chain
.head
;
2589 file
!= (lang_statement_union_type
*) NULL
;
2590 file
= file
->input_statement
.next
)
2592 input_bfd
= file
->input_statement
.the_bfd
;
2593 compatible
= bfd_arch_get_compatible (input_bfd
,
2595 if (compatible
== NULL
)
2596 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2597 bfd_printable_name (input_bfd
), input_bfd
,
2598 bfd_printable_name (output_bfd
));
2601 bfd_merge_private_bfd_data (input_bfd
, output_bfd
);
2605 /* Look through all the global common symbols and attach them to the
2606 correct section. The -sort-common command line switch may be used
2607 to roughly sort the entries by size. */
2612 if (link_info
.relocateable
2613 && ! command_line
.force_common_definition
)
2616 if (! config
.sort_common
)
2617 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2622 for (power
= 4; power
>= 0; power
--)
2623 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2628 /* Place one common symbol in the correct section. */
2631 lang_one_common (h
, info
)
2632 struct bfd_link_hash_entry
*h
;
2635 unsigned int power_of_two
;
2639 if (h
->type
!= bfd_link_hash_common
)
2643 power_of_two
= h
->u
.c
.p
->alignment_power
;
2645 if (config
.sort_common
2646 && power_of_two
< (unsigned int) *(int *) info
)
2649 section
= h
->u
.c
.p
->section
;
2651 /* Increase the size of the section. */
2652 section
->_raw_size
= ALIGN_N (section
->_raw_size
,
2653 (bfd_size_type
) (1 << power_of_two
));
2655 /* Adjust the alignment if necessary. */
2656 if (power_of_two
> section
->alignment_power
)
2657 section
->alignment_power
= power_of_two
;
2659 /* Change the symbol from common to defined. */
2660 h
->type
= bfd_link_hash_defined
;
2661 h
->u
.def
.section
= section
;
2662 h
->u
.def
.value
= section
->_raw_size
;
2664 /* Increase the size of the section. */
2665 section
->_raw_size
+= size
;
2667 /* Make sure the section is allocated in memory. */
2668 section
->flags
|= SEC_ALLOC
;
2670 if (config
.map_file
!= NULL
)
2672 static boolean header_printed
;
2677 if (! header_printed
)
2679 minfo ("\nAllocating common symbols\n");
2680 minfo ("Common symbol size file\n\n");
2681 header_printed
= true;
2684 name
= demangle (h
->root
.string
);
2686 len
= strlen (name
);
2701 if (size
<= 0xffffffff)
2702 sprintf (buf
, "%lx", (unsigned long) size
);
2704 sprintf_vma (buf
, size
);
2714 minfo ("%B\n", section
->owner
);
2721 run through the input files and ensure that every input
2722 section has somewhere to go. If one is found without
2723 a destination then create an input request and place it
2724 into the statement tree.
2728 lang_place_orphans ()
2730 lang_input_statement_type
*file
;
2732 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2733 file
!= (lang_input_statement_type
*) NULL
;
2734 file
= (lang_input_statement_type
*) file
->next
)
2738 for (s
= file
->the_bfd
->sections
;
2739 s
!= (asection
*) NULL
;
2742 if (s
->output_section
== (asection
*) NULL
)
2744 /* This section of the file is not attatched, root
2745 around for a sensible place for it to go */
2747 if (file
->just_syms_flag
)
2749 /* We are only retrieving symbol values from this
2750 file. We want the symbols to act as though the
2751 values in the file are absolute. */
2752 s
->output_section
= bfd_abs_section_ptr
;
2753 s
->output_offset
= s
->vma
;
2755 else if (strcmp (s
->name
, "COMMON") == 0)
2757 /* This is a lonely common section which must have
2758 come from an archive. We attach to the section
2759 with the wildcard. */
2760 if (! link_info
.relocateable
2761 || command_line
.force_common_definition
)
2763 if (default_common_section
== NULL
)
2766 /* This message happens when using the
2767 svr3.ifile linker script, so I have
2769 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2771 default_common_section
=
2772 lang_output_section_statement_lookup (".bss");
2775 wild_doit (&default_common_section
->children
, s
,
2776 default_common_section
, file
);
2779 else if (ldemul_place_orphan (file
, s
))
2783 lang_output_section_statement_type
*os
=
2784 lang_output_section_statement_lookup (s
->name
);
2786 wild_doit (&os
->children
, s
, os
, file
);
2795 lang_set_flags (ptr
, flags
)
2799 boolean state
= false;
2814 /* ptr->flag_read = state; */
2817 /* ptr->flag_write = state; */
2820 /* ptr->flag_executable= state;*/
2824 /* ptr->flag_loadable= state;*/
2827 einfo ("%P%F: invalid syntax in flags\n");
2834 /* Call a function on each input file. This function will be called
2835 on an archive, but not on the elements. */
2838 lang_for_each_input_file (func
)
2839 void (*func
) PARAMS ((lang_input_statement_type
*));
2841 lang_input_statement_type
*f
;
2843 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
2845 f
= (lang_input_statement_type
*) f
->next_real_file
)
2849 /* Call a function on each file. The function will be called on all
2850 the elements of an archive which are included in the link, but will
2851 not be called on the archive file itself. */
2854 lang_for_each_file (func
)
2855 void (*func
) PARAMS ((lang_input_statement_type
*));
2857 lang_input_statement_type
*f
;
2859 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2860 f
!= (lang_input_statement_type
*) NULL
;
2861 f
= (lang_input_statement_type
*) f
->next
)
2872 lang_for_each_input_section (func
)
2873 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2875 lang_input_statement_type
*f
;
2877 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2878 f
!= (lang_input_statement_type
*) NULL
;
2879 f
= (lang_input_statement_type
*) f
->next
)
2883 for (s
= f
->the_bfd
->sections
;
2884 s
!= (asection
*) NULL
;
2887 func (f
->the_bfd
, s
);
2895 ldlang_add_file (entry
)
2896 lang_input_statement_type
* entry
;
2900 lang_statement_append (&file_chain
,
2901 (lang_statement_union_type
*) entry
,
2904 /* The BFD linker needs to have a list of all input BFDs involved in
2906 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2907 ASSERT (entry
->the_bfd
!= output_bfd
);
2908 for (pp
= &link_info
.input_bfds
;
2909 *pp
!= (bfd
*) NULL
;
2910 pp
= &(*pp
)->link_next
)
2912 *pp
= entry
->the_bfd
;
2913 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2914 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
2916 /* Look through the sections and check for any which should not be
2917 included in the link. We need to do this now, so that we can
2918 notice when the backend linker tries to report multiple
2919 definition errors for symbols which are in sections we aren't
2920 going to link. FIXME: It might be better to entirely ignore
2921 symbols which are defined in sections which are going to be
2922 discarded. This would require modifying the backend linker for
2923 each backend which might set the SEC_LINK_ONCE flag. If we do
2924 this, we should probably handle SEC_EXCLUDE in the same way. */
2926 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) NULL
);
2930 lang_add_output (name
, from_script
)
2934 /* Make -o on command line override OUTPUT in script. */
2935 if (had_output_filename
== false || !from_script
)
2937 output_filename
= name
;
2938 had_output_filename
= true;
2943 static lang_output_section_statement_type
*current_section
;
2955 for (l
= 0; l
< 32; l
++)
2957 if (i
>= (unsigned int) x
)
2966 lang_enter_output_section_statement (output_section_statement_name
,
2967 address_exp
, sectype
, block_value
,
2968 align
, subalign
, ebase
)
2969 const char *output_section_statement_name
;
2970 etree_type
* address_exp
;
2971 enum section_type sectype
;
2972 bfd_vma block_value
;
2974 etree_type
*subalign
;
2977 lang_output_section_statement_type
*os
;
2981 lang_output_section_statement_lookup (output_section_statement_name
);
2985 /* Add this statement to tree */
2986 /* add_statement(lang_output_section_statement_enum,
2987 output_section_statement);*/
2988 /* Make next things chain into subchain of this */
2990 if (os
->addr_tree
==
2991 (etree_type
*) NULL
)
2996 os
->sectype
= sectype
;
2997 if (sectype
!= noload_section
)
2998 os
->flags
= SEC_NO_FLAGS
;
3000 os
->flags
= SEC_NEVER_LOAD
;
3001 os
->block_value
= block_value
? block_value
: 1;
3002 stat_ptr
= &os
->children
;
3004 os
->subsection_alignment
= topower(
3005 exp_get_value_int(subalign
, -1,
3006 "subsection alignment",
3008 os
->section_alignment
= topower(
3009 exp_get_value_int(align
, -1,
3010 "section alignment", 0));
3012 os
->load_base
= ebase
;
3019 lang_output_statement_type
*new =
3020 new_stat (lang_output_statement
, stat_ptr
);
3022 new->name
= output_filename
;
3025 /* Reset the current counters in the regions */
3027 reset_memory_regions ()
3029 lang_memory_region_type
*p
= lang_memory_region_list
;
3031 for (p
= lang_memory_region_list
;
3032 p
!= (lang_memory_region_type
*) NULL
;
3035 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
3036 p
->current
= p
->origin
;
3043 lang_reasonable_defaults ();
3044 current_target
= default_target
;
3046 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
3048 ldemul_create_output_section_statements ();
3050 /* Add to the hash table all undefineds on the command line */
3051 lang_place_undefineds ();
3053 /* Create a bfd for each input file */
3054 current_target
= default_target
;
3055 open_input_bfds (statement_list
.head
, false);
3057 ldemul_after_open ();
3059 /* Build all sets based on the information gathered from the input
3061 ldctor_build_sets ();
3063 /* Size up the common data */
3066 /* Run through the contours of the script and attatch input sections
3067 to the correct output sections
3069 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
3070 (lang_output_section_statement_type
*) NULL
);
3073 /* Find any sections not attatched explicitly and handle them */
3074 lang_place_orphans ();
3076 ldemul_before_allocation ();
3078 /* We must record the program headers before we try to fix the
3079 section positions, since they will affect SIZEOF_HEADERS. */
3080 lang_record_phdrs ();
3082 /* Now run around and relax if we can */
3083 if (command_line
.relax
)
3085 /* First time round is a trial run to get the 'worst case'
3086 addresses of the objects if there was no relaxing. */
3087 lang_size_sections (statement_list
.head
,
3089 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
3091 /* Keep relaxing until bfd_relax_section gives up. */
3094 reset_memory_regions ();
3096 relax_again
= false;
3098 /* Do all the assignments with our current guesses as to
3100 lang_do_assignments (statement_list
.head
,
3102 (fill_type
) 0, (bfd_vma
) 0);
3104 /* Perform another relax pass - this time we know where the
3105 globals are, so can make better guess. */
3106 lang_size_sections (statement_list
.head
,
3108 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
3110 while (relax_again
);
3114 /* Size up the sections. */
3115 lang_size_sections (statement_list
.head
,
3117 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
3120 /* See if anything special should be done now we know how big
3122 ldemul_after_allocation ();
3124 /* Fix any .startof. or .sizeof. symbols. */
3125 lang_set_startof ();
3127 /* Do all the assignments, now that we know the final restingplaces
3128 of all the symbols */
3130 lang_do_assignments (statement_list
.head
,
3132 (fill_type
) 0, (bfd_vma
) 0);
3134 /* Make sure that we're not mixing architectures */
3144 /* EXPORTED TO YACC */
3147 lang_add_wild (section_name
, filename
)
3148 CONST
char *CONST section_name
;
3149 CONST
char *CONST filename
;
3151 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
3154 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
3156 placed_commons
= true;
3158 if (filename
!= (char *) NULL
)
3160 lang_has_input_file
= true;
3162 new->section_name
= section_name
;
3163 new->filename
= filename
;
3164 lang_list_init (&new->children
);
3168 lang_section_start (name
, address
)
3170 etree_type
* address
;
3172 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
3174 ad
->section_name
= name
;
3175 ad
->address
= address
;
3178 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3179 because of a -e argument on the command line, or zero if this is
3180 called by ENTRY in a linker script. Command line arguments take
3183 /* WINDOWS_NT. When an entry point has been specified, we will also force
3184 this symbol to be defined by calling ldlang_add_undef (equivalent to
3185 having switch -u entry_name on the command line). The reason we do
3186 this is so that the user doesn't have to because they would have to use
3187 the -u switch if they were specifying an entry point other than
3188 _mainCRTStartup. Specifically, if creating a windows application, entry
3189 point _WinMainCRTStartup must be specified.
3190 What I have found for non console applications (entry not _mainCRTStartup)
3191 is that the .obj that contains mainCRTStartup is brought in since it is
3192 the first encountered in libc.lib and it has other symbols in it which will
3193 be pulled in by the link process. To avoid this, adding -u with the entry
3194 point name specified forces the correct .obj to be used. We can avoid
3195 making the user do this by always adding the entry point name as an
3196 undefined symbol. */
3199 lang_add_entry (name
, cmdline
)
3203 if (entry_symbol
== NULL
3205 || ! entry_from_cmdline
)
3207 entry_symbol
= name
;
3208 entry_from_cmdline
= cmdline
;
3211 /* don't do this yet. It seems to work (the executables run), but the
3212 image created is very different from what I was getting before indicating
3213 that something else is being pulled in. When everything else is working,
3214 then try to put this back in to see if it will do the right thing for
3215 other more complicated applications */
3216 ldlang_add_undef (name
);
3221 lang_add_target (name
)
3224 lang_target_statement_type
*new = new_stat (lang_target_statement
,
3240 map_option_f
= true;
3251 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
3258 lang_add_data (type
, exp
)
3260 union etree_union
*exp
;
3263 lang_data_statement_type
*new = new_stat (lang_data_statement
,
3271 /* Create a new reloc statement. RELOC is the BFD relocation type to
3272 generate. HOWTO is the corresponding howto structure (we could
3273 look this up, but the caller has already done so). SECTION is the
3274 section to generate a reloc against, or NAME is the name of the
3275 symbol to generate a reloc against. Exactly one of SECTION and
3276 NAME must be NULL. ADDEND is an expression for the addend. */
3279 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
3280 bfd_reloc_code_real_type reloc
;
3281 reloc_howto_type
*howto
;
3284 union etree_union
*addend
;
3286 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
3290 p
->section
= section
;
3292 p
->addend_exp
= addend
;
3294 p
->addend_value
= 0;
3295 p
->output_section
= NULL
;
3300 lang_add_assignment (exp
)
3303 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
3310 lang_add_attribute (attribute
)
3311 enum statement_enum attribute
;
3313 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
3320 if (startup_file
!= (char *) NULL
)
3322 einfo ("%P%Fmultiple STARTUP files\n");
3324 first_file
->filename
= name
;
3325 first_file
->local_sym_name
= name
;
3326 first_file
->real
= true;
3328 startup_file
= name
;
3335 lang_float_flag
= maybe
;
3339 lang_leave_output_section_statement (fill
, memspec
)
3341 CONST
char *memspec
;
3343 current_section
->fill
= fill
;
3344 current_section
->region
= lang_memory_region_lookup (memspec
);
3345 stat_ptr
= &statement_list
;
3349 Create an absolute symbol with the given name with the value of the
3350 address of first byte of the section named.
3352 If the symbol already exists, then do nothing.
3355 lang_abs_symbol_at_beginning_of (secname
, name
)
3356 const char *secname
;
3359 struct bfd_link_hash_entry
*h
;
3361 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3362 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3363 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3365 if (h
->type
== bfd_link_hash_new
3366 || h
->type
== bfd_link_hash_undefined
)
3370 h
->type
= bfd_link_hash_defined
;
3372 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3373 if (sec
== (asection
*) NULL
)
3376 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
3378 h
->u
.def
.section
= bfd_abs_section_ptr
;
3383 Create an absolute symbol with the given name with the value of the
3384 address of the first byte after the end of the section named.
3386 If the symbol already exists, then do nothing.
3389 lang_abs_symbol_at_end_of (secname
, name
)
3390 const char *secname
;
3393 struct bfd_link_hash_entry
*h
;
3395 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3396 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3397 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3399 if (h
->type
== bfd_link_hash_new
3400 || h
->type
== bfd_link_hash_undefined
)
3404 h
->type
= bfd_link_hash_defined
;
3406 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3407 if (sec
== (asection
*) NULL
)
3410 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
3411 + bfd_section_size (output_bfd
, sec
));
3413 h
->u
.def
.section
= bfd_abs_section_ptr
;
3418 lang_statement_append (list
, element
, field
)
3419 lang_statement_list_type
* list
;
3420 lang_statement_union_type
* element
;
3421 lang_statement_union_type
** field
;
3423 *(list
->tail
) = element
;
3427 /* Set the output format type. -oformat overrides scripts. */
3430 lang_add_output_format (format
, big
, little
, from_script
)
3436 if (output_target
== NULL
|| !from_script
)
3438 if (command_line
.endian
== ENDIAN_BIG
3441 else if (command_line
.endian
== ENDIAN_LITTLE
3445 output_target
= format
;
3449 /* Enter a group. This creates a new lang_group_statement, and sets
3450 stat_ptr to build new statements within the group. */
3455 lang_group_statement_type
*g
;
3457 g
= new_stat (lang_group_statement
, stat_ptr
);
3458 lang_list_init (&g
->children
);
3459 stat_ptr
= &g
->children
;
3462 /* Leave a group. This just resets stat_ptr to start writing to the
3463 regular list of statements again. Note that this will not work if
3464 groups can occur inside anything else which can adjust stat_ptr,
3465 but currently they can't. */
3470 stat_ptr
= &statement_list
;
3473 /* Add a new program header. This is called for each entry in a PHDRS
3474 command in a linker script. */
3477 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
3485 struct lang_phdr
*n
, **pp
;
3487 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
3490 n
->type
= exp_get_value_int (type
, 0, "program header type",
3491 lang_final_phase_enum
);
3492 n
->filehdr
= filehdr
;
3497 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3502 /* Record that a section should be placed in a phdr. */
3505 lang_section_in_phdr (name
)
3508 struct lang_output_section_phdr_list
*n
;
3510 n
= ((struct lang_output_section_phdr_list
*)
3511 stat_alloc (sizeof (struct lang_output_section_phdr_list
)));
3514 n
->next
= current_section
->phdrs
;
3515 current_section
->phdrs
= n
;
3518 /* Record the program header information in the output BFD. FIXME: We
3519 should not be calling an ELF specific function here. */
3522 lang_record_phdrs ()
3526 struct lang_output_section_phdr_list
*last
;
3527 struct lang_phdr
*l
;
3528 lang_statement_union_type
*u
;
3531 secs
= xmalloc (alc
* sizeof (asection
*));
3533 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
3540 for (u
= lang_output_section_statement
.head
;
3542 u
= u
->output_section_statement
.next
)
3544 lang_output_section_statement_type
*os
;
3545 struct lang_output_section_phdr_list
*pl
;
3547 os
= &u
->output_section_statement
;
3554 if (os
->sectype
== noload_section
3555 || os
->bfd_section
== NULL
3556 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
3561 if (os
->bfd_section
== NULL
)
3564 for (; pl
!= NULL
; pl
= pl
->next
)
3566 if (strcmp (pl
->name
, l
->name
) == 0)
3571 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
3573 secs
[c
] = os
->bfd_section
;
3580 if (l
->flags
== NULL
)
3583 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
3584 lang_final_phase_enum
);
3589 at
= exp_get_vma (l
->at
, 0, "phdr load address",
3590 lang_final_phase_enum
);
3592 if (! bfd_record_phdr (output_bfd
, l
->type
,
3593 l
->flags
== NULL
? false : true,
3595 l
->at
== NULL
? false : true,
3596 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
3597 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3602 /* Make sure all the phdr assignments succeeded. */
3603 for (u
= lang_output_section_statement
.head
;
3605 u
= u
->output_section_statement
.next
)
3607 struct lang_output_section_phdr_list
*pl
;
3609 if (u
->output_section_statement
.bfd_section
== NULL
)
3612 for (pl
= u
->output_section_statement
.phdrs
;
3615 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
3616 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3617 u
->output_section_statement
.name
, pl
->name
);