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"
37 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
39 lang_statement_list_type
*));
43 static struct obstack stat_obstack
;
45 #define obstack_chunk_alloc xmalloc
46 #define obstack_chunk_free free
47 static CONST
char *startup_file
;
48 static lang_statement_list_type input_file_chain
;
49 static boolean placed_commons
= false;
50 static lang_output_section_statement_type
*default_common_section
;
51 static boolean map_option_f
;
52 static bfd_vma print_dot
;
53 static lang_input_statement_type
*first_file
;
54 static lang_statement_list_type lang_output_section_statement
;
55 static CONST
char *current_target
;
56 static CONST
char *output_target
;
57 static lang_statement_list_type statement_list
;
58 static struct lang_phdr
*lang_phdr_list
;
60 static void lang_for_each_statement_worker
61 PARAMS ((void (*func
) (lang_statement_union_type
*),
62 lang_statement_union_type
*s
));
63 static lang_input_statement_type
*new_afile
64 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
65 const char *target
, boolean add_to_list
));
66 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
67 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
68 static void wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
70 lang_input_statement_type
*file
,
71 lang_output_section_statement_type
*output
));
72 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
73 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
74 lang_statement_list_type
*));
75 static void wild
PARAMS ((lang_wild_statement_type
*s
,
76 const char *section
, const char *file
,
78 lang_output_section_statement_type
*output
));
79 static bfd
*open_output
PARAMS ((const char *name
));
80 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
81 static void open_input_bfds
82 PARAMS ((lang_statement_union_type
*statement
, boolean
));
83 static void lang_reasonable_defaults
PARAMS ((void));
84 static void lang_place_undefineds
PARAMS ((void));
85 static void map_input_to_output_sections
86 PARAMS ((lang_statement_union_type
*s
,
88 lang_output_section_statement_type
*output_section_statement
));
89 static void print_output_section_statement
90 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
91 static void print_assignment
92 PARAMS ((lang_assignment_statement_type
*assignment
,
93 lang_output_section_statement_type
*output_section
));
94 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
95 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
96 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
97 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
98 static void print_address_statement
PARAMS ((lang_address_statement_type
*));
99 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
100 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
101 static void print_wild_statement
102 PARAMS ((lang_wild_statement_type
*w
,
103 lang_output_section_statement_type
*os
));
104 static void print_group
105 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
106 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
107 lang_output_section_statement_type
*os
));
108 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
109 lang_output_section_statement_type
*os
));
110 static void print_statements
PARAMS ((void));
111 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
112 fill_type fill
, unsigned int power
,
113 asection
*output_section_statement
,
115 static bfd_vma size_input_section
116 PARAMS ((lang_statement_union_type
**this_ptr
,
117 lang_output_section_statement_type
*output_section_statement
,
118 fill_type fill
, bfd_vma dot
, boolean relax
));
119 static void lang_finish
PARAMS ((void));
120 static void lang_check
PARAMS ((void));
121 static void lang_common
PARAMS ((void));
122 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
123 static void lang_place_orphans
PARAMS ((void));
124 static int topower
PARAMS ((int));
125 static void lang_set_startof
PARAMS ((void));
126 static void reset_memory_regions
PARAMS ((void));
127 static void lang_record_phdrs
PARAMS ((void));
130 lang_output_section_statement_type
*abs_output_section
;
131 lang_statement_list_type
*stat_ptr
= &statement_list
;
132 lang_statement_list_type file_chain
= { 0 };
133 const char *entry_symbol
= NULL
;
134 boolean entry_from_cmdline
;
135 boolean lang_has_input_file
= false;
136 boolean had_output_filename
= false;
137 boolean lang_float_flag
= false;
138 boolean delete_output_file_on_failure
= false;
140 etree_type
*base
; /* Relocation base - or null */
143 #if defined(__STDC__) || defined(ALMOST_STDC)
144 #define cat(a,b) a##b
146 #define cat(a,b) a/**/b
149 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
151 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
153 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
155 #define SECTION_NAME_MAP_LENGTH (16)
161 return obstack_alloc (&stat_obstack
, size
);
164 /*----------------------------------------------------------------------
165 lang_for_each_statement walks the parse tree and calls the provided
166 function for each node
170 lang_for_each_statement_worker (func
, s
)
171 void (*func
) PARAMS ((lang_statement_union_type
*));
172 lang_statement_union_type
*s
;
174 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
178 switch (s
->header
.type
)
180 case lang_constructors_statement_enum
:
181 lang_for_each_statement_worker (func
, constructor_list
.head
);
183 case lang_output_section_statement_enum
:
184 lang_for_each_statement_worker
186 s
->output_section_statement
.children
.head
);
188 case lang_wild_statement_enum
:
189 lang_for_each_statement_worker
191 s
->wild_statement
.children
.head
);
193 case lang_group_statement_enum
:
194 lang_for_each_statement_worker (func
,
195 s
->group_statement
.children
.head
);
197 case lang_data_statement_enum
:
198 case lang_reloc_statement_enum
:
199 case lang_object_symbols_statement_enum
:
200 case lang_output_statement_enum
:
201 case lang_target_statement_enum
:
202 case lang_input_section_enum
:
203 case lang_input_statement_enum
:
204 case lang_assignment_statement_enum
:
205 case lang_padding_statement_enum
:
206 case lang_address_statement_enum
:
207 case lang_fill_statement_enum
:
217 lang_for_each_statement (func
)
218 void (*func
) PARAMS ((lang_statement_union_type
*));
220 lang_for_each_statement_worker (func
,
221 statement_list
.head
);
224 /*----------------------------------------------------------------------*/
226 lang_list_init (list
)
227 lang_statement_list_type
*list
;
229 list
->head
= (lang_statement_union_type
*) NULL
;
230 list
->tail
= &list
->head
;
233 /*----------------------------------------------------------------------
235 build a new statement node for the parse tree
240 lang_statement_union_type
*
241 new_statement (type
, size
, list
)
242 enum statement_enum type
;
244 lang_statement_list_type
* list
;
246 lang_statement_union_type
*new = (lang_statement_union_type
*)
249 new->header
.type
= type
;
250 new->header
.next
= (lang_statement_union_type
*) NULL
;
251 lang_statement_append (list
, new, &new->header
.next
);
256 Build a new input file node for the language. There are several ways
257 in which we treat an input file, eg, we only look at symbols, or
258 prefix it with a -l etc.
260 We can be supplied with requests for input files more than once;
261 they may, for example be split over serveral lines like foo.o(.text)
262 foo.o(.data) etc, so when asked for a file we check that we havn't
263 got it already so we don't duplicate the bfd.
266 static lang_input_statement_type
*
267 new_afile (name
, file_type
, target
, add_to_list
)
269 lang_input_file_enum_type file_type
;
273 lang_input_statement_type
*p
;
276 p
= new_stat (lang_input_statement
, stat_ptr
);
279 p
= ((lang_input_statement_type
*)
280 stat_alloc (sizeof (lang_input_statement_type
)));
281 p
->header
.next
= NULL
;
284 lang_has_input_file
= true;
286 p
->complained
= false;
289 case lang_input_file_is_symbols_only_enum
:
291 p
->is_archive
= false;
293 p
->local_sym_name
= name
;
294 p
->just_syms_flag
= true;
295 p
->search_dirs_flag
= false;
297 case lang_input_file_is_fake_enum
:
299 p
->is_archive
= false;
301 p
->local_sym_name
= name
;
302 p
->just_syms_flag
= false;
303 p
->search_dirs_flag
= false;
305 case lang_input_file_is_l_enum
:
306 p
->is_archive
= true;
309 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
310 p
->just_syms_flag
= false;
311 p
->search_dirs_flag
= true;
313 case lang_input_file_is_marker_enum
:
315 p
->is_archive
= false;
317 p
->local_sym_name
= name
;
318 p
->just_syms_flag
= false;
319 p
->search_dirs_flag
= true;
321 case lang_input_file_is_search_file_enum
:
323 p
->is_archive
= false;
325 p
->local_sym_name
= name
;
326 p
->just_syms_flag
= false;
327 p
->search_dirs_flag
= true;
329 case lang_input_file_is_file_enum
:
331 p
->is_archive
= false;
333 p
->local_sym_name
= name
;
334 p
->just_syms_flag
= false;
335 p
->search_dirs_flag
= false;
340 p
->the_bfd
= (bfd
*) NULL
;
341 p
->asymbols
= (asymbol
**) NULL
;
342 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
343 p
->next
= (lang_statement_union_type
*) NULL
;
345 p
->common_output_section
= (asection
*) NULL
;
346 p
->dynamic
= config
.dynamic_link
;
347 p
->whole_archive
= whole_archive
;
349 lang_statement_append (&input_file_chain
,
350 (lang_statement_union_type
*) p
,
355 lang_input_statement_type
*
356 lang_add_input_file (name
, file_type
, target
)
358 lang_input_file_enum_type file_type
;
361 lang_has_input_file
= true;
362 return new_afile (name
, file_type
, target
, true);
365 /* Build enough state so that the parser can build its tree */
369 obstack_begin (&stat_obstack
, 1000);
371 stat_ptr
= &statement_list
;
373 lang_list_init (stat_ptr
);
375 lang_list_init (&input_file_chain
);
376 lang_list_init (&lang_output_section_statement
);
377 lang_list_init (&file_chain
);
378 first_file
= lang_add_input_file ((char *) NULL
,
379 lang_input_file_is_marker_enum
,
381 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
383 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
387 /*----------------------------------------------------------------------
388 A region is an area of memory declared with the
389 MEMORY { name:org=exp, len=exp ... }
392 We maintain a list of all the regions here
394 If no regions are specified in the script, then the default is used
395 which is created when looked up to be the entire data space
398 static lang_memory_region_type
*lang_memory_region_list
;
399 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
401 lang_memory_region_type
*
402 lang_memory_region_lookup (name
)
403 CONST
char *CONST name
;
406 lang_memory_region_type
*p
= lang_memory_region_list
;
408 for (p
= lang_memory_region_list
;
409 p
!= (lang_memory_region_type
*) NULL
;
412 if (strcmp (p
->name
, name
) == 0)
419 /* This code used to always use the first region in the list as the
420 default region. I changed it to instead use a region
421 encompassing all of memory as the default region. This permits
422 NOLOAD sections to work reasonably without requiring a region.
423 People should specify what region they mean, if they really want
425 if (strcmp (name
, "*default*") == 0)
427 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
429 return lang_memory_region_list
;
435 lang_memory_region_type
*new =
436 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
438 new->name
= buystring (name
);
439 new->next
= (lang_memory_region_type
*) NULL
;
441 *lang_memory_region_list_tail
= new;
442 lang_memory_region_list_tail
= &new->next
;
444 new->length
= ~(bfd_size_type
)0;
446 new->had_full_message
= false;
453 lang_output_section_statement_type
*
454 lang_output_section_find (name
)
455 CONST
char *CONST name
;
457 lang_statement_union_type
*u
;
458 lang_output_section_statement_type
*lookup
;
460 for (u
= lang_output_section_statement
.head
;
461 u
!= (lang_statement_union_type
*) NULL
;
464 lookup
= &u
->output_section_statement
;
465 if (strcmp (name
, lookup
->name
) == 0)
470 return (lang_output_section_statement_type
*) NULL
;
473 lang_output_section_statement_type
*
474 lang_output_section_statement_lookup (name
)
475 CONST
char *CONST name
;
477 lang_output_section_statement_type
*lookup
;
479 lookup
= lang_output_section_find (name
);
480 if (lookup
== (lang_output_section_statement_type
*) NULL
)
483 lookup
= (lang_output_section_statement_type
*)
484 new_stat (lang_output_section_statement
, stat_ptr
);
485 lookup
->region
= (lang_memory_region_type
*) NULL
;
487 lookup
->block_value
= 1;
490 lookup
->next
= (lang_statement_union_type
*) NULL
;
491 lookup
->bfd_section
= (asection
*) NULL
;
492 lookup
->processed
= false;
493 lookup
->loadable
= 1;
494 lookup
->addr_tree
= (etree_type
*) NULL
;
495 lang_list_init (&lookup
->children
);
497 lookup
->memspec
= (CONST
char *) NULL
;
499 lookup
->subsection_alignment
= -1;
500 lookup
->section_alignment
= -1;
501 lookup
->load_base
= (union etree_union
*) NULL
;
502 lookup
->phdrs
= NULL
;
504 lang_statement_append (&lang_output_section_statement
,
505 (lang_statement_union_type
*) lookup
,
514 lang_memory_region_type
*m
;
516 minfo ("\nMemory Configuration\n\n");
517 fprintf (config
.map_file
, "%-16s %-18s %-18s\n",
518 "Name", "Origin", "Length");
520 for (m
= lang_memory_region_list
;
521 m
!= (lang_memory_region_type
*) NULL
;
527 fprintf (config
.map_file
, "%-16s ", m
->name
);
529 sprintf_vma (buf
, m
->origin
);
530 minfo ("0x%s ", buf
);
538 minfo ("0x%V\n", m
->length
);
541 fprintf (config
.map_file
, "\nLinker script and memory map\n\n");
551 lang_output_section_statement_type
* s
;
553 section_userdata_type
*new;
555 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
556 einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME
);
558 new = ((section_userdata_type
*)
559 stat_alloc (sizeof (section_userdata_type
)));
561 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
562 if (s
->bfd_section
== (asection
*) NULL
)
563 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
564 if (s
->bfd_section
== (asection
*) NULL
)
566 einfo ("%P%F: output format %s cannot represent section called %s\n",
567 output_bfd
->xvec
->name
, s
->name
);
569 s
->bfd_section
->output_section
= s
->bfd_section
;
571 /* We initialize an output sections output offset to minus its own */
572 /* vma to allow us to output a section through itself */
573 s
->bfd_section
->output_offset
= 0;
574 get_userdata (s
->bfd_section
) = (PTR
) new;
577 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
578 once into the output. This routine checks each sections, and
579 arranges to discard it if a section of the same name has already
580 been linked. This code assumes that all relevant sections have the
581 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
582 section name. This is called via bfd_map_over_sections. */
586 section_already_linked (abfd
, sec
, ignore
)
593 struct sec_link_once
*next
;
596 static struct sec_link_once
*sec_link_once_list
;
599 struct sec_link_once
*l
;
601 flags
= bfd_get_section_flags (abfd
, sec
);
603 if ((flags
& SEC_LINK_ONCE
) == 0)
606 name
= bfd_get_section_name (abfd
, sec
);
608 for (l
= sec_link_once_list
; l
!= NULL
; l
= l
->next
)
610 if (strcmp (name
, bfd_get_section_name (l
->sec
->owner
, l
->sec
)) == 0)
612 /* The section has already been linked. See if we should
614 switch (flags
& SEC_LINK_DUPLICATES
)
619 case SEC_LINK_DUPLICATES_DISCARD
:
622 case SEC_LINK_DUPLICATES_ONE_ONLY
:
623 einfo ("%P: %B: warning: ignoring duplicate section `%s'\n",
627 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
628 /* FIXME: We should really dig out the contents of both
629 sections and memcmp them. The COFF/PE spec says that
630 the Microsoft linker does not implement this
631 correctly, so I'm not going to bother doing it
634 case SEC_LINK_DUPLICATES_SAME_SIZE
:
635 if (bfd_section_size (abfd
, sec
)
636 != bfd_section_size (l
->sec
->owner
, l
->sec
))
637 einfo ("%P: %B: warning: duplicate section `%s' has different size\n",
642 /* Set the output_section field so that wild_doit does not
643 create a lang_input_section structure for this section. */
644 sec
->output_section
= bfd_abs_section_ptr
;
650 /* This is the first section with this name. Record it. */
652 l
= (struct sec_link_once
*) xmalloc (sizeof *l
);
654 l
->next
= sec_link_once_list
;
655 sec_link_once_list
= l
;
658 /* The wild routines.
660 These expand statements like *(.text) and foo.o to a list of
661 explicit actions, like foo.o(.text), bar.o(.text) and
662 foo.o(.text, .data). */
664 /* Add SECTION to the output section OUTPUT. Do this by creating a
665 lang_input_section statement which is placed at PTR. FILE is the
666 input file which holds SECTION. */
669 wild_doit (ptr
, section
, output
, file
)
670 lang_statement_list_type
*ptr
;
672 lang_output_section_statement_type
*output
;
673 lang_input_statement_type
*file
;
678 flags
= bfd_get_section_flags (section
->owner
, section
);
682 /* If we are doing a final link, discard sections marked with
684 if (! link_info
.relocateable
685 && (flags
& SEC_EXCLUDE
) != 0)
688 /* Discard input sections which are assigned to a section named
689 DISCARD_SECTION_NAME. */
690 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
693 /* Discard debugging sections if we are stripping debugging
695 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
696 && (flags
& SEC_DEBUGGING
) != 0)
701 if (section
->output_section
== NULL
)
703 /* This prevents future calls from assigning this section. */
704 section
->output_section
= bfd_abs_section_ptr
;
709 if (section
->output_section
== NULL
)
711 lang_input_section_type
*new;
713 if (output
->bfd_section
== NULL
)
716 /* Add a section reference to the list */
717 new = new_stat (lang_input_section
, ptr
);
719 new->section
= section
;
721 section
->output_section
= output
->bfd_section
;
723 /* We don't copy the SEC_NEVER_LOAD flag from an input section
724 to an output section, because we want to be able to include a
725 SEC_NEVER_LOAD section in the middle of an otherwise loaded
726 section (I don't know why we want to do this, but we do).
727 build_link_order in ldwrite.c handles this case by turning
728 the embedded SEC_NEVER_LOAD section into a fill. */
729 section
->output_section
->flags
|=
730 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
732 if (! output
->loadable
)
734 /* Turn off load flag */
735 output
->bfd_section
->flags
&= ~SEC_LOAD
;
736 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
739 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
740 output
->bfd_section
->alignment_power
= section
->alignment_power
;
742 /* If supplied an aligment, then force it. */
743 if (output
->section_alignment
!= -1)
744 output
->bfd_section
->alignment_power
= output
->section_alignment
;
748 /* Expand a wild statement for a particular FILE. SECTION may be
749 NULL, in which case it is a wild card. */
752 wild_section (ptr
, section
, file
, output
)
753 lang_wild_statement_type
*ptr
;
755 lang_input_statement_type
*file
;
756 lang_output_section_statement_type
*output
;
758 if (file
->just_syms_flag
== false)
760 register asection
*s
;
762 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
764 /* Attach all sections named SECTION. If SECTION is NULL,
765 then attach all sections.
767 Previously, if SECTION was NULL, this code did not call
768 wild_doit if the SEC_IS_COMMON flag was set for the
769 section. I did not understand that, and I took it out.
773 || strcmp (bfd_get_section_name (file
->the_bfd
, s
),
775 wild_doit (&ptr
->children
, s
, output
, file
);
780 /* This is passed a file name which must have been seen already and
781 added to the statement tree. We will see if it has been opened
782 already and had its symbols read. If not then we'll read it. */
784 static lang_input_statement_type
*
788 lang_input_statement_type
*search
;
790 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
791 search
!= (lang_input_statement_type
*) NULL
;
792 search
= (lang_input_statement_type
*) search
->next_real_file
)
794 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
796 if (search
->filename
!= (char *) NULL
797 && name
!= (char *) NULL
798 && strcmp (search
->filename
, name
) == 0)
802 if (search
== (lang_input_statement_type
*) NULL
)
803 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
806 /* If we have already added this file, or this file is not real
807 (FIXME: can that ever actually happen?) or the name is NULL
808 (FIXME: can that ever actually happen?) don't add this file. */
811 || search
->filename
== (const char *) NULL
)
814 load_symbols (search
, (lang_statement_list_type
*) NULL
);
819 /* Get the symbols for an input file. */
822 load_symbols (entry
, place
)
823 lang_input_statement_type
*entry
;
824 lang_statement_list_type
*place
;
831 ldfile_open_file (entry
);
833 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
834 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
837 lang_statement_list_type
*hold
;
839 err
= bfd_get_error ();
840 if (err
== bfd_error_file_ambiguously_recognized
)
844 einfo ("%B: file not recognized: %E\n", entry
->the_bfd
);
845 einfo ("%B: matching formats:", entry
->the_bfd
);
846 for (p
= matching
; *p
!= NULL
; p
++)
850 else if (err
!= bfd_error_file_not_recognized
852 einfo ("%F%B: file not recognized: %E\n", entry
->the_bfd
);
854 bfd_close (entry
->the_bfd
);
855 entry
->the_bfd
= NULL
;
857 /* See if the emulation has some special knowledge. */
859 if (ldemul_unrecognized_file (entry
))
862 /* Try to interpret the file as a linker script. */
864 ldfile_open_command_file (entry
->filename
);
869 ldfile_assumed_script
= true;
870 parser_input
= input_script
;
872 ldfile_assumed_script
= false;
879 /* We don't call ldlang_add_file for an archive. Instead, the
880 add_symbols entry point will call ldlang_add_file, via the
881 add_archive_element callback, for each element of the archive
883 switch (bfd_get_format (entry
->the_bfd
))
889 ldlang_add_file (entry
);
890 if (trace_files
|| trace_file_tries
)
891 info_msg ("%I\n", entry
);
895 if (entry
->whole_archive
)
897 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
899 while (member
!= NULL
)
901 if (! bfd_check_format (member
, bfd_object
))
902 einfo ("%F%B: object %B in archive is not object\n",
903 entry
->the_bfd
, member
);
904 if (! ((*link_info
.callbacks
->add_archive_element
)
905 (&link_info
, member
, "--whole-archive")))
907 if (! bfd_link_add_symbols (member
, &link_info
))
908 einfo ("%F%B: could not read symbols: %E\n", member
);
909 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
913 entry
->loaded
= true;
919 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
920 einfo ("%F%B: could not read symbols: %E\n", entry
->the_bfd
);
922 entry
->loaded
= true;
925 /* Handle a wild statement. SECTION or FILE or both may be NULL,
926 indicating that it is a wildcard. Separate lang_input_section
927 statements are created for each part of the expansion; they are
928 added after the wild statement S. OUTPUT is the output section. */
931 wild (s
, section
, file
, target
, output
)
932 lang_wild_statement_type
*s
;
936 lang_output_section_statement_type
*output
;
938 lang_input_statement_type
*f
;
940 if (file
== (char *) NULL
)
942 /* Perform the iteration over all files in the list */
943 for (f
= (lang_input_statement_type
*) file_chain
.head
;
944 f
!= (lang_input_statement_type
*) NULL
;
945 f
= (lang_input_statement_type
*) f
->next
)
947 wild_section (s
, section
, f
, output
);
952 /* Perform the iteration over a single file */
953 f
= lookup_name (file
);
954 if (f
->the_bfd
== NULL
955 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
956 wild_section (s
, section
, f
, output
);
961 /* This is an archive file. We must map each member of the
962 archive separately. */
963 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
964 while (member
!= NULL
)
966 /* When lookup_name is called, it will call the
967 add_symbols entry point for the archive. For each
968 element of the archive which is included, BFD will
969 call ldlang_add_file, which will set the usrdata
970 field of the member to the lang_input_statement. */
971 if (member
->usrdata
!= NULL
)
973 wild_section (s
, section
,
974 (lang_input_statement_type
*) member
->usrdata
,
978 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
983 if (section
!= (char *) NULL
984 && strcmp (section
, "COMMON") == 0
985 && default_common_section
== NULL
)
987 /* Remember the section that common is going to in case we later
988 get something which doesn't know where to put it. */
989 default_common_section
= output
;
993 /* Open the output file. */
1001 if (output_target
== (char *) NULL
)
1003 if (current_target
!= (char *) NULL
)
1004 output_target
= current_target
;
1006 output_target
= default_target
;
1008 output
= bfd_openw (name
, output_target
);
1010 if (output
== (bfd
*) NULL
)
1012 if (bfd_get_error () == bfd_error_invalid_target
)
1014 einfo ("%P%F: target %s not found\n", output_target
);
1016 einfo ("%P%F: cannot open output file %s: %E\n", name
);
1019 delete_output_file_on_failure
= 1;
1021 /* output->flags |= D_PAGED;*/
1023 if (! bfd_set_format (output
, bfd_object
))
1024 einfo ("%P%F:%s: can not make object file: %E\n", name
);
1025 if (! bfd_set_arch_mach (output
,
1026 ldfile_output_architecture
,
1027 ldfile_output_machine
))
1028 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
1030 link_info
.hash
= bfd_link_hash_table_create (output
);
1031 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1032 einfo ("%P%F: can not create link hash table: %E\n");
1034 bfd_set_gp_size (output
, g_switch_value
);
1042 ldlang_open_output (statement
)
1043 lang_statement_union_type
* statement
;
1045 switch (statement
->header
.type
)
1047 case lang_output_statement_enum
:
1048 ASSERT (output_bfd
== (bfd
*) NULL
);
1049 output_bfd
= open_output (statement
->output_statement
.name
);
1050 ldemul_set_output_arch ();
1051 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1052 output_bfd
->flags
|= D_PAGED
;
1054 output_bfd
->flags
&= ~D_PAGED
;
1055 if (config
.text_read_only
)
1056 output_bfd
->flags
|= WP_TEXT
;
1058 output_bfd
->flags
&= ~WP_TEXT
;
1059 if (link_info
.traditional_format
)
1060 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1062 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1065 case lang_target_statement_enum
:
1066 current_target
= statement
->target_statement
.target
;
1073 /* Open all the input files. */
1076 open_input_bfds (s
, force
)
1077 lang_statement_union_type
*s
;
1080 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1082 switch (s
->header
.type
)
1084 case lang_constructors_statement_enum
:
1085 open_input_bfds (constructor_list
.head
, force
);
1087 case lang_output_section_statement_enum
:
1088 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1090 case lang_wild_statement_enum
:
1091 /* Maybe we should load the file's symbols */
1092 if (s
->wild_statement
.filename
)
1093 (void) lookup_name (s
->wild_statement
.filename
);
1094 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1096 case lang_group_statement_enum
:
1098 struct bfd_link_hash_entry
*undefs
;
1100 /* We must continually search the entries in the group
1101 until no new symbols are added to the list of undefined
1106 undefs
= link_info
.hash
->undefs_tail
;
1107 open_input_bfds (s
->group_statement
.children
.head
, true);
1109 while (undefs
!= link_info
.hash
->undefs_tail
);
1112 case lang_target_statement_enum
:
1113 current_target
= s
->target_statement
.target
;
1115 case lang_input_statement_enum
:
1116 if (s
->input_statement
.real
== true)
1118 lang_statement_list_type add
;
1120 s
->input_statement
.target
= current_target
;
1122 /* If we are being called from within a group, and this
1123 is an archive which has already been searched, then
1124 force it to be researched. */
1126 && s
->input_statement
.loaded
1127 && bfd_check_format (s
->input_statement
.the_bfd
,
1129 s
->input_statement
.loaded
= false;
1131 lang_list_init (&add
);
1133 load_symbols (&s
->input_statement
, &add
);
1135 if (add
.head
!= NULL
)
1137 *add
.tail
= s
->next
;
1148 /* If there are [COMMONS] statements, put a wild one into the bss section */
1151 lang_reasonable_defaults ()
1154 lang_output_section_statement_lookup (".text");
1155 lang_output_section_statement_lookup (".data");
1157 default_common_section
=
1158 lang_output_section_statement_lookup (".bss");
1161 if (placed_commons
== false)
1163 lang_wild_statement_type
*new =
1164 new_stat (lang_wild_statement
,
1165 &default_common_section
->children
);
1167 new->section_name
= "COMMON";
1168 new->filename
= (char *) NULL
;
1169 lang_list_init (&new->children
);
1176 Add the supplied name to the symbol table as an undefined reference.
1177 Remove items from the chain as we open input bfds
1179 typedef struct ldlang_undef_chain_list
1181 struct ldlang_undef_chain_list
*next
;
1183 } ldlang_undef_chain_list_type
;
1185 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1188 ldlang_add_undef (name
)
1189 CONST
char *CONST name
;
1191 ldlang_undef_chain_list_type
*new =
1192 ((ldlang_undef_chain_list_type
*)
1193 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1195 new->next
= ldlang_undef_chain_list_head
;
1196 ldlang_undef_chain_list_head
= new;
1198 new->name
= buystring (name
);
1201 /* Run through the list of undefineds created above and place them
1202 into the linker hash table as undefined symbols belonging to the
1206 lang_place_undefineds ()
1208 ldlang_undef_chain_list_type
*ptr
;
1210 for (ptr
= ldlang_undef_chain_list_head
;
1211 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1214 struct bfd_link_hash_entry
*h
;
1216 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1217 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1218 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1219 if (h
->type
== bfd_link_hash_new
)
1221 h
->type
= bfd_link_hash_undefined
;
1222 h
->u
.undef
.abfd
= NULL
;
1223 bfd_link_add_undef (link_info
.hash
, h
);
1228 /* Open input files and attatch to output sections */
1230 map_input_to_output_sections (s
, target
, output_section_statement
)
1231 lang_statement_union_type
* s
;
1233 lang_output_section_statement_type
* output_section_statement
;
1235 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1237 switch (s
->header
.type
)
1241 case lang_wild_statement_enum
:
1242 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1243 s
->wild_statement
.filename
, target
,
1244 output_section_statement
);
1247 case lang_constructors_statement_enum
:
1248 map_input_to_output_sections (constructor_list
.head
,
1250 output_section_statement
);
1252 case lang_output_section_statement_enum
:
1253 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1255 &s
->output_section_statement
);
1257 case lang_output_statement_enum
:
1259 case lang_target_statement_enum
:
1260 target
= s
->target_statement
.target
;
1262 case lang_group_statement_enum
:
1263 map_input_to_output_sections (s
->group_statement
.children
.head
,
1265 output_section_statement
);
1267 case lang_fill_statement_enum
:
1268 case lang_input_section_enum
:
1269 case lang_object_symbols_statement_enum
:
1270 case lang_data_statement_enum
:
1271 case lang_reloc_statement_enum
:
1272 case lang_assignment_statement_enum
:
1273 case lang_padding_statement_enum
:
1274 case lang_input_statement_enum
:
1275 if (output_section_statement
!= NULL
1276 && output_section_statement
->bfd_section
== NULL
)
1277 init_os (output_section_statement
);
1279 case lang_afile_asection_pair_statement_enum
:
1282 case lang_address_statement_enum
:
1283 /* Mark the specified section with the supplied address */
1285 lang_output_section_statement_type
*os
=
1286 lang_output_section_statement_lookup
1287 (s
->address_statement
.section_name
);
1289 if (os
->bfd_section
== NULL
)
1291 os
->addr_tree
= s
->address_statement
.address
;
1299 print_output_section_statement (output_section_statement
)
1300 lang_output_section_statement_type
* output_section_statement
;
1302 asection
*section
= output_section_statement
->bfd_section
;
1305 if (output_section_statement
!= abs_output_section
)
1307 minfo ("\n%s", output_section_statement
->name
);
1309 if (section
!= NULL
)
1311 print_dot
= section
->vma
;
1313 len
= strlen (output_section_statement
->name
);
1314 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
1319 while (len
< SECTION_NAME_MAP_LENGTH
)
1325 minfo ("0x%V %W", section
->vma
, section
->_raw_size
);
1327 if (output_section_statement
->load_base
!= NULL
)
1331 addr
= exp_get_abs_int (output_section_statement
->load_base
, 0,
1332 "load base", lang_final_phase_enum
);
1333 minfo (" load address 0x%V", addr
);
1340 print_statement_list (output_section_statement
->children
.head
,
1341 output_section_statement
);
1345 print_assignment (assignment
, output_section
)
1346 lang_assignment_statement_type
* assignment
;
1347 lang_output_section_statement_type
* output_section
;
1350 etree_value_type result
;
1352 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1355 result
= exp_fold_tree (assignment
->exp
->assign
.src
, output_section
,
1356 lang_final_phase_enum
, print_dot
, &print_dot
);
1358 minfo ("0x%V", result
.value
+ result
.section
->bfd_section
->vma
);
1369 exp_print_tree (assignment
->exp
);
1375 print_input_statement (statm
)
1376 lang_input_statement_type
* statm
;
1378 if (statm
->filename
!= (char *) NULL
)
1380 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1384 /* Print all symbols defined in a particular section. This is called
1385 via bfd_link_hash_traverse. */
1388 print_one_symbol (hash_entry
, ptr
)
1389 struct bfd_link_hash_entry
*hash_entry
;
1392 asection
*sec
= (asection
*) ptr
;
1394 if ((hash_entry
->type
== bfd_link_hash_defined
1395 || hash_entry
->type
== bfd_link_hash_defweak
)
1396 && sec
== hash_entry
->u
.def
.section
)
1400 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1403 (hash_entry
->u
.def
.value
1404 + hash_entry
->u
.def
.section
->output_offset
1405 + hash_entry
->u
.def
.section
->output_section
->vma
));
1407 minfo (" %T\n", hash_entry
->root
.string
);
1413 /* Print information about an input section to the map file. */
1416 print_input_section (in
)
1417 lang_input_section_type
* in
;
1419 asection
*i
= in
->section
;
1420 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
1426 minfo ("%s", i
->name
);
1428 if (i
->output_section
!= NULL
)
1432 len
= 1 + strlen (i
->name
);
1433 if (len
>= SECTION_NAME_MAP_LENGTH
- 1)
1438 while (len
< SECTION_NAME_MAP_LENGTH
)
1444 minfo ("0x%V %W %B\n",
1445 i
->output_section
->vma
+ i
->output_offset
, size
,
1448 if (i
->_cooked_size
!= 0 && i
->_cooked_size
!= i
->_raw_size
)
1450 len
= SECTION_NAME_MAP_LENGTH
+ 3;
1462 minfo ("%W (size before relaxing)\n", i
->_raw_size
);
1465 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
1467 print_dot
= i
->output_section
->vma
+ i
->output_offset
+ size
;
1473 print_fill_statement (fill
)
1474 lang_fill_statement_type
* fill
;
1476 fprintf (config
.map_file
, " FILL mask 0x%x\n", fill
->fill
);
1480 print_data_statement (data
)
1481 lang_data_statement_type
* data
;
1488 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1491 addr
= data
->output_vma
;
1492 if (data
->output_section
!= NULL
)
1493 addr
+= data
->output_section
->vma
;
1517 minfo ("0x%V %W %s 0x%v", addr
, size
, name
, data
->value
);
1519 if (data
->exp
->type
.node_class
!= etree_value
)
1522 exp_print_tree (data
->exp
);
1527 print_dot
= addr
+ size
;
1530 /* Print an address statement. These are generated by options like
1534 print_address_statement (address
)
1535 lang_address_statement_type
*address
;
1537 minfo ("Address of section %s set to ", address
->section_name
);
1538 exp_print_tree (address
->address
);
1542 /* Print a reloc statement. */
1545 print_reloc_statement (reloc
)
1546 lang_reloc_statement_type
*reloc
;
1552 for (i
= 0; i
< SECTION_NAME_MAP_LENGTH
; i
++)
1555 addr
= reloc
->output_vma
;
1556 if (reloc
->output_section
!= NULL
)
1557 addr
+= reloc
->output_section
->vma
;
1559 size
= bfd_get_reloc_size (reloc
->howto
);
1561 minfo ("0x%V %W RELOC %s ", addr
, size
, reloc
->howto
->name
);
1563 if (reloc
->name
!= NULL
)
1564 minfo ("%s+", reloc
->name
);
1566 minfo ("%s+", reloc
->section
->name
);
1568 exp_print_tree (reloc
->addend_exp
);
1572 print_dot
= addr
+ size
;
1576 print_padding_statement (s
)
1577 lang_padding_statement_type
*s
;
1584 len
= sizeof " *fill*" - 1;
1585 while (len
< SECTION_NAME_MAP_LENGTH
)
1591 addr
= s
->output_offset
;
1592 if (s
->output_section
!= NULL
)
1593 addr
+= s
->output_section
->vma
;
1594 minfo ("0x%V %W", addr
, s
->size
);
1597 minfo (" 0x%x", s
->fill
);
1601 print_dot
= addr
+ s
->size
;
1605 print_wild_statement (w
, os
)
1606 lang_wild_statement_type
* w
;
1607 lang_output_section_statement_type
* os
;
1611 if (w
->filename
!= NULL
)
1612 minfo ("%s", w
->filename
);
1616 if (w
->section_name
!= NULL
)
1617 minfo ("(%s)", w
->section_name
);
1623 print_statement_list (w
->children
.head
, os
);
1626 /* Print a group statement. */
1630 lang_group_statement_type
*s
;
1631 lang_output_section_statement_type
*os
;
1633 fprintf (config
.map_file
, "START GROUP\n");
1634 print_statement_list (s
->children
.head
, os
);
1635 fprintf (config
.map_file
, "END GROUP\n");
1638 /* Print the list of statements in S.
1639 This can be called for any statement type. */
1642 print_statement_list (s
, os
)
1643 lang_statement_union_type
*s
;
1644 lang_output_section_statement_type
*os
;
1648 print_statement (s
, os
);
1653 /* Print the first statement in statement list S.
1654 This can be called for any statement type. */
1657 print_statement (s
, os
)
1658 lang_statement_union_type
*s
;
1659 lang_output_section_statement_type
*os
;
1661 switch (s
->header
.type
)
1664 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1667 case lang_constructors_statement_enum
:
1668 if (constructor_list
.head
!= NULL
)
1670 minfo (" CONSTRUCTORS\n");
1671 print_statement_list (constructor_list
.head
, os
);
1674 case lang_wild_statement_enum
:
1675 print_wild_statement (&s
->wild_statement
, os
);
1677 case lang_address_statement_enum
:
1678 print_address_statement (&s
->address_statement
);
1680 case lang_object_symbols_statement_enum
:
1681 minfo (" CREATE_OBJECT_SYMBOLS\n");
1683 case lang_fill_statement_enum
:
1684 print_fill_statement (&s
->fill_statement
);
1686 case lang_data_statement_enum
:
1687 print_data_statement (&s
->data_statement
);
1689 case lang_reloc_statement_enum
:
1690 print_reloc_statement (&s
->reloc_statement
);
1692 case lang_input_section_enum
:
1693 print_input_section (&s
->input_section
);
1695 case lang_padding_statement_enum
:
1696 print_padding_statement (&s
->padding_statement
);
1698 case lang_output_section_statement_enum
:
1699 print_output_section_statement (&s
->output_section_statement
);
1701 case lang_assignment_statement_enum
:
1702 print_assignment (&s
->assignment_statement
, os
);
1704 case lang_target_statement_enum
:
1705 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1707 case lang_output_statement_enum
:
1708 minfo ("OUTPUT(%s", s
->output_statement
.name
);
1709 if (output_target
!= NULL
)
1710 minfo (" %s", output_target
);
1713 case lang_input_statement_enum
:
1714 print_input_statement (&s
->input_statement
);
1716 case lang_group_statement_enum
:
1717 print_group (&s
->group_statement
, os
);
1719 case lang_afile_asection_pair_statement_enum
:
1728 print_statement_list (statement_list
.head
, abs_output_section
);
1731 /* Print the first N statements in statement list S to STDERR.
1732 If N == 0, nothing is printed.
1733 If N < 0, the entire list is printed.
1734 Intended to be called from GDB. */
1737 dprint_statement (s
, n
)
1738 lang_statement_union_type
* s
;
1741 FILE *map_save
= config
.map_file
;
1743 config
.map_file
= stderr
;
1746 print_statement_list (s
, abs_output_section
);
1751 print_statement (s
, abs_output_section
);
1756 config
.map_file
= map_save
;
1760 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1761 lang_statement_union_type
** this_ptr
;
1764 asection
* output_section_statement
;
1767 /* Align this section first to the
1768 input sections requirement, then
1769 to the output section's requirement.
1770 If this alignment is > than any seen before,
1771 then record it too. Perform the alignment by
1772 inserting a magic 'padding' statement.
1775 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1777 if (alignment_needed
!= 0)
1779 lang_statement_union_type
*new =
1780 ((lang_statement_union_type
*)
1781 stat_alloc (sizeof (lang_padding_statement_type
)));
1783 /* Link into existing chain */
1784 new->header
.next
= *this_ptr
;
1786 new->header
.type
= lang_padding_statement_enum
;
1787 new->padding_statement
.output_section
= output_section_statement
;
1788 new->padding_statement
.output_offset
=
1789 dot
- output_section_statement
->vma
;
1790 new->padding_statement
.fill
= fill
;
1791 new->padding_statement
.size
= alignment_needed
;
1795 /* Remember the most restrictive alignment */
1796 if (power
> output_section_statement
->alignment_power
)
1798 output_section_statement
->alignment_power
= power
;
1800 output_section_statement
->_raw_size
+= alignment_needed
;
1801 return alignment_needed
+ dot
;
1805 /* Work out how much this section will move the dot point */
1807 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1808 lang_statement_union_type
** this_ptr
;
1809 lang_output_section_statement_type
* output_section_statement
;
1814 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1815 asection
*i
= is
->section
;
1817 if (is
->ifile
->just_syms_flag
== false)
1819 if (output_section_statement
->subsection_alignment
!= -1)
1820 i
->alignment_power
=
1821 output_section_statement
->subsection_alignment
;
1823 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1824 output_section_statement
->bfd_section
, dot
);
1826 /* Remember where in the output section this input section goes */
1828 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1830 /* Mark how big the output section must be to contain this now
1832 if (i
->_cooked_size
!= 0)
1833 dot
+= i
->_cooked_size
;
1835 dot
+= i
->_raw_size
;
1836 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1840 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1846 /* This variable indicates whether bfd_relax_section should be called
1849 static boolean relax_again
;
1851 /* Set the sizes for all the output sections. */
1854 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1855 lang_statement_union_type
* s
;
1856 lang_output_section_statement_type
* output_section_statement
;
1857 lang_statement_union_type
** prev
;
1862 /* Size up the sections from their constituent parts */
1863 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1865 switch (s
->header
.type
)
1868 case lang_output_section_statement_enum
:
1871 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1873 if (os
->bfd_section
== NULL
)
1875 /* This section was never actually created. */
1879 /* If this is a COFF shared library section, use the size and
1880 address from the input section. FIXME: This is COFF
1881 specific; it would be cleaner if there were some other way
1882 to do this, but nothing simple comes to mind. */
1883 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
1887 if (os
->children
.head
== NULL
1888 || os
->children
.head
->next
!= NULL
1889 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
1890 einfo ("%P%X: Internal error on COFF shared library section %s",
1893 input
= os
->children
.head
->input_section
.section
;
1894 bfd_set_section_vma (os
->bfd_section
->owner
,
1896 bfd_section_vma (input
->owner
, input
));
1897 os
->bfd_section
->_raw_size
= input
->_raw_size
;
1901 if (bfd_is_abs_section (os
->bfd_section
))
1903 /* No matter what happens, an abs section starts at zero */
1904 ASSERT (os
->bfd_section
->vma
== 0);
1908 if (os
->addr_tree
== (etree_type
*) NULL
)
1910 /* No address specified for this section, get one
1911 from the region specification
1913 if (os
->region
== (lang_memory_region_type
*) NULL
)
1915 os
->region
= lang_memory_region_lookup ("*default*");
1917 dot
= os
->region
->current
;
1918 if (os
->section_alignment
== -1)
1919 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1925 r
= exp_fold_tree (os
->addr_tree
,
1927 lang_allocating_phase_enum
,
1929 if (r
.valid
== false)
1931 einfo ("%F%S: non constant address expression for section %s\n",
1936 /* The section starts here */
1937 /* First, align to what the section needs */
1939 if (os
->section_alignment
!= -1)
1940 dot
= align_power (dot
, os
->section_alignment
);
1942 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1944 os
->bfd_section
->output_offset
= 0;
1947 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1948 os
->fill
, dot
, relax
);
1949 /* Ignore the size of the input sections, use the vma and size to */
1952 after
= ALIGN_N (os
->bfd_section
->vma
+
1953 os
->bfd_section
->_raw_size
,
1954 /* The coercion here is important, see ld.h. */
1955 (bfd_vma
) os
->block_value
);
1957 if (bfd_is_abs_section (os
->bfd_section
))
1958 ASSERT (after
== os
->bfd_section
->vma
);
1960 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1961 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1962 os
->processed
= true;
1964 /* Replace into region ? */
1965 if (os
->region
!= (lang_memory_region_type
*) NULL
)
1967 os
->region
->current
= dot
;
1968 /* Make sure this isn't silly. */
1969 if (os
->region
->current
< os
->region
->origin
1970 || (os
->region
->current
- os
->region
->origin
1971 > os
->region
->length
))
1973 if (os
->addr_tree
!= (etree_type
*) NULL
)
1975 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
1976 os
->region
->current
,
1977 os
->bfd_section
->owner
,
1978 os
->bfd_section
->name
,
1983 einfo ("%X%P: region %s is full (%B section %s)\n",
1985 os
->bfd_section
->owner
,
1986 os
->bfd_section
->name
);
1988 /* Reset the region pointer. */
1989 os
->region
->current
= os
->region
->origin
;
1995 case lang_constructors_statement_enum
:
1996 dot
= lang_size_sections (constructor_list
.head
,
1997 output_section_statement
,
1998 &s
->wild_statement
.children
.head
,
2003 case lang_data_statement_enum
:
2005 unsigned int size
= 0;
2007 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2008 s
->data_statement
.output_section
=
2009 output_section_statement
->bfd_section
;
2011 switch (s
->data_statement
.type
)
2028 output_section_statement
->bfd_section
->_raw_size
+= size
;
2029 /* The output section gets contents, and then we inspect for
2030 any flags set in the input script which override any ALLOC */
2031 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2032 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2033 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2038 case lang_reloc_statement_enum
:
2042 s
->reloc_statement
.output_vma
=
2043 dot
- output_section_statement
->bfd_section
->vma
;
2044 s
->reloc_statement
.output_section
=
2045 output_section_statement
->bfd_section
;
2046 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2048 output_section_statement
->bfd_section
->_raw_size
+= size
;
2052 case lang_wild_statement_enum
:
2054 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2055 output_section_statement
,
2056 &s
->wild_statement
.children
.head
,
2062 case lang_object_symbols_statement_enum
:
2063 link_info
.create_object_symbols_section
=
2064 output_section_statement
->bfd_section
;
2066 case lang_output_statement_enum
:
2067 case lang_target_statement_enum
:
2069 case lang_input_section_enum
:
2073 i
= (*prev
)->input_section
.section
;
2076 if (i
->_cooked_size
== 0)
2077 i
->_cooked_size
= i
->_raw_size
;
2083 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2084 einfo ("%P%F: can't relax section: %E\n");
2088 dot
= size_input_section (prev
,
2089 output_section_statement
,
2090 output_section_statement
->fill
,
2094 case lang_input_statement_enum
:
2096 case lang_fill_statement_enum
:
2097 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2099 fill
= s
->fill_statement
.fill
;
2101 case lang_assignment_statement_enum
:
2103 bfd_vma newdot
= dot
;
2105 exp_fold_tree (s
->assignment_statement
.exp
,
2106 output_section_statement
,
2107 lang_allocating_phase_enum
,
2111 if (newdot
!= dot
&& !relax
)
2113 /* The assignment changed dot. Insert a pad. */
2114 if (output_section_statement
== abs_output_section
)
2116 /* If we don't have an output section, then just adjust
2117 the default memory address. */
2118 lang_memory_region_lookup ("*default*")->current
= newdot
;
2122 lang_statement_union_type
*new =
2123 ((lang_statement_union_type
*)
2124 stat_alloc (sizeof (lang_padding_statement_type
)));
2126 /* Link into existing chain */
2127 new->header
.next
= *prev
;
2129 new->header
.type
= lang_padding_statement_enum
;
2130 new->padding_statement
.output_section
=
2131 output_section_statement
->bfd_section
;
2132 new->padding_statement
.output_offset
=
2133 dot
- output_section_statement
->bfd_section
->vma
;
2134 new->padding_statement
.fill
= fill
;
2135 new->padding_statement
.size
= newdot
- dot
;
2136 output_section_statement
->bfd_section
->_raw_size
+=
2137 new->padding_statement
.size
;
2145 case lang_padding_statement_enum
:
2146 /* If we are relaxing, and this is not the first pass, some
2147 padding statements may have been inserted during previous
2148 passes. We may have to move the padding statement to a new
2149 location if dot has a different value at this point in this
2150 pass than it did at this point in the previous pass. */
2151 s
->padding_statement
.output_offset
=
2152 dot
- output_section_statement
->bfd_section
->vma
;
2153 dot
+= s
->padding_statement
.size
;
2154 output_section_statement
->bfd_section
->_raw_size
+=
2155 s
->padding_statement
.size
;
2158 case lang_group_statement_enum
:
2159 dot
= lang_size_sections (s
->group_statement
.children
.head
,
2160 output_section_statement
,
2161 &s
->group_statement
.children
.head
,
2169 /* This can only get here when relaxing is turned on */
2171 case lang_address_statement_enum
:
2174 prev
= &s
->header
.next
;
2180 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
2181 lang_statement_union_type
* s
;
2182 lang_output_section_statement_type
* output_section_statement
;
2186 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2188 switch (s
->header
.type
)
2190 case lang_constructors_statement_enum
:
2191 dot
= lang_do_assignments (constructor_list
.head
,
2192 output_section_statement
,
2197 case lang_output_section_statement_enum
:
2199 lang_output_section_statement_type
*os
=
2200 &(s
->output_section_statement
);
2202 if (os
->bfd_section
!= NULL
)
2204 dot
= os
->bfd_section
->vma
;
2205 (void) lang_do_assignments (os
->children
.head
, os
,
2207 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2211 /* If nothing has been placed into the output section then
2212 it won't have a bfd_section. */
2213 if (os
->bfd_section
)
2215 os
->bfd_section
->lma
2216 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
2221 case lang_wild_statement_enum
:
2223 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
2224 output_section_statement
,
2229 case lang_object_symbols_statement_enum
:
2230 case lang_output_statement_enum
:
2231 case lang_target_statement_enum
:
2233 case lang_common_statement_enum
:
2236 case lang_data_statement_enum
:
2238 etree_value_type value
;
2240 value
= exp_fold_tree (s
->data_statement
.exp
,
2242 lang_final_phase_enum
, dot
, &dot
);
2243 s
->data_statement
.value
= value
.value
;
2244 if (value
.valid
== false)
2245 einfo ("%F%P: invalid data statement\n");
2247 switch (s
->data_statement
.type
)
2264 case lang_reloc_statement_enum
:
2266 etree_value_type value
;
2268 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
2270 lang_final_phase_enum
, dot
, &dot
);
2271 s
->reloc_statement
.addend_value
= value
.value
;
2272 if (value
.valid
== false)
2273 einfo ("%F%P: invalid reloc statement\n");
2275 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2278 case lang_input_section_enum
:
2280 asection
*in
= s
->input_section
.section
;
2282 if (in
->_cooked_size
!= 0)
2283 dot
+= in
->_cooked_size
;
2285 dot
+= in
->_raw_size
;
2289 case lang_input_statement_enum
:
2291 case lang_fill_statement_enum
:
2292 fill
= s
->fill_statement
.fill
;
2294 case lang_assignment_statement_enum
:
2296 exp_fold_tree (s
->assignment_statement
.exp
,
2297 output_section_statement
,
2298 lang_final_phase_enum
,
2304 case lang_padding_statement_enum
:
2305 dot
+= s
->padding_statement
.size
;
2308 case lang_group_statement_enum
:
2309 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
2310 output_section_statement
,
2318 case lang_address_statement_enum
:
2326 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2327 operator .startof. (section_name), it produces an undefined symbol
2328 .startof.section_name. Similarly, when it sees
2329 .sizeof. (section_name), it produces an undefined symbol
2330 .sizeof.section_name. For all the output sections, we look for
2331 such symbols, and set them to the correct value. */
2338 if (link_info
.relocateable
)
2341 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2343 const char *secname
;
2345 struct bfd_link_hash_entry
*h
;
2347 secname
= bfd_get_section_name (output_bfd
, s
);
2348 buf
= xmalloc (10 + strlen (secname
));
2350 sprintf (buf
, ".startof.%s", secname
);
2351 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2352 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2354 h
->type
= bfd_link_hash_defined
;
2355 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
2356 h
->u
.def
.section
= bfd_abs_section_ptr
;
2359 sprintf (buf
, ".sizeof.%s", secname
);
2360 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2361 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2363 h
->type
= bfd_link_hash_defined
;
2364 if (s
->_cooked_size
!= 0)
2365 h
->u
.def
.value
= s
->_cooked_size
;
2367 h
->u
.def
.value
= s
->_raw_size
;
2368 h
->u
.def
.section
= bfd_abs_section_ptr
;
2378 struct bfd_link_hash_entry
*h
;
2381 if (link_info
.relocateable
|| link_info
.shared
)
2386 if (entry_symbol
== (char *) NULL
)
2388 /* No entry has been specified. Look for start, but don't warn
2389 if we don't find it. */
2390 entry_symbol
= "start";
2394 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
2395 if (h
!= (struct bfd_link_hash_entry
*) NULL
2396 && (h
->type
== bfd_link_hash_defined
2397 || h
->type
== bfd_link_hash_defweak
)
2398 && h
->u
.def
.section
->output_section
!= NULL
)
2402 val
= (h
->u
.def
.value
2403 + bfd_get_section_vma (output_bfd
,
2404 h
->u
.def
.section
->output_section
)
2405 + h
->u
.def
.section
->output_offset
);
2406 if (! bfd_set_start_address (output_bfd
, val
))
2407 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
2413 /* Can't find the entry symbol. Use the first address in the
2415 ts
= bfd_get_section_by_name (output_bfd
, ".text");
2416 if (ts
!= (asection
*) NULL
)
2419 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2420 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
2421 if (! bfd_set_start_address (output_bfd
,
2422 bfd_get_section_vma (output_bfd
, ts
)))
2423 einfo ("%P%F: can't set start address\n");
2428 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2434 /* Check that the architecture of all the input files is compatible
2435 with the output file. Also call the backend to let it do any
2436 other checking that is needed. */
2441 lang_statement_union_type
*file
;
2443 CONST bfd_arch_info_type
*compatible
;
2445 for (file
= file_chain
.head
;
2446 file
!= (lang_statement_union_type
*) NULL
;
2447 file
= file
->input_statement
.next
)
2449 input_bfd
= file
->input_statement
.the_bfd
;
2450 compatible
= bfd_arch_get_compatible (input_bfd
,
2452 if (compatible
== NULL
)
2453 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2454 bfd_printable_name (input_bfd
), input_bfd
,
2455 bfd_printable_name (output_bfd
));
2458 bfd_merge_private_bfd_data (input_bfd
, output_bfd
);
2462 /* Look through all the global common symbols and attach them to the
2463 correct section. The -sort-common command line switch may be used
2464 to roughly sort the entries by size. */
2469 if (link_info
.relocateable
2470 && ! command_line
.force_common_definition
)
2473 if (! config
.sort_common
)
2474 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2479 for (power
= 4; power
>= 0; power
--)
2480 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2485 /* Place one common symbol in the correct section. */
2488 lang_one_common (h
, info
)
2489 struct bfd_link_hash_entry
*h
;
2492 unsigned int power_of_two
;
2496 if (h
->type
!= bfd_link_hash_common
)
2500 power_of_two
= h
->u
.c
.p
->alignment_power
;
2502 if (config
.sort_common
2503 && power_of_two
< (unsigned int) *(int *) info
)
2506 section
= h
->u
.c
.p
->section
;
2508 /* Increase the size of the section. */
2509 section
->_raw_size
= ALIGN_N (section
->_raw_size
,
2510 (bfd_size_type
) (1 << power_of_two
));
2512 /* Adjust the alignment if necessary. */
2513 if (power_of_two
> section
->alignment_power
)
2514 section
->alignment_power
= power_of_two
;
2516 /* Change the symbol from common to defined. */
2517 h
->type
= bfd_link_hash_defined
;
2518 h
->u
.def
.section
= section
;
2519 h
->u
.def
.value
= section
->_raw_size
;
2521 /* Increase the size of the section. */
2522 section
->_raw_size
+= size
;
2524 /* Make sure the section is allocated in memory. */
2525 section
->flags
|= SEC_ALLOC
;
2527 if (config
.map_file
!= NULL
)
2529 static boolean header_printed
;
2534 if (! header_printed
)
2536 minfo ("\nAllocating common symbols\n");
2537 minfo ("Common symbol size file\n\n");
2538 header_printed
= true;
2541 name
= demangle (h
->root
.string
);
2543 len
= strlen (name
);
2558 if (size
<= 0xffffffff)
2559 sprintf (buf
, "%lx", (unsigned long) size
);
2561 sprintf_vma (buf
, size
);
2571 minfo ("%B\n", section
->owner
);
2578 run through the input files and ensure that every input
2579 section has somewhere to go. If one is found without
2580 a destination then create an input request and place it
2581 into the statement tree.
2585 lang_place_orphans ()
2587 lang_input_statement_type
*file
;
2589 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2590 file
!= (lang_input_statement_type
*) NULL
;
2591 file
= (lang_input_statement_type
*) file
->next
)
2595 for (s
= file
->the_bfd
->sections
;
2596 s
!= (asection
*) NULL
;
2599 if (s
->output_section
== (asection
*) NULL
)
2601 /* This section of the file is not attatched, root
2602 around for a sensible place for it to go */
2604 if (file
->just_syms_flag
)
2606 /* We are only retrieving symbol values from this
2607 file. We want the symbols to act as though the
2608 values in the file are absolute. */
2609 s
->output_section
= bfd_abs_section_ptr
;
2610 s
->output_offset
= s
->vma
;
2612 else if (file
->common_section
== s
)
2614 /* This is a lonely common section which must
2615 have come from an archive. We attatch to the
2616 section with the wildcard */
2617 if (! link_info
.relocateable
2618 && ! command_line
.force_common_definition
)
2620 if (default_common_section
==
2621 (lang_output_section_statement_type
*) NULL
)
2623 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2625 default_common_section
=
2626 lang_output_section_statement_lookup (".bss");
2629 wild_doit (&default_common_section
->children
, s
,
2630 default_common_section
, file
);
2633 else if (ldemul_place_orphan (file
, s
))
2637 lang_output_section_statement_type
*os
=
2638 lang_output_section_statement_lookup (s
->name
);
2640 wild_doit (&os
->children
, s
, os
, file
);
2649 lang_set_flags (ptr
, flags
)
2653 boolean state
= false;
2668 /* ptr->flag_read = state; */
2671 /* ptr->flag_write = state; */
2674 /* ptr->flag_executable= state;*/
2678 /* ptr->flag_loadable= state;*/
2681 einfo ("%P%F: invalid syntax in flags\n");
2688 /* Call a function on each input file. This function will be called
2689 on an archive, but not on the elements. */
2692 lang_for_each_input_file (func
)
2693 void (*func
) PARAMS ((lang_input_statement_type
*));
2695 lang_input_statement_type
*f
;
2697 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
2699 f
= (lang_input_statement_type
*) f
->next_real_file
)
2703 /* Call a function on each file. The function will be called on all
2704 the elements of an archive which are included in the link, but will
2705 not be called on the archive file itself. */
2708 lang_for_each_file (func
)
2709 void (*func
) PARAMS ((lang_input_statement_type
*));
2711 lang_input_statement_type
*f
;
2713 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2714 f
!= (lang_input_statement_type
*) NULL
;
2715 f
= (lang_input_statement_type
*) f
->next
)
2726 lang_for_each_input_section (func
)
2727 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2729 lang_input_statement_type
*f
;
2731 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2732 f
!= (lang_input_statement_type
*) NULL
;
2733 f
= (lang_input_statement_type
*) f
->next
)
2737 for (s
= f
->the_bfd
->sections
;
2738 s
!= (asection
*) NULL
;
2741 func (f
->the_bfd
, s
);
2749 ldlang_add_file (entry
)
2750 lang_input_statement_type
* entry
;
2754 lang_statement_append (&file_chain
,
2755 (lang_statement_union_type
*) entry
,
2758 /* The BFD linker needs to have a list of all input BFDs involved in
2760 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2761 ASSERT (entry
->the_bfd
!= output_bfd
);
2762 for (pp
= &link_info
.input_bfds
;
2763 *pp
!= (bfd
*) NULL
;
2764 pp
= &(*pp
)->link_next
)
2766 *pp
= entry
->the_bfd
;
2767 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2768 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
2770 /* Look through the sections and check for any which should not be
2771 included in the link. We need to do this now, so that we can
2772 notice when the backend linker tries to report multiple
2773 definition errors for symbols which are in sections we aren't
2774 going to link. FIXME: It might be better to entirely ignore
2775 symbols which are defined in sections which are going to be
2776 discarded. This would require modifying the backend linker for
2777 each backend which might set the SEC_LINK_ONCE flag. If we do
2778 this, we should probably handle SEC_EXCLUDE in the same way. */
2780 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) NULL
);
2784 lang_add_output (name
, from_script
)
2788 /* Make -o on command line override OUTPUT in script. */
2789 if (had_output_filename
== false || !from_script
)
2791 output_filename
= name
;
2792 had_output_filename
= true;
2797 static lang_output_section_statement_type
*current_section
;
2809 for (l
= 0; l
< 32; l
++)
2811 if (i
>= (unsigned int) x
)
2819 lang_enter_output_section_statement (output_section_statement_name
,
2820 address_exp
, flags
, block_value
,
2821 align
, subalign
, ebase
)
2822 const char *output_section_statement_name
;
2823 etree_type
* address_exp
;
2825 bfd_vma block_value
;
2827 etree_type
*subalign
;
2830 lang_output_section_statement_type
*os
;
2834 lang_output_section_statement_lookup (output_section_statement_name
);
2838 /* Add this statement to tree */
2839 /* add_statement(lang_output_section_statement_enum,
2840 output_section_statement);*/
2841 /* Make next things chain into subchain of this */
2843 if (os
->addr_tree
==
2844 (etree_type
*) NULL
)
2850 if (flags
& SEC_NEVER_LOAD
)
2854 os
->block_value
= block_value
? block_value
: 1;
2855 stat_ptr
= &os
->children
;
2857 os
->subsection_alignment
= topower(
2858 exp_get_value_int(subalign
, -1,
2859 "subsection alignment",
2861 os
->section_alignment
= topower(
2862 exp_get_value_int(align
, -1,
2863 "section alignment", 0));
2865 os
->load_base
= ebase
;
2872 lang_output_statement_type
*new =
2873 new_stat (lang_output_statement
, stat_ptr
);
2875 new->name
= output_filename
;
2878 /* Reset the current counters in the regions */
2880 reset_memory_regions ()
2882 lang_memory_region_type
*p
= lang_memory_region_list
;
2884 for (p
= lang_memory_region_list
;
2885 p
!= (lang_memory_region_type
*) NULL
;
2888 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2889 p
->current
= p
->origin
;
2896 lang_reasonable_defaults ();
2897 current_target
= default_target
;
2899 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2901 ldemul_create_output_section_statements ();
2903 /* Add to the hash table all undefineds on the command line */
2904 lang_place_undefineds ();
2906 /* Create a bfd for each input file */
2907 current_target
= default_target
;
2908 open_input_bfds (statement_list
.head
, false);
2910 ldemul_after_open ();
2912 /* Build all sets based on the information gathered from the input
2914 ldctor_build_sets ();
2916 /* Size up the common data */
2919 /* Run through the contours of the script and attatch input sections
2920 to the correct output sections
2922 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2923 (lang_output_section_statement_type
*) NULL
);
2926 /* Find any sections not attatched explicitly and handle them */
2927 lang_place_orphans ();
2929 ldemul_before_allocation ();
2931 /* We must record the program headers before we try to fix the
2932 section positions, since they will affect SIZEOF_HEADERS. */
2933 lang_record_phdrs ();
2935 /* Now run around and relax if we can */
2936 if (command_line
.relax
)
2938 /* First time round is a trial run to get the 'worst case'
2939 addresses of the objects if there was no relaxing. */
2940 lang_size_sections (statement_list
.head
,
2942 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2944 /* Keep relaxing until bfd_relax_section gives up. */
2947 reset_memory_regions ();
2949 relax_again
= false;
2951 /* Do all the assignments with our current guesses as to
2953 lang_do_assignments (statement_list
.head
,
2955 (fill_type
) 0, (bfd_vma
) 0);
2957 /* Perform another relax pass - this time we know where the
2958 globals are, so can make better guess. */
2959 lang_size_sections (statement_list
.head
,
2961 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2963 while (relax_again
);
2967 /* Size up the sections. */
2968 lang_size_sections (statement_list
.head
,
2970 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2973 /* See if anything special should be done now we know how big
2975 ldemul_after_allocation ();
2977 /* Fix any .startof. or .sizeof. symbols. */
2978 lang_set_startof ();
2980 /* Do all the assignments, now that we know the final restingplaces
2981 of all the symbols */
2983 lang_do_assignments (statement_list
.head
,
2985 (fill_type
) 0, (bfd_vma
) 0);
2987 /* Make sure that we're not mixing architectures */
2997 /* EXPORTED TO YACC */
3000 lang_add_wild (section_name
, filename
)
3001 CONST
char *CONST section_name
;
3002 CONST
char *CONST filename
;
3004 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
3007 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
3009 placed_commons
= true;
3011 if (filename
!= (char *) NULL
)
3013 lang_has_input_file
= true;
3015 new->section_name
= section_name
;
3016 new->filename
= filename
;
3017 lang_list_init (&new->children
);
3021 lang_section_start (name
, address
)
3023 etree_type
* address
;
3025 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
3027 ad
->section_name
= name
;
3028 ad
->address
= address
;
3031 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3032 because of a -e argument on the command line, or zero if this is
3033 called by ENTRY in a linker script. Command line arguments take
3036 /* WINDOWS_NT. When an entry point has been specified, we will also force
3037 this symbol to be defined by calling ldlang_add_undef (equivalent to
3038 having switch -u entry_name on the command line). The reason we do
3039 this is so that the user doesn't have to because they would have to use
3040 the -u switch if they were specifying an entry point other than
3041 _mainCRTStartup. Specifically, if creating a windows application, entry
3042 point _WinMainCRTStartup must be specified.
3043 What I have found for non console applications (entry not _mainCRTStartup)
3044 is that the .obj that contains mainCRTStartup is brought in since it is
3045 the first encountered in libc.lib and it has other symbols in it which will
3046 be pulled in by the link process. To avoid this, adding -u with the entry
3047 point name specified forces the correct .obj to be used. We can avoid
3048 making the user do this by always adding the entry point name as an
3049 undefined symbol. */
3052 lang_add_entry (name
, cmdline
)
3056 if (entry_symbol
== NULL
3058 || ! entry_from_cmdline
)
3060 entry_symbol
= name
;
3061 entry_from_cmdline
= cmdline
;
3064 /* don't do this yet. It seems to work (the executables run), but the
3065 image created is very different from what I was getting before indicating
3066 that something else is being pulled in. When everything else is working,
3067 then try to put this back in to see if it will do the right thing for
3068 other more complicated applications */
3069 ldlang_add_undef (name
);
3074 lang_add_target (name
)
3077 lang_target_statement_type
*new = new_stat (lang_target_statement
,
3093 map_option_f
= true;
3104 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
3111 lang_add_data (type
, exp
)
3113 union etree_union
*exp
;
3116 lang_data_statement_type
*new = new_stat (lang_data_statement
,
3124 /* Create a new reloc statement. RELOC is the BFD relocation type to
3125 generate. HOWTO is the corresponding howto structure (we could
3126 look this up, but the caller has already done so). SECTION is the
3127 section to generate a reloc against, or NAME is the name of the
3128 symbol to generate a reloc against. Exactly one of SECTION and
3129 NAME must be NULL. ADDEND is an expression for the addend. */
3132 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
3133 bfd_reloc_code_real_type reloc
;
3134 reloc_howto_type
*howto
;
3137 union etree_union
*addend
;
3139 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
3143 p
->section
= section
;
3145 p
->addend_exp
= addend
;
3147 p
->addend_value
= 0;
3148 p
->output_section
= NULL
;
3153 lang_add_assignment (exp
)
3156 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
3163 lang_add_attribute (attribute
)
3164 enum statement_enum attribute
;
3166 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
3173 if (startup_file
!= (char *) NULL
)
3175 einfo ("%P%Fmultiple STARTUP files\n");
3177 first_file
->filename
= name
;
3178 first_file
->local_sym_name
= name
;
3179 first_file
->real
= true;
3181 startup_file
= name
;
3188 lang_float_flag
= maybe
;
3192 lang_leave_output_section_statement (fill
, memspec
)
3194 CONST
char *memspec
;
3196 current_section
->fill
= fill
;
3197 current_section
->region
= lang_memory_region_lookup (memspec
);
3198 stat_ptr
= &statement_list
;
3202 Create an absolute symbol with the given name with the value of the
3203 address of first byte of the section named.
3205 If the symbol already exists, then do nothing.
3208 lang_abs_symbol_at_beginning_of (secname
, name
)
3209 const char *secname
;
3212 struct bfd_link_hash_entry
*h
;
3214 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3215 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3216 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3218 if (h
->type
== bfd_link_hash_new
3219 || h
->type
== bfd_link_hash_undefined
)
3223 h
->type
= bfd_link_hash_defined
;
3225 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3226 if (sec
== (asection
*) NULL
)
3229 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
3231 h
->u
.def
.section
= bfd_abs_section_ptr
;
3236 Create an absolute symbol with the given name with the value of the
3237 address of the first byte after the end of the section named.
3239 If the symbol already exists, then do nothing.
3242 lang_abs_symbol_at_end_of (secname
, name
)
3243 const char *secname
;
3246 struct bfd_link_hash_entry
*h
;
3248 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3249 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3250 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3252 if (h
->type
== bfd_link_hash_new
3253 || h
->type
== bfd_link_hash_undefined
)
3257 h
->type
= bfd_link_hash_defined
;
3259 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3260 if (sec
== (asection
*) NULL
)
3263 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
3264 + bfd_section_size (output_bfd
, sec
));
3266 h
->u
.def
.section
= bfd_abs_section_ptr
;
3271 lang_statement_append (list
, element
, field
)
3272 lang_statement_list_type
* list
;
3273 lang_statement_union_type
* element
;
3274 lang_statement_union_type
** field
;
3276 *(list
->tail
) = element
;
3280 /* Set the output format type. -oformat overrides scripts. */
3283 lang_add_output_format (format
, big
, little
, from_script
)
3289 if (output_target
== NULL
|| !from_script
)
3291 if (command_line
.endian
== ENDIAN_BIG
3294 else if (command_line
.endian
== ENDIAN_LITTLE
3298 output_target
= format
;
3302 /* Enter a group. This creates a new lang_group_statement, and sets
3303 stat_ptr to build new statements within the group. */
3308 lang_group_statement_type
*g
;
3310 g
= new_stat (lang_group_statement
, stat_ptr
);
3311 lang_list_init (&g
->children
);
3312 stat_ptr
= &g
->children
;
3315 /* Leave a group. This just resets stat_ptr to start writing to the
3316 regular list of statements again. Note that this will not work if
3317 groups can occur inside anything else which can adjust stat_ptr,
3318 but currently they can't. */
3323 stat_ptr
= &statement_list
;
3326 /* Add a new program header. This is called for each entry in a PHDRS
3327 command in a linker script. */
3330 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
3338 struct lang_phdr
*n
, **pp
;
3340 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
3343 n
->type
= exp_get_value_int (type
, 0, "program header type",
3344 lang_final_phase_enum
);
3345 n
->filehdr
= filehdr
;
3350 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3355 /* Record that a section should be placed in a phdr. */
3358 lang_section_in_phdr (name
)
3361 struct lang_output_section_phdr_list
*n
;
3363 n
= ((struct lang_output_section_phdr_list
*)
3364 stat_alloc (sizeof (struct lang_output_section_phdr_list
)));
3367 n
->next
= current_section
->phdrs
;
3368 current_section
->phdrs
= n
;
3371 /* Record the program header information in the output BFD. FIXME: We
3372 should not be calling an ELF specific function here. */
3375 lang_record_phdrs ()
3379 struct lang_output_section_phdr_list
*last
;
3380 struct lang_phdr
*l
;
3381 lang_statement_union_type
*u
;
3384 secs
= xmalloc (alc
* sizeof (asection
*));
3386 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
3393 for (u
= lang_output_section_statement
.head
;
3395 u
= u
->output_section_statement
.next
)
3397 lang_output_section_statement_type
*os
;
3398 struct lang_output_section_phdr_list
*pl
;
3400 os
= &u
->output_section_statement
;
3408 || os
->bfd_section
== NULL
3409 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
3414 if (os
->bfd_section
== NULL
)
3417 for (; pl
!= NULL
; pl
= pl
->next
)
3419 if (strcmp (pl
->name
, l
->name
) == 0)
3424 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
3426 secs
[c
] = os
->bfd_section
;
3433 if (l
->flags
== NULL
)
3436 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
3437 lang_final_phase_enum
);
3442 at
= exp_get_vma (l
->at
, 0, "phdr load address",
3443 lang_final_phase_enum
);
3445 if (! bfd_record_phdr (output_bfd
, l
->type
,
3446 l
->flags
== NULL
? false : true,
3448 l
->at
== NULL
? false : true,
3449 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
3450 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3455 /* Make sure all the phdr assignments succeeded. */
3456 for (u
= lang_output_section_statement
.head
;
3458 u
= u
->output_section_statement
.next
)
3460 struct lang_output_section_phdr_list
*pl
;
3462 if (u
->output_section_statement
.bfd_section
== NULL
)
3465 for (pl
= u
->output_section_statement
.phdrs
;
3468 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
3469 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3470 u
->output_section_statement
.name
, pl
->name
);